blob: 2b7e7bd190fd5a072ad9500c707726294d128a76 [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
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040064struct vmcs {
65 u32 revision_id;
66 u32 abort;
67 char data[0];
68};
69
70struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100071 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +030072 struct list_head local_vcpus_link;
Avi Kivity313dbd42008-07-17 18:04:30 +030073 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040074 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030075 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020076 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040077 struct kvm_msr_entry *guest_msrs;
78 struct kvm_msr_entry *host_msrs;
79 int nmsrs;
80 int save_nmsrs;
81 int msr_offset_efer;
82#ifdef CONFIG_X86_64
83 int msr_offset_kernel_gs_base;
84#endif
85 struct vmcs *vmcs;
86 struct {
87 int loaded;
88 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020089 int gs_ldt_reload_needed;
90 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030091 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040092 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +020093 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +030094 int vm86_active;
95 u8 save_iopl;
96 struct kvm_save_segment {
97 u16 selector;
98 unsigned long base;
99 u32 limit;
100 u32 ar;
101 } tr, es, ds, fs, gs;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200102 struct {
103 bool pending;
104 u8 vector;
105 unsigned rip;
106 } irq;
107 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800108 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300109 bool emulation_required;
Avi Kivity8b3079a2009-01-05 12:10:54 +0200110 enum emulation_result invalid_state_emulation_result;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200111
112 /* Support for vnmi-less CPUs */
113 int soft_vnmi_blocked;
114 ktime_t entry_time;
115 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800116 u32 exit_reason;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400117};
118
119static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
120{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000121 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400122}
123
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800124static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800125static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300126
Avi Kivity6aa8b732006-12-10 02:21:36 -0800127static DEFINE_PER_CPU(struct vmcs *, vmxarea);
128static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300129static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200131static unsigned long *vmx_io_bitmap_a;
132static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200133static unsigned long *vmx_msr_bitmap_legacy;
134static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300135
Sheng Yang2384d2b2008-01-17 15:14:33 +0800136static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
137static DEFINE_SPINLOCK(vmx_vpid_lock);
138
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300139static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140 int size;
141 int order;
142 u32 revision_id;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300143 u32 pin_based_exec_ctrl;
144 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800145 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300146 u32 vmexit_ctrl;
147 u32 vmentry_ctrl;
148} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800149
Hannes Ederefff9e52008-11-28 17:02:06 +0100150static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800151 u32 ept;
152 u32 vpid;
153} vmx_capability;
154
Avi Kivity6aa8b732006-12-10 02:21:36 -0800155#define VMX_SEGMENT_FIELD(seg) \
156 [VCPU_SREG_##seg] = { \
157 .selector = GUEST_##seg##_SELECTOR, \
158 .base = GUEST_##seg##_BASE, \
159 .limit = GUEST_##seg##_LIMIT, \
160 .ar_bytes = GUEST_##seg##_AR_BYTES, \
161 }
162
163static struct kvm_vmx_segment_field {
164 unsigned selector;
165 unsigned base;
166 unsigned limit;
167 unsigned ar_bytes;
168} kvm_vmx_segment_fields[] = {
169 VMX_SEGMENT_FIELD(CS),
170 VMX_SEGMENT_FIELD(DS),
171 VMX_SEGMENT_FIELD(ES),
172 VMX_SEGMENT_FIELD(FS),
173 VMX_SEGMENT_FIELD(GS),
174 VMX_SEGMENT_FIELD(SS),
175 VMX_SEGMENT_FIELD(TR),
176 VMX_SEGMENT_FIELD(LDTR),
177};
178
Avi Kivity6de4f3ad2009-05-31 22:58:47 +0300179static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
180
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300181/*
182 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
183 * away by decrementing the array size.
184 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800186#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800187 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
188#endif
189 MSR_EFER, MSR_K6_STAR,
190};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200191#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400193static void load_msrs(struct kvm_msr_entry *e, int n)
194{
195 int i;
196
197 for (i = 0; i < n; ++i)
198 wrmsrl(e[i].index, e[i].data);
199}
200
201static void save_msrs(struct kvm_msr_entry *e, int n)
202{
203 int i;
204
205 for (i = 0; i < n; ++i)
206 rdmsrl(e[i].index, e[i].data);
207}
208
Avi Kivity6aa8b732006-12-10 02:21:36 -0800209static inline int is_page_fault(u32 intr_info)
210{
211 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
212 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100213 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800214}
215
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300216static inline int is_no_device(u32 intr_info)
217{
218 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
219 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100220 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300221}
222
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500223static inline int is_invalid_opcode(u32 intr_info)
224{
225 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
226 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100227 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500228}
229
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230static inline int is_external_interrupt(u32 intr_info)
231{
232 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
233 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
234}
235
Andi Kleena0861c02009-06-08 17:37:09 +0800236static inline int is_machine_check(u32 intr_info)
237{
238 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
239 INTR_INFO_VALID_MASK)) ==
240 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
241}
242
Sheng Yang25c5f222008-03-28 13:18:56 +0800243static inline int cpu_has_vmx_msr_bitmap(void)
244{
Sheng Yang04547152009-04-01 15:52:31 +0800245 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800246}
247
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800248static inline int cpu_has_vmx_tpr_shadow(void)
249{
Sheng Yang04547152009-04-01 15:52:31 +0800250 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800251}
252
253static inline int vm_need_tpr_shadow(struct kvm *kvm)
254{
Sheng Yang04547152009-04-01 15:52:31 +0800255 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800256}
257
Sheng Yangf78e0e22007-10-29 09:40:42 +0800258static inline int cpu_has_secondary_exec_ctrls(void)
259{
Sheng Yang04547152009-04-01 15:52:31 +0800260 return vmcs_config.cpu_based_exec_ctrl &
261 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800262}
263
Avi Kivity774ead32007-12-26 13:57:04 +0200264static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800265{
Sheng Yang04547152009-04-01 15:52:31 +0800266 return vmcs_config.cpu_based_2nd_exec_ctrl &
267 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
268}
269
270static inline bool cpu_has_vmx_flexpriority(void)
271{
272 return cpu_has_vmx_tpr_shadow() &&
273 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800274}
275
Marcelo Tosattie7997942009-06-11 12:07:40 -0300276static inline bool cpu_has_vmx_ept_execute_only(void)
277{
278 return !!(vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT);
279}
280
281static inline bool cpu_has_vmx_eptp_uncacheable(void)
282{
283 return !!(vmx_capability.ept & VMX_EPTP_UC_BIT);
284}
285
286static inline bool cpu_has_vmx_eptp_writeback(void)
287{
288 return !!(vmx_capability.ept & VMX_EPTP_WB_BIT);
289}
290
291static inline bool cpu_has_vmx_ept_2m_page(void)
292{
293 return !!(vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT);
294}
295
Sheng Yangd56f5462008-04-25 10:13:16 +0800296static inline int cpu_has_vmx_invept_individual_addr(void)
297{
Sheng Yang04547152009-04-01 15:52:31 +0800298 return !!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800299}
300
301static inline int cpu_has_vmx_invept_context(void)
302{
Sheng Yang04547152009-04-01 15:52:31 +0800303 return !!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800304}
305
306static inline int cpu_has_vmx_invept_global(void)
307{
Sheng Yang04547152009-04-01 15:52:31 +0800308 return !!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800309}
310
311static inline int cpu_has_vmx_ept(void)
312{
Sheng Yang04547152009-04-01 15:52:31 +0800313 return vmcs_config.cpu_based_2nd_exec_ctrl &
314 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800315}
316
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700317static inline int cpu_has_vmx_unrestricted_guest(void)
318{
319 return vmcs_config.cpu_based_2nd_exec_ctrl &
320 SECONDARY_EXEC_UNRESTRICTED_GUEST;
321}
322
Sheng Yangf78e0e22007-10-29 09:40:42 +0800323static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
324{
Sheng Yang04547152009-04-01 15:52:31 +0800325 return flexpriority_enabled &&
326 (cpu_has_vmx_virtualize_apic_accesses()) &&
327 (irqchip_in_kernel(kvm));
Sheng Yangf78e0e22007-10-29 09:40:42 +0800328}
329
Sheng Yang2384d2b2008-01-17 15:14:33 +0800330static inline int cpu_has_vmx_vpid(void)
331{
Sheng Yang04547152009-04-01 15:52:31 +0800332 return vmcs_config.cpu_based_2nd_exec_ctrl &
333 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800334}
335
Sheng Yangf08864b2008-05-15 18:23:25 +0800336static inline int cpu_has_virtual_nmis(void)
337{
338 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
339}
340
Sheng Yang04547152009-04-01 15:52:31 +0800341static inline bool report_flexpriority(void)
342{
343 return flexpriority_enabled;
344}
345
Rusty Russell8b9cf982007-07-30 16:31:43 +1000346static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800347{
348 int i;
349
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400350 for (i = 0; i < vmx->nmsrs; ++i)
351 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300352 return i;
353 return -1;
354}
355
Sheng Yang2384d2b2008-01-17 15:14:33 +0800356static inline void __invvpid(int ext, u16 vpid, gva_t gva)
357{
358 struct {
359 u64 vpid : 16;
360 u64 rsvd : 48;
361 u64 gva;
362 } operand = { vpid, 0, gva };
363
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300364 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800365 /* CF==1 or ZF==1 --> rc = -1 */
366 "; ja 1f ; ud2 ; 1:"
367 : : "a"(&operand), "c"(ext) : "cc", "memory");
368}
369
Sheng Yang14394422008-04-28 12:24:45 +0800370static inline void __invept(int ext, u64 eptp, gpa_t gpa)
371{
372 struct {
373 u64 eptp, gpa;
374 } operand = {eptp, gpa};
375
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300376 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800377 /* CF==1 or ZF==1 --> rc = -1 */
378 "; ja 1f ; ud2 ; 1:\n"
379 : : "a" (&operand), "c" (ext) : "cc", "memory");
380}
381
Rusty Russell8b9cf982007-07-30 16:31:43 +1000382static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300383{
384 int i;
385
Rusty Russell8b9cf982007-07-30 16:31:43 +1000386 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300387 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400388 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000389 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800390}
391
Avi Kivity6aa8b732006-12-10 02:21:36 -0800392static void vmcs_clear(struct vmcs *vmcs)
393{
394 u64 phys_addr = __pa(vmcs);
395 u8 error;
396
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300397 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
399 : "cc", "memory");
400 if (error)
401 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
402 vmcs, phys_addr);
403}
404
405static void __vcpu_clear(void *arg)
406{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000407 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800408 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800409
Rusty Russell8b9cf982007-07-30 16:31:43 +1000410 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400411 vmcs_clear(vmx->vmcs);
412 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800413 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800414 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300415 list_del(&vmx->local_vcpus_link);
416 vmx->vcpu.cpu = -1;
417 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418}
419
Rusty Russell8b9cf982007-07-30 16:31:43 +1000420static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800421{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200422 if (vmx->vcpu.cpu == -1)
423 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200424 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800425}
426
Sheng Yang2384d2b2008-01-17 15:14:33 +0800427static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
428{
429 if (vmx->vpid == 0)
430 return;
431
432 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
433}
434
Sheng Yang14394422008-04-28 12:24:45 +0800435static inline void ept_sync_global(void)
436{
437 if (cpu_has_vmx_invept_global())
438 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
439}
440
441static inline void ept_sync_context(u64 eptp)
442{
Avi Kivity089d0342009-03-23 18:26:32 +0200443 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800444 if (cpu_has_vmx_invept_context())
445 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
446 else
447 ept_sync_global();
448 }
449}
450
451static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
452{
Avi Kivity089d0342009-03-23 18:26:32 +0200453 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800454 if (cpu_has_vmx_invept_individual_addr())
455 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
456 eptp, gpa);
457 else
458 ept_sync_context(eptp);
459 }
460}
461
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462static unsigned long vmcs_readl(unsigned long field)
463{
464 unsigned long value;
465
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300466 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467 : "=a"(value) : "d"(field) : "cc");
468 return value;
469}
470
471static u16 vmcs_read16(unsigned long field)
472{
473 return vmcs_readl(field);
474}
475
476static u32 vmcs_read32(unsigned long field)
477{
478 return vmcs_readl(field);
479}
480
481static u64 vmcs_read64(unsigned long field)
482{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800483#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 return vmcs_readl(field);
485#else
486 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
487#endif
488}
489
Avi Kivitye52de1b2007-01-05 16:36:56 -0800490static noinline void vmwrite_error(unsigned long field, unsigned long value)
491{
492 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
493 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
494 dump_stack();
495}
496
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497static void vmcs_writel(unsigned long field, unsigned long value)
498{
499 u8 error;
500
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300501 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400502 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800503 if (unlikely(error))
504 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800505}
506
507static void vmcs_write16(unsigned long field, u16 value)
508{
509 vmcs_writel(field, value);
510}
511
512static void vmcs_write32(unsigned long field, u32 value)
513{
514 vmcs_writel(field, value);
515}
516
517static void vmcs_write64(unsigned long field, u64 value)
518{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300520#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521 asm volatile ("");
522 vmcs_writel(field+1, value >> 32);
523#endif
524}
525
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300526static void vmcs_clear_bits(unsigned long field, u32 mask)
527{
528 vmcs_writel(field, vmcs_readl(field) & ~mask);
529}
530
531static void vmcs_set_bits(unsigned long field, u32 mask)
532{
533 vmcs_writel(field, vmcs_readl(field) | mask);
534}
535
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300536static void update_exception_bitmap(struct kvm_vcpu *vcpu)
537{
538 u32 eb;
539
Andi Kleena0861c02009-06-08 17:37:09 +0800540 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300541 if (!vcpu->fpu_active)
542 eb |= 1u << NM_VECTOR;
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100543 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
544 if (vcpu->guest_debug &
545 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
546 eb |= 1u << DB_VECTOR;
547 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
548 eb |= 1u << BP_VECTOR;
549 }
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300550 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300551 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200552 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800553 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300554 vmcs_write32(EXCEPTION_BITMAP, eb);
555}
556
Avi Kivity33ed6322007-05-02 16:54:03 +0300557static void reload_tss(void)
558{
Avi Kivity33ed6322007-05-02 16:54:03 +0300559 /*
560 * VT restores TR but not its size. Useless.
561 */
562 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200563 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300564
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300565 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300566 descs = (void *)gdt.base;
567 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
568 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300569}
570
Rusty Russell8b9cf982007-07-30 16:31:43 +1000571static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300572{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400573 int efer_offset = vmx->msr_offset_efer;
Roel Kluin3a34a882009-08-04 02:08:45 -0700574 u64 host_efer;
575 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300576 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300577
Avi Kivity51c6cf62007-08-29 03:48:05 +0300578 if (efer_offset < 0)
579 return;
Roel Kluin3a34a882009-08-04 02:08:45 -0700580 host_efer = vmx->host_msrs[efer_offset].data;
581 guest_efer = vmx->guest_msrs[efer_offset].data;
582
Avi Kivity51c6cf62007-08-29 03:48:05 +0300583 /*
584 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
585 * outside long mode
586 */
587 ignore_bits = EFER_NX | EFER_SCE;
588#ifdef CONFIG_X86_64
589 ignore_bits |= EFER_LMA | EFER_LME;
590 /* SCE is meaningful only in long mode on Intel */
591 if (guest_efer & EFER_LMA)
592 ignore_bits &= ~(u64)EFER_SCE;
593#endif
594 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
595 return;
596
597 vmx->host_state.guest_efer_loaded = 1;
598 guest_efer &= ~ignore_bits;
599 guest_efer |= host_efer & ignore_bits;
600 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000601 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300602}
603
Avi Kivity51c6cf62007-08-29 03:48:05 +0300604static void reload_host_efer(struct vcpu_vmx *vmx)
605{
606 if (vmx->host_state.guest_efer_loaded) {
607 vmx->host_state.guest_efer_loaded = 0;
608 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
609 }
610}
611
Avi Kivity04d2cc72007-09-10 18:10:54 +0300612static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300613{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300614 struct vcpu_vmx *vmx = to_vmx(vcpu);
615
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400616 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300617 return;
618
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400619 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300620 /*
621 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
622 * allow segment selectors with cpl > 0 or ti == 1.
623 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300624 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200625 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300626 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200627 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400628 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200629 vmx->host_state.fs_reload_needed = 0;
630 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300631 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200632 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300633 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300634 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400635 if (!(vmx->host_state.gs_sel & 7))
636 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300637 else {
638 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200639 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300640 }
641
642#ifdef CONFIG_X86_64
643 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
644 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
645#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400646 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
647 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300648#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300649
650#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400651 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400652 save_msrs(vmx->host_msrs +
653 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400654
Avi Kivity707c0872007-05-02 17:33:43 +0300655#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400656 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300657 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300658}
659
Avi Kivitya9b21b62008-06-24 11:48:49 +0300660static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300661{
Avi Kivity15ad7142007-07-11 18:17:21 +0300662 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300663
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400664 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300665 return;
666
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200667 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400668 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200669 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300670 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200671 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300672 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300673 /*
674 * If we have to reload gs, we must take care to
675 * preserve our gs base.
676 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300677 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300678 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300679#ifdef CONFIG_X86_64
680 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
681#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300682 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300683 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200684 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400685 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
686 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300687 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300688}
689
Avi Kivitya9b21b62008-06-24 11:48:49 +0300690static void vmx_load_host_state(struct vcpu_vmx *vmx)
691{
692 preempt_disable();
693 __vmx_load_host_state(vmx);
694 preempt_enable();
695}
696
Avi Kivity6aa8b732006-12-10 02:21:36 -0800697/*
698 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
699 * vcpu mutex is already taken.
700 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300701static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800702{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400703 struct vcpu_vmx *vmx = to_vmx(vcpu);
704 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200705 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706
Eddie Donga3d7f852007-09-03 16:15:12 +0300707 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000708 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300709 kvm_migrate_timers(vcpu);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800710 vpid_sync_vcpu_all(vmx);
Avi Kivity543e4242008-05-13 16:22:47 +0300711 local_irq_disable();
712 list_add(&vmx->local_vcpus_link,
713 &per_cpu(vcpus_on_cpu, cpu));
714 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300715 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400717 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800718 u8 error;
719
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400720 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300721 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800722 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
723 : "cc");
724 if (error)
725 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400726 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800727 }
728
729 if (vcpu->cpu != cpu) {
730 struct descriptor_table dt;
731 unsigned long sysenter_esp;
732
733 vcpu->cpu = cpu;
734 /*
735 * Linux uses per-cpu TSS and GDT, so set these when switching
736 * processors.
737 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300738 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
739 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
741
742 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
743 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300744
745 /*
746 * Make sure the time stamp counter is monotonous.
747 */
748 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200749 if (tsc_this < vcpu->arch.host_tsc) {
750 delta = vcpu->arch.host_tsc - tsc_this;
751 new_offset = vmcs_read64(TSC_OFFSET) + delta;
752 vmcs_write64(TSC_OFFSET, new_offset);
753 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800754 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755}
756
757static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
758{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300759 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760}
761
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300762static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
763{
764 if (vcpu->fpu_active)
765 return;
766 vcpu->fpu_active = 1;
Rusty Russell707d92f2007-07-17 23:19:08 +1000767 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800768 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92f2007-07-17 23:19:08 +1000769 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300770 update_exception_bitmap(vcpu);
771}
772
773static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
774{
775 if (!vcpu->fpu_active)
776 return;
777 vcpu->fpu_active = 0;
Rusty Russell707d92f2007-07-17 23:19:08 +1000778 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300779 update_exception_bitmap(vcpu);
780}
781
Avi Kivity6aa8b732006-12-10 02:21:36 -0800782static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
783{
Avi Kivity345dcaa2009-08-12 15:29:37 +0300784 unsigned long rflags;
785
786 rflags = vmcs_readl(GUEST_RFLAGS);
787 if (to_vmx(vcpu)->rmode.vm86_active)
788 rflags &= ~(unsigned long)(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
789 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790}
791
792static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
793{
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300794 if (to_vmx(vcpu)->rmode.vm86_active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100795 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796 vmcs_writel(GUEST_RFLAGS, rflags);
797}
798
Glauber Costa2809f5d2009-05-12 16:21:05 -0400799static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
800{
801 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
802 int ret = 0;
803
804 if (interruptibility & GUEST_INTR_STATE_STI)
805 ret |= X86_SHADOW_INT_STI;
806 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
807 ret |= X86_SHADOW_INT_MOV_SS;
808
809 return ret & mask;
810}
811
812static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
813{
814 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
815 u32 interruptibility = interruptibility_old;
816
817 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
818
819 if (mask & X86_SHADOW_INT_MOV_SS)
820 interruptibility |= GUEST_INTR_STATE_MOV_SS;
821 if (mask & X86_SHADOW_INT_STI)
822 interruptibility |= GUEST_INTR_STATE_STI;
823
824 if ((interruptibility != interruptibility_old))
825 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
826}
827
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
829{
830 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800831
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300832 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800833 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300834 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835
Glauber Costa2809f5d2009-05-12 16:21:05 -0400836 /* skipping an emulated instruction also counts */
837 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838}
839
Avi Kivity298101d2007-11-25 13:41:11 +0200840static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
841 bool has_error_code, u32 error_code)
842{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200843 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100844 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200845
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100846 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200847 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100848 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
849 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200850
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300851 if (vmx->rmode.vm86_active) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200852 vmx->rmode.irq.pending = true;
853 vmx->rmode.irq.vector = nr;
854 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +0300855 if (kvm_exception_is_soft(nr))
856 vmx->rmode.irq.rip +=
857 vmx->vcpu.arch.event_exit_inst_len;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100858 intr_info |= INTR_TYPE_SOFT_INTR;
859 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200860 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
861 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
862 return;
863 }
864
Gleb Natapov66fd3f72009-05-11 13:35:50 +0300865 if (kvm_exception_is_soft(nr)) {
866 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
867 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100868 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
869 } else
870 intr_info |= INTR_TYPE_HARD_EXCEPTION;
871
872 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200873}
874
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875/*
Eddie Donga75beee2007-05-17 18:55:15 +0300876 * Swap MSR entry in host/guest MSR entry array.
877 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200878#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000879static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300880{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400881 struct kvm_msr_entry tmp;
882
883 tmp = vmx->guest_msrs[to];
884 vmx->guest_msrs[to] = vmx->guest_msrs[from];
885 vmx->guest_msrs[from] = tmp;
886 tmp = vmx->host_msrs[to];
887 vmx->host_msrs[to] = vmx->host_msrs[from];
888 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300889}
Gabriel C54e11fa2007-08-01 16:23:10 +0200890#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300891
892/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300893 * Set up the vmcs to automatically save and restore system
894 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
895 * mode, as fiddling with msrs is very expensive.
896 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000897static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300898{
Eddie Dong2cc51562007-05-21 07:28:09 +0300899 int save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +0200900 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +0300901
Avi Kivity33f9c502008-02-27 16:06:57 +0200902 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300903 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300904#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000905 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300906 int index;
907
Rusty Russell8b9cf982007-07-30 16:31:43 +1000908 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300909 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000910 move_msr_up(vmx, index, save_nmsrs++);
911 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300912 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000913 move_msr_up(vmx, index, save_nmsrs++);
914 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300915 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000916 move_msr_up(vmx, index, save_nmsrs++);
917 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300918 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000919 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300920 /*
921 * MSR_K6_STAR is only needed on long mode guests, and only
922 * if efer.sce is enabled.
923 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000924 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800925 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000926 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300927 }
Eddie Donga75beee2007-05-17 18:55:15 +0300928#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400929 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300930
Eddie Donga75beee2007-05-17 18:55:15 +0300931#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400932 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000933 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300934#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000935 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivity58972972009-02-24 22:26:47 +0200936
937 if (cpu_has_vmx_msr_bitmap()) {
938 if (is_long_mode(&vmx->vcpu))
939 msr_bitmap = vmx_msr_bitmap_longmode;
940 else
941 msr_bitmap = vmx_msr_bitmap_legacy;
942
943 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
944 }
Avi Kivitye38aea32007-04-19 13:22:48 +0300945}
946
947/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948 * reads and returns guest's timestamp counter "register"
949 * guest_tsc = host_tsc + tsc_offset -- 21.3
950 */
951static u64 guest_read_tsc(void)
952{
953 u64 host_tsc, tsc_offset;
954
955 rdtscll(host_tsc);
956 tsc_offset = vmcs_read64(TSC_OFFSET);
957 return host_tsc + tsc_offset;
958}
959
960/*
961 * writes 'guest_tsc' into guest's timestamp counter "register"
962 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
963 */
Marcelo Tosatti53f658b2008-12-11 20:45:05 +0100964static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
967}
968
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969/*
970 * Reads an msr value (of 'msr_index') into 'pdata'.
971 * Returns 0 on success, non-0 otherwise.
972 * Assumes vcpu_load() was already called.
973 */
974static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
975{
976 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400977 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978
979 if (!pdata) {
980 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
981 return -EINVAL;
982 }
983
984 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800985#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 case MSR_FS_BASE:
987 data = vmcs_readl(GUEST_FS_BASE);
988 break;
989 case MSR_GS_BASE:
990 data = vmcs_readl(GUEST_GS_BASE);
991 break;
992 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800993 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994#endif
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530995 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996 data = guest_read_tsc();
997 break;
998 case MSR_IA32_SYSENTER_CS:
999 data = vmcs_read32(GUEST_SYSENTER_CS);
1000 break;
1001 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001002 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 break;
1004 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001005 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007 default:
Avi Kivity516a1a72009-02-15 02:32:07 +02001008 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +10001009 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001010 if (msr) {
1011 data = msr->data;
1012 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001014 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015 }
1016
1017 *pdata = data;
1018 return 0;
1019}
1020
1021/*
1022 * Writes msr value into into the appropriate "register".
1023 * Returns 0 on success, non-0 otherwise.
1024 * Assumes vcpu_load() was already called.
1025 */
1026static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1027{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001028 struct vcpu_vmx *vmx = to_vmx(vcpu);
1029 struct kvm_msr_entry *msr;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01001030 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +03001031 int ret = 0;
1032
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001034 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001035 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001036 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001037 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001038#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039 case MSR_FS_BASE:
1040 vmcs_writel(GUEST_FS_BASE, data);
1041 break;
1042 case MSR_GS_BASE:
1043 vmcs_writel(GUEST_GS_BASE, data);
1044 break;
1045#endif
1046 case MSR_IA32_SYSENTER_CS:
1047 vmcs_write32(GUEST_SYSENTER_CS, data);
1048 break;
1049 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001050 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051 break;
1052 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001053 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001054 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301055 case MSR_IA32_TSC:
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01001056 rdtscll(host_tsc);
1057 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001059 case MSR_IA32_CR_PAT:
1060 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1061 vmcs_write64(GUEST_IA32_PAT, data);
1062 vcpu->arch.pat = data;
1063 break;
1064 }
1065 /* Otherwise falls through to kvm_set_msr_common */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066 default:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001067 vmx_load_host_state(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +10001068 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001069 if (msr) {
1070 msr->data = data;
1071 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001073 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074 }
1075
Eddie Dong2cc51562007-05-21 07:28:09 +03001076 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077}
1078
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001079static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001081 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1082 switch (reg) {
1083 case VCPU_REGS_RSP:
1084 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1085 break;
1086 case VCPU_REGS_RIP:
1087 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1088 break;
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03001089 case VCPU_EXREG_PDPTR:
1090 if (enable_ept)
1091 ept_save_pdptrs(vcpu);
1092 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001093 default:
1094 break;
1095 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096}
1097
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001098static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001099{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001100 int old_debug = vcpu->guest_debug;
1101 unsigned long flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001103 vcpu->guest_debug = dbg->control;
1104 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
1105 vcpu->guest_debug = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001107 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1108 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1109 else
1110 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1111
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001112 flags = vmcs_readl(GUEST_RFLAGS);
1113 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
1114 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1115 else if (old_debug & KVM_GUESTDBG_SINGLESTEP)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001116 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001117 vmcs_writel(GUEST_RFLAGS, flags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001119 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120
1121 return 0;
1122}
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
Avi Kivity774c47f2007-02-12 00:54:47 -08001140static void 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
Avi Kivity543e4242008-05-13 16:22:47 +03001146 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001148 if ((old & (FEATURE_CONTROL_LOCKED |
1149 FEATURE_CONTROL_VMXON_ENABLED))
1150 != (FEATURE_CONTROL_LOCKED |
1151 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001153 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001154 FEATURE_CONTROL_LOCKED |
1155 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001156 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001157 asm volatile (ASM_VMX_VMXON_RAX
1158 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159 : "memory", "cc");
1160}
1161
Avi Kivity543e4242008-05-13 16:22:47 +03001162static void vmclear_local_vcpus(void)
1163{
1164 int cpu = raw_smp_processor_id();
1165 struct vcpu_vmx *vmx, *n;
1166
1167 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1168 local_vcpus_link)
1169 __vcpu_clear(vmx);
1170}
1171
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001172
1173/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1174 * tricks.
1175 */
1176static void kvm_cpu_vmxoff(void)
1177{
1178 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1179 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1180}
1181
Avi Kivity6aa8b732006-12-10 02:21:36 -08001182static void hardware_disable(void *garbage)
1183{
Avi Kivity543e4242008-05-13 16:22:47 +03001184 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001185 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186}
1187
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001188static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001189 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001190{
1191 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001192 u32 ctl = ctl_min | ctl_opt;
1193
1194 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1195
1196 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1197 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1198
1199 /* Ensure minimum (required) set of control bits are supported. */
1200 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001201 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001202
1203 *result = ctl;
1204 return 0;
1205}
1206
Yang, Sheng002c7f72007-07-31 14:23:01 +03001207static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001208{
1209 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001210 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001211 u32 _pin_based_exec_control = 0;
1212 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001213 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001214 u32 _vmexit_control = 0;
1215 u32 _vmentry_control = 0;
1216
1217 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001218 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001219 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1220 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001221 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001222
1223 min = CPU_BASED_HLT_EXITING |
1224#ifdef CONFIG_X86_64
1225 CPU_BASED_CR8_LOAD_EXITING |
1226 CPU_BASED_CR8_STORE_EXITING |
1227#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001228 CPU_BASED_CR3_LOAD_EXITING |
1229 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001230 CPU_BASED_USE_IO_BITMAPS |
1231 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001232 CPU_BASED_USE_TSC_OFFSETING |
1233 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001234 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001235 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001236 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001237 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1238 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001239 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001240#ifdef CONFIG_X86_64
1241 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1242 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1243 ~CPU_BASED_CR8_STORE_EXITING;
1244#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001245 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001246 min2 = 0;
1247 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001248 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001249 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001250 SECONDARY_EXEC_ENABLE_EPT |
1251 SECONDARY_EXEC_UNRESTRICTED_GUEST;
Sheng Yangd56f5462008-04-25 10:13:16 +08001252 if (adjust_vmx_controls(min2, opt2,
1253 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001254 &_cpu_based_2nd_exec_control) < 0)
1255 return -EIO;
1256 }
1257#ifndef CONFIG_X86_64
1258 if (!(_cpu_based_2nd_exec_control &
1259 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1260 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1261#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001262 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001263 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1264 enabled */
Sheng Yangd56f5462008-04-25 10:13:16 +08001265 min &= ~(CPU_BASED_CR3_LOAD_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001266 CPU_BASED_CR3_STORE_EXITING |
1267 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001268 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1269 &_cpu_based_exec_control) < 0)
1270 return -EIO;
1271 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1272 vmx_capability.ept, vmx_capability.vpid);
1273 }
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001274
1275 min = 0;
1276#ifdef CONFIG_X86_64
1277 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1278#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001279 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001280 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1281 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001282 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001283
Sheng Yang468d4722008-10-09 16:01:55 +08001284 min = 0;
1285 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001286 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1287 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001288 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001290 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001291
1292 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1293 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001294 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001295
1296#ifdef CONFIG_X86_64
1297 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1298 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001299 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001300#endif
1301
1302 /* Require Write-Back (WB) memory type for VMCS accesses. */
1303 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001304 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001305
Yang, Sheng002c7f72007-07-31 14:23:01 +03001306 vmcs_conf->size = vmx_msr_high & 0x1fff;
1307 vmcs_conf->order = get_order(vmcs_config.size);
1308 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001309
Yang, Sheng002c7f72007-07-31 14:23:01 +03001310 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1311 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001312 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001313 vmcs_conf->vmexit_ctrl = _vmexit_control;
1314 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001315
1316 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001317}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318
1319static struct vmcs *alloc_vmcs_cpu(int cpu)
1320{
1321 int node = cpu_to_node(cpu);
1322 struct page *pages;
1323 struct vmcs *vmcs;
1324
Mel Gorman6484eb32009-06-16 15:31:54 -07001325 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326 if (!pages)
1327 return NULL;
1328 vmcs = page_address(pages);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001329 memset(vmcs, 0, vmcs_config.size);
1330 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331 return vmcs;
1332}
1333
1334static struct vmcs *alloc_vmcs(void)
1335{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001336 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337}
1338
1339static void free_vmcs(struct vmcs *vmcs)
1340{
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001341 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001342}
1343
Sam Ravnborg39959582007-06-01 00:47:13 -07001344static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001345{
1346 int cpu;
1347
1348 for_each_online_cpu(cpu)
1349 free_vmcs(per_cpu(vmxarea, cpu));
1350}
1351
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352static __init int alloc_kvm_area(void)
1353{
1354 int cpu;
1355
1356 for_each_online_cpu(cpu) {
1357 struct vmcs *vmcs;
1358
1359 vmcs = alloc_vmcs_cpu(cpu);
1360 if (!vmcs) {
1361 free_kvm_area();
1362 return -ENOMEM;
1363 }
1364
1365 per_cpu(vmxarea, cpu) = vmcs;
1366 }
1367 return 0;
1368}
1369
1370static __init int hardware_setup(void)
1371{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001372 if (setup_vmcs_config(&vmcs_config) < 0)
1373 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001374
1375 if (boot_cpu_has(X86_FEATURE_NX))
1376 kvm_enable_efer_bits(EFER_NX);
1377
Sheng Yang93ba03c2009-04-01 15:52:32 +08001378 if (!cpu_has_vmx_vpid())
1379 enable_vpid = 0;
1380
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001381 if (!cpu_has_vmx_ept()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001382 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001383 enable_unrestricted_guest = 0;
1384 }
1385
1386 if (!cpu_has_vmx_unrestricted_guest())
1387 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001388
1389 if (!cpu_has_vmx_flexpriority())
1390 flexpriority_enabled = 0;
1391
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001392 if (!cpu_has_vmx_tpr_shadow())
1393 kvm_x86_ops->update_cr8_intercept = NULL;
1394
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001395 if (enable_ept && !cpu_has_vmx_ept_2m_page())
1396 kvm_disable_largepages();
1397
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398 return alloc_kvm_area();
1399}
1400
1401static __exit void hardware_unsetup(void)
1402{
1403 free_kvm_area();
1404}
1405
Avi Kivity6aa8b732006-12-10 02:21:36 -08001406static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1407{
1408 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1409
Avi Kivity6af11b92007-03-19 13:18:10 +02001410 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411 vmcs_write16(sf->selector, save->selector);
1412 vmcs_writel(sf->base, save->base);
1413 vmcs_write32(sf->limit, save->limit);
1414 vmcs_write32(sf->ar_bytes, save->ar);
1415 } else {
1416 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1417 << AR_DPL_SHIFT;
1418 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1419 }
1420}
1421
1422static void enter_pmode(struct kvm_vcpu *vcpu)
1423{
1424 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001425 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001427 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001428 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001430 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
1431 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
1432 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001433
1434 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001435 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001436 flags |= (vmx->rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001437 vmcs_writel(GUEST_RFLAGS, flags);
1438
Rusty Russell66aee912007-07-17 23:34:16 +10001439 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1440 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441
1442 update_exception_bitmap(vcpu);
1443
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001444 if (emulate_invalid_guest_state)
1445 return;
1446
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001447 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
1448 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
1449 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
1450 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001451
1452 vmcs_write16(GUEST_SS_SELECTOR, 0);
1453 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1454
1455 vmcs_write16(GUEST_CS_SELECTOR,
1456 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1457 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1458}
1459
Mike Dayd77c26f2007-10-08 09:02:08 -04001460static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001461{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001462 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001463 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1464 kvm->memslots[0].npages - 3;
1465 return base_gfn << PAGE_SHIFT;
1466 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001467 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468}
1469
1470static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1471{
1472 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1473
1474 save->selector = vmcs_read16(sf->selector);
1475 save->base = vmcs_readl(sf->base);
1476 save->limit = vmcs_read32(sf->limit);
1477 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001478 vmcs_write16(sf->selector, save->base >> 4);
1479 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480 vmcs_write32(sf->limit, 0xffff);
1481 vmcs_write32(sf->ar_bytes, 0xf3);
1482}
1483
1484static void enter_rmode(struct kvm_vcpu *vcpu)
1485{
1486 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001487 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001489 if (enable_unrestricted_guest)
1490 return;
1491
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001492 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001493 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001495 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1497
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001498 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1500
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001501 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1503
1504 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001505 vmx->rmode.save_iopl
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001506 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001508 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001509
1510 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001511 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512 update_exception_bitmap(vcpu);
1513
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001514 if (emulate_invalid_guest_state)
1515 goto continue_rmode;
1516
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1518 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1519 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1520
1521 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001522 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001523 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1524 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001525 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1526
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001527 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
1528 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
1529 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
1530 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001531
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001532continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001533 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001534 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001535}
1536
Amit Shah401d10d2009-02-20 22:53:37 +05301537static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1538{
1539 struct vcpu_vmx *vmx = to_vmx(vcpu);
1540 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1541
1542 vcpu->arch.shadow_efer = efer;
1543 if (!msr)
1544 return;
1545 if (efer & EFER_LMA) {
1546 vmcs_write32(VM_ENTRY_CONTROLS,
1547 vmcs_read32(VM_ENTRY_CONTROLS) |
1548 VM_ENTRY_IA32E_MODE);
1549 msr->data = efer;
1550 } else {
1551 vmcs_write32(VM_ENTRY_CONTROLS,
1552 vmcs_read32(VM_ENTRY_CONTROLS) &
1553 ~VM_ENTRY_IA32E_MODE);
1554
1555 msr->data = efer & ~EFER_LME;
1556 }
1557 setup_msrs(vmx);
1558}
1559
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001560#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001561
1562static void enter_lmode(struct kvm_vcpu *vcpu)
1563{
1564 u32 guest_tr_ar;
1565
1566 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1567 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1568 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001569 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001570 vmcs_write32(GUEST_TR_AR_BYTES,
1571 (guest_tr_ar & ~AR_TYPE_MASK)
1572 | AR_TYPE_BUSY_64_TSS);
1573 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001574 vcpu->arch.shadow_efer |= EFER_LMA;
Amit Shah401d10d2009-02-20 22:53:37 +05301575 vmx_set_efer(vcpu, vcpu->arch.shadow_efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001576}
1577
1578static void exit_lmode(struct kvm_vcpu *vcpu)
1579{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001580 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581
1582 vmcs_write32(VM_ENTRY_CONTROLS,
1583 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001584 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001585}
1586
1587#endif
1588
Sheng Yang2384d2b2008-01-17 15:14:33 +08001589static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1590{
1591 vpid_sync_vcpu_all(to_vmx(vcpu));
Avi Kivity089d0342009-03-23 18:26:32 +02001592 if (enable_ept)
Sheng Yang4e1096d2008-07-06 19:16:51 +08001593 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001594}
1595
Anthony Liguori25c4c272007-04-27 09:29:21 +03001596static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001597{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001598 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1599 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001600}
1601
Sheng Yang14394422008-04-28 12:24:45 +08001602static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1603{
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03001604 if (!test_bit(VCPU_EXREG_PDPTR,
1605 (unsigned long *)&vcpu->arch.regs_dirty))
1606 return;
1607
Sheng Yang14394422008-04-28 12:24:45 +08001608 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Sheng Yang14394422008-04-28 12:24:45 +08001609 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1610 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1611 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1612 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1613 }
1614}
1615
Avi Kivity8f5d5492009-05-31 18:41:29 +03001616static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
1617{
1618 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1619 vcpu->arch.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
1620 vcpu->arch.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
1621 vcpu->arch.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
1622 vcpu->arch.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
1623 }
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03001624
1625 __set_bit(VCPU_EXREG_PDPTR,
1626 (unsigned long *)&vcpu->arch.regs_avail);
1627 __set_bit(VCPU_EXREG_PDPTR,
1628 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03001629}
1630
Sheng Yang14394422008-04-28 12:24:45 +08001631static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1632
1633static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1634 unsigned long cr0,
1635 struct kvm_vcpu *vcpu)
1636{
1637 if (!(cr0 & X86_CR0_PG)) {
1638 /* From paging/starting to nonpaging */
1639 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001640 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001641 (CPU_BASED_CR3_LOAD_EXITING |
1642 CPU_BASED_CR3_STORE_EXITING));
1643 vcpu->arch.cr0 = cr0;
1644 vmx_set_cr4(vcpu, vcpu->arch.cr4);
Sheng Yang14394422008-04-28 12:24:45 +08001645 *hw_cr0 &= ~X86_CR0_WP;
1646 } else if (!is_paging(vcpu)) {
1647 /* From nonpaging to paging */
1648 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001649 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001650 ~(CPU_BASED_CR3_LOAD_EXITING |
1651 CPU_BASED_CR3_STORE_EXITING));
1652 vcpu->arch.cr0 = cr0;
1653 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1654 if (!(vcpu->arch.cr0 & X86_CR0_WP))
1655 *hw_cr0 &= ~X86_CR0_WP;
1656 }
1657}
1658
1659static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1660 struct kvm_vcpu *vcpu)
1661{
1662 if (!is_paging(vcpu)) {
1663 *hw_cr4 &= ~X86_CR4_PAE;
1664 *hw_cr4 |= X86_CR4_PSE;
1665 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1666 *hw_cr4 &= ~X86_CR4_PAE;
1667}
1668
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1670{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001671 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001672 unsigned long hw_cr0;
1673
1674 if (enable_unrestricted_guest)
1675 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
1676 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
1677 else
1678 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08001679
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001680 vmx_fpu_deactivate(vcpu);
1681
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001682 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001683 enter_pmode(vcpu);
1684
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001685 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001686 enter_rmode(vcpu);
1687
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001688#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001689 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001690 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10001692 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693 exit_lmode(vcpu);
1694 }
1695#endif
1696
Avi Kivity089d0342009-03-23 18:26:32 +02001697 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001698 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1699
Avi Kivity6aa8b732006-12-10 02:21:36 -08001700 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001701 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001702 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001703
Rusty Russell707d92f2007-07-17 23:19:08 +10001704 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001705 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706}
1707
Sheng Yang14394422008-04-28 12:24:45 +08001708static u64 construct_eptp(unsigned long root_hpa)
1709{
1710 u64 eptp;
1711
1712 /* TODO write the value reading from MSR */
1713 eptp = VMX_EPT_DEFAULT_MT |
1714 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1715 eptp |= (root_hpa & PAGE_MASK);
1716
1717 return eptp;
1718}
1719
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1721{
Sheng Yang14394422008-04-28 12:24:45 +08001722 unsigned long guest_cr3;
1723 u64 eptp;
1724
1725 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02001726 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001727 eptp = construct_eptp(cr3);
1728 vmcs_write64(EPT_POINTER, eptp);
Sheng Yang14394422008-04-28 12:24:45 +08001729 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
Sheng Yangb927a3c2009-07-21 10:42:48 +08001730 vcpu->kvm->arch.ept_identity_map_addr;
Sheng Yang14394422008-04-28 12:24:45 +08001731 }
1732
Sheng Yang2384d2b2008-01-17 15:14:33 +08001733 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001734 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001735 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001736 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001737}
1738
1739static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1740{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001741 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08001742 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1743
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001744 vcpu->arch.cr4 = cr4;
Avi Kivity089d0342009-03-23 18:26:32 +02001745 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001746 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1747
1748 vmcs_writel(CR4_READ_SHADOW, cr4);
1749 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750}
1751
Avi Kivity6aa8b732006-12-10 02:21:36 -08001752static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1753{
1754 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1755
1756 return vmcs_readl(sf->base);
1757}
1758
1759static void vmx_get_segment(struct kvm_vcpu *vcpu,
1760 struct kvm_segment *var, int seg)
1761{
1762 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1763 u32 ar;
1764
1765 var->base = vmcs_readl(sf->base);
1766 var->limit = vmcs_read32(sf->limit);
1767 var->selector = vmcs_read16(sf->selector);
1768 ar = vmcs_read32(sf->ar_bytes);
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02001769 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001770 ar = 0;
1771 var->type = ar & 15;
1772 var->s = (ar >> 4) & 1;
1773 var->dpl = (ar >> 5) & 3;
1774 var->present = (ar >> 7) & 1;
1775 var->avl = (ar >> 12) & 1;
1776 var->l = (ar >> 13) & 1;
1777 var->db = (ar >> 14) & 1;
1778 var->g = (ar >> 15) & 1;
1779 var->unusable = (ar >> 16) & 1;
1780}
1781
Izik Eidus2e4d2652008-03-24 19:38:34 +02001782static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1783{
Izik Eidus2e4d2652008-03-24 19:38:34 +02001784 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1785 return 0;
1786
1787 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1788 return 3;
1789
Avi Kivityeab4b8a2009-08-04 15:02:54 +03001790 return vmcs_read16(GUEST_CS_SELECTOR) & 3;
Izik Eidus2e4d2652008-03-24 19:38:34 +02001791}
1792
Avi Kivity653e3102007-05-07 10:55:37 +03001793static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001794{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001795 u32 ar;
1796
Avi Kivity653e3102007-05-07 10:55:37 +03001797 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001798 ar = 1 << 16;
1799 else {
1800 ar = var->type & 15;
1801 ar |= (var->s & 1) << 4;
1802 ar |= (var->dpl & 3) << 5;
1803 ar |= (var->present & 1) << 7;
1804 ar |= (var->avl & 1) << 12;
1805 ar |= (var->l & 1) << 13;
1806 ar |= (var->db & 1) << 14;
1807 ar |= (var->g & 1) << 15;
1808 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001809 if (ar == 0) /* a 0 value means unusable */
1810 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001811
1812 return ar;
1813}
1814
1815static void vmx_set_segment(struct kvm_vcpu *vcpu,
1816 struct kvm_segment *var, int seg)
1817{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001818 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03001819 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1820 u32 ar;
1821
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001822 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
1823 vmx->rmode.tr.selector = var->selector;
1824 vmx->rmode.tr.base = var->base;
1825 vmx->rmode.tr.limit = var->limit;
1826 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001827 return;
1828 }
1829 vmcs_writel(sf->base, var->base);
1830 vmcs_write32(sf->limit, var->limit);
1831 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001832 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001833 /*
1834 * Hack real-mode segments into vm86 compatibility.
1835 */
1836 if (var->base == 0xffff0000 && var->selector == 0xf000)
1837 vmcs_writel(sf->base, 0xf0000);
1838 ar = 0xf3;
1839 } else
1840 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001841
1842 /*
1843 * Fix the "Accessed" bit in AR field of segment registers for older
1844 * qemu binaries.
1845 * IA32 arch specifies that at the time of processor reset the
1846 * "Accessed" bit in the AR field of segment registers is 1. And qemu
1847 * is setting it to 0 in the usedland code. This causes invalid guest
1848 * state vmexit when "unrestricted guest" mode is turned on.
1849 * Fix for this setup issue in cpu_reset is being pushed in the qemu
1850 * tree. Newer qemu binaries with that qemu fix would not need this
1851 * kvm hack.
1852 */
1853 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
1854 ar |= 0x1; /* Accessed */
1855
Avi Kivity6aa8b732006-12-10 02:21:36 -08001856 vmcs_write32(sf->ar_bytes, ar);
1857}
1858
Avi Kivity6aa8b732006-12-10 02:21:36 -08001859static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1860{
1861 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1862
1863 *db = (ar >> 14) & 1;
1864 *l = (ar >> 13) & 1;
1865}
1866
1867static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1868{
1869 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1870 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1871}
1872
1873static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1874{
1875 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1876 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1877}
1878
1879static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1880{
1881 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1882 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1883}
1884
1885static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1886{
1887 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1888 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1889}
1890
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001891static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1892{
1893 struct kvm_segment var;
1894 u32 ar;
1895
1896 vmx_get_segment(vcpu, &var, seg);
1897 ar = vmx_segment_access_rights(&var);
1898
1899 if (var.base != (var.selector << 4))
1900 return false;
1901 if (var.limit != 0xffff)
1902 return false;
1903 if (ar != 0xf3)
1904 return false;
1905
1906 return true;
1907}
1908
1909static bool code_segment_valid(struct kvm_vcpu *vcpu)
1910{
1911 struct kvm_segment cs;
1912 unsigned int cs_rpl;
1913
1914 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1915 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1916
Avi Kivity1872a3f2009-01-04 23:26:52 +02001917 if (cs.unusable)
1918 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001919 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1920 return false;
1921 if (!cs.s)
1922 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001923 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001924 if (cs.dpl > cs_rpl)
1925 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001926 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001927 if (cs.dpl != cs_rpl)
1928 return false;
1929 }
1930 if (!cs.present)
1931 return false;
1932
1933 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1934 return true;
1935}
1936
1937static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1938{
1939 struct kvm_segment ss;
1940 unsigned int ss_rpl;
1941
1942 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1943 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1944
Avi Kivity1872a3f2009-01-04 23:26:52 +02001945 if (ss.unusable)
1946 return true;
1947 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001948 return false;
1949 if (!ss.s)
1950 return false;
1951 if (ss.dpl != ss_rpl) /* DPL != RPL */
1952 return false;
1953 if (!ss.present)
1954 return false;
1955
1956 return true;
1957}
1958
1959static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1960{
1961 struct kvm_segment var;
1962 unsigned int rpl;
1963
1964 vmx_get_segment(vcpu, &var, seg);
1965 rpl = var.selector & SELECTOR_RPL_MASK;
1966
Avi Kivity1872a3f2009-01-04 23:26:52 +02001967 if (var.unusable)
1968 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001969 if (!var.s)
1970 return false;
1971 if (!var.present)
1972 return false;
1973 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
1974 if (var.dpl < rpl) /* DPL < RPL */
1975 return false;
1976 }
1977
1978 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1979 * rights flags
1980 */
1981 return true;
1982}
1983
1984static bool tr_valid(struct kvm_vcpu *vcpu)
1985{
1986 struct kvm_segment tr;
1987
1988 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
1989
Avi Kivity1872a3f2009-01-04 23:26:52 +02001990 if (tr.unusable)
1991 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001992 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1993 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001994 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001995 return false;
1996 if (!tr.present)
1997 return false;
1998
1999 return true;
2000}
2001
2002static bool ldtr_valid(struct kvm_vcpu *vcpu)
2003{
2004 struct kvm_segment ldtr;
2005
2006 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2007
Avi Kivity1872a3f2009-01-04 23:26:52 +02002008 if (ldtr.unusable)
2009 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002010 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2011 return false;
2012 if (ldtr.type != 2)
2013 return false;
2014 if (!ldtr.present)
2015 return false;
2016
2017 return true;
2018}
2019
2020static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2021{
2022 struct kvm_segment cs, ss;
2023
2024 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2025 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2026
2027 return ((cs.selector & SELECTOR_RPL_MASK) ==
2028 (ss.selector & SELECTOR_RPL_MASK));
2029}
2030
2031/*
2032 * Check if guest state is valid. Returns true if valid, false if
2033 * not.
2034 * We assume that registers are always usable
2035 */
2036static bool guest_state_valid(struct kvm_vcpu *vcpu)
2037{
2038 /* real mode guest state checks */
2039 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
2040 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2041 return false;
2042 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2043 return false;
2044 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2045 return false;
2046 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2047 return false;
2048 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2049 return false;
2050 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2051 return false;
2052 } else {
2053 /* protected mode guest state checks */
2054 if (!cs_ss_rpl_check(vcpu))
2055 return false;
2056 if (!code_segment_valid(vcpu))
2057 return false;
2058 if (!stack_segment_valid(vcpu))
2059 return false;
2060 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2061 return false;
2062 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2063 return false;
2064 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2065 return false;
2066 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2067 return false;
2068 if (!tr_valid(vcpu))
2069 return false;
2070 if (!ldtr_valid(vcpu))
2071 return false;
2072 }
2073 /* TODO:
2074 * - Add checks on RIP
2075 * - Add checks on RFLAGS
2076 */
2077
2078 return true;
2079}
2080
Mike Dayd77c26f2007-10-08 09:02:08 -04002081static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002082{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002083 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002084 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002085 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02002086 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087
Izik Eidus195aefd2007-10-01 22:14:18 +02002088 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2089 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002090 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002091 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002092 r = kvm_write_guest_page(kvm, fn++, &data,
2093 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002094 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002095 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002096 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2097 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002098 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002099 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2100 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002101 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002102 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002103 r = kvm_write_guest_page(kvm, fn, &data,
2104 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2105 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002106 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002107 goto out;
2108
2109 ret = 1;
2110out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002111 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112}
2113
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002114static int init_rmode_identity_map(struct kvm *kvm)
2115{
2116 int i, r, ret;
2117 pfn_t identity_map_pfn;
2118 u32 tmp;
2119
Avi Kivity089d0342009-03-23 18:26:32 +02002120 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002121 return 1;
2122 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2123 printk(KERN_ERR "EPT: identity-mapping pagetable "
2124 "haven't been allocated!\n");
2125 return 0;
2126 }
2127 if (likely(kvm->arch.ept_identity_pagetable_done))
2128 return 1;
2129 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002130 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002131 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2132 if (r < 0)
2133 goto out;
2134 /* Set up identity-mapping pagetable for EPT in real mode */
2135 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2136 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2137 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2138 r = kvm_write_guest_page(kvm, identity_map_pfn,
2139 &tmp, i * sizeof(tmp), sizeof(tmp));
2140 if (r < 0)
2141 goto out;
2142 }
2143 kvm->arch.ept_identity_pagetable_done = true;
2144 ret = 1;
2145out:
2146 return ret;
2147}
2148
Avi Kivity6aa8b732006-12-10 02:21:36 -08002149static void seg_setup(int seg)
2150{
2151 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002152 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153
2154 vmcs_write16(sf->selector, 0);
2155 vmcs_writel(sf->base, 0);
2156 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002157 if (enable_unrestricted_guest) {
2158 ar = 0x93;
2159 if (seg == VCPU_SREG_CS)
2160 ar |= 0x08; /* code segment */
2161 } else
2162 ar = 0xf3;
2163
2164 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165}
2166
Sheng Yangf78e0e22007-10-29 09:40:42 +08002167static int alloc_apic_access_page(struct kvm *kvm)
2168{
2169 struct kvm_userspace_memory_region kvm_userspace_mem;
2170 int r = 0;
2171
Izik Eidus72dc67a2008-02-10 18:04:15 +02002172 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002173 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002174 goto out;
2175 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2176 kvm_userspace_mem.flags = 0;
2177 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2178 kvm_userspace_mem.memory_size = PAGE_SIZE;
2179 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2180 if (r)
2181 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002182
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002183 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002184out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002185 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002186 return r;
2187}
2188
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002189static int alloc_identity_pagetable(struct kvm *kvm)
2190{
2191 struct kvm_userspace_memory_region kvm_userspace_mem;
2192 int r = 0;
2193
2194 down_write(&kvm->slots_lock);
2195 if (kvm->arch.ept_identity_pagetable)
2196 goto out;
2197 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2198 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002199 kvm_userspace_mem.guest_phys_addr =
2200 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002201 kvm_userspace_mem.memory_size = PAGE_SIZE;
2202 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2203 if (r)
2204 goto out;
2205
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002206 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
Sheng Yangb927a3c2009-07-21 10:42:48 +08002207 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002208out:
2209 up_write(&kvm->slots_lock);
2210 return r;
2211}
2212
Sheng Yang2384d2b2008-01-17 15:14:33 +08002213static void allocate_vpid(struct vcpu_vmx *vmx)
2214{
2215 int vpid;
2216
2217 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002218 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002219 return;
2220 spin_lock(&vmx_vpid_lock);
2221 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2222 if (vpid < VMX_NR_VPIDS) {
2223 vmx->vpid = vpid;
2224 __set_bit(vpid, vmx_vpid_bitmap);
2225 }
2226 spin_unlock(&vmx_vpid_lock);
2227}
2228
Avi Kivity58972972009-02-24 22:26:47 +02002229static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002230{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002231 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002232
2233 if (!cpu_has_vmx_msr_bitmap())
2234 return;
2235
2236 /*
2237 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2238 * have the write-low and read-high bitmap offsets the wrong way round.
2239 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2240 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002241 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002242 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2243 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002244 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2245 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002246 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2247 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002248 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002249}
2250
Avi Kivity58972972009-02-24 22:26:47 +02002251static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2252{
2253 if (!longmode_only)
2254 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2255 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2256}
2257
Avi Kivity6aa8b732006-12-10 02:21:36 -08002258/*
2259 * Sets up the vmcs for emulated real mode.
2260 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002261static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262{
Sheng Yang468d4722008-10-09 16:01:55 +08002263 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002264 u32 junk;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002265 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266 unsigned long a;
2267 struct descriptor_table dt;
2268 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002269 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002270 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271
Avi Kivity6aa8b732006-12-10 02:21:36 -08002272 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002273 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2274 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002275
Sheng Yang25c5f222008-03-28 13:18:56 +08002276 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002277 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002278
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2280
Avi Kivity6aa8b732006-12-10 02:21:36 -08002281 /* Control */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002282 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2283 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002284
2285 exec_control = vmcs_config.cpu_based_exec_ctrl;
2286 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2287 exec_control &= ~CPU_BASED_TPR_SHADOW;
2288#ifdef CONFIG_X86_64
2289 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2290 CPU_BASED_CR8_LOAD_EXITING;
2291#endif
2292 }
Avi Kivity089d0342009-03-23 18:26:32 +02002293 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002294 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002295 CPU_BASED_CR3_LOAD_EXITING |
2296 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002297 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298
Sheng Yang83ff3b92007-11-21 14:33:25 +08002299 if (cpu_has_secondary_exec_ctrls()) {
2300 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2301 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2302 exec_control &=
2303 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002304 if (vmx->vpid == 0)
2305 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Avi Kivity089d0342009-03-23 18:26:32 +02002306 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002307 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002308 if (!enable_unrestricted_guest)
2309 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002310 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2311 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002312
Avi Kivityc7addb92007-09-16 18:58:32 +02002313 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2314 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002315 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2316
2317 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2318 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2319 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2320
2321 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2322 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2323 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002324 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2325 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002327#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002328 rdmsrl(MSR_FS_BASE, a);
2329 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2330 rdmsrl(MSR_GS_BASE, a);
2331 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2332#else
2333 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2334 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2335#endif
2336
2337 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2338
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002339 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002340 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2341
Mike Dayd77c26f2007-10-08 09:02:08 -04002342 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002343 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002344 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2345 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2346 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347
2348 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2349 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2350 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2351 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2352 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2353 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2354
Sheng Yang468d4722008-10-09 16:01:55 +08002355 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2356 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2357 host_pat = msr_low | ((u64) msr_high << 32);
2358 vmcs_write64(HOST_IA32_PAT, host_pat);
2359 }
2360 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2361 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2362 host_pat = msr_low | ((u64) msr_high << 32);
2363 /* Write the default value follow host pat */
2364 vmcs_write64(GUEST_IA32_PAT, host_pat);
2365 /* Keep arch.pat sync with GUEST_IA32_PAT */
2366 vmx->vcpu.arch.pat = host_pat;
2367 }
2368
Avi Kivity6aa8b732006-12-10 02:21:36 -08002369 for (i = 0; i < NR_VMX_MSR; ++i) {
2370 u32 index = vmx_msr_index[i];
2371 u32 data_low, data_high;
2372 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002373 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374
2375 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2376 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002377 if (wrmsr_safe(index, data_low, data_high) < 0)
2378 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002380 vmx->host_msrs[j].index = index;
2381 vmx->host_msrs[j].reserved = 0;
2382 vmx->host_msrs[j].data = data;
2383 vmx->guest_msrs[j] = vmx->host_msrs[j];
2384 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002387 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002388
2389 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002390 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2391
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002392 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2393 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2394
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002395 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2396 rdtscll(tsc_this);
2397 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2398 tsc_base = tsc_this;
2399
2400 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002401
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002402 return 0;
2403}
2404
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002405static int init_rmode(struct kvm *kvm)
2406{
2407 if (!init_rmode_tss(kvm))
2408 return 0;
2409 if (!init_rmode_identity_map(kvm))
2410 return 0;
2411 return 1;
2412}
2413
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002414static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2415{
2416 struct vcpu_vmx *vmx = to_vmx(vcpu);
2417 u64 msr;
2418 int ret;
2419
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002420 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002421 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002422 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002423 ret = -ENOMEM;
2424 goto out;
2425 }
2426
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002427 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002428
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002429 vmx->soft_vnmi_blocked = 0;
2430
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002431 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002432 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002433 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002434 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002435 msr |= MSR_IA32_APICBASE_BSP;
2436 kvm_set_apic_base(&vmx->vcpu, msr);
2437
2438 fx_init(&vmx->vcpu);
2439
Avi Kivity5706be02008-08-20 15:07:31 +03002440 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002441 /*
2442 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2443 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2444 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002445 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002446 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2447 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2448 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002449 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2450 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002451 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002452
2453 seg_setup(VCPU_SREG_DS);
2454 seg_setup(VCPU_SREG_ES);
2455 seg_setup(VCPU_SREG_FS);
2456 seg_setup(VCPU_SREG_GS);
2457 seg_setup(VCPU_SREG_SS);
2458
2459 vmcs_write16(GUEST_TR_SELECTOR, 0);
2460 vmcs_writel(GUEST_TR_BASE, 0);
2461 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2462 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2463
2464 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2465 vmcs_writel(GUEST_LDTR_BASE, 0);
2466 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2467 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2468
2469 vmcs_write32(GUEST_SYSENTER_CS, 0);
2470 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2471 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2472
2473 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002474 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002475 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002476 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002477 kvm_rip_write(vcpu, 0);
2478 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002479
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002480 vmcs_writel(GUEST_DR7, 0x400);
2481
2482 vmcs_writel(GUEST_GDTR_BASE, 0);
2483 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2484
2485 vmcs_writel(GUEST_IDTR_BASE, 0);
2486 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2487
2488 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2489 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2490 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2491
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002492 /* Special registers */
2493 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2494
2495 setup_msrs(vmx);
2496
Avi Kivity6aa8b732006-12-10 02:21:36 -08002497 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2498
Sheng Yangf78e0e22007-10-29 09:40:42 +08002499 if (cpu_has_vmx_tpr_shadow()) {
2500 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2501 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2502 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002503 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002504 vmcs_write32(TPR_THRESHOLD, 0);
2505 }
2506
2507 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2508 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002509 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002510
Sheng Yang2384d2b2008-01-17 15:14:33 +08002511 if (vmx->vpid != 0)
2512 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2513
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002514 vmx->vcpu.arch.cr0 = 0x60000010;
2515 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002516 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002517 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002518 vmx_fpu_activate(&vmx->vcpu);
2519 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002520
Sheng Yang2384d2b2008-01-17 15:14:33 +08002521 vpid_sync_vcpu_all(vmx);
2522
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002523 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002524
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002525 /* HACK: Don't enable emulation on guest boot/reset */
2526 vmx->emulation_required = 0;
2527
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002529 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002530 return ret;
2531}
2532
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002533static void enable_irq_window(struct kvm_vcpu *vcpu)
2534{
2535 u32 cpu_based_vm_exec_control;
2536
2537 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2538 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2539 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2540}
2541
2542static void enable_nmi_window(struct kvm_vcpu *vcpu)
2543{
2544 u32 cpu_based_vm_exec_control;
2545
2546 if (!cpu_has_virtual_nmis()) {
2547 enable_irq_window(vcpu);
2548 return;
2549 }
2550
2551 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2552 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2553 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2554}
2555
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002556static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03002557{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002558 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002559 uint32_t intr;
2560 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02002561
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002562 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002563
Avi Kivityfa89a812008-09-01 15:57:51 +03002564 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002565 if (vmx->rmode.vm86_active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002566 vmx->rmode.irq.pending = true;
2567 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002568 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +03002569 if (vcpu->arch.interrupt.soft)
2570 vmx->rmode.irq.rip +=
2571 vmx->vcpu.arch.event_exit_inst_len;
Avi Kivity9c5623e2007-11-08 18:19:20 +02002572 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2573 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2574 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002575 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002576 return;
2577 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002578 intr = irq | INTR_INFO_VALID_MASK;
2579 if (vcpu->arch.interrupt.soft) {
2580 intr |= INTR_TYPE_SOFT_INTR;
2581 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2582 vmx->vcpu.arch.event_exit_inst_len);
2583 } else
2584 intr |= INTR_TYPE_EXT_INTR;
2585 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03002586}
2587
Sheng Yangf08864b2008-05-15 18:23:25 +08002588static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2589{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002590 struct vcpu_vmx *vmx = to_vmx(vcpu);
2591
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002592 if (!cpu_has_virtual_nmis()) {
2593 /*
2594 * Tracking the NMI-blocked state in software is built upon
2595 * finding the next open IRQ window. This, in turn, depends on
2596 * well-behaving guests: They have to keep IRQs disabled at
2597 * least as long as the NMI handler runs. Otherwise we may
2598 * cause NMI nesting, maybe breaking the guest. But as this is
2599 * highly unlikely, we can live with the residual risk.
2600 */
2601 vmx->soft_vnmi_blocked = 1;
2602 vmx->vnmi_blocked_time = 0;
2603 }
2604
Jan Kiszka487b3912008-09-26 09:30:56 +02002605 ++vcpu->stat.nmi_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002606 if (vmx->rmode.vm86_active) {
Jan Kiszka66a5a342008-09-26 09:30:51 +02002607 vmx->rmode.irq.pending = true;
2608 vmx->rmode.irq.vector = NMI_VECTOR;
2609 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2610 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2611 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2612 INTR_INFO_VALID_MASK);
2613 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2614 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2615 return;
2616 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002617 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2618 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002619}
2620
Gleb Natapovc4282df2009-04-21 17:45:07 +03002621static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02002622{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002623 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03002624 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002625
Gleb Natapovc4282df2009-04-21 17:45:07 +03002626 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2627 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS |
2628 GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02002629}
2630
Gleb Natapov78646122009-03-23 12:12:11 +02002631static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
2632{
Gleb Natapovc4282df2009-04-21 17:45:07 +03002633 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2634 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2635 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02002636}
2637
Izik Eiduscbc94022007-10-25 00:29:55 +02002638static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2639{
2640 int ret;
2641 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002642 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002643 .guest_phys_addr = addr,
2644 .memory_size = PAGE_SIZE * 3,
2645 .flags = 0,
2646 };
2647
2648 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2649 if (ret)
2650 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002651 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002652 return 0;
2653}
2654
Avi Kivity6aa8b732006-12-10 02:21:36 -08002655static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2656 int vec, u32 err_code)
2657{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002658 /*
2659 * Instruction with address size override prefix opcode 0x67
2660 * Cause the #SS fault with 0 error code in VM86 mode.
2661 */
2662 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02002663 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002664 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002665 /*
2666 * Forward all other exceptions that are valid in real mode.
2667 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2668 * the required debugging infrastructure rework.
2669 */
2670 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002671 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002672 if (vcpu->guest_debug &
2673 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2674 return 0;
2675 kvm_queue_exception(vcpu, vec);
2676 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002677 case BP_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002678 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2679 return 0;
2680 /* fall through */
2681 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002682 case OF_VECTOR:
2683 case BR_VECTOR:
2684 case UD_VECTOR:
2685 case DF_VECTOR:
2686 case SS_VECTOR:
2687 case GP_VECTOR:
2688 case MF_VECTOR:
2689 kvm_queue_exception(vcpu, vec);
2690 return 1;
2691 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692 return 0;
2693}
2694
Andi Kleena0861c02009-06-08 17:37:09 +08002695/*
2696 * Trigger machine check on the host. We assume all the MSRs are already set up
2697 * by the CPU and that we still run on the same CPU as the MCE occurred on.
2698 * We pass a fake environment to the machine check handler because we want
2699 * the guest to be always treated like user space, no matter what context
2700 * it used internally.
2701 */
2702static void kvm_machine_check(void)
2703{
2704#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
2705 struct pt_regs regs = {
2706 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
2707 .flags = X86_EFLAGS_IF,
2708 };
2709
2710 do_machine_check(&regs, 0);
2711#endif
2712}
2713
2714static int handle_machine_check(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2715{
2716 /* already handled by vcpu_run */
2717 return 1;
2718}
2719
Avi Kivity6aa8b732006-12-10 02:21:36 -08002720static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2721{
Avi Kivity1155f762007-11-22 11:30:47 +02002722 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002723 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002724 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725 u32 vect_info;
2726 enum emulation_result er;
2727
Avi Kivity1155f762007-11-22 11:30:47 +02002728 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002729 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2730
Andi Kleena0861c02009-06-08 17:37:09 +08002731 if (is_machine_check(intr_info))
2732 return handle_machine_check(vcpu, kvm_run);
2733
Avi Kivity6aa8b732006-12-10 02:21:36 -08002734 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002735 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002736 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002737 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002738
Jan Kiszkae4a41882008-09-26 09:30:46 +02002739 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002740 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002741
2742 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002743 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002744 return 1;
2745 }
2746
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002747 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08002748 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002749 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002750 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002751 return 1;
2752 }
2753
Avi Kivity6aa8b732006-12-10 02:21:36 -08002754 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002755 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002756 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002757 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2758 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002759 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02002760 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002761 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002763 trace_kvm_page_fault(cr2, error_code);
2764
Gleb Natapov3298b752009-05-11 13:35:46 +03002765 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03002766 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002767 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002768 }
2769
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002770 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002772 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002773 if (vcpu->arch.halt_request) {
2774 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002775 return kvm_emulate_halt(vcpu);
2776 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002778 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002780 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002781 switch (ex_no) {
2782 case DB_VECTOR:
2783 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2784 if (!(vcpu->guest_debug &
2785 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2786 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2787 kvm_queue_exception(vcpu, DB_VECTOR);
2788 return 1;
2789 }
2790 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2791 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2792 /* fall through */
2793 case BP_VECTOR:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002794 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002795 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2796 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002797 break;
2798 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002799 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2800 kvm_run->ex.exception = ex_no;
2801 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002802 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002803 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804 return 0;
2805}
2806
2807static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2808 struct kvm_run *kvm_run)
2809{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002810 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811 return 1;
2812}
2813
Avi Kivity988ad742007-02-12 00:54:36 -08002814static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2815{
2816 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2817 return 0;
2818}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819
Avi Kivity6aa8b732006-12-10 02:21:36 -08002820static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2821{
He, Qingbfdaab02007-09-12 14:18:28 +08002822 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01002823 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002824 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825
Avi Kivity1165f5f2007-04-19 17:27:43 +03002826 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002827 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002828 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002829
2830 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002831 if (emulate_instruction(vcpu,
2832 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002833 return 0;
2834 return 1;
2835 }
2836
2837 size = (exit_qualification & 7) + 1;
2838 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002839 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002840
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002841 skip_emulated_instruction(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002842 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843}
2844
Ingo Molnar102d8322007-02-19 14:37:47 +02002845static void
2846vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2847{
2848 /*
2849 * Patch in the VMCALL instruction:
2850 */
2851 hypercall[0] = 0x0f;
2852 hypercall[1] = 0x01;
2853 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002854}
2855
Avi Kivity6aa8b732006-12-10 02:21:36 -08002856static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2857{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002858 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002859 int cr;
2860 int reg;
2861
He, Qingbfdaab02007-09-12 14:18:28 +08002862 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863 cr = exit_qualification & 15;
2864 reg = (exit_qualification >> 8) & 15;
2865 switch ((exit_qualification >> 4) & 3) {
2866 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002867 val = kvm_register_read(vcpu, reg);
2868 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869 switch (cr) {
2870 case 0:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002871 kvm_set_cr0(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002872 skip_emulated_instruction(vcpu);
2873 return 1;
2874 case 3:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002875 kvm_set_cr3(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002876 skip_emulated_instruction(vcpu);
2877 return 1;
2878 case 4:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002879 kvm_set_cr4(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002880 skip_emulated_instruction(vcpu);
2881 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002882 case 8: {
2883 u8 cr8_prev = kvm_get_cr8(vcpu);
2884 u8 cr8 = kvm_register_read(vcpu, reg);
2885 kvm_set_cr8(vcpu, cr8);
2886 skip_emulated_instruction(vcpu);
2887 if (irqchip_in_kernel(vcpu->kvm))
2888 return 1;
2889 if (cr8_prev <= cr8)
2890 return 1;
2891 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2892 return 0;
2893 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002894 };
2895 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002896 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002897 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002898 vcpu->arch.cr0 &= ~X86_CR0_TS;
2899 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002900 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002901 skip_emulated_instruction(vcpu);
2902 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002903 case 1: /*mov from cr*/
2904 switch (cr) {
2905 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002906 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002907 trace_kvm_cr_read(cr, vcpu->arch.cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908 skip_emulated_instruction(vcpu);
2909 return 1;
2910 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002911 val = kvm_get_cr8(vcpu);
2912 kvm_register_write(vcpu, reg, val);
2913 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002914 skip_emulated_instruction(vcpu);
2915 return 1;
2916 }
2917 break;
2918 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002919 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002920
2921 skip_emulated_instruction(vcpu);
2922 return 1;
2923 default:
2924 break;
2925 }
2926 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002927 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002928 (int)(exit_qualification >> 4) & 3, cr);
2929 return 0;
2930}
2931
2932static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2933{
He, Qingbfdaab02007-09-12 14:18:28 +08002934 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002935 unsigned long val;
2936 int dr, reg;
2937
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002938 dr = vmcs_readl(GUEST_DR7);
2939 if (dr & DR7_GD) {
2940 /*
2941 * As the vm-exit takes precedence over the debug trap, we
2942 * need to emulate the latter, either for the host or the
2943 * guest debugging itself.
2944 */
2945 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
2946 kvm_run->debug.arch.dr6 = vcpu->arch.dr6;
2947 kvm_run->debug.arch.dr7 = dr;
2948 kvm_run->debug.arch.pc =
2949 vmcs_readl(GUEST_CS_BASE) +
2950 vmcs_readl(GUEST_RIP);
2951 kvm_run->debug.arch.exception = DB_VECTOR;
2952 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2953 return 0;
2954 } else {
2955 vcpu->arch.dr7 &= ~DR7_GD;
2956 vcpu->arch.dr6 |= DR6_BD;
2957 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2958 kvm_queue_exception(vcpu, DB_VECTOR);
2959 return 1;
2960 }
2961 }
2962
He, Qingbfdaab02007-09-12 14:18:28 +08002963 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002964 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
2965 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
2966 if (exit_qualification & TYPE_MOV_FROM_DR) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 switch (dr) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002968 case 0 ... 3:
2969 val = vcpu->arch.db[dr];
2970 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002971 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002972 val = vcpu->arch.dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002973 break;
2974 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002975 val = vcpu->arch.dr7;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002976 break;
2977 default:
2978 val = 0;
2979 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002980 kvm_register_write(vcpu, reg, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002981 } else {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002982 val = vcpu->arch.regs[reg];
2983 switch (dr) {
2984 case 0 ... 3:
2985 vcpu->arch.db[dr] = val;
2986 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
2987 vcpu->arch.eff_db[dr] = val;
2988 break;
2989 case 4 ... 5:
2990 if (vcpu->arch.cr4 & X86_CR4_DE)
2991 kvm_queue_exception(vcpu, UD_VECTOR);
2992 break;
2993 case 6:
2994 if (val & 0xffffffff00000000ULL) {
2995 kvm_queue_exception(vcpu, GP_VECTOR);
2996 break;
2997 }
2998 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
2999 break;
3000 case 7:
3001 if (val & 0xffffffff00000000ULL) {
3002 kvm_queue_exception(vcpu, GP_VECTOR);
3003 break;
3004 }
3005 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
3006 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
3007 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3008 vcpu->arch.switch_db_regs =
3009 (val & DR7_BP_EN_MASK);
3010 }
3011 break;
3012 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003013 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003014 skip_emulated_instruction(vcpu);
3015 return 1;
3016}
3017
3018static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3019{
Avi Kivity06465c52007-02-28 20:46:53 +02003020 kvm_emulate_cpuid(vcpu);
3021 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003022}
3023
3024static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3025{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003026 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003027 u64 data;
3028
3029 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003030 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003031 return 1;
3032 }
3033
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003034 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003035
Avi Kivity6aa8b732006-12-10 02:21:36 -08003036 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003037 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3038 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003039 skip_emulated_instruction(vcpu);
3040 return 1;
3041}
3042
3043static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3044{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003045 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3046 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3047 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003048
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003049 trace_kvm_msr_write(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003050
Avi Kivity6aa8b732006-12-10 02:21:36 -08003051 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003052 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003053 return 1;
3054 }
3055
3056 skip_emulated_instruction(vcpu);
3057 return 1;
3058}
3059
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003060static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
3061 struct kvm_run *kvm_run)
3062{
3063 return 1;
3064}
3065
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066static int handle_interrupt_window(struct kvm_vcpu *vcpu,
3067 struct kvm_run *kvm_run)
3068{
Eddie Dong85f455f2007-07-06 12:20:49 +03003069 u32 cpu_based_vm_exec_control;
3070
3071 /* clear pending irq */
3072 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3073 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3074 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003075
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003076 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003077
Dor Laorc1150d82007-01-05 16:36:24 -08003078 /*
3079 * If the user space waits to inject interrupts, exit as soon as
3080 * possible
3081 */
Gleb Natapov80618232009-04-21 17:44:56 +03003082 if (!irqchip_in_kernel(vcpu->kvm) &&
3083 kvm_run->request_interrupt_window &&
3084 !kvm_cpu_has_interrupt(vcpu)) {
Dor Laorc1150d82007-01-05 16:36:24 -08003085 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003086 return 0;
3087 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003088 return 1;
3089}
3090
3091static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3092{
3093 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003094 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003095}
3096
Ingo Molnarc21415e2007-02-19 14:37:47 +02003097static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3098{
Dor Laor510043d2007-02-19 18:25:43 +02003099 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003100 kvm_emulate_hypercall(vcpu);
3101 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003102}
3103
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003104static int handle_vmx_insn(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3105{
3106 kvm_queue_exception(vcpu, UD_VECTOR);
3107 return 1;
3108}
3109
Marcelo Tosattia7052892008-09-23 13:18:35 -03003110static int handle_invlpg(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3111{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003112 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003113
3114 kvm_mmu_invlpg(vcpu, exit_qualification);
3115 skip_emulated_instruction(vcpu);
3116 return 1;
3117}
3118
Eddie Donge5edaa02007-11-11 12:28:35 +02003119static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3120{
3121 skip_emulated_instruction(vcpu);
3122 /* TODO: Add support for VT-d/pass-through device */
3123 return 1;
3124}
3125
Sheng Yangf78e0e22007-10-29 09:40:42 +08003126static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3127{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003128 unsigned long exit_qualification;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003129 enum emulation_result er;
3130 unsigned long offset;
3131
Sheng Yangf9c617f2009-03-25 10:08:52 +08003132 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003133 offset = exit_qualification & 0xffful;
3134
3135 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3136
3137 if (er != EMULATE_DONE) {
3138 printk(KERN_ERR
3139 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3140 offset);
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003141 return -ENOEXEC;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003142 }
3143 return 1;
3144}
3145
Izik Eidus37817f22008-03-24 23:14:53 +02003146static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3147{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003148 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003149 unsigned long exit_qualification;
3150 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003151 int reason, type, idt_v;
3152
3153 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3154 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003155
3156 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3157
3158 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003159 if (reason == TASK_SWITCH_GATE && idt_v) {
3160 switch (type) {
3161 case INTR_TYPE_NMI_INTR:
3162 vcpu->arch.nmi_injected = false;
3163 if (cpu_has_virtual_nmis())
3164 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3165 GUEST_INTR_STATE_NMI);
3166 break;
3167 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003168 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003169 kvm_clear_interrupt_queue(vcpu);
3170 break;
3171 case INTR_TYPE_HARD_EXCEPTION:
3172 case INTR_TYPE_SOFT_EXCEPTION:
3173 kvm_clear_exception_queue(vcpu);
3174 break;
3175 default:
3176 break;
3177 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003178 }
Izik Eidus37817f22008-03-24 23:14:53 +02003179 tss_selector = exit_qualification;
3180
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003181 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3182 type != INTR_TYPE_EXT_INTR &&
3183 type != INTR_TYPE_NMI_INTR))
3184 skip_emulated_instruction(vcpu);
3185
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003186 if (!kvm_task_switch(vcpu, tss_selector, reason))
3187 return 0;
3188
3189 /* clear all local breakpoint enable flags */
3190 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3191
3192 /*
3193 * TODO: What about debug traps on tss switch?
3194 * Are we supposed to inject them and update dr6?
3195 */
3196
3197 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003198}
3199
Sheng Yang14394422008-04-28 12:24:45 +08003200static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3201{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003202 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003203 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003204 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003205
Sheng Yangf9c617f2009-03-25 10:08:52 +08003206 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003207
3208 if (exit_qualification & (1 << 6)) {
3209 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003210 return -EINVAL;
Sheng Yang14394422008-04-28 12:24:45 +08003211 }
3212
3213 gla_validity = (exit_qualification >> 7) & 0x3;
3214 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3215 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3216 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3217 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003218 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003219 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3220 (long unsigned int)exit_qualification);
3221 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity596ae892009-06-03 14:12:10 +03003222 kvm_run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
3223 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003224 }
3225
3226 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003227 trace_kvm_page_fault(gpa, exit_qualification);
Sheng Yang49cd7d22009-02-11 13:50:40 +08003228 return kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003229}
3230
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003231static u64 ept_rsvd_mask(u64 spte, int level)
3232{
3233 int i;
3234 u64 mask = 0;
3235
3236 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
3237 mask |= (1ULL << i);
3238
3239 if (level > 2)
3240 /* bits 7:3 reserved */
3241 mask |= 0xf8;
3242 else if (level == 2) {
3243 if (spte & (1ULL << 7))
3244 /* 2MB ref, bits 20:12 reserved */
3245 mask |= 0x1ff000;
3246 else
3247 /* bits 6:3 reserved */
3248 mask |= 0x78;
3249 }
3250
3251 return mask;
3252}
3253
3254static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
3255 int level)
3256{
3257 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
3258
3259 /* 010b (write-only) */
3260 WARN_ON((spte & 0x7) == 0x2);
3261
3262 /* 110b (write/execute) */
3263 WARN_ON((spte & 0x7) == 0x6);
3264
3265 /* 100b (execute-only) and value not supported by logical processor */
3266 if (!cpu_has_vmx_ept_execute_only())
3267 WARN_ON((spte & 0x7) == 0x4);
3268
3269 /* not 000b */
3270 if ((spte & 0x7)) {
3271 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
3272
3273 if (rsvd_bits != 0) {
3274 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
3275 __func__, rsvd_bits);
3276 WARN_ON(1);
3277 }
3278
3279 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
3280 u64 ept_mem_type = (spte & 0x38) >> 3;
3281
3282 if (ept_mem_type == 2 || ept_mem_type == 3 ||
3283 ept_mem_type == 7) {
3284 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
3285 __func__, ept_mem_type);
3286 WARN_ON(1);
3287 }
3288 }
3289 }
3290}
3291
3292static int handle_ept_misconfig(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3293{
3294 u64 sptes[4];
3295 int nr_sptes, i;
3296 gpa_t gpa;
3297
3298 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3299
3300 printk(KERN_ERR "EPT: Misconfiguration.\n");
3301 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
3302
3303 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
3304
3305 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
3306 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
3307
3308 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3309 kvm_run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
3310
3311 return 0;
3312}
3313
Sheng Yangf08864b2008-05-15 18:23:25 +08003314static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3315{
3316 u32 cpu_based_vm_exec_control;
3317
3318 /* clear pending NMI */
3319 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3320 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3321 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3322 ++vcpu->stat.nmi_window_exits;
3323
3324 return 1;
3325}
3326
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003327static void handle_invalid_guest_state(struct kvm_vcpu *vcpu,
3328 struct kvm_run *kvm_run)
3329{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003330 struct vcpu_vmx *vmx = to_vmx(vcpu);
3331 enum emulation_result err = EMULATE_DONE;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003332
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003333 local_irq_enable();
Jan Kiszka34f0c1a2009-07-22 23:53:26 +02003334 preempt_enable();
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003335
3336 while (!guest_state_valid(vcpu)) {
3337 err = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3338
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003339 if (err == EMULATE_DO_MMIO)
3340 break;
3341
3342 if (err != EMULATE_DONE) {
3343 kvm_report_emulation_failure(vcpu, "emulation failure");
Jan Kiszka263799a2009-07-21 10:43:07 +02003344 break;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003345 }
3346
3347 if (signal_pending(current))
3348 break;
3349 if (need_resched())
3350 schedule();
3351 }
3352
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003353 preempt_disable();
Jan Kiszka34f0c1a2009-07-22 23:53:26 +02003354 local_irq_disable();
Avi Kivity8b3079a2009-01-05 12:10:54 +02003355
3356 vmx->invalid_state_emulation_result = err;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003357}
3358
Avi Kivity6aa8b732006-12-10 02:21:36 -08003359/*
3360 * The exit handlers return 1 if the exit was handled fully and guest execution
3361 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3362 * to be done to userspace and return 0.
3363 */
3364static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
3365 struct kvm_run *kvm_run) = {
3366 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3367 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003368 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003369 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003370 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003371 [EXIT_REASON_CR_ACCESS] = handle_cr,
3372 [EXIT_REASON_DR_ACCESS] = handle_dr,
3373 [EXIT_REASON_CPUID] = handle_cpuid,
3374 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3375 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3376 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3377 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003378 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003379 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003380 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3381 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3382 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3383 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3384 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3385 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3386 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3387 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3388 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003389 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3390 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003391 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003392 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08003393 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003394 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
3395 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003396};
3397
3398static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003399 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003400
3401/*
3402 * The guest has exited. See if we can fix it or if we need userspace
3403 * assistance.
3404 */
Avi Kivity6062d012009-03-23 17:35:17 +02003405static int vmx_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003406{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003407 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003408 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003409 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003410
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003411 trace_kvm_exit(exit_reason, kvm_rip_read(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003412
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003413 /* If we need to emulate an MMIO from handle_invalid_guest_state
3414 * we just return 0 */
Avi Kivity10f32d82009-01-05 00:53:19 +02003415 if (vmx->emulation_required && emulate_invalid_guest_state) {
3416 if (guest_state_valid(vcpu))
3417 vmx->emulation_required = 0;
Avi Kivity8b3079a2009-01-05 12:10:54 +02003418 return vmx->invalid_state_emulation_result != EMULATE_DO_MMIO;
Avi Kivity10f32d82009-01-05 00:53:19 +02003419 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003420
Sheng Yang14394422008-04-28 12:24:45 +08003421 /* Access CR3 don't cause VMExit in paging mode, so we need
3422 * to sync with guest real CR3. */
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03003423 if (enable_ept && is_paging(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08003424 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003425
Avi Kivity29bd8a72007-09-10 17:27:03 +03003426 if (unlikely(vmx->fail)) {
3427 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3428 kvm_run->fail_entry.hardware_entry_failure_reason
3429 = vmcs_read32(VM_INSTRUCTION_ERROR);
3430 return 0;
3431 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003432
Mike Dayd77c26f2007-10-08 09:02:08 -04003433 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003434 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003435 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3436 exit_reason != EXIT_REASON_TASK_SWITCH))
3437 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3438 "(0x%x) and exit reason is 0x%x\n",
3439 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003440
3441 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03003442 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003443 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003444 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003445 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003446 /*
3447 * This CPU don't support us in finding the end of an
3448 * NMI-blocked window if the guest runs with IRQs
3449 * disabled. So we pull the trigger after 1 s of
3450 * futile waiting, but inform the user about this.
3451 */
3452 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3453 "state on VCPU %d after 1 s timeout\n",
3454 __func__, vcpu->vcpu_id);
3455 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003456 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003457 }
3458
Avi Kivity6aa8b732006-12-10 02:21:36 -08003459 if (exit_reason < kvm_vmx_max_exit_handlers
3460 && kvm_vmx_exit_handlers[exit_reason])
3461 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
3462 else {
3463 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3464 kvm_run->hw.hardware_exit_reason = exit_reason;
3465 }
3466 return 0;
3467}
3468
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003469static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003470{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003471 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003472 vmcs_write32(TPR_THRESHOLD, 0);
3473 return;
3474 }
3475
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003476 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003477}
3478
Avi Kivitycf393f72008-07-01 16:20:21 +03003479static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3480{
3481 u32 exit_intr_info;
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003482 u32 idt_vectoring_info = vmx->idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003483 bool unblock_nmi;
3484 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003485 int type;
3486 bool idtv_info_valid;
Avi Kivitycf393f72008-07-01 16:20:21 +03003487
3488 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Gleb Natapov20f65982009-05-11 13:35:55 +03003489
Andi Kleena0861c02009-06-08 17:37:09 +08003490 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
3491
3492 /* Handle machine checks before interrupts are enabled */
3493 if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
3494 || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
3495 && is_machine_check(exit_intr_info)))
3496 kvm_machine_check();
3497
Gleb Natapov20f65982009-05-11 13:35:55 +03003498 /* We need to handle NMIs before interrupts are enabled */
3499 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003500 (exit_intr_info & INTR_INFO_VALID_MASK))
Gleb Natapov20f65982009-05-11 13:35:55 +03003501 asm("int $2");
Gleb Natapov20f65982009-05-11 13:35:55 +03003502
3503 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3504
Avi Kivitycf393f72008-07-01 16:20:21 +03003505 if (cpu_has_virtual_nmis()) {
3506 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3507 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3508 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003509 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03003510 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3511 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003512 * SDM 3: 23.2.2 (September 2008)
3513 * Bit 12 is undefined in any of the following cases:
3514 * If the VM exit sets the valid bit in the IDT-vectoring
3515 * information field.
3516 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03003517 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003518 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
3519 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03003520 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3521 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003522 } else if (unlikely(vmx->soft_vnmi_blocked))
3523 vmx->vnmi_blocked_time +=
3524 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003525
Gleb Natapov37b96e92009-03-30 16:03:13 +03003526 vmx->vcpu.arch.nmi_injected = false;
3527 kvm_clear_exception_queue(&vmx->vcpu);
3528 kvm_clear_interrupt_queue(&vmx->vcpu);
3529
3530 if (!idtv_info_valid)
3531 return;
3532
Avi Kivity668f6122008-07-02 09:28:55 +03003533 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3534 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003535
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003536 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003537 case INTR_TYPE_NMI_INTR:
3538 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03003539 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003540 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03003541 * Clear bit "block by NMI" before VM entry if a NMI
3542 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03003543 */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003544 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3545 GUEST_INTR_STATE_NMI);
3546 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003547 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003548 vmx->vcpu.arch.event_exit_inst_len =
3549 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3550 /* fall through */
3551 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03003552 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003553 u32 err = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3554 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03003555 } else
3556 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003557 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003558 case INTR_TYPE_SOFT_INTR:
3559 vmx->vcpu.arch.event_exit_inst_len =
3560 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3561 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003562 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003563 kvm_queue_interrupt(&vmx->vcpu, vector,
3564 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003565 break;
3566 default:
3567 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03003568 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003569}
3570
Avi Kivity9c8cba32007-11-22 11:42:59 +02003571/*
3572 * Failure to inject an interrupt should give us the information
3573 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3574 * when fetching the interrupt redirection bitmap in the real-mode
3575 * tss, this doesn't happen. So we do it ourselves.
3576 */
3577static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3578{
3579 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003580 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003581 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003582 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003583 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3584 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3585 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3586 return;
3587 }
3588 vmx->idt_vectoring_info =
3589 VECTORING_INFO_VALID_MASK
3590 | INTR_TYPE_EXT_INTR
3591 | vmx->rmode.irq.vector;
3592}
3593
Avi Kivityc8019492008-07-14 14:44:59 +03003594#ifdef CONFIG_X86_64
3595#define R "r"
3596#define Q "q"
3597#else
3598#define R "e"
3599#define Q "l"
3600#endif
3601
Avi Kivity04d2cc72007-09-10 18:10:54 +03003602static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003603{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003604 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitye6adf282007-04-30 16:07:54 +03003605
Avi Kivity8f5d5492009-05-31 18:41:29 +03003606 if (enable_ept && is_paging(vcpu)) {
3607 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3608 ept_load_pdptrs(vcpu);
3609 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003610 /* Record the guest's net vcpu time for enforced NMI injections. */
3611 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3612 vmx->entry_time = ktime_get();
3613
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003614 /* Handle invalid guest state instead of entering VMX */
3615 if (vmx->emulation_required && emulate_invalid_guest_state) {
3616 handle_invalid_guest_state(vcpu, kvm_run);
3617 return;
3618 }
3619
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003620 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3621 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3622 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3623 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3624
Gleb Natapov787ff732009-05-18 11:44:06 +03003625 /* When single-stepping over STI and MOV SS, we must clear the
3626 * corresponding interruptibility bits in the guest state. Otherwise
3627 * vmentry fails as it then expects bit 14 (BS) in pending debug
3628 * exceptions being set, but that's not correct for the guest debugging
3629 * case. */
3630 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3631 vmx_set_interrupt_shadow(vcpu, 0);
3632
Avi Kivitye6adf282007-04-30 16:07:54 +03003633 /*
3634 * Loading guest fpu may have cleared host cr0.ts
3635 */
3636 vmcs_writel(HOST_CR0, read_cr0());
3637
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003638 set_debugreg(vcpu->arch.dr6, 6);
3639
Mike Dayd77c26f2007-10-08 09:02:08 -04003640 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003641 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003642 "push %%"R"dx; push %%"R"bp;"
3643 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003644 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3645 "je 1f \n\t"
3646 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003647 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003648 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03003649 /* Reload cr2 if changed */
3650 "mov %c[cr2](%0), %%"R"ax \n\t"
3651 "mov %%cr2, %%"R"dx \n\t"
3652 "cmp %%"R"ax, %%"R"dx \n\t"
3653 "je 2f \n\t"
3654 "mov %%"R"ax, %%cr2 \n\t"
3655 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003656 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003657 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003658 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003659 "mov %c[rax](%0), %%"R"ax \n\t"
3660 "mov %c[rbx](%0), %%"R"bx \n\t"
3661 "mov %c[rdx](%0), %%"R"dx \n\t"
3662 "mov %c[rsi](%0), %%"R"si \n\t"
3663 "mov %c[rdi](%0), %%"R"di \n\t"
3664 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003665#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003666 "mov %c[r8](%0), %%r8 \n\t"
3667 "mov %c[r9](%0), %%r9 \n\t"
3668 "mov %c[r10](%0), %%r10 \n\t"
3669 "mov %c[r11](%0), %%r11 \n\t"
3670 "mov %c[r12](%0), %%r12 \n\t"
3671 "mov %c[r13](%0), %%r13 \n\t"
3672 "mov %c[r14](%0), %%r14 \n\t"
3673 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003674#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003675 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3676
Avi Kivity6aa8b732006-12-10 02:21:36 -08003677 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003678 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003679 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003680 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003681 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003682 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003683 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003684 "xchg %0, (%%"R"sp) \n\t"
3685 "mov %%"R"ax, %c[rax](%0) \n\t"
3686 "mov %%"R"bx, %c[rbx](%0) \n\t"
3687 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3688 "mov %%"R"dx, %c[rdx](%0) \n\t"
3689 "mov %%"R"si, %c[rsi](%0) \n\t"
3690 "mov %%"R"di, %c[rdi](%0) \n\t"
3691 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003692#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003693 "mov %%r8, %c[r8](%0) \n\t"
3694 "mov %%r9, %c[r9](%0) \n\t"
3695 "mov %%r10, %c[r10](%0) \n\t"
3696 "mov %%r11, %c[r11](%0) \n\t"
3697 "mov %%r12, %c[r12](%0) \n\t"
3698 "mov %%r13, %c[r13](%0) \n\t"
3699 "mov %%r14, %c[r14](%0) \n\t"
3700 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003701#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003702 "mov %%cr2, %%"R"ax \n\t"
3703 "mov %%"R"ax, %c[cr2](%0) \n\t"
3704
3705 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003706 "setbe %c[fail](%0) \n\t"
3707 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3708 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3709 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03003710 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003711 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3712 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3713 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3714 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3715 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3716 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3717 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003718#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003719 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3720 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3721 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3722 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3723 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3724 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3725 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3726 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003727#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003728 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003729 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003730 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003731#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003732 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3733#endif
3734 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003735
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03003736 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
3737 | (1 << VCPU_EXREG_PDPTR));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003738 vcpu->arch.regs_dirty = 0;
3739
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003740 get_debugreg(vcpu->arch.dr6, 6);
3741
Avi Kivity1155f762007-11-22 11:30:47 +02003742 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003743 if (vmx->rmode.irq.pending)
3744 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003745
Mike Dayd77c26f2007-10-08 09:02:08 -04003746 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003747 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003748
Avi Kivitycf393f72008-07-01 16:20:21 +03003749 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003750}
3751
Avi Kivityc8019492008-07-14 14:44:59 +03003752#undef R
3753#undef Q
3754
Avi Kivity6aa8b732006-12-10 02:21:36 -08003755static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3756{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003757 struct vcpu_vmx *vmx = to_vmx(vcpu);
3758
3759 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003760 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003761 free_vmcs(vmx->vmcs);
3762 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003763 }
3764}
3765
3766static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3767{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003768 struct vcpu_vmx *vmx = to_vmx(vcpu);
3769
Sheng Yang2384d2b2008-01-17 15:14:33 +08003770 spin_lock(&vmx_vpid_lock);
3771 if (vmx->vpid != 0)
3772 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3773 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003774 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003775 kfree(vmx->host_msrs);
3776 kfree(vmx->guest_msrs);
3777 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003778 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003779}
3780
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003781static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003782{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003783 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003784 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003785 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003786
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003787 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003788 return ERR_PTR(-ENOMEM);
3789
Sheng Yang2384d2b2008-01-17 15:14:33 +08003790 allocate_vpid(vmx);
3791
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003792 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3793 if (err)
3794 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003795
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003796 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003797 if (!vmx->guest_msrs) {
3798 err = -ENOMEM;
3799 goto uninit_vcpu;
3800 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003801
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003802 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3803 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003804 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003805
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003806 vmx->vmcs = alloc_vmcs();
3807 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003808 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003809
3810 vmcs_clear(vmx->vmcs);
3811
Avi Kivity15ad7142007-07-11 18:17:21 +03003812 cpu = get_cpu();
3813 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003814 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003815 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003816 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003817 if (err)
3818 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003819 if (vm_need_virtualize_apic_accesses(kvm))
3820 if (alloc_apic_access_page(kvm) != 0)
3821 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003822
Sheng Yangb927a3c2009-07-21 10:42:48 +08003823 if (enable_ept) {
3824 if (!kvm->arch.ept_identity_map_addr)
3825 kvm->arch.ept_identity_map_addr =
3826 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003827 if (alloc_identity_pagetable(kvm) != 0)
3828 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003829 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003830
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003831 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003832
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003833free_vmcs:
3834 free_vmcs(vmx->vmcs);
3835free_msrs:
3836 kfree(vmx->host_msrs);
3837free_guest_msrs:
3838 kfree(vmx->guest_msrs);
3839uninit_vcpu:
3840 kvm_vcpu_uninit(&vmx->vcpu);
3841free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003842 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003843 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003844}
3845
Yang, Sheng002c7f72007-07-31 14:23:01 +03003846static void __init vmx_check_processor_compat(void *rtn)
3847{
3848 struct vmcs_config vmcs_conf;
3849
3850 *(int *)rtn = 0;
3851 if (setup_vmcs_config(&vmcs_conf) < 0)
3852 *(int *)rtn = -EIO;
3853 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3854 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3855 smp_processor_id());
3856 *(int *)rtn = -EIO;
3857 }
3858}
3859
Sheng Yang67253af2008-04-25 10:20:22 +08003860static int get_ept_level(void)
3861{
3862 return VMX_EPT_DEFAULT_GAW + 1;
3863}
3864
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003865static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003866{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003867 u64 ret;
3868
Sheng Yang522c68c2009-04-27 20:35:43 +08003869 /* For VT-d and EPT combination
3870 * 1. MMIO: always map as UC
3871 * 2. EPT with VT-d:
3872 * a. VT-d without snooping control feature: can't guarantee the
3873 * result, try to trust guest.
3874 * b. VT-d with snooping control feature: snooping control feature of
3875 * VT-d engine can guarantee the cache correctness. Just set it
3876 * to WB to keep consistent with host. So the same as item 3.
3877 * 3. EPT without VT-d: always map as WB and set IGMT=1 to keep
3878 * consistent with host MTRR
3879 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003880 if (is_mmio)
3881 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08003882 else if (vcpu->kvm->arch.iommu_domain &&
3883 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
3884 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
3885 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003886 else
Sheng Yang522c68c2009-04-27 20:35:43 +08003887 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
3888 | VMX_EPT_IGMT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003889
3890 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08003891}
3892
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003893static const struct trace_print_flags vmx_exit_reasons_str[] = {
3894 { EXIT_REASON_EXCEPTION_NMI, "exception" },
3895 { EXIT_REASON_EXTERNAL_INTERRUPT, "ext_irq" },
3896 { EXIT_REASON_TRIPLE_FAULT, "triple_fault" },
3897 { EXIT_REASON_NMI_WINDOW, "nmi_window" },
3898 { EXIT_REASON_IO_INSTRUCTION, "io_instruction" },
3899 { EXIT_REASON_CR_ACCESS, "cr_access" },
3900 { EXIT_REASON_DR_ACCESS, "dr_access" },
3901 { EXIT_REASON_CPUID, "cpuid" },
3902 { EXIT_REASON_MSR_READ, "rdmsr" },
3903 { EXIT_REASON_MSR_WRITE, "wrmsr" },
3904 { EXIT_REASON_PENDING_INTERRUPT, "interrupt_window" },
3905 { EXIT_REASON_HLT, "halt" },
3906 { EXIT_REASON_INVLPG, "invlpg" },
3907 { EXIT_REASON_VMCALL, "hypercall" },
3908 { EXIT_REASON_TPR_BELOW_THRESHOLD, "tpr_below_thres" },
3909 { EXIT_REASON_APIC_ACCESS, "apic_access" },
3910 { EXIT_REASON_WBINVD, "wbinvd" },
3911 { EXIT_REASON_TASK_SWITCH, "task_switch" },
3912 { EXIT_REASON_EPT_VIOLATION, "ept_violation" },
3913 { -1, NULL }
3914};
3915
Joerg Roedel344f4142009-07-27 16:30:48 +02003916static bool vmx_gb_page_enable(void)
3917{
3918 return false;
3919}
3920
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003921static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003922 .cpu_has_kvm_support = cpu_has_kvm_support,
3923 .disabled_by_bios = vmx_disabled_by_bios,
3924 .hardware_setup = hardware_setup,
3925 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003926 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003927 .hardware_enable = hardware_enable,
3928 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08003929 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003930
3931 .vcpu_create = vmx_create_vcpu,
3932 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003933 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003934
Avi Kivity04d2cc72007-09-10 18:10:54 +03003935 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003936 .vcpu_load = vmx_vcpu_load,
3937 .vcpu_put = vmx_vcpu_put,
3938
3939 .set_guest_debug = set_guest_debug,
3940 .get_msr = vmx_get_msr,
3941 .set_msr = vmx_set_msr,
3942 .get_segment_base = vmx_get_segment_base,
3943 .get_segment = vmx_get_segment,
3944 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003945 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003946 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003947 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003948 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003949 .set_cr3 = vmx_set_cr3,
3950 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003951 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003952 .get_idt = vmx_get_idt,
3953 .set_idt = vmx_set_idt,
3954 .get_gdt = vmx_get_gdt,
3955 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003956 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003957 .get_rflags = vmx_get_rflags,
3958 .set_rflags = vmx_set_rflags,
3959
3960 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003961
Avi Kivity6aa8b732006-12-10 02:21:36 -08003962 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02003963 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003964 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003965 .set_interrupt_shadow = vmx_set_interrupt_shadow,
3966 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003967 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003968 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003969 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003970 .queue_exception = vmx_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003971 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003972 .nmi_allowed = vmx_nmi_allowed,
3973 .enable_nmi_window = enable_nmi_window,
3974 .enable_irq_window = enable_irq_window,
3975 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003976
Izik Eiduscbc94022007-10-25 00:29:55 +02003977 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003978 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003979 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003980
3981 .exit_reasons_str = vmx_exit_reasons_str,
Joerg Roedel344f4142009-07-27 16:30:48 +02003982 .gb_page_enable = vmx_gb_page_enable,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003983};
3984
3985static int __init vmx_init(void)
3986{
He, Qingfdef3ad2007-04-30 09:45:24 +03003987 int r;
3988
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003989 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03003990 if (!vmx_io_bitmap_a)
3991 return -ENOMEM;
3992
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003993 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03003994 if (!vmx_io_bitmap_b) {
3995 r = -ENOMEM;
3996 goto out;
3997 }
3998
Avi Kivity58972972009-02-24 22:26:47 +02003999 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
4000 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08004001 r = -ENOMEM;
4002 goto out1;
4003 }
4004
Avi Kivity58972972009-02-24 22:26:47 +02004005 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
4006 if (!vmx_msr_bitmap_longmode) {
4007 r = -ENOMEM;
4008 goto out2;
4009 }
4010
He, Qingfdef3ad2007-04-30 09:45:24 +03004011 /*
4012 * Allow direct access to the PC debug port (it is often used for I/O
4013 * delays, but the vmexits simply slow things down).
4014 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004015 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
4016 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004017
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004018 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004019
Avi Kivity58972972009-02-24 22:26:47 +02004020 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
4021 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08004022
Sheng Yang2384d2b2008-01-17 15:14:33 +08004023 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
4024
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004025 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004026 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02004027 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08004028
Avi Kivity58972972009-02-24 22:26:47 +02004029 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
4030 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
4031 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
4032 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
4033 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
4034 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03004035
Avi Kivity089d0342009-03-23 18:26:32 +02004036 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004037 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004038 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08004039 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08004040 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004041 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004042 kvm_enable_tdp();
4043 } else
4044 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08004045
Avi Kivityc7addb92007-09-16 18:58:32 +02004046 if (bypass_guest_pf)
4047 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
4048
Sheng Yang14394422008-04-28 12:24:45 +08004049 ept_sync_global();
4050
He, Qingfdef3ad2007-04-30 09:45:24 +03004051 return 0;
4052
Avi Kivity58972972009-02-24 22:26:47 +02004053out3:
4054 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08004055out2:
Avi Kivity58972972009-02-24 22:26:47 +02004056 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03004057out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004058 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03004059out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004060 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004061 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004062}
4063
4064static void __exit vmx_exit(void)
4065{
Avi Kivity58972972009-02-24 22:26:47 +02004066 free_page((unsigned long)vmx_msr_bitmap_legacy);
4067 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004068 free_page((unsigned long)vmx_io_bitmap_b);
4069 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004070
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004071 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004072}
4073
4074module_init(vmx_init)
4075module_exit(vmx_exit)