blob: 32512519e1acb99a424506ee08e362f6a212f3d9 [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
89struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100090 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +030091 struct list_head local_vcpus_link;
Avi Kivity313dbd42008-07-17 18:04:30 +030092 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040093 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030094 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020095 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040096 struct kvm_msr_entry *guest_msrs;
97 struct kvm_msr_entry *host_msrs;
98 int nmsrs;
99 int save_nmsrs;
100 int msr_offset_efer;
101#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300102 u64 msr_host_kernel_gs_base;
103 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400104#endif
105 struct vmcs *vmcs;
106 struct {
107 int loaded;
108 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200109 int gs_ldt_reload_needed;
110 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300111 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -0400112 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200113 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300114 int vm86_active;
115 u8 save_iopl;
116 struct kvm_save_segment {
117 u16 selector;
118 unsigned long base;
119 u32 limit;
120 u32 ar;
121 } tr, es, ds, fs, gs;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200122 struct {
123 bool pending;
124 u8 vector;
125 unsigned rip;
126 } irq;
127 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800128 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300129 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200130
131 /* Support for vnmi-less CPUs */
132 int soft_vnmi_blocked;
133 ktime_t entry_time;
134 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800135 u32 exit_reason;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400136};
137
138static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
139{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000140 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400141}
142
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800143static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800144static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300145
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146static DEFINE_PER_CPU(struct vmcs *, vmxarea);
147static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300148static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800149
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200150static unsigned long *vmx_io_bitmap_a;
151static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200152static unsigned long *vmx_msr_bitmap_legacy;
153static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300154
Sheng Yang2384d2b2008-01-17 15:14:33 +0800155static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
156static DEFINE_SPINLOCK(vmx_vpid_lock);
157
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300158static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800159 int size;
160 int order;
161 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300162 u32 pin_based_exec_ctrl;
163 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800164 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300165 u32 vmexit_ctrl;
166 u32 vmentry_ctrl;
167} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800168
Hannes Ederefff9e52008-11-28 17:02:06 +0100169static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800170 u32 ept;
171 u32 vpid;
172} vmx_capability;
173
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174#define VMX_SEGMENT_FIELD(seg) \
175 [VCPU_SREG_##seg] = { \
176 .selector = GUEST_##seg##_SELECTOR, \
177 .base = GUEST_##seg##_BASE, \
178 .limit = GUEST_##seg##_LIMIT, \
179 .ar_bytes = GUEST_##seg##_AR_BYTES, \
180 }
181
182static struct kvm_vmx_segment_field {
183 unsigned selector;
184 unsigned base;
185 unsigned limit;
186 unsigned ar_bytes;
187} kvm_vmx_segment_fields[] = {
188 VMX_SEGMENT_FIELD(CS),
189 VMX_SEGMENT_FIELD(DS),
190 VMX_SEGMENT_FIELD(ES),
191 VMX_SEGMENT_FIELD(FS),
192 VMX_SEGMENT_FIELD(GS),
193 VMX_SEGMENT_FIELD(SS),
194 VMX_SEGMENT_FIELD(TR),
195 VMX_SEGMENT_FIELD(LDTR),
196};
197
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300198static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
199
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300200/*
201 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
202 * away by decrementing the array size.
203 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800204static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800205#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300206 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800207#endif
208 MSR_EFER, MSR_K6_STAR,
209};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200210#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400212static void load_msrs(struct kvm_msr_entry *e, int n)
213{
214 int i;
215
216 for (i = 0; i < n; ++i)
217 wrmsrl(e[i].index, e[i].data);
218}
219
220static void save_msrs(struct kvm_msr_entry *e, int n)
221{
222 int i;
223
224 for (i = 0; i < n; ++i)
225 rdmsrl(e[i].index, e[i].data);
226}
227
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228static inline int is_page_fault(u32 intr_info)
229{
230 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
231 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100232 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800233}
234
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300235static inline int is_no_device(u32 intr_info)
236{
237 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
238 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100239 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300240}
241
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500242static inline int is_invalid_opcode(u32 intr_info)
243{
244 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
245 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100246 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500247}
248
Avi Kivity6aa8b732006-12-10 02:21:36 -0800249static inline int is_external_interrupt(u32 intr_info)
250{
251 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
252 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
253}
254
Andi Kleena0861c02009-06-08 17:37:09 +0800255static inline int is_machine_check(u32 intr_info)
256{
257 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
258 INTR_INFO_VALID_MASK)) ==
259 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
260}
261
Sheng Yang25c5f222008-03-28 13:18:56 +0800262static inline int cpu_has_vmx_msr_bitmap(void)
263{
Sheng Yang04547152009-04-01 15:52:31 +0800264 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800265}
266
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800267static inline int cpu_has_vmx_tpr_shadow(void)
268{
Sheng Yang04547152009-04-01 15:52:31 +0800269 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800270}
271
272static inline int vm_need_tpr_shadow(struct kvm *kvm)
273{
Sheng Yang04547152009-04-01 15:52:31 +0800274 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800275}
276
Sheng Yangf78e0e22007-10-29 09:40:42 +0800277static inline int cpu_has_secondary_exec_ctrls(void)
278{
Sheng Yang04547152009-04-01 15:52:31 +0800279 return vmcs_config.cpu_based_exec_ctrl &
280 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800281}
282
Avi Kivity774ead32007-12-26 13:57:04 +0200283static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800284{
Sheng Yang04547152009-04-01 15:52:31 +0800285 return vmcs_config.cpu_based_2nd_exec_ctrl &
286 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
287}
288
289static inline bool cpu_has_vmx_flexpriority(void)
290{
291 return cpu_has_vmx_tpr_shadow() &&
292 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800293}
294
Marcelo Tosattie7997942009-06-11 12:07:40 -0300295static inline bool cpu_has_vmx_ept_execute_only(void)
296{
297 return !!(vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT);
298}
299
300static inline bool cpu_has_vmx_eptp_uncacheable(void)
301{
302 return !!(vmx_capability.ept & VMX_EPTP_UC_BIT);
303}
304
305static inline bool cpu_has_vmx_eptp_writeback(void)
306{
307 return !!(vmx_capability.ept & VMX_EPTP_WB_BIT);
308}
309
310static inline bool cpu_has_vmx_ept_2m_page(void)
311{
312 return !!(vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT);
313}
314
Sheng Yangd56f5462008-04-25 10:13:16 +0800315static inline int cpu_has_vmx_invept_individual_addr(void)
316{
Sheng Yang04547152009-04-01 15:52:31 +0800317 return !!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800318}
319
320static inline int cpu_has_vmx_invept_context(void)
321{
Sheng Yang04547152009-04-01 15:52:31 +0800322 return !!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800323}
324
325static inline int cpu_has_vmx_invept_global(void)
326{
Sheng Yang04547152009-04-01 15:52:31 +0800327 return !!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800328}
329
330static inline int cpu_has_vmx_ept(void)
331{
Sheng Yang04547152009-04-01 15:52:31 +0800332 return vmcs_config.cpu_based_2nd_exec_ctrl &
333 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800334}
335
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700336static inline int cpu_has_vmx_unrestricted_guest(void)
337{
338 return vmcs_config.cpu_based_2nd_exec_ctrl &
339 SECONDARY_EXEC_UNRESTRICTED_GUEST;
340}
341
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800342static inline int cpu_has_vmx_ple(void)
343{
344 return vmcs_config.cpu_based_2nd_exec_ctrl &
345 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
346}
347
Sheng Yangf78e0e22007-10-29 09:40:42 +0800348static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
349{
Sheng Yang04547152009-04-01 15:52:31 +0800350 return flexpriority_enabled &&
351 (cpu_has_vmx_virtualize_apic_accesses()) &&
352 (irqchip_in_kernel(kvm));
Sheng Yangf78e0e22007-10-29 09:40:42 +0800353}
354
Sheng Yang2384d2b2008-01-17 15:14:33 +0800355static inline int cpu_has_vmx_vpid(void)
356{
Sheng Yang04547152009-04-01 15:52:31 +0800357 return vmcs_config.cpu_based_2nd_exec_ctrl &
358 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800359}
360
Sheng Yangf08864b2008-05-15 18:23:25 +0800361static inline int cpu_has_virtual_nmis(void)
362{
363 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
364}
365
Sheng Yang04547152009-04-01 15:52:31 +0800366static inline bool report_flexpriority(void)
367{
368 return flexpriority_enabled;
369}
370
Rusty Russell8b9cf982007-07-30 16:31:43 +1000371static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800372{
373 int i;
374
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400375 for (i = 0; i < vmx->nmsrs; ++i)
376 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300377 return i;
378 return -1;
379}
380
Sheng Yang2384d2b2008-01-17 15:14:33 +0800381static inline void __invvpid(int ext, u16 vpid, gva_t gva)
382{
383 struct {
384 u64 vpid : 16;
385 u64 rsvd : 48;
386 u64 gva;
387 } operand = { vpid, 0, gva };
388
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300389 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800390 /* CF==1 or ZF==1 --> rc = -1 */
391 "; ja 1f ; ud2 ; 1:"
392 : : "a"(&operand), "c"(ext) : "cc", "memory");
393}
394
Sheng Yang14394422008-04-28 12:24:45 +0800395static inline void __invept(int ext, u64 eptp, gpa_t gpa)
396{
397 struct {
398 u64 eptp, gpa;
399 } operand = {eptp, gpa};
400
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300401 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800402 /* CF==1 or ZF==1 --> rc = -1 */
403 "; ja 1f ; ud2 ; 1:\n"
404 : : "a" (&operand), "c" (ext) : "cc", "memory");
405}
406
Rusty Russell8b9cf982007-07-30 16:31:43 +1000407static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300408{
409 int i;
410
Rusty Russell8b9cf982007-07-30 16:31:43 +1000411 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300412 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400413 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000414 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800415}
416
Avi Kivity6aa8b732006-12-10 02:21:36 -0800417static void vmcs_clear(struct vmcs *vmcs)
418{
419 u64 phys_addr = __pa(vmcs);
420 u8 error;
421
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300422 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800423 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
424 : "cc", "memory");
425 if (error)
426 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
427 vmcs, phys_addr);
428}
429
430static void __vcpu_clear(void *arg)
431{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000432 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800433 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800434
Rusty Russell8b9cf982007-07-30 16:31:43 +1000435 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400436 vmcs_clear(vmx->vmcs);
437 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800439 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300440 list_del(&vmx->local_vcpus_link);
441 vmx->vcpu.cpu = -1;
442 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443}
444
Rusty Russell8b9cf982007-07-30 16:31:43 +1000445static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800446{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200447 if (vmx->vcpu.cpu == -1)
448 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200449 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800450}
451
Sheng Yang2384d2b2008-01-17 15:14:33 +0800452static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
453{
454 if (vmx->vpid == 0)
455 return;
456
457 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
458}
459
Sheng Yang14394422008-04-28 12:24:45 +0800460static inline void ept_sync_global(void)
461{
462 if (cpu_has_vmx_invept_global())
463 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
464}
465
466static inline void ept_sync_context(u64 eptp)
467{
Avi Kivity089d0342009-03-23 18:26:32 +0200468 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800469 if (cpu_has_vmx_invept_context())
470 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
471 else
472 ept_sync_global();
473 }
474}
475
476static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
477{
Avi Kivity089d0342009-03-23 18:26:32 +0200478 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800479 if (cpu_has_vmx_invept_individual_addr())
480 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
481 eptp, gpa);
482 else
483 ept_sync_context(eptp);
484 }
485}
486
Avi Kivity6aa8b732006-12-10 02:21:36 -0800487static unsigned long vmcs_readl(unsigned long field)
488{
489 unsigned long value;
490
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300491 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800492 : "=a"(value) : "d"(field) : "cc");
493 return value;
494}
495
496static u16 vmcs_read16(unsigned long field)
497{
498 return vmcs_readl(field);
499}
500
501static u32 vmcs_read32(unsigned long field)
502{
503 return vmcs_readl(field);
504}
505
506static u64 vmcs_read64(unsigned long field)
507{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800508#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800509 return vmcs_readl(field);
510#else
511 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
512#endif
513}
514
Avi Kivitye52de1b2007-01-05 16:36:56 -0800515static noinline void vmwrite_error(unsigned long field, unsigned long value)
516{
517 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
518 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
519 dump_stack();
520}
521
Avi Kivity6aa8b732006-12-10 02:21:36 -0800522static void vmcs_writel(unsigned long field, unsigned long value)
523{
524 u8 error;
525
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300526 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400527 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800528 if (unlikely(error))
529 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530}
531
532static void vmcs_write16(unsigned long field, u16 value)
533{
534 vmcs_writel(field, value);
535}
536
537static void vmcs_write32(unsigned long field, u32 value)
538{
539 vmcs_writel(field, value);
540}
541
542static void vmcs_write64(unsigned long field, u64 value)
543{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800544 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300545#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800546 asm volatile ("");
547 vmcs_writel(field+1, value >> 32);
548#endif
549}
550
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300551static void vmcs_clear_bits(unsigned long field, u32 mask)
552{
553 vmcs_writel(field, vmcs_readl(field) & ~mask);
554}
555
556static void vmcs_set_bits(unsigned long field, u32 mask)
557{
558 vmcs_writel(field, vmcs_readl(field) | mask);
559}
560
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300561static void update_exception_bitmap(struct kvm_vcpu *vcpu)
562{
563 u32 eb;
564
Andi Kleena0861c02009-06-08 17:37:09 +0800565 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300566 if (!vcpu->fpu_active)
567 eb |= 1u << NM_VECTOR;
Avi Kivitye8a48342009-09-01 16:06:25 +0300568 /*
569 * Unconditionally intercept #DB so we can maintain dr6 without
570 * reading it every exit.
571 */
572 eb |= 1u << DB_VECTOR;
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100573 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100574 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
575 eb |= 1u << BP_VECTOR;
576 }
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300577 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300578 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200579 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800580 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300581 vmcs_write32(EXCEPTION_BITMAP, eb);
582}
583
Avi Kivity33ed6322007-05-02 16:54:03 +0300584static void reload_tss(void)
585{
Avi Kivity33ed6322007-05-02 16:54:03 +0300586 /*
587 * VT restores TR but not its size. Useless.
588 */
589 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200590 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300591
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300592 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300593 descs = (void *)gdt.base;
594 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
595 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300596}
597
Rusty Russell8b9cf982007-07-30 16:31:43 +1000598static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300599{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400600 int efer_offset = vmx->msr_offset_efer;
Roel Kluin3a34a882009-08-04 02:08:45 -0700601 u64 host_efer;
602 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300603 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300604
Avi Kivity51c6cf62007-08-29 03:48:05 +0300605 if (efer_offset < 0)
606 return;
Roel Kluin3a34a882009-08-04 02:08:45 -0700607 host_efer = vmx->host_msrs[efer_offset].data;
608 guest_efer = vmx->guest_msrs[efer_offset].data;
609
Avi Kivity51c6cf62007-08-29 03:48:05 +0300610 /*
611 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
612 * outside long mode
613 */
614 ignore_bits = EFER_NX | EFER_SCE;
615#ifdef CONFIG_X86_64
616 ignore_bits |= EFER_LMA | EFER_LME;
617 /* SCE is meaningful only in long mode on Intel */
618 if (guest_efer & EFER_LMA)
619 ignore_bits &= ~(u64)EFER_SCE;
620#endif
621 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
622 return;
623
624 vmx->host_state.guest_efer_loaded = 1;
625 guest_efer &= ~ignore_bits;
626 guest_efer |= host_efer & ignore_bits;
627 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000628 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300629}
630
Avi Kivity51c6cf62007-08-29 03:48:05 +0300631static void reload_host_efer(struct vcpu_vmx *vmx)
632{
633 if (vmx->host_state.guest_efer_loaded) {
634 vmx->host_state.guest_efer_loaded = 0;
635 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
636 }
637}
638
Avi Kivity04d2cc72007-09-10 18:10:54 +0300639static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300640{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300641 struct vcpu_vmx *vmx = to_vmx(vcpu);
642
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400643 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300644 return;
645
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400646 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300647 /*
648 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
649 * allow segment selectors with cpl > 0 or ti == 1.
650 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300651 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200652 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300653 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200654 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400655 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200656 vmx->host_state.fs_reload_needed = 0;
657 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300658 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200659 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300660 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300661 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400662 if (!(vmx->host_state.gs_sel & 7))
663 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300664 else {
665 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200666 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300667 }
668
669#ifdef CONFIG_X86_64
670 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
671 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
672#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400673 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
674 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300675#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300676
677#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300678 if (is_long_mode(&vmx->vcpu)) {
679 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
680 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
681 }
Avi Kivity707c0872007-05-02 17:33:43 +0300682#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400683 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300684 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300685}
686
Avi Kivitya9b21b62008-06-24 11:48:49 +0300687static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300688{
Avi Kivity15ad7142007-07-11 18:17:21 +0300689 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300690
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400691 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300692 return;
693
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200694 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400695 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200696 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300697 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200698 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300699 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300700 /*
701 * If we have to reload gs, we must take care to
702 * preserve our gs base.
703 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300704 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300705 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300706#ifdef CONFIG_X86_64
707 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
708#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300709 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300710 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200711 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400712 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
713 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300714 reload_host_efer(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +0300715#ifdef CONFIG_X86_64
716 if (is_long_mode(&vmx->vcpu)) {
717 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
718 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
719 }
720#endif
Avi Kivity33ed6322007-05-02 16:54:03 +0300721}
722
Avi Kivitya9b21b62008-06-24 11:48:49 +0300723static void vmx_load_host_state(struct vcpu_vmx *vmx)
724{
725 preempt_disable();
726 __vmx_load_host_state(vmx);
727 preempt_enable();
728}
729
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730/*
731 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
732 * vcpu mutex is already taken.
733 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300734static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400736 struct vcpu_vmx *vmx = to_vmx(vcpu);
737 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200738 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739
Eddie Donga3d7f852007-09-03 16:15:12 +0300740 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000741 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300742 kvm_migrate_timers(vcpu);
Marcelo Tosattieb5109e2009-10-01 19:16:58 -0300743 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
Avi Kivity543e4242008-05-13 16:22:47 +0300744 local_irq_disable();
745 list_add(&vmx->local_vcpus_link,
746 &per_cpu(vcpus_on_cpu, cpu));
747 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300748 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400750 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751 u8 error;
752
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400753 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300754 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
756 : "cc");
757 if (error)
758 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400759 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760 }
761
762 if (vcpu->cpu != cpu) {
763 struct descriptor_table dt;
764 unsigned long sysenter_esp;
765
766 vcpu->cpu = cpu;
767 /*
768 * Linux uses per-cpu TSS and GDT, so set these when switching
769 * processors.
770 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300771 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
772 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
774
775 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
776 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300777
778 /*
779 * Make sure the time stamp counter is monotonous.
780 */
781 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200782 if (tsc_this < vcpu->arch.host_tsc) {
783 delta = vcpu->arch.host_tsc - tsc_this;
784 new_offset = vmcs_read64(TSC_OFFSET) + delta;
785 vmcs_write64(TSC_OFFSET, new_offset);
786 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800787 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788}
789
790static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
791{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300792 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793}
794
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300795static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
796{
797 if (vcpu->fpu_active)
798 return;
799 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000800 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800801 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92fa2007-07-17 23:19:08 +1000802 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300803 update_exception_bitmap(vcpu);
804}
805
806static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
807{
808 if (!vcpu->fpu_active)
809 return;
810 vcpu->fpu_active = 0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000811 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300812 update_exception_bitmap(vcpu);
813}
814
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
816{
Avi Kivity345dcaa2009-08-12 15:29:37 +0300817 unsigned long rflags;
818
819 rflags = vmcs_readl(GUEST_RFLAGS);
820 if (to_vmx(vcpu)->rmode.vm86_active)
821 rflags &= ~(unsigned long)(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
822 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823}
824
825static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
826{
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300827 if (to_vmx(vcpu)->rmode.vm86_active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100828 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829 vmcs_writel(GUEST_RFLAGS, rflags);
830}
831
Glauber Costa2809f5d2009-05-12 16:21:05 -0400832static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
833{
834 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
835 int ret = 0;
836
837 if (interruptibility & GUEST_INTR_STATE_STI)
838 ret |= X86_SHADOW_INT_STI;
839 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
840 ret |= X86_SHADOW_INT_MOV_SS;
841
842 return ret & mask;
843}
844
845static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
846{
847 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
848 u32 interruptibility = interruptibility_old;
849
850 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
851
852 if (mask & X86_SHADOW_INT_MOV_SS)
853 interruptibility |= GUEST_INTR_STATE_MOV_SS;
854 if (mask & X86_SHADOW_INT_STI)
855 interruptibility |= GUEST_INTR_STATE_STI;
856
857 if ((interruptibility != interruptibility_old))
858 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
859}
860
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
862{
863 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300865 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300867 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868
Glauber Costa2809f5d2009-05-12 16:21:05 -0400869 /* skipping an emulated instruction also counts */
870 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871}
872
Avi Kivity298101d2007-11-25 13:41:11 +0200873static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
874 bool has_error_code, u32 error_code)
875{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200876 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100877 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200878
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100879 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200880 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100881 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
882 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200883
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300884 if (vmx->rmode.vm86_active) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200885 vmx->rmode.irq.pending = true;
886 vmx->rmode.irq.vector = nr;
887 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +0300888 if (kvm_exception_is_soft(nr))
889 vmx->rmode.irq.rip +=
890 vmx->vcpu.arch.event_exit_inst_len;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100891 intr_info |= INTR_TYPE_SOFT_INTR;
892 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200893 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
894 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
895 return;
896 }
897
Gleb Natapov66fd3f72009-05-11 13:35:50 +0300898 if (kvm_exception_is_soft(nr)) {
899 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
900 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100901 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
902 } else
903 intr_info |= INTR_TYPE_HARD_EXCEPTION;
904
905 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200906}
907
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908/*
Eddie Donga75beee2007-05-17 18:55:15 +0300909 * Swap MSR entry in host/guest MSR entry array.
910 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200911#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000912static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300913{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400914 struct kvm_msr_entry tmp;
915
916 tmp = vmx->guest_msrs[to];
917 vmx->guest_msrs[to] = vmx->guest_msrs[from];
918 vmx->guest_msrs[from] = tmp;
919 tmp = vmx->host_msrs[to];
920 vmx->host_msrs[to] = vmx->host_msrs[from];
921 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300922}
Gabriel C54e11fa2007-08-01 16:23:10 +0200923#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300924
925/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300926 * Set up the vmcs to automatically save and restore system
927 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
928 * mode, as fiddling with msrs is very expensive.
929 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000930static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300931{
Eddie Dong2cc51562007-05-21 07:28:09 +0300932 int save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +0200933 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +0300934
Avi Kivity33f9c502008-02-27 16:06:57 +0200935 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300936 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300937#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000938 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300939 int index;
940
Rusty Russell8b9cf982007-07-30 16:31:43 +1000941 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300942 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000943 move_msr_up(vmx, index, save_nmsrs++);
944 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300945 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000946 move_msr_up(vmx, index, save_nmsrs++);
947 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300948 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000949 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300950 /*
951 * MSR_K6_STAR is only needed on long mode guests, and only
952 * if efer.sce is enabled.
953 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000954 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800955 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000956 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300957 }
Eddie Donga75beee2007-05-17 18:55:15 +0300958#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400959 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300960
Rusty Russell8b9cf982007-07-30 16:31:43 +1000961 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivity58972972009-02-24 22:26:47 +0200962
963 if (cpu_has_vmx_msr_bitmap()) {
964 if (is_long_mode(&vmx->vcpu))
965 msr_bitmap = vmx_msr_bitmap_longmode;
966 else
967 msr_bitmap = vmx_msr_bitmap_legacy;
968
969 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
970 }
Avi Kivitye38aea32007-04-19 13:22:48 +0300971}
972
973/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 * reads and returns guest's timestamp counter "register"
975 * guest_tsc = host_tsc + tsc_offset -- 21.3
976 */
977static u64 guest_read_tsc(void)
978{
979 u64 host_tsc, tsc_offset;
980
981 rdtscll(host_tsc);
982 tsc_offset = vmcs_read64(TSC_OFFSET);
983 return host_tsc + tsc_offset;
984}
985
986/*
987 * writes 'guest_tsc' into guest's timestamp counter "register"
988 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
989 */
Marcelo Tosatti53f658b32008-12-11 20:45:05 +0100990static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
993}
994
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995/*
996 * Reads an msr value (of 'msr_index') into 'pdata'.
997 * Returns 0 on success, non-0 otherwise.
998 * Assumes vcpu_load() was already called.
999 */
1000static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1001{
1002 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001003 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004
1005 if (!pdata) {
1006 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
1007 return -EINVAL;
1008 }
1009
1010 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001011#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012 case MSR_FS_BASE:
1013 data = vmcs_readl(GUEST_FS_BASE);
1014 break;
1015 case MSR_GS_BASE:
1016 data = vmcs_readl(GUEST_GS_BASE);
1017 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001018 case MSR_KERNEL_GS_BASE:
1019 vmx_load_host_state(to_vmx(vcpu));
1020 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
1021 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001023 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024#endif
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301025 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026 data = guest_read_tsc();
1027 break;
1028 case MSR_IA32_SYSENTER_CS:
1029 data = vmcs_read32(GUEST_SYSENTER_CS);
1030 break;
1031 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001032 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 break;
1034 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001035 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001038 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001039 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001040 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity3bab1f52006-12-29 16:49:48 -08001041 data = msr->data;
1042 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001044 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001045 }
1046
1047 *pdata = data;
1048 return 0;
1049}
1050
1051/*
1052 * Writes msr value into into the appropriate "register".
1053 * Returns 0 on success, non-0 otherwise.
1054 * Assumes vcpu_load() was already called.
1055 */
1056static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1057{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001058 struct vcpu_vmx *vmx = to_vmx(vcpu);
1059 struct kvm_msr_entry *msr;
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01001060 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +03001061 int ret = 0;
1062
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001064 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001065 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001066 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001067 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001068#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 case MSR_FS_BASE:
1070 vmcs_writel(GUEST_FS_BASE, data);
1071 break;
1072 case MSR_GS_BASE:
1073 vmcs_writel(GUEST_GS_BASE, data);
1074 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001075 case MSR_KERNEL_GS_BASE:
1076 vmx_load_host_state(vmx);
1077 vmx->msr_guest_kernel_gs_base = data;
1078 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079#endif
1080 case MSR_IA32_SYSENTER_CS:
1081 vmcs_write32(GUEST_SYSENTER_CS, data);
1082 break;
1083 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001084 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085 break;
1086 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001087 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301089 case MSR_IA32_TSC:
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01001090 rdtscll(host_tsc);
1091 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001093 case MSR_IA32_CR_PAT:
1094 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1095 vmcs_write64(GUEST_IA32_PAT, data);
1096 vcpu->arch.pat = data;
1097 break;
1098 }
1099 /* Otherwise falls through to kvm_set_msr_common */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001101 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001102 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001103 vmx_load_host_state(vmx);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001104 msr->data = data;
1105 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001107 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 }
1109
Eddie Dong2cc51562007-05-21 07:28:09 +03001110 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111}
1112
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001113static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001115 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1116 switch (reg) {
1117 case VCPU_REGS_RSP:
1118 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1119 break;
1120 case VCPU_REGS_RIP:
1121 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1122 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001123 case VCPU_EXREG_PDPTR:
1124 if (enable_ept)
1125 ept_save_pdptrs(vcpu);
1126 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001127 default:
1128 break;
1129 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130}
1131
Jan Kiszka355be0b2009-10-03 00:31:21 +02001132static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133{
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001134 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1135 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1136 else
1137 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1138
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001139 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001140}
1141
1142static __init int cpu_has_kvm_support(void)
1143{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001144 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145}
1146
1147static __init int vmx_disabled_by_bios(void)
1148{
1149 u64 msr;
1150
1151 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001152 return (msr & (FEATURE_CONTROL_LOCKED |
1153 FEATURE_CONTROL_VMXON_ENABLED))
1154 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001155 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156}
1157
Alexander Graf10474ae2009-09-15 11:37:46 +02001158static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159{
1160 int cpu = raw_smp_processor_id();
1161 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1162 u64 old;
1163
Alexander Graf10474ae2009-09-15 11:37:46 +02001164 if (read_cr4() & X86_CR4_VMXE)
1165 return -EBUSY;
1166
Avi Kivity543e4242008-05-13 16:22:47 +03001167 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001169 if ((old & (FEATURE_CONTROL_LOCKED |
1170 FEATURE_CONTROL_VMXON_ENABLED))
1171 != (FEATURE_CONTROL_LOCKED |
1172 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001174 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001175 FEATURE_CONTROL_LOCKED |
1176 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001177 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001178 asm volatile (ASM_VMX_VMXON_RAX
1179 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180 : "memory", "cc");
Alexander Graf10474ae2009-09-15 11:37:46 +02001181
1182 ept_sync_global();
1183
1184 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185}
1186
Avi Kivity543e4242008-05-13 16:22:47 +03001187static void vmclear_local_vcpus(void)
1188{
1189 int cpu = raw_smp_processor_id();
1190 struct vcpu_vmx *vmx, *n;
1191
1192 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1193 local_vcpus_link)
1194 __vcpu_clear(vmx);
1195}
1196
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001197
1198/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1199 * tricks.
1200 */
1201static void kvm_cpu_vmxoff(void)
1202{
1203 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1204 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1205}
1206
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207static void hardware_disable(void *garbage)
1208{
Avi Kivity543e4242008-05-13 16:22:47 +03001209 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001210 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001211}
1212
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001213static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001214 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001215{
1216 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001217 u32 ctl = ctl_min | ctl_opt;
1218
1219 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1220
1221 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1222 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1223
1224 /* Ensure minimum (required) set of control bits are supported. */
1225 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001226 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001227
1228 *result = ctl;
1229 return 0;
1230}
1231
Yang, Sheng002c7f72007-07-31 14:23:01 +03001232static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001233{
1234 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001235 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001236 u32 _pin_based_exec_control = 0;
1237 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001238 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001239 u32 _vmexit_control = 0;
1240 u32 _vmentry_control = 0;
1241
1242 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001243 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001244 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1245 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001246 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001247
1248 min = CPU_BASED_HLT_EXITING |
1249#ifdef CONFIG_X86_64
1250 CPU_BASED_CR8_LOAD_EXITING |
1251 CPU_BASED_CR8_STORE_EXITING |
1252#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001253 CPU_BASED_CR3_LOAD_EXITING |
1254 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001255 CPU_BASED_USE_IO_BITMAPS |
1256 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001257 CPU_BASED_USE_TSC_OFFSETING |
1258 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001259 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001260 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001261 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001262 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1263 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001264 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001265#ifdef CONFIG_X86_64
1266 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1267 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1268 ~CPU_BASED_CR8_STORE_EXITING;
1269#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001270 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001271 min2 = 0;
1272 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001273 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001274 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001275 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001276 SECONDARY_EXEC_UNRESTRICTED_GUEST |
1277 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08001278 if (adjust_vmx_controls(min2, opt2,
1279 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001280 &_cpu_based_2nd_exec_control) < 0)
1281 return -EIO;
1282 }
1283#ifndef CONFIG_X86_64
1284 if (!(_cpu_based_2nd_exec_control &
1285 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1286 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1287#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001288 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001289 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1290 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03001291 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
1292 CPU_BASED_CR3_STORE_EXITING |
1293 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001294 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1295 vmx_capability.ept, vmx_capability.vpid);
1296 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001297
1298 min = 0;
1299#ifdef CONFIG_X86_64
1300 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1301#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001302 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001303 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1304 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001305 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001306
Sheng Yang468d4722008-10-09 16:01:55 +08001307 min = 0;
1308 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001309 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1310 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001311 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001312
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001313 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001314
1315 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1316 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001317 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001318
1319#ifdef CONFIG_X86_64
1320 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1321 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001322 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001323#endif
1324
1325 /* Require Write-Back (WB) memory type for VMCS accesses. */
1326 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001327 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001328
Yang, Sheng002c7f72007-07-31 14:23:01 +03001329 vmcs_conf->size = vmx_msr_high & 0x1fff;
1330 vmcs_conf->order = get_order(vmcs_config.size);
1331 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001332
Yang, Sheng002c7f72007-07-31 14:23:01 +03001333 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1334 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001335 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001336 vmcs_conf->vmexit_ctrl = _vmexit_control;
1337 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001338
1339 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001340}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001341
1342static struct vmcs *alloc_vmcs_cpu(int cpu)
1343{
1344 int node = cpu_to_node(cpu);
1345 struct page *pages;
1346 struct vmcs *vmcs;
1347
Mel Gorman6484eb32009-06-16 15:31:54 -07001348 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001349 if (!pages)
1350 return NULL;
1351 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001352 memset(vmcs, 0, vmcs_config.size);
1353 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001354 return vmcs;
1355}
1356
1357static struct vmcs *alloc_vmcs(void)
1358{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001359 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360}
1361
1362static void free_vmcs(struct vmcs *vmcs)
1363{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001364 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001365}
1366
Sam Ravnborg39959582007-06-01 00:47:13 -07001367static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368{
1369 int cpu;
1370
Zachary Amsden3230bb42009-09-29 11:38:37 -10001371 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10001373 per_cpu(vmxarea, cpu) = NULL;
1374 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375}
1376
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377static __init int alloc_kvm_area(void)
1378{
1379 int cpu;
1380
Zachary Amsden3230bb42009-09-29 11:38:37 -10001381 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382 struct vmcs *vmcs;
1383
1384 vmcs = alloc_vmcs_cpu(cpu);
1385 if (!vmcs) {
1386 free_kvm_area();
1387 return -ENOMEM;
1388 }
1389
1390 per_cpu(vmxarea, cpu) = vmcs;
1391 }
1392 return 0;
1393}
1394
1395static __init int hardware_setup(void)
1396{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001397 if (setup_vmcs_config(&vmcs_config) < 0)
1398 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001399
1400 if (boot_cpu_has(X86_FEATURE_NX))
1401 kvm_enable_efer_bits(EFER_NX);
1402
Sheng Yang93ba03c2009-04-01 15:52:32 +08001403 if (!cpu_has_vmx_vpid())
1404 enable_vpid = 0;
1405
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001406 if (!cpu_has_vmx_ept()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001407 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001408 enable_unrestricted_guest = 0;
1409 }
1410
1411 if (!cpu_has_vmx_unrestricted_guest())
1412 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001413
1414 if (!cpu_has_vmx_flexpriority())
1415 flexpriority_enabled = 0;
1416
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001417 if (!cpu_has_vmx_tpr_shadow())
1418 kvm_x86_ops->update_cr8_intercept = NULL;
1419
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001420 if (enable_ept && !cpu_has_vmx_ept_2m_page())
1421 kvm_disable_largepages();
1422
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001423 if (!cpu_has_vmx_ple())
1424 ple_gap = 0;
1425
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426 return alloc_kvm_area();
1427}
1428
1429static __exit void hardware_unsetup(void)
1430{
1431 free_kvm_area();
1432}
1433
Avi Kivity6aa8b732006-12-10 02:21:36 -08001434static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1435{
1436 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1437
Avi Kivity6af11b92007-03-19 13:18:10 +02001438 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439 vmcs_write16(sf->selector, save->selector);
1440 vmcs_writel(sf->base, save->base);
1441 vmcs_write32(sf->limit, save->limit);
1442 vmcs_write32(sf->ar_bytes, save->ar);
1443 } else {
1444 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1445 << AR_DPL_SHIFT;
1446 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1447 }
1448}
1449
1450static void enter_pmode(struct kvm_vcpu *vcpu)
1451{
1452 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001453 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001455 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001456 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001458 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
1459 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
1460 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001461
1462 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001463 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001464 flags |= (vmx->rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001465 vmcs_writel(GUEST_RFLAGS, flags);
1466
Rusty Russell66aee912007-07-17 23:34:16 +10001467 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1468 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001469
1470 update_exception_bitmap(vcpu);
1471
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001472 if (emulate_invalid_guest_state)
1473 return;
1474
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001475 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
1476 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
1477 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
1478 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479
1480 vmcs_write16(GUEST_SS_SELECTOR, 0);
1481 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1482
1483 vmcs_write16(GUEST_CS_SELECTOR,
1484 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1485 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1486}
1487
Mike Dayd77c26f2007-10-08 09:02:08 -04001488static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001489{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001490 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001491 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1492 kvm->memslots[0].npages - 3;
1493 return base_gfn << PAGE_SHIFT;
1494 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001495 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496}
1497
1498static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1499{
1500 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1501
1502 save->selector = vmcs_read16(sf->selector);
1503 save->base = vmcs_readl(sf->base);
1504 save->limit = vmcs_read32(sf->limit);
1505 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001506 vmcs_write16(sf->selector, save->base >> 4);
1507 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508 vmcs_write32(sf->limit, 0xffff);
1509 vmcs_write32(sf->ar_bytes, 0xf3);
1510}
1511
1512static void enter_rmode(struct kvm_vcpu *vcpu)
1513{
1514 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001515 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001517 if (enable_unrestricted_guest)
1518 return;
1519
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001520 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001521 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001523 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1525
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001526 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1528
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001529 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001530 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1531
1532 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001533 vmx->rmode.save_iopl
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001534 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001535
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001536 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537
1538 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001539 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001540 update_exception_bitmap(vcpu);
1541
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001542 if (emulate_invalid_guest_state)
1543 goto continue_rmode;
1544
Avi Kivity6aa8b732006-12-10 02:21:36 -08001545 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1546 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1547 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1548
1549 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001550 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001551 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1552 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001553 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1554
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001555 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
1556 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
1557 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
1558 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001559
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001560continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001561 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001562 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001563}
1564
Amit Shah401d10d2009-02-20 22:53:37 +05301565static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1566{
1567 struct vcpu_vmx *vmx = to_vmx(vcpu);
1568 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1569
Avi Kivity44ea2b12009-09-06 15:55:37 +03001570 /*
1571 * Force kernel_gs_base reloading before EFER changes, as control
1572 * of this msr depends on is_long_mode().
1573 */
1574 vmx_load_host_state(to_vmx(vcpu));
Amit Shah401d10d2009-02-20 22:53:37 +05301575 vcpu->arch.shadow_efer = efer;
1576 if (!msr)
1577 return;
1578 if (efer & EFER_LMA) {
1579 vmcs_write32(VM_ENTRY_CONTROLS,
1580 vmcs_read32(VM_ENTRY_CONTROLS) |
1581 VM_ENTRY_IA32E_MODE);
1582 msr->data = efer;
1583 } else {
1584 vmcs_write32(VM_ENTRY_CONTROLS,
1585 vmcs_read32(VM_ENTRY_CONTROLS) &
1586 ~VM_ENTRY_IA32E_MODE);
1587
1588 msr->data = efer & ~EFER_LME;
1589 }
1590 setup_msrs(vmx);
1591}
1592
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001593#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594
1595static void enter_lmode(struct kvm_vcpu *vcpu)
1596{
1597 u32 guest_tr_ar;
1598
1599 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1600 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1601 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001602 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603 vmcs_write32(GUEST_TR_AR_BYTES,
1604 (guest_tr_ar & ~AR_TYPE_MASK)
1605 | AR_TYPE_BUSY_64_TSS);
1606 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001607 vcpu->arch.shadow_efer |= EFER_LMA;
Amit Shah401d10d2009-02-20 22:53:37 +05301608 vmx_set_efer(vcpu, vcpu->arch.shadow_efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609}
1610
1611static void exit_lmode(struct kvm_vcpu *vcpu)
1612{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001613 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614
1615 vmcs_write32(VM_ENTRY_CONTROLS,
1616 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001617 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618}
1619
1620#endif
1621
Sheng Yang2384d2b2008-01-17 15:14:33 +08001622static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1623{
1624 vpid_sync_vcpu_all(to_vmx(vcpu));
Avi Kivity089d0342009-03-23 18:26:32 +02001625 if (enable_ept)
Sheng Yang4e1096d2008-07-06 19:16:51 +08001626 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001627}
1628
Anthony Liguori25c4c272007-04-27 09:29:21 +03001629static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001630{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001631 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1632 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001633}
1634
Sheng Yang14394422008-04-28 12:24:45 +08001635static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1636{
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001637 if (!test_bit(VCPU_EXREG_PDPTR,
1638 (unsigned long *)&vcpu->arch.regs_dirty))
1639 return;
1640
Sheng Yang14394422008-04-28 12:24:45 +08001641 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Sheng Yang14394422008-04-28 12:24:45 +08001642 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1643 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1644 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1645 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1646 }
1647}
1648
Avi Kivity8f5d5492009-05-31 18:41:29 +03001649static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
1650{
1651 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1652 vcpu->arch.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
1653 vcpu->arch.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
1654 vcpu->arch.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
1655 vcpu->arch.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
1656 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001657
1658 __set_bit(VCPU_EXREG_PDPTR,
1659 (unsigned long *)&vcpu->arch.regs_avail);
1660 __set_bit(VCPU_EXREG_PDPTR,
1661 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03001662}
1663
Sheng Yang14394422008-04-28 12:24:45 +08001664static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1665
1666static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1667 unsigned long cr0,
1668 struct kvm_vcpu *vcpu)
1669{
1670 if (!(cr0 & X86_CR0_PG)) {
1671 /* From paging/starting to nonpaging */
1672 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001673 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001674 (CPU_BASED_CR3_LOAD_EXITING |
1675 CPU_BASED_CR3_STORE_EXITING));
1676 vcpu->arch.cr0 = cr0;
1677 vmx_set_cr4(vcpu, vcpu->arch.cr4);
Sheng Yang14394422008-04-28 12:24:45 +08001678 } else if (!is_paging(vcpu)) {
1679 /* From nonpaging to paging */
1680 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001681 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001682 ~(CPU_BASED_CR3_LOAD_EXITING |
1683 CPU_BASED_CR3_STORE_EXITING));
1684 vcpu->arch.cr0 = cr0;
1685 vmx_set_cr4(vcpu, vcpu->arch.cr4);
Sheng Yang14394422008-04-28 12:24:45 +08001686 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08001687
1688 if (!(cr0 & X86_CR0_WP))
1689 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08001690}
1691
1692static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1693 struct kvm_vcpu *vcpu)
1694{
1695 if (!is_paging(vcpu)) {
1696 *hw_cr4 &= ~X86_CR4_PAE;
1697 *hw_cr4 |= X86_CR4_PSE;
1698 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1699 *hw_cr4 &= ~X86_CR4_PAE;
1700}
1701
Avi Kivity6aa8b732006-12-10 02:21:36 -08001702static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1703{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001704 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001705 unsigned long hw_cr0;
1706
1707 if (enable_unrestricted_guest)
1708 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
1709 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
1710 else
1711 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08001712
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001713 vmx_fpu_deactivate(vcpu);
1714
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001715 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001716 enter_pmode(vcpu);
1717
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001718 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001719 enter_rmode(vcpu);
1720
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001721#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001722 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001723 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001724 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001725 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726 exit_lmode(vcpu);
1727 }
1728#endif
1729
Avi Kivity089d0342009-03-23 18:26:32 +02001730 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001731 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1732
Avi Kivity6aa8b732006-12-10 02:21:36 -08001733 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001734 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001735 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001736
Rusty Russell707d92fa2007-07-17 23:19:08 +10001737 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001738 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001739}
1740
Sheng Yang14394422008-04-28 12:24:45 +08001741static u64 construct_eptp(unsigned long root_hpa)
1742{
1743 u64 eptp;
1744
1745 /* TODO write the value reading from MSR */
1746 eptp = VMX_EPT_DEFAULT_MT |
1747 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1748 eptp |= (root_hpa & PAGE_MASK);
1749
1750 return eptp;
1751}
1752
Avi Kivity6aa8b732006-12-10 02:21:36 -08001753static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1754{
Sheng Yang14394422008-04-28 12:24:45 +08001755 unsigned long guest_cr3;
1756 u64 eptp;
1757
1758 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02001759 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001760 eptp = construct_eptp(cr3);
1761 vmcs_write64(EPT_POINTER, eptp);
Sheng Yang14394422008-04-28 12:24:45 +08001762 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
Sheng Yangb927a3c2009-07-21 10:42:48 +08001763 vcpu->kvm->arch.ept_identity_map_addr;
Sheng Yang14394422008-04-28 12:24:45 +08001764 }
1765
Sheng Yang2384d2b2008-01-17 15:14:33 +08001766 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001767 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001768 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001769 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001770}
1771
1772static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1773{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001774 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08001775 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1776
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001777 vcpu->arch.cr4 = cr4;
Avi Kivity089d0342009-03-23 18:26:32 +02001778 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001779 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1780
1781 vmcs_writel(CR4_READ_SHADOW, cr4);
1782 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001783}
1784
Avi Kivity6aa8b732006-12-10 02:21:36 -08001785static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1786{
1787 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1788
1789 return vmcs_readl(sf->base);
1790}
1791
1792static void vmx_get_segment(struct kvm_vcpu *vcpu,
1793 struct kvm_segment *var, int seg)
1794{
1795 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1796 u32 ar;
1797
1798 var->base = vmcs_readl(sf->base);
1799 var->limit = vmcs_read32(sf->limit);
1800 var->selector = vmcs_read16(sf->selector);
1801 ar = vmcs_read32(sf->ar_bytes);
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02001802 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001803 ar = 0;
1804 var->type = ar & 15;
1805 var->s = (ar >> 4) & 1;
1806 var->dpl = (ar >> 5) & 3;
1807 var->present = (ar >> 7) & 1;
1808 var->avl = (ar >> 12) & 1;
1809 var->l = (ar >> 13) & 1;
1810 var->db = (ar >> 14) & 1;
1811 var->g = (ar >> 15) & 1;
1812 var->unusable = (ar >> 16) & 1;
1813}
1814
Izik Eidus2e4d2652008-03-24 19:38:34 +02001815static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1816{
Izik Eidus2e4d2652008-03-24 19:38:34 +02001817 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1818 return 0;
1819
1820 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1821 return 3;
1822
Avi Kivityeab4b8a2009-08-04 15:02:54 +03001823 return vmcs_read16(GUEST_CS_SELECTOR) & 3;
Izik Eidus2e4d2652008-03-24 19:38:34 +02001824}
1825
Avi Kivity653e3102007-05-07 10:55:37 +03001826static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001827{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001828 u32 ar;
1829
Avi Kivity653e3102007-05-07 10:55:37 +03001830 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831 ar = 1 << 16;
1832 else {
1833 ar = var->type & 15;
1834 ar |= (var->s & 1) << 4;
1835 ar |= (var->dpl & 3) << 5;
1836 ar |= (var->present & 1) << 7;
1837 ar |= (var->avl & 1) << 12;
1838 ar |= (var->l & 1) << 13;
1839 ar |= (var->db & 1) << 14;
1840 ar |= (var->g & 1) << 15;
1841 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001842 if (ar == 0) /* a 0 value means unusable */
1843 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001844
1845 return ar;
1846}
1847
1848static void vmx_set_segment(struct kvm_vcpu *vcpu,
1849 struct kvm_segment *var, int seg)
1850{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001851 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03001852 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1853 u32 ar;
1854
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001855 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
1856 vmx->rmode.tr.selector = var->selector;
1857 vmx->rmode.tr.base = var->base;
1858 vmx->rmode.tr.limit = var->limit;
1859 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001860 return;
1861 }
1862 vmcs_writel(sf->base, var->base);
1863 vmcs_write32(sf->limit, var->limit);
1864 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001865 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001866 /*
1867 * Hack real-mode segments into vm86 compatibility.
1868 */
1869 if (var->base == 0xffff0000 && var->selector == 0xf000)
1870 vmcs_writel(sf->base, 0xf0000);
1871 ar = 0xf3;
1872 } else
1873 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001874
1875 /*
1876 * Fix the "Accessed" bit in AR field of segment registers for older
1877 * qemu binaries.
1878 * IA32 arch specifies that at the time of processor reset the
1879 * "Accessed" bit in the AR field of segment registers is 1. And qemu
1880 * is setting it to 0 in the usedland code. This causes invalid guest
1881 * state vmexit when "unrestricted guest" mode is turned on.
1882 * Fix for this setup issue in cpu_reset is being pushed in the qemu
1883 * tree. Newer qemu binaries with that qemu fix would not need this
1884 * kvm hack.
1885 */
1886 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
1887 ar |= 0x1; /* Accessed */
1888
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889 vmcs_write32(sf->ar_bytes, ar);
1890}
1891
Avi Kivity6aa8b732006-12-10 02:21:36 -08001892static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1893{
1894 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1895
1896 *db = (ar >> 14) & 1;
1897 *l = (ar >> 13) & 1;
1898}
1899
1900static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1901{
1902 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1903 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1904}
1905
1906static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1907{
1908 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1909 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1910}
1911
1912static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1913{
1914 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1915 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1916}
1917
1918static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1919{
1920 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1921 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1922}
1923
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001924static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1925{
1926 struct kvm_segment var;
1927 u32 ar;
1928
1929 vmx_get_segment(vcpu, &var, seg);
1930 ar = vmx_segment_access_rights(&var);
1931
1932 if (var.base != (var.selector << 4))
1933 return false;
1934 if (var.limit != 0xffff)
1935 return false;
1936 if (ar != 0xf3)
1937 return false;
1938
1939 return true;
1940}
1941
1942static bool code_segment_valid(struct kvm_vcpu *vcpu)
1943{
1944 struct kvm_segment cs;
1945 unsigned int cs_rpl;
1946
1947 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1948 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1949
Avi Kivity1872a3f2009-01-04 23:26:52 +02001950 if (cs.unusable)
1951 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001952 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1953 return false;
1954 if (!cs.s)
1955 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001956 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001957 if (cs.dpl > cs_rpl)
1958 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001959 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001960 if (cs.dpl != cs_rpl)
1961 return false;
1962 }
1963 if (!cs.present)
1964 return false;
1965
1966 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1967 return true;
1968}
1969
1970static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1971{
1972 struct kvm_segment ss;
1973 unsigned int ss_rpl;
1974
1975 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1976 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1977
Avi Kivity1872a3f2009-01-04 23:26:52 +02001978 if (ss.unusable)
1979 return true;
1980 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001981 return false;
1982 if (!ss.s)
1983 return false;
1984 if (ss.dpl != ss_rpl) /* DPL != RPL */
1985 return false;
1986 if (!ss.present)
1987 return false;
1988
1989 return true;
1990}
1991
1992static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1993{
1994 struct kvm_segment var;
1995 unsigned int rpl;
1996
1997 vmx_get_segment(vcpu, &var, seg);
1998 rpl = var.selector & SELECTOR_RPL_MASK;
1999
Avi Kivity1872a3f2009-01-04 23:26:52 +02002000 if (var.unusable)
2001 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002002 if (!var.s)
2003 return false;
2004 if (!var.present)
2005 return false;
2006 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
2007 if (var.dpl < rpl) /* DPL < RPL */
2008 return false;
2009 }
2010
2011 /* TODO: Add other members to kvm_segment_field to allow checking for other access
2012 * rights flags
2013 */
2014 return true;
2015}
2016
2017static bool tr_valid(struct kvm_vcpu *vcpu)
2018{
2019 struct kvm_segment tr;
2020
2021 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
2022
Avi Kivity1872a3f2009-01-04 23:26:52 +02002023 if (tr.unusable)
2024 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002025 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2026 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002027 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002028 return false;
2029 if (!tr.present)
2030 return false;
2031
2032 return true;
2033}
2034
2035static bool ldtr_valid(struct kvm_vcpu *vcpu)
2036{
2037 struct kvm_segment ldtr;
2038
2039 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2040
Avi Kivity1872a3f2009-01-04 23:26:52 +02002041 if (ldtr.unusable)
2042 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002043 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2044 return false;
2045 if (ldtr.type != 2)
2046 return false;
2047 if (!ldtr.present)
2048 return false;
2049
2050 return true;
2051}
2052
2053static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2054{
2055 struct kvm_segment cs, ss;
2056
2057 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2058 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2059
2060 return ((cs.selector & SELECTOR_RPL_MASK) ==
2061 (ss.selector & SELECTOR_RPL_MASK));
2062}
2063
2064/*
2065 * Check if guest state is valid. Returns true if valid, false if
2066 * not.
2067 * We assume that registers are always usable
2068 */
2069static bool guest_state_valid(struct kvm_vcpu *vcpu)
2070{
2071 /* real mode guest state checks */
2072 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
2073 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2074 return false;
2075 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2076 return false;
2077 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2078 return false;
2079 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2080 return false;
2081 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2082 return false;
2083 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2084 return false;
2085 } else {
2086 /* protected mode guest state checks */
2087 if (!cs_ss_rpl_check(vcpu))
2088 return false;
2089 if (!code_segment_valid(vcpu))
2090 return false;
2091 if (!stack_segment_valid(vcpu))
2092 return false;
2093 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2094 return false;
2095 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2096 return false;
2097 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2098 return false;
2099 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2100 return false;
2101 if (!tr_valid(vcpu))
2102 return false;
2103 if (!ldtr_valid(vcpu))
2104 return false;
2105 }
2106 /* TODO:
2107 * - Add checks on RIP
2108 * - Add checks on RFLAGS
2109 */
2110
2111 return true;
2112}
2113
Mike Dayd77c26f2007-10-08 09:02:08 -04002114static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002116 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002117 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002118 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02002119 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120
Izik Eidus195aefd2007-10-01 22:14:18 +02002121 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2122 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002123 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002124 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002125 r = kvm_write_guest_page(kvm, fn++, &data,
2126 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002127 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002128 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002129 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2130 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002131 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002132 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2133 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002134 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002135 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002136 r = kvm_write_guest_page(kvm, fn, &data,
2137 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2138 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002139 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002140 goto out;
2141
2142 ret = 1;
2143out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002144 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002145}
2146
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002147static int init_rmode_identity_map(struct kvm *kvm)
2148{
2149 int i, r, ret;
2150 pfn_t identity_map_pfn;
2151 u32 tmp;
2152
Avi Kivity089d0342009-03-23 18:26:32 +02002153 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002154 return 1;
2155 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2156 printk(KERN_ERR "EPT: identity-mapping pagetable "
2157 "haven't been allocated!\n");
2158 return 0;
2159 }
2160 if (likely(kvm->arch.ept_identity_pagetable_done))
2161 return 1;
2162 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002163 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002164 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2165 if (r < 0)
2166 goto out;
2167 /* Set up identity-mapping pagetable for EPT in real mode */
2168 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2169 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2170 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2171 r = kvm_write_guest_page(kvm, identity_map_pfn,
2172 &tmp, i * sizeof(tmp), sizeof(tmp));
2173 if (r < 0)
2174 goto out;
2175 }
2176 kvm->arch.ept_identity_pagetable_done = true;
2177 ret = 1;
2178out:
2179 return ret;
2180}
2181
Avi Kivity6aa8b732006-12-10 02:21:36 -08002182static void seg_setup(int seg)
2183{
2184 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002185 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002186
2187 vmcs_write16(sf->selector, 0);
2188 vmcs_writel(sf->base, 0);
2189 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002190 if (enable_unrestricted_guest) {
2191 ar = 0x93;
2192 if (seg == VCPU_SREG_CS)
2193 ar |= 0x08; /* code segment */
2194 } else
2195 ar = 0xf3;
2196
2197 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002198}
2199
Sheng Yangf78e0e22007-10-29 09:40:42 +08002200static int alloc_apic_access_page(struct kvm *kvm)
2201{
2202 struct kvm_userspace_memory_region kvm_userspace_mem;
2203 int r = 0;
2204
Izik Eidus72dc67a2008-02-10 18:04:15 +02002205 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002206 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002207 goto out;
2208 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2209 kvm_userspace_mem.flags = 0;
2210 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2211 kvm_userspace_mem.memory_size = PAGE_SIZE;
2212 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2213 if (r)
2214 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002215
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002216 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002217out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002218 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002219 return r;
2220}
2221
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002222static int alloc_identity_pagetable(struct kvm *kvm)
2223{
2224 struct kvm_userspace_memory_region kvm_userspace_mem;
2225 int r = 0;
2226
2227 down_write(&kvm->slots_lock);
2228 if (kvm->arch.ept_identity_pagetable)
2229 goto out;
2230 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2231 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002232 kvm_userspace_mem.guest_phys_addr =
2233 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002234 kvm_userspace_mem.memory_size = PAGE_SIZE;
2235 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2236 if (r)
2237 goto out;
2238
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002239 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
Sheng Yangb927a3c2009-07-21 10:42:48 +08002240 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002241out:
2242 up_write(&kvm->slots_lock);
2243 return r;
2244}
2245
Sheng Yang2384d2b2008-01-17 15:14:33 +08002246static void allocate_vpid(struct vcpu_vmx *vmx)
2247{
2248 int vpid;
2249
2250 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002251 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002252 return;
2253 spin_lock(&vmx_vpid_lock);
2254 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2255 if (vpid < VMX_NR_VPIDS) {
2256 vmx->vpid = vpid;
2257 __set_bit(vpid, vmx_vpid_bitmap);
2258 }
2259 spin_unlock(&vmx_vpid_lock);
2260}
2261
Avi Kivity58972972009-02-24 22:26:47 +02002262static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002263{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002264 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002265
2266 if (!cpu_has_vmx_msr_bitmap())
2267 return;
2268
2269 /*
2270 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2271 * have the write-low and read-high bitmap offsets the wrong way round.
2272 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2273 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002274 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002275 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2276 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002277 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2278 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002279 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2280 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002281 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002282}
2283
Avi Kivity58972972009-02-24 22:26:47 +02002284static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2285{
2286 if (!longmode_only)
2287 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2288 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2289}
2290
Avi Kivity6aa8b732006-12-10 02:21:36 -08002291/*
2292 * Sets up the vmcs for emulated real mode.
2293 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002294static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002295{
Sheng Yang468d4722008-10-09 16:01:55 +08002296 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002297 u32 junk;
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01002298 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299 unsigned long a;
2300 struct descriptor_table dt;
2301 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002302 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002303 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002306 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2307 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308
Sheng Yang25c5f222008-03-28 13:18:56 +08002309 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002310 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002311
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2313
Avi Kivity6aa8b732006-12-10 02:21:36 -08002314 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002315 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2316 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002317
2318 exec_control = vmcs_config.cpu_based_exec_ctrl;
2319 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2320 exec_control &= ~CPU_BASED_TPR_SHADOW;
2321#ifdef CONFIG_X86_64
2322 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2323 CPU_BASED_CR8_LOAD_EXITING;
2324#endif
2325 }
Avi Kivity089d0342009-03-23 18:26:32 +02002326 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002327 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002328 CPU_BASED_CR3_LOAD_EXITING |
2329 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002330 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331
Sheng Yang83ff3b92007-11-21 14:33:25 +08002332 if (cpu_has_secondary_exec_ctrls()) {
2333 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2334 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2335 exec_control &=
2336 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002337 if (vmx->vpid == 0)
2338 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Avi Kivity089d0342009-03-23 18:26:32 +02002339 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002340 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002341 if (!enable_unrestricted_guest)
2342 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002343 if (!ple_gap)
2344 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002345 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2346 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002347
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002348 if (ple_gap) {
2349 vmcs_write32(PLE_GAP, ple_gap);
2350 vmcs_write32(PLE_WINDOW, ple_window);
2351 }
2352
Avi Kivityc7addb92007-09-16 18:58:32 +02002353 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2354 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2356
2357 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2358 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2359 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2360
2361 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2362 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2363 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002364 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2365 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002367#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368 rdmsrl(MSR_FS_BASE, a);
2369 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2370 rdmsrl(MSR_GS_BASE, a);
2371 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2372#else
2373 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2374 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2375#endif
2376
2377 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2378
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002379 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2381
Mike Dayd77c26f2007-10-08 09:02:08 -04002382 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002383 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002384 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2385 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2386 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387
2388 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2389 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2390 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2391 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2392 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2393 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2394
Sheng Yang468d4722008-10-09 16:01:55 +08002395 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2396 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2397 host_pat = msr_low | ((u64) msr_high << 32);
2398 vmcs_write64(HOST_IA32_PAT, host_pat);
2399 }
2400 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2401 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2402 host_pat = msr_low | ((u64) msr_high << 32);
2403 /* Write the default value follow host pat */
2404 vmcs_write64(GUEST_IA32_PAT, host_pat);
2405 /* Keep arch.pat sync with GUEST_IA32_PAT */
2406 vmx->vcpu.arch.pat = host_pat;
2407 }
2408
Avi Kivity6aa8b732006-12-10 02:21:36 -08002409 for (i = 0; i < NR_VMX_MSR; ++i) {
2410 u32 index = vmx_msr_index[i];
2411 u32 data_low, data_high;
2412 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002413 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002414
2415 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2416 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002417 if (wrmsr_safe(index, data_low, data_high) < 0)
2418 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002419 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002420 vmx->host_msrs[j].index = index;
2421 vmx->host_msrs[j].reserved = 0;
2422 vmx->host_msrs[j].data = data;
2423 vmx->guest_msrs[j] = vmx->host_msrs[j];
2424 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002427 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428
2429 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002430 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2431
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002432 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2433 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2434
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01002435 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2436 rdtscll(tsc_this);
2437 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2438 tsc_base = tsc_this;
2439
2440 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002441
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002442 return 0;
2443}
2444
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002445static int init_rmode(struct kvm *kvm)
2446{
2447 if (!init_rmode_tss(kvm))
2448 return 0;
2449 if (!init_rmode_identity_map(kvm))
2450 return 0;
2451 return 1;
2452}
2453
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002454static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2455{
2456 struct vcpu_vmx *vmx = to_vmx(vcpu);
2457 u64 msr;
2458 int ret;
2459
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002460 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002461 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002462 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002463 ret = -ENOMEM;
2464 goto out;
2465 }
2466
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002467 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002468
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002469 vmx->soft_vnmi_blocked = 0;
2470
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002471 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002472 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002473 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002474 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002475 msr |= MSR_IA32_APICBASE_BSP;
2476 kvm_set_apic_base(&vmx->vcpu, msr);
2477
2478 fx_init(&vmx->vcpu);
2479
Avi Kivity5706be02008-08-20 15:07:31 +03002480 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002481 /*
2482 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2483 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2484 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002485 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002486 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2487 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2488 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002489 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2490 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002491 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002492
2493 seg_setup(VCPU_SREG_DS);
2494 seg_setup(VCPU_SREG_ES);
2495 seg_setup(VCPU_SREG_FS);
2496 seg_setup(VCPU_SREG_GS);
2497 seg_setup(VCPU_SREG_SS);
2498
2499 vmcs_write16(GUEST_TR_SELECTOR, 0);
2500 vmcs_writel(GUEST_TR_BASE, 0);
2501 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2502 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2503
2504 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2505 vmcs_writel(GUEST_LDTR_BASE, 0);
2506 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2507 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2508
2509 vmcs_write32(GUEST_SYSENTER_CS, 0);
2510 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2511 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2512
2513 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002514 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002515 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002516 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002517 kvm_rip_write(vcpu, 0);
2518 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002519
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002520 vmcs_writel(GUEST_DR7, 0x400);
2521
2522 vmcs_writel(GUEST_GDTR_BASE, 0);
2523 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2524
2525 vmcs_writel(GUEST_IDTR_BASE, 0);
2526 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2527
2528 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2529 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2530 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2531
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002532 /* Special registers */
2533 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2534
2535 setup_msrs(vmx);
2536
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2538
Sheng Yangf78e0e22007-10-29 09:40:42 +08002539 if (cpu_has_vmx_tpr_shadow()) {
2540 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2541 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2542 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002543 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002544 vmcs_write32(TPR_THRESHOLD, 0);
2545 }
2546
2547 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2548 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002549 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550
Sheng Yang2384d2b2008-01-17 15:14:33 +08002551 if (vmx->vpid != 0)
2552 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2553
Eduardo Habkostfa400522009-10-24 02:49:58 -02002554 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002555 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002556 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002557 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002558 vmx_fpu_activate(&vmx->vcpu);
2559 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002560
Sheng Yang2384d2b2008-01-17 15:14:33 +08002561 vpid_sync_vcpu_all(vmx);
2562
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002563 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002564
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002565 /* HACK: Don't enable emulation on guest boot/reset */
2566 vmx->emulation_required = 0;
2567
Avi Kivity6aa8b732006-12-10 02:21:36 -08002568out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002569 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002570 return ret;
2571}
2572
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002573static void enable_irq_window(struct kvm_vcpu *vcpu)
2574{
2575 u32 cpu_based_vm_exec_control;
2576
2577 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2578 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2579 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2580}
2581
2582static void enable_nmi_window(struct kvm_vcpu *vcpu)
2583{
2584 u32 cpu_based_vm_exec_control;
2585
2586 if (!cpu_has_virtual_nmis()) {
2587 enable_irq_window(vcpu);
2588 return;
2589 }
2590
2591 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2592 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2593 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2594}
2595
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002596static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03002597{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002598 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002599 uint32_t intr;
2600 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02002601
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002602 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002603
Avi Kivityfa89a812008-09-01 15:57:51 +03002604 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002605 if (vmx->rmode.vm86_active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002606 vmx->rmode.irq.pending = true;
2607 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002608 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +03002609 if (vcpu->arch.interrupt.soft)
2610 vmx->rmode.irq.rip +=
2611 vmx->vcpu.arch.event_exit_inst_len;
Avi Kivity9c5623e2007-11-08 18:19:20 +02002612 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2613 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2614 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002615 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002616 return;
2617 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002618 intr = irq | INTR_INFO_VALID_MASK;
2619 if (vcpu->arch.interrupt.soft) {
2620 intr |= INTR_TYPE_SOFT_INTR;
2621 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2622 vmx->vcpu.arch.event_exit_inst_len);
2623 } else
2624 intr |= INTR_TYPE_EXT_INTR;
2625 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03002626}
2627
Sheng Yangf08864b2008-05-15 18:23:25 +08002628static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2629{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002630 struct vcpu_vmx *vmx = to_vmx(vcpu);
2631
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002632 if (!cpu_has_virtual_nmis()) {
2633 /*
2634 * Tracking the NMI-blocked state in software is built upon
2635 * finding the next open IRQ window. This, in turn, depends on
2636 * well-behaving guests: They have to keep IRQs disabled at
2637 * least as long as the NMI handler runs. Otherwise we may
2638 * cause NMI nesting, maybe breaking the guest. But as this is
2639 * highly unlikely, we can live with the residual risk.
2640 */
2641 vmx->soft_vnmi_blocked = 1;
2642 vmx->vnmi_blocked_time = 0;
2643 }
2644
Jan Kiszka487b3912008-09-26 09:30:56 +02002645 ++vcpu->stat.nmi_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002646 if (vmx->rmode.vm86_active) {
Jan Kiszka66a5a342008-09-26 09:30:51 +02002647 vmx->rmode.irq.pending = true;
2648 vmx->rmode.irq.vector = NMI_VECTOR;
2649 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2650 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2651 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2652 INTR_INFO_VALID_MASK);
2653 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2654 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2655 return;
2656 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002657 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2658 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002659}
2660
Gleb Natapovc4282df2009-04-21 17:45:07 +03002661static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02002662{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002663 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03002664 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002665
Gleb Natapovc4282df2009-04-21 17:45:07 +03002666 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2667 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS |
2668 GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02002669}
2670
Gleb Natapov78646122009-03-23 12:12:11 +02002671static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
2672{
Gleb Natapovc4282df2009-04-21 17:45:07 +03002673 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2674 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2675 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02002676}
2677
Izik Eiduscbc94022007-10-25 00:29:55 +02002678static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2679{
2680 int ret;
2681 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002682 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002683 .guest_phys_addr = addr,
2684 .memory_size = PAGE_SIZE * 3,
2685 .flags = 0,
2686 };
2687
2688 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2689 if (ret)
2690 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002691 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002692 return 0;
2693}
2694
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2696 int vec, u32 err_code)
2697{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002698 /*
2699 * Instruction with address size override prefix opcode 0x67
2700 * Cause the #SS fault with 0 error code in VM86 mode.
2701 */
2702 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Avi Kivity851ba692009-08-24 11:10:17 +03002703 if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002705 /*
2706 * Forward all other exceptions that are valid in real mode.
2707 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2708 * the required debugging infrastructure rework.
2709 */
2710 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002711 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002712 if (vcpu->guest_debug &
2713 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2714 return 0;
2715 kvm_queue_exception(vcpu, vec);
2716 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002717 case BP_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002718 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2719 return 0;
2720 /* fall through */
2721 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002722 case OF_VECTOR:
2723 case BR_VECTOR:
2724 case UD_VECTOR:
2725 case DF_VECTOR:
2726 case SS_VECTOR:
2727 case GP_VECTOR:
2728 case MF_VECTOR:
2729 kvm_queue_exception(vcpu, vec);
2730 return 1;
2731 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732 return 0;
2733}
2734
Andi Kleena0861c02009-06-08 17:37:09 +08002735/*
2736 * Trigger machine check on the host. We assume all the MSRs are already set up
2737 * by the CPU and that we still run on the same CPU as the MCE occurred on.
2738 * We pass a fake environment to the machine check handler because we want
2739 * the guest to be always treated like user space, no matter what context
2740 * it used internally.
2741 */
2742static void kvm_machine_check(void)
2743{
2744#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
2745 struct pt_regs regs = {
2746 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
2747 .flags = X86_EFLAGS_IF,
2748 };
2749
2750 do_machine_check(&regs, 0);
2751#endif
2752}
2753
Avi Kivity851ba692009-08-24 11:10:17 +03002754static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08002755{
2756 /* already handled by vcpu_run */
2757 return 1;
2758}
2759
Avi Kivity851ba692009-08-24 11:10:17 +03002760static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002761{
Avi Kivity1155f762007-11-22 11:30:47 +02002762 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002763 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002764 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002765 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766 u32 vect_info;
2767 enum emulation_result er;
2768
Avi Kivity1155f762007-11-22 11:30:47 +02002769 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2771
Andi Kleena0861c02009-06-08 17:37:09 +08002772 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03002773 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08002774
Avi Kivity6aa8b732006-12-10 02:21:36 -08002775 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002776 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002778 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779
Jan Kiszkae4a41882008-09-26 09:30:46 +02002780 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002781 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002782
2783 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002784 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002785 return 1;
2786 }
2787
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002788 if (is_invalid_opcode(intr_info)) {
Avi Kivity851ba692009-08-24 11:10:17 +03002789 er = emulate_instruction(vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002790 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002791 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002792 return 1;
2793 }
2794
Avi Kivity6aa8b732006-12-10 02:21:36 -08002795 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002796 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002797 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002798 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2799 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002800 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02002801 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002802 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002803 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002804 trace_kvm_page_fault(cr2, error_code);
2805
Gleb Natapov3298b752009-05-11 13:35:46 +03002806 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03002807 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002808 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002809 }
2810
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002811 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002813 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002814 if (vcpu->arch.halt_request) {
2815 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002816 return kvm_emulate_halt(vcpu);
2817 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002819 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002820
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002821 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002822 switch (ex_no) {
2823 case DB_VECTOR:
2824 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2825 if (!(vcpu->guest_debug &
2826 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2827 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2828 kvm_queue_exception(vcpu, DB_VECTOR);
2829 return 1;
2830 }
2831 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2832 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2833 /* fall through */
2834 case BP_VECTOR:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002836 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2837 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002838 break;
2839 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002840 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2841 kvm_run->ex.exception = ex_no;
2842 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002843 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845 return 0;
2846}
2847
Avi Kivity851ba692009-08-24 11:10:17 +03002848static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002849{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002850 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851 return 1;
2852}
2853
Avi Kivity851ba692009-08-24 11:10:17 +03002854static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08002855{
Avi Kivity851ba692009-08-24 11:10:17 +03002856 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08002857 return 0;
2858}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002859
Avi Kivity851ba692009-08-24 11:10:17 +03002860static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861{
He, Qingbfdaab02007-09-12 14:18:28 +08002862 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01002863 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002864 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002865
Avi Kivity1165f5f2007-04-19 17:27:43 +03002866 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002867 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002868 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002869
2870 if (string) {
Avi Kivity851ba692009-08-24 11:10:17 +03002871 if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002872 return 0;
2873 return 1;
2874 }
2875
2876 size = (exit_qualification & 7) + 1;
2877 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002878 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002879
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002880 skip_emulated_instruction(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002881 return kvm_emulate_pio(vcpu, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882}
2883
Ingo Molnar102d8322007-02-19 14:37:47 +02002884static void
2885vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2886{
2887 /*
2888 * Patch in the VMCALL instruction:
2889 */
2890 hypercall[0] = 0x0f;
2891 hypercall[1] = 0x01;
2892 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002893}
2894
Avi Kivity851ba692009-08-24 11:10:17 +03002895static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002897 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002898 int cr;
2899 int reg;
2900
He, Qingbfdaab02007-09-12 14:18:28 +08002901 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902 cr = exit_qualification & 15;
2903 reg = (exit_qualification >> 8) & 15;
2904 switch ((exit_qualification >> 4) & 3) {
2905 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002906 val = kvm_register_read(vcpu, reg);
2907 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908 switch (cr) {
2909 case 0:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002910 kvm_set_cr0(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002911 skip_emulated_instruction(vcpu);
2912 return 1;
2913 case 3:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002914 kvm_set_cr3(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002915 skip_emulated_instruction(vcpu);
2916 return 1;
2917 case 4:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002918 kvm_set_cr4(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002919 skip_emulated_instruction(vcpu);
2920 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002921 case 8: {
2922 u8 cr8_prev = kvm_get_cr8(vcpu);
2923 u8 cr8 = kvm_register_read(vcpu, reg);
2924 kvm_set_cr8(vcpu, cr8);
2925 skip_emulated_instruction(vcpu);
2926 if (irqchip_in_kernel(vcpu->kvm))
2927 return 1;
2928 if (cr8_prev <= cr8)
2929 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03002930 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002931 return 0;
2932 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933 };
2934 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002935 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002936 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002937 vcpu->arch.cr0 &= ~X86_CR0_TS;
2938 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002939 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002940 skip_emulated_instruction(vcpu);
2941 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002942 case 1: /*mov from cr*/
2943 switch (cr) {
2944 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002945 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002946 trace_kvm_cr_read(cr, vcpu->arch.cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002947 skip_emulated_instruction(vcpu);
2948 return 1;
2949 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002950 val = kvm_get_cr8(vcpu);
2951 kvm_register_write(vcpu, reg, val);
2952 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002953 skip_emulated_instruction(vcpu);
2954 return 1;
2955 }
2956 break;
2957 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002958 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959
2960 skip_emulated_instruction(vcpu);
2961 return 1;
2962 default:
2963 break;
2964 }
Avi Kivity851ba692009-08-24 11:10:17 +03002965 vcpu->run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002966 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 (int)(exit_qualification >> 4) & 3, cr);
2968 return 0;
2969}
2970
Avi Kivity851ba692009-08-24 11:10:17 +03002971static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002972{
He, Qingbfdaab02007-09-12 14:18:28 +08002973 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002974 unsigned long val;
2975 int dr, reg;
2976
Avi Kivity0a79b002009-09-01 12:03:25 +03002977 if (!kvm_require_cpl(vcpu, 0))
2978 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002979 dr = vmcs_readl(GUEST_DR7);
2980 if (dr & DR7_GD) {
2981 /*
2982 * As the vm-exit takes precedence over the debug trap, we
2983 * need to emulate the latter, either for the host or the
2984 * guest debugging itself.
2985 */
2986 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03002987 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
2988 vcpu->run->debug.arch.dr7 = dr;
2989 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002990 vmcs_readl(GUEST_CS_BASE) +
2991 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03002992 vcpu->run->debug.arch.exception = DB_VECTOR;
2993 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002994 return 0;
2995 } else {
2996 vcpu->arch.dr7 &= ~DR7_GD;
2997 vcpu->arch.dr6 |= DR6_BD;
2998 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2999 kvm_queue_exception(vcpu, DB_VECTOR);
3000 return 1;
3001 }
3002 }
3003
He, Qingbfdaab02007-09-12 14:18:28 +08003004 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003005 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
3006 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
3007 if (exit_qualification & TYPE_MOV_FROM_DR) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003008 switch (dr) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003009 case 0 ... 3:
3010 val = vcpu->arch.db[dr];
3011 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003012 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003013 val = vcpu->arch.dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003014 break;
3015 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003016 val = vcpu->arch.dr7;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003017 break;
3018 default:
3019 val = 0;
3020 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003021 kvm_register_write(vcpu, reg, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003022 } else {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003023 val = vcpu->arch.regs[reg];
3024 switch (dr) {
3025 case 0 ... 3:
3026 vcpu->arch.db[dr] = val;
3027 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
3028 vcpu->arch.eff_db[dr] = val;
3029 break;
3030 case 4 ... 5:
3031 if (vcpu->arch.cr4 & X86_CR4_DE)
3032 kvm_queue_exception(vcpu, UD_VECTOR);
3033 break;
3034 case 6:
3035 if (val & 0xffffffff00000000ULL) {
3036 kvm_queue_exception(vcpu, GP_VECTOR);
3037 break;
3038 }
3039 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
3040 break;
3041 case 7:
3042 if (val & 0xffffffff00000000ULL) {
3043 kvm_queue_exception(vcpu, GP_VECTOR);
3044 break;
3045 }
3046 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
3047 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
3048 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3049 vcpu->arch.switch_db_regs =
3050 (val & DR7_BP_EN_MASK);
3051 }
3052 break;
3053 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003054 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003055 skip_emulated_instruction(vcpu);
3056 return 1;
3057}
3058
Avi Kivity851ba692009-08-24 11:10:17 +03003059static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003060{
Avi Kivity06465c52007-02-28 20:46:53 +02003061 kvm_emulate_cpuid(vcpu);
3062 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003063}
3064
Avi Kivity851ba692009-08-24 11:10:17 +03003065static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003067 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003068 u64 data;
3069
3070 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003071 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003072 return 1;
3073 }
3074
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003075 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003076
Avi Kivity6aa8b732006-12-10 02:21:36 -08003077 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003078 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3079 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003080 skip_emulated_instruction(vcpu);
3081 return 1;
3082}
3083
Avi Kivity851ba692009-08-24 11:10:17 +03003084static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003085{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003086 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3087 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3088 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003090 trace_kvm_msr_write(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003091
Avi Kivity6aa8b732006-12-10 02:21:36 -08003092 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003093 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003094 return 1;
3095 }
3096
3097 skip_emulated_instruction(vcpu);
3098 return 1;
3099}
3100
Avi Kivity851ba692009-08-24 11:10:17 +03003101static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003102{
3103 return 1;
3104}
3105
Avi Kivity851ba692009-08-24 11:10:17 +03003106static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003107{
Eddie Dong85f455f2007-07-06 12:20:49 +03003108 u32 cpu_based_vm_exec_control;
3109
3110 /* clear pending irq */
3111 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3112 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3113 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003114
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003115 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003116
Dor Laorc1150d82007-01-05 16:36:24 -08003117 /*
3118 * If the user space waits to inject interrupts, exit as soon as
3119 * possible
3120 */
Gleb Natapov80618232009-04-21 17:44:56 +03003121 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003122 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03003123 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003124 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003125 return 0;
3126 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003127 return 1;
3128}
3129
Avi Kivity851ba692009-08-24 11:10:17 +03003130static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003131{
3132 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003133 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003134}
3135
Avi Kivity851ba692009-08-24 11:10:17 +03003136static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02003137{
Dor Laor510043d2007-02-19 18:25:43 +02003138 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003139 kvm_emulate_hypercall(vcpu);
3140 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003141}
3142
Avi Kivity851ba692009-08-24 11:10:17 +03003143static int handle_vmx_insn(struct kvm_vcpu *vcpu)
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003144{
3145 kvm_queue_exception(vcpu, UD_VECTOR);
3146 return 1;
3147}
3148
Avi Kivity851ba692009-08-24 11:10:17 +03003149static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003150{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003151 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003152
3153 kvm_mmu_invlpg(vcpu, exit_qualification);
3154 skip_emulated_instruction(vcpu);
3155 return 1;
3156}
3157
Avi Kivity851ba692009-08-24 11:10:17 +03003158static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02003159{
3160 skip_emulated_instruction(vcpu);
3161 /* TODO: Add support for VT-d/pass-through device */
3162 return 1;
3163}
3164
Avi Kivity851ba692009-08-24 11:10:17 +03003165static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003166{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003167 unsigned long exit_qualification;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003168 enum emulation_result er;
3169 unsigned long offset;
3170
Sheng Yangf9c617f2009-03-25 10:08:52 +08003171 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003172 offset = exit_qualification & 0xffful;
3173
Avi Kivity851ba692009-08-24 11:10:17 +03003174 er = emulate_instruction(vcpu, 0, 0, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003175
3176 if (er != EMULATE_DONE) {
3177 printk(KERN_ERR
3178 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3179 offset);
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003180 return -ENOEXEC;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003181 }
3182 return 1;
3183}
3184
Avi Kivity851ba692009-08-24 11:10:17 +03003185static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02003186{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003187 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003188 unsigned long exit_qualification;
3189 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003190 int reason, type, idt_v;
3191
3192 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3193 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003194
3195 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3196
3197 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003198 if (reason == TASK_SWITCH_GATE && idt_v) {
3199 switch (type) {
3200 case INTR_TYPE_NMI_INTR:
3201 vcpu->arch.nmi_injected = false;
3202 if (cpu_has_virtual_nmis())
3203 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3204 GUEST_INTR_STATE_NMI);
3205 break;
3206 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003207 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003208 kvm_clear_interrupt_queue(vcpu);
3209 break;
3210 case INTR_TYPE_HARD_EXCEPTION:
3211 case INTR_TYPE_SOFT_EXCEPTION:
3212 kvm_clear_exception_queue(vcpu);
3213 break;
3214 default:
3215 break;
3216 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003217 }
Izik Eidus37817f22008-03-24 23:14:53 +02003218 tss_selector = exit_qualification;
3219
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003220 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3221 type != INTR_TYPE_EXT_INTR &&
3222 type != INTR_TYPE_NMI_INTR))
3223 skip_emulated_instruction(vcpu);
3224
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003225 if (!kvm_task_switch(vcpu, tss_selector, reason))
3226 return 0;
3227
3228 /* clear all local breakpoint enable flags */
3229 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3230
3231 /*
3232 * TODO: What about debug traps on tss switch?
3233 * Are we supposed to inject them and update dr6?
3234 */
3235
3236 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003237}
3238
Avi Kivity851ba692009-08-24 11:10:17 +03003239static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08003240{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003241 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003242 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003243 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003244
Sheng Yangf9c617f2009-03-25 10:08:52 +08003245 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003246
3247 if (exit_qualification & (1 << 6)) {
3248 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003249 return -EINVAL;
Sheng Yang14394422008-04-28 12:24:45 +08003250 }
3251
3252 gla_validity = (exit_qualification >> 7) & 0x3;
3253 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3254 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3255 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3256 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003257 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003258 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3259 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03003260 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3261 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03003262 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003263 }
3264
3265 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003266 trace_kvm_page_fault(gpa, exit_qualification);
Sheng Yang49cd7d22009-02-11 13:50:40 +08003267 return kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003268}
3269
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003270static u64 ept_rsvd_mask(u64 spte, int level)
3271{
3272 int i;
3273 u64 mask = 0;
3274
3275 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
3276 mask |= (1ULL << i);
3277
3278 if (level > 2)
3279 /* bits 7:3 reserved */
3280 mask |= 0xf8;
3281 else if (level == 2) {
3282 if (spte & (1ULL << 7))
3283 /* 2MB ref, bits 20:12 reserved */
3284 mask |= 0x1ff000;
3285 else
3286 /* bits 6:3 reserved */
3287 mask |= 0x78;
3288 }
3289
3290 return mask;
3291}
3292
3293static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
3294 int level)
3295{
3296 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
3297
3298 /* 010b (write-only) */
3299 WARN_ON((spte & 0x7) == 0x2);
3300
3301 /* 110b (write/execute) */
3302 WARN_ON((spte & 0x7) == 0x6);
3303
3304 /* 100b (execute-only) and value not supported by logical processor */
3305 if (!cpu_has_vmx_ept_execute_only())
3306 WARN_ON((spte & 0x7) == 0x4);
3307
3308 /* not 000b */
3309 if ((spte & 0x7)) {
3310 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
3311
3312 if (rsvd_bits != 0) {
3313 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
3314 __func__, rsvd_bits);
3315 WARN_ON(1);
3316 }
3317
3318 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
3319 u64 ept_mem_type = (spte & 0x38) >> 3;
3320
3321 if (ept_mem_type == 2 || ept_mem_type == 3 ||
3322 ept_mem_type == 7) {
3323 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
3324 __func__, ept_mem_type);
3325 WARN_ON(1);
3326 }
3327 }
3328 }
3329}
3330
Avi Kivity851ba692009-08-24 11:10:17 +03003331static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003332{
3333 u64 sptes[4];
3334 int nr_sptes, i;
3335 gpa_t gpa;
3336
3337 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3338
3339 printk(KERN_ERR "EPT: Misconfiguration.\n");
3340 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
3341
3342 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
3343
3344 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
3345 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
3346
Avi Kivity851ba692009-08-24 11:10:17 +03003347 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3348 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003349
3350 return 0;
3351}
3352
Avi Kivity851ba692009-08-24 11:10:17 +03003353static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08003354{
3355 u32 cpu_based_vm_exec_control;
3356
3357 /* clear pending NMI */
3358 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3359 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3360 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3361 ++vcpu->stat.nmi_window_exits;
3362
3363 return 1;
3364}
3365
Mohammed Gamal80ced182009-09-01 12:48:18 +02003366static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003367{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003368 struct vcpu_vmx *vmx = to_vmx(vcpu);
3369 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003370 int ret = 1;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003371
3372 while (!guest_state_valid(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003373 err = emulate_instruction(vcpu, 0, 0, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003374
Mohammed Gamal80ced182009-09-01 12:48:18 +02003375 if (err == EMULATE_DO_MMIO) {
3376 ret = 0;
3377 goto out;
3378 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003379
3380 if (err != EMULATE_DONE) {
3381 kvm_report_emulation_failure(vcpu, "emulation failure");
Mohammed Gamal80ced182009-09-01 12:48:18 +02003382 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3383 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3384 ret = 0;
3385 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003386 }
3387
3388 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02003389 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003390 if (need_resched())
3391 schedule();
3392 }
3393
Mohammed Gamal80ced182009-09-01 12:48:18 +02003394 vmx->emulation_required = 0;
3395out:
3396 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003397}
3398
Avi Kivity6aa8b732006-12-10 02:21:36 -08003399/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003400 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
3401 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
3402 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03003403static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003404{
3405 skip_emulated_instruction(vcpu);
3406 kvm_vcpu_on_spin(vcpu);
3407
3408 return 1;
3409}
3410
3411/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003412 * The exit handlers return 1 if the exit was handled fully and guest execution
3413 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3414 * to be done to userspace and return 0.
3415 */
Avi Kivity851ba692009-08-24 11:10:17 +03003416static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003417 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3418 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003419 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003420 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003422 [EXIT_REASON_CR_ACCESS] = handle_cr,
3423 [EXIT_REASON_DR_ACCESS] = handle_dr,
3424 [EXIT_REASON_CPUID] = handle_cpuid,
3425 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3426 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3427 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3428 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003429 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003430 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003431 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3432 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3433 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3434 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3435 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3436 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3437 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3438 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3439 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003440 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3441 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003442 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003443 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08003444 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003445 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
3446 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003447 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003448};
3449
3450static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003451 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003452
3453/*
3454 * The guest has exited. See if we can fix it or if we need userspace
3455 * assistance.
3456 */
Avi Kivity851ba692009-08-24 11:10:17 +03003457static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003458{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003459 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003460 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003461 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003462
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003463 trace_kvm_exit(exit_reason, kvm_rip_read(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003464
Mohammed Gamal80ced182009-09-01 12:48:18 +02003465 /* If guest state is invalid, start emulating */
3466 if (vmx->emulation_required && emulate_invalid_guest_state)
3467 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003468
Sheng Yang14394422008-04-28 12:24:45 +08003469 /* Access CR3 don't cause VMExit in paging mode, so we need
3470 * to sync with guest real CR3. */
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003471 if (enable_ept && is_paging(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08003472 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003473
Avi Kivity29bd8a72007-09-10 17:27:03 +03003474 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003475 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3476 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03003477 = vmcs_read32(VM_INSTRUCTION_ERROR);
3478 return 0;
3479 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003480
Mike Dayd77c26f2007-10-08 09:02:08 -04003481 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003482 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003483 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3484 exit_reason != EXIT_REASON_TASK_SWITCH))
3485 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3486 "(0x%x) and exit reason is 0x%x\n",
3487 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003488
3489 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03003490 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003491 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003492 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003493 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003494 /*
3495 * This CPU don't support us in finding the end of an
3496 * NMI-blocked window if the guest runs with IRQs
3497 * disabled. So we pull the trigger after 1 s of
3498 * futile waiting, but inform the user about this.
3499 */
3500 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3501 "state on VCPU %d after 1 s timeout\n",
3502 __func__, vcpu->vcpu_id);
3503 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003504 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003505 }
3506
Avi Kivity6aa8b732006-12-10 02:21:36 -08003507 if (exit_reason < kvm_vmx_max_exit_handlers
3508 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03003509 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003510 else {
Avi Kivity851ba692009-08-24 11:10:17 +03003511 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3512 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513 }
3514 return 0;
3515}
3516
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003517static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003518{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003519 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003520 vmcs_write32(TPR_THRESHOLD, 0);
3521 return;
3522 }
3523
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003524 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003525}
3526
Avi Kivitycf393f72008-07-01 16:20:21 +03003527static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3528{
3529 u32 exit_intr_info;
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003530 u32 idt_vectoring_info = vmx->idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003531 bool unblock_nmi;
3532 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003533 int type;
3534 bool idtv_info_valid;
Avi Kivitycf393f72008-07-01 16:20:21 +03003535
3536 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Gleb Natapov20f65982009-05-11 13:35:55 +03003537
Andi Kleena0861c02009-06-08 17:37:09 +08003538 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
3539
3540 /* Handle machine checks before interrupts are enabled */
3541 if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
3542 || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
3543 && is_machine_check(exit_intr_info)))
3544 kvm_machine_check();
3545
Gleb Natapov20f65982009-05-11 13:35:55 +03003546 /* We need to handle NMIs before interrupts are enabled */
3547 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003548 (exit_intr_info & INTR_INFO_VALID_MASK))
Gleb Natapov20f65982009-05-11 13:35:55 +03003549 asm("int $2");
Gleb Natapov20f65982009-05-11 13:35:55 +03003550
3551 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3552
Avi Kivitycf393f72008-07-01 16:20:21 +03003553 if (cpu_has_virtual_nmis()) {
3554 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3555 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3556 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003557 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03003558 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3559 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003560 * SDM 3: 23.2.2 (September 2008)
3561 * Bit 12 is undefined in any of the following cases:
3562 * If the VM exit sets the valid bit in the IDT-vectoring
3563 * information field.
3564 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03003565 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003566 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
3567 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03003568 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3569 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003570 } else if (unlikely(vmx->soft_vnmi_blocked))
3571 vmx->vnmi_blocked_time +=
3572 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003573
Gleb Natapov37b96e92009-03-30 16:03:13 +03003574 vmx->vcpu.arch.nmi_injected = false;
3575 kvm_clear_exception_queue(&vmx->vcpu);
3576 kvm_clear_interrupt_queue(&vmx->vcpu);
3577
3578 if (!idtv_info_valid)
3579 return;
3580
Avi Kivity668f6122008-07-02 09:28:55 +03003581 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3582 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003583
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003584 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003585 case INTR_TYPE_NMI_INTR:
3586 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03003587 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003588 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03003589 * Clear bit "block by NMI" before VM entry if a NMI
3590 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03003591 */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003592 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3593 GUEST_INTR_STATE_NMI);
3594 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003595 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003596 vmx->vcpu.arch.event_exit_inst_len =
3597 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3598 /* fall through */
3599 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03003600 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003601 u32 err = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3602 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03003603 } else
3604 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003605 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003606 case INTR_TYPE_SOFT_INTR:
3607 vmx->vcpu.arch.event_exit_inst_len =
3608 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3609 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003610 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003611 kvm_queue_interrupt(&vmx->vcpu, vector,
3612 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003613 break;
3614 default:
3615 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03003616 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003617}
3618
Avi Kivity9c8cba32007-11-22 11:42:59 +02003619/*
3620 * Failure to inject an interrupt should give us the information
3621 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3622 * when fetching the interrupt redirection bitmap in the real-mode
3623 * tss, this doesn't happen. So we do it ourselves.
3624 */
3625static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3626{
3627 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003628 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003629 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003630 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003631 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3632 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3633 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3634 return;
3635 }
3636 vmx->idt_vectoring_info =
3637 VECTORING_INFO_VALID_MASK
3638 | INTR_TYPE_EXT_INTR
3639 | vmx->rmode.irq.vector;
3640}
3641
Avi Kivityc8019492008-07-14 14:44:59 +03003642#ifdef CONFIG_X86_64
3643#define R "r"
3644#define Q "q"
3645#else
3646#define R "e"
3647#define Q "l"
3648#endif
3649
Avi Kivity851ba692009-08-24 11:10:17 +03003650static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003651{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003652 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitye6adf282007-04-30 16:07:54 +03003653
Avi Kivity8f5d5492009-05-31 18:41:29 +03003654 if (enable_ept && is_paging(vcpu)) {
3655 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3656 ept_load_pdptrs(vcpu);
3657 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003658 /* Record the guest's net vcpu time for enforced NMI injections. */
3659 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3660 vmx->entry_time = ktime_get();
3661
Mohammed Gamal80ced182009-09-01 12:48:18 +02003662 /* Don't enter VMX if guest state is invalid, let the exit handler
3663 start emulation until we arrive back to a valid state */
3664 if (vmx->emulation_required && emulate_invalid_guest_state)
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003665 return;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003666
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003667 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3668 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3669 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3670 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3671
Gleb Natapov787ff732009-05-18 11:44:06 +03003672 /* When single-stepping over STI and MOV SS, we must clear the
3673 * corresponding interruptibility bits in the guest state. Otherwise
3674 * vmentry fails as it then expects bit 14 (BS) in pending debug
3675 * exceptions being set, but that's not correct for the guest debugging
3676 * case. */
3677 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3678 vmx_set_interrupt_shadow(vcpu, 0);
3679
Avi Kivitye6adf282007-04-30 16:07:54 +03003680 /*
3681 * Loading guest fpu may have cleared host cr0.ts
3682 */
3683 vmcs_writel(HOST_CR0, read_cr0());
3684
Avi Kivitye8a48342009-09-01 16:06:25 +03003685 if (vcpu->arch.switch_db_regs)
3686 set_debugreg(vcpu->arch.dr6, 6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003687
Mike Dayd77c26f2007-10-08 09:02:08 -04003688 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003689 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003690 "push %%"R"dx; push %%"R"bp;"
3691 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003692 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3693 "je 1f \n\t"
3694 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003695 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003696 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03003697 /* Reload cr2 if changed */
3698 "mov %c[cr2](%0), %%"R"ax \n\t"
3699 "mov %%cr2, %%"R"dx \n\t"
3700 "cmp %%"R"ax, %%"R"dx \n\t"
3701 "je 2f \n\t"
3702 "mov %%"R"ax, %%cr2 \n\t"
3703 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003704 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003705 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003706 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003707 "mov %c[rax](%0), %%"R"ax \n\t"
3708 "mov %c[rbx](%0), %%"R"bx \n\t"
3709 "mov %c[rdx](%0), %%"R"dx \n\t"
3710 "mov %c[rsi](%0), %%"R"si \n\t"
3711 "mov %c[rdi](%0), %%"R"di \n\t"
3712 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003713#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003714 "mov %c[r8](%0), %%r8 \n\t"
3715 "mov %c[r9](%0), %%r9 \n\t"
3716 "mov %c[r10](%0), %%r10 \n\t"
3717 "mov %c[r11](%0), %%r11 \n\t"
3718 "mov %c[r12](%0), %%r12 \n\t"
3719 "mov %c[r13](%0), %%r13 \n\t"
3720 "mov %c[r14](%0), %%r14 \n\t"
3721 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003722#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003723 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3724
Avi Kivity6aa8b732006-12-10 02:21:36 -08003725 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003726 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003727 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003728 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003729 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003730 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003731 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003732 "xchg %0, (%%"R"sp) \n\t"
3733 "mov %%"R"ax, %c[rax](%0) \n\t"
3734 "mov %%"R"bx, %c[rbx](%0) \n\t"
3735 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3736 "mov %%"R"dx, %c[rdx](%0) \n\t"
3737 "mov %%"R"si, %c[rsi](%0) \n\t"
3738 "mov %%"R"di, %c[rdi](%0) \n\t"
3739 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003740#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003741 "mov %%r8, %c[r8](%0) \n\t"
3742 "mov %%r9, %c[r9](%0) \n\t"
3743 "mov %%r10, %c[r10](%0) \n\t"
3744 "mov %%r11, %c[r11](%0) \n\t"
3745 "mov %%r12, %c[r12](%0) \n\t"
3746 "mov %%r13, %c[r13](%0) \n\t"
3747 "mov %%r14, %c[r14](%0) \n\t"
3748 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003749#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003750 "mov %%cr2, %%"R"ax \n\t"
3751 "mov %%"R"ax, %c[cr2](%0) \n\t"
3752
3753 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003754 "setbe %c[fail](%0) \n\t"
3755 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3756 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3757 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03003758 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003759 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3760 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3761 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3762 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3763 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3764 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3765 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003766#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003767 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3768 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3769 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3770 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3771 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3772 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3773 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3774 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003775#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003776 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003777 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003778 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003779#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003780 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3781#endif
3782 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003783
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003784 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
3785 | (1 << VCPU_EXREG_PDPTR));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003786 vcpu->arch.regs_dirty = 0;
3787
Avi Kivitye8a48342009-09-01 16:06:25 +03003788 if (vcpu->arch.switch_db_regs)
3789 get_debugreg(vcpu->arch.dr6, 6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003790
Avi Kivity1155f762007-11-22 11:30:47 +02003791 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003792 if (vmx->rmode.irq.pending)
3793 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003794
Mike Dayd77c26f2007-10-08 09:02:08 -04003795 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003796 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003797
Avi Kivitycf393f72008-07-01 16:20:21 +03003798 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003799}
3800
Avi Kivityc8019492008-07-14 14:44:59 +03003801#undef R
3802#undef Q
3803
Avi Kivity6aa8b732006-12-10 02:21:36 -08003804static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3805{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003806 struct vcpu_vmx *vmx = to_vmx(vcpu);
3807
3808 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003809 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003810 free_vmcs(vmx->vmcs);
3811 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003812 }
3813}
3814
3815static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3816{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003817 struct vcpu_vmx *vmx = to_vmx(vcpu);
3818
Sheng Yang2384d2b2008-01-17 15:14:33 +08003819 spin_lock(&vmx_vpid_lock);
3820 if (vmx->vpid != 0)
3821 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3822 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003823 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003824 kfree(vmx->host_msrs);
3825 kfree(vmx->guest_msrs);
3826 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003827 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003828}
3829
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003830static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003831{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003832 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003833 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003834 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003835
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003836 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003837 return ERR_PTR(-ENOMEM);
3838
Sheng Yang2384d2b2008-01-17 15:14:33 +08003839 allocate_vpid(vmx);
3840
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003841 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3842 if (err)
3843 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003844
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003845 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003846 if (!vmx->guest_msrs) {
3847 err = -ENOMEM;
3848 goto uninit_vcpu;
3849 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003850
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003851 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3852 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003853 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003854
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003855 vmx->vmcs = alloc_vmcs();
3856 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003857 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003858
3859 vmcs_clear(vmx->vmcs);
3860
Avi Kivity15ad7142007-07-11 18:17:21 +03003861 cpu = get_cpu();
3862 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003863 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003864 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003865 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003866 if (err)
3867 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003868 if (vm_need_virtualize_apic_accesses(kvm))
3869 if (alloc_apic_access_page(kvm) != 0)
3870 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003871
Sheng Yangb927a3c2009-07-21 10:42:48 +08003872 if (enable_ept) {
3873 if (!kvm->arch.ept_identity_map_addr)
3874 kvm->arch.ept_identity_map_addr =
3875 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003876 if (alloc_identity_pagetable(kvm) != 0)
3877 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003878 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003879
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003880 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003881
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003882free_vmcs:
3883 free_vmcs(vmx->vmcs);
3884free_msrs:
3885 kfree(vmx->host_msrs);
3886free_guest_msrs:
3887 kfree(vmx->guest_msrs);
3888uninit_vcpu:
3889 kvm_vcpu_uninit(&vmx->vcpu);
3890free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003891 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003892 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003893}
3894
Yang, Sheng002c7f72007-07-31 14:23:01 +03003895static void __init vmx_check_processor_compat(void *rtn)
3896{
3897 struct vmcs_config vmcs_conf;
3898
3899 *(int *)rtn = 0;
3900 if (setup_vmcs_config(&vmcs_conf) < 0)
3901 *(int *)rtn = -EIO;
3902 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3903 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3904 smp_processor_id());
3905 *(int *)rtn = -EIO;
3906 }
3907}
3908
Sheng Yang67253af2008-04-25 10:20:22 +08003909static int get_ept_level(void)
3910{
3911 return VMX_EPT_DEFAULT_GAW + 1;
3912}
3913
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003914static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003915{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003916 u64 ret;
3917
Sheng Yang522c68c2009-04-27 20:35:43 +08003918 /* For VT-d and EPT combination
3919 * 1. MMIO: always map as UC
3920 * 2. EPT with VT-d:
3921 * a. VT-d without snooping control feature: can't guarantee the
3922 * result, try to trust guest.
3923 * b. VT-d with snooping control feature: snooping control feature of
3924 * VT-d engine can guarantee the cache correctness. Just set it
3925 * to WB to keep consistent with host. So the same as item 3.
3926 * 3. EPT without VT-d: always map as WB and set IGMT=1 to keep
3927 * consistent with host MTRR
3928 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003929 if (is_mmio)
3930 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08003931 else if (vcpu->kvm->arch.iommu_domain &&
3932 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
3933 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
3934 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003935 else
Sheng Yang522c68c2009-04-27 20:35:43 +08003936 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
3937 | VMX_EPT_IGMT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003938
3939 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08003940}
3941
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003942static const struct trace_print_flags vmx_exit_reasons_str[] = {
3943 { EXIT_REASON_EXCEPTION_NMI, "exception" },
3944 { EXIT_REASON_EXTERNAL_INTERRUPT, "ext_irq" },
3945 { EXIT_REASON_TRIPLE_FAULT, "triple_fault" },
3946 { EXIT_REASON_NMI_WINDOW, "nmi_window" },
3947 { EXIT_REASON_IO_INSTRUCTION, "io_instruction" },
3948 { EXIT_REASON_CR_ACCESS, "cr_access" },
3949 { EXIT_REASON_DR_ACCESS, "dr_access" },
3950 { EXIT_REASON_CPUID, "cpuid" },
3951 { EXIT_REASON_MSR_READ, "rdmsr" },
3952 { EXIT_REASON_MSR_WRITE, "wrmsr" },
3953 { EXIT_REASON_PENDING_INTERRUPT, "interrupt_window" },
3954 { EXIT_REASON_HLT, "halt" },
3955 { EXIT_REASON_INVLPG, "invlpg" },
3956 { EXIT_REASON_VMCALL, "hypercall" },
3957 { EXIT_REASON_TPR_BELOW_THRESHOLD, "tpr_below_thres" },
3958 { EXIT_REASON_APIC_ACCESS, "apic_access" },
3959 { EXIT_REASON_WBINVD, "wbinvd" },
3960 { EXIT_REASON_TASK_SWITCH, "task_switch" },
3961 { EXIT_REASON_EPT_VIOLATION, "ept_violation" },
3962 { -1, NULL }
3963};
3964
Joerg Roedel344f4142009-07-27 16:30:48 +02003965static bool vmx_gb_page_enable(void)
3966{
3967 return false;
3968}
3969
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003970static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003971 .cpu_has_kvm_support = cpu_has_kvm_support,
3972 .disabled_by_bios = vmx_disabled_by_bios,
3973 .hardware_setup = hardware_setup,
3974 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003975 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003976 .hardware_enable = hardware_enable,
3977 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08003978 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003979
3980 .vcpu_create = vmx_create_vcpu,
3981 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003982 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003983
Avi Kivity04d2cc72007-09-10 18:10:54 +03003984 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003985 .vcpu_load = vmx_vcpu_load,
3986 .vcpu_put = vmx_vcpu_put,
3987
3988 .set_guest_debug = set_guest_debug,
3989 .get_msr = vmx_get_msr,
3990 .set_msr = vmx_set_msr,
3991 .get_segment_base = vmx_get_segment_base,
3992 .get_segment = vmx_get_segment,
3993 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003994 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003995 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003996 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003997 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003998 .set_cr3 = vmx_set_cr3,
3999 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004000 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004001 .get_idt = vmx_get_idt,
4002 .set_idt = vmx_set_idt,
4003 .get_gdt = vmx_get_gdt,
4004 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004005 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004006 .get_rflags = vmx_get_rflags,
4007 .set_rflags = vmx_set_rflags,
4008
4009 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004010
Avi Kivity6aa8b732006-12-10 02:21:36 -08004011 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02004012 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004013 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004014 .set_interrupt_shadow = vmx_set_interrupt_shadow,
4015 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004016 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004017 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004018 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004019 .queue_exception = vmx_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02004020 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004021 .nmi_allowed = vmx_nmi_allowed,
4022 .enable_nmi_window = enable_nmi_window,
4023 .enable_irq_window = enable_irq_window,
4024 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004025
Izik Eiduscbc94022007-10-25 00:29:55 +02004026 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004027 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004028 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004029
4030 .exit_reasons_str = vmx_exit_reasons_str,
Joerg Roedel344f4142009-07-27 16:30:48 +02004031 .gb_page_enable = vmx_gb_page_enable,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004032};
4033
4034static int __init vmx_init(void)
4035{
He, Qingfdef3ad2007-04-30 09:45:24 +03004036 int r;
4037
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004038 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004039 if (!vmx_io_bitmap_a)
4040 return -ENOMEM;
4041
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004042 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004043 if (!vmx_io_bitmap_b) {
4044 r = -ENOMEM;
4045 goto out;
4046 }
4047
Avi Kivity58972972009-02-24 22:26:47 +02004048 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
4049 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08004050 r = -ENOMEM;
4051 goto out1;
4052 }
4053
Avi Kivity58972972009-02-24 22:26:47 +02004054 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
4055 if (!vmx_msr_bitmap_longmode) {
4056 r = -ENOMEM;
4057 goto out2;
4058 }
4059
He, Qingfdef3ad2007-04-30 09:45:24 +03004060 /*
4061 * Allow direct access to the PC debug port (it is often used for I/O
4062 * delays, but the vmexits simply slow things down).
4063 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004064 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
4065 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004066
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004067 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004068
Avi Kivity58972972009-02-24 22:26:47 +02004069 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
4070 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08004071
Sheng Yang2384d2b2008-01-17 15:14:33 +08004072 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
4073
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004074 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004075 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02004076 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08004077
Avi Kivity58972972009-02-24 22:26:47 +02004078 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
4079 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
4080 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
4081 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
4082 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
4083 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03004084
Avi Kivity089d0342009-03-23 18:26:32 +02004085 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004086 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004087 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08004088 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08004089 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004090 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004091 kvm_enable_tdp();
4092 } else
4093 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08004094
Avi Kivityc7addb92007-09-16 18:58:32 +02004095 if (bypass_guest_pf)
4096 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
4097
He, Qingfdef3ad2007-04-30 09:45:24 +03004098 return 0;
4099
Avi Kivity58972972009-02-24 22:26:47 +02004100out3:
4101 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08004102out2:
Avi Kivity58972972009-02-24 22:26:47 +02004103 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03004104out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004105 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03004106out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004107 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004108 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004109}
4110
4111static void __exit vmx_exit(void)
4112{
Avi Kivity58972972009-02-24 22:26:47 +02004113 free_page((unsigned long)vmx_msr_bitmap_legacy);
4114 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004115 free_page((unsigned long)vmx_io_bitmap_b);
4116 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004117
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004118 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004119}
4120
4121module_init(vmx_init)
4122module_exit(vmx_exit)