blob: f482100eff87bd44303306dc6fa53353a6595113 [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;
Avi Kivitye8a48342009-09-01 16:06:25 +0300543 /*
544 * Unconditionally intercept #DB so we can maintain dr6 without
545 * reading it every exit.
546 */
547 eb |= 1u << DB_VECTOR;
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100548 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100549 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
550 eb |= 1u << BP_VECTOR;
551 }
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300552 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300553 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200554 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800555 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300556 vmcs_write32(EXCEPTION_BITMAP, eb);
557}
558
Avi Kivity33ed6322007-05-02 16:54:03 +0300559static void reload_tss(void)
560{
Avi Kivity33ed6322007-05-02 16:54:03 +0300561 /*
562 * VT restores TR but not its size. Useless.
563 */
564 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200565 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300566
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300567 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300568 descs = (void *)gdt.base;
569 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
570 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300571}
572
Rusty Russell8b9cf982007-07-30 16:31:43 +1000573static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300574{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400575 int efer_offset = vmx->msr_offset_efer;
Roel Kluin3a34a882009-08-04 02:08:45 -0700576 u64 host_efer;
577 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300578 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300579
Avi Kivity51c6cf62007-08-29 03:48:05 +0300580 if (efer_offset < 0)
581 return;
Roel Kluin3a34a882009-08-04 02:08:45 -0700582 host_efer = vmx->host_msrs[efer_offset].data;
583 guest_efer = vmx->guest_msrs[efer_offset].data;
584
Avi Kivity51c6cf62007-08-29 03:48:05 +0300585 /*
586 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
587 * outside long mode
588 */
589 ignore_bits = EFER_NX | EFER_SCE;
590#ifdef CONFIG_X86_64
591 ignore_bits |= EFER_LMA | EFER_LME;
592 /* SCE is meaningful only in long mode on Intel */
593 if (guest_efer & EFER_LMA)
594 ignore_bits &= ~(u64)EFER_SCE;
595#endif
596 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
597 return;
598
599 vmx->host_state.guest_efer_loaded = 1;
600 guest_efer &= ~ignore_bits;
601 guest_efer |= host_efer & ignore_bits;
602 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000603 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300604}
605
Avi Kivity51c6cf62007-08-29 03:48:05 +0300606static void reload_host_efer(struct vcpu_vmx *vmx)
607{
608 if (vmx->host_state.guest_efer_loaded) {
609 vmx->host_state.guest_efer_loaded = 0;
610 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
611 }
612}
613
Avi Kivity04d2cc72007-09-10 18:10:54 +0300614static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300615{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300616 struct vcpu_vmx *vmx = to_vmx(vcpu);
617
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400618 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300619 return;
620
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400621 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300622 /*
623 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
624 * allow segment selectors with cpl > 0 or ti == 1.
625 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300626 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200627 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300628 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200629 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400630 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200631 vmx->host_state.fs_reload_needed = 0;
632 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300633 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200634 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300635 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300636 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400637 if (!(vmx->host_state.gs_sel & 7))
638 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300639 else {
640 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200641 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300642 }
643
644#ifdef CONFIG_X86_64
645 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
646 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
647#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400648 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
649 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300650#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300651
652#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400653 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400654 save_msrs(vmx->host_msrs +
655 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400656
Avi Kivity707c0872007-05-02 17:33:43 +0300657#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400658 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300659 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300660}
661
Avi Kivitya9b21b62008-06-24 11:48:49 +0300662static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300663{
Avi Kivity15ad7142007-07-11 18:17:21 +0300664 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300665
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400666 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300667 return;
668
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200669 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400670 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200671 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300672 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200673 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300674 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300675 /*
676 * If we have to reload gs, we must take care to
677 * preserve our gs base.
678 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300679 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300680 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300681#ifdef CONFIG_X86_64
682 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
683#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300684 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300685 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200686 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400687 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
688 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300689 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300690}
691
Avi Kivitya9b21b62008-06-24 11:48:49 +0300692static void vmx_load_host_state(struct vcpu_vmx *vmx)
693{
694 preempt_disable();
695 __vmx_load_host_state(vmx);
696 preempt_enable();
697}
698
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699/*
700 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
701 * vcpu mutex is already taken.
702 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300703static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400705 struct vcpu_vmx *vmx = to_vmx(vcpu);
706 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200707 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708
Eddie Donga3d7f852007-09-03 16:15:12 +0300709 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000710 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300711 kvm_migrate_timers(vcpu);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800712 vpid_sync_vcpu_all(vmx);
Avi Kivity543e4242008-05-13 16:22:47 +0300713 local_irq_disable();
714 list_add(&vmx->local_vcpus_link,
715 &per_cpu(vcpus_on_cpu, cpu));
716 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300717 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800718
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400719 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800720 u8 error;
721
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400722 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300723 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800724 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
725 : "cc");
726 if (error)
727 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400728 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800729 }
730
731 if (vcpu->cpu != cpu) {
732 struct descriptor_table dt;
733 unsigned long sysenter_esp;
734
735 vcpu->cpu = cpu;
736 /*
737 * Linux uses per-cpu TSS and GDT, so set these when switching
738 * processors.
739 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300740 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
741 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800742 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
743
744 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
745 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300746
747 /*
748 * Make sure the time stamp counter is monotonous.
749 */
750 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200751 if (tsc_this < vcpu->arch.host_tsc) {
752 delta = vcpu->arch.host_tsc - tsc_this;
753 new_offset = vmcs_read64(TSC_OFFSET) + delta;
754 vmcs_write64(TSC_OFFSET, new_offset);
755 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757}
758
759static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
760{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300761 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762}
763
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300764static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
765{
766 if (vcpu->fpu_active)
767 return;
768 vcpu->fpu_active = 1;
Rusty Russell707d92f2007-07-17 23:19:08 +1000769 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800770 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92f2007-07-17 23:19:08 +1000771 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300772 update_exception_bitmap(vcpu);
773}
774
775static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
776{
777 if (!vcpu->fpu_active)
778 return;
779 vcpu->fpu_active = 0;
Rusty Russell707d92f2007-07-17 23:19:08 +1000780 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300781 update_exception_bitmap(vcpu);
782}
783
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
785{
Avi Kivity345dcaa2009-08-12 15:29:37 +0300786 unsigned long rflags;
787
788 rflags = vmcs_readl(GUEST_RFLAGS);
789 if (to_vmx(vcpu)->rmode.vm86_active)
790 rflags &= ~(unsigned long)(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
791 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792}
793
794static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
795{
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300796 if (to_vmx(vcpu)->rmode.vm86_active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100797 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798 vmcs_writel(GUEST_RFLAGS, rflags);
799}
800
Glauber Costa2809f5d2009-05-12 16:21:05 -0400801static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
802{
803 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
804 int ret = 0;
805
806 if (interruptibility & GUEST_INTR_STATE_STI)
807 ret |= X86_SHADOW_INT_STI;
808 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
809 ret |= X86_SHADOW_INT_MOV_SS;
810
811 return ret & mask;
812}
813
814static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
815{
816 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
817 u32 interruptibility = interruptibility_old;
818
819 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
820
821 if (mask & X86_SHADOW_INT_MOV_SS)
822 interruptibility |= GUEST_INTR_STATE_MOV_SS;
823 if (mask & X86_SHADOW_INT_STI)
824 interruptibility |= GUEST_INTR_STATE_STI;
825
826 if ((interruptibility != interruptibility_old))
827 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
828}
829
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
831{
832 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800833
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300834 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300836 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837
Glauber Costa2809f5d2009-05-12 16:21:05 -0400838 /* skipping an emulated instruction also counts */
839 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800840}
841
Avi Kivity298101d2007-11-25 13:41:11 +0200842static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
843 bool has_error_code, u32 error_code)
844{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200845 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100846 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200847
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100848 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200849 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100850 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
851 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200852
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300853 if (vmx->rmode.vm86_active) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200854 vmx->rmode.irq.pending = true;
855 vmx->rmode.irq.vector = nr;
856 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +0300857 if (kvm_exception_is_soft(nr))
858 vmx->rmode.irq.rip +=
859 vmx->vcpu.arch.event_exit_inst_len;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100860 intr_info |= INTR_TYPE_SOFT_INTR;
861 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200862 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
863 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
864 return;
865 }
866
Gleb Natapov66fd3f72009-05-11 13:35:50 +0300867 if (kvm_exception_is_soft(nr)) {
868 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
869 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100870 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
871 } else
872 intr_info |= INTR_TYPE_HARD_EXCEPTION;
873
874 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200875}
876
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877/*
Eddie Donga75beee2007-05-17 18:55:15 +0300878 * Swap MSR entry in host/guest MSR entry array.
879 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200880#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000881static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300882{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400883 struct kvm_msr_entry tmp;
884
885 tmp = vmx->guest_msrs[to];
886 vmx->guest_msrs[to] = vmx->guest_msrs[from];
887 vmx->guest_msrs[from] = tmp;
888 tmp = vmx->host_msrs[to];
889 vmx->host_msrs[to] = vmx->host_msrs[from];
890 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300891}
Gabriel C54e11fa2007-08-01 16:23:10 +0200892#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300893
894/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300895 * Set up the vmcs to automatically save and restore system
896 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
897 * mode, as fiddling with msrs is very expensive.
898 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000899static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300900{
Eddie Dong2cc51562007-05-21 07:28:09 +0300901 int save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +0200902 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +0300903
Avi Kivity33f9c502008-02-27 16:06:57 +0200904 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300905 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300906#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000907 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300908 int index;
909
Rusty Russell8b9cf982007-07-30 16:31:43 +1000910 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300911 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000912 move_msr_up(vmx, index, save_nmsrs++);
913 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300914 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000915 move_msr_up(vmx, index, save_nmsrs++);
916 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300917 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000918 move_msr_up(vmx, index, save_nmsrs++);
919 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300920 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000921 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300922 /*
923 * MSR_K6_STAR is only needed on long mode guests, and only
924 * if efer.sce is enabled.
925 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000926 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800927 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000928 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300929 }
Eddie Donga75beee2007-05-17 18:55:15 +0300930#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400931 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300932
Eddie Donga75beee2007-05-17 18:55:15 +0300933#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400934 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000935 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300936#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000937 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivity58972972009-02-24 22:26:47 +0200938
939 if (cpu_has_vmx_msr_bitmap()) {
940 if (is_long_mode(&vmx->vcpu))
941 msr_bitmap = vmx_msr_bitmap_longmode;
942 else
943 msr_bitmap = vmx_msr_bitmap_legacy;
944
945 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
946 }
Avi Kivitye38aea32007-04-19 13:22:48 +0300947}
948
949/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950 * reads and returns guest's timestamp counter "register"
951 * guest_tsc = host_tsc + tsc_offset -- 21.3
952 */
953static u64 guest_read_tsc(void)
954{
955 u64 host_tsc, tsc_offset;
956
957 rdtscll(host_tsc);
958 tsc_offset = vmcs_read64(TSC_OFFSET);
959 return host_tsc + tsc_offset;
960}
961
962/*
963 * writes 'guest_tsc' into guest's timestamp counter "register"
964 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
965 */
Marcelo Tosatti53f658b2008-12-11 20:45:05 +0100966static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
969}
970
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971/*
972 * Reads an msr value (of 'msr_index') into 'pdata'.
973 * Returns 0 on success, non-0 otherwise.
974 * Assumes vcpu_load() was already called.
975 */
976static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
977{
978 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400979 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980
981 if (!pdata) {
982 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
983 return -EINVAL;
984 }
985
986 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800987#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 case MSR_FS_BASE:
989 data = vmcs_readl(GUEST_FS_BASE);
990 break;
991 case MSR_GS_BASE:
992 data = vmcs_readl(GUEST_GS_BASE);
993 break;
994 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800995 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996#endif
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530997 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 data = guest_read_tsc();
999 break;
1000 case MSR_IA32_SYSENTER_CS:
1001 data = vmcs_read32(GUEST_SYSENTER_CS);
1002 break;
1003 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001004 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005 break;
1006 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001007 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009 default:
Avi Kivity516a1a72009-02-15 02:32:07 +02001010 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +10001011 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001012 if (msr) {
1013 data = msr->data;
1014 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001016 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017 }
1018
1019 *pdata = data;
1020 return 0;
1021}
1022
1023/*
1024 * Writes msr value into into the appropriate "register".
1025 * Returns 0 on success, non-0 otherwise.
1026 * Assumes vcpu_load() was already called.
1027 */
1028static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1029{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001030 struct vcpu_vmx *vmx = to_vmx(vcpu);
1031 struct kvm_msr_entry *msr;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01001032 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +03001033 int ret = 0;
1034
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001036 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001037 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001038 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001039 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001040#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041 case MSR_FS_BASE:
1042 vmcs_writel(GUEST_FS_BASE, data);
1043 break;
1044 case MSR_GS_BASE:
1045 vmcs_writel(GUEST_GS_BASE, data);
1046 break;
1047#endif
1048 case MSR_IA32_SYSENTER_CS:
1049 vmcs_write32(GUEST_SYSENTER_CS, data);
1050 break;
1051 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001052 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053 break;
1054 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001055 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301057 case MSR_IA32_TSC:
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01001058 rdtscll(host_tsc);
1059 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001061 case MSR_IA32_CR_PAT:
1062 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1063 vmcs_write64(GUEST_IA32_PAT, data);
1064 vcpu->arch.pat = data;
1065 break;
1066 }
1067 /* Otherwise falls through to kvm_set_msr_common */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001068 default:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001069 vmx_load_host_state(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +10001070 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001071 if (msr) {
1072 msr->data = data;
1073 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001075 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076 }
1077
Eddie Dong2cc51562007-05-21 07:28:09 +03001078 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079}
1080
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001081static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001083 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1084 switch (reg) {
1085 case VCPU_REGS_RSP:
1086 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1087 break;
1088 case VCPU_REGS_RIP:
1089 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1090 break;
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03001091 case VCPU_EXREG_PDPTR:
1092 if (enable_ept)
1093 ept_save_pdptrs(vcpu);
1094 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001095 default:
1096 break;
1097 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098}
1099
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001100static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001102 int old_debug = vcpu->guest_debug;
1103 unsigned long flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001105 vcpu->guest_debug = dbg->control;
1106 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
1107 vcpu->guest_debug = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001109 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1110 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1111 else
1112 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1113
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001114 flags = vmcs_readl(GUEST_RFLAGS);
1115 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
1116 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1117 else if (old_debug & KVM_GUESTDBG_SINGLESTEP)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001119 vmcs_writel(GUEST_RFLAGS, flags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001121 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001122
1123 return 0;
1124}
1125
1126static __init int cpu_has_kvm_support(void)
1127{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001128 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129}
1130
1131static __init int vmx_disabled_by_bios(void)
1132{
1133 u64 msr;
1134
1135 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001136 return (msr & (FEATURE_CONTROL_LOCKED |
1137 FEATURE_CONTROL_VMXON_ENABLED))
1138 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001139 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001140}
1141
Avi Kivity774c47f2007-02-12 00:54:47 -08001142static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143{
1144 int cpu = raw_smp_processor_id();
1145 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1146 u64 old;
1147
Avi Kivity543e4242008-05-13 16:22:47 +03001148 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001150 if ((old & (FEATURE_CONTROL_LOCKED |
1151 FEATURE_CONTROL_VMXON_ENABLED))
1152 != (FEATURE_CONTROL_LOCKED |
1153 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001155 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001156 FEATURE_CONTROL_LOCKED |
1157 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001158 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001159 asm volatile (ASM_VMX_VMXON_RAX
1160 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001161 : "memory", "cc");
1162}
1163
Avi Kivity543e4242008-05-13 16:22:47 +03001164static void vmclear_local_vcpus(void)
1165{
1166 int cpu = raw_smp_processor_id();
1167 struct vcpu_vmx *vmx, *n;
1168
1169 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1170 local_vcpus_link)
1171 __vcpu_clear(vmx);
1172}
1173
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001174
1175/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1176 * tricks.
1177 */
1178static void kvm_cpu_vmxoff(void)
1179{
1180 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1181 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1182}
1183
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184static void hardware_disable(void *garbage)
1185{
Avi Kivity543e4242008-05-13 16:22:47 +03001186 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001187 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188}
1189
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001190static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001191 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001192{
1193 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001194 u32 ctl = ctl_min | ctl_opt;
1195
1196 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1197
1198 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1199 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1200
1201 /* Ensure minimum (required) set of control bits are supported. */
1202 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001203 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001204
1205 *result = ctl;
1206 return 0;
1207}
1208
Yang, Sheng002c7f72007-07-31 14:23:01 +03001209static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001210{
1211 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001212 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001213 u32 _pin_based_exec_control = 0;
1214 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001215 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001216 u32 _vmexit_control = 0;
1217 u32 _vmentry_control = 0;
1218
1219 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001220 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001221 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1222 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001223 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001224
1225 min = CPU_BASED_HLT_EXITING |
1226#ifdef CONFIG_X86_64
1227 CPU_BASED_CR8_LOAD_EXITING |
1228 CPU_BASED_CR8_STORE_EXITING |
1229#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001230 CPU_BASED_CR3_LOAD_EXITING |
1231 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001232 CPU_BASED_USE_IO_BITMAPS |
1233 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001234 CPU_BASED_USE_TSC_OFFSETING |
1235 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001236 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001237 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001238 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001239 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1240 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001241 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001242#ifdef CONFIG_X86_64
1243 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1244 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1245 ~CPU_BASED_CR8_STORE_EXITING;
1246#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001247 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001248 min2 = 0;
1249 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001250 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001251 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001252 SECONDARY_EXEC_ENABLE_EPT |
1253 SECONDARY_EXEC_UNRESTRICTED_GUEST;
Sheng Yangd56f5462008-04-25 10:13:16 +08001254 if (adjust_vmx_controls(min2, opt2,
1255 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001256 &_cpu_based_2nd_exec_control) < 0)
1257 return -EIO;
1258 }
1259#ifndef CONFIG_X86_64
1260 if (!(_cpu_based_2nd_exec_control &
1261 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1262 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1263#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001264 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001265 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1266 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03001267 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
1268 CPU_BASED_CR3_STORE_EXITING |
1269 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001270 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1271 vmx_capability.ept, vmx_capability.vpid);
1272 }
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001273
1274 min = 0;
1275#ifdef CONFIG_X86_64
1276 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1277#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001278 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001279 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1280 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001281 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001282
Sheng Yang468d4722008-10-09 16:01:55 +08001283 min = 0;
1284 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001285 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1286 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001287 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001288
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001289 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001290
1291 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1292 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001293 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001294
1295#ifdef CONFIG_X86_64
1296 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1297 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001298 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001299#endif
1300
1301 /* Require Write-Back (WB) memory type for VMCS accesses. */
1302 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001303 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001304
Yang, Sheng002c7f72007-07-31 14:23:01 +03001305 vmcs_conf->size = vmx_msr_high & 0x1fff;
1306 vmcs_conf->order = get_order(vmcs_config.size);
1307 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001308
Yang, Sheng002c7f72007-07-31 14:23:01 +03001309 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1310 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001311 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001312 vmcs_conf->vmexit_ctrl = _vmexit_control;
1313 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001314
1315 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001316}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317
1318static struct vmcs *alloc_vmcs_cpu(int cpu)
1319{
1320 int node = cpu_to_node(cpu);
1321 struct page *pages;
1322 struct vmcs *vmcs;
1323
Mel Gorman6484eb32009-06-16 15:31:54 -07001324 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325 if (!pages)
1326 return NULL;
1327 vmcs = page_address(pages);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001328 memset(vmcs, 0, vmcs_config.size);
1329 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001330 return vmcs;
1331}
1332
1333static struct vmcs *alloc_vmcs(void)
1334{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001335 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336}
1337
1338static void free_vmcs(struct vmcs *vmcs)
1339{
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001340 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001341}
1342
Sam Ravnborg39959582007-06-01 00:47:13 -07001343static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344{
1345 int cpu;
1346
1347 for_each_online_cpu(cpu)
1348 free_vmcs(per_cpu(vmxarea, cpu));
1349}
1350
Avi Kivity6aa8b732006-12-10 02:21:36 -08001351static __init int alloc_kvm_area(void)
1352{
1353 int cpu;
1354
1355 for_each_online_cpu(cpu) {
1356 struct vmcs *vmcs;
1357
1358 vmcs = alloc_vmcs_cpu(cpu);
1359 if (!vmcs) {
1360 free_kvm_area();
1361 return -ENOMEM;
1362 }
1363
1364 per_cpu(vmxarea, cpu) = vmcs;
1365 }
1366 return 0;
1367}
1368
1369static __init int hardware_setup(void)
1370{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001371 if (setup_vmcs_config(&vmcs_config) < 0)
1372 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001373
1374 if (boot_cpu_has(X86_FEATURE_NX))
1375 kvm_enable_efer_bits(EFER_NX);
1376
Sheng Yang93ba03c2009-04-01 15:52:32 +08001377 if (!cpu_has_vmx_vpid())
1378 enable_vpid = 0;
1379
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001380 if (!cpu_has_vmx_ept()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001381 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001382 enable_unrestricted_guest = 0;
1383 }
1384
1385 if (!cpu_has_vmx_unrestricted_guest())
1386 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001387
1388 if (!cpu_has_vmx_flexpriority())
1389 flexpriority_enabled = 0;
1390
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001391 if (!cpu_has_vmx_tpr_shadow())
1392 kvm_x86_ops->update_cr8_intercept = NULL;
1393
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001394 if (enable_ept && !cpu_has_vmx_ept_2m_page())
1395 kvm_disable_largepages();
1396
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397 return alloc_kvm_area();
1398}
1399
1400static __exit void hardware_unsetup(void)
1401{
1402 free_kvm_area();
1403}
1404
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1406{
1407 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1408
Avi Kivity6af11b92007-03-19 13:18:10 +02001409 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410 vmcs_write16(sf->selector, save->selector);
1411 vmcs_writel(sf->base, save->base);
1412 vmcs_write32(sf->limit, save->limit);
1413 vmcs_write32(sf->ar_bytes, save->ar);
1414 } else {
1415 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1416 << AR_DPL_SHIFT;
1417 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1418 }
1419}
1420
1421static void enter_pmode(struct kvm_vcpu *vcpu)
1422{
1423 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001424 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001426 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001427 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001429 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
1430 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
1431 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432
1433 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001434 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001435 flags |= (vmx->rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436 vmcs_writel(GUEST_RFLAGS, flags);
1437
Rusty Russell66aee912007-07-17 23:34:16 +10001438 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1439 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440
1441 update_exception_bitmap(vcpu);
1442
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001443 if (emulate_invalid_guest_state)
1444 return;
1445
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001446 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
1447 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
1448 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
1449 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450
1451 vmcs_write16(GUEST_SS_SELECTOR, 0);
1452 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1453
1454 vmcs_write16(GUEST_CS_SELECTOR,
1455 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1456 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1457}
1458
Mike Dayd77c26f2007-10-08 09:02:08 -04001459static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001460{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001461 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001462 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1463 kvm->memslots[0].npages - 3;
1464 return base_gfn << PAGE_SHIFT;
1465 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001466 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001467}
1468
1469static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1470{
1471 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1472
1473 save->selector = vmcs_read16(sf->selector);
1474 save->base = vmcs_readl(sf->base);
1475 save->limit = vmcs_read32(sf->limit);
1476 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001477 vmcs_write16(sf->selector, save->base >> 4);
1478 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479 vmcs_write32(sf->limit, 0xffff);
1480 vmcs_write32(sf->ar_bytes, 0xf3);
1481}
1482
1483static void enter_rmode(struct kvm_vcpu *vcpu)
1484{
1485 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001486 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001488 if (enable_unrestricted_guest)
1489 return;
1490
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001491 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001492 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001493
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001494 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1496
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001497 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1499
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001500 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1502
1503 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001504 vmx->rmode.save_iopl
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001505 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001507 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508
1509 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001510 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001511 update_exception_bitmap(vcpu);
1512
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001513 if (emulate_invalid_guest_state)
1514 goto continue_rmode;
1515
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1517 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1518 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1519
1520 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001521 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001522 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1523 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1525
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001526 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
1527 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
1528 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
1529 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001530
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001531continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001532 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001533 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001534}
1535
Amit Shah401d10d2009-02-20 22:53:37 +05301536static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1537{
1538 struct vcpu_vmx *vmx = to_vmx(vcpu);
1539 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1540
1541 vcpu->arch.shadow_efer = efer;
1542 if (!msr)
1543 return;
1544 if (efer & EFER_LMA) {
1545 vmcs_write32(VM_ENTRY_CONTROLS,
1546 vmcs_read32(VM_ENTRY_CONTROLS) |
1547 VM_ENTRY_IA32E_MODE);
1548 msr->data = efer;
1549 } else {
1550 vmcs_write32(VM_ENTRY_CONTROLS,
1551 vmcs_read32(VM_ENTRY_CONTROLS) &
1552 ~VM_ENTRY_IA32E_MODE);
1553
1554 msr->data = efer & ~EFER_LME;
1555 }
1556 setup_msrs(vmx);
1557}
1558
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001559#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001560
1561static void enter_lmode(struct kvm_vcpu *vcpu)
1562{
1563 u32 guest_tr_ar;
1564
1565 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1566 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1567 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001568 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001569 vmcs_write32(GUEST_TR_AR_BYTES,
1570 (guest_tr_ar & ~AR_TYPE_MASK)
1571 | AR_TYPE_BUSY_64_TSS);
1572 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001573 vcpu->arch.shadow_efer |= EFER_LMA;
Amit Shah401d10d2009-02-20 22:53:37 +05301574 vmx_set_efer(vcpu, vcpu->arch.shadow_efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001575}
1576
1577static void exit_lmode(struct kvm_vcpu *vcpu)
1578{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001579 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001580
1581 vmcs_write32(VM_ENTRY_CONTROLS,
1582 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001583 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001584}
1585
1586#endif
1587
Sheng Yang2384d2b2008-01-17 15:14:33 +08001588static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1589{
1590 vpid_sync_vcpu_all(to_vmx(vcpu));
Avi Kivity089d0342009-03-23 18:26:32 +02001591 if (enable_ept)
Sheng Yang4e1096d2008-07-06 19:16:51 +08001592 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001593}
1594
Anthony Liguori25c4c272007-04-27 09:29:21 +03001595static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001596{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001597 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1598 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001599}
1600
Sheng Yang14394422008-04-28 12:24:45 +08001601static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1602{
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03001603 if (!test_bit(VCPU_EXREG_PDPTR,
1604 (unsigned long *)&vcpu->arch.regs_dirty))
1605 return;
1606
Sheng Yang14394422008-04-28 12:24:45 +08001607 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Sheng Yang14394422008-04-28 12:24:45 +08001608 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1609 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1610 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1611 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1612 }
1613}
1614
Avi Kivity8f5d5492009-05-31 18:41:29 +03001615static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
1616{
1617 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1618 vcpu->arch.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
1619 vcpu->arch.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
1620 vcpu->arch.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
1621 vcpu->arch.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
1622 }
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03001623
1624 __set_bit(VCPU_EXREG_PDPTR,
1625 (unsigned long *)&vcpu->arch.regs_avail);
1626 __set_bit(VCPU_EXREG_PDPTR,
1627 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03001628}
1629
Sheng Yang14394422008-04-28 12:24:45 +08001630static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1631
1632static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1633 unsigned long cr0,
1634 struct kvm_vcpu *vcpu)
1635{
1636 if (!(cr0 & X86_CR0_PG)) {
1637 /* From paging/starting to nonpaging */
1638 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001639 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001640 (CPU_BASED_CR3_LOAD_EXITING |
1641 CPU_BASED_CR3_STORE_EXITING));
1642 vcpu->arch.cr0 = cr0;
1643 vmx_set_cr4(vcpu, vcpu->arch.cr4);
Sheng Yang14394422008-04-28 12:24:45 +08001644 } else if (!is_paging(vcpu)) {
1645 /* From nonpaging to paging */
1646 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001647 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001648 ~(CPU_BASED_CR3_LOAD_EXITING |
1649 CPU_BASED_CR3_STORE_EXITING));
1650 vcpu->arch.cr0 = cr0;
1651 vmx_set_cr4(vcpu, vcpu->arch.cr4);
Sheng Yang14394422008-04-28 12:24:45 +08001652 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08001653
1654 if (!(cr0 & X86_CR0_WP))
1655 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08001656}
1657
1658static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1659 struct kvm_vcpu *vcpu)
1660{
1661 if (!is_paging(vcpu)) {
1662 *hw_cr4 &= ~X86_CR4_PAE;
1663 *hw_cr4 |= X86_CR4_PSE;
1664 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1665 *hw_cr4 &= ~X86_CR4_PAE;
1666}
1667
Avi Kivity6aa8b732006-12-10 02:21:36 -08001668static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1669{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001670 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001671 unsigned long hw_cr0;
1672
1673 if (enable_unrestricted_guest)
1674 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
1675 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
1676 else
1677 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08001678
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001679 vmx_fpu_deactivate(vcpu);
1680
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001681 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001682 enter_pmode(vcpu);
1683
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001684 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001685 enter_rmode(vcpu);
1686
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001687#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001688 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001689 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10001691 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692 exit_lmode(vcpu);
1693 }
1694#endif
1695
Avi Kivity089d0342009-03-23 18:26:32 +02001696 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001697 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1698
Avi Kivity6aa8b732006-12-10 02:21:36 -08001699 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001700 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001701 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001702
Rusty Russell707d92f2007-07-17 23:19:08 +10001703 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001704 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705}
1706
Sheng Yang14394422008-04-28 12:24:45 +08001707static u64 construct_eptp(unsigned long root_hpa)
1708{
1709 u64 eptp;
1710
1711 /* TODO write the value reading from MSR */
1712 eptp = VMX_EPT_DEFAULT_MT |
1713 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1714 eptp |= (root_hpa & PAGE_MASK);
1715
1716 return eptp;
1717}
1718
Avi Kivity6aa8b732006-12-10 02:21:36 -08001719static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1720{
Sheng Yang14394422008-04-28 12:24:45 +08001721 unsigned long guest_cr3;
1722 u64 eptp;
1723
1724 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02001725 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001726 eptp = construct_eptp(cr3);
1727 vmcs_write64(EPT_POINTER, eptp);
Sheng Yang14394422008-04-28 12:24:45 +08001728 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
Sheng Yangb927a3c2009-07-21 10:42:48 +08001729 vcpu->kvm->arch.ept_identity_map_addr;
Sheng Yang14394422008-04-28 12:24:45 +08001730 }
1731
Sheng Yang2384d2b2008-01-17 15:14:33 +08001732 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001733 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001734 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001735 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001736}
1737
1738static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1739{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001740 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08001741 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1742
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001743 vcpu->arch.cr4 = cr4;
Avi Kivity089d0342009-03-23 18:26:32 +02001744 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001745 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1746
1747 vmcs_writel(CR4_READ_SHADOW, cr4);
1748 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001749}
1750
Avi Kivity6aa8b732006-12-10 02:21:36 -08001751static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1752{
1753 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1754
1755 return vmcs_readl(sf->base);
1756}
1757
1758static void vmx_get_segment(struct kvm_vcpu *vcpu,
1759 struct kvm_segment *var, int seg)
1760{
1761 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1762 u32 ar;
1763
1764 var->base = vmcs_readl(sf->base);
1765 var->limit = vmcs_read32(sf->limit);
1766 var->selector = vmcs_read16(sf->selector);
1767 ar = vmcs_read32(sf->ar_bytes);
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02001768 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001769 ar = 0;
1770 var->type = ar & 15;
1771 var->s = (ar >> 4) & 1;
1772 var->dpl = (ar >> 5) & 3;
1773 var->present = (ar >> 7) & 1;
1774 var->avl = (ar >> 12) & 1;
1775 var->l = (ar >> 13) & 1;
1776 var->db = (ar >> 14) & 1;
1777 var->g = (ar >> 15) & 1;
1778 var->unusable = (ar >> 16) & 1;
1779}
1780
Izik Eidus2e4d2652008-03-24 19:38:34 +02001781static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1782{
Izik Eidus2e4d2652008-03-24 19:38:34 +02001783 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1784 return 0;
1785
1786 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1787 return 3;
1788
Avi Kivityeab4b8a2009-08-04 15:02:54 +03001789 return vmcs_read16(GUEST_CS_SELECTOR) & 3;
Izik Eidus2e4d2652008-03-24 19:38:34 +02001790}
1791
Avi Kivity653e3102007-05-07 10:55:37 +03001792static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001793{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001794 u32 ar;
1795
Avi Kivity653e3102007-05-07 10:55:37 +03001796 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001797 ar = 1 << 16;
1798 else {
1799 ar = var->type & 15;
1800 ar |= (var->s & 1) << 4;
1801 ar |= (var->dpl & 3) << 5;
1802 ar |= (var->present & 1) << 7;
1803 ar |= (var->avl & 1) << 12;
1804 ar |= (var->l & 1) << 13;
1805 ar |= (var->db & 1) << 14;
1806 ar |= (var->g & 1) << 15;
1807 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001808 if (ar == 0) /* a 0 value means unusable */
1809 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001810
1811 return ar;
1812}
1813
1814static void vmx_set_segment(struct kvm_vcpu *vcpu,
1815 struct kvm_segment *var, int seg)
1816{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001817 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03001818 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1819 u32 ar;
1820
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001821 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
1822 vmx->rmode.tr.selector = var->selector;
1823 vmx->rmode.tr.base = var->base;
1824 vmx->rmode.tr.limit = var->limit;
1825 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001826 return;
1827 }
1828 vmcs_writel(sf->base, var->base);
1829 vmcs_write32(sf->limit, var->limit);
1830 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001831 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001832 /*
1833 * Hack real-mode segments into vm86 compatibility.
1834 */
1835 if (var->base == 0xffff0000 && var->selector == 0xf000)
1836 vmcs_writel(sf->base, 0xf0000);
1837 ar = 0xf3;
1838 } else
1839 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001840
1841 /*
1842 * Fix the "Accessed" bit in AR field of segment registers for older
1843 * qemu binaries.
1844 * IA32 arch specifies that at the time of processor reset the
1845 * "Accessed" bit in the AR field of segment registers is 1. And qemu
1846 * is setting it to 0 in the usedland code. This causes invalid guest
1847 * state vmexit when "unrestricted guest" mode is turned on.
1848 * Fix for this setup issue in cpu_reset is being pushed in the qemu
1849 * tree. Newer qemu binaries with that qemu fix would not need this
1850 * kvm hack.
1851 */
1852 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
1853 ar |= 0x1; /* Accessed */
1854
Avi Kivity6aa8b732006-12-10 02:21:36 -08001855 vmcs_write32(sf->ar_bytes, ar);
1856}
1857
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1859{
1860 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1861
1862 *db = (ar >> 14) & 1;
1863 *l = (ar >> 13) & 1;
1864}
1865
1866static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1867{
1868 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1869 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1870}
1871
1872static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1873{
1874 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1875 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1876}
1877
1878static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1879{
1880 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1881 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1882}
1883
1884static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1885{
1886 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1887 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1888}
1889
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001890static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1891{
1892 struct kvm_segment var;
1893 u32 ar;
1894
1895 vmx_get_segment(vcpu, &var, seg);
1896 ar = vmx_segment_access_rights(&var);
1897
1898 if (var.base != (var.selector << 4))
1899 return false;
1900 if (var.limit != 0xffff)
1901 return false;
1902 if (ar != 0xf3)
1903 return false;
1904
1905 return true;
1906}
1907
1908static bool code_segment_valid(struct kvm_vcpu *vcpu)
1909{
1910 struct kvm_segment cs;
1911 unsigned int cs_rpl;
1912
1913 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1914 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1915
Avi Kivity1872a3f2009-01-04 23:26:52 +02001916 if (cs.unusable)
1917 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001918 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1919 return false;
1920 if (!cs.s)
1921 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001922 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001923 if (cs.dpl > cs_rpl)
1924 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001925 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001926 if (cs.dpl != cs_rpl)
1927 return false;
1928 }
1929 if (!cs.present)
1930 return false;
1931
1932 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1933 return true;
1934}
1935
1936static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1937{
1938 struct kvm_segment ss;
1939 unsigned int ss_rpl;
1940
1941 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1942 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1943
Avi Kivity1872a3f2009-01-04 23:26:52 +02001944 if (ss.unusable)
1945 return true;
1946 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001947 return false;
1948 if (!ss.s)
1949 return false;
1950 if (ss.dpl != ss_rpl) /* DPL != RPL */
1951 return false;
1952 if (!ss.present)
1953 return false;
1954
1955 return true;
1956}
1957
1958static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1959{
1960 struct kvm_segment var;
1961 unsigned int rpl;
1962
1963 vmx_get_segment(vcpu, &var, seg);
1964 rpl = var.selector & SELECTOR_RPL_MASK;
1965
Avi Kivity1872a3f2009-01-04 23:26:52 +02001966 if (var.unusable)
1967 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001968 if (!var.s)
1969 return false;
1970 if (!var.present)
1971 return false;
1972 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
1973 if (var.dpl < rpl) /* DPL < RPL */
1974 return false;
1975 }
1976
1977 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1978 * rights flags
1979 */
1980 return true;
1981}
1982
1983static bool tr_valid(struct kvm_vcpu *vcpu)
1984{
1985 struct kvm_segment tr;
1986
1987 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
1988
Avi Kivity1872a3f2009-01-04 23:26:52 +02001989 if (tr.unusable)
1990 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001991 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1992 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001993 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001994 return false;
1995 if (!tr.present)
1996 return false;
1997
1998 return true;
1999}
2000
2001static bool ldtr_valid(struct kvm_vcpu *vcpu)
2002{
2003 struct kvm_segment ldtr;
2004
2005 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2006
Avi Kivity1872a3f2009-01-04 23:26:52 +02002007 if (ldtr.unusable)
2008 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002009 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2010 return false;
2011 if (ldtr.type != 2)
2012 return false;
2013 if (!ldtr.present)
2014 return false;
2015
2016 return true;
2017}
2018
2019static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2020{
2021 struct kvm_segment cs, ss;
2022
2023 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2024 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2025
2026 return ((cs.selector & SELECTOR_RPL_MASK) ==
2027 (ss.selector & SELECTOR_RPL_MASK));
2028}
2029
2030/*
2031 * Check if guest state is valid. Returns true if valid, false if
2032 * not.
2033 * We assume that registers are always usable
2034 */
2035static bool guest_state_valid(struct kvm_vcpu *vcpu)
2036{
2037 /* real mode guest state checks */
2038 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
2039 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2040 return false;
2041 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2042 return false;
2043 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2044 return false;
2045 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2046 return false;
2047 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2048 return false;
2049 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2050 return false;
2051 } else {
2052 /* protected mode guest state checks */
2053 if (!cs_ss_rpl_check(vcpu))
2054 return false;
2055 if (!code_segment_valid(vcpu))
2056 return false;
2057 if (!stack_segment_valid(vcpu))
2058 return false;
2059 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2060 return false;
2061 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2062 return false;
2063 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2064 return false;
2065 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2066 return false;
2067 if (!tr_valid(vcpu))
2068 return false;
2069 if (!ldtr_valid(vcpu))
2070 return false;
2071 }
2072 /* TODO:
2073 * - Add checks on RIP
2074 * - Add checks on RFLAGS
2075 */
2076
2077 return true;
2078}
2079
Mike Dayd77c26f2007-10-08 09:02:08 -04002080static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002081{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002082 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002083 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002084 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02002085 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002086
Izik Eidus195aefd2007-10-01 22:14:18 +02002087 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2088 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002089 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002090 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002091 r = kvm_write_guest_page(kvm, fn++, &data,
2092 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002093 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002094 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002095 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2096 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002097 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002098 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2099 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002100 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002101 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002102 r = kvm_write_guest_page(kvm, fn, &data,
2103 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2104 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002105 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002106 goto out;
2107
2108 ret = 1;
2109out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002110 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111}
2112
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002113static int init_rmode_identity_map(struct kvm *kvm)
2114{
2115 int i, r, ret;
2116 pfn_t identity_map_pfn;
2117 u32 tmp;
2118
Avi Kivity089d0342009-03-23 18:26:32 +02002119 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002120 return 1;
2121 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2122 printk(KERN_ERR "EPT: identity-mapping pagetable "
2123 "haven't been allocated!\n");
2124 return 0;
2125 }
2126 if (likely(kvm->arch.ept_identity_pagetable_done))
2127 return 1;
2128 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002129 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002130 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2131 if (r < 0)
2132 goto out;
2133 /* Set up identity-mapping pagetable for EPT in real mode */
2134 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2135 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2136 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2137 r = kvm_write_guest_page(kvm, identity_map_pfn,
2138 &tmp, i * sizeof(tmp), sizeof(tmp));
2139 if (r < 0)
2140 goto out;
2141 }
2142 kvm->arch.ept_identity_pagetable_done = true;
2143 ret = 1;
2144out:
2145 return ret;
2146}
2147
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148static void seg_setup(int seg)
2149{
2150 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002151 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002152
2153 vmcs_write16(sf->selector, 0);
2154 vmcs_writel(sf->base, 0);
2155 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002156 if (enable_unrestricted_guest) {
2157 ar = 0x93;
2158 if (seg == VCPU_SREG_CS)
2159 ar |= 0x08; /* code segment */
2160 } else
2161 ar = 0xf3;
2162
2163 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002164}
2165
Sheng Yangf78e0e22007-10-29 09:40:42 +08002166static int alloc_apic_access_page(struct kvm *kvm)
2167{
2168 struct kvm_userspace_memory_region kvm_userspace_mem;
2169 int r = 0;
2170
Izik Eidus72dc67a2008-02-10 18:04:15 +02002171 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002172 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002173 goto out;
2174 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2175 kvm_userspace_mem.flags = 0;
2176 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2177 kvm_userspace_mem.memory_size = PAGE_SIZE;
2178 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2179 if (r)
2180 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002181
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002182 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002183out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002184 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002185 return r;
2186}
2187
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002188static int alloc_identity_pagetable(struct kvm *kvm)
2189{
2190 struct kvm_userspace_memory_region kvm_userspace_mem;
2191 int r = 0;
2192
2193 down_write(&kvm->slots_lock);
2194 if (kvm->arch.ept_identity_pagetable)
2195 goto out;
2196 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2197 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002198 kvm_userspace_mem.guest_phys_addr =
2199 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002200 kvm_userspace_mem.memory_size = PAGE_SIZE;
2201 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2202 if (r)
2203 goto out;
2204
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002205 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
Sheng Yangb927a3c2009-07-21 10:42:48 +08002206 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002207out:
2208 up_write(&kvm->slots_lock);
2209 return r;
2210}
2211
Sheng Yang2384d2b2008-01-17 15:14:33 +08002212static void allocate_vpid(struct vcpu_vmx *vmx)
2213{
2214 int vpid;
2215
2216 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002217 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002218 return;
2219 spin_lock(&vmx_vpid_lock);
2220 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2221 if (vpid < VMX_NR_VPIDS) {
2222 vmx->vpid = vpid;
2223 __set_bit(vpid, vmx_vpid_bitmap);
2224 }
2225 spin_unlock(&vmx_vpid_lock);
2226}
2227
Avi Kivity58972972009-02-24 22:26:47 +02002228static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002229{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002230 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002231
2232 if (!cpu_has_vmx_msr_bitmap())
2233 return;
2234
2235 /*
2236 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2237 * have the write-low and read-high bitmap offsets the wrong way round.
2238 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2239 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002240 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002241 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2242 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002243 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2244 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002245 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2246 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002247 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002248}
2249
Avi Kivity58972972009-02-24 22:26:47 +02002250static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2251{
2252 if (!longmode_only)
2253 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2254 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2255}
2256
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257/*
2258 * Sets up the vmcs for emulated real mode.
2259 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002260static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261{
Sheng Yang468d4722008-10-09 16:01:55 +08002262 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002263 u32 junk;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002264 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265 unsigned long a;
2266 struct descriptor_table dt;
2267 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002268 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002269 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002272 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2273 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002274
Sheng Yang25c5f222008-03-28 13:18:56 +08002275 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002276 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002277
Avi Kivity6aa8b732006-12-10 02:21:36 -08002278 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2279
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280 /* Control */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002281 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2282 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002283
2284 exec_control = vmcs_config.cpu_based_exec_ctrl;
2285 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2286 exec_control &= ~CPU_BASED_TPR_SHADOW;
2287#ifdef CONFIG_X86_64
2288 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2289 CPU_BASED_CR8_LOAD_EXITING;
2290#endif
2291 }
Avi Kivity089d0342009-03-23 18:26:32 +02002292 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002293 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002294 CPU_BASED_CR3_LOAD_EXITING |
2295 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002296 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002297
Sheng Yang83ff3b92007-11-21 14:33:25 +08002298 if (cpu_has_secondary_exec_ctrls()) {
2299 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2300 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2301 exec_control &=
2302 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002303 if (vmx->vpid == 0)
2304 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Avi Kivity089d0342009-03-23 18:26:32 +02002305 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002306 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002307 if (!enable_unrestricted_guest)
2308 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002309 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2310 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002311
Avi Kivityc7addb92007-09-16 18:58:32 +02002312 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2313 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002314 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2315
2316 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2317 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2318 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2319
2320 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2321 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2322 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002323 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2324 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002326#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327 rdmsrl(MSR_FS_BASE, a);
2328 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2329 rdmsrl(MSR_GS_BASE, a);
2330 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2331#else
2332 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2333 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2334#endif
2335
2336 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2337
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002338 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2340
Mike Dayd77c26f2007-10-08 09:02:08 -04002341 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002342 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002343 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2344 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2345 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346
2347 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2348 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2349 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2350 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2351 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2352 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2353
Sheng Yang468d4722008-10-09 16:01:55 +08002354 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2355 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2356 host_pat = msr_low | ((u64) msr_high << 32);
2357 vmcs_write64(HOST_IA32_PAT, host_pat);
2358 }
2359 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2360 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2361 host_pat = msr_low | ((u64) msr_high << 32);
2362 /* Write the default value follow host pat */
2363 vmcs_write64(GUEST_IA32_PAT, host_pat);
2364 /* Keep arch.pat sync with GUEST_IA32_PAT */
2365 vmx->vcpu.arch.pat = host_pat;
2366 }
2367
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368 for (i = 0; i < NR_VMX_MSR; ++i) {
2369 u32 index = vmx_msr_index[i];
2370 u32 data_low, data_high;
2371 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002372 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373
2374 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2375 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002376 if (wrmsr_safe(index, data_low, data_high) < 0)
2377 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002379 vmx->host_msrs[j].index = index;
2380 vmx->host_msrs[j].reserved = 0;
2381 vmx->host_msrs[j].data = data;
2382 vmx->guest_msrs[j] = vmx->host_msrs[j];
2383 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002384 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002386 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387
2388 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002389 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2390
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002391 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2392 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2393
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002394 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2395 rdtscll(tsc_this);
2396 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2397 tsc_base = tsc_this;
2398
2399 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002400
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002401 return 0;
2402}
2403
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002404static int init_rmode(struct kvm *kvm)
2405{
2406 if (!init_rmode_tss(kvm))
2407 return 0;
2408 if (!init_rmode_identity_map(kvm))
2409 return 0;
2410 return 1;
2411}
2412
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002413static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2414{
2415 struct vcpu_vmx *vmx = to_vmx(vcpu);
2416 u64 msr;
2417 int ret;
2418
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002419 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002420 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002421 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002422 ret = -ENOMEM;
2423 goto out;
2424 }
2425
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002426 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002427
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002428 vmx->soft_vnmi_blocked = 0;
2429
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002430 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002431 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002432 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002433 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002434 msr |= MSR_IA32_APICBASE_BSP;
2435 kvm_set_apic_base(&vmx->vcpu, msr);
2436
2437 fx_init(&vmx->vcpu);
2438
Avi Kivity5706be02008-08-20 15:07:31 +03002439 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002440 /*
2441 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2442 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2443 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002444 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002445 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2446 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2447 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002448 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2449 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002450 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002451
2452 seg_setup(VCPU_SREG_DS);
2453 seg_setup(VCPU_SREG_ES);
2454 seg_setup(VCPU_SREG_FS);
2455 seg_setup(VCPU_SREG_GS);
2456 seg_setup(VCPU_SREG_SS);
2457
2458 vmcs_write16(GUEST_TR_SELECTOR, 0);
2459 vmcs_writel(GUEST_TR_BASE, 0);
2460 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2461 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2462
2463 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2464 vmcs_writel(GUEST_LDTR_BASE, 0);
2465 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2466 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2467
2468 vmcs_write32(GUEST_SYSENTER_CS, 0);
2469 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2470 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2471
2472 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002473 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002474 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002475 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002476 kvm_rip_write(vcpu, 0);
2477 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002478
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002479 vmcs_writel(GUEST_DR7, 0x400);
2480
2481 vmcs_writel(GUEST_GDTR_BASE, 0);
2482 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2483
2484 vmcs_writel(GUEST_IDTR_BASE, 0);
2485 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2486
2487 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2488 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2489 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2490
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002491 /* Special registers */
2492 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2493
2494 setup_msrs(vmx);
2495
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2497
Sheng Yangf78e0e22007-10-29 09:40:42 +08002498 if (cpu_has_vmx_tpr_shadow()) {
2499 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2500 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2501 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002502 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002503 vmcs_write32(TPR_THRESHOLD, 0);
2504 }
2505
2506 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2507 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002508 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509
Sheng Yang2384d2b2008-01-17 15:14:33 +08002510 if (vmx->vpid != 0)
2511 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2512
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002513 vmx->vcpu.arch.cr0 = 0x60000010;
2514 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002515 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002516 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002517 vmx_fpu_activate(&vmx->vcpu);
2518 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519
Sheng Yang2384d2b2008-01-17 15:14:33 +08002520 vpid_sync_vcpu_all(vmx);
2521
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002522 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002524 /* HACK: Don't enable emulation on guest boot/reset */
2525 vmx->emulation_required = 0;
2526
Avi Kivity6aa8b732006-12-10 02:21:36 -08002527out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002528 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002529 return ret;
2530}
2531
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002532static void enable_irq_window(struct kvm_vcpu *vcpu)
2533{
2534 u32 cpu_based_vm_exec_control;
2535
2536 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2537 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2538 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2539}
2540
2541static void enable_nmi_window(struct kvm_vcpu *vcpu)
2542{
2543 u32 cpu_based_vm_exec_control;
2544
2545 if (!cpu_has_virtual_nmis()) {
2546 enable_irq_window(vcpu);
2547 return;
2548 }
2549
2550 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2551 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2552 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2553}
2554
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002555static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03002556{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002557 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002558 uint32_t intr;
2559 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02002560
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002561 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002562
Avi Kivityfa89a812008-09-01 15:57:51 +03002563 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002564 if (vmx->rmode.vm86_active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002565 vmx->rmode.irq.pending = true;
2566 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002567 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +03002568 if (vcpu->arch.interrupt.soft)
2569 vmx->rmode.irq.rip +=
2570 vmx->vcpu.arch.event_exit_inst_len;
Avi Kivity9c5623e2007-11-08 18:19:20 +02002571 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2572 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2573 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002574 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002575 return;
2576 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002577 intr = irq | INTR_INFO_VALID_MASK;
2578 if (vcpu->arch.interrupt.soft) {
2579 intr |= INTR_TYPE_SOFT_INTR;
2580 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2581 vmx->vcpu.arch.event_exit_inst_len);
2582 } else
2583 intr |= INTR_TYPE_EXT_INTR;
2584 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03002585}
2586
Sheng Yangf08864b2008-05-15 18:23:25 +08002587static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2588{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002589 struct vcpu_vmx *vmx = to_vmx(vcpu);
2590
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002591 if (!cpu_has_virtual_nmis()) {
2592 /*
2593 * Tracking the NMI-blocked state in software is built upon
2594 * finding the next open IRQ window. This, in turn, depends on
2595 * well-behaving guests: They have to keep IRQs disabled at
2596 * least as long as the NMI handler runs. Otherwise we may
2597 * cause NMI nesting, maybe breaking the guest. But as this is
2598 * highly unlikely, we can live with the residual risk.
2599 */
2600 vmx->soft_vnmi_blocked = 1;
2601 vmx->vnmi_blocked_time = 0;
2602 }
2603
Jan Kiszka487b3912008-09-26 09:30:56 +02002604 ++vcpu->stat.nmi_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002605 if (vmx->rmode.vm86_active) {
Jan Kiszka66a5a342008-09-26 09:30:51 +02002606 vmx->rmode.irq.pending = true;
2607 vmx->rmode.irq.vector = NMI_VECTOR;
2608 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2609 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2610 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2611 INTR_INFO_VALID_MASK);
2612 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2613 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2614 return;
2615 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002616 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2617 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002618}
2619
Gleb Natapovc4282df2009-04-21 17:45:07 +03002620static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02002621{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002622 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03002623 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002624
Gleb Natapovc4282df2009-04-21 17:45:07 +03002625 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2626 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS |
2627 GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02002628}
2629
Gleb Natapov78646122009-03-23 12:12:11 +02002630static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
2631{
Gleb Natapovc4282df2009-04-21 17:45:07 +03002632 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2633 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2634 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02002635}
2636
Izik Eiduscbc94022007-10-25 00:29:55 +02002637static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2638{
2639 int ret;
2640 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002641 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002642 .guest_phys_addr = addr,
2643 .memory_size = PAGE_SIZE * 3,
2644 .flags = 0,
2645 };
2646
2647 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2648 if (ret)
2649 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002650 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002651 return 0;
2652}
2653
Avi Kivity6aa8b732006-12-10 02:21:36 -08002654static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2655 int vec, u32 err_code)
2656{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002657 /*
2658 * Instruction with address size override prefix opcode 0x67
2659 * Cause the #SS fault with 0 error code in VM86 mode.
2660 */
2661 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02002662 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002664 /*
2665 * Forward all other exceptions that are valid in real mode.
2666 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2667 * the required debugging infrastructure rework.
2668 */
2669 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002670 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002671 if (vcpu->guest_debug &
2672 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2673 return 0;
2674 kvm_queue_exception(vcpu, vec);
2675 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002676 case BP_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002677 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2678 return 0;
2679 /* fall through */
2680 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002681 case OF_VECTOR:
2682 case BR_VECTOR:
2683 case UD_VECTOR:
2684 case DF_VECTOR:
2685 case SS_VECTOR:
2686 case GP_VECTOR:
2687 case MF_VECTOR:
2688 kvm_queue_exception(vcpu, vec);
2689 return 1;
2690 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002691 return 0;
2692}
2693
Andi Kleena0861c02009-06-08 17:37:09 +08002694/*
2695 * Trigger machine check on the host. We assume all the MSRs are already set up
2696 * by the CPU and that we still run on the same CPU as the MCE occurred on.
2697 * We pass a fake environment to the machine check handler because we want
2698 * the guest to be always treated like user space, no matter what context
2699 * it used internally.
2700 */
2701static void kvm_machine_check(void)
2702{
2703#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
2704 struct pt_regs regs = {
2705 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
2706 .flags = X86_EFLAGS_IF,
2707 };
2708
2709 do_machine_check(&regs, 0);
2710#endif
2711}
2712
2713static int handle_machine_check(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2714{
2715 /* already handled by vcpu_run */
2716 return 1;
2717}
2718
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2720{
Avi Kivity1155f762007-11-22 11:30:47 +02002721 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002722 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002723 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724 u32 vect_info;
2725 enum emulation_result er;
2726
Avi Kivity1155f762007-11-22 11:30:47 +02002727 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2729
Andi Kleena0861c02009-06-08 17:37:09 +08002730 if (is_machine_check(intr_info))
2731 return handle_machine_check(vcpu, kvm_run);
2732
Avi Kivity6aa8b732006-12-10 02:21:36 -08002733 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002734 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002736 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737
Jan Kiszkae4a41882008-09-26 09:30:46 +02002738 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002739 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002740
2741 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002742 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002743 return 1;
2744 }
2745
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002746 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08002747 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002748 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002749 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002750 return 1;
2751 }
2752
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002754 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002755 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002756 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2757 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002758 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02002759 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002760 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002761 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002762 trace_kvm_page_fault(cr2, error_code);
2763
Gleb Natapov3298b752009-05-11 13:35:46 +03002764 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03002765 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002766 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002767 }
2768
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002769 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002771 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002772 if (vcpu->arch.halt_request) {
2773 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002774 return kvm_emulate_halt(vcpu);
2775 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002776 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002777 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002778
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002779 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002780 switch (ex_no) {
2781 case DB_VECTOR:
2782 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2783 if (!(vcpu->guest_debug &
2784 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2785 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2786 kvm_queue_exception(vcpu, DB_VECTOR);
2787 return 1;
2788 }
2789 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2790 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2791 /* fall through */
2792 case BP_VECTOR:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002793 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002794 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2795 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002796 break;
2797 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002798 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2799 kvm_run->ex.exception = ex_no;
2800 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002801 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002803 return 0;
2804}
2805
2806static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2807 struct kvm_run *kvm_run)
2808{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002809 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810 return 1;
2811}
2812
Avi Kivity988ad742007-02-12 00:54:36 -08002813static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2814{
2815 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2816 return 0;
2817}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2820{
He, Qingbfdaab02007-09-12 14:18:28 +08002821 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01002822 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002823 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002824
Avi Kivity1165f5f2007-04-19 17:27:43 +03002825 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002826 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002827 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002828
2829 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002830 if (emulate_instruction(vcpu,
2831 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002832 return 0;
2833 return 1;
2834 }
2835
2836 size = (exit_qualification & 7) + 1;
2837 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002838 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002839
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002840 skip_emulated_instruction(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002841 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842}
2843
Ingo Molnar102d8322007-02-19 14:37:47 +02002844static void
2845vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2846{
2847 /*
2848 * Patch in the VMCALL instruction:
2849 */
2850 hypercall[0] = 0x0f;
2851 hypercall[1] = 0x01;
2852 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002853}
2854
Avi Kivity6aa8b732006-12-10 02:21:36 -08002855static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2856{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002857 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002858 int cr;
2859 int reg;
2860
He, Qingbfdaab02007-09-12 14:18:28 +08002861 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862 cr = exit_qualification & 15;
2863 reg = (exit_qualification >> 8) & 15;
2864 switch ((exit_qualification >> 4) & 3) {
2865 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002866 val = kvm_register_read(vcpu, reg);
2867 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002868 switch (cr) {
2869 case 0:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002870 kvm_set_cr0(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002871 skip_emulated_instruction(vcpu);
2872 return 1;
2873 case 3:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002874 kvm_set_cr3(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002875 skip_emulated_instruction(vcpu);
2876 return 1;
2877 case 4:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002878 kvm_set_cr4(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002879 skip_emulated_instruction(vcpu);
2880 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002881 case 8: {
2882 u8 cr8_prev = kvm_get_cr8(vcpu);
2883 u8 cr8 = kvm_register_read(vcpu, reg);
2884 kvm_set_cr8(vcpu, cr8);
2885 skip_emulated_instruction(vcpu);
2886 if (irqchip_in_kernel(vcpu->kvm))
2887 return 1;
2888 if (cr8_prev <= cr8)
2889 return 1;
2890 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2891 return 0;
2892 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002893 };
2894 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002895 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002896 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002897 vcpu->arch.cr0 &= ~X86_CR0_TS;
2898 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002899 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002900 skip_emulated_instruction(vcpu);
2901 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902 case 1: /*mov from cr*/
2903 switch (cr) {
2904 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002905 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002906 trace_kvm_cr_read(cr, vcpu->arch.cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907 skip_emulated_instruction(vcpu);
2908 return 1;
2909 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002910 val = kvm_get_cr8(vcpu);
2911 kvm_register_write(vcpu, reg, val);
2912 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913 skip_emulated_instruction(vcpu);
2914 return 1;
2915 }
2916 break;
2917 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002918 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002919
2920 skip_emulated_instruction(vcpu);
2921 return 1;
2922 default:
2923 break;
2924 }
2925 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002926 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002927 (int)(exit_qualification >> 4) & 3, cr);
2928 return 0;
2929}
2930
2931static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2932{
He, Qingbfdaab02007-09-12 14:18:28 +08002933 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002934 unsigned long val;
2935 int dr, reg;
2936
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002937 dr = vmcs_readl(GUEST_DR7);
2938 if (dr & DR7_GD) {
2939 /*
2940 * As the vm-exit takes precedence over the debug trap, we
2941 * need to emulate the latter, either for the host or the
2942 * guest debugging itself.
2943 */
2944 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
2945 kvm_run->debug.arch.dr6 = vcpu->arch.dr6;
2946 kvm_run->debug.arch.dr7 = dr;
2947 kvm_run->debug.arch.pc =
2948 vmcs_readl(GUEST_CS_BASE) +
2949 vmcs_readl(GUEST_RIP);
2950 kvm_run->debug.arch.exception = DB_VECTOR;
2951 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2952 return 0;
2953 } else {
2954 vcpu->arch.dr7 &= ~DR7_GD;
2955 vcpu->arch.dr6 |= DR6_BD;
2956 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2957 kvm_queue_exception(vcpu, DB_VECTOR);
2958 return 1;
2959 }
2960 }
2961
He, Qingbfdaab02007-09-12 14:18:28 +08002962 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002963 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
2964 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
2965 if (exit_qualification & TYPE_MOV_FROM_DR) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002966 switch (dr) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002967 case 0 ... 3:
2968 val = vcpu->arch.db[dr];
2969 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002970 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002971 val = vcpu->arch.dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002972 break;
2973 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002974 val = vcpu->arch.dr7;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002975 break;
2976 default:
2977 val = 0;
2978 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002979 kvm_register_write(vcpu, reg, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002980 } else {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002981 val = vcpu->arch.regs[reg];
2982 switch (dr) {
2983 case 0 ... 3:
2984 vcpu->arch.db[dr] = val;
2985 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
2986 vcpu->arch.eff_db[dr] = val;
2987 break;
2988 case 4 ... 5:
2989 if (vcpu->arch.cr4 & X86_CR4_DE)
2990 kvm_queue_exception(vcpu, UD_VECTOR);
2991 break;
2992 case 6:
2993 if (val & 0xffffffff00000000ULL) {
2994 kvm_queue_exception(vcpu, GP_VECTOR);
2995 break;
2996 }
2997 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
2998 break;
2999 case 7:
3000 if (val & 0xffffffff00000000ULL) {
3001 kvm_queue_exception(vcpu, GP_VECTOR);
3002 break;
3003 }
3004 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
3005 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
3006 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3007 vcpu->arch.switch_db_regs =
3008 (val & DR7_BP_EN_MASK);
3009 }
3010 break;
3011 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003012 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003013 skip_emulated_instruction(vcpu);
3014 return 1;
3015}
3016
3017static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3018{
Avi Kivity06465c52007-02-28 20:46:53 +02003019 kvm_emulate_cpuid(vcpu);
3020 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021}
3022
3023static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3024{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003025 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003026 u64 data;
3027
3028 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003029 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003030 return 1;
3031 }
3032
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003033 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003034
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003036 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3037 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003038 skip_emulated_instruction(vcpu);
3039 return 1;
3040}
3041
3042static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3043{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003044 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3045 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3046 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003048 trace_kvm_msr_write(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003049
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003051 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003052 return 1;
3053 }
3054
3055 skip_emulated_instruction(vcpu);
3056 return 1;
3057}
3058
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003059static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
3060 struct kvm_run *kvm_run)
3061{
3062 return 1;
3063}
3064
Avi Kivity6aa8b732006-12-10 02:21:36 -08003065static int handle_interrupt_window(struct kvm_vcpu *vcpu,
3066 struct kvm_run *kvm_run)
3067{
Eddie Dong85f455f2007-07-06 12:20:49 +03003068 u32 cpu_based_vm_exec_control;
3069
3070 /* clear pending irq */
3071 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3072 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3073 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003074
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003075 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003076
Dor Laorc1150d82007-01-05 16:36:24 -08003077 /*
3078 * If the user space waits to inject interrupts, exit as soon as
3079 * possible
3080 */
Gleb Natapov80618232009-04-21 17:44:56 +03003081 if (!irqchip_in_kernel(vcpu->kvm) &&
3082 kvm_run->request_interrupt_window &&
3083 !kvm_cpu_has_interrupt(vcpu)) {
Dor Laorc1150d82007-01-05 16:36:24 -08003084 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003085 return 0;
3086 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003087 return 1;
3088}
3089
3090static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3091{
3092 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003093 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003094}
3095
Ingo Molnarc21415e2007-02-19 14:37:47 +02003096static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3097{
Dor Laor510043d2007-02-19 18:25:43 +02003098 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003099 kvm_emulate_hypercall(vcpu);
3100 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003101}
3102
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003103static int handle_vmx_insn(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3104{
3105 kvm_queue_exception(vcpu, UD_VECTOR);
3106 return 1;
3107}
3108
Marcelo Tosattia7052892008-09-23 13:18:35 -03003109static int handle_invlpg(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3110{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003111 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003112
3113 kvm_mmu_invlpg(vcpu, exit_qualification);
3114 skip_emulated_instruction(vcpu);
3115 return 1;
3116}
3117
Eddie Donge5edaa02007-11-11 12:28:35 +02003118static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3119{
3120 skip_emulated_instruction(vcpu);
3121 /* TODO: Add support for VT-d/pass-through device */
3122 return 1;
3123}
3124
Sheng Yangf78e0e22007-10-29 09:40:42 +08003125static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3126{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003127 unsigned long exit_qualification;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003128 enum emulation_result er;
3129 unsigned long offset;
3130
Sheng Yangf9c617f2009-03-25 10:08:52 +08003131 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003132 offset = exit_qualification & 0xffful;
3133
3134 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3135
3136 if (er != EMULATE_DONE) {
3137 printk(KERN_ERR
3138 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3139 offset);
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003140 return -ENOEXEC;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003141 }
3142 return 1;
3143}
3144
Izik Eidus37817f22008-03-24 23:14:53 +02003145static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3146{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003147 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003148 unsigned long exit_qualification;
3149 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003150 int reason, type, idt_v;
3151
3152 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3153 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003154
3155 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3156
3157 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003158 if (reason == TASK_SWITCH_GATE && idt_v) {
3159 switch (type) {
3160 case INTR_TYPE_NMI_INTR:
3161 vcpu->arch.nmi_injected = false;
3162 if (cpu_has_virtual_nmis())
3163 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3164 GUEST_INTR_STATE_NMI);
3165 break;
3166 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003167 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003168 kvm_clear_interrupt_queue(vcpu);
3169 break;
3170 case INTR_TYPE_HARD_EXCEPTION:
3171 case INTR_TYPE_SOFT_EXCEPTION:
3172 kvm_clear_exception_queue(vcpu);
3173 break;
3174 default:
3175 break;
3176 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003177 }
Izik Eidus37817f22008-03-24 23:14:53 +02003178 tss_selector = exit_qualification;
3179
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003180 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3181 type != INTR_TYPE_EXT_INTR &&
3182 type != INTR_TYPE_NMI_INTR))
3183 skip_emulated_instruction(vcpu);
3184
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003185 if (!kvm_task_switch(vcpu, tss_selector, reason))
3186 return 0;
3187
3188 /* clear all local breakpoint enable flags */
3189 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3190
3191 /*
3192 * TODO: What about debug traps on tss switch?
3193 * Are we supposed to inject them and update dr6?
3194 */
3195
3196 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003197}
3198
Sheng Yang14394422008-04-28 12:24:45 +08003199static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3200{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003201 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003202 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003203 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003204
Sheng Yangf9c617f2009-03-25 10:08:52 +08003205 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003206
3207 if (exit_qualification & (1 << 6)) {
3208 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003209 return -EINVAL;
Sheng Yang14394422008-04-28 12:24:45 +08003210 }
3211
3212 gla_validity = (exit_qualification >> 7) & 0x3;
3213 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3214 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3215 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3216 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003217 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003218 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3219 (long unsigned int)exit_qualification);
3220 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity596ae892009-06-03 14:12:10 +03003221 kvm_run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
3222 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003223 }
3224
3225 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003226 trace_kvm_page_fault(gpa, exit_qualification);
Sheng Yang49cd7d22009-02-11 13:50:40 +08003227 return kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003228}
3229
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003230static u64 ept_rsvd_mask(u64 spte, int level)
3231{
3232 int i;
3233 u64 mask = 0;
3234
3235 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
3236 mask |= (1ULL << i);
3237
3238 if (level > 2)
3239 /* bits 7:3 reserved */
3240 mask |= 0xf8;
3241 else if (level == 2) {
3242 if (spte & (1ULL << 7))
3243 /* 2MB ref, bits 20:12 reserved */
3244 mask |= 0x1ff000;
3245 else
3246 /* bits 6:3 reserved */
3247 mask |= 0x78;
3248 }
3249
3250 return mask;
3251}
3252
3253static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
3254 int level)
3255{
3256 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
3257
3258 /* 010b (write-only) */
3259 WARN_ON((spte & 0x7) == 0x2);
3260
3261 /* 110b (write/execute) */
3262 WARN_ON((spte & 0x7) == 0x6);
3263
3264 /* 100b (execute-only) and value not supported by logical processor */
3265 if (!cpu_has_vmx_ept_execute_only())
3266 WARN_ON((spte & 0x7) == 0x4);
3267
3268 /* not 000b */
3269 if ((spte & 0x7)) {
3270 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
3271
3272 if (rsvd_bits != 0) {
3273 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
3274 __func__, rsvd_bits);
3275 WARN_ON(1);
3276 }
3277
3278 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
3279 u64 ept_mem_type = (spte & 0x38) >> 3;
3280
3281 if (ept_mem_type == 2 || ept_mem_type == 3 ||
3282 ept_mem_type == 7) {
3283 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
3284 __func__, ept_mem_type);
3285 WARN_ON(1);
3286 }
3287 }
3288 }
3289}
3290
3291static int handle_ept_misconfig(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3292{
3293 u64 sptes[4];
3294 int nr_sptes, i;
3295 gpa_t gpa;
3296
3297 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3298
3299 printk(KERN_ERR "EPT: Misconfiguration.\n");
3300 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
3301
3302 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
3303
3304 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
3305 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
3306
3307 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3308 kvm_run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
3309
3310 return 0;
3311}
3312
Sheng Yangf08864b2008-05-15 18:23:25 +08003313static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3314{
3315 u32 cpu_based_vm_exec_control;
3316
3317 /* clear pending NMI */
3318 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3319 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3320 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3321 ++vcpu->stat.nmi_window_exits;
3322
3323 return 1;
3324}
3325
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003326static void handle_invalid_guest_state(struct kvm_vcpu *vcpu,
3327 struct kvm_run *kvm_run)
3328{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003329 struct vcpu_vmx *vmx = to_vmx(vcpu);
3330 enum emulation_result err = EMULATE_DONE;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003331
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003332 local_irq_enable();
Jan Kiszka34f0c1a2009-07-22 23:53:26 +02003333 preempt_enable();
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003334
3335 while (!guest_state_valid(vcpu)) {
3336 err = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3337
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003338 if (err == EMULATE_DO_MMIO)
3339 break;
3340
3341 if (err != EMULATE_DONE) {
3342 kvm_report_emulation_failure(vcpu, "emulation failure");
Jan Kiszka263799a2009-07-21 10:43:07 +02003343 break;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003344 }
3345
3346 if (signal_pending(current))
3347 break;
3348 if (need_resched())
3349 schedule();
3350 }
3351
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003352 preempt_disable();
Jan Kiszka34f0c1a2009-07-22 23:53:26 +02003353 local_irq_disable();
Avi Kivity8b3079a2009-01-05 12:10:54 +02003354
3355 vmx->invalid_state_emulation_result = err;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003356}
3357
Avi Kivity6aa8b732006-12-10 02:21:36 -08003358/*
3359 * The exit handlers return 1 if the exit was handled fully and guest execution
3360 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3361 * to be done to userspace and return 0.
3362 */
3363static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
3364 struct kvm_run *kvm_run) = {
3365 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3366 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003367 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003368 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003369 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003370 [EXIT_REASON_CR_ACCESS] = handle_cr,
3371 [EXIT_REASON_DR_ACCESS] = handle_dr,
3372 [EXIT_REASON_CPUID] = handle_cpuid,
3373 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3374 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3375 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3376 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003377 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003378 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003379 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3380 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3381 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3382 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3383 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3384 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3385 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3386 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3387 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003388 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3389 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003390 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003391 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08003392 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003393 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
3394 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003395};
3396
3397static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003398 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003399
3400/*
3401 * The guest has exited. See if we can fix it or if we need userspace
3402 * assistance.
3403 */
Avi Kivity6062d012009-03-23 17:35:17 +02003404static int vmx_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003405{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003406 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003407 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003408 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003409
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003410 trace_kvm_exit(exit_reason, kvm_rip_read(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003411
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003412 /* If we need to emulate an MMIO from handle_invalid_guest_state
3413 * we just return 0 */
Avi Kivity10f32d82009-01-05 00:53:19 +02003414 if (vmx->emulation_required && emulate_invalid_guest_state) {
3415 if (guest_state_valid(vcpu))
3416 vmx->emulation_required = 0;
Avi Kivity8b3079a2009-01-05 12:10:54 +02003417 return vmx->invalid_state_emulation_result != EMULATE_DO_MMIO;
Avi Kivity10f32d82009-01-05 00:53:19 +02003418 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003419
Sheng Yang14394422008-04-28 12:24:45 +08003420 /* Access CR3 don't cause VMExit in paging mode, so we need
3421 * to sync with guest real CR3. */
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03003422 if (enable_ept && is_paging(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08003423 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003424
Avi Kivity29bd8a72007-09-10 17:27:03 +03003425 if (unlikely(vmx->fail)) {
3426 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3427 kvm_run->fail_entry.hardware_entry_failure_reason
3428 = vmcs_read32(VM_INSTRUCTION_ERROR);
3429 return 0;
3430 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003431
Mike Dayd77c26f2007-10-08 09:02:08 -04003432 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003433 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003434 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3435 exit_reason != EXIT_REASON_TASK_SWITCH))
3436 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3437 "(0x%x) and exit reason is 0x%x\n",
3438 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003439
3440 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03003441 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003442 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003443 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003444 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003445 /*
3446 * This CPU don't support us in finding the end of an
3447 * NMI-blocked window if the guest runs with IRQs
3448 * disabled. So we pull the trigger after 1 s of
3449 * futile waiting, but inform the user about this.
3450 */
3451 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3452 "state on VCPU %d after 1 s timeout\n",
3453 __func__, vcpu->vcpu_id);
3454 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003455 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003456 }
3457
Avi Kivity6aa8b732006-12-10 02:21:36 -08003458 if (exit_reason < kvm_vmx_max_exit_handlers
3459 && kvm_vmx_exit_handlers[exit_reason])
3460 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
3461 else {
3462 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3463 kvm_run->hw.hardware_exit_reason = exit_reason;
3464 }
3465 return 0;
3466}
3467
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003468static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003469{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003470 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003471 vmcs_write32(TPR_THRESHOLD, 0);
3472 return;
3473 }
3474
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003475 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003476}
3477
Avi Kivitycf393f72008-07-01 16:20:21 +03003478static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3479{
3480 u32 exit_intr_info;
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003481 u32 idt_vectoring_info = vmx->idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003482 bool unblock_nmi;
3483 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003484 int type;
3485 bool idtv_info_valid;
Avi Kivitycf393f72008-07-01 16:20:21 +03003486
3487 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Gleb Natapov20f65982009-05-11 13:35:55 +03003488
Andi Kleena0861c02009-06-08 17:37:09 +08003489 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
3490
3491 /* Handle machine checks before interrupts are enabled */
3492 if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
3493 || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
3494 && is_machine_check(exit_intr_info)))
3495 kvm_machine_check();
3496
Gleb Natapov20f65982009-05-11 13:35:55 +03003497 /* We need to handle NMIs before interrupts are enabled */
3498 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003499 (exit_intr_info & INTR_INFO_VALID_MASK))
Gleb Natapov20f65982009-05-11 13:35:55 +03003500 asm("int $2");
Gleb Natapov20f65982009-05-11 13:35:55 +03003501
3502 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3503
Avi Kivitycf393f72008-07-01 16:20:21 +03003504 if (cpu_has_virtual_nmis()) {
3505 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3506 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3507 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003508 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03003509 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3510 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003511 * SDM 3: 23.2.2 (September 2008)
3512 * Bit 12 is undefined in any of the following cases:
3513 * If the VM exit sets the valid bit in the IDT-vectoring
3514 * information field.
3515 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03003516 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003517 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
3518 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03003519 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3520 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003521 } else if (unlikely(vmx->soft_vnmi_blocked))
3522 vmx->vnmi_blocked_time +=
3523 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003524
Gleb Natapov37b96e92009-03-30 16:03:13 +03003525 vmx->vcpu.arch.nmi_injected = false;
3526 kvm_clear_exception_queue(&vmx->vcpu);
3527 kvm_clear_interrupt_queue(&vmx->vcpu);
3528
3529 if (!idtv_info_valid)
3530 return;
3531
Avi Kivity668f6122008-07-02 09:28:55 +03003532 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3533 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003534
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003535 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003536 case INTR_TYPE_NMI_INTR:
3537 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03003538 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003539 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03003540 * Clear bit "block by NMI" before VM entry if a NMI
3541 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03003542 */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003543 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3544 GUEST_INTR_STATE_NMI);
3545 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003546 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003547 vmx->vcpu.arch.event_exit_inst_len =
3548 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3549 /* fall through */
3550 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03003551 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003552 u32 err = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3553 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03003554 } else
3555 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003556 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003557 case INTR_TYPE_SOFT_INTR:
3558 vmx->vcpu.arch.event_exit_inst_len =
3559 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3560 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003561 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003562 kvm_queue_interrupt(&vmx->vcpu, vector,
3563 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003564 break;
3565 default:
3566 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03003567 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003568}
3569
Avi Kivity9c8cba32007-11-22 11:42:59 +02003570/*
3571 * Failure to inject an interrupt should give us the information
3572 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3573 * when fetching the interrupt redirection bitmap in the real-mode
3574 * tss, this doesn't happen. So we do it ourselves.
3575 */
3576static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3577{
3578 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003579 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003580 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003581 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003582 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3583 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3584 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3585 return;
3586 }
3587 vmx->idt_vectoring_info =
3588 VECTORING_INFO_VALID_MASK
3589 | INTR_TYPE_EXT_INTR
3590 | vmx->rmode.irq.vector;
3591}
3592
Avi Kivityc8019492008-07-14 14:44:59 +03003593#ifdef CONFIG_X86_64
3594#define R "r"
3595#define Q "q"
3596#else
3597#define R "e"
3598#define Q "l"
3599#endif
3600
Avi Kivity04d2cc72007-09-10 18:10:54 +03003601static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003602{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003603 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitye6adf282007-04-30 16:07:54 +03003604
Avi Kivity8f5d5492009-05-31 18:41:29 +03003605 if (enable_ept && is_paging(vcpu)) {
3606 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3607 ept_load_pdptrs(vcpu);
3608 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003609 /* Record the guest's net vcpu time for enforced NMI injections. */
3610 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3611 vmx->entry_time = ktime_get();
3612
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003613 /* Handle invalid guest state instead of entering VMX */
3614 if (vmx->emulation_required && emulate_invalid_guest_state) {
3615 handle_invalid_guest_state(vcpu, kvm_run);
3616 return;
3617 }
3618
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003619 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3620 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3621 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3622 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3623
Gleb Natapov787ff732009-05-18 11:44:06 +03003624 /* When single-stepping over STI and MOV SS, we must clear the
3625 * corresponding interruptibility bits in the guest state. Otherwise
3626 * vmentry fails as it then expects bit 14 (BS) in pending debug
3627 * exceptions being set, but that's not correct for the guest debugging
3628 * case. */
3629 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3630 vmx_set_interrupt_shadow(vcpu, 0);
3631
Avi Kivitye6adf282007-04-30 16:07:54 +03003632 /*
3633 * Loading guest fpu may have cleared host cr0.ts
3634 */
3635 vmcs_writel(HOST_CR0, read_cr0());
3636
Avi Kivitye8a48342009-09-01 16:06:25 +03003637 if (vcpu->arch.switch_db_regs)
3638 set_debugreg(vcpu->arch.dr6, 6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003639
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
Avi Kivitye8a48342009-09-01 16:06:25 +03003740 if (vcpu->arch.switch_db_regs)
3741 get_debugreg(vcpu->arch.dr6, 6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003742
Avi Kivity1155f762007-11-22 11:30:47 +02003743 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003744 if (vmx->rmode.irq.pending)
3745 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003746
Mike Dayd77c26f2007-10-08 09:02:08 -04003747 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003748 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003749
Avi Kivitycf393f72008-07-01 16:20:21 +03003750 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003751}
3752
Avi Kivityc8019492008-07-14 14:44:59 +03003753#undef R
3754#undef Q
3755
Avi Kivity6aa8b732006-12-10 02:21:36 -08003756static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3757{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003758 struct vcpu_vmx *vmx = to_vmx(vcpu);
3759
3760 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003761 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003762 free_vmcs(vmx->vmcs);
3763 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003764 }
3765}
3766
3767static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3768{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003769 struct vcpu_vmx *vmx = to_vmx(vcpu);
3770
Sheng Yang2384d2b2008-01-17 15:14:33 +08003771 spin_lock(&vmx_vpid_lock);
3772 if (vmx->vpid != 0)
3773 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3774 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003775 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003776 kfree(vmx->host_msrs);
3777 kfree(vmx->guest_msrs);
3778 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003779 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003780}
3781
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003782static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003783{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003784 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003785 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003786 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003787
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003788 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003789 return ERR_PTR(-ENOMEM);
3790
Sheng Yang2384d2b2008-01-17 15:14:33 +08003791 allocate_vpid(vmx);
3792
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003793 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3794 if (err)
3795 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003796
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003797 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003798 if (!vmx->guest_msrs) {
3799 err = -ENOMEM;
3800 goto uninit_vcpu;
3801 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003802
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003803 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3804 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003805 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003806
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003807 vmx->vmcs = alloc_vmcs();
3808 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003809 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003810
3811 vmcs_clear(vmx->vmcs);
3812
Avi Kivity15ad7142007-07-11 18:17:21 +03003813 cpu = get_cpu();
3814 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003815 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003816 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003817 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003818 if (err)
3819 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003820 if (vm_need_virtualize_apic_accesses(kvm))
3821 if (alloc_apic_access_page(kvm) != 0)
3822 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003823
Sheng Yangb927a3c2009-07-21 10:42:48 +08003824 if (enable_ept) {
3825 if (!kvm->arch.ept_identity_map_addr)
3826 kvm->arch.ept_identity_map_addr =
3827 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003828 if (alloc_identity_pagetable(kvm) != 0)
3829 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003830 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003831
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003832 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003833
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003834free_vmcs:
3835 free_vmcs(vmx->vmcs);
3836free_msrs:
3837 kfree(vmx->host_msrs);
3838free_guest_msrs:
3839 kfree(vmx->guest_msrs);
3840uninit_vcpu:
3841 kvm_vcpu_uninit(&vmx->vcpu);
3842free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003843 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003844 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003845}
3846
Yang, Sheng002c7f72007-07-31 14:23:01 +03003847static void __init vmx_check_processor_compat(void *rtn)
3848{
3849 struct vmcs_config vmcs_conf;
3850
3851 *(int *)rtn = 0;
3852 if (setup_vmcs_config(&vmcs_conf) < 0)
3853 *(int *)rtn = -EIO;
3854 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3855 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3856 smp_processor_id());
3857 *(int *)rtn = -EIO;
3858 }
3859}
3860
Sheng Yang67253af2008-04-25 10:20:22 +08003861static int get_ept_level(void)
3862{
3863 return VMX_EPT_DEFAULT_GAW + 1;
3864}
3865
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003866static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003867{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003868 u64 ret;
3869
Sheng Yang522c68c2009-04-27 20:35:43 +08003870 /* For VT-d and EPT combination
3871 * 1. MMIO: always map as UC
3872 * 2. EPT with VT-d:
3873 * a. VT-d without snooping control feature: can't guarantee the
3874 * result, try to trust guest.
3875 * b. VT-d with snooping control feature: snooping control feature of
3876 * VT-d engine can guarantee the cache correctness. Just set it
3877 * to WB to keep consistent with host. So the same as item 3.
3878 * 3. EPT without VT-d: always map as WB and set IGMT=1 to keep
3879 * consistent with host MTRR
3880 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003881 if (is_mmio)
3882 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08003883 else if (vcpu->kvm->arch.iommu_domain &&
3884 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
3885 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
3886 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003887 else
Sheng Yang522c68c2009-04-27 20:35:43 +08003888 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
3889 | VMX_EPT_IGMT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003890
3891 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08003892}
3893
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003894static const struct trace_print_flags vmx_exit_reasons_str[] = {
3895 { EXIT_REASON_EXCEPTION_NMI, "exception" },
3896 { EXIT_REASON_EXTERNAL_INTERRUPT, "ext_irq" },
3897 { EXIT_REASON_TRIPLE_FAULT, "triple_fault" },
3898 { EXIT_REASON_NMI_WINDOW, "nmi_window" },
3899 { EXIT_REASON_IO_INSTRUCTION, "io_instruction" },
3900 { EXIT_REASON_CR_ACCESS, "cr_access" },
3901 { EXIT_REASON_DR_ACCESS, "dr_access" },
3902 { EXIT_REASON_CPUID, "cpuid" },
3903 { EXIT_REASON_MSR_READ, "rdmsr" },
3904 { EXIT_REASON_MSR_WRITE, "wrmsr" },
3905 { EXIT_REASON_PENDING_INTERRUPT, "interrupt_window" },
3906 { EXIT_REASON_HLT, "halt" },
3907 { EXIT_REASON_INVLPG, "invlpg" },
3908 { EXIT_REASON_VMCALL, "hypercall" },
3909 { EXIT_REASON_TPR_BELOW_THRESHOLD, "tpr_below_thres" },
3910 { EXIT_REASON_APIC_ACCESS, "apic_access" },
3911 { EXIT_REASON_WBINVD, "wbinvd" },
3912 { EXIT_REASON_TASK_SWITCH, "task_switch" },
3913 { EXIT_REASON_EPT_VIOLATION, "ept_violation" },
3914 { -1, NULL }
3915};
3916
Joerg Roedel344f4142009-07-27 16:30:48 +02003917static bool vmx_gb_page_enable(void)
3918{
3919 return false;
3920}
3921
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003922static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003923 .cpu_has_kvm_support = cpu_has_kvm_support,
3924 .disabled_by_bios = vmx_disabled_by_bios,
3925 .hardware_setup = hardware_setup,
3926 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003927 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003928 .hardware_enable = hardware_enable,
3929 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08003930 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003931
3932 .vcpu_create = vmx_create_vcpu,
3933 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003934 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003935
Avi Kivity04d2cc72007-09-10 18:10:54 +03003936 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003937 .vcpu_load = vmx_vcpu_load,
3938 .vcpu_put = vmx_vcpu_put,
3939
3940 .set_guest_debug = set_guest_debug,
3941 .get_msr = vmx_get_msr,
3942 .set_msr = vmx_set_msr,
3943 .get_segment_base = vmx_get_segment_base,
3944 .get_segment = vmx_get_segment,
3945 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003946 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003947 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003948 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003949 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003950 .set_cr3 = vmx_set_cr3,
3951 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003952 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003953 .get_idt = vmx_get_idt,
3954 .set_idt = vmx_set_idt,
3955 .get_gdt = vmx_get_gdt,
3956 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003957 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003958 .get_rflags = vmx_get_rflags,
3959 .set_rflags = vmx_set_rflags,
3960
3961 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003962
Avi Kivity6aa8b732006-12-10 02:21:36 -08003963 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02003964 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003965 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003966 .set_interrupt_shadow = vmx_set_interrupt_shadow,
3967 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003968 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003969 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003970 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003971 .queue_exception = vmx_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003972 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003973 .nmi_allowed = vmx_nmi_allowed,
3974 .enable_nmi_window = enable_nmi_window,
3975 .enable_irq_window = enable_irq_window,
3976 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003977
Izik Eiduscbc94022007-10-25 00:29:55 +02003978 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003979 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003980 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003981
3982 .exit_reasons_str = vmx_exit_reasons_str,
Joerg Roedel344f4142009-07-27 16:30:48 +02003983 .gb_page_enable = vmx_gb_page_enable,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003984};
3985
3986static int __init vmx_init(void)
3987{
He, Qingfdef3ad2007-04-30 09:45:24 +03003988 int r;
3989
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003990 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03003991 if (!vmx_io_bitmap_a)
3992 return -ENOMEM;
3993
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003994 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03003995 if (!vmx_io_bitmap_b) {
3996 r = -ENOMEM;
3997 goto out;
3998 }
3999
Avi Kivity58972972009-02-24 22:26:47 +02004000 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
4001 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08004002 r = -ENOMEM;
4003 goto out1;
4004 }
4005
Avi Kivity58972972009-02-24 22:26:47 +02004006 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
4007 if (!vmx_msr_bitmap_longmode) {
4008 r = -ENOMEM;
4009 goto out2;
4010 }
4011
He, Qingfdef3ad2007-04-30 09:45:24 +03004012 /*
4013 * Allow direct access to the PC debug port (it is often used for I/O
4014 * delays, but the vmexits simply slow things down).
4015 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004016 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
4017 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004018
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004019 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004020
Avi Kivity58972972009-02-24 22:26:47 +02004021 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
4022 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08004023
Sheng Yang2384d2b2008-01-17 15:14:33 +08004024 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
4025
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004026 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004027 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02004028 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08004029
Avi Kivity58972972009-02-24 22:26:47 +02004030 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
4031 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
4032 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
4033 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
4034 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
4035 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03004036
Avi Kivity089d0342009-03-23 18:26:32 +02004037 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004038 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004039 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08004040 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08004041 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004042 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004043 kvm_enable_tdp();
4044 } else
4045 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08004046
Avi Kivityc7addb92007-09-16 18:58:32 +02004047 if (bypass_guest_pf)
4048 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
4049
Sheng Yang14394422008-04-28 12:24:45 +08004050 ept_sync_global();
4051
He, Qingfdef3ad2007-04-30 09:45:24 +03004052 return 0;
4053
Avi Kivity58972972009-02-24 22:26:47 +02004054out3:
4055 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08004056out2:
Avi Kivity58972972009-02-24 22:26:47 +02004057 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03004058out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004059 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03004060out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004061 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004062 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004063}
4064
4065static void __exit vmx_exit(void)
4066{
Avi Kivity58972972009-02-24 22:26:47 +02004067 free_page((unsigned long)vmx_msr_bitmap_legacy);
4068 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004069 free_page((unsigned long)vmx_io_bitmap_b);
4070 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004071
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004072 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004073}
4074
4075module_init(vmx_init)
4076module_exit(vmx_exit)