blob: 284e905c59d317fccf5a1d181744b5ced4c0da1d [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
Avi Kivitycdc0e242009-12-06 17:21:14 +020064#define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \
65 (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD)
66#define KVM_GUEST_CR0_MASK \
67 (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
68#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \
69 (X86_CR0_WP | X86_CR0_NE | X86_CR0_TS | X86_CR0_MP)
70#define KVM_VM_CR0_ALWAYS_ON \
71 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
72#define KVM_GUEST_CR4_MASK \
73 (X86_CR4_VME | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_PGE | X86_CR4_VMXE)
74#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
75#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
76
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +080077/*
78 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
79 * ple_gap: upper bound on the amount of time between two successive
80 * executions of PAUSE in a loop. Also indicate if ple enabled.
81 * According to test, this time is usually small than 41 cycles.
82 * ple_window: upper bound on the amount of time a guest is allowed to execute
83 * in a PAUSE loop. Tests indicate that most spinlocks are held for
84 * less than 2^12 cycles
85 * Time is measured based on a counter that runs at the same rate as the TSC,
86 * refer SDM volume 3b section 21.6.13 & 22.1.3.
87 */
88#define KVM_VMX_DEFAULT_PLE_GAP 41
89#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
90static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
91module_param(ple_gap, int, S_IRUGO);
92
93static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
94module_param(ple_window, int, S_IRUGO);
95
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040096struct vmcs {
97 u32 revision_id;
98 u32 abort;
99 char data[0];
100};
101
Avi Kivity26bb0982009-09-07 11:14:12 +0300102struct shared_msr_entry {
103 unsigned index;
104 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200105 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300106};
107
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400108struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000109 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +0300110 struct list_head local_vcpus_link;
Avi Kivity313dbd42008-07-17 18:04:30 +0300111 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400112 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300113 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +0200114 u32 idt_vectoring_info;
Avi Kivity26bb0982009-09-07 11:14:12 +0300115 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400116 int nmsrs;
117 int save_nmsrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400118#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300119 u64 msr_host_kernel_gs_base;
120 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400121#endif
122 struct vmcs *vmcs;
123 struct {
124 int loaded;
125 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200126 int gs_ldt_reload_needed;
127 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400128 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200129 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300130 int vm86_active;
131 u8 save_iopl;
132 struct kvm_save_segment {
133 u16 selector;
134 unsigned long base;
135 u32 limit;
136 u32 ar;
137 } tr, es, ds, fs, gs;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200138 struct {
139 bool pending;
140 u8 vector;
141 unsigned rip;
142 } irq;
143 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800144 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300145 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200146
147 /* Support for vnmi-less CPUs */
148 int soft_vnmi_blocked;
149 ktime_t entry_time;
150 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800151 u32 exit_reason;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400152};
153
154static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
155{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000156 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400157}
158
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800159static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800160static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300161
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162static DEFINE_PER_CPU(struct vmcs *, vmxarea);
163static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300164static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800165
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200166static unsigned long *vmx_io_bitmap_a;
167static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200168static unsigned long *vmx_msr_bitmap_legacy;
169static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300170
Sheng Yang2384d2b2008-01-17 15:14:33 +0800171static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
172static DEFINE_SPINLOCK(vmx_vpid_lock);
173
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300174static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800175 int size;
176 int order;
177 u32 revision_id;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300178 u32 pin_based_exec_ctrl;
179 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800180 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300181 u32 vmexit_ctrl;
182 u32 vmentry_ctrl;
183} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184
Hannes Ederefff9e52008-11-28 17:02:06 +0100185static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800186 u32 ept;
187 u32 vpid;
188} vmx_capability;
189
Avi Kivity6aa8b732006-12-10 02:21:36 -0800190#define VMX_SEGMENT_FIELD(seg) \
191 [VCPU_SREG_##seg] = { \
192 .selector = GUEST_##seg##_SELECTOR, \
193 .base = GUEST_##seg##_BASE, \
194 .limit = GUEST_##seg##_LIMIT, \
195 .ar_bytes = GUEST_##seg##_AR_BYTES, \
196 }
197
198static struct kvm_vmx_segment_field {
199 unsigned selector;
200 unsigned base;
201 unsigned limit;
202 unsigned ar_bytes;
203} kvm_vmx_segment_fields[] = {
204 VMX_SEGMENT_FIELD(CS),
205 VMX_SEGMENT_FIELD(DS),
206 VMX_SEGMENT_FIELD(ES),
207 VMX_SEGMENT_FIELD(FS),
208 VMX_SEGMENT_FIELD(GS),
209 VMX_SEGMENT_FIELD(SS),
210 VMX_SEGMENT_FIELD(TR),
211 VMX_SEGMENT_FIELD(LDTR),
212};
213
Avi Kivity26bb0982009-09-07 11:14:12 +0300214static u64 host_efer;
215
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300216static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
217
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300218/*
219 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
220 * away by decrementing the array size.
221 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800223#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300224 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225#endif
226 MSR_EFER, MSR_K6_STAR,
227};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200228#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230static inline int is_page_fault(u32 intr_info)
231{
232 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
233 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100234 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800235}
236
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300237static inline int is_no_device(u32 intr_info)
238{
239 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
240 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100241 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300242}
243
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500244static inline int is_invalid_opcode(u32 intr_info)
245{
246 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
247 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100248 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500249}
250
Avi Kivity6aa8b732006-12-10 02:21:36 -0800251static inline int is_external_interrupt(u32 intr_info)
252{
253 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
254 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
255}
256
Andi Kleena0861c02009-06-08 17:37:09 +0800257static inline int is_machine_check(u32 intr_info)
258{
259 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
260 INTR_INFO_VALID_MASK)) ==
261 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
262}
263
Sheng Yang25c5f222008-03-28 13:18:56 +0800264static inline int cpu_has_vmx_msr_bitmap(void)
265{
Sheng Yang04547152009-04-01 15:52:31 +0800266 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800267}
268
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800269static inline int cpu_has_vmx_tpr_shadow(void)
270{
Sheng Yang04547152009-04-01 15:52:31 +0800271 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800272}
273
274static inline int vm_need_tpr_shadow(struct kvm *kvm)
275{
Sheng Yang04547152009-04-01 15:52:31 +0800276 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800277}
278
Sheng Yangf78e0e22007-10-29 09:40:42 +0800279static inline int cpu_has_secondary_exec_ctrls(void)
280{
Sheng Yang04547152009-04-01 15:52:31 +0800281 return vmcs_config.cpu_based_exec_ctrl &
282 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800283}
284
Avi Kivity774ead32007-12-26 13:57:04 +0200285static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800286{
Sheng Yang04547152009-04-01 15:52:31 +0800287 return vmcs_config.cpu_based_2nd_exec_ctrl &
288 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
289}
290
291static inline bool cpu_has_vmx_flexpriority(void)
292{
293 return cpu_has_vmx_tpr_shadow() &&
294 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800295}
296
Marcelo Tosattie7997942009-06-11 12:07:40 -0300297static inline bool cpu_has_vmx_ept_execute_only(void)
298{
299 return !!(vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT);
300}
301
302static inline bool cpu_has_vmx_eptp_uncacheable(void)
303{
304 return !!(vmx_capability.ept & VMX_EPTP_UC_BIT);
305}
306
307static inline bool cpu_has_vmx_eptp_writeback(void)
308{
309 return !!(vmx_capability.ept & VMX_EPTP_WB_BIT);
310}
311
312static inline bool cpu_has_vmx_ept_2m_page(void)
313{
314 return !!(vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT);
315}
316
Sheng Yangd56f5462008-04-25 10:13:16 +0800317static inline int cpu_has_vmx_invept_individual_addr(void)
318{
Sheng Yang04547152009-04-01 15:52:31 +0800319 return !!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800320}
321
322static inline int cpu_has_vmx_invept_context(void)
323{
Sheng Yang04547152009-04-01 15:52:31 +0800324 return !!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800325}
326
327static inline int cpu_has_vmx_invept_global(void)
328{
Sheng Yang04547152009-04-01 15:52:31 +0800329 return !!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800330}
331
332static inline int cpu_has_vmx_ept(void)
333{
Sheng Yang04547152009-04-01 15:52:31 +0800334 return vmcs_config.cpu_based_2nd_exec_ctrl &
335 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800336}
337
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700338static inline int cpu_has_vmx_unrestricted_guest(void)
339{
340 return vmcs_config.cpu_based_2nd_exec_ctrl &
341 SECONDARY_EXEC_UNRESTRICTED_GUEST;
342}
343
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800344static inline int cpu_has_vmx_ple(void)
345{
346 return vmcs_config.cpu_based_2nd_exec_ctrl &
347 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
348}
349
Sheng Yangf78e0e22007-10-29 09:40:42 +0800350static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
351{
Sheng Yang04547152009-04-01 15:52:31 +0800352 return flexpriority_enabled &&
353 (cpu_has_vmx_virtualize_apic_accesses()) &&
354 (irqchip_in_kernel(kvm));
Sheng Yangf78e0e22007-10-29 09:40:42 +0800355}
356
Sheng Yang2384d2b2008-01-17 15:14:33 +0800357static inline int cpu_has_vmx_vpid(void)
358{
Sheng Yang04547152009-04-01 15:52:31 +0800359 return vmcs_config.cpu_based_2nd_exec_ctrl &
360 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800361}
362
Sheng Yangf08864b2008-05-15 18:23:25 +0800363static inline int cpu_has_virtual_nmis(void)
364{
365 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
366}
367
Sheng Yang04547152009-04-01 15:52:31 +0800368static inline bool report_flexpriority(void)
369{
370 return flexpriority_enabled;
371}
372
Rusty Russell8b9cf982007-07-30 16:31:43 +1000373static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800374{
375 int i;
376
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400377 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300378 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300379 return i;
380 return -1;
381}
382
Sheng Yang2384d2b2008-01-17 15:14:33 +0800383static inline void __invvpid(int ext, u16 vpid, gva_t gva)
384{
385 struct {
386 u64 vpid : 16;
387 u64 rsvd : 48;
388 u64 gva;
389 } operand = { vpid, 0, gva };
390
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300391 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800392 /* CF==1 or ZF==1 --> rc = -1 */
393 "; ja 1f ; ud2 ; 1:"
394 : : "a"(&operand), "c"(ext) : "cc", "memory");
395}
396
Sheng Yang14394422008-04-28 12:24:45 +0800397static inline void __invept(int ext, u64 eptp, gpa_t gpa)
398{
399 struct {
400 u64 eptp, gpa;
401 } operand = {eptp, gpa};
402
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300403 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800404 /* CF==1 or ZF==1 --> rc = -1 */
405 "; ja 1f ; ud2 ; 1:\n"
406 : : "a" (&operand), "c" (ext) : "cc", "memory");
407}
408
Avi Kivity26bb0982009-09-07 11:14:12 +0300409static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300410{
411 int i;
412
Rusty Russell8b9cf982007-07-30 16:31:43 +1000413 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300414 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400415 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000416 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800417}
418
Avi Kivity6aa8b732006-12-10 02:21:36 -0800419static void vmcs_clear(struct vmcs *vmcs)
420{
421 u64 phys_addr = __pa(vmcs);
422 u8 error;
423
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300424 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800425 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
426 : "cc", "memory");
427 if (error)
428 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
429 vmcs, phys_addr);
430}
431
432static void __vcpu_clear(void *arg)
433{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000434 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800435 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436
Rusty Russell8b9cf982007-07-30 16:31:43 +1000437 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400438 vmcs_clear(vmx->vmcs);
439 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800441 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300442 list_del(&vmx->local_vcpus_link);
443 vmx->vcpu.cpu = -1;
444 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445}
446
Rusty Russell8b9cf982007-07-30 16:31:43 +1000447static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800448{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200449 if (vmx->vcpu.cpu == -1)
450 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200451 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800452}
453
Sheng Yang2384d2b2008-01-17 15:14:33 +0800454static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
455{
456 if (vmx->vpid == 0)
457 return;
458
459 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
460}
461
Sheng Yang14394422008-04-28 12:24:45 +0800462static inline void ept_sync_global(void)
463{
464 if (cpu_has_vmx_invept_global())
465 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
466}
467
468static inline void ept_sync_context(u64 eptp)
469{
Avi Kivity089d0342009-03-23 18:26:32 +0200470 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800471 if (cpu_has_vmx_invept_context())
472 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
473 else
474 ept_sync_global();
475 }
476}
477
478static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
479{
Avi Kivity089d0342009-03-23 18:26:32 +0200480 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800481 if (cpu_has_vmx_invept_individual_addr())
482 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
483 eptp, gpa);
484 else
485 ept_sync_context(eptp);
486 }
487}
488
Avi Kivity6aa8b732006-12-10 02:21:36 -0800489static unsigned long vmcs_readl(unsigned long field)
490{
491 unsigned long value;
492
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300493 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800494 : "=a"(value) : "d"(field) : "cc");
495 return value;
496}
497
498static u16 vmcs_read16(unsigned long field)
499{
500 return vmcs_readl(field);
501}
502
503static u32 vmcs_read32(unsigned long field)
504{
505 return vmcs_readl(field);
506}
507
508static u64 vmcs_read64(unsigned long field)
509{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800510#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800511 return vmcs_readl(field);
512#else
513 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
514#endif
515}
516
Avi Kivitye52de1b2007-01-05 16:36:56 -0800517static noinline void vmwrite_error(unsigned long field, unsigned long value)
518{
519 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
520 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
521 dump_stack();
522}
523
Avi Kivity6aa8b732006-12-10 02:21:36 -0800524static void vmcs_writel(unsigned long field, unsigned long value)
525{
526 u8 error;
527
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300528 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400529 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800530 if (unlikely(error))
531 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800532}
533
534static void vmcs_write16(unsigned long field, u16 value)
535{
536 vmcs_writel(field, value);
537}
538
539static void vmcs_write32(unsigned long field, u32 value)
540{
541 vmcs_writel(field, value);
542}
543
544static void vmcs_write64(unsigned long field, u64 value)
545{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800546 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300547#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800548 asm volatile ("");
549 vmcs_writel(field+1, value >> 32);
550#endif
551}
552
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300553static void vmcs_clear_bits(unsigned long field, u32 mask)
554{
555 vmcs_writel(field, vmcs_readl(field) & ~mask);
556}
557
558static void vmcs_set_bits(unsigned long field, u32 mask)
559{
560 vmcs_writel(field, vmcs_readl(field) | mask);
561}
562
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300563static void update_exception_bitmap(struct kvm_vcpu *vcpu)
564{
565 u32 eb;
566
Andi Kleena0861c02009-06-08 17:37:09 +0800567 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300568 if (!vcpu->fpu_active)
569 eb |= 1u << NM_VECTOR;
Avi Kivitye8a48342009-09-01 16:06:25 +0300570 /*
571 * Unconditionally intercept #DB so we can maintain dr6 without
572 * reading it every exit.
573 */
574 eb |= 1u << DB_VECTOR;
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100575 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100576 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
577 eb |= 1u << BP_VECTOR;
578 }
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300579 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300580 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200581 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800582 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300583 vmcs_write32(EXCEPTION_BITMAP, eb);
584}
585
Avi Kivity33ed6322007-05-02 16:54:03 +0300586static void reload_tss(void)
587{
Avi Kivity33ed6322007-05-02 16:54:03 +0300588 /*
589 * VT restores TR but not its size. Useless.
590 */
591 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200592 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300593
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300594 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300595 descs = (void *)gdt.base;
596 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
597 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300598}
599
Avi Kivity92c0d902009-10-29 11:00:16 +0200600static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +0300601{
Roel Kluin3a34a882009-08-04 02:08:45 -0700602 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300603 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300604
Avi Kivity26bb0982009-09-07 11:14:12 +0300605 guest_efer = vmx->vcpu.arch.shadow_efer;
Roel Kluin3a34a882009-08-04 02:08:45 -0700606
Avi Kivity51c6cf62007-08-29 03:48:05 +0300607 /*
608 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
609 * outside long mode
610 */
611 ignore_bits = EFER_NX | EFER_SCE;
612#ifdef CONFIG_X86_64
613 ignore_bits |= EFER_LMA | EFER_LME;
614 /* SCE is meaningful only in long mode on Intel */
615 if (guest_efer & EFER_LMA)
616 ignore_bits &= ~(u64)EFER_SCE;
617#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +0300618 guest_efer &= ~ignore_bits;
619 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +0300620 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +0200621 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +0300622 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300623}
624
Avi Kivity04d2cc72007-09-10 18:10:54 +0300625static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300626{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300627 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +0300628 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300629
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400630 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300631 return;
632
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400633 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300634 /*
635 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
636 * allow segment selectors with cpl > 0 or ti == 1.
637 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300638 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200639 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300640 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200641 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400642 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200643 vmx->host_state.fs_reload_needed = 0;
644 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300645 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200646 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300647 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300648 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400649 if (!(vmx->host_state.gs_sel & 7))
650 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300651 else {
652 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200653 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300654 }
655
656#ifdef CONFIG_X86_64
657 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
658 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
659#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400660 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
661 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300662#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300663
664#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300665 if (is_long_mode(&vmx->vcpu)) {
666 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
667 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
668 }
Avi Kivity707c0872007-05-02 17:33:43 +0300669#endif
Avi Kivity26bb0982009-09-07 11:14:12 +0300670 for (i = 0; i < vmx->save_nmsrs; ++i)
671 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +0200672 vmx->guest_msrs[i].data,
673 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +0300674}
675
Avi Kivitya9b21b62008-06-24 11:48:49 +0300676static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300677{
Avi Kivity15ad7142007-07-11 18:17:21 +0300678 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300679
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400680 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300681 return;
682
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200683 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400684 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200685 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300686 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200687 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300688 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300689 /*
690 * If we have to reload gs, we must take care to
691 * preserve our gs base.
692 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300693 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300694 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300695#ifdef CONFIG_X86_64
696 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
697#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300698 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300699 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200700 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +0300701#ifdef CONFIG_X86_64
702 if (is_long_mode(&vmx->vcpu)) {
703 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
704 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
705 }
706#endif
Avi Kivity33ed6322007-05-02 16:54:03 +0300707}
708
Avi Kivitya9b21b62008-06-24 11:48:49 +0300709static void vmx_load_host_state(struct vcpu_vmx *vmx)
710{
711 preempt_disable();
712 __vmx_load_host_state(vmx);
713 preempt_enable();
714}
715
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716/*
717 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
718 * vcpu mutex is already taken.
719 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300720static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400722 struct vcpu_vmx *vmx = to_vmx(vcpu);
723 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200724 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800725
Eddie Donga3d7f852007-09-03 16:15:12 +0300726 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000727 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300728 kvm_migrate_timers(vcpu);
Marcelo Tosattieb5109e2009-10-01 19:16:58 -0300729 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
Avi Kivity543e4242008-05-13 16:22:47 +0300730 local_irq_disable();
731 list_add(&vmx->local_vcpus_link,
732 &per_cpu(vcpus_on_cpu, cpu));
733 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300734 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400736 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737 u8 error;
738
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400739 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300740 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
742 : "cc");
743 if (error)
744 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400745 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746 }
747
748 if (vcpu->cpu != cpu) {
749 struct descriptor_table dt;
750 unsigned long sysenter_esp;
751
752 vcpu->cpu = cpu;
753 /*
754 * Linux uses per-cpu TSS and GDT, so set these when switching
755 * processors.
756 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300757 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
758 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800759 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
760
761 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
762 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300763
764 /*
765 * Make sure the time stamp counter is monotonous.
766 */
767 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200768 if (tsc_this < vcpu->arch.host_tsc) {
769 delta = vcpu->arch.host_tsc - tsc_this;
770 new_offset = vmcs_read64(TSC_OFFSET) + delta;
771 vmcs_write64(TSC_OFFSET, new_offset);
772 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774}
775
776static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
777{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300778 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779}
780
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300781static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
782{
783 if (vcpu->fpu_active)
784 return;
785 vcpu->fpu_active = 1;
Rusty Russell707d92f2007-07-17 23:19:08 +1000786 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800787 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92f2007-07-17 23:19:08 +1000788 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300789 update_exception_bitmap(vcpu);
790}
791
792static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
793{
794 if (!vcpu->fpu_active)
795 return;
796 vcpu->fpu_active = 0;
Rusty Russell707d92f2007-07-17 23:19:08 +1000797 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300798 update_exception_bitmap(vcpu);
799}
800
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
802{
Avi Kivity345dcaa2009-08-12 15:29:37 +0300803 unsigned long rflags;
804
805 rflags = vmcs_readl(GUEST_RFLAGS);
806 if (to_vmx(vcpu)->rmode.vm86_active)
807 rflags &= ~(unsigned long)(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
808 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800809}
810
811static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
812{
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300813 if (to_vmx(vcpu)->rmode.vm86_active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100814 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815 vmcs_writel(GUEST_RFLAGS, rflags);
816}
817
Glauber Costa2809f5d2009-05-12 16:21:05 -0400818static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
819{
820 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
821 int ret = 0;
822
823 if (interruptibility & GUEST_INTR_STATE_STI)
824 ret |= X86_SHADOW_INT_STI;
825 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
826 ret |= X86_SHADOW_INT_MOV_SS;
827
828 return ret & mask;
829}
830
831static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
832{
833 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
834 u32 interruptibility = interruptibility_old;
835
836 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
837
838 if (mask & X86_SHADOW_INT_MOV_SS)
839 interruptibility |= GUEST_INTR_STATE_MOV_SS;
840 if (mask & X86_SHADOW_INT_STI)
841 interruptibility |= GUEST_INTR_STATE_STI;
842
843 if ((interruptibility != interruptibility_old))
844 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
845}
846
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
848{
849 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300851 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300853 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800854
Glauber Costa2809f5d2009-05-12 16:21:05 -0400855 /* skipping an emulated instruction also counts */
856 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857}
858
Avi Kivity298101d2007-11-25 13:41:11 +0200859static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
860 bool has_error_code, u32 error_code)
861{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200862 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100863 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200864
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100865 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200866 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100867 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
868 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200869
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300870 if (vmx->rmode.vm86_active) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200871 vmx->rmode.irq.pending = true;
872 vmx->rmode.irq.vector = nr;
873 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +0300874 if (kvm_exception_is_soft(nr))
875 vmx->rmode.irq.rip +=
876 vmx->vcpu.arch.event_exit_inst_len;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100877 intr_info |= INTR_TYPE_SOFT_INTR;
878 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200879 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
880 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
881 return;
882 }
883
Gleb Natapov66fd3f72009-05-11 13:35:50 +0300884 if (kvm_exception_is_soft(nr)) {
885 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
886 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100887 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
888 } else
889 intr_info |= INTR_TYPE_HARD_EXCEPTION;
890
891 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200892}
893
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894/*
Eddie Donga75beee2007-05-17 18:55:15 +0300895 * Swap MSR entry in host/guest MSR entry array.
896 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000897static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300898{
Avi Kivity26bb0982009-09-07 11:14:12 +0300899 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400900
901 tmp = vmx->guest_msrs[to];
902 vmx->guest_msrs[to] = vmx->guest_msrs[from];
903 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300904}
905
906/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300907 * Set up the vmcs to automatically save and restore system
908 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
909 * mode, as fiddling with msrs is very expensive.
910 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000911static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300912{
Avi Kivity26bb0982009-09-07 11:14:12 +0300913 int save_nmsrs, index;
Avi Kivity58972972009-02-24 22:26:47 +0200914 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +0300915
Avi Kivity33f9c502008-02-27 16:06:57 +0200916 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300917 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300918#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000919 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000920 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300921 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000922 move_msr_up(vmx, index, save_nmsrs++);
923 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300924 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000925 move_msr_up(vmx, index, save_nmsrs++);
926 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300927 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000928 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300929 /*
930 * MSR_K6_STAR is only needed on long mode guests, and only
931 * if efer.sce is enabled.
932 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000933 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800934 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000935 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300936 }
Eddie Donga75beee2007-05-17 18:55:15 +0300937#endif
Avi Kivity92c0d902009-10-29 11:00:16 +0200938 index = __find_msr_index(vmx, MSR_EFER);
939 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +0300940 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300941
Avi Kivity26bb0982009-09-07 11:14:12 +0300942 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +0200943
944 if (cpu_has_vmx_msr_bitmap()) {
945 if (is_long_mode(&vmx->vcpu))
946 msr_bitmap = vmx_msr_bitmap_longmode;
947 else
948 msr_bitmap = vmx_msr_bitmap_legacy;
949
950 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
951 }
Avi Kivitye38aea32007-04-19 13:22:48 +0300952}
953
954/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955 * reads and returns guest's timestamp counter "register"
956 * guest_tsc = host_tsc + tsc_offset -- 21.3
957 */
958static u64 guest_read_tsc(void)
959{
960 u64 host_tsc, tsc_offset;
961
962 rdtscll(host_tsc);
963 tsc_offset = vmcs_read64(TSC_OFFSET);
964 return host_tsc + tsc_offset;
965}
966
967/*
968 * writes 'guest_tsc' into guest's timestamp counter "register"
969 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
970 */
Marcelo Tosatti53f658b2008-12-11 20:45:05 +0100971static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
974}
975
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976/*
977 * Reads an msr value (of 'msr_index') into 'pdata'.
978 * Returns 0 on success, non-0 otherwise.
979 * Assumes vcpu_load() was already called.
980 */
981static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
982{
983 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +0300984 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985
986 if (!pdata) {
987 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
988 return -EINVAL;
989 }
990
991 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800992#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 case MSR_FS_BASE:
994 data = vmcs_readl(GUEST_FS_BASE);
995 break;
996 case MSR_GS_BASE:
997 data = vmcs_readl(GUEST_GS_BASE);
998 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +0300999 case MSR_KERNEL_GS_BASE:
1000 vmx_load_host_state(to_vmx(vcpu));
1001 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
1002 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001003#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001005 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301006 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007 data = guest_read_tsc();
1008 break;
1009 case MSR_IA32_SYSENTER_CS:
1010 data = vmcs_read32(GUEST_SYSENTER_CS);
1011 break;
1012 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001013 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014 break;
1015 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001016 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018 default:
Avi Kivity26bb0982009-09-07 11:14:12 +03001019 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +10001020 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001021 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001022 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity3bab1f52006-12-29 16:49:48 -08001023 data = msr->data;
1024 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001026 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027 }
1028
1029 *pdata = data;
1030 return 0;
1031}
1032
1033/*
1034 * Writes msr value into into the appropriate "register".
1035 * Returns 0 on success, non-0 otherwise.
1036 * Assumes vcpu_load() was already called.
1037 */
1038static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1039{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001040 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001041 struct shared_msr_entry *msr;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01001042 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +03001043 int ret = 0;
1044
Avi Kivity6aa8b732006-12-10 02:21:36 -08001045 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001046 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001047 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001048 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001049 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001050#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051 case MSR_FS_BASE:
1052 vmcs_writel(GUEST_FS_BASE, data);
1053 break;
1054 case MSR_GS_BASE:
1055 vmcs_writel(GUEST_GS_BASE, data);
1056 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001057 case MSR_KERNEL_GS_BASE:
1058 vmx_load_host_state(vmx);
1059 vmx->msr_guest_kernel_gs_base = data;
1060 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061#endif
1062 case MSR_IA32_SYSENTER_CS:
1063 vmcs_write32(GUEST_SYSENTER_CS, data);
1064 break;
1065 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001066 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067 break;
1068 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001069 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001070 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301071 case MSR_IA32_TSC:
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01001072 rdtscll(host_tsc);
1073 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001075 case MSR_IA32_CR_PAT:
1076 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1077 vmcs_write64(GUEST_IA32_PAT, data);
1078 vcpu->arch.pat = data;
1079 break;
1080 }
1081 /* Otherwise falls through to kvm_set_msr_common */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001083 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001084 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001085 vmx_load_host_state(vmx);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001086 msr->data = data;
1087 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001089 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090 }
1091
Eddie Dong2cc51562007-05-21 07:28:09 +03001092 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093}
1094
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001095static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001097 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1098 switch (reg) {
1099 case VCPU_REGS_RSP:
1100 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1101 break;
1102 case VCPU_REGS_RIP:
1103 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1104 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001105 case VCPU_EXREG_PDPTR:
1106 if (enable_ept)
1107 ept_save_pdptrs(vcpu);
1108 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001109 default:
1110 break;
1111 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112}
1113
Jan Kiszka355be0b2009-10-03 00:31:21 +02001114static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115{
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001116 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1117 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1118 else
1119 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1120
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001121 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001122}
1123
1124static __init int cpu_has_kvm_support(void)
1125{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001126 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127}
1128
1129static __init int vmx_disabled_by_bios(void)
1130{
1131 u64 msr;
1132
1133 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001134 return (msr & (FEATURE_CONTROL_LOCKED |
1135 FEATURE_CONTROL_VMXON_ENABLED))
1136 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001137 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138}
1139
Alexander Graf10474ae2009-09-15 11:37:46 +02001140static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141{
1142 int cpu = raw_smp_processor_id();
1143 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1144 u64 old;
1145
Alexander Graf10474ae2009-09-15 11:37:46 +02001146 if (read_cr4() & X86_CR4_VMXE)
1147 return -EBUSY;
1148
Avi Kivity543e4242008-05-13 16:22:47 +03001149 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001151 if ((old & (FEATURE_CONTROL_LOCKED |
1152 FEATURE_CONTROL_VMXON_ENABLED))
1153 != (FEATURE_CONTROL_LOCKED |
1154 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001156 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001157 FEATURE_CONTROL_LOCKED |
1158 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001159 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001160 asm volatile (ASM_VMX_VMXON_RAX
1161 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001162 : "memory", "cc");
Alexander Graf10474ae2009-09-15 11:37:46 +02001163
1164 ept_sync_global();
1165
1166 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167}
1168
Avi Kivity543e4242008-05-13 16:22:47 +03001169static void vmclear_local_vcpus(void)
1170{
1171 int cpu = raw_smp_processor_id();
1172 struct vcpu_vmx *vmx, *n;
1173
1174 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1175 local_vcpus_link)
1176 __vcpu_clear(vmx);
1177}
1178
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001179
1180/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1181 * tricks.
1182 */
1183static void kvm_cpu_vmxoff(void)
1184{
1185 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1186 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1187}
1188
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189static void hardware_disable(void *garbage)
1190{
Avi Kivity543e4242008-05-13 16:22:47 +03001191 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001192 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001193}
1194
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001195static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001196 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001197{
1198 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001199 u32 ctl = ctl_min | ctl_opt;
1200
1201 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1202
1203 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1204 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1205
1206 /* Ensure minimum (required) set of control bits are supported. */
1207 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001208 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001209
1210 *result = ctl;
1211 return 0;
1212}
1213
Yang, Sheng002c7f72007-07-31 14:23:01 +03001214static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001215{
1216 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001217 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001218 u32 _pin_based_exec_control = 0;
1219 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001220 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001221 u32 _vmexit_control = 0;
1222 u32 _vmentry_control = 0;
1223
1224 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001225 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001226 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1227 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001228 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001229
1230 min = CPU_BASED_HLT_EXITING |
1231#ifdef CONFIG_X86_64
1232 CPU_BASED_CR8_LOAD_EXITING |
1233 CPU_BASED_CR8_STORE_EXITING |
1234#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001235 CPU_BASED_CR3_LOAD_EXITING |
1236 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001237 CPU_BASED_USE_IO_BITMAPS |
1238 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001239 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08001240 CPU_BASED_MWAIT_EXITING |
1241 CPU_BASED_MONITOR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001242 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001243 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001244 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001245 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001246 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1247 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001248 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001249#ifdef CONFIG_X86_64
1250 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1251 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1252 ~CPU_BASED_CR8_STORE_EXITING;
1253#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001254 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001255 min2 = 0;
1256 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001257 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001258 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001259 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001260 SECONDARY_EXEC_UNRESTRICTED_GUEST |
1261 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08001262 if (adjust_vmx_controls(min2, opt2,
1263 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001264 &_cpu_based_2nd_exec_control) < 0)
1265 return -EIO;
1266 }
1267#ifndef CONFIG_X86_64
1268 if (!(_cpu_based_2nd_exec_control &
1269 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1270 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1271#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001272 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001273 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1274 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03001275 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
1276 CPU_BASED_CR3_STORE_EXITING |
1277 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001278 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1279 vmx_capability.ept, vmx_capability.vpid);
1280 }
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001281
1282 min = 0;
1283#ifdef CONFIG_X86_64
1284 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1285#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001286 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001287 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1288 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001289 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001290
Sheng Yang468d4722008-10-09 16:01:55 +08001291 min = 0;
1292 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001293 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1294 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001295 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001297 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001298
1299 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1300 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001301 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001302
1303#ifdef CONFIG_X86_64
1304 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1305 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001306 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001307#endif
1308
1309 /* Require Write-Back (WB) memory type for VMCS accesses. */
1310 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001311 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001312
Yang, Sheng002c7f72007-07-31 14:23:01 +03001313 vmcs_conf->size = vmx_msr_high & 0x1fff;
1314 vmcs_conf->order = get_order(vmcs_config.size);
1315 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001316
Yang, Sheng002c7f72007-07-31 14:23:01 +03001317 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1318 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001319 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001320 vmcs_conf->vmexit_ctrl = _vmexit_control;
1321 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001322
1323 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001324}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325
1326static struct vmcs *alloc_vmcs_cpu(int cpu)
1327{
1328 int node = cpu_to_node(cpu);
1329 struct page *pages;
1330 struct vmcs *vmcs;
1331
Mel Gorman6484eb32009-06-16 15:31:54 -07001332 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333 if (!pages)
1334 return NULL;
1335 vmcs = page_address(pages);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001336 memset(vmcs, 0, vmcs_config.size);
1337 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338 return vmcs;
1339}
1340
1341static struct vmcs *alloc_vmcs(void)
1342{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001343 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344}
1345
1346static void free_vmcs(struct vmcs *vmcs)
1347{
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001348 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001349}
1350
Sam Ravnborg39959582007-06-01 00:47:13 -07001351static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352{
1353 int cpu;
1354
Zachary Amsden3230bb42009-09-29 11:38:37 -10001355 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10001357 per_cpu(vmxarea, cpu) = NULL;
1358 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359}
1360
Avi Kivity6aa8b732006-12-10 02:21:36 -08001361static __init int alloc_kvm_area(void)
1362{
1363 int cpu;
1364
Zachary Amsden3230bb42009-09-29 11:38:37 -10001365 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366 struct vmcs *vmcs;
1367
1368 vmcs = alloc_vmcs_cpu(cpu);
1369 if (!vmcs) {
1370 free_kvm_area();
1371 return -ENOMEM;
1372 }
1373
1374 per_cpu(vmxarea, cpu) = vmcs;
1375 }
1376 return 0;
1377}
1378
1379static __init int hardware_setup(void)
1380{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001381 if (setup_vmcs_config(&vmcs_config) < 0)
1382 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001383
1384 if (boot_cpu_has(X86_FEATURE_NX))
1385 kvm_enable_efer_bits(EFER_NX);
1386
Sheng Yang93ba03c2009-04-01 15:52:32 +08001387 if (!cpu_has_vmx_vpid())
1388 enable_vpid = 0;
1389
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001390 if (!cpu_has_vmx_ept()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001391 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001392 enable_unrestricted_guest = 0;
1393 }
1394
1395 if (!cpu_has_vmx_unrestricted_guest())
1396 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001397
1398 if (!cpu_has_vmx_flexpriority())
1399 flexpriority_enabled = 0;
1400
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001401 if (!cpu_has_vmx_tpr_shadow())
1402 kvm_x86_ops->update_cr8_intercept = NULL;
1403
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001404 if (enable_ept && !cpu_has_vmx_ept_2m_page())
1405 kvm_disable_largepages();
1406
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001407 if (!cpu_has_vmx_ple())
1408 ple_gap = 0;
1409
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410 return alloc_kvm_area();
1411}
1412
1413static __exit void hardware_unsetup(void)
1414{
1415 free_kvm_area();
1416}
1417
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1419{
1420 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1421
Avi Kivity6af11b92007-03-19 13:18:10 +02001422 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001423 vmcs_write16(sf->selector, save->selector);
1424 vmcs_writel(sf->base, save->base);
1425 vmcs_write32(sf->limit, save->limit);
1426 vmcs_write32(sf->ar_bytes, save->ar);
1427 } else {
1428 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1429 << AR_DPL_SHIFT;
1430 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1431 }
1432}
1433
1434static void enter_pmode(struct kvm_vcpu *vcpu)
1435{
1436 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001437 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001438
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001439 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001440 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001442 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
1443 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
1444 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001445
1446 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001447 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001448 flags |= (vmx->rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449 vmcs_writel(GUEST_RFLAGS, flags);
1450
Rusty Russell66aee912007-07-17 23:34:16 +10001451 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1452 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453
1454 update_exception_bitmap(vcpu);
1455
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001456 if (emulate_invalid_guest_state)
1457 return;
1458
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001459 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
1460 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
1461 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
1462 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001463
1464 vmcs_write16(GUEST_SS_SELECTOR, 0);
1465 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1466
1467 vmcs_write16(GUEST_CS_SELECTOR,
1468 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1469 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1470}
1471
Mike Dayd77c26f2007-10-08 09:02:08 -04001472static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001474 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001475 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1476 kvm->memslots[0].npages - 3;
1477 return base_gfn << PAGE_SHIFT;
1478 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001479 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480}
1481
1482static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1483{
1484 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1485
1486 save->selector = vmcs_read16(sf->selector);
1487 save->base = vmcs_readl(sf->base);
1488 save->limit = vmcs_read32(sf->limit);
1489 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001490 vmcs_write16(sf->selector, save->base >> 4);
1491 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001492 vmcs_write32(sf->limit, 0xffff);
1493 vmcs_write32(sf->ar_bytes, 0xf3);
1494}
1495
1496static void enter_rmode(struct kvm_vcpu *vcpu)
1497{
1498 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001499 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001500
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001501 if (enable_unrestricted_guest)
1502 return;
1503
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001504 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001505 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001507 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1509
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001510 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001511 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1512
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001513 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1515
1516 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001517 vmx->rmode.save_iopl
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001518 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001520 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521
1522 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001523 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524 update_exception_bitmap(vcpu);
1525
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001526 if (emulate_invalid_guest_state)
1527 goto continue_rmode;
1528
Avi Kivity6aa8b732006-12-10 02:21:36 -08001529 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1530 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1531 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1532
1533 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001534 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001535 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1536 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1538
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001539 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
1540 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
1541 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
1542 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001543
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001544continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001545 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001546 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001547}
1548
Amit Shah401d10d2009-02-20 22:53:37 +05301549static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1550{
1551 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001552 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1553
1554 if (!msr)
1555 return;
Amit Shah401d10d2009-02-20 22:53:37 +05301556
Avi Kivity44ea2b12009-09-06 15:55:37 +03001557 /*
1558 * Force kernel_gs_base reloading before EFER changes, as control
1559 * of this msr depends on is_long_mode().
1560 */
1561 vmx_load_host_state(to_vmx(vcpu));
Amit Shah401d10d2009-02-20 22:53:37 +05301562 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{
Avi Kivityfc78f512009-12-07 12:16:48 +02001618 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
1619
1620 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
1621 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08001622}
1623
Sheng Yang14394422008-04-28 12:24:45 +08001624static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1625{
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001626 if (!test_bit(VCPU_EXREG_PDPTR,
1627 (unsigned long *)&vcpu->arch.regs_dirty))
1628 return;
1629
Sheng Yang14394422008-04-28 12:24:45 +08001630 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Sheng Yang14394422008-04-28 12:24:45 +08001631 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1632 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1633 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1634 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1635 }
1636}
1637
Avi Kivity8f5d5492009-05-31 18:41:29 +03001638static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
1639{
1640 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1641 vcpu->arch.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
1642 vcpu->arch.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
1643 vcpu->arch.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
1644 vcpu->arch.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
1645 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001646
1647 __set_bit(VCPU_EXREG_PDPTR,
1648 (unsigned long *)&vcpu->arch.regs_avail);
1649 __set_bit(VCPU_EXREG_PDPTR,
1650 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03001651}
1652
Sheng Yang14394422008-04-28 12:24:45 +08001653static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1654
1655static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1656 unsigned long cr0,
1657 struct kvm_vcpu *vcpu)
1658{
1659 if (!(cr0 & X86_CR0_PG)) {
1660 /* From paging/starting to nonpaging */
1661 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001662 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001663 (CPU_BASED_CR3_LOAD_EXITING |
1664 CPU_BASED_CR3_STORE_EXITING));
1665 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02001666 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08001667 } else if (!is_paging(vcpu)) {
1668 /* From nonpaging to paging */
1669 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001670 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001671 ~(CPU_BASED_CR3_LOAD_EXITING |
1672 CPU_BASED_CR3_STORE_EXITING));
1673 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02001674 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08001675 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08001676
1677 if (!(cr0 & X86_CR0_WP))
1678 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08001679}
1680
1681static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1682 struct kvm_vcpu *vcpu)
1683{
1684 if (!is_paging(vcpu)) {
1685 *hw_cr4 &= ~X86_CR4_PAE;
1686 *hw_cr4 |= X86_CR4_PSE;
1687 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1688 *hw_cr4 &= ~X86_CR4_PAE;
1689}
1690
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1692{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001693 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001694 unsigned long hw_cr0;
1695
1696 if (enable_unrestricted_guest)
1697 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
1698 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
1699 else
1700 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08001701
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001702 vmx_fpu_deactivate(vcpu);
1703
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001704 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705 enter_pmode(vcpu);
1706
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001707 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001708 enter_rmode(vcpu);
1709
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001710#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001711 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001712 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001713 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10001714 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001715 exit_lmode(vcpu);
1716 }
1717#endif
1718
Avi Kivity089d0342009-03-23 18:26:32 +02001719 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001720 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1721
Avi Kivity6aa8b732006-12-10 02:21:36 -08001722 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001723 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001724 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001725
Rusty Russell707d92f2007-07-17 23:19:08 +10001726 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001727 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001728}
1729
Sheng Yang14394422008-04-28 12:24:45 +08001730static u64 construct_eptp(unsigned long root_hpa)
1731{
1732 u64 eptp;
1733
1734 /* TODO write the value reading from MSR */
1735 eptp = VMX_EPT_DEFAULT_MT |
1736 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1737 eptp |= (root_hpa & PAGE_MASK);
1738
1739 return eptp;
1740}
1741
Avi Kivity6aa8b732006-12-10 02:21:36 -08001742static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1743{
Sheng Yang14394422008-04-28 12:24:45 +08001744 unsigned long guest_cr3;
1745 u64 eptp;
1746
1747 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02001748 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001749 eptp = construct_eptp(cr3);
1750 vmcs_write64(EPT_POINTER, eptp);
Sheng Yang14394422008-04-28 12:24:45 +08001751 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
Sheng Yangb927a3c2009-07-21 10:42:48 +08001752 vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02001753 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001754 }
1755
Sheng Yang2384d2b2008-01-17 15:14:33 +08001756 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001757 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001758 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001759 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001760}
1761
1762static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1763{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001764 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08001765 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1766
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001767 vcpu->arch.cr4 = cr4;
Avi Kivity089d0342009-03-23 18:26:32 +02001768 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001769 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1770
1771 vmcs_writel(CR4_READ_SHADOW, cr4);
1772 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001773}
1774
Avi Kivity6aa8b732006-12-10 02:21:36 -08001775static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1776{
1777 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1778
1779 return vmcs_readl(sf->base);
1780}
1781
1782static void vmx_get_segment(struct kvm_vcpu *vcpu,
1783 struct kvm_segment *var, int seg)
1784{
1785 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1786 u32 ar;
1787
1788 var->base = vmcs_readl(sf->base);
1789 var->limit = vmcs_read32(sf->limit);
1790 var->selector = vmcs_read16(sf->selector);
1791 ar = vmcs_read32(sf->ar_bytes);
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02001792 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001793 ar = 0;
1794 var->type = ar & 15;
1795 var->s = (ar >> 4) & 1;
1796 var->dpl = (ar >> 5) & 3;
1797 var->present = (ar >> 7) & 1;
1798 var->avl = (ar >> 12) & 1;
1799 var->l = (ar >> 13) & 1;
1800 var->db = (ar >> 14) & 1;
1801 var->g = (ar >> 15) & 1;
1802 var->unusable = (ar >> 16) & 1;
1803}
1804
Izik Eidus2e4d2652008-03-24 19:38:34 +02001805static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1806{
Izik Eidus2e4d2652008-03-24 19:38:34 +02001807 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1808 return 0;
1809
1810 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1811 return 3;
1812
Avi Kivityeab4b8a2009-08-04 15:02:54 +03001813 return vmcs_read16(GUEST_CS_SELECTOR) & 3;
Izik Eidus2e4d2652008-03-24 19:38:34 +02001814}
1815
Avi Kivity653e3102007-05-07 10:55:37 +03001816static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001817{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001818 u32 ar;
1819
Avi Kivity653e3102007-05-07 10:55:37 +03001820 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001821 ar = 1 << 16;
1822 else {
1823 ar = var->type & 15;
1824 ar |= (var->s & 1) << 4;
1825 ar |= (var->dpl & 3) << 5;
1826 ar |= (var->present & 1) << 7;
1827 ar |= (var->avl & 1) << 12;
1828 ar |= (var->l & 1) << 13;
1829 ar |= (var->db & 1) << 14;
1830 ar |= (var->g & 1) << 15;
1831 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001832 if (ar == 0) /* a 0 value means unusable */
1833 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001834
1835 return ar;
1836}
1837
1838static void vmx_set_segment(struct kvm_vcpu *vcpu,
1839 struct kvm_segment *var, int seg)
1840{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001841 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03001842 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1843 u32 ar;
1844
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001845 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
1846 vmx->rmode.tr.selector = var->selector;
1847 vmx->rmode.tr.base = var->base;
1848 vmx->rmode.tr.limit = var->limit;
1849 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001850 return;
1851 }
1852 vmcs_writel(sf->base, var->base);
1853 vmcs_write32(sf->limit, var->limit);
1854 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001855 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001856 /*
1857 * Hack real-mode segments into vm86 compatibility.
1858 */
1859 if (var->base == 0xffff0000 && var->selector == 0xf000)
1860 vmcs_writel(sf->base, 0xf0000);
1861 ar = 0xf3;
1862 } else
1863 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001864
1865 /*
1866 * Fix the "Accessed" bit in AR field of segment registers for older
1867 * qemu binaries.
1868 * IA32 arch specifies that at the time of processor reset the
1869 * "Accessed" bit in the AR field of segment registers is 1. And qemu
1870 * is setting it to 0 in the usedland code. This causes invalid guest
1871 * state vmexit when "unrestricted guest" mode is turned on.
1872 * Fix for this setup issue in cpu_reset is being pushed in the qemu
1873 * tree. Newer qemu binaries with that qemu fix would not need this
1874 * kvm hack.
1875 */
1876 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
1877 ar |= 0x1; /* Accessed */
1878
Avi Kivity6aa8b732006-12-10 02:21:36 -08001879 vmcs_write32(sf->ar_bytes, ar);
1880}
1881
Avi Kivity6aa8b732006-12-10 02:21:36 -08001882static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1883{
1884 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1885
1886 *db = (ar >> 14) & 1;
1887 *l = (ar >> 13) & 1;
1888}
1889
1890static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1891{
1892 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1893 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1894}
1895
1896static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1897{
1898 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1899 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1900}
1901
1902static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1903{
1904 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1905 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1906}
1907
1908static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1909{
1910 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1911 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1912}
1913
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001914static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1915{
1916 struct kvm_segment var;
1917 u32 ar;
1918
1919 vmx_get_segment(vcpu, &var, seg);
1920 ar = vmx_segment_access_rights(&var);
1921
1922 if (var.base != (var.selector << 4))
1923 return false;
1924 if (var.limit != 0xffff)
1925 return false;
1926 if (ar != 0xf3)
1927 return false;
1928
1929 return true;
1930}
1931
1932static bool code_segment_valid(struct kvm_vcpu *vcpu)
1933{
1934 struct kvm_segment cs;
1935 unsigned int cs_rpl;
1936
1937 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1938 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1939
Avi Kivity1872a3f2009-01-04 23:26:52 +02001940 if (cs.unusable)
1941 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001942 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1943 return false;
1944 if (!cs.s)
1945 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001946 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001947 if (cs.dpl > cs_rpl)
1948 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001949 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001950 if (cs.dpl != cs_rpl)
1951 return false;
1952 }
1953 if (!cs.present)
1954 return false;
1955
1956 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1957 return true;
1958}
1959
1960static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1961{
1962 struct kvm_segment ss;
1963 unsigned int ss_rpl;
1964
1965 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1966 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1967
Avi Kivity1872a3f2009-01-04 23:26:52 +02001968 if (ss.unusable)
1969 return true;
1970 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001971 return false;
1972 if (!ss.s)
1973 return false;
1974 if (ss.dpl != ss_rpl) /* DPL != RPL */
1975 return false;
1976 if (!ss.present)
1977 return false;
1978
1979 return true;
1980}
1981
1982static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1983{
1984 struct kvm_segment var;
1985 unsigned int rpl;
1986
1987 vmx_get_segment(vcpu, &var, seg);
1988 rpl = var.selector & SELECTOR_RPL_MASK;
1989
Avi Kivity1872a3f2009-01-04 23:26:52 +02001990 if (var.unusable)
1991 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001992 if (!var.s)
1993 return false;
1994 if (!var.present)
1995 return false;
1996 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
1997 if (var.dpl < rpl) /* DPL < RPL */
1998 return false;
1999 }
2000
2001 /* TODO: Add other members to kvm_segment_field to allow checking for other access
2002 * rights flags
2003 */
2004 return true;
2005}
2006
2007static bool tr_valid(struct kvm_vcpu *vcpu)
2008{
2009 struct kvm_segment tr;
2010
2011 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
2012
Avi Kivity1872a3f2009-01-04 23:26:52 +02002013 if (tr.unusable)
2014 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002015 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2016 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002017 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002018 return false;
2019 if (!tr.present)
2020 return false;
2021
2022 return true;
2023}
2024
2025static bool ldtr_valid(struct kvm_vcpu *vcpu)
2026{
2027 struct kvm_segment ldtr;
2028
2029 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2030
Avi Kivity1872a3f2009-01-04 23:26:52 +02002031 if (ldtr.unusable)
2032 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002033 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2034 return false;
2035 if (ldtr.type != 2)
2036 return false;
2037 if (!ldtr.present)
2038 return false;
2039
2040 return true;
2041}
2042
2043static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2044{
2045 struct kvm_segment cs, ss;
2046
2047 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2048 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2049
2050 return ((cs.selector & SELECTOR_RPL_MASK) ==
2051 (ss.selector & SELECTOR_RPL_MASK));
2052}
2053
2054/*
2055 * Check if guest state is valid. Returns true if valid, false if
2056 * not.
2057 * We assume that registers are always usable
2058 */
2059static bool guest_state_valid(struct kvm_vcpu *vcpu)
2060{
2061 /* real mode guest state checks */
2062 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
2063 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2064 return false;
2065 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2066 return false;
2067 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2068 return false;
2069 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2070 return false;
2071 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2072 return false;
2073 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2074 return false;
2075 } else {
2076 /* protected mode guest state checks */
2077 if (!cs_ss_rpl_check(vcpu))
2078 return false;
2079 if (!code_segment_valid(vcpu))
2080 return false;
2081 if (!stack_segment_valid(vcpu))
2082 return false;
2083 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2084 return false;
2085 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2086 return false;
2087 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2088 return false;
2089 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2090 return false;
2091 if (!tr_valid(vcpu))
2092 return false;
2093 if (!ldtr_valid(vcpu))
2094 return false;
2095 }
2096 /* TODO:
2097 * - Add checks on RIP
2098 * - Add checks on RFLAGS
2099 */
2100
2101 return true;
2102}
2103
Mike Dayd77c26f2007-10-08 09:02:08 -04002104static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002105{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002107 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002108 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02002109 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110
Izik Eidus195aefd2007-10-01 22:14:18 +02002111 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2112 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002113 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002114 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002115 r = kvm_write_guest_page(kvm, fn++, &data,
2116 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002117 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 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2123 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002124 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002125 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002126 r = kvm_write_guest_page(kvm, fn, &data,
2127 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2128 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002129 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002130 goto out;
2131
2132 ret = 1;
2133out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002134 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135}
2136
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002137static int init_rmode_identity_map(struct kvm *kvm)
2138{
2139 int i, r, ret;
2140 pfn_t identity_map_pfn;
2141 u32 tmp;
2142
Avi Kivity089d0342009-03-23 18:26:32 +02002143 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002144 return 1;
2145 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2146 printk(KERN_ERR "EPT: identity-mapping pagetable "
2147 "haven't been allocated!\n");
2148 return 0;
2149 }
2150 if (likely(kvm->arch.ept_identity_pagetable_done))
2151 return 1;
2152 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002153 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002154 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2155 if (r < 0)
2156 goto out;
2157 /* Set up identity-mapping pagetable for EPT in real mode */
2158 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2159 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2160 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2161 r = kvm_write_guest_page(kvm, identity_map_pfn,
2162 &tmp, i * sizeof(tmp), sizeof(tmp));
2163 if (r < 0)
2164 goto out;
2165 }
2166 kvm->arch.ept_identity_pagetable_done = true;
2167 ret = 1;
2168out:
2169 return ret;
2170}
2171
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172static void seg_setup(int seg)
2173{
2174 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002175 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002176
2177 vmcs_write16(sf->selector, 0);
2178 vmcs_writel(sf->base, 0);
2179 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002180 if (enable_unrestricted_guest) {
2181 ar = 0x93;
2182 if (seg == VCPU_SREG_CS)
2183 ar |= 0x08; /* code segment */
2184 } else
2185 ar = 0xf3;
2186
2187 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002188}
2189
Sheng Yangf78e0e22007-10-29 09:40:42 +08002190static int alloc_apic_access_page(struct kvm *kvm)
2191{
2192 struct kvm_userspace_memory_region kvm_userspace_mem;
2193 int r = 0;
2194
Izik Eidus72dc67a2008-02-10 18:04:15 +02002195 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002196 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002197 goto out;
2198 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2199 kvm_userspace_mem.flags = 0;
2200 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2201 kvm_userspace_mem.memory_size = PAGE_SIZE;
2202 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2203 if (r)
2204 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002205
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002206 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002207out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002208 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002209 return r;
2210}
2211
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002212static int alloc_identity_pagetable(struct kvm *kvm)
2213{
2214 struct kvm_userspace_memory_region kvm_userspace_mem;
2215 int r = 0;
2216
2217 down_write(&kvm->slots_lock);
2218 if (kvm->arch.ept_identity_pagetable)
2219 goto out;
2220 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2221 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002222 kvm_userspace_mem.guest_phys_addr =
2223 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002224 kvm_userspace_mem.memory_size = PAGE_SIZE;
2225 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2226 if (r)
2227 goto out;
2228
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002229 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
Sheng Yangb927a3c2009-07-21 10:42:48 +08002230 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002231out:
2232 up_write(&kvm->slots_lock);
2233 return r;
2234}
2235
Sheng Yang2384d2b2008-01-17 15:14:33 +08002236static void allocate_vpid(struct vcpu_vmx *vmx)
2237{
2238 int vpid;
2239
2240 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002241 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002242 return;
2243 spin_lock(&vmx_vpid_lock);
2244 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2245 if (vpid < VMX_NR_VPIDS) {
2246 vmx->vpid = vpid;
2247 __set_bit(vpid, vmx_vpid_bitmap);
2248 }
2249 spin_unlock(&vmx_vpid_lock);
2250}
2251
Avi Kivity58972972009-02-24 22:26:47 +02002252static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002253{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002254 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002255
2256 if (!cpu_has_vmx_msr_bitmap())
2257 return;
2258
2259 /*
2260 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2261 * have the write-low and read-high bitmap offsets the wrong way round.
2262 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2263 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002264 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002265 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2266 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002267 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2268 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002269 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2270 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002271 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002272}
2273
Avi Kivity58972972009-02-24 22:26:47 +02002274static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2275{
2276 if (!longmode_only)
2277 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2278 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2279}
2280
Avi Kivity6aa8b732006-12-10 02:21:36 -08002281/*
2282 * Sets up the vmcs for emulated real mode.
2283 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002284static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285{
Sheng Yang468d4722008-10-09 16:01:55 +08002286 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002287 u32 junk;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002288 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289 unsigned long a;
2290 struct descriptor_table dt;
2291 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002292 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002293 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294
Avi Kivity6aa8b732006-12-10 02:21:36 -08002295 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002296 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2297 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298
Sheng Yang25c5f222008-03-28 13:18:56 +08002299 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002300 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002301
Avi Kivity6aa8b732006-12-10 02:21:36 -08002302 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2303
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304 /* Control */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002305 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2306 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002307
2308 exec_control = vmcs_config.cpu_based_exec_ctrl;
2309 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2310 exec_control &= ~CPU_BASED_TPR_SHADOW;
2311#ifdef CONFIG_X86_64
2312 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2313 CPU_BASED_CR8_LOAD_EXITING;
2314#endif
2315 }
Avi Kivity089d0342009-03-23 18:26:32 +02002316 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002317 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002318 CPU_BASED_CR3_LOAD_EXITING |
2319 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002320 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321
Sheng Yang83ff3b92007-11-21 14:33:25 +08002322 if (cpu_has_secondary_exec_ctrls()) {
2323 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2324 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2325 exec_control &=
2326 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002327 if (vmx->vpid == 0)
2328 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang046d8712009-11-27 16:46:26 +08002329 if (!enable_ept) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002330 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang046d8712009-11-27 16:46:26 +08002331 enable_unrestricted_guest = 0;
2332 }
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002333 if (!enable_unrestricted_guest)
2334 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002335 if (!ple_gap)
2336 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002337 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2338 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002339
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002340 if (ple_gap) {
2341 vmcs_write32(PLE_GAP, ple_gap);
2342 vmcs_write32(PLE_WINDOW, ple_window);
2343 }
2344
Avi Kivityc7addb92007-09-16 18:58:32 +02002345 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2346 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2348
2349 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2350 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2351 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2352
2353 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2354 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2355 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002356 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2357 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002358 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002359#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360 rdmsrl(MSR_FS_BASE, a);
2361 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2362 rdmsrl(MSR_GS_BASE, a);
2363 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2364#else
2365 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2366 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2367#endif
2368
2369 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2370
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002371 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2373
Mike Dayd77c26f2007-10-08 09:02:08 -04002374 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002375 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002376 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2377 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2378 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379
2380 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2381 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2382 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2383 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2384 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2385 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2386
Sheng Yang468d4722008-10-09 16:01:55 +08002387 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2388 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2389 host_pat = msr_low | ((u64) msr_high << 32);
2390 vmcs_write64(HOST_IA32_PAT, host_pat);
2391 }
2392 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2393 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2394 host_pat = msr_low | ((u64) msr_high << 32);
2395 /* Write the default value follow host pat */
2396 vmcs_write64(GUEST_IA32_PAT, host_pat);
2397 /* Keep arch.pat sync with GUEST_IA32_PAT */
2398 vmx->vcpu.arch.pat = host_pat;
2399 }
2400
Avi Kivity6aa8b732006-12-10 02:21:36 -08002401 for (i = 0; i < NR_VMX_MSR; ++i) {
2402 u32 index = vmx_msr_index[i];
2403 u32 data_low, data_high;
2404 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002405 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002406
2407 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2408 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002409 if (wrmsr_safe(index, data_low, data_high) < 0)
2410 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002411 data = data_low | ((u64)data_high << 32);
Avi Kivity26bb0982009-09-07 11:14:12 +03002412 vmx->guest_msrs[j].index = i;
2413 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02002414 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002415 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002416 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002417
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002418 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002419
2420 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002421 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2422
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002423 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2424 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
Avi Kivityfc78f512009-12-07 12:16:48 +02002425 vmx->vcpu.arch.cr4_guest_owned_bits = ~KVM_GUEST_CR4_MASK;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002426
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002427 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2428 rdtscll(tsc_this);
2429 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2430 tsc_base = tsc_this;
2431
2432 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002433
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002434 return 0;
2435}
2436
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002437static int init_rmode(struct kvm *kvm)
2438{
2439 if (!init_rmode_tss(kvm))
2440 return 0;
2441 if (!init_rmode_identity_map(kvm))
2442 return 0;
2443 return 1;
2444}
2445
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002446static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2447{
2448 struct vcpu_vmx *vmx = to_vmx(vcpu);
2449 u64 msr;
2450 int ret;
2451
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002452 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002453 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002454 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002455 ret = -ENOMEM;
2456 goto out;
2457 }
2458
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002459 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002460
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002461 vmx->soft_vnmi_blocked = 0;
2462
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002463 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002464 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002465 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002466 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002467 msr |= MSR_IA32_APICBASE_BSP;
2468 kvm_set_apic_base(&vmx->vcpu, msr);
2469
2470 fx_init(&vmx->vcpu);
2471
Avi Kivity5706be02008-08-20 15:07:31 +03002472 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002473 /*
2474 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2475 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2476 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002477 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002478 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2479 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2480 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002481 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2482 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002483 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002484
2485 seg_setup(VCPU_SREG_DS);
2486 seg_setup(VCPU_SREG_ES);
2487 seg_setup(VCPU_SREG_FS);
2488 seg_setup(VCPU_SREG_GS);
2489 seg_setup(VCPU_SREG_SS);
2490
2491 vmcs_write16(GUEST_TR_SELECTOR, 0);
2492 vmcs_writel(GUEST_TR_BASE, 0);
2493 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2494 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2495
2496 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2497 vmcs_writel(GUEST_LDTR_BASE, 0);
2498 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2499 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2500
2501 vmcs_write32(GUEST_SYSENTER_CS, 0);
2502 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2503 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2504
2505 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002506 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002507 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002508 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002509 kvm_rip_write(vcpu, 0);
2510 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002511
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002512 vmcs_writel(GUEST_DR7, 0x400);
2513
2514 vmcs_writel(GUEST_GDTR_BASE, 0);
2515 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2516
2517 vmcs_writel(GUEST_IDTR_BASE, 0);
2518 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2519
2520 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2521 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2522 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2523
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002524 /* Special registers */
2525 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2526
2527 setup_msrs(vmx);
2528
Avi Kivity6aa8b732006-12-10 02:21:36 -08002529 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2530
Sheng Yangf78e0e22007-10-29 09:40:42 +08002531 if (cpu_has_vmx_tpr_shadow()) {
2532 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2533 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2534 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002535 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002536 vmcs_write32(TPR_THRESHOLD, 0);
2537 }
2538
2539 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2540 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002541 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002542
Sheng Yang2384d2b2008-01-17 15:14:33 +08002543 if (vmx->vpid != 0)
2544 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2545
Eduardo Habkostfa400522009-10-24 02:49:58 -02002546 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002547 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002548 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002549 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002550 vmx_fpu_activate(&vmx->vcpu);
2551 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002552
Sheng Yang2384d2b2008-01-17 15:14:33 +08002553 vpid_sync_vcpu_all(vmx);
2554
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002555 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002556
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002557 /* HACK: Don't enable emulation on guest boot/reset */
2558 vmx->emulation_required = 0;
2559
Avi Kivity6aa8b732006-12-10 02:21:36 -08002560out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002561 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002562 return ret;
2563}
2564
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002565static void enable_irq_window(struct kvm_vcpu *vcpu)
2566{
2567 u32 cpu_based_vm_exec_control;
2568
2569 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2570 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2571 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2572}
2573
2574static void enable_nmi_window(struct kvm_vcpu *vcpu)
2575{
2576 u32 cpu_based_vm_exec_control;
2577
2578 if (!cpu_has_virtual_nmis()) {
2579 enable_irq_window(vcpu);
2580 return;
2581 }
2582
2583 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2584 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2585 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2586}
2587
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002588static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03002589{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002590 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002591 uint32_t intr;
2592 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02002593
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002594 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002595
Avi Kivityfa89a812008-09-01 15:57:51 +03002596 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002597 if (vmx->rmode.vm86_active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002598 vmx->rmode.irq.pending = true;
2599 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002600 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +03002601 if (vcpu->arch.interrupt.soft)
2602 vmx->rmode.irq.rip +=
2603 vmx->vcpu.arch.event_exit_inst_len;
Avi Kivity9c5623e2007-11-08 18:19:20 +02002604 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2605 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2606 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002607 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002608 return;
2609 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002610 intr = irq | INTR_INFO_VALID_MASK;
2611 if (vcpu->arch.interrupt.soft) {
2612 intr |= INTR_TYPE_SOFT_INTR;
2613 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2614 vmx->vcpu.arch.event_exit_inst_len);
2615 } else
2616 intr |= INTR_TYPE_EXT_INTR;
2617 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03002618}
2619
Sheng Yangf08864b2008-05-15 18:23:25 +08002620static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2621{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002622 struct vcpu_vmx *vmx = to_vmx(vcpu);
2623
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002624 if (!cpu_has_virtual_nmis()) {
2625 /*
2626 * Tracking the NMI-blocked state in software is built upon
2627 * finding the next open IRQ window. This, in turn, depends on
2628 * well-behaving guests: They have to keep IRQs disabled at
2629 * least as long as the NMI handler runs. Otherwise we may
2630 * cause NMI nesting, maybe breaking the guest. But as this is
2631 * highly unlikely, we can live with the residual risk.
2632 */
2633 vmx->soft_vnmi_blocked = 1;
2634 vmx->vnmi_blocked_time = 0;
2635 }
2636
Jan Kiszka487b3912008-09-26 09:30:56 +02002637 ++vcpu->stat.nmi_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002638 if (vmx->rmode.vm86_active) {
Jan Kiszka66a5a342008-09-26 09:30:51 +02002639 vmx->rmode.irq.pending = true;
2640 vmx->rmode.irq.vector = NMI_VECTOR;
2641 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2642 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2643 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2644 INTR_INFO_VALID_MASK);
2645 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2646 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2647 return;
2648 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002649 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2650 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002651}
2652
Gleb Natapovc4282df2009-04-21 17:45:07 +03002653static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02002654{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002655 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03002656 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002657
Gleb Natapovc4282df2009-04-21 17:45:07 +03002658 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2659 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS |
2660 GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02002661}
2662
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002663static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
2664{
2665 if (!cpu_has_virtual_nmis())
2666 return to_vmx(vcpu)->soft_vnmi_blocked;
2667 else
2668 return !!(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2669 GUEST_INTR_STATE_NMI);
2670}
2671
2672static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
2673{
2674 struct vcpu_vmx *vmx = to_vmx(vcpu);
2675
2676 if (!cpu_has_virtual_nmis()) {
2677 if (vmx->soft_vnmi_blocked != masked) {
2678 vmx->soft_vnmi_blocked = masked;
2679 vmx->vnmi_blocked_time = 0;
2680 }
2681 } else {
2682 if (masked)
2683 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
2684 GUEST_INTR_STATE_NMI);
2685 else
2686 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
2687 GUEST_INTR_STATE_NMI);
2688 }
2689}
2690
Gleb Natapov78646122009-03-23 12:12:11 +02002691static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
2692{
Gleb Natapovc4282df2009-04-21 17:45:07 +03002693 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2694 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2695 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02002696}
2697
Izik Eiduscbc94022007-10-25 00:29:55 +02002698static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2699{
2700 int ret;
2701 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002702 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002703 .guest_phys_addr = addr,
2704 .memory_size = PAGE_SIZE * 3,
2705 .flags = 0,
2706 };
2707
2708 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2709 if (ret)
2710 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002711 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002712 return 0;
2713}
2714
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2716 int vec, u32 err_code)
2717{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002718 /*
2719 * Instruction with address size override prefix opcode 0x67
2720 * Cause the #SS fault with 0 error code in VM86 mode.
2721 */
2722 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Avi Kivity851ba692009-08-24 11:10:17 +03002723 if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002725 /*
2726 * Forward all other exceptions that are valid in real mode.
2727 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2728 * the required debugging infrastructure rework.
2729 */
2730 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002731 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002732 if (vcpu->guest_debug &
2733 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2734 return 0;
2735 kvm_queue_exception(vcpu, vec);
2736 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002737 case BP_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002738 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2739 return 0;
2740 /* fall through */
2741 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002742 case OF_VECTOR:
2743 case BR_VECTOR:
2744 case UD_VECTOR:
2745 case DF_VECTOR:
2746 case SS_VECTOR:
2747 case GP_VECTOR:
2748 case MF_VECTOR:
2749 kvm_queue_exception(vcpu, vec);
2750 return 1;
2751 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002752 return 0;
2753}
2754
Andi Kleena0861c02009-06-08 17:37:09 +08002755/*
2756 * Trigger machine check on the host. We assume all the MSRs are already set up
2757 * by the CPU and that we still run on the same CPU as the MCE occurred on.
2758 * We pass a fake environment to the machine check handler because we want
2759 * the guest to be always treated like user space, no matter what context
2760 * it used internally.
2761 */
2762static void kvm_machine_check(void)
2763{
2764#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
2765 struct pt_regs regs = {
2766 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
2767 .flags = X86_EFLAGS_IF,
2768 };
2769
2770 do_machine_check(&regs, 0);
2771#endif
2772}
2773
Avi Kivity851ba692009-08-24 11:10:17 +03002774static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08002775{
2776 /* already handled by vcpu_run */
2777 return 1;
2778}
2779
Avi Kivity851ba692009-08-24 11:10:17 +03002780static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002781{
Avi Kivity1155f762007-11-22 11:30:47 +02002782 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002783 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002784 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002785 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786 u32 vect_info;
2787 enum emulation_result er;
2788
Avi Kivity1155f762007-11-22 11:30:47 +02002789 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2791
Andi Kleena0861c02009-06-08 17:37:09 +08002792 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03002793 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08002794
Avi Kivity6aa8b732006-12-10 02:21:36 -08002795 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Avi Kivity65ac7262009-11-04 11:59:01 +02002796 !is_page_fault(intr_info)) {
2797 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2798 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
2799 vcpu->run->internal.ndata = 2;
2800 vcpu->run->internal.data[0] = vect_info;
2801 vcpu->run->internal.data[1] = intr_info;
2802 return 0;
2803 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804
Jan Kiszkae4a41882008-09-26 09:30:46 +02002805 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002806 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002807
2808 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002809 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002810 return 1;
2811 }
2812
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002813 if (is_invalid_opcode(intr_info)) {
Avi Kivity851ba692009-08-24 11:10:17 +03002814 er = emulate_instruction(vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002815 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002816 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002817 return 1;
2818 }
2819
Avi Kivity6aa8b732006-12-10 02:21:36 -08002820 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002821 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002822 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2824 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002825 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02002826 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002827 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002829 trace_kvm_page_fault(cr2, error_code);
2830
Gleb Natapov3298b752009-05-11 13:35:46 +03002831 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03002832 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002833 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002834 }
2835
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002836 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002837 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002838 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002839 if (vcpu->arch.halt_request) {
2840 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002841 return kvm_emulate_halt(vcpu);
2842 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002844 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002846 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002847 switch (ex_no) {
2848 case DB_VECTOR:
2849 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2850 if (!(vcpu->guest_debug &
2851 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2852 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2853 kvm_queue_exception(vcpu, DB_VECTOR);
2854 return 1;
2855 }
2856 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2857 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2858 /* fall through */
2859 case BP_VECTOR:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002861 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2862 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002863 break;
2864 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002865 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2866 kvm_run->ex.exception = ex_no;
2867 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002868 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002870 return 0;
2871}
2872
Avi Kivity851ba692009-08-24 11:10:17 +03002873static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002875 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002876 return 1;
2877}
2878
Avi Kivity851ba692009-08-24 11:10:17 +03002879static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08002880{
Avi Kivity851ba692009-08-24 11:10:17 +03002881 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08002882 return 0;
2883}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002884
Avi Kivity851ba692009-08-24 11:10:17 +03002885static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886{
He, Qingbfdaab02007-09-12 14:18:28 +08002887 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01002888 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002889 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002890
Avi Kivity1165f5f2007-04-19 17:27:43 +03002891 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002892 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002893 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002894
2895 if (string) {
Avi Kivity851ba692009-08-24 11:10:17 +03002896 if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002897 return 0;
2898 return 1;
2899 }
2900
2901 size = (exit_qualification & 7) + 1;
2902 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002903 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002904
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002905 skip_emulated_instruction(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002906 return kvm_emulate_pio(vcpu, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907}
2908
Ingo Molnar102d8322007-02-19 14:37:47 +02002909static void
2910vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2911{
2912 /*
2913 * Patch in the VMCALL instruction:
2914 */
2915 hypercall[0] = 0x0f;
2916 hypercall[1] = 0x01;
2917 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002918}
2919
Avi Kivity851ba692009-08-24 11:10:17 +03002920static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002921{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002922 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002923 int cr;
2924 int reg;
2925
He, Qingbfdaab02007-09-12 14:18:28 +08002926 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002927 cr = exit_qualification & 15;
2928 reg = (exit_qualification >> 8) & 15;
2929 switch ((exit_qualification >> 4) & 3) {
2930 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002931 val = kvm_register_read(vcpu, reg);
2932 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933 switch (cr) {
2934 case 0:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002935 kvm_set_cr0(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002936 skip_emulated_instruction(vcpu);
2937 return 1;
2938 case 3:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002939 kvm_set_cr3(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002940 skip_emulated_instruction(vcpu);
2941 return 1;
2942 case 4:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002943 kvm_set_cr4(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002944 skip_emulated_instruction(vcpu);
2945 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002946 case 8: {
2947 u8 cr8_prev = kvm_get_cr8(vcpu);
2948 u8 cr8 = kvm_register_read(vcpu, reg);
2949 kvm_set_cr8(vcpu, cr8);
2950 skip_emulated_instruction(vcpu);
2951 if (irqchip_in_kernel(vcpu->kvm))
2952 return 1;
2953 if (cr8_prev <= cr8)
2954 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03002955 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002956 return 0;
2957 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002958 };
2959 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002960 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002961 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002962 vcpu->arch.cr0 &= ~X86_CR0_TS;
2963 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002964 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002965 skip_emulated_instruction(vcpu);
2966 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 case 1: /*mov from cr*/
2968 switch (cr) {
2969 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002970 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002971 trace_kvm_cr_read(cr, vcpu->arch.cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002972 skip_emulated_instruction(vcpu);
2973 return 1;
2974 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002975 val = kvm_get_cr8(vcpu);
2976 kvm_register_write(vcpu, reg, val);
2977 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002978 skip_emulated_instruction(vcpu);
2979 return 1;
2980 }
2981 break;
2982 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002983 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002984
2985 skip_emulated_instruction(vcpu);
2986 return 1;
2987 default:
2988 break;
2989 }
Avi Kivity851ba692009-08-24 11:10:17 +03002990 vcpu->run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002991 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002992 (int)(exit_qualification >> 4) & 3, cr);
2993 return 0;
2994}
2995
Avi Kivity851ba692009-08-24 11:10:17 +03002996static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002997{
He, Qingbfdaab02007-09-12 14:18:28 +08002998 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002999 unsigned long val;
3000 int dr, reg;
3001
Avi Kivity0a79b002009-09-01 12:03:25 +03003002 if (!kvm_require_cpl(vcpu, 0))
3003 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003004 dr = vmcs_readl(GUEST_DR7);
3005 if (dr & DR7_GD) {
3006 /*
3007 * As the vm-exit takes precedence over the debug trap, we
3008 * need to emulate the latter, either for the host or the
3009 * guest debugging itself.
3010 */
3011 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03003012 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
3013 vcpu->run->debug.arch.dr7 = dr;
3014 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003015 vmcs_readl(GUEST_CS_BASE) +
3016 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03003017 vcpu->run->debug.arch.exception = DB_VECTOR;
3018 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003019 return 0;
3020 } else {
3021 vcpu->arch.dr7 &= ~DR7_GD;
3022 vcpu->arch.dr6 |= DR6_BD;
3023 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3024 kvm_queue_exception(vcpu, DB_VECTOR);
3025 return 1;
3026 }
3027 }
3028
He, Qingbfdaab02007-09-12 14:18:28 +08003029 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003030 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
3031 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
3032 if (exit_qualification & TYPE_MOV_FROM_DR) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003033 switch (dr) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003034 case 0 ... 3:
3035 val = vcpu->arch.db[dr];
3036 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003037 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003038 val = vcpu->arch.dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003039 break;
3040 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003041 val = vcpu->arch.dr7;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003042 break;
3043 default:
3044 val = 0;
3045 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003046 kvm_register_write(vcpu, reg, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047 } else {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003048 val = vcpu->arch.regs[reg];
3049 switch (dr) {
3050 case 0 ... 3:
3051 vcpu->arch.db[dr] = val;
3052 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
3053 vcpu->arch.eff_db[dr] = val;
3054 break;
3055 case 4 ... 5:
Avi Kivityfc78f512009-12-07 12:16:48 +02003056 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003057 kvm_queue_exception(vcpu, UD_VECTOR);
3058 break;
3059 case 6:
3060 if (val & 0xffffffff00000000ULL) {
3061 kvm_queue_exception(vcpu, GP_VECTOR);
3062 break;
3063 }
3064 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
3065 break;
3066 case 7:
3067 if (val & 0xffffffff00000000ULL) {
3068 kvm_queue_exception(vcpu, GP_VECTOR);
3069 break;
3070 }
3071 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
3072 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
3073 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3074 vcpu->arch.switch_db_regs =
3075 (val & DR7_BP_EN_MASK);
3076 }
3077 break;
3078 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003079 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003080 skip_emulated_instruction(vcpu);
3081 return 1;
3082}
3083
Avi Kivity851ba692009-08-24 11:10:17 +03003084static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003085{
Avi Kivity06465c52007-02-28 20:46:53 +02003086 kvm_emulate_cpuid(vcpu);
3087 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003088}
3089
Avi Kivity851ba692009-08-24 11:10:17 +03003090static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003091{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003092 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003093 u64 data;
3094
3095 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003096 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003097 return 1;
3098 }
3099
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003100 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003101
Avi Kivity6aa8b732006-12-10 02:21:36 -08003102 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003103 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3104 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003105 skip_emulated_instruction(vcpu);
3106 return 1;
3107}
3108
Avi Kivity851ba692009-08-24 11:10:17 +03003109static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003110{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003111 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3112 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3113 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003114
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003115 trace_kvm_msr_write(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003116
Avi Kivity6aa8b732006-12-10 02:21:36 -08003117 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003118 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003119 return 1;
3120 }
3121
3122 skip_emulated_instruction(vcpu);
3123 return 1;
3124}
3125
Avi Kivity851ba692009-08-24 11:10:17 +03003126static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003127{
3128 return 1;
3129}
3130
Avi Kivity851ba692009-08-24 11:10:17 +03003131static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003132{
Eddie Dong85f455f2007-07-06 12:20:49 +03003133 u32 cpu_based_vm_exec_control;
3134
3135 /* clear pending irq */
3136 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3137 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3138 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003139
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003140 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003141
Dor Laorc1150d82007-01-05 16:36:24 -08003142 /*
3143 * If the user space waits to inject interrupts, exit as soon as
3144 * possible
3145 */
Gleb Natapov80618232009-04-21 17:44:56 +03003146 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003147 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03003148 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003149 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003150 return 0;
3151 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003152 return 1;
3153}
3154
Avi Kivity851ba692009-08-24 11:10:17 +03003155static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156{
3157 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003158 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003159}
3160
Avi Kivity851ba692009-08-24 11:10:17 +03003161static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02003162{
Dor Laor510043d2007-02-19 18:25:43 +02003163 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003164 kvm_emulate_hypercall(vcpu);
3165 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003166}
3167
Avi Kivity851ba692009-08-24 11:10:17 +03003168static int handle_vmx_insn(struct kvm_vcpu *vcpu)
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003169{
3170 kvm_queue_exception(vcpu, UD_VECTOR);
3171 return 1;
3172}
3173
Avi Kivity851ba692009-08-24 11:10:17 +03003174static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003175{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003176 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003177
3178 kvm_mmu_invlpg(vcpu, exit_qualification);
3179 skip_emulated_instruction(vcpu);
3180 return 1;
3181}
3182
Avi Kivity851ba692009-08-24 11:10:17 +03003183static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02003184{
3185 skip_emulated_instruction(vcpu);
3186 /* TODO: Add support for VT-d/pass-through device */
3187 return 1;
3188}
3189
Avi Kivity851ba692009-08-24 11:10:17 +03003190static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003191{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003192 unsigned long exit_qualification;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003193 enum emulation_result er;
3194 unsigned long offset;
3195
Sheng Yangf9c617f2009-03-25 10:08:52 +08003196 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003197 offset = exit_qualification & 0xffful;
3198
Avi Kivity851ba692009-08-24 11:10:17 +03003199 er = emulate_instruction(vcpu, 0, 0, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003200
3201 if (er != EMULATE_DONE) {
3202 printk(KERN_ERR
3203 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3204 offset);
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003205 return -ENOEXEC;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003206 }
3207 return 1;
3208}
3209
Avi Kivity851ba692009-08-24 11:10:17 +03003210static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02003211{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003212 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003213 unsigned long exit_qualification;
3214 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003215 int reason, type, idt_v;
3216
3217 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3218 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003219
3220 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3221
3222 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003223 if (reason == TASK_SWITCH_GATE && idt_v) {
3224 switch (type) {
3225 case INTR_TYPE_NMI_INTR:
3226 vcpu->arch.nmi_injected = false;
3227 if (cpu_has_virtual_nmis())
3228 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3229 GUEST_INTR_STATE_NMI);
3230 break;
3231 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003232 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003233 kvm_clear_interrupt_queue(vcpu);
3234 break;
3235 case INTR_TYPE_HARD_EXCEPTION:
3236 case INTR_TYPE_SOFT_EXCEPTION:
3237 kvm_clear_exception_queue(vcpu);
3238 break;
3239 default:
3240 break;
3241 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003242 }
Izik Eidus37817f22008-03-24 23:14:53 +02003243 tss_selector = exit_qualification;
3244
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003245 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3246 type != INTR_TYPE_EXT_INTR &&
3247 type != INTR_TYPE_NMI_INTR))
3248 skip_emulated_instruction(vcpu);
3249
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003250 if (!kvm_task_switch(vcpu, tss_selector, reason))
3251 return 0;
3252
3253 /* clear all local breakpoint enable flags */
3254 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3255
3256 /*
3257 * TODO: What about debug traps on tss switch?
3258 * Are we supposed to inject them and update dr6?
3259 */
3260
3261 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003262}
3263
Avi Kivity851ba692009-08-24 11:10:17 +03003264static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08003265{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003266 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003267 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003268 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003269
Sheng Yangf9c617f2009-03-25 10:08:52 +08003270 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003271
3272 if (exit_qualification & (1 << 6)) {
3273 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003274 return -EINVAL;
Sheng Yang14394422008-04-28 12:24:45 +08003275 }
3276
3277 gla_validity = (exit_qualification >> 7) & 0x3;
3278 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3279 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3280 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3281 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003282 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003283 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3284 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03003285 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3286 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03003287 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003288 }
3289
3290 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003291 trace_kvm_page_fault(gpa, exit_qualification);
Sheng Yang49cd7d22009-02-11 13:50:40 +08003292 return kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003293}
3294
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003295static u64 ept_rsvd_mask(u64 spte, int level)
3296{
3297 int i;
3298 u64 mask = 0;
3299
3300 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
3301 mask |= (1ULL << i);
3302
3303 if (level > 2)
3304 /* bits 7:3 reserved */
3305 mask |= 0xf8;
3306 else if (level == 2) {
3307 if (spte & (1ULL << 7))
3308 /* 2MB ref, bits 20:12 reserved */
3309 mask |= 0x1ff000;
3310 else
3311 /* bits 6:3 reserved */
3312 mask |= 0x78;
3313 }
3314
3315 return mask;
3316}
3317
3318static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
3319 int level)
3320{
3321 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
3322
3323 /* 010b (write-only) */
3324 WARN_ON((spte & 0x7) == 0x2);
3325
3326 /* 110b (write/execute) */
3327 WARN_ON((spte & 0x7) == 0x6);
3328
3329 /* 100b (execute-only) and value not supported by logical processor */
3330 if (!cpu_has_vmx_ept_execute_only())
3331 WARN_ON((spte & 0x7) == 0x4);
3332
3333 /* not 000b */
3334 if ((spte & 0x7)) {
3335 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
3336
3337 if (rsvd_bits != 0) {
3338 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
3339 __func__, rsvd_bits);
3340 WARN_ON(1);
3341 }
3342
3343 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
3344 u64 ept_mem_type = (spte & 0x38) >> 3;
3345
3346 if (ept_mem_type == 2 || ept_mem_type == 3 ||
3347 ept_mem_type == 7) {
3348 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
3349 __func__, ept_mem_type);
3350 WARN_ON(1);
3351 }
3352 }
3353 }
3354}
3355
Avi Kivity851ba692009-08-24 11:10:17 +03003356static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003357{
3358 u64 sptes[4];
3359 int nr_sptes, i;
3360 gpa_t gpa;
3361
3362 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3363
3364 printk(KERN_ERR "EPT: Misconfiguration.\n");
3365 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
3366
3367 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
3368
3369 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
3370 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
3371
Avi Kivity851ba692009-08-24 11:10:17 +03003372 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3373 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003374
3375 return 0;
3376}
3377
Avi Kivity851ba692009-08-24 11:10:17 +03003378static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08003379{
3380 u32 cpu_based_vm_exec_control;
3381
3382 /* clear pending NMI */
3383 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3384 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3385 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3386 ++vcpu->stat.nmi_window_exits;
3387
3388 return 1;
3389}
3390
Mohammed Gamal80ced182009-09-01 12:48:18 +02003391static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003392{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003393 struct vcpu_vmx *vmx = to_vmx(vcpu);
3394 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003395 int ret = 1;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003396
3397 while (!guest_state_valid(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003398 err = emulate_instruction(vcpu, 0, 0, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003399
Mohammed Gamal80ced182009-09-01 12:48:18 +02003400 if (err == EMULATE_DO_MMIO) {
3401 ret = 0;
3402 goto out;
3403 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003404
3405 if (err != EMULATE_DONE) {
3406 kvm_report_emulation_failure(vcpu, "emulation failure");
Mohammed Gamal80ced182009-09-01 12:48:18 +02003407 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3408 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
Avi Kivitya9c73992009-11-04 11:54:59 +02003409 vcpu->run->internal.ndata = 0;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003410 ret = 0;
3411 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003412 }
3413
3414 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02003415 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003416 if (need_resched())
3417 schedule();
3418 }
3419
Mohammed Gamal80ced182009-09-01 12:48:18 +02003420 vmx->emulation_required = 0;
3421out:
3422 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003423}
3424
Avi Kivity6aa8b732006-12-10 02:21:36 -08003425/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003426 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
3427 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
3428 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03003429static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003430{
3431 skip_emulated_instruction(vcpu);
3432 kvm_vcpu_on_spin(vcpu);
3433
3434 return 1;
3435}
3436
Sheng Yang59708672009-12-15 13:29:54 +08003437static int handle_invalid_op(struct kvm_vcpu *vcpu)
3438{
3439 kvm_queue_exception(vcpu, UD_VECTOR);
3440 return 1;
3441}
3442
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003443/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003444 * The exit handlers return 1 if the exit was handled fully and guest execution
3445 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3446 * to be done to userspace and return 0.
3447 */
Avi Kivity851ba692009-08-24 11:10:17 +03003448static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003449 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3450 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003451 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003452 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003453 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003454 [EXIT_REASON_CR_ACCESS] = handle_cr,
3455 [EXIT_REASON_DR_ACCESS] = handle_dr,
3456 [EXIT_REASON_CPUID] = handle_cpuid,
3457 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3458 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3459 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3460 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003461 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003462 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003463 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3464 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3465 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3466 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3467 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3468 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3469 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3470 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3471 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003472 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3473 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003474 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003475 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08003476 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003477 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
3478 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003479 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sheng Yang59708672009-12-15 13:29:54 +08003480 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
3481 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003482};
3483
3484static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003485 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003486
3487/*
3488 * The guest has exited. See if we can fix it or if we need userspace
3489 * assistance.
3490 */
Avi Kivity851ba692009-08-24 11:10:17 +03003491static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003493 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003494 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003495 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003496
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003497 trace_kvm_exit(exit_reason, kvm_rip_read(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003498
Mohammed Gamal80ced182009-09-01 12:48:18 +02003499 /* If guest state is invalid, start emulating */
3500 if (vmx->emulation_required && emulate_invalid_guest_state)
3501 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003502
Sheng Yang14394422008-04-28 12:24:45 +08003503 /* Access CR3 don't cause VMExit in paging mode, so we need
3504 * to sync with guest real CR3. */
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003505 if (enable_ept && is_paging(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08003506 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003507
Avi Kivity29bd8a72007-09-10 17:27:03 +03003508 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003509 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3510 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03003511 = vmcs_read32(VM_INSTRUCTION_ERROR);
3512 return 0;
3513 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003514
Mike Dayd77c26f2007-10-08 09:02:08 -04003515 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003516 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003517 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3518 exit_reason != EXIT_REASON_TASK_SWITCH))
3519 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3520 "(0x%x) and exit reason is 0x%x\n",
3521 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003522
3523 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03003524 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003525 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003526 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003527 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003528 /*
3529 * This CPU don't support us in finding the end of an
3530 * NMI-blocked window if the guest runs with IRQs
3531 * disabled. So we pull the trigger after 1 s of
3532 * futile waiting, but inform the user about this.
3533 */
3534 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3535 "state on VCPU %d after 1 s timeout\n",
3536 __func__, vcpu->vcpu_id);
3537 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003538 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003539 }
3540
Avi Kivity6aa8b732006-12-10 02:21:36 -08003541 if (exit_reason < kvm_vmx_max_exit_handlers
3542 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03003543 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003544 else {
Avi Kivity851ba692009-08-24 11:10:17 +03003545 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3546 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003547 }
3548 return 0;
3549}
3550
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003551static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003552{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003553 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003554 vmcs_write32(TPR_THRESHOLD, 0);
3555 return;
3556 }
3557
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003558 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003559}
3560
Avi Kivitycf393f72008-07-01 16:20:21 +03003561static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3562{
3563 u32 exit_intr_info;
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003564 u32 idt_vectoring_info = vmx->idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003565 bool unblock_nmi;
3566 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003567 int type;
3568 bool idtv_info_valid;
Avi Kivitycf393f72008-07-01 16:20:21 +03003569
3570 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Gleb Natapov20f65982009-05-11 13:35:55 +03003571
Andi Kleena0861c02009-06-08 17:37:09 +08003572 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
3573
3574 /* Handle machine checks before interrupts are enabled */
3575 if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
3576 || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
3577 && is_machine_check(exit_intr_info)))
3578 kvm_machine_check();
3579
Gleb Natapov20f65982009-05-11 13:35:55 +03003580 /* We need to handle NMIs before interrupts are enabled */
3581 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003582 (exit_intr_info & INTR_INFO_VALID_MASK))
Gleb Natapov20f65982009-05-11 13:35:55 +03003583 asm("int $2");
Gleb Natapov20f65982009-05-11 13:35:55 +03003584
3585 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3586
Avi Kivitycf393f72008-07-01 16:20:21 +03003587 if (cpu_has_virtual_nmis()) {
3588 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3589 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3590 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003591 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03003592 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3593 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003594 * SDM 3: 23.2.2 (September 2008)
3595 * Bit 12 is undefined in any of the following cases:
3596 * If the VM exit sets the valid bit in the IDT-vectoring
3597 * information field.
3598 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03003599 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003600 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
3601 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03003602 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3603 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003604 } else if (unlikely(vmx->soft_vnmi_blocked))
3605 vmx->vnmi_blocked_time +=
3606 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003607
Gleb Natapov37b96e92009-03-30 16:03:13 +03003608 vmx->vcpu.arch.nmi_injected = false;
3609 kvm_clear_exception_queue(&vmx->vcpu);
3610 kvm_clear_interrupt_queue(&vmx->vcpu);
3611
3612 if (!idtv_info_valid)
3613 return;
3614
Avi Kivity668f6122008-07-02 09:28:55 +03003615 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3616 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003617
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003618 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003619 case INTR_TYPE_NMI_INTR:
3620 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03003621 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003622 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03003623 * Clear bit "block by NMI" before VM entry if a NMI
3624 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03003625 */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003626 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3627 GUEST_INTR_STATE_NMI);
3628 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003629 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003630 vmx->vcpu.arch.event_exit_inst_len =
3631 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3632 /* fall through */
3633 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03003634 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003635 u32 err = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3636 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03003637 } else
3638 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003639 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003640 case INTR_TYPE_SOFT_INTR:
3641 vmx->vcpu.arch.event_exit_inst_len =
3642 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3643 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003644 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003645 kvm_queue_interrupt(&vmx->vcpu, vector,
3646 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003647 break;
3648 default:
3649 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03003650 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003651}
3652
Avi Kivity9c8cba32007-11-22 11:42:59 +02003653/*
3654 * Failure to inject an interrupt should give us the information
3655 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3656 * when fetching the interrupt redirection bitmap in the real-mode
3657 * tss, this doesn't happen. So we do it ourselves.
3658 */
3659static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3660{
3661 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003662 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003663 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003664 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003665 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3666 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3667 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3668 return;
3669 }
3670 vmx->idt_vectoring_info =
3671 VECTORING_INFO_VALID_MASK
3672 | INTR_TYPE_EXT_INTR
3673 | vmx->rmode.irq.vector;
3674}
3675
Avi Kivityc8019492008-07-14 14:44:59 +03003676#ifdef CONFIG_X86_64
3677#define R "r"
3678#define Q "q"
3679#else
3680#define R "e"
3681#define Q "l"
3682#endif
3683
Avi Kivity851ba692009-08-24 11:10:17 +03003684static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003685{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003686 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitye6adf282007-04-30 16:07:54 +03003687
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003688 /* Record the guest's net vcpu time for enforced NMI injections. */
3689 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3690 vmx->entry_time = ktime_get();
3691
Mohammed Gamal80ced182009-09-01 12:48:18 +02003692 /* Don't enter VMX if guest state is invalid, let the exit handler
3693 start emulation until we arrive back to a valid state */
3694 if (vmx->emulation_required && emulate_invalid_guest_state)
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003695 return;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003696
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003697 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3698 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3699 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3700 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3701
Gleb Natapov787ff732009-05-18 11:44:06 +03003702 /* When single-stepping over STI and MOV SS, we must clear the
3703 * corresponding interruptibility bits in the guest state. Otherwise
3704 * vmentry fails as it then expects bit 14 (BS) in pending debug
3705 * exceptions being set, but that's not correct for the guest debugging
3706 * case. */
3707 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3708 vmx_set_interrupt_shadow(vcpu, 0);
3709
Avi Kivitye6adf282007-04-30 16:07:54 +03003710 /*
3711 * Loading guest fpu may have cleared host cr0.ts
3712 */
3713 vmcs_writel(HOST_CR0, read_cr0());
3714
Avi Kivitye8a48342009-09-01 16:06:25 +03003715 if (vcpu->arch.switch_db_regs)
3716 set_debugreg(vcpu->arch.dr6, 6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003717
Mike Dayd77c26f2007-10-08 09:02:08 -04003718 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003719 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003720 "push %%"R"dx; push %%"R"bp;"
3721 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003722 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3723 "je 1f \n\t"
3724 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003725 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003726 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03003727 /* Reload cr2 if changed */
3728 "mov %c[cr2](%0), %%"R"ax \n\t"
3729 "mov %%cr2, %%"R"dx \n\t"
3730 "cmp %%"R"ax, %%"R"dx \n\t"
3731 "je 2f \n\t"
3732 "mov %%"R"ax, %%cr2 \n\t"
3733 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003734 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003735 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003736 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003737 "mov %c[rax](%0), %%"R"ax \n\t"
3738 "mov %c[rbx](%0), %%"R"bx \n\t"
3739 "mov %c[rdx](%0), %%"R"dx \n\t"
3740 "mov %c[rsi](%0), %%"R"si \n\t"
3741 "mov %c[rdi](%0), %%"R"di \n\t"
3742 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003743#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003744 "mov %c[r8](%0), %%r8 \n\t"
3745 "mov %c[r9](%0), %%r9 \n\t"
3746 "mov %c[r10](%0), %%r10 \n\t"
3747 "mov %c[r11](%0), %%r11 \n\t"
3748 "mov %c[r12](%0), %%r12 \n\t"
3749 "mov %c[r13](%0), %%r13 \n\t"
3750 "mov %c[r14](%0), %%r14 \n\t"
3751 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003752#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003753 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3754
Avi Kivity6aa8b732006-12-10 02:21:36 -08003755 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003756 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003757 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003758 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003759 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003760 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003761 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003762 "xchg %0, (%%"R"sp) \n\t"
3763 "mov %%"R"ax, %c[rax](%0) \n\t"
3764 "mov %%"R"bx, %c[rbx](%0) \n\t"
3765 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3766 "mov %%"R"dx, %c[rdx](%0) \n\t"
3767 "mov %%"R"si, %c[rsi](%0) \n\t"
3768 "mov %%"R"di, %c[rdi](%0) \n\t"
3769 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003770#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003771 "mov %%r8, %c[r8](%0) \n\t"
3772 "mov %%r9, %c[r9](%0) \n\t"
3773 "mov %%r10, %c[r10](%0) \n\t"
3774 "mov %%r11, %c[r11](%0) \n\t"
3775 "mov %%r12, %c[r12](%0) \n\t"
3776 "mov %%r13, %c[r13](%0) \n\t"
3777 "mov %%r14, %c[r14](%0) \n\t"
3778 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003779#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003780 "mov %%cr2, %%"R"ax \n\t"
3781 "mov %%"R"ax, %c[cr2](%0) \n\t"
3782
3783 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003784 "setbe %c[fail](%0) \n\t"
3785 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3786 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3787 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03003788 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003789 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3790 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3791 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3792 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3793 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3794 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3795 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003796#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003797 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3798 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3799 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3800 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3801 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3802 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3803 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3804 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003805#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003806 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003807 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003808 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003809#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003810 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3811#endif
3812 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003813
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003814 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
3815 | (1 << VCPU_EXREG_PDPTR));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003816 vcpu->arch.regs_dirty = 0;
3817
Avi Kivitye8a48342009-09-01 16:06:25 +03003818 if (vcpu->arch.switch_db_regs)
3819 get_debugreg(vcpu->arch.dr6, 6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003820
Avi Kivity1155f762007-11-22 11:30:47 +02003821 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003822 if (vmx->rmode.irq.pending)
3823 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003824
Mike Dayd77c26f2007-10-08 09:02:08 -04003825 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003826 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003827
Avi Kivitycf393f72008-07-01 16:20:21 +03003828 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003829}
3830
Avi Kivityc8019492008-07-14 14:44:59 +03003831#undef R
3832#undef Q
3833
Avi Kivity6aa8b732006-12-10 02:21:36 -08003834static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3835{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003836 struct vcpu_vmx *vmx = to_vmx(vcpu);
3837
3838 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003839 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003840 free_vmcs(vmx->vmcs);
3841 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003842 }
3843}
3844
3845static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3846{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003847 struct vcpu_vmx *vmx = to_vmx(vcpu);
3848
Sheng Yang2384d2b2008-01-17 15:14:33 +08003849 spin_lock(&vmx_vpid_lock);
3850 if (vmx->vpid != 0)
3851 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3852 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003853 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003854 kfree(vmx->guest_msrs);
3855 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003856 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003857}
3858
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003859static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003860{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003861 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003862 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003863 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003865 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003866 return ERR_PTR(-ENOMEM);
3867
Sheng Yang2384d2b2008-01-17 15:14:33 +08003868 allocate_vpid(vmx);
3869
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003870 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3871 if (err)
3872 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003873
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003874 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003875 if (!vmx->guest_msrs) {
3876 err = -ENOMEM;
3877 goto uninit_vcpu;
3878 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003879
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003880 vmx->vmcs = alloc_vmcs();
3881 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003882 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003883
3884 vmcs_clear(vmx->vmcs);
3885
Avi Kivity15ad7142007-07-11 18:17:21 +03003886 cpu = get_cpu();
3887 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003888 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003889 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003890 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003891 if (err)
3892 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003893 if (vm_need_virtualize_apic_accesses(kvm))
3894 if (alloc_apic_access_page(kvm) != 0)
3895 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003896
Sheng Yangb927a3c2009-07-21 10:42:48 +08003897 if (enable_ept) {
3898 if (!kvm->arch.ept_identity_map_addr)
3899 kvm->arch.ept_identity_map_addr =
3900 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003901 if (alloc_identity_pagetable(kvm) != 0)
3902 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003903 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003904
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003905 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003906
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003907free_vmcs:
3908 free_vmcs(vmx->vmcs);
3909free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003910 kfree(vmx->guest_msrs);
3911uninit_vcpu:
3912 kvm_vcpu_uninit(&vmx->vcpu);
3913free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003914 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003915 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003916}
3917
Yang, Sheng002c7f72007-07-31 14:23:01 +03003918static void __init vmx_check_processor_compat(void *rtn)
3919{
3920 struct vmcs_config vmcs_conf;
3921
3922 *(int *)rtn = 0;
3923 if (setup_vmcs_config(&vmcs_conf) < 0)
3924 *(int *)rtn = -EIO;
3925 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3926 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3927 smp_processor_id());
3928 *(int *)rtn = -EIO;
3929 }
3930}
3931
Sheng Yang67253af2008-04-25 10:20:22 +08003932static int get_ept_level(void)
3933{
3934 return VMX_EPT_DEFAULT_GAW + 1;
3935}
3936
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003937static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003938{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003939 u64 ret;
3940
Sheng Yang522c68c2009-04-27 20:35:43 +08003941 /* For VT-d and EPT combination
3942 * 1. MMIO: always map as UC
3943 * 2. EPT with VT-d:
3944 * a. VT-d without snooping control feature: can't guarantee the
3945 * result, try to trust guest.
3946 * b. VT-d with snooping control feature: snooping control feature of
3947 * VT-d engine can guarantee the cache correctness. Just set it
3948 * to WB to keep consistent with host. So the same as item 3.
3949 * 3. EPT without VT-d: always map as WB and set IGMT=1 to keep
3950 * consistent with host MTRR
3951 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003952 if (is_mmio)
3953 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08003954 else if (vcpu->kvm->arch.iommu_domain &&
3955 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
3956 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
3957 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003958 else
Sheng Yang522c68c2009-04-27 20:35:43 +08003959 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
3960 | VMX_EPT_IGMT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003961
3962 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08003963}
3964
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003965static const struct trace_print_flags vmx_exit_reasons_str[] = {
3966 { EXIT_REASON_EXCEPTION_NMI, "exception" },
3967 { EXIT_REASON_EXTERNAL_INTERRUPT, "ext_irq" },
3968 { EXIT_REASON_TRIPLE_FAULT, "triple_fault" },
3969 { EXIT_REASON_NMI_WINDOW, "nmi_window" },
3970 { EXIT_REASON_IO_INSTRUCTION, "io_instruction" },
3971 { EXIT_REASON_CR_ACCESS, "cr_access" },
3972 { EXIT_REASON_DR_ACCESS, "dr_access" },
3973 { EXIT_REASON_CPUID, "cpuid" },
3974 { EXIT_REASON_MSR_READ, "rdmsr" },
3975 { EXIT_REASON_MSR_WRITE, "wrmsr" },
3976 { EXIT_REASON_PENDING_INTERRUPT, "interrupt_window" },
3977 { EXIT_REASON_HLT, "halt" },
3978 { EXIT_REASON_INVLPG, "invlpg" },
3979 { EXIT_REASON_VMCALL, "hypercall" },
3980 { EXIT_REASON_TPR_BELOW_THRESHOLD, "tpr_below_thres" },
3981 { EXIT_REASON_APIC_ACCESS, "apic_access" },
3982 { EXIT_REASON_WBINVD, "wbinvd" },
3983 { EXIT_REASON_TASK_SWITCH, "task_switch" },
3984 { EXIT_REASON_EPT_VIOLATION, "ept_violation" },
3985 { -1, NULL }
3986};
3987
Joerg Roedel344f4142009-07-27 16:30:48 +02003988static bool vmx_gb_page_enable(void)
3989{
3990 return false;
3991}
3992
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003993static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003994 .cpu_has_kvm_support = cpu_has_kvm_support,
3995 .disabled_by_bios = vmx_disabled_by_bios,
3996 .hardware_setup = hardware_setup,
3997 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003998 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003999 .hardware_enable = hardware_enable,
4000 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08004001 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004002
4003 .vcpu_create = vmx_create_vcpu,
4004 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004005 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004006
Avi Kivity04d2cc72007-09-10 18:10:54 +03004007 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004008 .vcpu_load = vmx_vcpu_load,
4009 .vcpu_put = vmx_vcpu_put,
4010
4011 .set_guest_debug = set_guest_debug,
4012 .get_msr = vmx_get_msr,
4013 .set_msr = vmx_set_msr,
4014 .get_segment_base = vmx_get_segment_base,
4015 .get_segment = vmx_get_segment,
4016 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004017 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004018 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004019 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004020 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004021 .set_cr3 = vmx_set_cr3,
4022 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004023 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004024 .get_idt = vmx_get_idt,
4025 .set_idt = vmx_set_idt,
4026 .get_gdt = vmx_get_gdt,
4027 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004028 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004029 .get_rflags = vmx_get_rflags,
4030 .set_rflags = vmx_set_rflags,
4031
4032 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004033
Avi Kivity6aa8b732006-12-10 02:21:36 -08004034 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02004035 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004036 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004037 .set_interrupt_shadow = vmx_set_interrupt_shadow,
4038 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004039 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004040 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004041 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004042 .queue_exception = vmx_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02004043 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004044 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004045 .get_nmi_mask = vmx_get_nmi_mask,
4046 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004047 .enable_nmi_window = enable_nmi_window,
4048 .enable_irq_window = enable_irq_window,
4049 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004050
Izik Eiduscbc94022007-10-25 00:29:55 +02004051 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004052 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004053 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004054
4055 .exit_reasons_str = vmx_exit_reasons_str,
Joerg Roedel344f4142009-07-27 16:30:48 +02004056 .gb_page_enable = vmx_gb_page_enable,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004057};
4058
4059static int __init vmx_init(void)
4060{
Avi Kivity26bb0982009-09-07 11:14:12 +03004061 int r, i;
4062
4063 rdmsrl_safe(MSR_EFER, &host_efer);
4064
4065 for (i = 0; i < NR_VMX_MSR; ++i)
4066 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03004067
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004068 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004069 if (!vmx_io_bitmap_a)
4070 return -ENOMEM;
4071
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004072 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004073 if (!vmx_io_bitmap_b) {
4074 r = -ENOMEM;
4075 goto out;
4076 }
4077
Avi Kivity58972972009-02-24 22:26:47 +02004078 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
4079 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08004080 r = -ENOMEM;
4081 goto out1;
4082 }
4083
Avi Kivity58972972009-02-24 22:26:47 +02004084 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
4085 if (!vmx_msr_bitmap_longmode) {
4086 r = -ENOMEM;
4087 goto out2;
4088 }
4089
He, Qingfdef3ad2007-04-30 09:45:24 +03004090 /*
4091 * Allow direct access to the PC debug port (it is often used for I/O
4092 * delays, but the vmexits simply slow things down).
4093 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004094 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
4095 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004096
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004097 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004098
Avi Kivity58972972009-02-24 22:26:47 +02004099 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
4100 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08004101
Sheng Yang2384d2b2008-01-17 15:14:33 +08004102 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
4103
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004104 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004105 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02004106 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08004107
Avi Kivity58972972009-02-24 22:26:47 +02004108 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
4109 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
4110 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
4111 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
4112 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
4113 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03004114
Avi Kivity089d0342009-03-23 18:26:32 +02004115 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004116 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004117 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08004118 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08004119 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004120 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004121 kvm_enable_tdp();
4122 } else
4123 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08004124
Avi Kivityc7addb92007-09-16 18:58:32 +02004125 if (bypass_guest_pf)
4126 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
4127
He, Qingfdef3ad2007-04-30 09:45:24 +03004128 return 0;
4129
Avi Kivity58972972009-02-24 22:26:47 +02004130out3:
4131 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08004132out2:
Avi Kivity58972972009-02-24 22:26:47 +02004133 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03004134out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004135 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03004136out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004137 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004138 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004139}
4140
4141static void __exit vmx_exit(void)
4142{
Avi Kivity58972972009-02-24 22:26:47 +02004143 free_page((unsigned long)vmx_msr_bitmap_legacy);
4144 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004145 free_page((unsigned long)vmx_io_bitmap_b);
4146 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004147
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004148 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004149}
4150
4151module_init(vmx_init)
4152module_exit(vmx_exit)