blob: 364263a25ff89ed93207a29a97aa9a3b8c241717 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040020
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020023#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/mm.h>
25#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040026#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020027#include <linux/moduleparam.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030028#include <linux/ftrace_event.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030029#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030030#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080033#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020034#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020035#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080036#include <asm/mce.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080037
Marcelo Tosatti229456f2009-06-17 09:22:14 -030038#include "trace.h"
39
Avi Kivity4ecac3f2008-05-13 13:23:38 +030040#define __ex(x) __kvm_handle_fault_on_reboot(x)
41
Avi Kivity6aa8b732006-12-10 02:21:36 -080042MODULE_AUTHOR("Qumranet");
43MODULE_LICENSE("GPL");
44
Avi Kivity4462d212009-03-23 17:53:37 +020045static int __read_mostly bypass_guest_pf = 1;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020046module_param(bypass_guest_pf, bool, S_IRUGO);
Avi Kivityc7addb92007-09-16 18:58:32 +020047
Avi Kivity4462d212009-03-23 17:53:37 +020048static int __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020049module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080050
Avi Kivity4462d212009-03-23 17:53:37 +020051static int __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020052module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020053
Avi Kivity4462d212009-03-23 17:53:37 +020054static int __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020055module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080056
Nitin A Kamble3a624e22009-06-08 11:34:16 -070057static int __read_mostly enable_unrestricted_guest = 1;
58module_param_named(unrestricted_guest,
59 enable_unrestricted_guest, bool, S_IRUGO);
60
Avi Kivity4462d212009-03-23 17:53:37 +020061static int __read_mostly emulate_invalid_guest_state = 0;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020062module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030063
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +080064/*
65 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
66 * ple_gap: upper bound on the amount of time between two successive
67 * executions of PAUSE in a loop. Also indicate if ple enabled.
68 * According to test, this time is usually small than 41 cycles.
69 * ple_window: upper bound on the amount of time a guest is allowed to execute
70 * in a PAUSE loop. Tests indicate that most spinlocks are held for
71 * less than 2^12 cycles
72 * Time is measured based on a counter that runs at the same rate as the TSC,
73 * refer SDM volume 3b section 21.6.13 & 22.1.3.
74 */
75#define KVM_VMX_DEFAULT_PLE_GAP 41
76#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
77static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
78module_param(ple_gap, int, S_IRUGO);
79
80static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
81module_param(ple_window, int, S_IRUGO);
82
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040083struct vmcs {
84 u32 revision_id;
85 u32 abort;
86 char data[0];
87};
88
89struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100090 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +030091 struct list_head local_vcpus_link;
Avi Kivity313dbd42008-07-17 18:04:30 +030092 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040093 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030094 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020095 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040096 struct kvm_msr_entry *guest_msrs;
97 struct kvm_msr_entry *host_msrs;
98 int nmsrs;
99 int save_nmsrs;
100 int msr_offset_efer;
101#ifdef CONFIG_X86_64
102 int msr_offset_kernel_gs_base;
103#endif
104 struct vmcs *vmcs;
105 struct {
106 int loaded;
107 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200108 int gs_ldt_reload_needed;
109 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300110 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -0400111 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200112 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300113 int vm86_active;
114 u8 save_iopl;
115 struct kvm_save_segment {
116 u16 selector;
117 unsigned long base;
118 u32 limit;
119 u32 ar;
120 } tr, es, ds, fs, gs;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200121 struct {
122 bool pending;
123 u8 vector;
124 unsigned rip;
125 } irq;
126 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800127 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300128 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200129
130 /* Support for vnmi-less CPUs */
131 int soft_vnmi_blocked;
132 ktime_t entry_time;
133 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800134 u32 exit_reason;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400135};
136
137static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
138{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000139 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400140}
141
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800142static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800143static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300144
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145static DEFINE_PER_CPU(struct vmcs *, vmxarea);
146static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300147static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800148
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200149static unsigned long *vmx_io_bitmap_a;
150static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200151static unsigned long *vmx_msr_bitmap_legacy;
152static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300153
Sheng Yang2384d2b2008-01-17 15:14:33 +0800154static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
155static DEFINE_SPINLOCK(vmx_vpid_lock);
156
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300157static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800158 int size;
159 int order;
160 u32 revision_id;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300161 u32 pin_based_exec_ctrl;
162 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800163 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300164 u32 vmexit_ctrl;
165 u32 vmentry_ctrl;
166} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800167
Hannes Ederefff9e52008-11-28 17:02:06 +0100168static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800169 u32 ept;
170 u32 vpid;
171} vmx_capability;
172
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173#define VMX_SEGMENT_FIELD(seg) \
174 [VCPU_SREG_##seg] = { \
175 .selector = GUEST_##seg##_SELECTOR, \
176 .base = GUEST_##seg##_BASE, \
177 .limit = GUEST_##seg##_LIMIT, \
178 .ar_bytes = GUEST_##seg##_AR_BYTES, \
179 }
180
181static struct kvm_vmx_segment_field {
182 unsigned selector;
183 unsigned base;
184 unsigned limit;
185 unsigned ar_bytes;
186} kvm_vmx_segment_fields[] = {
187 VMX_SEGMENT_FIELD(CS),
188 VMX_SEGMENT_FIELD(DS),
189 VMX_SEGMENT_FIELD(ES),
190 VMX_SEGMENT_FIELD(FS),
191 VMX_SEGMENT_FIELD(GS),
192 VMX_SEGMENT_FIELD(SS),
193 VMX_SEGMENT_FIELD(TR),
194 VMX_SEGMENT_FIELD(LDTR),
195};
196
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300197static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
198
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300199/*
200 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
201 * away by decrementing the array size.
202 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800204#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
206#endif
207 MSR_EFER, MSR_K6_STAR,
208};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200209#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800210
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400211static void load_msrs(struct kvm_msr_entry *e, int n)
212{
213 int i;
214
215 for (i = 0; i < n; ++i)
216 wrmsrl(e[i].index, e[i].data);
217}
218
219static void save_msrs(struct kvm_msr_entry *e, int n)
220{
221 int i;
222
223 for (i = 0; i < n; ++i)
224 rdmsrl(e[i].index, e[i].data);
225}
226
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227static inline int is_page_fault(u32 intr_info)
228{
229 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
230 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100231 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232}
233
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300234static inline int is_no_device(u32 intr_info)
235{
236 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
237 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100238 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300239}
240
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500241static inline int is_invalid_opcode(u32 intr_info)
242{
243 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
244 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100245 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500246}
247
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248static inline int is_external_interrupt(u32 intr_info)
249{
250 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
251 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
252}
253
Andi Kleena0861c02009-06-08 17:37:09 +0800254static inline int is_machine_check(u32 intr_info)
255{
256 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
257 INTR_INFO_VALID_MASK)) ==
258 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
259}
260
Sheng Yang25c5f222008-03-28 13:18:56 +0800261static inline int cpu_has_vmx_msr_bitmap(void)
262{
Sheng Yang04547152009-04-01 15:52:31 +0800263 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800264}
265
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800266static inline int cpu_has_vmx_tpr_shadow(void)
267{
Sheng Yang04547152009-04-01 15:52:31 +0800268 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800269}
270
271static inline int vm_need_tpr_shadow(struct kvm *kvm)
272{
Sheng Yang04547152009-04-01 15:52:31 +0800273 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800274}
275
Sheng Yangf78e0e22007-10-29 09:40:42 +0800276static inline int cpu_has_secondary_exec_ctrls(void)
277{
Sheng Yang04547152009-04-01 15:52:31 +0800278 return vmcs_config.cpu_based_exec_ctrl &
279 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800280}
281
Avi Kivity774ead32007-12-26 13:57:04 +0200282static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800283{
Sheng Yang04547152009-04-01 15:52:31 +0800284 return vmcs_config.cpu_based_2nd_exec_ctrl &
285 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
286}
287
288static inline bool cpu_has_vmx_flexpriority(void)
289{
290 return cpu_has_vmx_tpr_shadow() &&
291 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800292}
293
Marcelo Tosattie7997942009-06-11 12:07:40 -0300294static inline bool cpu_has_vmx_ept_execute_only(void)
295{
296 return !!(vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT);
297}
298
299static inline bool cpu_has_vmx_eptp_uncacheable(void)
300{
301 return !!(vmx_capability.ept & VMX_EPTP_UC_BIT);
302}
303
304static inline bool cpu_has_vmx_eptp_writeback(void)
305{
306 return !!(vmx_capability.ept & VMX_EPTP_WB_BIT);
307}
308
309static inline bool cpu_has_vmx_ept_2m_page(void)
310{
311 return !!(vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT);
312}
313
Sheng Yangd56f5462008-04-25 10:13:16 +0800314static inline int cpu_has_vmx_invept_individual_addr(void)
315{
Sheng Yang04547152009-04-01 15:52:31 +0800316 return !!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800317}
318
319static inline int cpu_has_vmx_invept_context(void)
320{
Sheng Yang04547152009-04-01 15:52:31 +0800321 return !!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800322}
323
324static inline int cpu_has_vmx_invept_global(void)
325{
Sheng Yang04547152009-04-01 15:52:31 +0800326 return !!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800327}
328
329static inline int cpu_has_vmx_ept(void)
330{
Sheng Yang04547152009-04-01 15:52:31 +0800331 return vmcs_config.cpu_based_2nd_exec_ctrl &
332 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800333}
334
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700335static inline int cpu_has_vmx_unrestricted_guest(void)
336{
337 return vmcs_config.cpu_based_2nd_exec_ctrl &
338 SECONDARY_EXEC_UNRESTRICTED_GUEST;
339}
340
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800341static inline int cpu_has_vmx_ple(void)
342{
343 return vmcs_config.cpu_based_2nd_exec_ctrl &
344 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
345}
346
Sheng Yangf78e0e22007-10-29 09:40:42 +0800347static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
348{
Sheng Yang04547152009-04-01 15:52:31 +0800349 return flexpriority_enabled &&
350 (cpu_has_vmx_virtualize_apic_accesses()) &&
351 (irqchip_in_kernel(kvm));
Sheng Yangf78e0e22007-10-29 09:40:42 +0800352}
353
Sheng Yang2384d2b2008-01-17 15:14:33 +0800354static inline int cpu_has_vmx_vpid(void)
355{
Sheng Yang04547152009-04-01 15:52:31 +0800356 return vmcs_config.cpu_based_2nd_exec_ctrl &
357 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800358}
359
Sheng Yangf08864b2008-05-15 18:23:25 +0800360static inline int cpu_has_virtual_nmis(void)
361{
362 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
363}
364
Sheng Yang04547152009-04-01 15:52:31 +0800365static inline bool report_flexpriority(void)
366{
367 return flexpriority_enabled;
368}
369
Rusty Russell8b9cf982007-07-30 16:31:43 +1000370static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800371{
372 int i;
373
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400374 for (i = 0; i < vmx->nmsrs; ++i)
375 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300376 return i;
377 return -1;
378}
379
Sheng Yang2384d2b2008-01-17 15:14:33 +0800380static inline void __invvpid(int ext, u16 vpid, gva_t gva)
381{
382 struct {
383 u64 vpid : 16;
384 u64 rsvd : 48;
385 u64 gva;
386 } operand = { vpid, 0, gva };
387
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300388 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800389 /* CF==1 or ZF==1 --> rc = -1 */
390 "; ja 1f ; ud2 ; 1:"
391 : : "a"(&operand), "c"(ext) : "cc", "memory");
392}
393
Sheng Yang14394422008-04-28 12:24:45 +0800394static inline void __invept(int ext, u64 eptp, gpa_t gpa)
395{
396 struct {
397 u64 eptp, gpa;
398 } operand = {eptp, gpa};
399
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300400 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800401 /* CF==1 or ZF==1 --> rc = -1 */
402 "; ja 1f ; ud2 ; 1:\n"
403 : : "a" (&operand), "c" (ext) : "cc", "memory");
404}
405
Rusty Russell8b9cf982007-07-30 16:31:43 +1000406static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300407{
408 int i;
409
Rusty Russell8b9cf982007-07-30 16:31:43 +1000410 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300411 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400412 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000413 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800414}
415
Avi Kivity6aa8b732006-12-10 02:21:36 -0800416static void vmcs_clear(struct vmcs *vmcs)
417{
418 u64 phys_addr = __pa(vmcs);
419 u8 error;
420
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300421 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
423 : "cc", "memory");
424 if (error)
425 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
426 vmcs, phys_addr);
427}
428
429static void __vcpu_clear(void *arg)
430{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000431 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800432 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433
Rusty Russell8b9cf982007-07-30 16:31:43 +1000434 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400435 vmcs_clear(vmx->vmcs);
436 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800438 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300439 list_del(&vmx->local_vcpus_link);
440 vmx->vcpu.cpu = -1;
441 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442}
443
Rusty Russell8b9cf982007-07-30 16:31:43 +1000444static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800445{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200446 if (vmx->vcpu.cpu == -1)
447 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200448 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800449}
450
Sheng Yang2384d2b2008-01-17 15:14:33 +0800451static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
452{
453 if (vmx->vpid == 0)
454 return;
455
456 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
457}
458
Sheng Yang14394422008-04-28 12:24:45 +0800459static inline void ept_sync_global(void)
460{
461 if (cpu_has_vmx_invept_global())
462 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
463}
464
465static inline void ept_sync_context(u64 eptp)
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_context())
469 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
470 else
471 ept_sync_global();
472 }
473}
474
475static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
476{
Avi Kivity089d0342009-03-23 18:26:32 +0200477 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800478 if (cpu_has_vmx_invept_individual_addr())
479 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
480 eptp, gpa);
481 else
482 ept_sync_context(eptp);
483 }
484}
485
Avi Kivity6aa8b732006-12-10 02:21:36 -0800486static unsigned long vmcs_readl(unsigned long field)
487{
488 unsigned long value;
489
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300490 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800491 : "=a"(value) : "d"(field) : "cc");
492 return value;
493}
494
495static u16 vmcs_read16(unsigned long field)
496{
497 return vmcs_readl(field);
498}
499
500static u32 vmcs_read32(unsigned long field)
501{
502 return vmcs_readl(field);
503}
504
505static u64 vmcs_read64(unsigned long field)
506{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800507#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800508 return vmcs_readl(field);
509#else
510 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
511#endif
512}
513
Avi Kivitye52de1b2007-01-05 16:36:56 -0800514static noinline void vmwrite_error(unsigned long field, unsigned long value)
515{
516 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
517 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
518 dump_stack();
519}
520
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521static void vmcs_writel(unsigned long field, unsigned long value)
522{
523 u8 error;
524
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300525 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400526 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800527 if (unlikely(error))
528 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800529}
530
531static void vmcs_write16(unsigned long field, u16 value)
532{
533 vmcs_writel(field, value);
534}
535
536static void vmcs_write32(unsigned long field, u32 value)
537{
538 vmcs_writel(field, value);
539}
540
541static void vmcs_write64(unsigned long field, u64 value)
542{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800543 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300544#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800545 asm volatile ("");
546 vmcs_writel(field+1, value >> 32);
547#endif
548}
549
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300550static void vmcs_clear_bits(unsigned long field, u32 mask)
551{
552 vmcs_writel(field, vmcs_readl(field) & ~mask);
553}
554
555static void vmcs_set_bits(unsigned long field, u32 mask)
556{
557 vmcs_writel(field, vmcs_readl(field) | mask);
558}
559
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300560static void update_exception_bitmap(struct kvm_vcpu *vcpu)
561{
562 u32 eb;
563
Andi Kleena0861c02009-06-08 17:37:09 +0800564 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300565 if (!vcpu->fpu_active)
566 eb |= 1u << NM_VECTOR;
Avi Kivitye8a48342009-09-01 16:06:25 +0300567 /*
568 * Unconditionally intercept #DB so we can maintain dr6 without
569 * reading it every exit.
570 */
571 eb |= 1u << DB_VECTOR;
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100572 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100573 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
574 eb |= 1u << BP_VECTOR;
575 }
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300576 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300577 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200578 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800579 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300580 vmcs_write32(EXCEPTION_BITMAP, eb);
581}
582
Avi Kivity33ed6322007-05-02 16:54:03 +0300583static void reload_tss(void)
584{
Avi Kivity33ed6322007-05-02 16:54:03 +0300585 /*
586 * VT restores TR but not its size. Useless.
587 */
588 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200589 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300590
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300591 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300592 descs = (void *)gdt.base;
593 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
594 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300595}
596
Rusty Russell8b9cf982007-07-30 16:31:43 +1000597static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300598{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400599 int efer_offset = vmx->msr_offset_efer;
Roel Kluin3a34a882009-08-04 02:08:45 -0700600 u64 host_efer;
601 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300602 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300603
Avi Kivity51c6cf62007-08-29 03:48:05 +0300604 if (efer_offset < 0)
605 return;
Roel Kluin3a34a882009-08-04 02:08:45 -0700606 host_efer = vmx->host_msrs[efer_offset].data;
607 guest_efer = vmx->guest_msrs[efer_offset].data;
608
Avi Kivity51c6cf62007-08-29 03:48:05 +0300609 /*
610 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
611 * outside long mode
612 */
613 ignore_bits = EFER_NX | EFER_SCE;
614#ifdef CONFIG_X86_64
615 ignore_bits |= EFER_LMA | EFER_LME;
616 /* SCE is meaningful only in long mode on Intel */
617 if (guest_efer & EFER_LMA)
618 ignore_bits &= ~(u64)EFER_SCE;
619#endif
620 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
621 return;
622
623 vmx->host_state.guest_efer_loaded = 1;
624 guest_efer &= ~ignore_bits;
625 guest_efer |= host_efer & ignore_bits;
626 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000627 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300628}
629
Avi Kivity51c6cf62007-08-29 03:48:05 +0300630static void reload_host_efer(struct vcpu_vmx *vmx)
631{
632 if (vmx->host_state.guest_efer_loaded) {
633 vmx->host_state.guest_efer_loaded = 0;
634 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
635 }
636}
637
Avi Kivity04d2cc72007-09-10 18:10:54 +0300638static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300639{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300640 struct vcpu_vmx *vmx = to_vmx(vcpu);
641
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400642 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300643 return;
644
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400645 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300646 /*
647 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
648 * allow segment selectors with cpl > 0 or ti == 1.
649 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300650 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200651 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300652 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200653 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400654 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200655 vmx->host_state.fs_reload_needed = 0;
656 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300657 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200658 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300659 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300660 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400661 if (!(vmx->host_state.gs_sel & 7))
662 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300663 else {
664 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200665 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300666 }
667
668#ifdef CONFIG_X86_64
669 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
670 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
671#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400672 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
673 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300674#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300675
676#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400677 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400678 save_msrs(vmx->host_msrs +
679 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400680
Avi Kivity707c0872007-05-02 17:33:43 +0300681#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400682 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300683 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300684}
685
Avi Kivitya9b21b62008-06-24 11:48:49 +0300686static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300687{
Avi Kivity15ad7142007-07-11 18:17:21 +0300688 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300689
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400690 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300691 return;
692
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200693 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400694 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200695 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300696 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200697 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300698 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300699 /*
700 * If we have to reload gs, we must take care to
701 * preserve our gs base.
702 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300703 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300704 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300705#ifdef CONFIG_X86_64
706 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
707#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300708 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300709 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200710 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400711 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
712 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300713 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300714}
715
Avi Kivitya9b21b62008-06-24 11:48:49 +0300716static void vmx_load_host_state(struct vcpu_vmx *vmx)
717{
718 preempt_disable();
719 __vmx_load_host_state(vmx);
720 preempt_enable();
721}
722
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723/*
724 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
725 * vcpu mutex is already taken.
726 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300727static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400729 struct vcpu_vmx *vmx = to_vmx(vcpu);
730 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200731 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800732
Eddie Donga3d7f852007-09-03 16:15:12 +0300733 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000734 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300735 kvm_migrate_timers(vcpu);
Marcelo Tosattieb5109e2009-10-01 19:16:58 -0300736 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
Avi Kivity543e4242008-05-13 16:22:47 +0300737 local_irq_disable();
738 list_add(&vmx->local_vcpus_link,
739 &per_cpu(vcpus_on_cpu, cpu));
740 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300741 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800742
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400743 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744 u8 error;
745
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400746 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300747 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800748 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
749 : "cc");
750 if (error)
751 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400752 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753 }
754
755 if (vcpu->cpu != cpu) {
756 struct descriptor_table dt;
757 unsigned long sysenter_esp;
758
759 vcpu->cpu = cpu;
760 /*
761 * Linux uses per-cpu TSS and GDT, so set these when switching
762 * processors.
763 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300764 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
765 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
767
768 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
769 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300770
771 /*
772 * Make sure the time stamp counter is monotonous.
773 */
774 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200775 if (tsc_this < vcpu->arch.host_tsc) {
776 delta = vcpu->arch.host_tsc - tsc_this;
777 new_offset = vmcs_read64(TSC_OFFSET) + delta;
778 vmcs_write64(TSC_OFFSET, new_offset);
779 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800780 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781}
782
783static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
784{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300785 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786}
787
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300788static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
789{
790 if (vcpu->fpu_active)
791 return;
792 vcpu->fpu_active = 1;
Rusty Russell707d92f2007-07-17 23:19:08 +1000793 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800794 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92f2007-07-17 23:19:08 +1000795 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300796 update_exception_bitmap(vcpu);
797}
798
799static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
800{
801 if (!vcpu->fpu_active)
802 return;
803 vcpu->fpu_active = 0;
Rusty Russell707d92f2007-07-17 23:19:08 +1000804 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300805 update_exception_bitmap(vcpu);
806}
807
Avi Kivity6aa8b732006-12-10 02:21:36 -0800808static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
809{
Avi Kivity345dcaa2009-08-12 15:29:37 +0300810 unsigned long rflags;
811
812 rflags = vmcs_readl(GUEST_RFLAGS);
813 if (to_vmx(vcpu)->rmode.vm86_active)
814 rflags &= ~(unsigned long)(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
815 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816}
817
818static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
819{
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300820 if (to_vmx(vcpu)->rmode.vm86_active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100821 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822 vmcs_writel(GUEST_RFLAGS, rflags);
823}
824
Glauber Costa2809f5d2009-05-12 16:21:05 -0400825static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
826{
827 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
828 int ret = 0;
829
830 if (interruptibility & GUEST_INTR_STATE_STI)
831 ret |= X86_SHADOW_INT_STI;
832 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
833 ret |= X86_SHADOW_INT_MOV_SS;
834
835 return ret & mask;
836}
837
838static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
839{
840 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
841 u32 interruptibility = interruptibility_old;
842
843 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
844
845 if (mask & X86_SHADOW_INT_MOV_SS)
846 interruptibility |= GUEST_INTR_STATE_MOV_SS;
847 if (mask & X86_SHADOW_INT_STI)
848 interruptibility |= GUEST_INTR_STATE_STI;
849
850 if ((interruptibility != interruptibility_old))
851 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
852}
853
Avi Kivity6aa8b732006-12-10 02:21:36 -0800854static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
855{
856 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300858 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300860 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861
Glauber Costa2809f5d2009-05-12 16:21:05 -0400862 /* skipping an emulated instruction also counts */
863 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864}
865
Avi Kivity298101d2007-11-25 13:41:11 +0200866static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
867 bool has_error_code, u32 error_code)
868{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200869 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100870 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200871
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100872 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200873 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100874 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
875 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200876
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300877 if (vmx->rmode.vm86_active) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200878 vmx->rmode.irq.pending = true;
879 vmx->rmode.irq.vector = nr;
880 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +0300881 if (kvm_exception_is_soft(nr))
882 vmx->rmode.irq.rip +=
883 vmx->vcpu.arch.event_exit_inst_len;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100884 intr_info |= INTR_TYPE_SOFT_INTR;
885 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200886 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
887 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
888 return;
889 }
890
Gleb Natapov66fd3f72009-05-11 13:35:50 +0300891 if (kvm_exception_is_soft(nr)) {
892 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
893 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100894 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
895 } else
896 intr_info |= INTR_TYPE_HARD_EXCEPTION;
897
898 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200899}
900
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901/*
Eddie Donga75beee2007-05-17 18:55:15 +0300902 * Swap MSR entry in host/guest MSR entry array.
903 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200904#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000905static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300906{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400907 struct kvm_msr_entry tmp;
908
909 tmp = vmx->guest_msrs[to];
910 vmx->guest_msrs[to] = vmx->guest_msrs[from];
911 vmx->guest_msrs[from] = tmp;
912 tmp = vmx->host_msrs[to];
913 vmx->host_msrs[to] = vmx->host_msrs[from];
914 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300915}
Gabriel C54e11fa2007-08-01 16:23:10 +0200916#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300917
918/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300919 * Set up the vmcs to automatically save and restore system
920 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
921 * mode, as fiddling with msrs is very expensive.
922 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000923static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300924{
Eddie Dong2cc51562007-05-21 07:28:09 +0300925 int save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +0200926 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +0300927
Avi Kivity33f9c502008-02-27 16:06:57 +0200928 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300929 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300930#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000931 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300932 int index;
933
Rusty Russell8b9cf982007-07-30 16:31:43 +1000934 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300935 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000936 move_msr_up(vmx, index, save_nmsrs++);
937 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300938 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000939 move_msr_up(vmx, index, save_nmsrs++);
940 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300941 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000942 move_msr_up(vmx, index, save_nmsrs++);
943 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300944 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000945 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300946 /*
947 * MSR_K6_STAR is only needed on long mode guests, and only
948 * if efer.sce is enabled.
949 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000950 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800951 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000952 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300953 }
Eddie Donga75beee2007-05-17 18:55:15 +0300954#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400955 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300956
Eddie Donga75beee2007-05-17 18:55:15 +0300957#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400958 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000959 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300960#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000961 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivity58972972009-02-24 22:26:47 +0200962
963 if (cpu_has_vmx_msr_bitmap()) {
964 if (is_long_mode(&vmx->vcpu))
965 msr_bitmap = vmx_msr_bitmap_longmode;
966 else
967 msr_bitmap = vmx_msr_bitmap_legacy;
968
969 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
970 }
Avi Kivitye38aea32007-04-19 13:22:48 +0300971}
972
973/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 * reads and returns guest's timestamp counter "register"
975 * guest_tsc = host_tsc + tsc_offset -- 21.3
976 */
977static u64 guest_read_tsc(void)
978{
979 u64 host_tsc, tsc_offset;
980
981 rdtscll(host_tsc);
982 tsc_offset = vmcs_read64(TSC_OFFSET);
983 return host_tsc + tsc_offset;
984}
985
986/*
987 * writes 'guest_tsc' into guest's timestamp counter "register"
988 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
989 */
Marcelo Tosatti53f658b2008-12-11 20:45:05 +0100990static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
993}
994
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995/*
996 * Reads an msr value (of 'msr_index') into 'pdata'.
997 * Returns 0 on success, non-0 otherwise.
998 * Assumes vcpu_load() was already called.
999 */
1000static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1001{
1002 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001003 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004
1005 if (!pdata) {
1006 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
1007 return -EINVAL;
1008 }
1009
1010 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001011#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012 case MSR_FS_BASE:
1013 data = vmcs_readl(GUEST_FS_BASE);
1014 break;
1015 case MSR_GS_BASE:
1016 data = vmcs_readl(GUEST_GS_BASE);
1017 break;
1018 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001019 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020#endif
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301021 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022 data = guest_read_tsc();
1023 break;
1024 case MSR_IA32_SYSENTER_CS:
1025 data = vmcs_read32(GUEST_SYSENTER_CS);
1026 break;
1027 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001028 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029 break;
1030 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001031 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001034 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001035 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001036 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity3bab1f52006-12-29 16:49:48 -08001037 data = msr->data;
1038 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001040 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041 }
1042
1043 *pdata = data;
1044 return 0;
1045}
1046
1047/*
1048 * Writes msr value into into the appropriate "register".
1049 * Returns 0 on success, non-0 otherwise.
1050 * Assumes vcpu_load() was already called.
1051 */
1052static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1053{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001054 struct vcpu_vmx *vmx = to_vmx(vcpu);
1055 struct kvm_msr_entry *msr;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01001056 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +03001057 int ret = 0;
1058
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001060 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001061 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001062 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001063 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001064#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065 case MSR_FS_BASE:
1066 vmcs_writel(GUEST_FS_BASE, data);
1067 break;
1068 case MSR_GS_BASE:
1069 vmcs_writel(GUEST_GS_BASE, data);
1070 break;
1071#endif
1072 case MSR_IA32_SYSENTER_CS:
1073 vmcs_write32(GUEST_SYSENTER_CS, data);
1074 break;
1075 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001076 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077 break;
1078 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001079 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301081 case MSR_IA32_TSC:
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01001082 rdtscll(host_tsc);
1083 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001085 case MSR_IA32_CR_PAT:
1086 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1087 vmcs_write64(GUEST_IA32_PAT, data);
1088 vcpu->arch.pat = data;
1089 break;
1090 }
1091 /* Otherwise falls through to kvm_set_msr_common */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001093 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001094 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001095 vmx_load_host_state(vmx);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001096 msr->data = data;
1097 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001099 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100 }
1101
Eddie Dong2cc51562007-05-21 07:28:09 +03001102 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001103}
1104
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001105static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001107 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1108 switch (reg) {
1109 case VCPU_REGS_RSP:
1110 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1111 break;
1112 case VCPU_REGS_RIP:
1113 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1114 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001115 case VCPU_EXREG_PDPTR:
1116 if (enable_ept)
1117 ept_save_pdptrs(vcpu);
1118 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001119 default:
1120 break;
1121 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001122}
1123
Jan Kiszka355be0b2009-10-03 00:31:21 +02001124static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125{
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001126 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1127 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1128 else
1129 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1130
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001131 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001132}
1133
1134static __init int cpu_has_kvm_support(void)
1135{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001136 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137}
1138
1139static __init int vmx_disabled_by_bios(void)
1140{
1141 u64 msr;
1142
1143 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001144 return (msr & (FEATURE_CONTROL_LOCKED |
1145 FEATURE_CONTROL_VMXON_ENABLED))
1146 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001147 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148}
1149
Alexander Graf10474ae2009-09-15 11:37:46 +02001150static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151{
1152 int cpu = raw_smp_processor_id();
1153 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1154 u64 old;
1155
Alexander Graf10474ae2009-09-15 11:37:46 +02001156 if (read_cr4() & X86_CR4_VMXE)
1157 return -EBUSY;
1158
Avi Kivity543e4242008-05-13 16:22:47 +03001159 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001161 if ((old & (FEATURE_CONTROL_LOCKED |
1162 FEATURE_CONTROL_VMXON_ENABLED))
1163 != (FEATURE_CONTROL_LOCKED |
1164 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001166 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001167 FEATURE_CONTROL_LOCKED |
1168 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001169 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001170 asm volatile (ASM_VMX_VMXON_RAX
1171 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001172 : "memory", "cc");
Alexander Graf10474ae2009-09-15 11:37:46 +02001173
1174 ept_sync_global();
1175
1176 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001177}
1178
Avi Kivity543e4242008-05-13 16:22:47 +03001179static void vmclear_local_vcpus(void)
1180{
1181 int cpu = raw_smp_processor_id();
1182 struct vcpu_vmx *vmx, *n;
1183
1184 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1185 local_vcpus_link)
1186 __vcpu_clear(vmx);
1187}
1188
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001189
1190/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1191 * tricks.
1192 */
1193static void kvm_cpu_vmxoff(void)
1194{
1195 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1196 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1197}
1198
Avi Kivity6aa8b732006-12-10 02:21:36 -08001199static void hardware_disable(void *garbage)
1200{
Avi Kivity543e4242008-05-13 16:22:47 +03001201 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001202 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203}
1204
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001205static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001206 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207{
1208 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001209 u32 ctl = ctl_min | ctl_opt;
1210
1211 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1212
1213 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1214 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1215
1216 /* Ensure minimum (required) set of control bits are supported. */
1217 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001218 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001219
1220 *result = ctl;
1221 return 0;
1222}
1223
Yang, Sheng002c7f72007-07-31 14:23:01 +03001224static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001225{
1226 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001227 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001228 u32 _pin_based_exec_control = 0;
1229 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001230 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001231 u32 _vmexit_control = 0;
1232 u32 _vmentry_control = 0;
1233
1234 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001235 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001236 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1237 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001238 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001239
1240 min = CPU_BASED_HLT_EXITING |
1241#ifdef CONFIG_X86_64
1242 CPU_BASED_CR8_LOAD_EXITING |
1243 CPU_BASED_CR8_STORE_EXITING |
1244#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001245 CPU_BASED_CR3_LOAD_EXITING |
1246 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001247 CPU_BASED_USE_IO_BITMAPS |
1248 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001249 CPU_BASED_USE_TSC_OFFSETING |
1250 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001251 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001252 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001253 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001254 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1255 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001256 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001257#ifdef CONFIG_X86_64
1258 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1259 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1260 ~CPU_BASED_CR8_STORE_EXITING;
1261#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001262 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001263 min2 = 0;
1264 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001265 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001266 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001267 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001268 SECONDARY_EXEC_UNRESTRICTED_GUEST |
1269 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08001270 if (adjust_vmx_controls(min2, opt2,
1271 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001272 &_cpu_based_2nd_exec_control) < 0)
1273 return -EIO;
1274 }
1275#ifndef CONFIG_X86_64
1276 if (!(_cpu_based_2nd_exec_control &
1277 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1278 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1279#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001280 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001281 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1282 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03001283 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
1284 CPU_BASED_CR3_STORE_EXITING |
1285 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001286 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1287 vmx_capability.ept, vmx_capability.vpid);
1288 }
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001289
1290 min = 0;
1291#ifdef CONFIG_X86_64
1292 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1293#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001294 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001295 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1296 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001297 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001298
Sheng Yang468d4722008-10-09 16:01:55 +08001299 min = 0;
1300 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001301 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1302 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001303 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001304
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001305 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001306
1307 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1308 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001309 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001310
1311#ifdef CONFIG_X86_64
1312 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1313 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001314 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001315#endif
1316
1317 /* Require Write-Back (WB) memory type for VMCS accesses. */
1318 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001319 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001320
Yang, Sheng002c7f72007-07-31 14:23:01 +03001321 vmcs_conf->size = vmx_msr_high & 0x1fff;
1322 vmcs_conf->order = get_order(vmcs_config.size);
1323 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001324
Yang, Sheng002c7f72007-07-31 14:23:01 +03001325 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1326 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001327 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001328 vmcs_conf->vmexit_ctrl = _vmexit_control;
1329 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001330
1331 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001332}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333
1334static struct vmcs *alloc_vmcs_cpu(int cpu)
1335{
1336 int node = cpu_to_node(cpu);
1337 struct page *pages;
1338 struct vmcs *vmcs;
1339
Mel Gorman6484eb32009-06-16 15:31:54 -07001340 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001341 if (!pages)
1342 return NULL;
1343 vmcs = page_address(pages);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001344 memset(vmcs, 0, vmcs_config.size);
1345 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346 return vmcs;
1347}
1348
1349static struct vmcs *alloc_vmcs(void)
1350{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001351 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352}
1353
1354static void free_vmcs(struct vmcs *vmcs)
1355{
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001356 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357}
1358
Sam Ravnborg39959582007-06-01 00:47:13 -07001359static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360{
1361 int cpu;
1362
Zachary Amsden3230bb42009-09-29 11:38:37 -10001363 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10001365 per_cpu(vmxarea, cpu) = NULL;
1366 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367}
1368
Avi Kivity6aa8b732006-12-10 02:21:36 -08001369static __init int alloc_kvm_area(void)
1370{
1371 int cpu;
1372
Zachary Amsden3230bb42009-09-29 11:38:37 -10001373 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374 struct vmcs *vmcs;
1375
1376 vmcs = alloc_vmcs_cpu(cpu);
1377 if (!vmcs) {
1378 free_kvm_area();
1379 return -ENOMEM;
1380 }
1381
1382 per_cpu(vmxarea, cpu) = vmcs;
1383 }
1384 return 0;
1385}
1386
1387static __init int hardware_setup(void)
1388{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001389 if (setup_vmcs_config(&vmcs_config) < 0)
1390 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001391
1392 if (boot_cpu_has(X86_FEATURE_NX))
1393 kvm_enable_efer_bits(EFER_NX);
1394
Sheng Yang93ba03c2009-04-01 15:52:32 +08001395 if (!cpu_has_vmx_vpid())
1396 enable_vpid = 0;
1397
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001398 if (!cpu_has_vmx_ept()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001399 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001400 enable_unrestricted_guest = 0;
1401 }
1402
1403 if (!cpu_has_vmx_unrestricted_guest())
1404 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001405
1406 if (!cpu_has_vmx_flexpriority())
1407 flexpriority_enabled = 0;
1408
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001409 if (!cpu_has_vmx_tpr_shadow())
1410 kvm_x86_ops->update_cr8_intercept = NULL;
1411
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001412 if (enable_ept && !cpu_has_vmx_ept_2m_page())
1413 kvm_disable_largepages();
1414
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001415 if (!cpu_has_vmx_ple())
1416 ple_gap = 0;
1417
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418 return alloc_kvm_area();
1419}
1420
1421static __exit void hardware_unsetup(void)
1422{
1423 free_kvm_area();
1424}
1425
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1427{
1428 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1429
Avi Kivity6af11b92007-03-19 13:18:10 +02001430 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431 vmcs_write16(sf->selector, save->selector);
1432 vmcs_writel(sf->base, save->base);
1433 vmcs_write32(sf->limit, save->limit);
1434 vmcs_write32(sf->ar_bytes, save->ar);
1435 } else {
1436 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1437 << AR_DPL_SHIFT;
1438 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1439 }
1440}
1441
1442static void enter_pmode(struct kvm_vcpu *vcpu)
1443{
1444 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001445 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001446
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001447 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001448 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001450 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
1451 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
1452 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453
1454 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001455 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001456 flags |= (vmx->rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457 vmcs_writel(GUEST_RFLAGS, flags);
1458
Rusty Russell66aee912007-07-17 23:34:16 +10001459 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1460 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001461
1462 update_exception_bitmap(vcpu);
1463
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001464 if (emulate_invalid_guest_state)
1465 return;
1466
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001467 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
1468 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
1469 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
1470 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471
1472 vmcs_write16(GUEST_SS_SELECTOR, 0);
1473 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1474
1475 vmcs_write16(GUEST_CS_SELECTOR,
1476 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1477 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1478}
1479
Mike Dayd77c26f2007-10-08 09:02:08 -04001480static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001482 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001483 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1484 kvm->memslots[0].npages - 3;
1485 return base_gfn << PAGE_SHIFT;
1486 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001487 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488}
1489
1490static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1491{
1492 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1493
1494 save->selector = vmcs_read16(sf->selector);
1495 save->base = vmcs_readl(sf->base);
1496 save->limit = vmcs_read32(sf->limit);
1497 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001498 vmcs_write16(sf->selector, save->base >> 4);
1499 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001500 vmcs_write32(sf->limit, 0xffff);
1501 vmcs_write32(sf->ar_bytes, 0xf3);
1502}
1503
1504static void enter_rmode(struct kvm_vcpu *vcpu)
1505{
1506 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001507 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001509 if (enable_unrestricted_guest)
1510 return;
1511
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001512 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001513 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001515 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1517
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001518 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1520
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001521 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1523
1524 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001525 vmx->rmode.save_iopl
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001526 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001528 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001529
1530 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001531 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001532 update_exception_bitmap(vcpu);
1533
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001534 if (emulate_invalid_guest_state)
1535 goto continue_rmode;
1536
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1538 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1539 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1540
1541 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001542 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001543 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1544 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001545 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1546
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001547 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
1548 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
1549 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
1550 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001551
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001552continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001553 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001554 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001555}
1556
Amit Shah401d10d2009-02-20 22:53:37 +05301557static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1558{
1559 struct vcpu_vmx *vmx = to_vmx(vcpu);
1560 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1561
1562 vcpu->arch.shadow_efer = efer;
1563 if (!msr)
1564 return;
1565 if (efer & EFER_LMA) {
1566 vmcs_write32(VM_ENTRY_CONTROLS,
1567 vmcs_read32(VM_ENTRY_CONTROLS) |
1568 VM_ENTRY_IA32E_MODE);
1569 msr->data = efer;
1570 } else {
1571 vmcs_write32(VM_ENTRY_CONTROLS,
1572 vmcs_read32(VM_ENTRY_CONTROLS) &
1573 ~VM_ENTRY_IA32E_MODE);
1574
1575 msr->data = efer & ~EFER_LME;
1576 }
1577 setup_msrs(vmx);
1578}
1579
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001580#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581
1582static void enter_lmode(struct kvm_vcpu *vcpu)
1583{
1584 u32 guest_tr_ar;
1585
1586 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1587 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1588 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001589 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001590 vmcs_write32(GUEST_TR_AR_BYTES,
1591 (guest_tr_ar & ~AR_TYPE_MASK)
1592 | AR_TYPE_BUSY_64_TSS);
1593 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001594 vcpu->arch.shadow_efer |= EFER_LMA;
Amit Shah401d10d2009-02-20 22:53:37 +05301595 vmx_set_efer(vcpu, vcpu->arch.shadow_efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596}
1597
1598static void exit_lmode(struct kvm_vcpu *vcpu)
1599{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001600 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001601
1602 vmcs_write32(VM_ENTRY_CONTROLS,
1603 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001604 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001605}
1606
1607#endif
1608
Sheng Yang2384d2b2008-01-17 15:14:33 +08001609static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1610{
1611 vpid_sync_vcpu_all(to_vmx(vcpu));
Avi Kivity089d0342009-03-23 18:26:32 +02001612 if (enable_ept)
Sheng Yang4e1096d2008-07-06 19:16:51 +08001613 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001614}
1615
Anthony Liguori25c4c272007-04-27 09:29:21 +03001616static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001617{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001618 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1619 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001620}
1621
Sheng Yang14394422008-04-28 12:24:45 +08001622static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1623{
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001624 if (!test_bit(VCPU_EXREG_PDPTR,
1625 (unsigned long *)&vcpu->arch.regs_dirty))
1626 return;
1627
Sheng Yang14394422008-04-28 12:24:45 +08001628 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Sheng Yang14394422008-04-28 12:24:45 +08001629 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1630 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1631 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1632 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1633 }
1634}
1635
Avi Kivity8f5d5492009-05-31 18:41:29 +03001636static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
1637{
1638 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1639 vcpu->arch.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
1640 vcpu->arch.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
1641 vcpu->arch.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
1642 vcpu->arch.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
1643 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001644
1645 __set_bit(VCPU_EXREG_PDPTR,
1646 (unsigned long *)&vcpu->arch.regs_avail);
1647 __set_bit(VCPU_EXREG_PDPTR,
1648 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03001649}
1650
Sheng Yang14394422008-04-28 12:24:45 +08001651static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1652
1653static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1654 unsigned long cr0,
1655 struct kvm_vcpu *vcpu)
1656{
1657 if (!(cr0 & X86_CR0_PG)) {
1658 /* From paging/starting to nonpaging */
1659 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001660 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001661 (CPU_BASED_CR3_LOAD_EXITING |
1662 CPU_BASED_CR3_STORE_EXITING));
1663 vcpu->arch.cr0 = cr0;
1664 vmx_set_cr4(vcpu, vcpu->arch.cr4);
Sheng Yang14394422008-04-28 12:24:45 +08001665 } else if (!is_paging(vcpu)) {
1666 /* From nonpaging to paging */
1667 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001668 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001669 ~(CPU_BASED_CR3_LOAD_EXITING |
1670 CPU_BASED_CR3_STORE_EXITING));
1671 vcpu->arch.cr0 = cr0;
1672 vmx_set_cr4(vcpu, vcpu->arch.cr4);
Sheng Yang14394422008-04-28 12:24:45 +08001673 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08001674
1675 if (!(cr0 & X86_CR0_WP))
1676 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08001677}
1678
1679static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1680 struct kvm_vcpu *vcpu)
1681{
1682 if (!is_paging(vcpu)) {
1683 *hw_cr4 &= ~X86_CR4_PAE;
1684 *hw_cr4 |= X86_CR4_PSE;
1685 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1686 *hw_cr4 &= ~X86_CR4_PAE;
1687}
1688
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1690{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001691 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001692 unsigned long hw_cr0;
1693
1694 if (enable_unrestricted_guest)
1695 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
1696 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
1697 else
1698 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08001699
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001700 vmx_fpu_deactivate(vcpu);
1701
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001702 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001703 enter_pmode(vcpu);
1704
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001705 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706 enter_rmode(vcpu);
1707
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001708#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001709 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001710 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10001712 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001713 exit_lmode(vcpu);
1714 }
1715#endif
1716
Avi Kivity089d0342009-03-23 18:26:32 +02001717 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001718 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1719
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001721 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001722 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001723
Rusty Russell707d92f2007-07-17 23:19:08 +10001724 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001725 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726}
1727
Sheng Yang14394422008-04-28 12:24:45 +08001728static u64 construct_eptp(unsigned long root_hpa)
1729{
1730 u64 eptp;
1731
1732 /* TODO write the value reading from MSR */
1733 eptp = VMX_EPT_DEFAULT_MT |
1734 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1735 eptp |= (root_hpa & PAGE_MASK);
1736
1737 return eptp;
1738}
1739
Avi Kivity6aa8b732006-12-10 02:21:36 -08001740static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1741{
Sheng Yang14394422008-04-28 12:24:45 +08001742 unsigned long guest_cr3;
1743 u64 eptp;
1744
1745 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02001746 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001747 eptp = construct_eptp(cr3);
1748 vmcs_write64(EPT_POINTER, eptp);
Sheng Yang14394422008-04-28 12:24:45 +08001749 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
Sheng Yangb927a3c2009-07-21 10:42:48 +08001750 vcpu->kvm->arch.ept_identity_map_addr;
Sheng Yang14394422008-04-28 12:24:45 +08001751 }
1752
Sheng Yang2384d2b2008-01-17 15:14:33 +08001753 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001754 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001755 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001756 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001757}
1758
1759static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1760{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001761 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08001762 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1763
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001764 vcpu->arch.cr4 = cr4;
Avi Kivity089d0342009-03-23 18:26:32 +02001765 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001766 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1767
1768 vmcs_writel(CR4_READ_SHADOW, cr4);
1769 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001770}
1771
Avi Kivity6aa8b732006-12-10 02:21:36 -08001772static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1773{
1774 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1775
1776 return vmcs_readl(sf->base);
1777}
1778
1779static void vmx_get_segment(struct kvm_vcpu *vcpu,
1780 struct kvm_segment *var, int seg)
1781{
1782 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1783 u32 ar;
1784
1785 var->base = vmcs_readl(sf->base);
1786 var->limit = vmcs_read32(sf->limit);
1787 var->selector = vmcs_read16(sf->selector);
1788 ar = vmcs_read32(sf->ar_bytes);
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02001789 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790 ar = 0;
1791 var->type = ar & 15;
1792 var->s = (ar >> 4) & 1;
1793 var->dpl = (ar >> 5) & 3;
1794 var->present = (ar >> 7) & 1;
1795 var->avl = (ar >> 12) & 1;
1796 var->l = (ar >> 13) & 1;
1797 var->db = (ar >> 14) & 1;
1798 var->g = (ar >> 15) & 1;
1799 var->unusable = (ar >> 16) & 1;
1800}
1801
Izik Eidus2e4d2652008-03-24 19:38:34 +02001802static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1803{
Izik Eidus2e4d2652008-03-24 19:38:34 +02001804 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1805 return 0;
1806
1807 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1808 return 3;
1809
Avi Kivityeab4b8a2009-08-04 15:02:54 +03001810 return vmcs_read16(GUEST_CS_SELECTOR) & 3;
Izik Eidus2e4d2652008-03-24 19:38:34 +02001811}
1812
Avi Kivity653e3102007-05-07 10:55:37 +03001813static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001814{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001815 u32 ar;
1816
Avi Kivity653e3102007-05-07 10:55:37 +03001817 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001818 ar = 1 << 16;
1819 else {
1820 ar = var->type & 15;
1821 ar |= (var->s & 1) << 4;
1822 ar |= (var->dpl & 3) << 5;
1823 ar |= (var->present & 1) << 7;
1824 ar |= (var->avl & 1) << 12;
1825 ar |= (var->l & 1) << 13;
1826 ar |= (var->db & 1) << 14;
1827 ar |= (var->g & 1) << 15;
1828 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001829 if (ar == 0) /* a 0 value means unusable */
1830 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001831
1832 return ar;
1833}
1834
1835static void vmx_set_segment(struct kvm_vcpu *vcpu,
1836 struct kvm_segment *var, int seg)
1837{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001838 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03001839 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1840 u32 ar;
1841
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001842 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
1843 vmx->rmode.tr.selector = var->selector;
1844 vmx->rmode.tr.base = var->base;
1845 vmx->rmode.tr.limit = var->limit;
1846 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001847 return;
1848 }
1849 vmcs_writel(sf->base, var->base);
1850 vmcs_write32(sf->limit, var->limit);
1851 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001852 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001853 /*
1854 * Hack real-mode segments into vm86 compatibility.
1855 */
1856 if (var->base == 0xffff0000 && var->selector == 0xf000)
1857 vmcs_writel(sf->base, 0xf0000);
1858 ar = 0xf3;
1859 } else
1860 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001861
1862 /*
1863 * Fix the "Accessed" bit in AR field of segment registers for older
1864 * qemu binaries.
1865 * IA32 arch specifies that at the time of processor reset the
1866 * "Accessed" bit in the AR field of segment registers is 1. And qemu
1867 * is setting it to 0 in the usedland code. This causes invalid guest
1868 * state vmexit when "unrestricted guest" mode is turned on.
1869 * Fix for this setup issue in cpu_reset is being pushed in the qemu
1870 * tree. Newer qemu binaries with that qemu fix would not need this
1871 * kvm hack.
1872 */
1873 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
1874 ar |= 0x1; /* Accessed */
1875
Avi Kivity6aa8b732006-12-10 02:21:36 -08001876 vmcs_write32(sf->ar_bytes, ar);
1877}
1878
Avi Kivity6aa8b732006-12-10 02:21:36 -08001879static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1880{
1881 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1882
1883 *db = (ar >> 14) & 1;
1884 *l = (ar >> 13) & 1;
1885}
1886
1887static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1888{
1889 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1890 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1891}
1892
1893static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1894{
1895 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1896 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1897}
1898
1899static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1900{
1901 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1902 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1903}
1904
1905static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1906{
1907 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1908 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1909}
1910
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001911static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1912{
1913 struct kvm_segment var;
1914 u32 ar;
1915
1916 vmx_get_segment(vcpu, &var, seg);
1917 ar = vmx_segment_access_rights(&var);
1918
1919 if (var.base != (var.selector << 4))
1920 return false;
1921 if (var.limit != 0xffff)
1922 return false;
1923 if (ar != 0xf3)
1924 return false;
1925
1926 return true;
1927}
1928
1929static bool code_segment_valid(struct kvm_vcpu *vcpu)
1930{
1931 struct kvm_segment cs;
1932 unsigned int cs_rpl;
1933
1934 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1935 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1936
Avi Kivity1872a3f2009-01-04 23:26:52 +02001937 if (cs.unusable)
1938 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001939 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1940 return false;
1941 if (!cs.s)
1942 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001943 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001944 if (cs.dpl > cs_rpl)
1945 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001946 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001947 if (cs.dpl != cs_rpl)
1948 return false;
1949 }
1950 if (!cs.present)
1951 return false;
1952
1953 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1954 return true;
1955}
1956
1957static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1958{
1959 struct kvm_segment ss;
1960 unsigned int ss_rpl;
1961
1962 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1963 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1964
Avi Kivity1872a3f2009-01-04 23:26:52 +02001965 if (ss.unusable)
1966 return true;
1967 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001968 return false;
1969 if (!ss.s)
1970 return false;
1971 if (ss.dpl != ss_rpl) /* DPL != RPL */
1972 return false;
1973 if (!ss.present)
1974 return false;
1975
1976 return true;
1977}
1978
1979static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1980{
1981 struct kvm_segment var;
1982 unsigned int rpl;
1983
1984 vmx_get_segment(vcpu, &var, seg);
1985 rpl = var.selector & SELECTOR_RPL_MASK;
1986
Avi Kivity1872a3f2009-01-04 23:26:52 +02001987 if (var.unusable)
1988 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001989 if (!var.s)
1990 return false;
1991 if (!var.present)
1992 return false;
1993 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
1994 if (var.dpl < rpl) /* DPL < RPL */
1995 return false;
1996 }
1997
1998 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1999 * rights flags
2000 */
2001 return true;
2002}
2003
2004static bool tr_valid(struct kvm_vcpu *vcpu)
2005{
2006 struct kvm_segment tr;
2007
2008 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
2009
Avi Kivity1872a3f2009-01-04 23:26:52 +02002010 if (tr.unusable)
2011 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002012 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2013 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002014 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002015 return false;
2016 if (!tr.present)
2017 return false;
2018
2019 return true;
2020}
2021
2022static bool ldtr_valid(struct kvm_vcpu *vcpu)
2023{
2024 struct kvm_segment ldtr;
2025
2026 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2027
Avi Kivity1872a3f2009-01-04 23:26:52 +02002028 if (ldtr.unusable)
2029 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002030 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2031 return false;
2032 if (ldtr.type != 2)
2033 return false;
2034 if (!ldtr.present)
2035 return false;
2036
2037 return true;
2038}
2039
2040static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2041{
2042 struct kvm_segment cs, ss;
2043
2044 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2045 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2046
2047 return ((cs.selector & SELECTOR_RPL_MASK) ==
2048 (ss.selector & SELECTOR_RPL_MASK));
2049}
2050
2051/*
2052 * Check if guest state is valid. Returns true if valid, false if
2053 * not.
2054 * We assume that registers are always usable
2055 */
2056static bool guest_state_valid(struct kvm_vcpu *vcpu)
2057{
2058 /* real mode guest state checks */
2059 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
2060 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2061 return false;
2062 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2063 return false;
2064 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2065 return false;
2066 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2067 return false;
2068 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2069 return false;
2070 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2071 return false;
2072 } else {
2073 /* protected mode guest state checks */
2074 if (!cs_ss_rpl_check(vcpu))
2075 return false;
2076 if (!code_segment_valid(vcpu))
2077 return false;
2078 if (!stack_segment_valid(vcpu))
2079 return false;
2080 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2081 return false;
2082 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2083 return false;
2084 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2085 return false;
2086 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2087 return false;
2088 if (!tr_valid(vcpu))
2089 return false;
2090 if (!ldtr_valid(vcpu))
2091 return false;
2092 }
2093 /* TODO:
2094 * - Add checks on RIP
2095 * - Add checks on RFLAGS
2096 */
2097
2098 return true;
2099}
2100
Mike Dayd77c26f2007-10-08 09:02:08 -04002101static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002102{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002103 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002104 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002105 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02002106 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107
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 = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002112 r = kvm_write_guest_page(kvm, fn++, &data,
2113 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002114 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002115 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002116 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2117 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002118 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002119 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2120 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002121 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002122 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002123 r = kvm_write_guest_page(kvm, fn, &data,
2124 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2125 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002126 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002127 goto out;
2128
2129 ret = 1;
2130out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002131 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002132}
2133
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002134static int init_rmode_identity_map(struct kvm *kvm)
2135{
2136 int i, r, ret;
2137 pfn_t identity_map_pfn;
2138 u32 tmp;
2139
Avi Kivity089d0342009-03-23 18:26:32 +02002140 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002141 return 1;
2142 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2143 printk(KERN_ERR "EPT: identity-mapping pagetable "
2144 "haven't been allocated!\n");
2145 return 0;
2146 }
2147 if (likely(kvm->arch.ept_identity_pagetable_done))
2148 return 1;
2149 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002150 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002151 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2152 if (r < 0)
2153 goto out;
2154 /* Set up identity-mapping pagetable for EPT in real mode */
2155 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2156 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2157 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2158 r = kvm_write_guest_page(kvm, identity_map_pfn,
2159 &tmp, i * sizeof(tmp), sizeof(tmp));
2160 if (r < 0)
2161 goto out;
2162 }
2163 kvm->arch.ept_identity_pagetable_done = true;
2164 ret = 1;
2165out:
2166 return ret;
2167}
2168
Avi Kivity6aa8b732006-12-10 02:21:36 -08002169static void seg_setup(int seg)
2170{
2171 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002172 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173
2174 vmcs_write16(sf->selector, 0);
2175 vmcs_writel(sf->base, 0);
2176 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002177 if (enable_unrestricted_guest) {
2178 ar = 0x93;
2179 if (seg == VCPU_SREG_CS)
2180 ar |= 0x08; /* code segment */
2181 } else
2182 ar = 0xf3;
2183
2184 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002185}
2186
Sheng Yangf78e0e22007-10-29 09:40:42 +08002187static int alloc_apic_access_page(struct kvm *kvm)
2188{
2189 struct kvm_userspace_memory_region kvm_userspace_mem;
2190 int r = 0;
2191
Izik Eidus72dc67a2008-02-10 18:04:15 +02002192 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002193 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002194 goto out;
2195 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2196 kvm_userspace_mem.flags = 0;
2197 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2198 kvm_userspace_mem.memory_size = PAGE_SIZE;
2199 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2200 if (r)
2201 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002202
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002203 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002204out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002205 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002206 return r;
2207}
2208
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002209static int alloc_identity_pagetable(struct kvm *kvm)
2210{
2211 struct kvm_userspace_memory_region kvm_userspace_mem;
2212 int r = 0;
2213
2214 down_write(&kvm->slots_lock);
2215 if (kvm->arch.ept_identity_pagetable)
2216 goto out;
2217 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2218 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002219 kvm_userspace_mem.guest_phys_addr =
2220 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002221 kvm_userspace_mem.memory_size = PAGE_SIZE;
2222 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2223 if (r)
2224 goto out;
2225
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002226 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
Sheng Yangb927a3c2009-07-21 10:42:48 +08002227 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002228out:
2229 up_write(&kvm->slots_lock);
2230 return r;
2231}
2232
Sheng Yang2384d2b2008-01-17 15:14:33 +08002233static void allocate_vpid(struct vcpu_vmx *vmx)
2234{
2235 int vpid;
2236
2237 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002238 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002239 return;
2240 spin_lock(&vmx_vpid_lock);
2241 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2242 if (vpid < VMX_NR_VPIDS) {
2243 vmx->vpid = vpid;
2244 __set_bit(vpid, vmx_vpid_bitmap);
2245 }
2246 spin_unlock(&vmx_vpid_lock);
2247}
2248
Avi Kivity58972972009-02-24 22:26:47 +02002249static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002250{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002251 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002252
2253 if (!cpu_has_vmx_msr_bitmap())
2254 return;
2255
2256 /*
2257 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2258 * have the write-low and read-high bitmap offsets the wrong way round.
2259 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2260 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002261 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002262 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2263 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002264 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2265 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002266 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2267 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002268 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002269}
2270
Avi Kivity58972972009-02-24 22:26:47 +02002271static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2272{
2273 if (!longmode_only)
2274 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2275 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2276}
2277
Avi Kivity6aa8b732006-12-10 02:21:36 -08002278/*
2279 * Sets up the vmcs for emulated real mode.
2280 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002281static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002282{
Sheng Yang468d4722008-10-09 16:01:55 +08002283 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002284 u32 junk;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002285 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002286 unsigned long a;
2287 struct descriptor_table dt;
2288 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002289 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002290 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002291
Avi Kivity6aa8b732006-12-10 02:21:36 -08002292 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002293 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2294 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002295
Sheng Yang25c5f222008-03-28 13:18:56 +08002296 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002297 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002298
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2300
Avi Kivity6aa8b732006-12-10 02:21:36 -08002301 /* Control */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002302 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2303 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002304
2305 exec_control = vmcs_config.cpu_based_exec_ctrl;
2306 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2307 exec_control &= ~CPU_BASED_TPR_SHADOW;
2308#ifdef CONFIG_X86_64
2309 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2310 CPU_BASED_CR8_LOAD_EXITING;
2311#endif
2312 }
Avi Kivity089d0342009-03-23 18:26:32 +02002313 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002314 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002315 CPU_BASED_CR3_LOAD_EXITING |
2316 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002317 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318
Sheng Yang83ff3b92007-11-21 14:33:25 +08002319 if (cpu_has_secondary_exec_ctrls()) {
2320 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2321 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2322 exec_control &=
2323 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002324 if (vmx->vpid == 0)
2325 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Avi Kivity089d0342009-03-23 18:26:32 +02002326 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002327 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002328 if (!enable_unrestricted_guest)
2329 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002330 if (!ple_gap)
2331 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002332 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2333 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002334
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002335 if (ple_gap) {
2336 vmcs_write32(PLE_GAP, ple_gap);
2337 vmcs_write32(PLE_WINDOW, ple_window);
2338 }
2339
Avi Kivityc7addb92007-09-16 18:58:32 +02002340 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2341 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002342 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2343
2344 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2345 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2346 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2347
2348 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2349 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2350 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002351 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2352 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002353 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002354#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355 rdmsrl(MSR_FS_BASE, a);
2356 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2357 rdmsrl(MSR_GS_BASE, a);
2358 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2359#else
2360 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2361 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2362#endif
2363
2364 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2365
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002366 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2368
Mike Dayd77c26f2007-10-08 09:02:08 -04002369 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002370 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002371 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2372 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2373 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374
2375 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2376 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2377 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2378 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2379 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2380 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2381
Sheng Yang468d4722008-10-09 16:01:55 +08002382 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2383 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2384 host_pat = msr_low | ((u64) msr_high << 32);
2385 vmcs_write64(HOST_IA32_PAT, host_pat);
2386 }
2387 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2388 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2389 host_pat = msr_low | ((u64) msr_high << 32);
2390 /* Write the default value follow host pat */
2391 vmcs_write64(GUEST_IA32_PAT, host_pat);
2392 /* Keep arch.pat sync with GUEST_IA32_PAT */
2393 vmx->vcpu.arch.pat = host_pat;
2394 }
2395
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396 for (i = 0; i < NR_VMX_MSR; ++i) {
2397 u32 index = vmx_msr_index[i];
2398 u32 data_low, data_high;
2399 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002400 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002401
2402 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2403 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002404 if (wrmsr_safe(index, data_low, data_high) < 0)
2405 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002406 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002407 vmx->host_msrs[j].index = index;
2408 vmx->host_msrs[j].reserved = 0;
2409 vmx->host_msrs[j].data = data;
2410 vmx->guest_msrs[j] = vmx->host_msrs[j];
2411 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002412 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002414 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002415
2416 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002417 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2418
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002419 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2420 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2421
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002422 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2423 rdtscll(tsc_this);
2424 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2425 tsc_base = tsc_this;
2426
2427 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002428
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002429 return 0;
2430}
2431
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002432static int init_rmode(struct kvm *kvm)
2433{
2434 if (!init_rmode_tss(kvm))
2435 return 0;
2436 if (!init_rmode_identity_map(kvm))
2437 return 0;
2438 return 1;
2439}
2440
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002441static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2442{
2443 struct vcpu_vmx *vmx = to_vmx(vcpu);
2444 u64 msr;
2445 int ret;
2446
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002447 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002448 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002449 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002450 ret = -ENOMEM;
2451 goto out;
2452 }
2453
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002454 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002455
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002456 vmx->soft_vnmi_blocked = 0;
2457
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002458 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002459 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002460 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002461 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002462 msr |= MSR_IA32_APICBASE_BSP;
2463 kvm_set_apic_base(&vmx->vcpu, msr);
2464
2465 fx_init(&vmx->vcpu);
2466
Avi Kivity5706be02008-08-20 15:07:31 +03002467 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002468 /*
2469 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2470 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2471 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002472 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002473 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2474 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2475 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002476 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2477 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002478 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002479
2480 seg_setup(VCPU_SREG_DS);
2481 seg_setup(VCPU_SREG_ES);
2482 seg_setup(VCPU_SREG_FS);
2483 seg_setup(VCPU_SREG_GS);
2484 seg_setup(VCPU_SREG_SS);
2485
2486 vmcs_write16(GUEST_TR_SELECTOR, 0);
2487 vmcs_writel(GUEST_TR_BASE, 0);
2488 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2489 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2490
2491 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2492 vmcs_writel(GUEST_LDTR_BASE, 0);
2493 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2494 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2495
2496 vmcs_write32(GUEST_SYSENTER_CS, 0);
2497 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2498 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2499
2500 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002501 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002502 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002503 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002504 kvm_rip_write(vcpu, 0);
2505 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002506
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002507 vmcs_writel(GUEST_DR7, 0x400);
2508
2509 vmcs_writel(GUEST_GDTR_BASE, 0);
2510 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2511
2512 vmcs_writel(GUEST_IDTR_BASE, 0);
2513 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2514
2515 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2516 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2517 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2518
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002519 /* Special registers */
2520 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2521
2522 setup_msrs(vmx);
2523
Avi Kivity6aa8b732006-12-10 02:21:36 -08002524 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2525
Sheng Yangf78e0e22007-10-29 09:40:42 +08002526 if (cpu_has_vmx_tpr_shadow()) {
2527 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2528 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2529 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002530 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002531 vmcs_write32(TPR_THRESHOLD, 0);
2532 }
2533
2534 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2535 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002536 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537
Sheng Yang2384d2b2008-01-17 15:14:33 +08002538 if (vmx->vpid != 0)
2539 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2540
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002541 vmx->vcpu.arch.cr0 = 0x60000010;
2542 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002543 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002544 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002545 vmx_fpu_activate(&vmx->vcpu);
2546 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547
Sheng Yang2384d2b2008-01-17 15:14:33 +08002548 vpid_sync_vcpu_all(vmx);
2549
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002550 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002551
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002552 /* HACK: Don't enable emulation on guest boot/reset */
2553 vmx->emulation_required = 0;
2554
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002556 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557 return ret;
2558}
2559
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002560static void enable_irq_window(struct kvm_vcpu *vcpu)
2561{
2562 u32 cpu_based_vm_exec_control;
2563
2564 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2565 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2566 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2567}
2568
2569static void enable_nmi_window(struct kvm_vcpu *vcpu)
2570{
2571 u32 cpu_based_vm_exec_control;
2572
2573 if (!cpu_has_virtual_nmis()) {
2574 enable_irq_window(vcpu);
2575 return;
2576 }
2577
2578 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2579 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2580 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2581}
2582
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002583static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03002584{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002585 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002586 uint32_t intr;
2587 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02002588
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002589 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002590
Avi Kivityfa89a812008-09-01 15:57:51 +03002591 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002592 if (vmx->rmode.vm86_active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002593 vmx->rmode.irq.pending = true;
2594 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002595 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +03002596 if (vcpu->arch.interrupt.soft)
2597 vmx->rmode.irq.rip +=
2598 vmx->vcpu.arch.event_exit_inst_len;
Avi Kivity9c5623e2007-11-08 18:19:20 +02002599 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2600 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2601 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002602 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002603 return;
2604 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002605 intr = irq | INTR_INFO_VALID_MASK;
2606 if (vcpu->arch.interrupt.soft) {
2607 intr |= INTR_TYPE_SOFT_INTR;
2608 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2609 vmx->vcpu.arch.event_exit_inst_len);
2610 } else
2611 intr |= INTR_TYPE_EXT_INTR;
2612 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03002613}
2614
Sheng Yangf08864b2008-05-15 18:23:25 +08002615static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2616{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002617 struct vcpu_vmx *vmx = to_vmx(vcpu);
2618
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002619 if (!cpu_has_virtual_nmis()) {
2620 /*
2621 * Tracking the NMI-blocked state in software is built upon
2622 * finding the next open IRQ window. This, in turn, depends on
2623 * well-behaving guests: They have to keep IRQs disabled at
2624 * least as long as the NMI handler runs. Otherwise we may
2625 * cause NMI nesting, maybe breaking the guest. But as this is
2626 * highly unlikely, we can live with the residual risk.
2627 */
2628 vmx->soft_vnmi_blocked = 1;
2629 vmx->vnmi_blocked_time = 0;
2630 }
2631
Jan Kiszka487b3912008-09-26 09:30:56 +02002632 ++vcpu->stat.nmi_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002633 if (vmx->rmode.vm86_active) {
Jan Kiszka66a5a342008-09-26 09:30:51 +02002634 vmx->rmode.irq.pending = true;
2635 vmx->rmode.irq.vector = NMI_VECTOR;
2636 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2637 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2638 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2639 INTR_INFO_VALID_MASK);
2640 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2641 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2642 return;
2643 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002644 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2645 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002646}
2647
Gleb Natapovc4282df2009-04-21 17:45:07 +03002648static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02002649{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002650 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03002651 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002652
Gleb Natapovc4282df2009-04-21 17:45:07 +03002653 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2654 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS |
2655 GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02002656}
2657
Gleb Natapov78646122009-03-23 12:12:11 +02002658static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
2659{
Gleb Natapovc4282df2009-04-21 17:45:07 +03002660 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2661 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2662 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02002663}
2664
Izik Eiduscbc94022007-10-25 00:29:55 +02002665static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2666{
2667 int ret;
2668 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002669 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002670 .guest_phys_addr = addr,
2671 .memory_size = PAGE_SIZE * 3,
2672 .flags = 0,
2673 };
2674
2675 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2676 if (ret)
2677 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002678 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002679 return 0;
2680}
2681
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2683 int vec, u32 err_code)
2684{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002685 /*
2686 * Instruction with address size override prefix opcode 0x67
2687 * Cause the #SS fault with 0 error code in VM86 mode.
2688 */
2689 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Avi Kivity851ba692009-08-24 11:10:17 +03002690 if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002691 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002692 /*
2693 * Forward all other exceptions that are valid in real mode.
2694 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2695 * the required debugging infrastructure rework.
2696 */
2697 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002698 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002699 if (vcpu->guest_debug &
2700 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2701 return 0;
2702 kvm_queue_exception(vcpu, vec);
2703 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002704 case BP_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002705 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2706 return 0;
2707 /* fall through */
2708 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002709 case OF_VECTOR:
2710 case BR_VECTOR:
2711 case UD_VECTOR:
2712 case DF_VECTOR:
2713 case SS_VECTOR:
2714 case GP_VECTOR:
2715 case MF_VECTOR:
2716 kvm_queue_exception(vcpu, vec);
2717 return 1;
2718 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719 return 0;
2720}
2721
Andi Kleena0861c02009-06-08 17:37:09 +08002722/*
2723 * Trigger machine check on the host. We assume all the MSRs are already set up
2724 * by the CPU and that we still run on the same CPU as the MCE occurred on.
2725 * We pass a fake environment to the machine check handler because we want
2726 * the guest to be always treated like user space, no matter what context
2727 * it used internally.
2728 */
2729static void kvm_machine_check(void)
2730{
2731#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
2732 struct pt_regs regs = {
2733 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
2734 .flags = X86_EFLAGS_IF,
2735 };
2736
2737 do_machine_check(&regs, 0);
2738#endif
2739}
2740
Avi Kivity851ba692009-08-24 11:10:17 +03002741static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08002742{
2743 /* already handled by vcpu_run */
2744 return 1;
2745}
2746
Avi Kivity851ba692009-08-24 11:10:17 +03002747static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748{
Avi Kivity1155f762007-11-22 11:30:47 +02002749 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002750 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002751 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002752 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753 u32 vect_info;
2754 enum emulation_result er;
2755
Avi Kivity1155f762007-11-22 11:30:47 +02002756 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002757 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2758
Andi Kleena0861c02009-06-08 17:37:09 +08002759 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03002760 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08002761
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002763 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002765 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766
Jan Kiszkae4a41882008-09-26 09:30:46 +02002767 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002768 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002769
2770 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002771 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002772 return 1;
2773 }
2774
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002775 if (is_invalid_opcode(intr_info)) {
Avi Kivity851ba692009-08-24 11:10:17 +03002776 er = emulate_instruction(vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002777 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002778 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002779 return 1;
2780 }
2781
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002783 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002784 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2786 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002787 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02002788 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002789 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002791 trace_kvm_page_fault(cr2, error_code);
2792
Gleb Natapov3298b752009-05-11 13:35:46 +03002793 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03002794 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002795 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002796 }
2797
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002798 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002800 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002801 if (vcpu->arch.halt_request) {
2802 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002803 return kvm_emulate_halt(vcpu);
2804 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002805 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002806 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002807
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002808 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002809 switch (ex_no) {
2810 case DB_VECTOR:
2811 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2812 if (!(vcpu->guest_debug &
2813 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2814 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2815 kvm_queue_exception(vcpu, DB_VECTOR);
2816 return 1;
2817 }
2818 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2819 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2820 /* fall through */
2821 case BP_VECTOR:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002822 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002823 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2824 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002825 break;
2826 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002827 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2828 kvm_run->ex.exception = ex_no;
2829 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002830 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002832 return 0;
2833}
2834
Avi Kivity851ba692009-08-24 11:10:17 +03002835static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002837 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838 return 1;
2839}
2840
Avi Kivity851ba692009-08-24 11:10:17 +03002841static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08002842{
Avi Kivity851ba692009-08-24 11:10:17 +03002843 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08002844 return 0;
2845}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846
Avi Kivity851ba692009-08-24 11:10:17 +03002847static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848{
He, Qingbfdaab02007-09-12 14:18:28 +08002849 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01002850 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002851 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002852
Avi Kivity1165f5f2007-04-19 17:27:43 +03002853 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002854 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002855 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002856
2857 if (string) {
Avi Kivity851ba692009-08-24 11:10:17 +03002858 if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002859 return 0;
2860 return 1;
2861 }
2862
2863 size = (exit_qualification & 7) + 1;
2864 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002865 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002866
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002867 skip_emulated_instruction(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002868 return kvm_emulate_pio(vcpu, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869}
2870
Ingo Molnar102d8322007-02-19 14:37:47 +02002871static void
2872vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2873{
2874 /*
2875 * Patch in the VMCALL instruction:
2876 */
2877 hypercall[0] = 0x0f;
2878 hypercall[1] = 0x01;
2879 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002880}
2881
Avi Kivity851ba692009-08-24 11:10:17 +03002882static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002883{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002884 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885 int cr;
2886 int reg;
2887
He, Qingbfdaab02007-09-12 14:18:28 +08002888 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002889 cr = exit_qualification & 15;
2890 reg = (exit_qualification >> 8) & 15;
2891 switch ((exit_qualification >> 4) & 3) {
2892 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002893 val = kvm_register_read(vcpu, reg);
2894 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002895 switch (cr) {
2896 case 0:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002897 kvm_set_cr0(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002898 skip_emulated_instruction(vcpu);
2899 return 1;
2900 case 3:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002901 kvm_set_cr3(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902 skip_emulated_instruction(vcpu);
2903 return 1;
2904 case 4:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002905 kvm_set_cr4(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002906 skip_emulated_instruction(vcpu);
2907 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002908 case 8: {
2909 u8 cr8_prev = kvm_get_cr8(vcpu);
2910 u8 cr8 = kvm_register_read(vcpu, reg);
2911 kvm_set_cr8(vcpu, cr8);
2912 skip_emulated_instruction(vcpu);
2913 if (irqchip_in_kernel(vcpu->kvm))
2914 return 1;
2915 if (cr8_prev <= cr8)
2916 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03002917 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002918 return 0;
2919 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002920 };
2921 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002922 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002923 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002924 vcpu->arch.cr0 &= ~X86_CR0_TS;
2925 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002926 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002927 skip_emulated_instruction(vcpu);
2928 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002929 case 1: /*mov from cr*/
2930 switch (cr) {
2931 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002932 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002933 trace_kvm_cr_read(cr, vcpu->arch.cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002934 skip_emulated_instruction(vcpu);
2935 return 1;
2936 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002937 val = kvm_get_cr8(vcpu);
2938 kvm_register_write(vcpu, reg, val);
2939 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002940 skip_emulated_instruction(vcpu);
2941 return 1;
2942 }
2943 break;
2944 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002945 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002946
2947 skip_emulated_instruction(vcpu);
2948 return 1;
2949 default:
2950 break;
2951 }
Avi Kivity851ba692009-08-24 11:10:17 +03002952 vcpu->run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002953 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954 (int)(exit_qualification >> 4) & 3, cr);
2955 return 0;
2956}
2957
Avi Kivity851ba692009-08-24 11:10:17 +03002958static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959{
He, Qingbfdaab02007-09-12 14:18:28 +08002960 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002961 unsigned long val;
2962 int dr, reg;
2963
Avi Kivity0a79b002009-09-01 12:03:25 +03002964 if (!kvm_require_cpl(vcpu, 0))
2965 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002966 dr = vmcs_readl(GUEST_DR7);
2967 if (dr & DR7_GD) {
2968 /*
2969 * As the vm-exit takes precedence over the debug trap, we
2970 * need to emulate the latter, either for the host or the
2971 * guest debugging itself.
2972 */
2973 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03002974 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
2975 vcpu->run->debug.arch.dr7 = dr;
2976 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002977 vmcs_readl(GUEST_CS_BASE) +
2978 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03002979 vcpu->run->debug.arch.exception = DB_VECTOR;
2980 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002981 return 0;
2982 } else {
2983 vcpu->arch.dr7 &= ~DR7_GD;
2984 vcpu->arch.dr6 |= DR6_BD;
2985 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2986 kvm_queue_exception(vcpu, DB_VECTOR);
2987 return 1;
2988 }
2989 }
2990
He, Qingbfdaab02007-09-12 14:18:28 +08002991 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002992 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
2993 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
2994 if (exit_qualification & TYPE_MOV_FROM_DR) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002995 switch (dr) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002996 case 0 ... 3:
2997 val = vcpu->arch.db[dr];
2998 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002999 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003000 val = vcpu->arch.dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003001 break;
3002 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003003 val = vcpu->arch.dr7;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003004 break;
3005 default:
3006 val = 0;
3007 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003008 kvm_register_write(vcpu, reg, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003009 } else {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003010 val = vcpu->arch.regs[reg];
3011 switch (dr) {
3012 case 0 ... 3:
3013 vcpu->arch.db[dr] = val;
3014 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
3015 vcpu->arch.eff_db[dr] = val;
3016 break;
3017 case 4 ... 5:
3018 if (vcpu->arch.cr4 & X86_CR4_DE)
3019 kvm_queue_exception(vcpu, UD_VECTOR);
3020 break;
3021 case 6:
3022 if (val & 0xffffffff00000000ULL) {
3023 kvm_queue_exception(vcpu, GP_VECTOR);
3024 break;
3025 }
3026 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
3027 break;
3028 case 7:
3029 if (val & 0xffffffff00000000ULL) {
3030 kvm_queue_exception(vcpu, GP_VECTOR);
3031 break;
3032 }
3033 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
3034 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
3035 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3036 vcpu->arch.switch_db_regs =
3037 (val & DR7_BP_EN_MASK);
3038 }
3039 break;
3040 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003041 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003042 skip_emulated_instruction(vcpu);
3043 return 1;
3044}
3045
Avi Kivity851ba692009-08-24 11:10:17 +03003046static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047{
Avi Kivity06465c52007-02-28 20:46:53 +02003048 kvm_emulate_cpuid(vcpu);
3049 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050}
3051
Avi Kivity851ba692009-08-24 11:10:17 +03003052static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003053{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003054 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003055 u64 data;
3056
3057 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003058 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003059 return 1;
3060 }
3061
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003062 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003063
Avi Kivity6aa8b732006-12-10 02:21:36 -08003064 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003065 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3066 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003067 skip_emulated_instruction(vcpu);
3068 return 1;
3069}
3070
Avi Kivity851ba692009-08-24 11:10:17 +03003071static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003072{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003073 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3074 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3075 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003076
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003077 trace_kvm_msr_write(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003078
Avi Kivity6aa8b732006-12-10 02:21:36 -08003079 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003080 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003081 return 1;
3082 }
3083
3084 skip_emulated_instruction(vcpu);
3085 return 1;
3086}
3087
Avi Kivity851ba692009-08-24 11:10:17 +03003088static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003089{
3090 return 1;
3091}
3092
Avi Kivity851ba692009-08-24 11:10:17 +03003093static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003094{
Eddie Dong85f455f2007-07-06 12:20:49 +03003095 u32 cpu_based_vm_exec_control;
3096
3097 /* clear pending irq */
3098 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3099 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3100 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003101
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003102 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003103
Dor Laorc1150d82007-01-05 16:36:24 -08003104 /*
3105 * If the user space waits to inject interrupts, exit as soon as
3106 * possible
3107 */
Gleb Natapov80618232009-04-21 17:44:56 +03003108 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003109 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03003110 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003111 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003112 return 0;
3113 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003114 return 1;
3115}
3116
Avi Kivity851ba692009-08-24 11:10:17 +03003117static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003118{
3119 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003120 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003121}
3122
Avi Kivity851ba692009-08-24 11:10:17 +03003123static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02003124{
Dor Laor510043d2007-02-19 18:25:43 +02003125 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003126 kvm_emulate_hypercall(vcpu);
3127 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003128}
3129
Avi Kivity851ba692009-08-24 11:10:17 +03003130static int handle_vmx_insn(struct kvm_vcpu *vcpu)
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003131{
3132 kvm_queue_exception(vcpu, UD_VECTOR);
3133 return 1;
3134}
3135
Avi Kivity851ba692009-08-24 11:10:17 +03003136static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003137{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003138 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003139
3140 kvm_mmu_invlpg(vcpu, exit_qualification);
3141 skip_emulated_instruction(vcpu);
3142 return 1;
3143}
3144
Avi Kivity851ba692009-08-24 11:10:17 +03003145static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02003146{
3147 skip_emulated_instruction(vcpu);
3148 /* TODO: Add support for VT-d/pass-through device */
3149 return 1;
3150}
3151
Avi Kivity851ba692009-08-24 11:10:17 +03003152static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003153{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003154 unsigned long exit_qualification;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003155 enum emulation_result er;
3156 unsigned long offset;
3157
Sheng Yangf9c617f2009-03-25 10:08:52 +08003158 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003159 offset = exit_qualification & 0xffful;
3160
Avi Kivity851ba692009-08-24 11:10:17 +03003161 er = emulate_instruction(vcpu, 0, 0, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003162
3163 if (er != EMULATE_DONE) {
3164 printk(KERN_ERR
3165 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3166 offset);
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003167 return -ENOEXEC;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003168 }
3169 return 1;
3170}
3171
Avi Kivity851ba692009-08-24 11:10:17 +03003172static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02003173{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003174 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003175 unsigned long exit_qualification;
3176 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003177 int reason, type, idt_v;
3178
3179 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3180 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003181
3182 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3183
3184 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003185 if (reason == TASK_SWITCH_GATE && idt_v) {
3186 switch (type) {
3187 case INTR_TYPE_NMI_INTR:
3188 vcpu->arch.nmi_injected = false;
3189 if (cpu_has_virtual_nmis())
3190 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3191 GUEST_INTR_STATE_NMI);
3192 break;
3193 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003194 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003195 kvm_clear_interrupt_queue(vcpu);
3196 break;
3197 case INTR_TYPE_HARD_EXCEPTION:
3198 case INTR_TYPE_SOFT_EXCEPTION:
3199 kvm_clear_exception_queue(vcpu);
3200 break;
3201 default:
3202 break;
3203 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003204 }
Izik Eidus37817f22008-03-24 23:14:53 +02003205 tss_selector = exit_qualification;
3206
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003207 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3208 type != INTR_TYPE_EXT_INTR &&
3209 type != INTR_TYPE_NMI_INTR))
3210 skip_emulated_instruction(vcpu);
3211
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003212 if (!kvm_task_switch(vcpu, tss_selector, reason))
3213 return 0;
3214
3215 /* clear all local breakpoint enable flags */
3216 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3217
3218 /*
3219 * TODO: What about debug traps on tss switch?
3220 * Are we supposed to inject them and update dr6?
3221 */
3222
3223 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003224}
3225
Avi Kivity851ba692009-08-24 11:10:17 +03003226static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08003227{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003228 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003229 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003230 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003231
Sheng Yangf9c617f2009-03-25 10:08:52 +08003232 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003233
3234 if (exit_qualification & (1 << 6)) {
3235 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003236 return -EINVAL;
Sheng Yang14394422008-04-28 12:24:45 +08003237 }
3238
3239 gla_validity = (exit_qualification >> 7) & 0x3;
3240 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3241 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3242 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3243 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003244 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003245 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3246 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03003247 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3248 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03003249 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003250 }
3251
3252 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003253 trace_kvm_page_fault(gpa, exit_qualification);
Sheng Yang49cd7d22009-02-11 13:50:40 +08003254 return kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003255}
3256
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003257static u64 ept_rsvd_mask(u64 spte, int level)
3258{
3259 int i;
3260 u64 mask = 0;
3261
3262 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
3263 mask |= (1ULL << i);
3264
3265 if (level > 2)
3266 /* bits 7:3 reserved */
3267 mask |= 0xf8;
3268 else if (level == 2) {
3269 if (spte & (1ULL << 7))
3270 /* 2MB ref, bits 20:12 reserved */
3271 mask |= 0x1ff000;
3272 else
3273 /* bits 6:3 reserved */
3274 mask |= 0x78;
3275 }
3276
3277 return mask;
3278}
3279
3280static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
3281 int level)
3282{
3283 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
3284
3285 /* 010b (write-only) */
3286 WARN_ON((spte & 0x7) == 0x2);
3287
3288 /* 110b (write/execute) */
3289 WARN_ON((spte & 0x7) == 0x6);
3290
3291 /* 100b (execute-only) and value not supported by logical processor */
3292 if (!cpu_has_vmx_ept_execute_only())
3293 WARN_ON((spte & 0x7) == 0x4);
3294
3295 /* not 000b */
3296 if ((spte & 0x7)) {
3297 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
3298
3299 if (rsvd_bits != 0) {
3300 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
3301 __func__, rsvd_bits);
3302 WARN_ON(1);
3303 }
3304
3305 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
3306 u64 ept_mem_type = (spte & 0x38) >> 3;
3307
3308 if (ept_mem_type == 2 || ept_mem_type == 3 ||
3309 ept_mem_type == 7) {
3310 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
3311 __func__, ept_mem_type);
3312 WARN_ON(1);
3313 }
3314 }
3315 }
3316}
3317
Avi Kivity851ba692009-08-24 11:10:17 +03003318static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003319{
3320 u64 sptes[4];
3321 int nr_sptes, i;
3322 gpa_t gpa;
3323
3324 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3325
3326 printk(KERN_ERR "EPT: Misconfiguration.\n");
3327 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
3328
3329 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
3330
3331 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
3332 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
3333
Avi Kivity851ba692009-08-24 11:10:17 +03003334 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3335 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003336
3337 return 0;
3338}
3339
Avi Kivity851ba692009-08-24 11:10:17 +03003340static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08003341{
3342 u32 cpu_based_vm_exec_control;
3343
3344 /* clear pending NMI */
3345 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3346 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3347 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3348 ++vcpu->stat.nmi_window_exits;
3349
3350 return 1;
3351}
3352
Mohammed Gamal80ced182009-09-01 12:48:18 +02003353static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003354{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003355 struct vcpu_vmx *vmx = to_vmx(vcpu);
3356 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003357 int ret = 1;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003358
3359 while (!guest_state_valid(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003360 err = emulate_instruction(vcpu, 0, 0, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003361
Mohammed Gamal80ced182009-09-01 12:48:18 +02003362 if (err == EMULATE_DO_MMIO) {
3363 ret = 0;
3364 goto out;
3365 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003366
3367 if (err != EMULATE_DONE) {
3368 kvm_report_emulation_failure(vcpu, "emulation failure");
Mohammed Gamal80ced182009-09-01 12:48:18 +02003369 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3370 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3371 ret = 0;
3372 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003373 }
3374
3375 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02003376 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003377 if (need_resched())
3378 schedule();
3379 }
3380
Mohammed Gamal80ced182009-09-01 12:48:18 +02003381 vmx->emulation_required = 0;
3382out:
3383 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003384}
3385
Avi Kivity6aa8b732006-12-10 02:21:36 -08003386/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003387 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
3388 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
3389 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03003390static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003391{
3392 skip_emulated_instruction(vcpu);
3393 kvm_vcpu_on_spin(vcpu);
3394
3395 return 1;
3396}
3397
3398/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003399 * The exit handlers return 1 if the exit was handled fully and guest execution
3400 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3401 * to be done to userspace and return 0.
3402 */
Avi Kivity851ba692009-08-24 11:10:17 +03003403static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003404 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3405 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003406 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003407 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003408 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003409 [EXIT_REASON_CR_ACCESS] = handle_cr,
3410 [EXIT_REASON_DR_ACCESS] = handle_dr,
3411 [EXIT_REASON_CPUID] = handle_cpuid,
3412 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3413 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3414 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3415 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003416 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003417 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003418 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3419 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3420 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3421 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3422 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3423 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3424 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3425 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3426 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003427 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3428 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003429 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003430 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08003431 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003432 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
3433 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003434 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003435};
3436
3437static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003438 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003439
3440/*
3441 * The guest has exited. See if we can fix it or if we need userspace
3442 * assistance.
3443 */
Avi Kivity851ba692009-08-24 11:10:17 +03003444static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003445{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003446 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003447 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003448 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003449
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003450 trace_kvm_exit(exit_reason, kvm_rip_read(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003451
Mohammed Gamal80ced182009-09-01 12:48:18 +02003452 /* If guest state is invalid, start emulating */
3453 if (vmx->emulation_required && emulate_invalid_guest_state)
3454 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003455
Sheng Yang14394422008-04-28 12:24:45 +08003456 /* Access CR3 don't cause VMExit in paging mode, so we need
3457 * to sync with guest real CR3. */
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003458 if (enable_ept && is_paging(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08003459 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003460
Avi Kivity29bd8a72007-09-10 17:27:03 +03003461 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003462 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3463 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03003464 = vmcs_read32(VM_INSTRUCTION_ERROR);
3465 return 0;
3466 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003467
Mike Dayd77c26f2007-10-08 09:02:08 -04003468 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003469 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003470 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3471 exit_reason != EXIT_REASON_TASK_SWITCH))
3472 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3473 "(0x%x) and exit reason is 0x%x\n",
3474 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003475
3476 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03003477 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003478 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003479 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003480 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003481 /*
3482 * This CPU don't support us in finding the end of an
3483 * NMI-blocked window if the guest runs with IRQs
3484 * disabled. So we pull the trigger after 1 s of
3485 * futile waiting, but inform the user about this.
3486 */
3487 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3488 "state on VCPU %d after 1 s timeout\n",
3489 __func__, vcpu->vcpu_id);
3490 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003491 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003492 }
3493
Avi Kivity6aa8b732006-12-10 02:21:36 -08003494 if (exit_reason < kvm_vmx_max_exit_handlers
3495 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03003496 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003497 else {
Avi Kivity851ba692009-08-24 11:10:17 +03003498 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3499 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003500 }
3501 return 0;
3502}
3503
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003504static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003505{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003506 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003507 vmcs_write32(TPR_THRESHOLD, 0);
3508 return;
3509 }
3510
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003511 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003512}
3513
Avi Kivitycf393f72008-07-01 16:20:21 +03003514static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3515{
3516 u32 exit_intr_info;
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003517 u32 idt_vectoring_info = vmx->idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003518 bool unblock_nmi;
3519 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003520 int type;
3521 bool idtv_info_valid;
Avi Kivitycf393f72008-07-01 16:20:21 +03003522
3523 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Gleb Natapov20f65982009-05-11 13:35:55 +03003524
Andi Kleena0861c02009-06-08 17:37:09 +08003525 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
3526
3527 /* Handle machine checks before interrupts are enabled */
3528 if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
3529 || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
3530 && is_machine_check(exit_intr_info)))
3531 kvm_machine_check();
3532
Gleb Natapov20f65982009-05-11 13:35:55 +03003533 /* We need to handle NMIs before interrupts are enabled */
3534 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003535 (exit_intr_info & INTR_INFO_VALID_MASK))
Gleb Natapov20f65982009-05-11 13:35:55 +03003536 asm("int $2");
Gleb Natapov20f65982009-05-11 13:35:55 +03003537
3538 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3539
Avi Kivitycf393f72008-07-01 16:20:21 +03003540 if (cpu_has_virtual_nmis()) {
3541 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3542 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3543 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003544 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03003545 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3546 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003547 * SDM 3: 23.2.2 (September 2008)
3548 * Bit 12 is undefined in any of the following cases:
3549 * If the VM exit sets the valid bit in the IDT-vectoring
3550 * information field.
3551 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03003552 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003553 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
3554 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03003555 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3556 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003557 } else if (unlikely(vmx->soft_vnmi_blocked))
3558 vmx->vnmi_blocked_time +=
3559 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003560
Gleb Natapov37b96e92009-03-30 16:03:13 +03003561 vmx->vcpu.arch.nmi_injected = false;
3562 kvm_clear_exception_queue(&vmx->vcpu);
3563 kvm_clear_interrupt_queue(&vmx->vcpu);
3564
3565 if (!idtv_info_valid)
3566 return;
3567
Avi Kivity668f6122008-07-02 09:28:55 +03003568 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3569 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003570
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003571 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003572 case INTR_TYPE_NMI_INTR:
3573 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03003574 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003575 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03003576 * Clear bit "block by NMI" before VM entry if a NMI
3577 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03003578 */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003579 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3580 GUEST_INTR_STATE_NMI);
3581 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003582 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003583 vmx->vcpu.arch.event_exit_inst_len =
3584 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3585 /* fall through */
3586 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03003587 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003588 u32 err = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3589 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03003590 } else
3591 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003592 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003593 case INTR_TYPE_SOFT_INTR:
3594 vmx->vcpu.arch.event_exit_inst_len =
3595 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3596 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003597 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003598 kvm_queue_interrupt(&vmx->vcpu, vector,
3599 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003600 break;
3601 default:
3602 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03003603 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003604}
3605
Avi Kivity9c8cba32007-11-22 11:42:59 +02003606/*
3607 * Failure to inject an interrupt should give us the information
3608 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3609 * when fetching the interrupt redirection bitmap in the real-mode
3610 * tss, this doesn't happen. So we do it ourselves.
3611 */
3612static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3613{
3614 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003615 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003616 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003617 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003618 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3619 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3620 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3621 return;
3622 }
3623 vmx->idt_vectoring_info =
3624 VECTORING_INFO_VALID_MASK
3625 | INTR_TYPE_EXT_INTR
3626 | vmx->rmode.irq.vector;
3627}
3628
Avi Kivityc8019492008-07-14 14:44:59 +03003629#ifdef CONFIG_X86_64
3630#define R "r"
3631#define Q "q"
3632#else
3633#define R "e"
3634#define Q "l"
3635#endif
3636
Avi Kivity851ba692009-08-24 11:10:17 +03003637static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003638{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003639 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitye6adf282007-04-30 16:07:54 +03003640
Avi Kivity8f5d5492009-05-31 18:41:29 +03003641 if (enable_ept && is_paging(vcpu)) {
3642 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3643 ept_load_pdptrs(vcpu);
3644 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003645 /* Record the guest's net vcpu time for enforced NMI injections. */
3646 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3647 vmx->entry_time = ktime_get();
3648
Mohammed Gamal80ced182009-09-01 12:48:18 +02003649 /* Don't enter VMX if guest state is invalid, let the exit handler
3650 start emulation until we arrive back to a valid state */
3651 if (vmx->emulation_required && emulate_invalid_guest_state)
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003652 return;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003653
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003654 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3655 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3656 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3657 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3658
Gleb Natapov787ff732009-05-18 11:44:06 +03003659 /* When single-stepping over STI and MOV SS, we must clear the
3660 * corresponding interruptibility bits in the guest state. Otherwise
3661 * vmentry fails as it then expects bit 14 (BS) in pending debug
3662 * exceptions being set, but that's not correct for the guest debugging
3663 * case. */
3664 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3665 vmx_set_interrupt_shadow(vcpu, 0);
3666
Avi Kivitye6adf282007-04-30 16:07:54 +03003667 /*
3668 * Loading guest fpu may have cleared host cr0.ts
3669 */
3670 vmcs_writel(HOST_CR0, read_cr0());
3671
Avi Kivitye8a48342009-09-01 16:06:25 +03003672 if (vcpu->arch.switch_db_regs)
3673 set_debugreg(vcpu->arch.dr6, 6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003674
Mike Dayd77c26f2007-10-08 09:02:08 -04003675 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003676 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003677 "push %%"R"dx; push %%"R"bp;"
3678 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003679 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3680 "je 1f \n\t"
3681 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003682 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003683 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03003684 /* Reload cr2 if changed */
3685 "mov %c[cr2](%0), %%"R"ax \n\t"
3686 "mov %%cr2, %%"R"dx \n\t"
3687 "cmp %%"R"ax, %%"R"dx \n\t"
3688 "je 2f \n\t"
3689 "mov %%"R"ax, %%cr2 \n\t"
3690 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003691 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003692 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003693 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003694 "mov %c[rax](%0), %%"R"ax \n\t"
3695 "mov %c[rbx](%0), %%"R"bx \n\t"
3696 "mov %c[rdx](%0), %%"R"dx \n\t"
3697 "mov %c[rsi](%0), %%"R"si \n\t"
3698 "mov %c[rdi](%0), %%"R"di \n\t"
3699 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003700#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003701 "mov %c[r8](%0), %%r8 \n\t"
3702 "mov %c[r9](%0), %%r9 \n\t"
3703 "mov %c[r10](%0), %%r10 \n\t"
3704 "mov %c[r11](%0), %%r11 \n\t"
3705 "mov %c[r12](%0), %%r12 \n\t"
3706 "mov %c[r13](%0), %%r13 \n\t"
3707 "mov %c[r14](%0), %%r14 \n\t"
3708 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003709#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003710 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3711
Avi Kivity6aa8b732006-12-10 02:21:36 -08003712 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003713 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003714 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003715 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003716 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003717 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003718 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003719 "xchg %0, (%%"R"sp) \n\t"
3720 "mov %%"R"ax, %c[rax](%0) \n\t"
3721 "mov %%"R"bx, %c[rbx](%0) \n\t"
3722 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3723 "mov %%"R"dx, %c[rdx](%0) \n\t"
3724 "mov %%"R"si, %c[rsi](%0) \n\t"
3725 "mov %%"R"di, %c[rdi](%0) \n\t"
3726 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003727#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003728 "mov %%r8, %c[r8](%0) \n\t"
3729 "mov %%r9, %c[r9](%0) \n\t"
3730 "mov %%r10, %c[r10](%0) \n\t"
3731 "mov %%r11, %c[r11](%0) \n\t"
3732 "mov %%r12, %c[r12](%0) \n\t"
3733 "mov %%r13, %c[r13](%0) \n\t"
3734 "mov %%r14, %c[r14](%0) \n\t"
3735 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003736#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003737 "mov %%cr2, %%"R"ax \n\t"
3738 "mov %%"R"ax, %c[cr2](%0) \n\t"
3739
3740 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003741 "setbe %c[fail](%0) \n\t"
3742 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3743 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3744 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03003745 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003746 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3747 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3748 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3749 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3750 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3751 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3752 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003753#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003754 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3755 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3756 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3757 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3758 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3759 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3760 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3761 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003762#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003763 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003764 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003765 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003766#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003767 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3768#endif
3769 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003770
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003771 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
3772 | (1 << VCPU_EXREG_PDPTR));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003773 vcpu->arch.regs_dirty = 0;
3774
Avi Kivitye8a48342009-09-01 16:06:25 +03003775 if (vcpu->arch.switch_db_regs)
3776 get_debugreg(vcpu->arch.dr6, 6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003777
Avi Kivity1155f762007-11-22 11:30:47 +02003778 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003779 if (vmx->rmode.irq.pending)
3780 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003781
Mike Dayd77c26f2007-10-08 09:02:08 -04003782 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003783 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003784
Avi Kivitycf393f72008-07-01 16:20:21 +03003785 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003786}
3787
Avi Kivityc8019492008-07-14 14:44:59 +03003788#undef R
3789#undef Q
3790
Avi Kivity6aa8b732006-12-10 02:21:36 -08003791static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3792{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003793 struct vcpu_vmx *vmx = to_vmx(vcpu);
3794
3795 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003796 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003797 free_vmcs(vmx->vmcs);
3798 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003799 }
3800}
3801
3802static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3803{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003804 struct vcpu_vmx *vmx = to_vmx(vcpu);
3805
Sheng Yang2384d2b2008-01-17 15:14:33 +08003806 spin_lock(&vmx_vpid_lock);
3807 if (vmx->vpid != 0)
3808 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3809 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003810 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003811 kfree(vmx->host_msrs);
3812 kfree(vmx->guest_msrs);
3813 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003814 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003815}
3816
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003817static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003818{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003819 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003820 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003821 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003822
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003823 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003824 return ERR_PTR(-ENOMEM);
3825
Sheng Yang2384d2b2008-01-17 15:14:33 +08003826 allocate_vpid(vmx);
3827
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003828 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3829 if (err)
3830 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003831
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003832 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003833 if (!vmx->guest_msrs) {
3834 err = -ENOMEM;
3835 goto uninit_vcpu;
3836 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003837
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003838 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3839 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003840 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003841
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003842 vmx->vmcs = alloc_vmcs();
3843 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003844 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003845
3846 vmcs_clear(vmx->vmcs);
3847
Avi Kivity15ad7142007-07-11 18:17:21 +03003848 cpu = get_cpu();
3849 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003850 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003851 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003852 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003853 if (err)
3854 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003855 if (vm_need_virtualize_apic_accesses(kvm))
3856 if (alloc_apic_access_page(kvm) != 0)
3857 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003858
Sheng Yangb927a3c2009-07-21 10:42:48 +08003859 if (enable_ept) {
3860 if (!kvm->arch.ept_identity_map_addr)
3861 kvm->arch.ept_identity_map_addr =
3862 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003863 if (alloc_identity_pagetable(kvm) != 0)
3864 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003865 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003866
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003867 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003868
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003869free_vmcs:
3870 free_vmcs(vmx->vmcs);
3871free_msrs:
3872 kfree(vmx->host_msrs);
3873free_guest_msrs:
3874 kfree(vmx->guest_msrs);
3875uninit_vcpu:
3876 kvm_vcpu_uninit(&vmx->vcpu);
3877free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003878 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003879 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003880}
3881
Yang, Sheng002c7f72007-07-31 14:23:01 +03003882static void __init vmx_check_processor_compat(void *rtn)
3883{
3884 struct vmcs_config vmcs_conf;
3885
3886 *(int *)rtn = 0;
3887 if (setup_vmcs_config(&vmcs_conf) < 0)
3888 *(int *)rtn = -EIO;
3889 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3890 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3891 smp_processor_id());
3892 *(int *)rtn = -EIO;
3893 }
3894}
3895
Sheng Yang67253af2008-04-25 10:20:22 +08003896static int get_ept_level(void)
3897{
3898 return VMX_EPT_DEFAULT_GAW + 1;
3899}
3900
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003901static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003902{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003903 u64 ret;
3904
Sheng Yang522c68c2009-04-27 20:35:43 +08003905 /* For VT-d and EPT combination
3906 * 1. MMIO: always map as UC
3907 * 2. EPT with VT-d:
3908 * a. VT-d without snooping control feature: can't guarantee the
3909 * result, try to trust guest.
3910 * b. VT-d with snooping control feature: snooping control feature of
3911 * VT-d engine can guarantee the cache correctness. Just set it
3912 * to WB to keep consistent with host. So the same as item 3.
3913 * 3. EPT without VT-d: always map as WB and set IGMT=1 to keep
3914 * consistent with host MTRR
3915 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003916 if (is_mmio)
3917 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08003918 else if (vcpu->kvm->arch.iommu_domain &&
3919 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
3920 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
3921 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003922 else
Sheng Yang522c68c2009-04-27 20:35:43 +08003923 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
3924 | VMX_EPT_IGMT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003925
3926 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08003927}
3928
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003929static const struct trace_print_flags vmx_exit_reasons_str[] = {
3930 { EXIT_REASON_EXCEPTION_NMI, "exception" },
3931 { EXIT_REASON_EXTERNAL_INTERRUPT, "ext_irq" },
3932 { EXIT_REASON_TRIPLE_FAULT, "triple_fault" },
3933 { EXIT_REASON_NMI_WINDOW, "nmi_window" },
3934 { EXIT_REASON_IO_INSTRUCTION, "io_instruction" },
3935 { EXIT_REASON_CR_ACCESS, "cr_access" },
3936 { EXIT_REASON_DR_ACCESS, "dr_access" },
3937 { EXIT_REASON_CPUID, "cpuid" },
3938 { EXIT_REASON_MSR_READ, "rdmsr" },
3939 { EXIT_REASON_MSR_WRITE, "wrmsr" },
3940 { EXIT_REASON_PENDING_INTERRUPT, "interrupt_window" },
3941 { EXIT_REASON_HLT, "halt" },
3942 { EXIT_REASON_INVLPG, "invlpg" },
3943 { EXIT_REASON_VMCALL, "hypercall" },
3944 { EXIT_REASON_TPR_BELOW_THRESHOLD, "tpr_below_thres" },
3945 { EXIT_REASON_APIC_ACCESS, "apic_access" },
3946 { EXIT_REASON_WBINVD, "wbinvd" },
3947 { EXIT_REASON_TASK_SWITCH, "task_switch" },
3948 { EXIT_REASON_EPT_VIOLATION, "ept_violation" },
3949 { -1, NULL }
3950};
3951
Joerg Roedel344f4142009-07-27 16:30:48 +02003952static bool vmx_gb_page_enable(void)
3953{
3954 return false;
3955}
3956
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003957static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003958 .cpu_has_kvm_support = cpu_has_kvm_support,
3959 .disabled_by_bios = vmx_disabled_by_bios,
3960 .hardware_setup = hardware_setup,
3961 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003962 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003963 .hardware_enable = hardware_enable,
3964 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08003965 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003966
3967 .vcpu_create = vmx_create_vcpu,
3968 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003969 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003970
Avi Kivity04d2cc72007-09-10 18:10:54 +03003971 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003972 .vcpu_load = vmx_vcpu_load,
3973 .vcpu_put = vmx_vcpu_put,
3974
3975 .set_guest_debug = set_guest_debug,
3976 .get_msr = vmx_get_msr,
3977 .set_msr = vmx_set_msr,
3978 .get_segment_base = vmx_get_segment_base,
3979 .get_segment = vmx_get_segment,
3980 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003981 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003982 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003983 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003984 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003985 .set_cr3 = vmx_set_cr3,
3986 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003987 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003988 .get_idt = vmx_get_idt,
3989 .set_idt = vmx_set_idt,
3990 .get_gdt = vmx_get_gdt,
3991 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003992 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003993 .get_rflags = vmx_get_rflags,
3994 .set_rflags = vmx_set_rflags,
3995
3996 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003997
Avi Kivity6aa8b732006-12-10 02:21:36 -08003998 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02003999 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004000 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004001 .set_interrupt_shadow = vmx_set_interrupt_shadow,
4002 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004003 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004004 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004005 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004006 .queue_exception = vmx_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02004007 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004008 .nmi_allowed = vmx_nmi_allowed,
4009 .enable_nmi_window = enable_nmi_window,
4010 .enable_irq_window = enable_irq_window,
4011 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004012
Izik Eiduscbc94022007-10-25 00:29:55 +02004013 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004014 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004015 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004016
4017 .exit_reasons_str = vmx_exit_reasons_str,
Joerg Roedel344f4142009-07-27 16:30:48 +02004018 .gb_page_enable = vmx_gb_page_enable,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004019};
4020
4021static int __init vmx_init(void)
4022{
He, Qingfdef3ad2007-04-30 09:45:24 +03004023 int r;
4024
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004025 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004026 if (!vmx_io_bitmap_a)
4027 return -ENOMEM;
4028
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004029 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004030 if (!vmx_io_bitmap_b) {
4031 r = -ENOMEM;
4032 goto out;
4033 }
4034
Avi Kivity58972972009-02-24 22:26:47 +02004035 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
4036 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08004037 r = -ENOMEM;
4038 goto out1;
4039 }
4040
Avi Kivity58972972009-02-24 22:26:47 +02004041 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
4042 if (!vmx_msr_bitmap_longmode) {
4043 r = -ENOMEM;
4044 goto out2;
4045 }
4046
He, Qingfdef3ad2007-04-30 09:45:24 +03004047 /*
4048 * Allow direct access to the PC debug port (it is often used for I/O
4049 * delays, but the vmexits simply slow things down).
4050 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004051 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
4052 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004053
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004054 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004055
Avi Kivity58972972009-02-24 22:26:47 +02004056 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
4057 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08004058
Sheng Yang2384d2b2008-01-17 15:14:33 +08004059 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
4060
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004061 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004062 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02004063 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08004064
Avi Kivity58972972009-02-24 22:26:47 +02004065 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
4066 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
4067 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
4068 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
4069 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
4070 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03004071
Avi Kivity089d0342009-03-23 18:26:32 +02004072 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004073 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004074 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08004075 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08004076 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004077 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004078 kvm_enable_tdp();
4079 } else
4080 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08004081
Avi Kivityc7addb92007-09-16 18:58:32 +02004082 if (bypass_guest_pf)
4083 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
4084
He, Qingfdef3ad2007-04-30 09:45:24 +03004085 return 0;
4086
Avi Kivity58972972009-02-24 22:26:47 +02004087out3:
4088 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08004089out2:
Avi Kivity58972972009-02-24 22:26:47 +02004090 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03004091out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004092 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03004093out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004094 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004095 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004096}
4097
4098static void __exit vmx_exit(void)
4099{
Avi Kivity58972972009-02-24 22:26:47 +02004100 free_page((unsigned long)vmx_msr_bitmap_legacy);
4101 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004102 free_page((unsigned long)vmx_io_bitmap_b);
4103 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004104
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004105 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004106}
4107
4108module_init(vmx_init)
4109module_exit(vmx_exit)