blob: bf46253149c3ac0c134265b652e6e7f40a87119c [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040020
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020023#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/mm.h>
25#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040026#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020027#include <linux/moduleparam.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030028#include <linux/ftrace_event.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030029#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030030#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080033#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020034#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020035#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080036#include <asm/mce.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080037
Marcelo Tosatti229456f2009-06-17 09:22:14 -030038#include "trace.h"
39
Avi Kivity4ecac3f2008-05-13 13:23:38 +030040#define __ex(x) __kvm_handle_fault_on_reboot(x)
41
Avi Kivity6aa8b732006-12-10 02:21:36 -080042MODULE_AUTHOR("Qumranet");
43MODULE_LICENSE("GPL");
44
Avi Kivity4462d212009-03-23 17:53:37 +020045static int __read_mostly bypass_guest_pf = 1;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020046module_param(bypass_guest_pf, bool, S_IRUGO);
Avi Kivityc7addb92007-09-16 18:58:32 +020047
Avi Kivity4462d212009-03-23 17:53:37 +020048static int __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020049module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080050
Avi Kivity4462d212009-03-23 17:53:37 +020051static int __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020052module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020053
Avi Kivity4462d212009-03-23 17:53:37 +020054static int __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020055module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080056
Nitin A Kamble3a624e22009-06-08 11:34:16 -070057static int __read_mostly enable_unrestricted_guest = 1;
58module_param_named(unrestricted_guest,
59 enable_unrestricted_guest, bool, S_IRUGO);
60
Avi Kivity4462d212009-03-23 17:53:37 +020061static int __read_mostly emulate_invalid_guest_state = 0;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020062module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030063
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +080064/*
65 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
66 * ple_gap: upper bound on the amount of time between two successive
67 * executions of PAUSE in a loop. Also indicate if ple enabled.
68 * According to test, this time is usually small than 41 cycles.
69 * ple_window: upper bound on the amount of time a guest is allowed to execute
70 * in a PAUSE loop. Tests indicate that most spinlocks are held for
71 * less than 2^12 cycles
72 * Time is measured based on a counter that runs at the same rate as the TSC,
73 * refer SDM volume 3b section 21.6.13 & 22.1.3.
74 */
75#define KVM_VMX_DEFAULT_PLE_GAP 41
76#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
77static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
78module_param(ple_gap, int, S_IRUGO);
79
80static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
81module_param(ple_window, int, S_IRUGO);
82
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040083struct vmcs {
84 u32 revision_id;
85 u32 abort;
86 char data[0];
87};
88
Avi Kivity26bb0982009-09-07 11:14:12 +030089struct shared_msr_entry {
90 unsigned index;
91 u64 data;
92};
93
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040094struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100095 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +030096 struct list_head local_vcpus_link;
Avi Kivity313dbd492008-07-17 18:04:30 +030097 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040098 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030099 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +0200100 u32 idt_vectoring_info;
Avi Kivity26bb0982009-09-07 11:14:12 +0300101 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400102 int nmsrs;
103 int save_nmsrs;
104 int msr_offset_efer;
105#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300106 u64 msr_host_kernel_gs_base;
107 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400108#endif
109 struct vmcs *vmcs;
110 struct {
111 int loaded;
112 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200113 int gs_ldt_reload_needed;
114 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400115 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200116 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300117 int vm86_active;
118 u8 save_iopl;
119 struct kvm_save_segment {
120 u16 selector;
121 unsigned long base;
122 u32 limit;
123 u32 ar;
124 } tr, es, ds, fs, gs;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200125 struct {
126 bool pending;
127 u8 vector;
128 unsigned rip;
129 } irq;
130 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800131 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300132 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200133
134 /* Support for vnmi-less CPUs */
135 int soft_vnmi_blocked;
136 ktime_t entry_time;
137 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800138 u32 exit_reason;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400139};
140
141static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
142{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000143 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400144}
145
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800146static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800147static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300148
Avi Kivity6aa8b732006-12-10 02:21:36 -0800149static DEFINE_PER_CPU(struct vmcs *, vmxarea);
150static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300151static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800152
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200153static unsigned long *vmx_io_bitmap_a;
154static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200155static unsigned long *vmx_msr_bitmap_legacy;
156static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300157
Sheng Yang2384d2b2008-01-17 15:14:33 +0800158static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
159static DEFINE_SPINLOCK(vmx_vpid_lock);
160
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300161static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162 int size;
163 int order;
164 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300165 u32 pin_based_exec_ctrl;
166 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800167 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300168 u32 vmexit_ctrl;
169 u32 vmentry_ctrl;
170} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171
Hannes Ederefff9e52008-11-28 17:02:06 +0100172static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800173 u32 ept;
174 u32 vpid;
175} vmx_capability;
176
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177#define VMX_SEGMENT_FIELD(seg) \
178 [VCPU_SREG_##seg] = { \
179 .selector = GUEST_##seg##_SELECTOR, \
180 .base = GUEST_##seg##_BASE, \
181 .limit = GUEST_##seg##_LIMIT, \
182 .ar_bytes = GUEST_##seg##_AR_BYTES, \
183 }
184
185static struct kvm_vmx_segment_field {
186 unsigned selector;
187 unsigned base;
188 unsigned limit;
189 unsigned ar_bytes;
190} kvm_vmx_segment_fields[] = {
191 VMX_SEGMENT_FIELD(CS),
192 VMX_SEGMENT_FIELD(DS),
193 VMX_SEGMENT_FIELD(ES),
194 VMX_SEGMENT_FIELD(FS),
195 VMX_SEGMENT_FIELD(GS),
196 VMX_SEGMENT_FIELD(SS),
197 VMX_SEGMENT_FIELD(TR),
198 VMX_SEGMENT_FIELD(LDTR),
199};
200
Avi Kivity26bb0982009-09-07 11:14:12 +0300201static u64 host_efer;
202
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300203static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
204
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300205/*
206 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
207 * away by decrementing the array size.
208 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800209static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800210#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300211 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212#endif
213 MSR_EFER, MSR_K6_STAR,
214};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200215#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800216
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217static inline int is_page_fault(u32 intr_info)
218{
219 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
220 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100221 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222}
223
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300224static inline int is_no_device(u32 intr_info)
225{
226 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
227 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100228 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300229}
230
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500231static inline int is_invalid_opcode(u32 intr_info)
232{
233 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
234 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100235 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500236}
237
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238static inline int is_external_interrupt(u32 intr_info)
239{
240 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
241 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
242}
243
Andi Kleena0861c02009-06-08 17:37:09 +0800244static inline int is_machine_check(u32 intr_info)
245{
246 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
247 INTR_INFO_VALID_MASK)) ==
248 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
249}
250
Sheng Yang25c5f222008-03-28 13:18:56 +0800251static inline int cpu_has_vmx_msr_bitmap(void)
252{
Sheng Yang04547152009-04-01 15:52:31 +0800253 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800254}
255
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800256static inline int cpu_has_vmx_tpr_shadow(void)
257{
Sheng Yang04547152009-04-01 15:52:31 +0800258 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800259}
260
261static inline int vm_need_tpr_shadow(struct kvm *kvm)
262{
Sheng Yang04547152009-04-01 15:52:31 +0800263 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800264}
265
Sheng Yangf78e0e22007-10-29 09:40:42 +0800266static inline int cpu_has_secondary_exec_ctrls(void)
267{
Sheng Yang04547152009-04-01 15:52:31 +0800268 return vmcs_config.cpu_based_exec_ctrl &
269 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800270}
271
Avi Kivity774ead32007-12-26 13:57:04 +0200272static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800273{
Sheng Yang04547152009-04-01 15:52:31 +0800274 return vmcs_config.cpu_based_2nd_exec_ctrl &
275 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
276}
277
278static inline bool cpu_has_vmx_flexpriority(void)
279{
280 return cpu_has_vmx_tpr_shadow() &&
281 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800282}
283
Marcelo Tosattie7997942009-06-11 12:07:40 -0300284static inline bool cpu_has_vmx_ept_execute_only(void)
285{
286 return !!(vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT);
287}
288
289static inline bool cpu_has_vmx_eptp_uncacheable(void)
290{
291 return !!(vmx_capability.ept & VMX_EPTP_UC_BIT);
292}
293
294static inline bool cpu_has_vmx_eptp_writeback(void)
295{
296 return !!(vmx_capability.ept & VMX_EPTP_WB_BIT);
297}
298
299static inline bool cpu_has_vmx_ept_2m_page(void)
300{
301 return !!(vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT);
302}
303
Sheng Yangd56f5462008-04-25 10:13:16 +0800304static inline int cpu_has_vmx_invept_individual_addr(void)
305{
Sheng Yang04547152009-04-01 15:52:31 +0800306 return !!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800307}
308
309static inline int cpu_has_vmx_invept_context(void)
310{
Sheng Yang04547152009-04-01 15:52:31 +0800311 return !!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800312}
313
314static inline int cpu_has_vmx_invept_global(void)
315{
Sheng Yang04547152009-04-01 15:52:31 +0800316 return !!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800317}
318
319static inline int cpu_has_vmx_ept(void)
320{
Sheng Yang04547152009-04-01 15:52:31 +0800321 return vmcs_config.cpu_based_2nd_exec_ctrl &
322 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800323}
324
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700325static inline int cpu_has_vmx_unrestricted_guest(void)
326{
327 return vmcs_config.cpu_based_2nd_exec_ctrl &
328 SECONDARY_EXEC_UNRESTRICTED_GUEST;
329}
330
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800331static inline int cpu_has_vmx_ple(void)
332{
333 return vmcs_config.cpu_based_2nd_exec_ctrl &
334 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
335}
336
Sheng Yangf78e0e22007-10-29 09:40:42 +0800337static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
338{
Sheng Yang04547152009-04-01 15:52:31 +0800339 return flexpriority_enabled &&
340 (cpu_has_vmx_virtualize_apic_accesses()) &&
341 (irqchip_in_kernel(kvm));
Sheng Yangf78e0e22007-10-29 09:40:42 +0800342}
343
Sheng Yang2384d2b2008-01-17 15:14:33 +0800344static inline int cpu_has_vmx_vpid(void)
345{
Sheng Yang04547152009-04-01 15:52:31 +0800346 return vmcs_config.cpu_based_2nd_exec_ctrl &
347 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800348}
349
Sheng Yangf08864b2008-05-15 18:23:25 +0800350static inline int cpu_has_virtual_nmis(void)
351{
352 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
353}
354
Sheng Yang04547152009-04-01 15:52:31 +0800355static inline bool report_flexpriority(void)
356{
357 return flexpriority_enabled;
358}
359
Rusty Russell8b9cf982007-07-30 16:31:43 +1000360static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800361{
362 int i;
363
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400364 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300365 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300366 return i;
367 return -1;
368}
369
Sheng Yang2384d2b2008-01-17 15:14:33 +0800370static inline void __invvpid(int ext, u16 vpid, gva_t gva)
371{
372 struct {
373 u64 vpid : 16;
374 u64 rsvd : 48;
375 u64 gva;
376 } operand = { vpid, 0, gva };
377
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300378 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800379 /* CF==1 or ZF==1 --> rc = -1 */
380 "; ja 1f ; ud2 ; 1:"
381 : : "a"(&operand), "c"(ext) : "cc", "memory");
382}
383
Sheng Yang14394422008-04-28 12:24:45 +0800384static inline void __invept(int ext, u64 eptp, gpa_t gpa)
385{
386 struct {
387 u64 eptp, gpa;
388 } operand = {eptp, gpa};
389
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300390 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800391 /* CF==1 or ZF==1 --> rc = -1 */
392 "; ja 1f ; ud2 ; 1:\n"
393 : : "a" (&operand), "c" (ext) : "cc", "memory");
394}
395
Avi Kivity26bb0982009-09-07 11:14:12 +0300396static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300397{
398 int i;
399
Rusty Russell8b9cf982007-07-30 16:31:43 +1000400 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300401 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400402 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000403 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800404}
405
Avi Kivity6aa8b732006-12-10 02:21:36 -0800406static void vmcs_clear(struct vmcs *vmcs)
407{
408 u64 phys_addr = __pa(vmcs);
409 u8 error;
410
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300411 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800412 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
413 : "cc", "memory");
414 if (error)
415 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
416 vmcs, phys_addr);
417}
418
419static void __vcpu_clear(void *arg)
420{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000421 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800422 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800423
Rusty Russell8b9cf982007-07-30 16:31:43 +1000424 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400425 vmcs_clear(vmx->vmcs);
426 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800428 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300429 list_del(&vmx->local_vcpus_link);
430 vmx->vcpu.cpu = -1;
431 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800432}
433
Rusty Russell8b9cf982007-07-30 16:31:43 +1000434static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800435{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200436 if (vmx->vcpu.cpu == -1)
437 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200438 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800439}
440
Sheng Yang2384d2b2008-01-17 15:14:33 +0800441static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
442{
443 if (vmx->vpid == 0)
444 return;
445
446 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
447}
448
Sheng Yang14394422008-04-28 12:24:45 +0800449static inline void ept_sync_global(void)
450{
451 if (cpu_has_vmx_invept_global())
452 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
453}
454
455static inline void ept_sync_context(u64 eptp)
456{
Avi Kivity089d0342009-03-23 18:26:32 +0200457 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800458 if (cpu_has_vmx_invept_context())
459 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
460 else
461 ept_sync_global();
462 }
463}
464
465static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
466{
Avi Kivity089d0342009-03-23 18:26:32 +0200467 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800468 if (cpu_has_vmx_invept_individual_addr())
469 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
470 eptp, gpa);
471 else
472 ept_sync_context(eptp);
473 }
474}
475
Avi Kivity6aa8b732006-12-10 02:21:36 -0800476static unsigned long vmcs_readl(unsigned long field)
477{
478 unsigned long value;
479
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300480 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481 : "=a"(value) : "d"(field) : "cc");
482 return value;
483}
484
485static u16 vmcs_read16(unsigned long field)
486{
487 return vmcs_readl(field);
488}
489
490static u32 vmcs_read32(unsigned long field)
491{
492 return vmcs_readl(field);
493}
494
495static u64 vmcs_read64(unsigned long field)
496{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800497#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498 return vmcs_readl(field);
499#else
500 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
501#endif
502}
503
Avi Kivitye52de1b2007-01-05 16:36:56 -0800504static noinline void vmwrite_error(unsigned long field, unsigned long value)
505{
506 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
507 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
508 dump_stack();
509}
510
Avi Kivity6aa8b732006-12-10 02:21:36 -0800511static void vmcs_writel(unsigned long field, unsigned long value)
512{
513 u8 error;
514
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300515 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400516 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800517 if (unlikely(error))
518 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519}
520
521static void vmcs_write16(unsigned long field, u16 value)
522{
523 vmcs_writel(field, value);
524}
525
526static void vmcs_write32(unsigned long field, u32 value)
527{
528 vmcs_writel(field, value);
529}
530
531static void vmcs_write64(unsigned long field, u64 value)
532{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300534#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800535 asm volatile ("");
536 vmcs_writel(field+1, value >> 32);
537#endif
538}
539
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300540static void vmcs_clear_bits(unsigned long field, u32 mask)
541{
542 vmcs_writel(field, vmcs_readl(field) & ~mask);
543}
544
545static void vmcs_set_bits(unsigned long field, u32 mask)
546{
547 vmcs_writel(field, vmcs_readl(field) | mask);
548}
549
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300550static void update_exception_bitmap(struct kvm_vcpu *vcpu)
551{
552 u32 eb;
553
Andi Kleena0861c02009-06-08 17:37:09 +0800554 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300555 if (!vcpu->fpu_active)
556 eb |= 1u << NM_VECTOR;
Avi Kivitye8a48342009-09-01 16:06:25 +0300557 /*
558 * Unconditionally intercept #DB so we can maintain dr6 without
559 * reading it every exit.
560 */
561 eb |= 1u << DB_VECTOR;
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100562 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100563 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
564 eb |= 1u << BP_VECTOR;
565 }
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300566 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300567 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200568 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800569 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300570 vmcs_write32(EXCEPTION_BITMAP, eb);
571}
572
Avi Kivity33ed6322007-05-02 16:54:03 +0300573static void reload_tss(void)
574{
Avi Kivity33ed6322007-05-02 16:54:03 +0300575 /*
576 * VT restores TR but not its size. Useless.
577 */
578 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200579 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300580
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300581 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300582 descs = (void *)gdt.base;
583 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
584 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300585}
586
Avi Kivity26bb0982009-09-07 11:14:12 +0300587static bool update_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300588{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400589 int efer_offset = vmx->msr_offset_efer;
Roel Kluin3a34a882009-08-04 02:08:45 -0700590 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300591 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300592
Avi Kivity51c6cf62007-08-29 03:48:05 +0300593 if (efer_offset < 0)
Avi Kivity26bb0982009-09-07 11:14:12 +0300594 return false;
595 guest_efer = vmx->vcpu.arch.shadow_efer;
Roel Kluin3a34a882009-08-04 02:08:45 -0700596
Avi Kivity51c6cf62007-08-29 03:48:05 +0300597 /*
598 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
599 * outside long mode
600 */
601 ignore_bits = EFER_NX | EFER_SCE;
602#ifdef CONFIG_X86_64
603 ignore_bits |= EFER_LMA | EFER_LME;
604 /* SCE is meaningful only in long mode on Intel */
605 if (guest_efer & EFER_LMA)
606 ignore_bits &= ~(u64)EFER_SCE;
607#endif
608 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
Avi Kivity26bb0982009-09-07 11:14:12 +0300609 return false;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300610
Avi Kivity51c6cf62007-08-29 03:48:05 +0300611 guest_efer &= ~ignore_bits;
612 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +0300613 vmx->guest_msrs[efer_offset].data = guest_efer;
614 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300615}
616
Avi Kivity04d2cc72007-09-10 18:10:54 +0300617static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300618{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300619 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +0300620 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300621
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400622 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300623 return;
624
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400625 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300626 /*
627 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
628 * allow segment selectors with cpl > 0 or ti == 1.
629 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300630 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200631 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300632 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200633 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400634 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200635 vmx->host_state.fs_reload_needed = 0;
636 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300637 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200638 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300639 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300640 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400641 if (!(vmx->host_state.gs_sel & 7))
642 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300643 else {
644 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200645 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300646 }
647
648#ifdef CONFIG_X86_64
649 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
650 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
651#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400652 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
653 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300654#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300655
656#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300657 if (is_long_mode(&vmx->vcpu)) {
658 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
659 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
660 }
Avi Kivity707c0872007-05-02 17:33:43 +0300661#endif
Avi Kivity26bb0982009-09-07 11:14:12 +0300662 for (i = 0; i < vmx->save_nmsrs; ++i)
663 kvm_set_shared_msr(vmx->guest_msrs[i].index,
664 vmx->guest_msrs[i].data);
Avi Kivity33ed6322007-05-02 16:54:03 +0300665}
666
Avi Kivitya9b21b62008-06-24 11:48:49 +0300667static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300668{
Avi Kivity15ad7142007-07-11 18:17:21 +0300669 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300670
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400671 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300672 return;
673
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200674 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400675 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200676 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300677 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200678 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300679 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300680 /*
681 * If we have to reload gs, we must take care to
682 * preserve our gs base.
683 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300684 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300685 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300686#ifdef CONFIG_X86_64
687 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
688#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300689 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300690 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200691 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +0300692#ifdef CONFIG_X86_64
693 if (is_long_mode(&vmx->vcpu)) {
694 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
695 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
696 }
697#endif
Avi Kivity33ed6322007-05-02 16:54:03 +0300698}
699
Avi Kivitya9b21b62008-06-24 11:48:49 +0300700static void vmx_load_host_state(struct vcpu_vmx *vmx)
701{
702 preempt_disable();
703 __vmx_load_host_state(vmx);
704 preempt_enable();
705}
706
Avi Kivity6aa8b732006-12-10 02:21:36 -0800707/*
708 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
709 * vcpu mutex is already taken.
710 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300711static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400713 struct vcpu_vmx *vmx = to_vmx(vcpu);
714 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200715 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716
Eddie Donga3d7f852007-09-03 16:15:12 +0300717 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000718 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300719 kvm_migrate_timers(vcpu);
Marcelo Tosattieb5109e2009-10-01 19:16:58 -0300720 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
Avi Kivity543e4242008-05-13 16:22:47 +0300721 local_irq_disable();
722 list_add(&vmx->local_vcpus_link,
723 &per_cpu(vcpus_on_cpu, cpu));
724 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300725 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800726
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400727 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728 u8 error;
729
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400730 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300731 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800732 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
733 : "cc");
734 if (error)
735 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400736 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737 }
738
739 if (vcpu->cpu != cpu) {
740 struct descriptor_table dt;
741 unsigned long sysenter_esp;
742
743 vcpu->cpu = cpu;
744 /*
745 * Linux uses per-cpu TSS and GDT, so set these when switching
746 * processors.
747 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300748 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
749 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800750 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
751
752 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
753 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300754
755 /*
756 * Make sure the time stamp counter is monotonous.
757 */
758 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200759 if (tsc_this < vcpu->arch.host_tsc) {
760 delta = vcpu->arch.host_tsc - tsc_this;
761 new_offset = vmcs_read64(TSC_OFFSET) + delta;
762 vmcs_write64(TSC_OFFSET, new_offset);
763 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800765}
766
767static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
768{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300769 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770}
771
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300772static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
773{
774 if (vcpu->fpu_active)
775 return;
776 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000777 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800778 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92fa2007-07-17 23:19:08 +1000779 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300780 update_exception_bitmap(vcpu);
781}
782
783static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
784{
785 if (!vcpu->fpu_active)
786 return;
787 vcpu->fpu_active = 0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000788 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300789 update_exception_bitmap(vcpu);
790}
791
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
793{
Avi Kivity345dcaa2009-08-12 15:29:37 +0300794 unsigned long rflags;
795
796 rflags = vmcs_readl(GUEST_RFLAGS);
797 if (to_vmx(vcpu)->rmode.vm86_active)
798 rflags &= ~(unsigned long)(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
799 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800}
801
802static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
803{
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300804 if (to_vmx(vcpu)->rmode.vm86_active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100805 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806 vmcs_writel(GUEST_RFLAGS, rflags);
807}
808
Glauber Costa2809f5d2009-05-12 16:21:05 -0400809static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
810{
811 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
812 int ret = 0;
813
814 if (interruptibility & GUEST_INTR_STATE_STI)
815 ret |= X86_SHADOW_INT_STI;
816 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
817 ret |= X86_SHADOW_INT_MOV_SS;
818
819 return ret & mask;
820}
821
822static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
823{
824 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
825 u32 interruptibility = interruptibility_old;
826
827 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
828
829 if (mask & X86_SHADOW_INT_MOV_SS)
830 interruptibility |= GUEST_INTR_STATE_MOV_SS;
831 if (mask & X86_SHADOW_INT_STI)
832 interruptibility |= GUEST_INTR_STATE_STI;
833
834 if ((interruptibility != interruptibility_old))
835 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
836}
837
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
839{
840 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300842 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800843 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300844 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845
Glauber Costa2809f5d2009-05-12 16:21:05 -0400846 /* skipping an emulated instruction also counts */
847 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848}
849
Avi Kivity298101d2007-11-25 13:41:11 +0200850static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
851 bool has_error_code, u32 error_code)
852{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200853 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100854 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200855
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100856 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200857 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100858 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
859 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200860
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300861 if (vmx->rmode.vm86_active) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200862 vmx->rmode.irq.pending = true;
863 vmx->rmode.irq.vector = nr;
864 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +0300865 if (kvm_exception_is_soft(nr))
866 vmx->rmode.irq.rip +=
867 vmx->vcpu.arch.event_exit_inst_len;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100868 intr_info |= INTR_TYPE_SOFT_INTR;
869 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200870 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
871 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
872 return;
873 }
874
Gleb Natapov66fd3f72009-05-11 13:35:50 +0300875 if (kvm_exception_is_soft(nr)) {
876 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
877 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100878 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
879 } else
880 intr_info |= INTR_TYPE_HARD_EXCEPTION;
881
882 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200883}
884
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885/*
Eddie Donga75beee2007-05-17 18:55:15 +0300886 * Swap MSR entry in host/guest MSR entry array.
887 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000888static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300889{
Avi Kivity26bb0982009-09-07 11:14:12 +0300890 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400891
892 tmp = vmx->guest_msrs[to];
893 vmx->guest_msrs[to] = vmx->guest_msrs[from];
894 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300895}
896
897/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300898 * Set up the vmcs to automatically save and restore system
899 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
900 * mode, as fiddling with msrs is very expensive.
901 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000902static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300903{
Avi Kivity26bb0982009-09-07 11:14:12 +0300904 int save_nmsrs, index;
Avi Kivity58972972009-02-24 22:26:47 +0200905 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +0300906
Avi Kivity33f9c502008-02-27 16:06:57 +0200907 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300908 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300909#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000910 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000911 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300912 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000913 move_msr_up(vmx, index, save_nmsrs++);
914 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300915 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000916 move_msr_up(vmx, index, save_nmsrs++);
917 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300918 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000919 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300920 /*
921 * MSR_K6_STAR is only needed on long mode guests, and only
922 * if efer.sce is enabled.
923 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000924 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800925 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000926 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300927 }
Eddie Donga75beee2007-05-17 18:55:15 +0300928#endif
Avi Kivity26bb0982009-09-07 11:14:12 +0300929 vmx->msr_offset_efer = index = __find_msr_index(vmx, MSR_EFER);
930 if (index >= 0 && update_transition_efer(vmx))
931 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300932
Avi Kivity26bb0982009-09-07 11:14:12 +0300933 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +0200934
935 if (cpu_has_vmx_msr_bitmap()) {
936 if (is_long_mode(&vmx->vcpu))
937 msr_bitmap = vmx_msr_bitmap_longmode;
938 else
939 msr_bitmap = vmx_msr_bitmap_legacy;
940
941 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
942 }
Avi Kivitye38aea32007-04-19 13:22:48 +0300943}
944
945/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946 * reads and returns guest's timestamp counter "register"
947 * guest_tsc = host_tsc + tsc_offset -- 21.3
948 */
949static u64 guest_read_tsc(void)
950{
951 u64 host_tsc, tsc_offset;
952
953 rdtscll(host_tsc);
954 tsc_offset = vmcs_read64(TSC_OFFSET);
955 return host_tsc + tsc_offset;
956}
957
958/*
959 * writes 'guest_tsc' into guest's timestamp counter "register"
960 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
961 */
Marcelo Tosatti53f658b32008-12-11 20:45:05 +0100962static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
965}
966
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967/*
968 * Reads an msr value (of 'msr_index') into 'pdata'.
969 * Returns 0 on success, non-0 otherwise.
970 * Assumes vcpu_load() was already called.
971 */
972static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
973{
974 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +0300975 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976
977 if (!pdata) {
978 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
979 return -EINVAL;
980 }
981
982 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800983#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984 case MSR_FS_BASE:
985 data = vmcs_readl(GUEST_FS_BASE);
986 break;
987 case MSR_GS_BASE:
988 data = vmcs_readl(GUEST_GS_BASE);
989 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +0300990 case MSR_KERNEL_GS_BASE:
991 vmx_load_host_state(to_vmx(vcpu));
992 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
993 break;
Avi Kivity26bb0982009-09-07 11:14:12 +0300994#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800996 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530997 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 data = guest_read_tsc();
999 break;
1000 case MSR_IA32_SYSENTER_CS:
1001 data = vmcs_read32(GUEST_SYSENTER_CS);
1002 break;
1003 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001004 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005 break;
1006 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001007 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009 default:
Avi Kivity26bb0982009-09-07 11:14:12 +03001010 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +10001011 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001012 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001013 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity3bab1f52006-12-29 16:49:48 -08001014 data = msr->data;
1015 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001017 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018 }
1019
1020 *pdata = data;
1021 return 0;
1022}
1023
1024/*
1025 * Writes msr value into into the appropriate "register".
1026 * Returns 0 on success, non-0 otherwise.
1027 * Assumes vcpu_load() was already called.
1028 */
1029static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1030{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001031 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001032 struct shared_msr_entry *msr;
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01001033 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +03001034 int ret = 0;
1035
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001037 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001038 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001039 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001040 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001041#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042 case MSR_FS_BASE:
1043 vmcs_writel(GUEST_FS_BASE, data);
1044 break;
1045 case MSR_GS_BASE:
1046 vmcs_writel(GUEST_GS_BASE, data);
1047 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001048 case MSR_KERNEL_GS_BASE:
1049 vmx_load_host_state(vmx);
1050 vmx->msr_guest_kernel_gs_base = data;
1051 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001052#endif
1053 case MSR_IA32_SYSENTER_CS:
1054 vmcs_write32(GUEST_SYSENTER_CS, data);
1055 break;
1056 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001057 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058 break;
1059 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001060 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301062 case MSR_IA32_TSC:
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01001063 rdtscll(host_tsc);
1064 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001066 case MSR_IA32_CR_PAT:
1067 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1068 vmcs_write64(GUEST_IA32_PAT, data);
1069 vcpu->arch.pat = data;
1070 break;
1071 }
1072 /* Otherwise falls through to kvm_set_msr_common */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001074 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001075 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001076 vmx_load_host_state(vmx);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001077 msr->data = data;
1078 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001080 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081 }
1082
Eddie Dong2cc51562007-05-21 07:28:09 +03001083 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084}
1085
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001086static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001088 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1089 switch (reg) {
1090 case VCPU_REGS_RSP:
1091 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1092 break;
1093 case VCPU_REGS_RIP:
1094 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1095 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001096 case VCPU_EXREG_PDPTR:
1097 if (enable_ept)
1098 ept_save_pdptrs(vcpu);
1099 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001100 default:
1101 break;
1102 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001103}
1104
Jan Kiszka355be0b2009-10-03 00:31:21 +02001105static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106{
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001107 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1108 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1109 else
1110 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1111
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001112 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113}
1114
1115static __init int cpu_has_kvm_support(void)
1116{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001117 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118}
1119
1120static __init int vmx_disabled_by_bios(void)
1121{
1122 u64 msr;
1123
1124 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001125 return (msr & (FEATURE_CONTROL_LOCKED |
1126 FEATURE_CONTROL_VMXON_ENABLED))
1127 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001128 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129}
1130
Alexander Graf10474ae2009-09-15 11:37:46 +02001131static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001132{
1133 int cpu = raw_smp_processor_id();
1134 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1135 u64 old;
1136
Alexander Graf10474ae2009-09-15 11:37:46 +02001137 if (read_cr4() & X86_CR4_VMXE)
1138 return -EBUSY;
1139
Avi Kivity543e4242008-05-13 16:22:47 +03001140 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001142 if ((old & (FEATURE_CONTROL_LOCKED |
1143 FEATURE_CONTROL_VMXON_ENABLED))
1144 != (FEATURE_CONTROL_LOCKED |
1145 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001146 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001147 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001148 FEATURE_CONTROL_LOCKED |
1149 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001150 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001151 asm volatile (ASM_VMX_VMXON_RAX
1152 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153 : "memory", "cc");
Alexander Graf10474ae2009-09-15 11:37:46 +02001154
1155 ept_sync_global();
1156
1157 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158}
1159
Avi Kivity543e4242008-05-13 16:22:47 +03001160static void vmclear_local_vcpus(void)
1161{
1162 int cpu = raw_smp_processor_id();
1163 struct vcpu_vmx *vmx, *n;
1164
1165 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1166 local_vcpus_link)
1167 __vcpu_clear(vmx);
1168}
1169
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001170
1171/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1172 * tricks.
1173 */
1174static void kvm_cpu_vmxoff(void)
1175{
1176 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1177 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1178}
1179
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180static void hardware_disable(void *garbage)
1181{
Avi Kivity543e4242008-05-13 16:22:47 +03001182 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001183 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184}
1185
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001186static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001187 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188{
1189 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001190 u32 ctl = ctl_min | ctl_opt;
1191
1192 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1193
1194 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1195 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1196
1197 /* Ensure minimum (required) set of control bits are supported. */
1198 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001199 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001200
1201 *result = ctl;
1202 return 0;
1203}
1204
Yang, Sheng002c7f72007-07-31 14:23:01 +03001205static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001206{
1207 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001208 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001209 u32 _pin_based_exec_control = 0;
1210 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001211 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001212 u32 _vmexit_control = 0;
1213 u32 _vmentry_control = 0;
1214
1215 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001216 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001217 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1218 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001219 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001220
1221 min = CPU_BASED_HLT_EXITING |
1222#ifdef CONFIG_X86_64
1223 CPU_BASED_CR8_LOAD_EXITING |
1224 CPU_BASED_CR8_STORE_EXITING |
1225#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001226 CPU_BASED_CR3_LOAD_EXITING |
1227 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001228 CPU_BASED_USE_IO_BITMAPS |
1229 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001230 CPU_BASED_USE_TSC_OFFSETING |
1231 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001232 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001233 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001234 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001235 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1236 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001237 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001238#ifdef CONFIG_X86_64
1239 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1240 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1241 ~CPU_BASED_CR8_STORE_EXITING;
1242#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001243 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001244 min2 = 0;
1245 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001246 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001247 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001248 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001249 SECONDARY_EXEC_UNRESTRICTED_GUEST |
1250 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08001251 if (adjust_vmx_controls(min2, opt2,
1252 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001253 &_cpu_based_2nd_exec_control) < 0)
1254 return -EIO;
1255 }
1256#ifndef CONFIG_X86_64
1257 if (!(_cpu_based_2nd_exec_control &
1258 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1259 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1260#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001261 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001262 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1263 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03001264 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
1265 CPU_BASED_CR3_STORE_EXITING |
1266 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001267 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1268 vmx_capability.ept, vmx_capability.vpid);
1269 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001270
1271 min = 0;
1272#ifdef CONFIG_X86_64
1273 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1274#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001275 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001276 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1277 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001278 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001279
Sheng Yang468d4722008-10-09 16:01:55 +08001280 min = 0;
1281 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001282 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1283 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001284 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001285
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001286 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001287
1288 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1289 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001290 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001291
1292#ifdef CONFIG_X86_64
1293 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1294 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001295 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001296#endif
1297
1298 /* Require Write-Back (WB) memory type for VMCS accesses. */
1299 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001300 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001301
Yang, Sheng002c7f72007-07-31 14:23:01 +03001302 vmcs_conf->size = vmx_msr_high & 0x1fff;
1303 vmcs_conf->order = get_order(vmcs_config.size);
1304 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001305
Yang, Sheng002c7f72007-07-31 14:23:01 +03001306 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1307 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001308 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001309 vmcs_conf->vmexit_ctrl = _vmexit_control;
1310 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001311
1312 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001313}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314
1315static struct vmcs *alloc_vmcs_cpu(int cpu)
1316{
1317 int node = cpu_to_node(cpu);
1318 struct page *pages;
1319 struct vmcs *vmcs;
1320
Mel Gorman6484eb32009-06-16 15:31:54 -07001321 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322 if (!pages)
1323 return NULL;
1324 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001325 memset(vmcs, 0, vmcs_config.size);
1326 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327 return vmcs;
1328}
1329
1330static struct vmcs *alloc_vmcs(void)
1331{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001332 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333}
1334
1335static void free_vmcs(struct vmcs *vmcs)
1336{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001337 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338}
1339
Sam Ravnborg39959582007-06-01 00:47:13 -07001340static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001341{
1342 int cpu;
1343
Zachary Amsden3230bb42009-09-29 11:38:37 -10001344 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001345 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10001346 per_cpu(vmxarea, cpu) = NULL;
1347 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348}
1349
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350static __init int alloc_kvm_area(void)
1351{
1352 int cpu;
1353
Zachary Amsden3230bb42009-09-29 11:38:37 -10001354 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355 struct vmcs *vmcs;
1356
1357 vmcs = alloc_vmcs_cpu(cpu);
1358 if (!vmcs) {
1359 free_kvm_area();
1360 return -ENOMEM;
1361 }
1362
1363 per_cpu(vmxarea, cpu) = vmcs;
1364 }
1365 return 0;
1366}
1367
1368static __init int hardware_setup(void)
1369{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001370 if (setup_vmcs_config(&vmcs_config) < 0)
1371 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001372
1373 if (boot_cpu_has(X86_FEATURE_NX))
1374 kvm_enable_efer_bits(EFER_NX);
1375
Sheng Yang93ba03c2009-04-01 15:52:32 +08001376 if (!cpu_has_vmx_vpid())
1377 enable_vpid = 0;
1378
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001379 if (!cpu_has_vmx_ept()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001380 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001381 enable_unrestricted_guest = 0;
1382 }
1383
1384 if (!cpu_has_vmx_unrestricted_guest())
1385 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001386
1387 if (!cpu_has_vmx_flexpriority())
1388 flexpriority_enabled = 0;
1389
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001390 if (!cpu_has_vmx_tpr_shadow())
1391 kvm_x86_ops->update_cr8_intercept = NULL;
1392
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001393 if (enable_ept && !cpu_has_vmx_ept_2m_page())
1394 kvm_disable_largepages();
1395
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001396 if (!cpu_has_vmx_ple())
1397 ple_gap = 0;
1398
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399 return alloc_kvm_area();
1400}
1401
1402static __exit void hardware_unsetup(void)
1403{
1404 free_kvm_area();
1405}
1406
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1408{
1409 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1410
Avi Kivity6af11b92007-03-19 13:18:10 +02001411 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412 vmcs_write16(sf->selector, save->selector);
1413 vmcs_writel(sf->base, save->base);
1414 vmcs_write32(sf->limit, save->limit);
1415 vmcs_write32(sf->ar_bytes, save->ar);
1416 } else {
1417 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1418 << AR_DPL_SHIFT;
1419 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1420 }
1421}
1422
1423static void enter_pmode(struct kvm_vcpu *vcpu)
1424{
1425 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001426 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001427
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001428 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001429 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001430
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001431 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
1432 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
1433 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001434
1435 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001436 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001437 flags |= (vmx->rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001438 vmcs_writel(GUEST_RFLAGS, flags);
1439
Rusty Russell66aee912007-07-17 23:34:16 +10001440 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1441 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001442
1443 update_exception_bitmap(vcpu);
1444
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001445 if (emulate_invalid_guest_state)
1446 return;
1447
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001448 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
1449 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
1450 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
1451 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001452
1453 vmcs_write16(GUEST_SS_SELECTOR, 0);
1454 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1455
1456 vmcs_write16(GUEST_CS_SELECTOR,
1457 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1458 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1459}
1460
Mike Dayd77c26f2007-10-08 09:02:08 -04001461static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001463 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001464 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1465 kvm->memslots[0].npages - 3;
1466 return base_gfn << PAGE_SHIFT;
1467 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001468 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001469}
1470
1471static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1472{
1473 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1474
1475 save->selector = vmcs_read16(sf->selector);
1476 save->base = vmcs_readl(sf->base);
1477 save->limit = vmcs_read32(sf->limit);
1478 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001479 vmcs_write16(sf->selector, save->base >> 4);
1480 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481 vmcs_write32(sf->limit, 0xffff);
1482 vmcs_write32(sf->ar_bytes, 0xf3);
1483}
1484
1485static void enter_rmode(struct kvm_vcpu *vcpu)
1486{
1487 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001488 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001489
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001490 if (enable_unrestricted_guest)
1491 return;
1492
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001493 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001494 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001496 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001497 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1498
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001499 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001500 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1501
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001502 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1504
1505 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001506 vmx->rmode.save_iopl
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001507 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001509 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001510
1511 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001512 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001513 update_exception_bitmap(vcpu);
1514
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001515 if (emulate_invalid_guest_state)
1516 goto continue_rmode;
1517
Avi Kivity6aa8b732006-12-10 02:21:36 -08001518 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1519 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1520 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1521
1522 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001523 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001524 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1525 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001526 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1527
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001528 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
1529 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
1530 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
1531 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001532
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001533continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001534 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001535 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001536}
1537
Amit Shah401d10d2009-02-20 22:53:37 +05301538static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1539{
1540 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001541 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1542
1543 if (!msr)
1544 return;
Amit Shah401d10d2009-02-20 22:53:37 +05301545
Avi Kivity44ea2b12009-09-06 15:55:37 +03001546 /*
1547 * Force kernel_gs_base reloading before EFER changes, as control
1548 * of this msr depends on is_long_mode().
1549 */
1550 vmx_load_host_state(to_vmx(vcpu));
Amit Shah401d10d2009-02-20 22:53:37 +05301551 vcpu->arch.shadow_efer = efer;
1552 if (!msr)
1553 return;
1554 if (efer & EFER_LMA) {
1555 vmcs_write32(VM_ENTRY_CONTROLS,
1556 vmcs_read32(VM_ENTRY_CONTROLS) |
1557 VM_ENTRY_IA32E_MODE);
1558 msr->data = efer;
1559 } else {
1560 vmcs_write32(VM_ENTRY_CONTROLS,
1561 vmcs_read32(VM_ENTRY_CONTROLS) &
1562 ~VM_ENTRY_IA32E_MODE);
1563
1564 msr->data = efer & ~EFER_LME;
1565 }
1566 setup_msrs(vmx);
1567}
1568
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001569#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001570
1571static void enter_lmode(struct kvm_vcpu *vcpu)
1572{
1573 u32 guest_tr_ar;
1574
1575 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1576 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1577 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001578 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001579 vmcs_write32(GUEST_TR_AR_BYTES,
1580 (guest_tr_ar & ~AR_TYPE_MASK)
1581 | AR_TYPE_BUSY_64_TSS);
1582 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001583 vcpu->arch.shadow_efer |= EFER_LMA;
Amit Shah401d10d2009-02-20 22:53:37 +05301584 vmx_set_efer(vcpu, vcpu->arch.shadow_efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001585}
1586
1587static void exit_lmode(struct kvm_vcpu *vcpu)
1588{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001589 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001590
1591 vmcs_write32(VM_ENTRY_CONTROLS,
1592 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001593 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594}
1595
1596#endif
1597
Sheng Yang2384d2b2008-01-17 15:14:33 +08001598static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1599{
1600 vpid_sync_vcpu_all(to_vmx(vcpu));
Avi Kivity089d0342009-03-23 18:26:32 +02001601 if (enable_ept)
Sheng Yang4e1096d2008-07-06 19:16:51 +08001602 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001603}
1604
Anthony Liguori25c4c272007-04-27 09:29:21 +03001605static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001606{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001607 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1608 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001609}
1610
Sheng Yang14394422008-04-28 12:24:45 +08001611static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1612{
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001613 if (!test_bit(VCPU_EXREG_PDPTR,
1614 (unsigned long *)&vcpu->arch.regs_dirty))
1615 return;
1616
Sheng Yang14394422008-04-28 12:24:45 +08001617 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Sheng Yang14394422008-04-28 12:24:45 +08001618 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1619 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1620 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1621 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1622 }
1623}
1624
Avi Kivity8f5d5492009-05-31 18:41:29 +03001625static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
1626{
1627 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1628 vcpu->arch.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
1629 vcpu->arch.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
1630 vcpu->arch.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
1631 vcpu->arch.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
1632 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001633
1634 __set_bit(VCPU_EXREG_PDPTR,
1635 (unsigned long *)&vcpu->arch.regs_avail);
1636 __set_bit(VCPU_EXREG_PDPTR,
1637 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03001638}
1639
Sheng Yang14394422008-04-28 12:24:45 +08001640static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1641
1642static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1643 unsigned long cr0,
1644 struct kvm_vcpu *vcpu)
1645{
1646 if (!(cr0 & X86_CR0_PG)) {
1647 /* From paging/starting to nonpaging */
1648 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001649 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001650 (CPU_BASED_CR3_LOAD_EXITING |
1651 CPU_BASED_CR3_STORE_EXITING));
1652 vcpu->arch.cr0 = cr0;
1653 vmx_set_cr4(vcpu, vcpu->arch.cr4);
Sheng Yang14394422008-04-28 12:24:45 +08001654 } else if (!is_paging(vcpu)) {
1655 /* From nonpaging to paging */
1656 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001657 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001658 ~(CPU_BASED_CR3_LOAD_EXITING |
1659 CPU_BASED_CR3_STORE_EXITING));
1660 vcpu->arch.cr0 = cr0;
1661 vmx_set_cr4(vcpu, vcpu->arch.cr4);
Sheng Yang14394422008-04-28 12:24:45 +08001662 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08001663
1664 if (!(cr0 & X86_CR0_WP))
1665 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08001666}
1667
1668static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1669 struct kvm_vcpu *vcpu)
1670{
1671 if (!is_paging(vcpu)) {
1672 *hw_cr4 &= ~X86_CR4_PAE;
1673 *hw_cr4 |= X86_CR4_PSE;
1674 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1675 *hw_cr4 &= ~X86_CR4_PAE;
1676}
1677
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1679{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001680 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001681 unsigned long hw_cr0;
1682
1683 if (enable_unrestricted_guest)
1684 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
1685 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
1686 else
1687 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08001688
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001689 vmx_fpu_deactivate(vcpu);
1690
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001691 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692 enter_pmode(vcpu);
1693
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001694 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001695 enter_rmode(vcpu);
1696
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001697#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001698 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001699 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001700 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001701 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001702 exit_lmode(vcpu);
1703 }
1704#endif
1705
Avi Kivity089d0342009-03-23 18:26:32 +02001706 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001707 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1708
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001710 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001711 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001712
Rusty Russell707d92fa2007-07-17 23:19:08 +10001713 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001714 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001715}
1716
Sheng Yang14394422008-04-28 12:24:45 +08001717static u64 construct_eptp(unsigned long root_hpa)
1718{
1719 u64 eptp;
1720
1721 /* TODO write the value reading from MSR */
1722 eptp = VMX_EPT_DEFAULT_MT |
1723 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1724 eptp |= (root_hpa & PAGE_MASK);
1725
1726 return eptp;
1727}
1728
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1730{
Sheng Yang14394422008-04-28 12:24:45 +08001731 unsigned long guest_cr3;
1732 u64 eptp;
1733
1734 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02001735 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001736 eptp = construct_eptp(cr3);
1737 vmcs_write64(EPT_POINTER, eptp);
Sheng Yang14394422008-04-28 12:24:45 +08001738 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
Sheng Yangb927a3c2009-07-21 10:42:48 +08001739 vcpu->kvm->arch.ept_identity_map_addr;
Sheng Yang14394422008-04-28 12:24:45 +08001740 }
1741
Sheng Yang2384d2b2008-01-17 15:14:33 +08001742 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001743 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001744 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001745 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001746}
1747
1748static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1749{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001750 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08001751 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1752
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001753 vcpu->arch.cr4 = cr4;
Avi Kivity089d0342009-03-23 18:26:32 +02001754 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001755 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1756
1757 vmcs_writel(CR4_READ_SHADOW, cr4);
1758 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001759}
1760
Avi Kivity6aa8b732006-12-10 02:21:36 -08001761static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1762{
1763 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1764
1765 return vmcs_readl(sf->base);
1766}
1767
1768static void vmx_get_segment(struct kvm_vcpu *vcpu,
1769 struct kvm_segment *var, int seg)
1770{
1771 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1772 u32 ar;
1773
1774 var->base = vmcs_readl(sf->base);
1775 var->limit = vmcs_read32(sf->limit);
1776 var->selector = vmcs_read16(sf->selector);
1777 ar = vmcs_read32(sf->ar_bytes);
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02001778 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001779 ar = 0;
1780 var->type = ar & 15;
1781 var->s = (ar >> 4) & 1;
1782 var->dpl = (ar >> 5) & 3;
1783 var->present = (ar >> 7) & 1;
1784 var->avl = (ar >> 12) & 1;
1785 var->l = (ar >> 13) & 1;
1786 var->db = (ar >> 14) & 1;
1787 var->g = (ar >> 15) & 1;
1788 var->unusable = (ar >> 16) & 1;
1789}
1790
Izik Eidus2e4d2652008-03-24 19:38:34 +02001791static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1792{
Izik Eidus2e4d2652008-03-24 19:38:34 +02001793 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1794 return 0;
1795
1796 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1797 return 3;
1798
Avi Kivityeab4b8a2009-08-04 15:02:54 +03001799 return vmcs_read16(GUEST_CS_SELECTOR) & 3;
Izik Eidus2e4d2652008-03-24 19:38:34 +02001800}
1801
Avi Kivity653e3102007-05-07 10:55:37 +03001802static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001803{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001804 u32 ar;
1805
Avi Kivity653e3102007-05-07 10:55:37 +03001806 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001807 ar = 1 << 16;
1808 else {
1809 ar = var->type & 15;
1810 ar |= (var->s & 1) << 4;
1811 ar |= (var->dpl & 3) << 5;
1812 ar |= (var->present & 1) << 7;
1813 ar |= (var->avl & 1) << 12;
1814 ar |= (var->l & 1) << 13;
1815 ar |= (var->db & 1) << 14;
1816 ar |= (var->g & 1) << 15;
1817 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001818 if (ar == 0) /* a 0 value means unusable */
1819 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001820
1821 return ar;
1822}
1823
1824static void vmx_set_segment(struct kvm_vcpu *vcpu,
1825 struct kvm_segment *var, int seg)
1826{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001827 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03001828 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1829 u32 ar;
1830
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001831 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
1832 vmx->rmode.tr.selector = var->selector;
1833 vmx->rmode.tr.base = var->base;
1834 vmx->rmode.tr.limit = var->limit;
1835 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001836 return;
1837 }
1838 vmcs_writel(sf->base, var->base);
1839 vmcs_write32(sf->limit, var->limit);
1840 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001841 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001842 /*
1843 * Hack real-mode segments into vm86 compatibility.
1844 */
1845 if (var->base == 0xffff0000 && var->selector == 0xf000)
1846 vmcs_writel(sf->base, 0xf0000);
1847 ar = 0xf3;
1848 } else
1849 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001850
1851 /*
1852 * Fix the "Accessed" bit in AR field of segment registers for older
1853 * qemu binaries.
1854 * IA32 arch specifies that at the time of processor reset the
1855 * "Accessed" bit in the AR field of segment registers is 1. And qemu
1856 * is setting it to 0 in the usedland code. This causes invalid guest
1857 * state vmexit when "unrestricted guest" mode is turned on.
1858 * Fix for this setup issue in cpu_reset is being pushed in the qemu
1859 * tree. Newer qemu binaries with that qemu fix would not need this
1860 * kvm hack.
1861 */
1862 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
1863 ar |= 0x1; /* Accessed */
1864
Avi Kivity6aa8b732006-12-10 02:21:36 -08001865 vmcs_write32(sf->ar_bytes, ar);
1866}
1867
Avi Kivity6aa8b732006-12-10 02:21:36 -08001868static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1869{
1870 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1871
1872 *db = (ar >> 14) & 1;
1873 *l = (ar >> 13) & 1;
1874}
1875
1876static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1877{
1878 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1879 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1880}
1881
1882static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1883{
1884 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1885 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1886}
1887
1888static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1889{
1890 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1891 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1892}
1893
1894static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1895{
1896 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1897 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1898}
1899
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001900static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1901{
1902 struct kvm_segment var;
1903 u32 ar;
1904
1905 vmx_get_segment(vcpu, &var, seg);
1906 ar = vmx_segment_access_rights(&var);
1907
1908 if (var.base != (var.selector << 4))
1909 return false;
1910 if (var.limit != 0xffff)
1911 return false;
1912 if (ar != 0xf3)
1913 return false;
1914
1915 return true;
1916}
1917
1918static bool code_segment_valid(struct kvm_vcpu *vcpu)
1919{
1920 struct kvm_segment cs;
1921 unsigned int cs_rpl;
1922
1923 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1924 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1925
Avi Kivity1872a3f2009-01-04 23:26:52 +02001926 if (cs.unusable)
1927 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001928 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1929 return false;
1930 if (!cs.s)
1931 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001932 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001933 if (cs.dpl > cs_rpl)
1934 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001935 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001936 if (cs.dpl != cs_rpl)
1937 return false;
1938 }
1939 if (!cs.present)
1940 return false;
1941
1942 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1943 return true;
1944}
1945
1946static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1947{
1948 struct kvm_segment ss;
1949 unsigned int ss_rpl;
1950
1951 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1952 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1953
Avi Kivity1872a3f2009-01-04 23:26:52 +02001954 if (ss.unusable)
1955 return true;
1956 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001957 return false;
1958 if (!ss.s)
1959 return false;
1960 if (ss.dpl != ss_rpl) /* DPL != RPL */
1961 return false;
1962 if (!ss.present)
1963 return false;
1964
1965 return true;
1966}
1967
1968static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1969{
1970 struct kvm_segment var;
1971 unsigned int rpl;
1972
1973 vmx_get_segment(vcpu, &var, seg);
1974 rpl = var.selector & SELECTOR_RPL_MASK;
1975
Avi Kivity1872a3f2009-01-04 23:26:52 +02001976 if (var.unusable)
1977 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001978 if (!var.s)
1979 return false;
1980 if (!var.present)
1981 return false;
1982 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
1983 if (var.dpl < rpl) /* DPL < RPL */
1984 return false;
1985 }
1986
1987 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1988 * rights flags
1989 */
1990 return true;
1991}
1992
1993static bool tr_valid(struct kvm_vcpu *vcpu)
1994{
1995 struct kvm_segment tr;
1996
1997 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
1998
Avi Kivity1872a3f2009-01-04 23:26:52 +02001999 if (tr.unusable)
2000 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002001 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2002 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002003 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002004 return false;
2005 if (!tr.present)
2006 return false;
2007
2008 return true;
2009}
2010
2011static bool ldtr_valid(struct kvm_vcpu *vcpu)
2012{
2013 struct kvm_segment ldtr;
2014
2015 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2016
Avi Kivity1872a3f2009-01-04 23:26:52 +02002017 if (ldtr.unusable)
2018 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002019 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2020 return false;
2021 if (ldtr.type != 2)
2022 return false;
2023 if (!ldtr.present)
2024 return false;
2025
2026 return true;
2027}
2028
2029static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2030{
2031 struct kvm_segment cs, ss;
2032
2033 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2034 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2035
2036 return ((cs.selector & SELECTOR_RPL_MASK) ==
2037 (ss.selector & SELECTOR_RPL_MASK));
2038}
2039
2040/*
2041 * Check if guest state is valid. Returns true if valid, false if
2042 * not.
2043 * We assume that registers are always usable
2044 */
2045static bool guest_state_valid(struct kvm_vcpu *vcpu)
2046{
2047 /* real mode guest state checks */
2048 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
2049 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2050 return false;
2051 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2052 return false;
2053 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2054 return false;
2055 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2056 return false;
2057 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2058 return false;
2059 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2060 return false;
2061 } else {
2062 /* protected mode guest state checks */
2063 if (!cs_ss_rpl_check(vcpu))
2064 return false;
2065 if (!code_segment_valid(vcpu))
2066 return false;
2067 if (!stack_segment_valid(vcpu))
2068 return false;
2069 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2070 return false;
2071 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2072 return false;
2073 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2074 return false;
2075 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2076 return false;
2077 if (!tr_valid(vcpu))
2078 return false;
2079 if (!ldtr_valid(vcpu))
2080 return false;
2081 }
2082 /* TODO:
2083 * - Add checks on RIP
2084 * - Add checks on RFLAGS
2085 */
2086
2087 return true;
2088}
2089
Mike Dayd77c26f2007-10-08 09:02:08 -04002090static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002091{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002092 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002093 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002094 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02002095 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096
Izik Eidus195aefd2007-10-01 22:14:18 +02002097 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2098 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002099 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002100 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002101 r = kvm_write_guest_page(kvm, fn++, &data,
2102 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002103 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002104 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002105 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2106 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002107 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002108 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2109 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002110 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002111 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002112 r = kvm_write_guest_page(kvm, fn, &data,
2113 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2114 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002115 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002116 goto out;
2117
2118 ret = 1;
2119out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002120 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002121}
2122
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002123static int init_rmode_identity_map(struct kvm *kvm)
2124{
2125 int i, r, ret;
2126 pfn_t identity_map_pfn;
2127 u32 tmp;
2128
Avi Kivity089d0342009-03-23 18:26:32 +02002129 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002130 return 1;
2131 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2132 printk(KERN_ERR "EPT: identity-mapping pagetable "
2133 "haven't been allocated!\n");
2134 return 0;
2135 }
2136 if (likely(kvm->arch.ept_identity_pagetable_done))
2137 return 1;
2138 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002139 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002140 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2141 if (r < 0)
2142 goto out;
2143 /* Set up identity-mapping pagetable for EPT in real mode */
2144 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2145 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2146 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2147 r = kvm_write_guest_page(kvm, identity_map_pfn,
2148 &tmp, i * sizeof(tmp), sizeof(tmp));
2149 if (r < 0)
2150 goto out;
2151 }
2152 kvm->arch.ept_identity_pagetable_done = true;
2153 ret = 1;
2154out:
2155 return ret;
2156}
2157
Avi Kivity6aa8b732006-12-10 02:21:36 -08002158static void seg_setup(int seg)
2159{
2160 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002161 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002162
2163 vmcs_write16(sf->selector, 0);
2164 vmcs_writel(sf->base, 0);
2165 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002166 if (enable_unrestricted_guest) {
2167 ar = 0x93;
2168 if (seg == VCPU_SREG_CS)
2169 ar |= 0x08; /* code segment */
2170 } else
2171 ar = 0xf3;
2172
2173 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002174}
2175
Sheng Yangf78e0e22007-10-29 09:40:42 +08002176static int alloc_apic_access_page(struct kvm *kvm)
2177{
2178 struct kvm_userspace_memory_region kvm_userspace_mem;
2179 int r = 0;
2180
Izik Eidus72dc67a2008-02-10 18:04:15 +02002181 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002182 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002183 goto out;
2184 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2185 kvm_userspace_mem.flags = 0;
2186 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2187 kvm_userspace_mem.memory_size = PAGE_SIZE;
2188 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2189 if (r)
2190 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002191
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002192 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002193out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002194 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002195 return r;
2196}
2197
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002198static int alloc_identity_pagetable(struct kvm *kvm)
2199{
2200 struct kvm_userspace_memory_region kvm_userspace_mem;
2201 int r = 0;
2202
2203 down_write(&kvm->slots_lock);
2204 if (kvm->arch.ept_identity_pagetable)
2205 goto out;
2206 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2207 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002208 kvm_userspace_mem.guest_phys_addr =
2209 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002210 kvm_userspace_mem.memory_size = PAGE_SIZE;
2211 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2212 if (r)
2213 goto out;
2214
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002215 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
Sheng Yangb927a3c2009-07-21 10:42:48 +08002216 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002217out:
2218 up_write(&kvm->slots_lock);
2219 return r;
2220}
2221
Sheng Yang2384d2b2008-01-17 15:14:33 +08002222static void allocate_vpid(struct vcpu_vmx *vmx)
2223{
2224 int vpid;
2225
2226 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002227 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002228 return;
2229 spin_lock(&vmx_vpid_lock);
2230 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2231 if (vpid < VMX_NR_VPIDS) {
2232 vmx->vpid = vpid;
2233 __set_bit(vpid, vmx_vpid_bitmap);
2234 }
2235 spin_unlock(&vmx_vpid_lock);
2236}
2237
Avi Kivity58972972009-02-24 22:26:47 +02002238static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002239{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002240 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002241
2242 if (!cpu_has_vmx_msr_bitmap())
2243 return;
2244
2245 /*
2246 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2247 * have the write-low and read-high bitmap offsets the wrong way round.
2248 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2249 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002250 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002251 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2252 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002253 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2254 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002255 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2256 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002257 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002258}
2259
Avi Kivity58972972009-02-24 22:26:47 +02002260static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2261{
2262 if (!longmode_only)
2263 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2264 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2265}
2266
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267/*
2268 * Sets up the vmcs for emulated real mode.
2269 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002270static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271{
Sheng Yang468d4722008-10-09 16:01:55 +08002272 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273 u32 junk;
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01002274 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002275 unsigned long a;
2276 struct descriptor_table dt;
2277 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002278 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002279 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280
Avi Kivity6aa8b732006-12-10 02:21:36 -08002281 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002282 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2283 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002284
Sheng Yang25c5f222008-03-28 13:18:56 +08002285 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002286 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002287
Avi Kivity6aa8b732006-12-10 02:21:36 -08002288 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2289
Avi Kivity6aa8b732006-12-10 02:21:36 -08002290 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002291 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2292 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002293
2294 exec_control = vmcs_config.cpu_based_exec_ctrl;
2295 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2296 exec_control &= ~CPU_BASED_TPR_SHADOW;
2297#ifdef CONFIG_X86_64
2298 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2299 CPU_BASED_CR8_LOAD_EXITING;
2300#endif
2301 }
Avi Kivity089d0342009-03-23 18:26:32 +02002302 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002303 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002304 CPU_BASED_CR3_LOAD_EXITING |
2305 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002306 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002307
Sheng Yang83ff3b92007-11-21 14:33:25 +08002308 if (cpu_has_secondary_exec_ctrls()) {
2309 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2310 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2311 exec_control &=
2312 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002313 if (vmx->vpid == 0)
2314 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Avi Kivity089d0342009-03-23 18:26:32 +02002315 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002316 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002317 if (!enable_unrestricted_guest)
2318 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002319 if (!ple_gap)
2320 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002321 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2322 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002323
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002324 if (ple_gap) {
2325 vmcs_write32(PLE_GAP, ple_gap);
2326 vmcs_write32(PLE_WINDOW, ple_window);
2327 }
2328
Avi Kivityc7addb92007-09-16 18:58:32 +02002329 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2330 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2332
2333 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2334 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2335 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2336
2337 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2338 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2339 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002340 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2341 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002342 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002343#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344 rdmsrl(MSR_FS_BASE, a);
2345 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2346 rdmsrl(MSR_GS_BASE, a);
2347 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2348#else
2349 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2350 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2351#endif
2352
2353 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2354
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002355 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002356 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2357
Mike Dayd77c26f2007-10-08 09:02:08 -04002358 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002359 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002360 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2361 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2362 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363
2364 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2365 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2366 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2367 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2368 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2369 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2370
Sheng Yang468d4722008-10-09 16:01:55 +08002371 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2372 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2373 host_pat = msr_low | ((u64) msr_high << 32);
2374 vmcs_write64(HOST_IA32_PAT, host_pat);
2375 }
2376 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2377 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2378 host_pat = msr_low | ((u64) msr_high << 32);
2379 /* Write the default value follow host pat */
2380 vmcs_write64(GUEST_IA32_PAT, host_pat);
2381 /* Keep arch.pat sync with GUEST_IA32_PAT */
2382 vmx->vcpu.arch.pat = host_pat;
2383 }
2384
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385 for (i = 0; i < NR_VMX_MSR; ++i) {
2386 u32 index = vmx_msr_index[i];
2387 u32 data_low, data_high;
2388 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002389 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390
2391 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2392 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002393 if (wrmsr_safe(index, data_low, data_high) < 0)
2394 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002395 data = data_low | ((u64)data_high << 32);
Avi Kivity26bb0982009-09-07 11:14:12 +03002396 vmx->guest_msrs[j].index = i;
2397 vmx->guest_msrs[j].data = 0;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002398 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002400
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002401 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402
2403 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002404 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2405
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002406 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2407 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2408
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01002409 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2410 rdtscll(tsc_this);
2411 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2412 tsc_base = tsc_this;
2413
2414 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002415
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002416 return 0;
2417}
2418
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002419static int init_rmode(struct kvm *kvm)
2420{
2421 if (!init_rmode_tss(kvm))
2422 return 0;
2423 if (!init_rmode_identity_map(kvm))
2424 return 0;
2425 return 1;
2426}
2427
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002428static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2429{
2430 struct vcpu_vmx *vmx = to_vmx(vcpu);
2431 u64 msr;
2432 int ret;
2433
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002434 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002435 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002436 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002437 ret = -ENOMEM;
2438 goto out;
2439 }
2440
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002441 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002442
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002443 vmx->soft_vnmi_blocked = 0;
2444
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002445 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002446 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002447 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002448 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002449 msr |= MSR_IA32_APICBASE_BSP;
2450 kvm_set_apic_base(&vmx->vcpu, msr);
2451
2452 fx_init(&vmx->vcpu);
2453
Avi Kivity5706be02008-08-20 15:07:31 +03002454 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002455 /*
2456 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2457 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2458 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002459 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002460 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2461 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2462 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002463 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2464 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002465 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002466
2467 seg_setup(VCPU_SREG_DS);
2468 seg_setup(VCPU_SREG_ES);
2469 seg_setup(VCPU_SREG_FS);
2470 seg_setup(VCPU_SREG_GS);
2471 seg_setup(VCPU_SREG_SS);
2472
2473 vmcs_write16(GUEST_TR_SELECTOR, 0);
2474 vmcs_writel(GUEST_TR_BASE, 0);
2475 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2476 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2477
2478 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2479 vmcs_writel(GUEST_LDTR_BASE, 0);
2480 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2481 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2482
2483 vmcs_write32(GUEST_SYSENTER_CS, 0);
2484 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2485 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2486
2487 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002488 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002489 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002490 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002491 kvm_rip_write(vcpu, 0);
2492 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002493
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002494 vmcs_writel(GUEST_DR7, 0x400);
2495
2496 vmcs_writel(GUEST_GDTR_BASE, 0);
2497 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2498
2499 vmcs_writel(GUEST_IDTR_BASE, 0);
2500 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2501
2502 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2503 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2504 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2505
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002506 /* Special registers */
2507 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2508
2509 setup_msrs(vmx);
2510
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2512
Sheng Yangf78e0e22007-10-29 09:40:42 +08002513 if (cpu_has_vmx_tpr_shadow()) {
2514 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2515 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2516 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002517 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002518 vmcs_write32(TPR_THRESHOLD, 0);
2519 }
2520
2521 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2522 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002523 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002524
Sheng Yang2384d2b2008-01-17 15:14:33 +08002525 if (vmx->vpid != 0)
2526 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2527
Eduardo Habkostfa400522009-10-24 02:49:58 -02002528 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002529 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002530 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002531 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002532 vmx_fpu_activate(&vmx->vcpu);
2533 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534
Sheng Yang2384d2b2008-01-17 15:14:33 +08002535 vpid_sync_vcpu_all(vmx);
2536
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002537 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002538
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002539 /* HACK: Don't enable emulation on guest boot/reset */
2540 vmx->emulation_required = 0;
2541
Avi Kivity6aa8b732006-12-10 02:21:36 -08002542out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002543 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544 return ret;
2545}
2546
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002547static void enable_irq_window(struct kvm_vcpu *vcpu)
2548{
2549 u32 cpu_based_vm_exec_control;
2550
2551 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2552 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2553 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2554}
2555
2556static void enable_nmi_window(struct kvm_vcpu *vcpu)
2557{
2558 u32 cpu_based_vm_exec_control;
2559
2560 if (!cpu_has_virtual_nmis()) {
2561 enable_irq_window(vcpu);
2562 return;
2563 }
2564
2565 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2566 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2567 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2568}
2569
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002570static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03002571{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002572 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002573 uint32_t intr;
2574 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02002575
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002576 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002577
Avi Kivityfa89a812008-09-01 15:57:51 +03002578 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002579 if (vmx->rmode.vm86_active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002580 vmx->rmode.irq.pending = true;
2581 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002582 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +03002583 if (vcpu->arch.interrupt.soft)
2584 vmx->rmode.irq.rip +=
2585 vmx->vcpu.arch.event_exit_inst_len;
Avi Kivity9c5623e2007-11-08 18:19:20 +02002586 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2587 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2588 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002589 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002590 return;
2591 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002592 intr = irq | INTR_INFO_VALID_MASK;
2593 if (vcpu->arch.interrupt.soft) {
2594 intr |= INTR_TYPE_SOFT_INTR;
2595 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2596 vmx->vcpu.arch.event_exit_inst_len);
2597 } else
2598 intr |= INTR_TYPE_EXT_INTR;
2599 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03002600}
2601
Sheng Yangf08864b2008-05-15 18:23:25 +08002602static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2603{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002604 struct vcpu_vmx *vmx = to_vmx(vcpu);
2605
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002606 if (!cpu_has_virtual_nmis()) {
2607 /*
2608 * Tracking the NMI-blocked state in software is built upon
2609 * finding the next open IRQ window. This, in turn, depends on
2610 * well-behaving guests: They have to keep IRQs disabled at
2611 * least as long as the NMI handler runs. Otherwise we may
2612 * cause NMI nesting, maybe breaking the guest. But as this is
2613 * highly unlikely, we can live with the residual risk.
2614 */
2615 vmx->soft_vnmi_blocked = 1;
2616 vmx->vnmi_blocked_time = 0;
2617 }
2618
Jan Kiszka487b3912008-09-26 09:30:56 +02002619 ++vcpu->stat.nmi_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002620 if (vmx->rmode.vm86_active) {
Jan Kiszka66a5a342008-09-26 09:30:51 +02002621 vmx->rmode.irq.pending = true;
2622 vmx->rmode.irq.vector = NMI_VECTOR;
2623 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2624 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2625 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2626 INTR_INFO_VALID_MASK);
2627 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2628 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2629 return;
2630 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002631 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2632 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002633}
2634
Gleb Natapovc4282df2009-04-21 17:45:07 +03002635static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02002636{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002637 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03002638 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002639
Gleb Natapovc4282df2009-04-21 17:45:07 +03002640 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2641 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS |
2642 GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02002643}
2644
Gleb Natapov78646122009-03-23 12:12:11 +02002645static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
2646{
Gleb Natapovc4282df2009-04-21 17:45:07 +03002647 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2648 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2649 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02002650}
2651
Izik Eiduscbc94022007-10-25 00:29:55 +02002652static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2653{
2654 int ret;
2655 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002656 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002657 .guest_phys_addr = addr,
2658 .memory_size = PAGE_SIZE * 3,
2659 .flags = 0,
2660 };
2661
2662 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2663 if (ret)
2664 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002665 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002666 return 0;
2667}
2668
Avi Kivity6aa8b732006-12-10 02:21:36 -08002669static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2670 int vec, u32 err_code)
2671{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002672 /*
2673 * Instruction with address size override prefix opcode 0x67
2674 * Cause the #SS fault with 0 error code in VM86 mode.
2675 */
2676 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Avi Kivity851ba692009-08-24 11:10:17 +03002677 if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002679 /*
2680 * Forward all other exceptions that are valid in real mode.
2681 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2682 * the required debugging infrastructure rework.
2683 */
2684 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002685 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002686 if (vcpu->guest_debug &
2687 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2688 return 0;
2689 kvm_queue_exception(vcpu, vec);
2690 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002691 case BP_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002692 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2693 return 0;
2694 /* fall through */
2695 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002696 case OF_VECTOR:
2697 case BR_VECTOR:
2698 case UD_VECTOR:
2699 case DF_VECTOR:
2700 case SS_VECTOR:
2701 case GP_VECTOR:
2702 case MF_VECTOR:
2703 kvm_queue_exception(vcpu, vec);
2704 return 1;
2705 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002706 return 0;
2707}
2708
Andi Kleena0861c02009-06-08 17:37:09 +08002709/*
2710 * Trigger machine check on the host. We assume all the MSRs are already set up
2711 * by the CPU and that we still run on the same CPU as the MCE occurred on.
2712 * We pass a fake environment to the machine check handler because we want
2713 * the guest to be always treated like user space, no matter what context
2714 * it used internally.
2715 */
2716static void kvm_machine_check(void)
2717{
2718#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
2719 struct pt_regs regs = {
2720 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
2721 .flags = X86_EFLAGS_IF,
2722 };
2723
2724 do_machine_check(&regs, 0);
2725#endif
2726}
2727
Avi Kivity851ba692009-08-24 11:10:17 +03002728static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08002729{
2730 /* already handled by vcpu_run */
2731 return 1;
2732}
2733
Avi Kivity851ba692009-08-24 11:10:17 +03002734static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735{
Avi Kivity1155f762007-11-22 11:30:47 +02002736 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002737 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002738 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002739 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002740 u32 vect_info;
2741 enum emulation_result er;
2742
Avi Kivity1155f762007-11-22 11:30:47 +02002743 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002744 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2745
Andi Kleena0861c02009-06-08 17:37:09 +08002746 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03002747 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08002748
Avi Kivity6aa8b732006-12-10 02:21:36 -08002749 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002750 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002752 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753
Jan Kiszkae4a41882008-09-26 09:30:46 +02002754 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002755 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002756
2757 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002758 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002759 return 1;
2760 }
2761
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002762 if (is_invalid_opcode(intr_info)) {
Avi Kivity851ba692009-08-24 11:10:17 +03002763 er = emulate_instruction(vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002764 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002765 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002766 return 1;
2767 }
2768
Avi Kivity6aa8b732006-12-10 02:21:36 -08002769 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002770 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002771 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002772 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2773 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002774 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02002775 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002776 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002778 trace_kvm_page_fault(cr2, error_code);
2779
Gleb Natapov3298b752009-05-11 13:35:46 +03002780 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03002781 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002782 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783 }
2784
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002785 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002787 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002788 if (vcpu->arch.halt_request) {
2789 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002790 return kvm_emulate_halt(vcpu);
2791 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002792 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002793 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002794
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002795 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002796 switch (ex_no) {
2797 case DB_VECTOR:
2798 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2799 if (!(vcpu->guest_debug &
2800 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2801 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2802 kvm_queue_exception(vcpu, DB_VECTOR);
2803 return 1;
2804 }
2805 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2806 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2807 /* fall through */
2808 case BP_VECTOR:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002809 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002810 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2811 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002812 break;
2813 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002814 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2815 kvm_run->ex.exception = ex_no;
2816 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002817 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819 return 0;
2820}
2821
Avi Kivity851ba692009-08-24 11:10:17 +03002822static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002824 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825 return 1;
2826}
2827
Avi Kivity851ba692009-08-24 11:10:17 +03002828static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08002829{
Avi Kivity851ba692009-08-24 11:10:17 +03002830 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08002831 return 0;
2832}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833
Avi Kivity851ba692009-08-24 11:10:17 +03002834static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835{
He, Qingbfdaab02007-09-12 14:18:28 +08002836 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01002837 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002838 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839
Avi Kivity1165f5f2007-04-19 17:27:43 +03002840 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002841 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002842 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002843
2844 if (string) {
Avi Kivity851ba692009-08-24 11:10:17 +03002845 if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002846 return 0;
2847 return 1;
2848 }
2849
2850 size = (exit_qualification & 7) + 1;
2851 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002852 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002853
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002854 skip_emulated_instruction(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002855 return kvm_emulate_pio(vcpu, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002856}
2857
Ingo Molnar102d8322007-02-19 14:37:47 +02002858static void
2859vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2860{
2861 /*
2862 * Patch in the VMCALL instruction:
2863 */
2864 hypercall[0] = 0x0f;
2865 hypercall[1] = 0x01;
2866 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002867}
2868
Avi Kivity851ba692009-08-24 11:10:17 +03002869static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002870{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002871 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002872 int cr;
2873 int reg;
2874
He, Qingbfdaab02007-09-12 14:18:28 +08002875 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002876 cr = exit_qualification & 15;
2877 reg = (exit_qualification >> 8) & 15;
2878 switch ((exit_qualification >> 4) & 3) {
2879 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002880 val = kvm_register_read(vcpu, reg);
2881 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882 switch (cr) {
2883 case 0:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002884 kvm_set_cr0(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885 skip_emulated_instruction(vcpu);
2886 return 1;
2887 case 3:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002888 kvm_set_cr3(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002889 skip_emulated_instruction(vcpu);
2890 return 1;
2891 case 4:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002892 kvm_set_cr4(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002893 skip_emulated_instruction(vcpu);
2894 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002895 case 8: {
2896 u8 cr8_prev = kvm_get_cr8(vcpu);
2897 u8 cr8 = kvm_register_read(vcpu, reg);
2898 kvm_set_cr8(vcpu, cr8);
2899 skip_emulated_instruction(vcpu);
2900 if (irqchip_in_kernel(vcpu->kvm))
2901 return 1;
2902 if (cr8_prev <= cr8)
2903 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03002904 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002905 return 0;
2906 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907 };
2908 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002909 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002910 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002911 vcpu->arch.cr0 &= ~X86_CR0_TS;
2912 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002913 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002914 skip_emulated_instruction(vcpu);
2915 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002916 case 1: /*mov from cr*/
2917 switch (cr) {
2918 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002919 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002920 trace_kvm_cr_read(cr, vcpu->arch.cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002921 skip_emulated_instruction(vcpu);
2922 return 1;
2923 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002924 val = kvm_get_cr8(vcpu);
2925 kvm_register_write(vcpu, reg, val);
2926 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002927 skip_emulated_instruction(vcpu);
2928 return 1;
2929 }
2930 break;
2931 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002932 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933
2934 skip_emulated_instruction(vcpu);
2935 return 1;
2936 default:
2937 break;
2938 }
Avi Kivity851ba692009-08-24 11:10:17 +03002939 vcpu->run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002940 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002941 (int)(exit_qualification >> 4) & 3, cr);
2942 return 0;
2943}
2944
Avi Kivity851ba692009-08-24 11:10:17 +03002945static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002946{
He, Qingbfdaab02007-09-12 14:18:28 +08002947 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002948 unsigned long val;
2949 int dr, reg;
2950
Avi Kivity0a79b002009-09-01 12:03:25 +03002951 if (!kvm_require_cpl(vcpu, 0))
2952 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002953 dr = vmcs_readl(GUEST_DR7);
2954 if (dr & DR7_GD) {
2955 /*
2956 * As the vm-exit takes precedence over the debug trap, we
2957 * need to emulate the latter, either for the host or the
2958 * guest debugging itself.
2959 */
2960 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03002961 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
2962 vcpu->run->debug.arch.dr7 = dr;
2963 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002964 vmcs_readl(GUEST_CS_BASE) +
2965 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03002966 vcpu->run->debug.arch.exception = DB_VECTOR;
2967 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002968 return 0;
2969 } else {
2970 vcpu->arch.dr7 &= ~DR7_GD;
2971 vcpu->arch.dr6 |= DR6_BD;
2972 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2973 kvm_queue_exception(vcpu, DB_VECTOR);
2974 return 1;
2975 }
2976 }
2977
He, Qingbfdaab02007-09-12 14:18:28 +08002978 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002979 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
2980 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
2981 if (exit_qualification & TYPE_MOV_FROM_DR) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002982 switch (dr) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002983 case 0 ... 3:
2984 val = vcpu->arch.db[dr];
2985 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002986 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002987 val = vcpu->arch.dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002988 break;
2989 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002990 val = vcpu->arch.dr7;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002991 break;
2992 default:
2993 val = 0;
2994 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002995 kvm_register_write(vcpu, reg, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002996 } else {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002997 val = vcpu->arch.regs[reg];
2998 switch (dr) {
2999 case 0 ... 3:
3000 vcpu->arch.db[dr] = val;
3001 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
3002 vcpu->arch.eff_db[dr] = val;
3003 break;
3004 case 4 ... 5:
3005 if (vcpu->arch.cr4 & X86_CR4_DE)
3006 kvm_queue_exception(vcpu, UD_VECTOR);
3007 break;
3008 case 6:
3009 if (val & 0xffffffff00000000ULL) {
3010 kvm_queue_exception(vcpu, GP_VECTOR);
3011 break;
3012 }
3013 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
3014 break;
3015 case 7:
3016 if (val & 0xffffffff00000000ULL) {
3017 kvm_queue_exception(vcpu, GP_VECTOR);
3018 break;
3019 }
3020 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
3021 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
3022 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3023 vcpu->arch.switch_db_regs =
3024 (val & DR7_BP_EN_MASK);
3025 }
3026 break;
3027 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003028 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003029 skip_emulated_instruction(vcpu);
3030 return 1;
3031}
3032
Avi Kivity851ba692009-08-24 11:10:17 +03003033static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003034{
Avi Kivity06465c52007-02-28 20:46:53 +02003035 kvm_emulate_cpuid(vcpu);
3036 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003037}
3038
Avi Kivity851ba692009-08-24 11:10:17 +03003039static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003040{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003041 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003042 u64 data;
3043
3044 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003045 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003046 return 1;
3047 }
3048
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003049 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003050
Avi Kivity6aa8b732006-12-10 02:21:36 -08003051 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003052 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3053 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003054 skip_emulated_instruction(vcpu);
3055 return 1;
3056}
3057
Avi Kivity851ba692009-08-24 11:10:17 +03003058static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003059{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003060 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3061 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3062 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003063
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003064 trace_kvm_msr_write(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003065
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003067 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003068 return 1;
3069 }
3070
3071 skip_emulated_instruction(vcpu);
3072 return 1;
3073}
3074
Avi Kivity851ba692009-08-24 11:10:17 +03003075static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003076{
3077 return 1;
3078}
3079
Avi Kivity851ba692009-08-24 11:10:17 +03003080static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003081{
Eddie Dong85f455f2007-07-06 12:20:49 +03003082 u32 cpu_based_vm_exec_control;
3083
3084 /* clear pending irq */
3085 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3086 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3087 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003088
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003089 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003090
Dor Laorc1150d82007-01-05 16:36:24 -08003091 /*
3092 * If the user space waits to inject interrupts, exit as soon as
3093 * possible
3094 */
Gleb Natapov80618232009-04-21 17:44:56 +03003095 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003096 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03003097 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003098 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003099 return 0;
3100 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003101 return 1;
3102}
3103
Avi Kivity851ba692009-08-24 11:10:17 +03003104static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003105{
3106 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003107 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003108}
3109
Avi Kivity851ba692009-08-24 11:10:17 +03003110static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02003111{
Dor Laor510043d2007-02-19 18:25:43 +02003112 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003113 kvm_emulate_hypercall(vcpu);
3114 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003115}
3116
Avi Kivity851ba692009-08-24 11:10:17 +03003117static int handle_vmx_insn(struct kvm_vcpu *vcpu)
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003118{
3119 kvm_queue_exception(vcpu, UD_VECTOR);
3120 return 1;
3121}
3122
Avi Kivity851ba692009-08-24 11:10:17 +03003123static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003124{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003125 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003126
3127 kvm_mmu_invlpg(vcpu, exit_qualification);
3128 skip_emulated_instruction(vcpu);
3129 return 1;
3130}
3131
Avi Kivity851ba692009-08-24 11:10:17 +03003132static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02003133{
3134 skip_emulated_instruction(vcpu);
3135 /* TODO: Add support for VT-d/pass-through device */
3136 return 1;
3137}
3138
Avi Kivity851ba692009-08-24 11:10:17 +03003139static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003140{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003141 unsigned long exit_qualification;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003142 enum emulation_result er;
3143 unsigned long offset;
3144
Sheng Yangf9c617f2009-03-25 10:08:52 +08003145 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003146 offset = exit_qualification & 0xffful;
3147
Avi Kivity851ba692009-08-24 11:10:17 +03003148 er = emulate_instruction(vcpu, 0, 0, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003149
3150 if (er != EMULATE_DONE) {
3151 printk(KERN_ERR
3152 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3153 offset);
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003154 return -ENOEXEC;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003155 }
3156 return 1;
3157}
3158
Avi Kivity851ba692009-08-24 11:10:17 +03003159static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02003160{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003161 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003162 unsigned long exit_qualification;
3163 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003164 int reason, type, idt_v;
3165
3166 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3167 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003168
3169 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3170
3171 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003172 if (reason == TASK_SWITCH_GATE && idt_v) {
3173 switch (type) {
3174 case INTR_TYPE_NMI_INTR:
3175 vcpu->arch.nmi_injected = false;
3176 if (cpu_has_virtual_nmis())
3177 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3178 GUEST_INTR_STATE_NMI);
3179 break;
3180 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003181 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003182 kvm_clear_interrupt_queue(vcpu);
3183 break;
3184 case INTR_TYPE_HARD_EXCEPTION:
3185 case INTR_TYPE_SOFT_EXCEPTION:
3186 kvm_clear_exception_queue(vcpu);
3187 break;
3188 default:
3189 break;
3190 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003191 }
Izik Eidus37817f22008-03-24 23:14:53 +02003192 tss_selector = exit_qualification;
3193
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003194 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3195 type != INTR_TYPE_EXT_INTR &&
3196 type != INTR_TYPE_NMI_INTR))
3197 skip_emulated_instruction(vcpu);
3198
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003199 if (!kvm_task_switch(vcpu, tss_selector, reason))
3200 return 0;
3201
3202 /* clear all local breakpoint enable flags */
3203 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3204
3205 /*
3206 * TODO: What about debug traps on tss switch?
3207 * Are we supposed to inject them and update dr6?
3208 */
3209
3210 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003211}
3212
Avi Kivity851ba692009-08-24 11:10:17 +03003213static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08003214{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003215 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003216 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003217 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003218
Sheng Yangf9c617f2009-03-25 10:08:52 +08003219 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003220
3221 if (exit_qualification & (1 << 6)) {
3222 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003223 return -EINVAL;
Sheng Yang14394422008-04-28 12:24:45 +08003224 }
3225
3226 gla_validity = (exit_qualification >> 7) & 0x3;
3227 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3228 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3229 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3230 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003231 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003232 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3233 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03003234 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3235 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03003236 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003237 }
3238
3239 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003240 trace_kvm_page_fault(gpa, exit_qualification);
Sheng Yang49cd7d22009-02-11 13:50:40 +08003241 return kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003242}
3243
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003244static u64 ept_rsvd_mask(u64 spte, int level)
3245{
3246 int i;
3247 u64 mask = 0;
3248
3249 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
3250 mask |= (1ULL << i);
3251
3252 if (level > 2)
3253 /* bits 7:3 reserved */
3254 mask |= 0xf8;
3255 else if (level == 2) {
3256 if (spte & (1ULL << 7))
3257 /* 2MB ref, bits 20:12 reserved */
3258 mask |= 0x1ff000;
3259 else
3260 /* bits 6:3 reserved */
3261 mask |= 0x78;
3262 }
3263
3264 return mask;
3265}
3266
3267static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
3268 int level)
3269{
3270 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
3271
3272 /* 010b (write-only) */
3273 WARN_ON((spte & 0x7) == 0x2);
3274
3275 /* 110b (write/execute) */
3276 WARN_ON((spte & 0x7) == 0x6);
3277
3278 /* 100b (execute-only) and value not supported by logical processor */
3279 if (!cpu_has_vmx_ept_execute_only())
3280 WARN_ON((spte & 0x7) == 0x4);
3281
3282 /* not 000b */
3283 if ((spte & 0x7)) {
3284 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
3285
3286 if (rsvd_bits != 0) {
3287 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
3288 __func__, rsvd_bits);
3289 WARN_ON(1);
3290 }
3291
3292 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
3293 u64 ept_mem_type = (spte & 0x38) >> 3;
3294
3295 if (ept_mem_type == 2 || ept_mem_type == 3 ||
3296 ept_mem_type == 7) {
3297 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
3298 __func__, ept_mem_type);
3299 WARN_ON(1);
3300 }
3301 }
3302 }
3303}
3304
Avi Kivity851ba692009-08-24 11:10:17 +03003305static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003306{
3307 u64 sptes[4];
3308 int nr_sptes, i;
3309 gpa_t gpa;
3310
3311 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3312
3313 printk(KERN_ERR "EPT: Misconfiguration.\n");
3314 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
3315
3316 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
3317
3318 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
3319 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
3320
Avi Kivity851ba692009-08-24 11:10:17 +03003321 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3322 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003323
3324 return 0;
3325}
3326
Avi Kivity851ba692009-08-24 11:10:17 +03003327static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08003328{
3329 u32 cpu_based_vm_exec_control;
3330
3331 /* clear pending NMI */
3332 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3333 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3334 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3335 ++vcpu->stat.nmi_window_exits;
3336
3337 return 1;
3338}
3339
Mohammed Gamal80ced182009-09-01 12:48:18 +02003340static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003341{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003342 struct vcpu_vmx *vmx = to_vmx(vcpu);
3343 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003344 int ret = 1;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003345
3346 while (!guest_state_valid(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003347 err = emulate_instruction(vcpu, 0, 0, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003348
Mohammed Gamal80ced182009-09-01 12:48:18 +02003349 if (err == EMULATE_DO_MMIO) {
3350 ret = 0;
3351 goto out;
3352 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003353
3354 if (err != EMULATE_DONE) {
3355 kvm_report_emulation_failure(vcpu, "emulation failure");
Mohammed Gamal80ced182009-09-01 12:48:18 +02003356 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3357 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3358 ret = 0;
3359 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003360 }
3361
3362 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02003363 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003364 if (need_resched())
3365 schedule();
3366 }
3367
Mohammed Gamal80ced182009-09-01 12:48:18 +02003368 vmx->emulation_required = 0;
3369out:
3370 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003371}
3372
Avi Kivity6aa8b732006-12-10 02:21:36 -08003373/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003374 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
3375 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
3376 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03003377static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003378{
3379 skip_emulated_instruction(vcpu);
3380 kvm_vcpu_on_spin(vcpu);
3381
3382 return 1;
3383}
3384
3385/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003386 * The exit handlers return 1 if the exit was handled fully and guest execution
3387 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3388 * to be done to userspace and return 0.
3389 */
Avi Kivity851ba692009-08-24 11:10:17 +03003390static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003391 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3392 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003393 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003394 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003395 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003396 [EXIT_REASON_CR_ACCESS] = handle_cr,
3397 [EXIT_REASON_DR_ACCESS] = handle_dr,
3398 [EXIT_REASON_CPUID] = handle_cpuid,
3399 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3400 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3401 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3402 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003403 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003404 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003405 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3406 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3407 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3408 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3409 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3410 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3411 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3412 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3413 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003414 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3415 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003416 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003417 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08003418 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003419 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
3420 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003421 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003422};
3423
3424static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003425 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426
3427/*
3428 * The guest has exited. See if we can fix it or if we need userspace
3429 * assistance.
3430 */
Avi Kivity851ba692009-08-24 11:10:17 +03003431static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003432{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003433 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003434 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003435 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003436
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003437 trace_kvm_exit(exit_reason, kvm_rip_read(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003438
Mohammed Gamal80ced182009-09-01 12:48:18 +02003439 /* If guest state is invalid, start emulating */
3440 if (vmx->emulation_required && emulate_invalid_guest_state)
3441 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003442
Sheng Yang14394422008-04-28 12:24:45 +08003443 /* Access CR3 don't cause VMExit in paging mode, so we need
3444 * to sync with guest real CR3. */
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003445 if (enable_ept && is_paging(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08003446 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003447
Avi Kivity29bd8a72007-09-10 17:27:03 +03003448 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003449 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3450 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03003451 = vmcs_read32(VM_INSTRUCTION_ERROR);
3452 return 0;
3453 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003454
Mike Dayd77c26f2007-10-08 09:02:08 -04003455 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003456 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003457 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3458 exit_reason != EXIT_REASON_TASK_SWITCH))
3459 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3460 "(0x%x) and exit reason is 0x%x\n",
3461 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003462
3463 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03003464 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003465 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003466 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003467 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003468 /*
3469 * This CPU don't support us in finding the end of an
3470 * NMI-blocked window if the guest runs with IRQs
3471 * disabled. So we pull the trigger after 1 s of
3472 * futile waiting, but inform the user about this.
3473 */
3474 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3475 "state on VCPU %d after 1 s timeout\n",
3476 __func__, vcpu->vcpu_id);
3477 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003478 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003479 }
3480
Avi Kivity6aa8b732006-12-10 02:21:36 -08003481 if (exit_reason < kvm_vmx_max_exit_handlers
3482 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03003483 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003484 else {
Avi Kivity851ba692009-08-24 11:10:17 +03003485 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3486 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003487 }
3488 return 0;
3489}
3490
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003491static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003492{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003493 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003494 vmcs_write32(TPR_THRESHOLD, 0);
3495 return;
3496 }
3497
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003498 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003499}
3500
Avi Kivitycf393f72008-07-01 16:20:21 +03003501static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3502{
3503 u32 exit_intr_info;
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003504 u32 idt_vectoring_info = vmx->idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003505 bool unblock_nmi;
3506 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003507 int type;
3508 bool idtv_info_valid;
Avi Kivitycf393f72008-07-01 16:20:21 +03003509
3510 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Gleb Natapov20f65982009-05-11 13:35:55 +03003511
Andi Kleena0861c02009-06-08 17:37:09 +08003512 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
3513
3514 /* Handle machine checks before interrupts are enabled */
3515 if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
3516 || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
3517 && is_machine_check(exit_intr_info)))
3518 kvm_machine_check();
3519
Gleb Natapov20f65982009-05-11 13:35:55 +03003520 /* We need to handle NMIs before interrupts are enabled */
3521 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003522 (exit_intr_info & INTR_INFO_VALID_MASK))
Gleb Natapov20f65982009-05-11 13:35:55 +03003523 asm("int $2");
Gleb Natapov20f65982009-05-11 13:35:55 +03003524
3525 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3526
Avi Kivitycf393f72008-07-01 16:20:21 +03003527 if (cpu_has_virtual_nmis()) {
3528 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3529 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3530 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003531 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03003532 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3533 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003534 * SDM 3: 23.2.2 (September 2008)
3535 * Bit 12 is undefined in any of the following cases:
3536 * If the VM exit sets the valid bit in the IDT-vectoring
3537 * information field.
3538 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03003539 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003540 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
3541 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03003542 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3543 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003544 } else if (unlikely(vmx->soft_vnmi_blocked))
3545 vmx->vnmi_blocked_time +=
3546 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003547
Gleb Natapov37b96e92009-03-30 16:03:13 +03003548 vmx->vcpu.arch.nmi_injected = false;
3549 kvm_clear_exception_queue(&vmx->vcpu);
3550 kvm_clear_interrupt_queue(&vmx->vcpu);
3551
3552 if (!idtv_info_valid)
3553 return;
3554
Avi Kivity668f6122008-07-02 09:28:55 +03003555 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3556 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003557
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003558 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003559 case INTR_TYPE_NMI_INTR:
3560 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03003561 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003562 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03003563 * Clear bit "block by NMI" before VM entry if a NMI
3564 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03003565 */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003566 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3567 GUEST_INTR_STATE_NMI);
3568 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003569 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003570 vmx->vcpu.arch.event_exit_inst_len =
3571 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3572 /* fall through */
3573 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03003574 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003575 u32 err = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3576 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03003577 } else
3578 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003579 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003580 case INTR_TYPE_SOFT_INTR:
3581 vmx->vcpu.arch.event_exit_inst_len =
3582 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3583 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003584 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003585 kvm_queue_interrupt(&vmx->vcpu, vector,
3586 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003587 break;
3588 default:
3589 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03003590 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003591}
3592
Avi Kivity9c8cba32007-11-22 11:42:59 +02003593/*
3594 * Failure to inject an interrupt should give us the information
3595 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3596 * when fetching the interrupt redirection bitmap in the real-mode
3597 * tss, this doesn't happen. So we do it ourselves.
3598 */
3599static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3600{
3601 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003602 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003603 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003604 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003605 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3606 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3607 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3608 return;
3609 }
3610 vmx->idt_vectoring_info =
3611 VECTORING_INFO_VALID_MASK
3612 | INTR_TYPE_EXT_INTR
3613 | vmx->rmode.irq.vector;
3614}
3615
Avi Kivityc8019492008-07-14 14:44:59 +03003616#ifdef CONFIG_X86_64
3617#define R "r"
3618#define Q "q"
3619#else
3620#define R "e"
3621#define Q "l"
3622#endif
3623
Avi Kivity851ba692009-08-24 11:10:17 +03003624static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003625{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003626 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitye6adf282007-04-30 16:07:54 +03003627
Avi Kivity8f5d5492009-05-31 18:41:29 +03003628 if (enable_ept && is_paging(vcpu)) {
3629 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3630 ept_load_pdptrs(vcpu);
3631 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003632 /* Record the guest's net vcpu time for enforced NMI injections. */
3633 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3634 vmx->entry_time = ktime_get();
3635
Mohammed Gamal80ced182009-09-01 12:48:18 +02003636 /* Don't enter VMX if guest state is invalid, let the exit handler
3637 start emulation until we arrive back to a valid state */
3638 if (vmx->emulation_required && emulate_invalid_guest_state)
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003639 return;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003640
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003641 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3642 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3643 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3644 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3645
Gleb Natapov787ff732009-05-18 11:44:06 +03003646 /* When single-stepping over STI and MOV SS, we must clear the
3647 * corresponding interruptibility bits in the guest state. Otherwise
3648 * vmentry fails as it then expects bit 14 (BS) in pending debug
3649 * exceptions being set, but that's not correct for the guest debugging
3650 * case. */
3651 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3652 vmx_set_interrupt_shadow(vcpu, 0);
3653
Avi Kivitye6adf282007-04-30 16:07:54 +03003654 /*
3655 * Loading guest fpu may have cleared host cr0.ts
3656 */
3657 vmcs_writel(HOST_CR0, read_cr0());
3658
Avi Kivitye8a48342009-09-01 16:06:25 +03003659 if (vcpu->arch.switch_db_regs)
3660 set_debugreg(vcpu->arch.dr6, 6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003661
Mike Dayd77c26f2007-10-08 09:02:08 -04003662 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003663 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003664 "push %%"R"dx; push %%"R"bp;"
3665 "push %%"R"cx \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03003666 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3667 "je 1f \n\t"
3668 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003669 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03003670 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03003671 /* Reload cr2 if changed */
3672 "mov %c[cr2](%0), %%"R"ax \n\t"
3673 "mov %%cr2, %%"R"dx \n\t"
3674 "cmp %%"R"ax, %%"R"dx \n\t"
3675 "je 2f \n\t"
3676 "mov %%"R"ax, %%cr2 \n\t"
3677 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003678 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003679 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003680 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003681 "mov %c[rax](%0), %%"R"ax \n\t"
3682 "mov %c[rbx](%0), %%"R"bx \n\t"
3683 "mov %c[rdx](%0), %%"R"dx \n\t"
3684 "mov %c[rsi](%0), %%"R"si \n\t"
3685 "mov %c[rdi](%0), %%"R"di \n\t"
3686 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003687#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003688 "mov %c[r8](%0), %%r8 \n\t"
3689 "mov %c[r9](%0), %%r9 \n\t"
3690 "mov %c[r10](%0), %%r10 \n\t"
3691 "mov %c[r11](%0), %%r11 \n\t"
3692 "mov %c[r12](%0), %%r12 \n\t"
3693 "mov %c[r13](%0), %%r13 \n\t"
3694 "mov %c[r14](%0), %%r14 \n\t"
3695 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003696#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003697 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3698
Avi Kivity6aa8b732006-12-10 02:21:36 -08003699 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003700 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003701 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003702 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003703 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003704 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003705 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003706 "xchg %0, (%%"R"sp) \n\t"
3707 "mov %%"R"ax, %c[rax](%0) \n\t"
3708 "mov %%"R"bx, %c[rbx](%0) \n\t"
3709 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3710 "mov %%"R"dx, %c[rdx](%0) \n\t"
3711 "mov %%"R"si, %c[rsi](%0) \n\t"
3712 "mov %%"R"di, %c[rdi](%0) \n\t"
3713 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003714#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003715 "mov %%r8, %c[r8](%0) \n\t"
3716 "mov %%r9, %c[r9](%0) \n\t"
3717 "mov %%r10, %c[r10](%0) \n\t"
3718 "mov %%r11, %c[r11](%0) \n\t"
3719 "mov %%r12, %c[r12](%0) \n\t"
3720 "mov %%r13, %c[r13](%0) \n\t"
3721 "mov %%r14, %c[r14](%0) \n\t"
3722 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003723#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003724 "mov %%cr2, %%"R"ax \n\t"
3725 "mov %%"R"ax, %c[cr2](%0) \n\t"
3726
3727 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003728 "setbe %c[fail](%0) \n\t"
3729 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3730 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3731 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03003732 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003733 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3734 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3735 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3736 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3737 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3738 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3739 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003740#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003741 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3742 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3743 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3744 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3745 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3746 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3747 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3748 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003749#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003750 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003751 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003752 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003753#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003754 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3755#endif
3756 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003757
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003758 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
3759 | (1 << VCPU_EXREG_PDPTR));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003760 vcpu->arch.regs_dirty = 0;
3761
Avi Kivitye8a48342009-09-01 16:06:25 +03003762 if (vcpu->arch.switch_db_regs)
3763 get_debugreg(vcpu->arch.dr6, 6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003764
Avi Kivity1155f762007-11-22 11:30:47 +02003765 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003766 if (vmx->rmode.irq.pending)
3767 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003768
Mike Dayd77c26f2007-10-08 09:02:08 -04003769 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003770 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003771
Avi Kivitycf393f72008-07-01 16:20:21 +03003772 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003773}
3774
Avi Kivityc8019492008-07-14 14:44:59 +03003775#undef R
3776#undef Q
3777
Avi Kivity6aa8b732006-12-10 02:21:36 -08003778static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3779{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003780 struct vcpu_vmx *vmx = to_vmx(vcpu);
3781
3782 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003783 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003784 free_vmcs(vmx->vmcs);
3785 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003786 }
3787}
3788
3789static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3790{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003791 struct vcpu_vmx *vmx = to_vmx(vcpu);
3792
Sheng Yang2384d2b2008-01-17 15:14:33 +08003793 spin_lock(&vmx_vpid_lock);
3794 if (vmx->vpid != 0)
3795 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3796 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003797 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003798 kfree(vmx->guest_msrs);
3799 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003800 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003801}
3802
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003803static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003804{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003805 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003806 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003807 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003808
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003809 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003810 return ERR_PTR(-ENOMEM);
3811
Sheng Yang2384d2b2008-01-17 15:14:33 +08003812 allocate_vpid(vmx);
3813
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003814 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3815 if (err)
3816 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003817
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003818 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003819 if (!vmx->guest_msrs) {
3820 err = -ENOMEM;
3821 goto uninit_vcpu;
3822 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003823
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003824 vmx->vmcs = alloc_vmcs();
3825 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003826 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003827
3828 vmcs_clear(vmx->vmcs);
3829
Avi Kivity15ad7142007-07-11 18:17:21 +03003830 cpu = get_cpu();
3831 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003832 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003833 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003834 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003835 if (err)
3836 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003837 if (vm_need_virtualize_apic_accesses(kvm))
3838 if (alloc_apic_access_page(kvm) != 0)
3839 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003840
Sheng Yangb927a3c2009-07-21 10:42:48 +08003841 if (enable_ept) {
3842 if (!kvm->arch.ept_identity_map_addr)
3843 kvm->arch.ept_identity_map_addr =
3844 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003845 if (alloc_identity_pagetable(kvm) != 0)
3846 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003847 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003848
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003849 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003850
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003851free_vmcs:
3852 free_vmcs(vmx->vmcs);
3853free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003854 kfree(vmx->guest_msrs);
3855uninit_vcpu:
3856 kvm_vcpu_uninit(&vmx->vcpu);
3857free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003858 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003859 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003860}
3861
Yang, Sheng002c7f72007-07-31 14:23:01 +03003862static void __init vmx_check_processor_compat(void *rtn)
3863{
3864 struct vmcs_config vmcs_conf;
3865
3866 *(int *)rtn = 0;
3867 if (setup_vmcs_config(&vmcs_conf) < 0)
3868 *(int *)rtn = -EIO;
3869 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3870 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3871 smp_processor_id());
3872 *(int *)rtn = -EIO;
3873 }
3874}
3875
Sheng Yang67253af2008-04-25 10:20:22 +08003876static int get_ept_level(void)
3877{
3878 return VMX_EPT_DEFAULT_GAW + 1;
3879}
3880
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003881static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003882{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003883 u64 ret;
3884
Sheng Yang522c68c2009-04-27 20:35:43 +08003885 /* For VT-d and EPT combination
3886 * 1. MMIO: always map as UC
3887 * 2. EPT with VT-d:
3888 * a. VT-d without snooping control feature: can't guarantee the
3889 * result, try to trust guest.
3890 * b. VT-d with snooping control feature: snooping control feature of
3891 * VT-d engine can guarantee the cache correctness. Just set it
3892 * to WB to keep consistent with host. So the same as item 3.
3893 * 3. EPT without VT-d: always map as WB and set IGMT=1 to keep
3894 * consistent with host MTRR
3895 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003896 if (is_mmio)
3897 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08003898 else if (vcpu->kvm->arch.iommu_domain &&
3899 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
3900 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
3901 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003902 else
Sheng Yang522c68c2009-04-27 20:35:43 +08003903 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
3904 | VMX_EPT_IGMT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003905
3906 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08003907}
3908
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003909static const struct trace_print_flags vmx_exit_reasons_str[] = {
3910 { EXIT_REASON_EXCEPTION_NMI, "exception" },
3911 { EXIT_REASON_EXTERNAL_INTERRUPT, "ext_irq" },
3912 { EXIT_REASON_TRIPLE_FAULT, "triple_fault" },
3913 { EXIT_REASON_NMI_WINDOW, "nmi_window" },
3914 { EXIT_REASON_IO_INSTRUCTION, "io_instruction" },
3915 { EXIT_REASON_CR_ACCESS, "cr_access" },
3916 { EXIT_REASON_DR_ACCESS, "dr_access" },
3917 { EXIT_REASON_CPUID, "cpuid" },
3918 { EXIT_REASON_MSR_READ, "rdmsr" },
3919 { EXIT_REASON_MSR_WRITE, "wrmsr" },
3920 { EXIT_REASON_PENDING_INTERRUPT, "interrupt_window" },
3921 { EXIT_REASON_HLT, "halt" },
3922 { EXIT_REASON_INVLPG, "invlpg" },
3923 { EXIT_REASON_VMCALL, "hypercall" },
3924 { EXIT_REASON_TPR_BELOW_THRESHOLD, "tpr_below_thres" },
3925 { EXIT_REASON_APIC_ACCESS, "apic_access" },
3926 { EXIT_REASON_WBINVD, "wbinvd" },
3927 { EXIT_REASON_TASK_SWITCH, "task_switch" },
3928 { EXIT_REASON_EPT_VIOLATION, "ept_violation" },
3929 { -1, NULL }
3930};
3931
Joerg Roedel344f4142009-07-27 16:30:48 +02003932static bool vmx_gb_page_enable(void)
3933{
3934 return false;
3935}
3936
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003937static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003938 .cpu_has_kvm_support = cpu_has_kvm_support,
3939 .disabled_by_bios = vmx_disabled_by_bios,
3940 .hardware_setup = hardware_setup,
3941 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003942 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003943 .hardware_enable = hardware_enable,
3944 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08003945 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003946
3947 .vcpu_create = vmx_create_vcpu,
3948 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003949 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003950
Avi Kivity04d2cc72007-09-10 18:10:54 +03003951 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003952 .vcpu_load = vmx_vcpu_load,
3953 .vcpu_put = vmx_vcpu_put,
3954
3955 .set_guest_debug = set_guest_debug,
3956 .get_msr = vmx_get_msr,
3957 .set_msr = vmx_set_msr,
3958 .get_segment_base = vmx_get_segment_base,
3959 .get_segment = vmx_get_segment,
3960 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003961 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003962 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003963 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003964 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003965 .set_cr3 = vmx_set_cr3,
3966 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003967 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003968 .get_idt = vmx_get_idt,
3969 .set_idt = vmx_set_idt,
3970 .get_gdt = vmx_get_gdt,
3971 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003972 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003973 .get_rflags = vmx_get_rflags,
3974 .set_rflags = vmx_set_rflags,
3975
3976 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003977
Avi Kivity6aa8b732006-12-10 02:21:36 -08003978 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02003979 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003980 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003981 .set_interrupt_shadow = vmx_set_interrupt_shadow,
3982 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003983 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003984 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003985 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003986 .queue_exception = vmx_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003987 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003988 .nmi_allowed = vmx_nmi_allowed,
3989 .enable_nmi_window = enable_nmi_window,
3990 .enable_irq_window = enable_irq_window,
3991 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003992
Izik Eiduscbc94022007-10-25 00:29:55 +02003993 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003994 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003995 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003996
3997 .exit_reasons_str = vmx_exit_reasons_str,
Joerg Roedel344f4142009-07-27 16:30:48 +02003998 .gb_page_enable = vmx_gb_page_enable,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003999};
4000
4001static int __init vmx_init(void)
4002{
Avi Kivity26bb0982009-09-07 11:14:12 +03004003 int r, i;
4004
4005 rdmsrl_safe(MSR_EFER, &host_efer);
4006
4007 for (i = 0; i < NR_VMX_MSR; ++i)
4008 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03004009
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004010 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004011 if (!vmx_io_bitmap_a)
4012 return -ENOMEM;
4013
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004014 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004015 if (!vmx_io_bitmap_b) {
4016 r = -ENOMEM;
4017 goto out;
4018 }
4019
Avi Kivity58972972009-02-24 22:26:47 +02004020 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
4021 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08004022 r = -ENOMEM;
4023 goto out1;
4024 }
4025
Avi Kivity58972972009-02-24 22:26:47 +02004026 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
4027 if (!vmx_msr_bitmap_longmode) {
4028 r = -ENOMEM;
4029 goto out2;
4030 }
4031
He, Qingfdef3ad2007-04-30 09:45:24 +03004032 /*
4033 * Allow direct access to the PC debug port (it is often used for I/O
4034 * delays, but the vmexits simply slow things down).
4035 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004036 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
4037 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004038
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004039 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004040
Avi Kivity58972972009-02-24 22:26:47 +02004041 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
4042 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08004043
Sheng Yang2384d2b2008-01-17 15:14:33 +08004044 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
4045
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004046 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004047 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02004048 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08004049
Avi Kivity58972972009-02-24 22:26:47 +02004050 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
4051 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
4052 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
4053 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
4054 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
4055 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03004056
Avi Kivity089d0342009-03-23 18:26:32 +02004057 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004058 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004059 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08004060 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08004061 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004062 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004063 kvm_enable_tdp();
4064 } else
4065 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08004066
Avi Kivityc7addb92007-09-16 18:58:32 +02004067 if (bypass_guest_pf)
4068 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
4069
He, Qingfdef3ad2007-04-30 09:45:24 +03004070 return 0;
4071
Avi Kivity58972972009-02-24 22:26:47 +02004072out3:
4073 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08004074out2:
Avi Kivity58972972009-02-24 22:26:47 +02004075 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03004076out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004077 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03004078out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004079 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004080 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004081}
4082
4083static void __exit vmx_exit(void)
4084{
Avi Kivity58972972009-02-24 22:26:47 +02004085 free_page((unsigned long)vmx_msr_bitmap_legacy);
4086 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004087 free_page((unsigned long)vmx_io_bitmap_b);
4088 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004089
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004090 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004091}
4092
4093module_init(vmx_init)
4094module_exit(vmx_exit)