blob: f0f9773f0b0f9079329edb0057f410945cd2ff06 [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 Tosatti5fdbf972008-06-27 14:58:02 -030028#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030029#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080032#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020033#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020034#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080035#include <asm/mce.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080036
Avi Kivity4ecac3f2008-05-13 13:23:38 +030037#define __ex(x) __kvm_handle_fault_on_reboot(x)
38
Avi Kivity6aa8b732006-12-10 02:21:36 -080039MODULE_AUTHOR("Qumranet");
40MODULE_LICENSE("GPL");
41
Avi Kivity4462d212009-03-23 17:53:37 +020042static int __read_mostly bypass_guest_pf = 1;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020043module_param(bypass_guest_pf, bool, S_IRUGO);
Avi Kivityc7addb92007-09-16 18:58:32 +020044
Avi Kivity4462d212009-03-23 17:53:37 +020045static int __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020046module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080047
Avi Kivity4462d212009-03-23 17:53:37 +020048static int __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020049module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020050
Avi Kivity4462d212009-03-23 17:53:37 +020051static int __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020052module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080053
Nitin A Kamble3a624e22009-06-08 11:34:16 -070054static int __read_mostly enable_unrestricted_guest = 1;
55module_param_named(unrestricted_guest,
56 enable_unrestricted_guest, bool, S_IRUGO);
57
Avi Kivity4462d212009-03-23 17:53:37 +020058static int __read_mostly emulate_invalid_guest_state = 0;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020059module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030060
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040061struct vmcs {
62 u32 revision_id;
63 u32 abort;
64 char data[0];
65};
66
67struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100068 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +030069 struct list_head local_vcpus_link;
Avi Kivity313dbd42008-07-17 18:04:30 +030070 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040071 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030072 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020073 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040074 struct kvm_msr_entry *guest_msrs;
75 struct kvm_msr_entry *host_msrs;
76 int nmsrs;
77 int save_nmsrs;
78 int msr_offset_efer;
79#ifdef CONFIG_X86_64
80 int msr_offset_kernel_gs_base;
81#endif
82 struct vmcs *vmcs;
83 struct {
84 int loaded;
85 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020086 int gs_ldt_reload_needed;
87 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030088 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040089 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +020090 struct {
91 struct {
92 bool pending;
93 u8 vector;
94 unsigned rip;
95 } irq;
96 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +080097 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030098 bool emulation_required;
Avi Kivity8b3079a2009-01-05 12:10:54 +020099 enum emulation_result invalid_state_emulation_result;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200100
101 /* Support for vnmi-less CPUs */
102 int soft_vnmi_blocked;
103 ktime_t entry_time;
104 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800105 u32 exit_reason;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400106};
107
108static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
109{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000110 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400111}
112
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800113static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800114static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300115
Avi Kivity6aa8b732006-12-10 02:21:36 -0800116static DEFINE_PER_CPU(struct vmcs *, vmxarea);
117static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300118static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200120static unsigned long *vmx_io_bitmap_a;
121static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200122static unsigned long *vmx_msr_bitmap_legacy;
123static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300124
Sheng Yang2384d2b2008-01-17 15:14:33 +0800125static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
126static DEFINE_SPINLOCK(vmx_vpid_lock);
127
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300128static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800129 int size;
130 int order;
131 u32 revision_id;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300132 u32 pin_based_exec_ctrl;
133 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800134 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300135 u32 vmexit_ctrl;
136 u32 vmentry_ctrl;
137} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800138
Hannes Ederefff9e52008-11-28 17:02:06 +0100139static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800140 u32 ept;
141 u32 vpid;
142} vmx_capability;
143
Avi Kivity6aa8b732006-12-10 02:21:36 -0800144#define VMX_SEGMENT_FIELD(seg) \
145 [VCPU_SREG_##seg] = { \
146 .selector = GUEST_##seg##_SELECTOR, \
147 .base = GUEST_##seg##_BASE, \
148 .limit = GUEST_##seg##_LIMIT, \
149 .ar_bytes = GUEST_##seg##_AR_BYTES, \
150 }
151
152static struct kvm_vmx_segment_field {
153 unsigned selector;
154 unsigned base;
155 unsigned limit;
156 unsigned ar_bytes;
157} kvm_vmx_segment_fields[] = {
158 VMX_SEGMENT_FIELD(CS),
159 VMX_SEGMENT_FIELD(DS),
160 VMX_SEGMENT_FIELD(ES),
161 VMX_SEGMENT_FIELD(FS),
162 VMX_SEGMENT_FIELD(GS),
163 VMX_SEGMENT_FIELD(SS),
164 VMX_SEGMENT_FIELD(TR),
165 VMX_SEGMENT_FIELD(LDTR),
166};
167
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300168static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
169
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300170/*
171 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
172 * away by decrementing the array size.
173 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800175#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800176 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
177#endif
178 MSR_EFER, MSR_K6_STAR,
179};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200180#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400182static void load_msrs(struct kvm_msr_entry *e, int n)
183{
184 int i;
185
186 for (i = 0; i < n; ++i)
187 wrmsrl(e[i].index, e[i].data);
188}
189
190static void save_msrs(struct kvm_msr_entry *e, int n)
191{
192 int i;
193
194 for (i = 0; i < n; ++i)
195 rdmsrl(e[i].index, e[i].data);
196}
197
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198static inline int is_page_fault(u32 intr_info)
199{
200 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
201 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100202 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203}
204
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300205static inline int is_no_device(u32 intr_info)
206{
207 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
208 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100209 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300210}
211
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500212static inline int is_invalid_opcode(u32 intr_info)
213{
214 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
215 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100216 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500217}
218
Avi Kivity6aa8b732006-12-10 02:21:36 -0800219static inline int is_external_interrupt(u32 intr_info)
220{
221 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
222 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
223}
224
Andi Kleena0861c02009-06-08 17:37:09 +0800225static inline int is_machine_check(u32 intr_info)
226{
227 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
228 INTR_INFO_VALID_MASK)) ==
229 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
230}
231
Sheng Yang25c5f222008-03-28 13:18:56 +0800232static inline int cpu_has_vmx_msr_bitmap(void)
233{
Sheng Yang04547152009-04-01 15:52:31 +0800234 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800235}
236
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800237static inline int cpu_has_vmx_tpr_shadow(void)
238{
Sheng Yang04547152009-04-01 15:52:31 +0800239 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800240}
241
242static inline int vm_need_tpr_shadow(struct kvm *kvm)
243{
Sheng Yang04547152009-04-01 15:52:31 +0800244 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800245}
246
Sheng Yangf78e0e22007-10-29 09:40:42 +0800247static inline int cpu_has_secondary_exec_ctrls(void)
248{
Sheng Yang04547152009-04-01 15:52:31 +0800249 return vmcs_config.cpu_based_exec_ctrl &
250 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800251}
252
Avi Kivity774ead32007-12-26 13:57:04 +0200253static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800254{
Sheng Yang04547152009-04-01 15:52:31 +0800255 return vmcs_config.cpu_based_2nd_exec_ctrl &
256 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
257}
258
259static inline bool cpu_has_vmx_flexpriority(void)
260{
261 return cpu_has_vmx_tpr_shadow() &&
262 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800263}
264
Sheng Yangd56f5462008-04-25 10:13:16 +0800265static inline int cpu_has_vmx_invept_individual_addr(void)
266{
Sheng Yang04547152009-04-01 15:52:31 +0800267 return !!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800268}
269
270static inline int cpu_has_vmx_invept_context(void)
271{
Sheng Yang04547152009-04-01 15:52:31 +0800272 return !!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800273}
274
275static inline int cpu_has_vmx_invept_global(void)
276{
Sheng Yang04547152009-04-01 15:52:31 +0800277 return !!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800278}
279
280static inline int cpu_has_vmx_ept(void)
281{
Sheng Yang04547152009-04-01 15:52:31 +0800282 return vmcs_config.cpu_based_2nd_exec_ctrl &
283 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800284}
285
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700286static inline int cpu_has_vmx_unrestricted_guest(void)
287{
288 return vmcs_config.cpu_based_2nd_exec_ctrl &
289 SECONDARY_EXEC_UNRESTRICTED_GUEST;
290}
291
Sheng Yangf78e0e22007-10-29 09:40:42 +0800292static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
293{
Sheng Yang04547152009-04-01 15:52:31 +0800294 return flexpriority_enabled &&
295 (cpu_has_vmx_virtualize_apic_accesses()) &&
296 (irqchip_in_kernel(kvm));
Sheng Yangf78e0e22007-10-29 09:40:42 +0800297}
298
Sheng Yang2384d2b2008-01-17 15:14:33 +0800299static inline int cpu_has_vmx_vpid(void)
300{
Sheng Yang04547152009-04-01 15:52:31 +0800301 return vmcs_config.cpu_based_2nd_exec_ctrl &
302 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800303}
304
Sheng Yangf08864b2008-05-15 18:23:25 +0800305static inline int cpu_has_virtual_nmis(void)
306{
307 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
308}
309
Sheng Yang04547152009-04-01 15:52:31 +0800310static inline bool report_flexpriority(void)
311{
312 return flexpriority_enabled;
313}
314
Rusty Russell8b9cf982007-07-30 16:31:43 +1000315static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800316{
317 int i;
318
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400319 for (i = 0; i < vmx->nmsrs; ++i)
320 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300321 return i;
322 return -1;
323}
324
Sheng Yang2384d2b2008-01-17 15:14:33 +0800325static inline void __invvpid(int ext, u16 vpid, gva_t gva)
326{
327 struct {
328 u64 vpid : 16;
329 u64 rsvd : 48;
330 u64 gva;
331 } operand = { vpid, 0, gva };
332
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300333 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800334 /* CF==1 or ZF==1 --> rc = -1 */
335 "; ja 1f ; ud2 ; 1:"
336 : : "a"(&operand), "c"(ext) : "cc", "memory");
337}
338
Sheng Yang14394422008-04-28 12:24:45 +0800339static inline void __invept(int ext, u64 eptp, gpa_t gpa)
340{
341 struct {
342 u64 eptp, gpa;
343 } operand = {eptp, gpa};
344
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300345 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800346 /* CF==1 or ZF==1 --> rc = -1 */
347 "; ja 1f ; ud2 ; 1:\n"
348 : : "a" (&operand), "c" (ext) : "cc", "memory");
349}
350
Rusty Russell8b9cf982007-07-30 16:31:43 +1000351static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300352{
353 int i;
354
Rusty Russell8b9cf982007-07-30 16:31:43 +1000355 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300356 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400357 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000358 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800359}
360
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361static void vmcs_clear(struct vmcs *vmcs)
362{
363 u64 phys_addr = __pa(vmcs);
364 u8 error;
365
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300366 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
368 : "cc", "memory");
369 if (error)
370 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
371 vmcs, phys_addr);
372}
373
374static void __vcpu_clear(void *arg)
375{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000376 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800377 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378
Rusty Russell8b9cf982007-07-30 16:31:43 +1000379 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400380 vmcs_clear(vmx->vmcs);
381 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800382 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800383 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300384 list_del(&vmx->local_vcpus_link);
385 vmx->vcpu.cpu = -1;
386 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800387}
388
Rusty Russell8b9cf982007-07-30 16:31:43 +1000389static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800390{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200391 if (vmx->vcpu.cpu == -1)
392 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200393 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800394}
395
Sheng Yang2384d2b2008-01-17 15:14:33 +0800396static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
397{
398 if (vmx->vpid == 0)
399 return;
400
401 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
402}
403
Sheng Yang14394422008-04-28 12:24:45 +0800404static inline void ept_sync_global(void)
405{
406 if (cpu_has_vmx_invept_global())
407 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
408}
409
410static inline void ept_sync_context(u64 eptp)
411{
Avi Kivity089d0342009-03-23 18:26:32 +0200412 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800413 if (cpu_has_vmx_invept_context())
414 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
415 else
416 ept_sync_global();
417 }
418}
419
420static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
421{
Avi Kivity089d0342009-03-23 18:26:32 +0200422 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800423 if (cpu_has_vmx_invept_individual_addr())
424 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
425 eptp, gpa);
426 else
427 ept_sync_context(eptp);
428 }
429}
430
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431static unsigned long vmcs_readl(unsigned long field)
432{
433 unsigned long value;
434
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300435 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436 : "=a"(value) : "d"(field) : "cc");
437 return value;
438}
439
440static u16 vmcs_read16(unsigned long field)
441{
442 return vmcs_readl(field);
443}
444
445static u32 vmcs_read32(unsigned long field)
446{
447 return vmcs_readl(field);
448}
449
450static u64 vmcs_read64(unsigned long field)
451{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800452#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800453 return vmcs_readl(field);
454#else
455 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
456#endif
457}
458
Avi Kivitye52de1b2007-01-05 16:36:56 -0800459static noinline void vmwrite_error(unsigned long field, unsigned long value)
460{
461 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
462 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
463 dump_stack();
464}
465
Avi Kivity6aa8b732006-12-10 02:21:36 -0800466static void vmcs_writel(unsigned long field, unsigned long value)
467{
468 u8 error;
469
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300470 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400471 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800472 if (unlikely(error))
473 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800474}
475
476static void vmcs_write16(unsigned long field, u16 value)
477{
478 vmcs_writel(field, value);
479}
480
481static void vmcs_write32(unsigned long field, u32 value)
482{
483 vmcs_writel(field, value);
484}
485
486static void vmcs_write64(unsigned long field, u64 value)
487{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800488 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300489#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800490 asm volatile ("");
491 vmcs_writel(field+1, value >> 32);
492#endif
493}
494
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300495static void vmcs_clear_bits(unsigned long field, u32 mask)
496{
497 vmcs_writel(field, vmcs_readl(field) & ~mask);
498}
499
500static void vmcs_set_bits(unsigned long field, u32 mask)
501{
502 vmcs_writel(field, vmcs_readl(field) | mask);
503}
504
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300505static void update_exception_bitmap(struct kvm_vcpu *vcpu)
506{
507 u32 eb;
508
Andi Kleena0861c02009-06-08 17:37:09 +0800509 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300510 if (!vcpu->fpu_active)
511 eb |= 1u << NM_VECTOR;
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100512 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
513 if (vcpu->guest_debug &
514 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
515 eb |= 1u << DB_VECTOR;
516 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
517 eb |= 1u << BP_VECTOR;
518 }
Nitin A Kamble56b237e2009-06-04 15:04:08 -0700519 if (vcpu->arch.rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300520 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200521 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800522 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300523 vmcs_write32(EXCEPTION_BITMAP, eb);
524}
525
Avi Kivity33ed6322007-05-02 16:54:03 +0300526static void reload_tss(void)
527{
Avi Kivity33ed6322007-05-02 16:54:03 +0300528 /*
529 * VT restores TR but not its size. Useless.
530 */
531 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200532 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300533
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300534 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300535 descs = (void *)gdt.base;
536 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
537 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300538}
539
Rusty Russell8b9cf982007-07-30 16:31:43 +1000540static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300541{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400542 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300543 u64 host_efer = vmx->host_msrs[efer_offset].data;
544 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
545 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300546
Avi Kivity51c6cf62007-08-29 03:48:05 +0300547 if (efer_offset < 0)
548 return;
549 /*
550 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
551 * outside long mode
552 */
553 ignore_bits = EFER_NX | EFER_SCE;
554#ifdef CONFIG_X86_64
555 ignore_bits |= EFER_LMA | EFER_LME;
556 /* SCE is meaningful only in long mode on Intel */
557 if (guest_efer & EFER_LMA)
558 ignore_bits &= ~(u64)EFER_SCE;
559#endif
560 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
561 return;
562
563 vmx->host_state.guest_efer_loaded = 1;
564 guest_efer &= ~ignore_bits;
565 guest_efer |= host_efer & ignore_bits;
566 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000567 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300568}
569
Avi Kivity51c6cf62007-08-29 03:48:05 +0300570static void reload_host_efer(struct vcpu_vmx *vmx)
571{
572 if (vmx->host_state.guest_efer_loaded) {
573 vmx->host_state.guest_efer_loaded = 0;
574 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
575 }
576}
577
Avi Kivity04d2cc72007-09-10 18:10:54 +0300578static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300579{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300580 struct vcpu_vmx *vmx = to_vmx(vcpu);
581
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400582 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300583 return;
584
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400585 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300586 /*
587 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
588 * allow segment selectors with cpl > 0 or ti == 1.
589 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300590 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200591 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300592 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200593 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400594 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200595 vmx->host_state.fs_reload_needed = 0;
596 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300597 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200598 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300599 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300600 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400601 if (!(vmx->host_state.gs_sel & 7))
602 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300603 else {
604 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200605 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300606 }
607
608#ifdef CONFIG_X86_64
609 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
610 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
611#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400612 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
613 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300614#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300615
616#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400617 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400618 save_msrs(vmx->host_msrs +
619 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400620
Avi Kivity707c0872007-05-02 17:33:43 +0300621#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400622 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300623 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300624}
625
Avi Kivitya9b21b62008-06-24 11:48:49 +0300626static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300627{
Avi Kivity15ad7142007-07-11 18:17:21 +0300628 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300629
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400630 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300631 return;
632
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200633 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400634 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200635 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300636 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200637 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300638 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300639 /*
640 * If we have to reload gs, we must take care to
641 * preserve our gs base.
642 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300643 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300644 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300645#ifdef CONFIG_X86_64
646 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
647#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300648 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300649 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200650 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400651 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
652 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300653 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300654}
655
Avi Kivitya9b21b62008-06-24 11:48:49 +0300656static void vmx_load_host_state(struct vcpu_vmx *vmx)
657{
658 preempt_disable();
659 __vmx_load_host_state(vmx);
660 preempt_enable();
661}
662
Avi Kivity6aa8b732006-12-10 02:21:36 -0800663/*
664 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
665 * vcpu mutex is already taken.
666 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300667static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800668{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400669 struct vcpu_vmx *vmx = to_vmx(vcpu);
670 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200671 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672
Eddie Donga3d7f852007-09-03 16:15:12 +0300673 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000674 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300675 kvm_migrate_timers(vcpu);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800676 vpid_sync_vcpu_all(vmx);
Avi Kivity543e4242008-05-13 16:22:47 +0300677 local_irq_disable();
678 list_add(&vmx->local_vcpus_link,
679 &per_cpu(vcpus_on_cpu, cpu));
680 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300681 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400683 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684 u8 error;
685
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400686 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300687 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
689 : "cc");
690 if (error)
691 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400692 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800693 }
694
695 if (vcpu->cpu != cpu) {
696 struct descriptor_table dt;
697 unsigned long sysenter_esp;
698
699 vcpu->cpu = cpu;
700 /*
701 * Linux uses per-cpu TSS and GDT, so set these when switching
702 * processors.
703 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300704 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
705 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
707
708 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
709 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300710
711 /*
712 * Make sure the time stamp counter is monotonous.
713 */
714 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200715 if (tsc_this < vcpu->arch.host_tsc) {
716 delta = vcpu->arch.host_tsc - tsc_this;
717 new_offset = vmcs_read64(TSC_OFFSET) + delta;
718 vmcs_write64(TSC_OFFSET, new_offset);
719 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800720 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721}
722
723static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
724{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300725 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800726}
727
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300728static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
729{
730 if (vcpu->fpu_active)
731 return;
732 vcpu->fpu_active = 1;
Rusty Russell707d92f2007-07-17 23:19:08 +1000733 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800734 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92f2007-07-17 23:19:08 +1000735 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300736 update_exception_bitmap(vcpu);
737}
738
739static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
740{
741 if (!vcpu->fpu_active)
742 return;
743 vcpu->fpu_active = 0;
Rusty Russell707d92f2007-07-17 23:19:08 +1000744 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300745 update_exception_bitmap(vcpu);
746}
747
Avi Kivity6aa8b732006-12-10 02:21:36 -0800748static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
749{
750 return vmcs_readl(GUEST_RFLAGS);
751}
752
753static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
754{
Nitin A Kamble56b237e2009-06-04 15:04:08 -0700755 if (vcpu->arch.rmode.vm86_active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100756 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757 vmcs_writel(GUEST_RFLAGS, rflags);
758}
759
Glauber Costa2809f5d2009-05-12 16:21:05 -0400760static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
761{
762 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
763 int ret = 0;
764
765 if (interruptibility & GUEST_INTR_STATE_STI)
766 ret |= X86_SHADOW_INT_STI;
767 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
768 ret |= X86_SHADOW_INT_MOV_SS;
769
770 return ret & mask;
771}
772
773static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
774{
775 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
776 u32 interruptibility = interruptibility_old;
777
778 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
779
780 if (mask & X86_SHADOW_INT_MOV_SS)
781 interruptibility |= GUEST_INTR_STATE_MOV_SS;
782 if (mask & X86_SHADOW_INT_STI)
783 interruptibility |= GUEST_INTR_STATE_STI;
784
785 if ((interruptibility != interruptibility_old))
786 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
787}
788
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
790{
791 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300793 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300795 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796
Glauber Costa2809f5d2009-05-12 16:21:05 -0400797 /* skipping an emulated instruction also counts */
798 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799}
800
Avi Kivity298101d2007-11-25 13:41:11 +0200801static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
802 bool has_error_code, u32 error_code)
803{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200804 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100805 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200806
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100807 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200808 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100809 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
810 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200811
Nitin A Kamble56b237e2009-06-04 15:04:08 -0700812 if (vcpu->arch.rmode.vm86_active) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200813 vmx->rmode.irq.pending = true;
814 vmx->rmode.irq.vector = nr;
815 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +0300816 if (kvm_exception_is_soft(nr))
817 vmx->rmode.irq.rip +=
818 vmx->vcpu.arch.event_exit_inst_len;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100819 intr_info |= INTR_TYPE_SOFT_INTR;
820 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200821 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
822 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
823 return;
824 }
825
Gleb Natapov66fd3f72009-05-11 13:35:50 +0300826 if (kvm_exception_is_soft(nr)) {
827 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
828 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100829 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
830 } else
831 intr_info |= INTR_TYPE_HARD_EXCEPTION;
832
833 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200834}
835
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836/*
Eddie Donga75beee2007-05-17 18:55:15 +0300837 * Swap MSR entry in host/guest MSR entry array.
838 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200839#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000840static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300841{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400842 struct kvm_msr_entry tmp;
843
844 tmp = vmx->guest_msrs[to];
845 vmx->guest_msrs[to] = vmx->guest_msrs[from];
846 vmx->guest_msrs[from] = tmp;
847 tmp = vmx->host_msrs[to];
848 vmx->host_msrs[to] = vmx->host_msrs[from];
849 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300850}
Gabriel C54e11fa2007-08-01 16:23:10 +0200851#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300852
853/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300854 * Set up the vmcs to automatically save and restore system
855 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
856 * mode, as fiddling with msrs is very expensive.
857 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000858static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300859{
Eddie Dong2cc51562007-05-21 07:28:09 +0300860 int save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +0200861 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +0300862
Avi Kivity33f9c502008-02-27 16:06:57 +0200863 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300864 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300865#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000866 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300867 int index;
868
Rusty Russell8b9cf982007-07-30 16:31:43 +1000869 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300870 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000871 move_msr_up(vmx, index, save_nmsrs++);
872 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300873 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000874 move_msr_up(vmx, index, save_nmsrs++);
875 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300876 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000877 move_msr_up(vmx, index, save_nmsrs++);
878 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300879 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000880 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300881 /*
882 * MSR_K6_STAR is only needed on long mode guests, and only
883 * if efer.sce is enabled.
884 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000885 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800886 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000887 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300888 }
Eddie Donga75beee2007-05-17 18:55:15 +0300889#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400890 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300891
Eddie Donga75beee2007-05-17 18:55:15 +0300892#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400893 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000894 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300895#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000896 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivity58972972009-02-24 22:26:47 +0200897
898 if (cpu_has_vmx_msr_bitmap()) {
899 if (is_long_mode(&vmx->vcpu))
900 msr_bitmap = vmx_msr_bitmap_longmode;
901 else
902 msr_bitmap = vmx_msr_bitmap_legacy;
903
904 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
905 }
Avi Kivitye38aea32007-04-19 13:22:48 +0300906}
907
908/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909 * reads and returns guest's timestamp counter "register"
910 * guest_tsc = host_tsc + tsc_offset -- 21.3
911 */
912static u64 guest_read_tsc(void)
913{
914 u64 host_tsc, tsc_offset;
915
916 rdtscll(host_tsc);
917 tsc_offset = vmcs_read64(TSC_OFFSET);
918 return host_tsc + tsc_offset;
919}
920
921/*
922 * writes 'guest_tsc' into guest's timestamp counter "register"
923 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
924 */
Marcelo Tosatti53f658b2008-12-11 20:45:05 +0100925static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
928}
929
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930/*
931 * Reads an msr value (of 'msr_index') into 'pdata'.
932 * Returns 0 on success, non-0 otherwise.
933 * Assumes vcpu_load() was already called.
934 */
935static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
936{
937 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400938 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939
940 if (!pdata) {
941 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
942 return -EINVAL;
943 }
944
945 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800946#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947 case MSR_FS_BASE:
948 data = vmcs_readl(GUEST_FS_BASE);
949 break;
950 case MSR_GS_BASE:
951 data = vmcs_readl(GUEST_GS_BASE);
952 break;
953 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800954 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955#endif
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530956 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957 data = guest_read_tsc();
958 break;
959 case MSR_IA32_SYSENTER_CS:
960 data = vmcs_read32(GUEST_SYSENTER_CS);
961 break;
962 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200963 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964 break;
965 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200966 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 default:
Avi Kivity516a1a72009-02-15 02:32:07 +0200969 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +1000970 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800971 if (msr) {
972 data = msr->data;
973 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800975 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 }
977
978 *pdata = data;
979 return 0;
980}
981
982/*
983 * Writes msr value into into the appropriate "register".
984 * Returns 0 on success, non-0 otherwise.
985 * Assumes vcpu_load() was already called.
986 */
987static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
988{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400989 struct vcpu_vmx *vmx = to_vmx(vcpu);
990 struct kvm_msr_entry *msr;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +0100991 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +0300992 int ret = 0;
993
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -0800995 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300996 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +0300997 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +0300998 break;
Avi Kivity16175a72009-03-23 22:13:44 +0200999#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000 case MSR_FS_BASE:
1001 vmcs_writel(GUEST_FS_BASE, data);
1002 break;
1003 case MSR_GS_BASE:
1004 vmcs_writel(GUEST_GS_BASE, data);
1005 break;
1006#endif
1007 case MSR_IA32_SYSENTER_CS:
1008 vmcs_write32(GUEST_SYSENTER_CS, data);
1009 break;
1010 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001011 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012 break;
1013 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001014 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301016 case MSR_IA32_TSC:
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01001017 rdtscll(host_tsc);
1018 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019 break;
Chris Lalancetteefa67e02008-06-20 09:51:30 +02001020 case MSR_P6_PERFCTR0:
1021 case MSR_P6_PERFCTR1:
1022 case MSR_P6_EVNTSEL0:
1023 case MSR_P6_EVNTSEL1:
1024 /*
1025 * Just discard all writes to the performance counters; this
1026 * should keep both older linux and windows 64-bit guests
1027 * happy
1028 */
1029 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", msr_index, data);
1030
1031 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001032 case MSR_IA32_CR_PAT:
1033 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1034 vmcs_write64(GUEST_IA32_PAT, data);
1035 vcpu->arch.pat = data;
1036 break;
1037 }
1038 /* Otherwise falls through to kvm_set_msr_common */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039 default:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001040 vmx_load_host_state(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +10001041 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001042 if (msr) {
1043 msr->data = data;
1044 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001045 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001046 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047 }
1048
Eddie Dong2cc51562007-05-21 07:28:09 +03001049 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050}
1051
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001052static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001054 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1055 switch (reg) {
1056 case VCPU_REGS_RSP:
1057 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1058 break;
1059 case VCPU_REGS_RIP:
1060 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1061 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001062 case VCPU_EXREG_PDPTR:
1063 if (enable_ept)
1064 ept_save_pdptrs(vcpu);
1065 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001066 default:
1067 break;
1068 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069}
1070
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001071static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001073 int old_debug = vcpu->guest_debug;
1074 unsigned long flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001076 vcpu->guest_debug = dbg->control;
1077 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
1078 vcpu->guest_debug = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001080 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1081 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1082 else
1083 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1084
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001085 flags = vmcs_readl(GUEST_RFLAGS);
1086 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
1087 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1088 else if (old_debug & KVM_GUESTDBG_SINGLESTEP)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001090 vmcs_writel(GUEST_RFLAGS, flags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001092 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093
1094 return 0;
1095}
1096
1097static __init int cpu_has_kvm_support(void)
1098{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001099 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100}
1101
1102static __init int vmx_disabled_by_bios(void)
1103{
1104 u64 msr;
1105
1106 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001107 return (msr & (FEATURE_CONTROL_LOCKED |
1108 FEATURE_CONTROL_VMXON_ENABLED))
1109 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001110 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111}
1112
Avi Kivity774c47f2007-02-12 00:54:47 -08001113static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114{
1115 int cpu = raw_smp_processor_id();
1116 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1117 u64 old;
1118
Avi Kivity543e4242008-05-13 16:22:47 +03001119 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001121 if ((old & (FEATURE_CONTROL_LOCKED |
1122 FEATURE_CONTROL_VMXON_ENABLED))
1123 != (FEATURE_CONTROL_LOCKED |
1124 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001126 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001127 FEATURE_CONTROL_LOCKED |
1128 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001129 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001130 asm volatile (ASM_VMX_VMXON_RAX
1131 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001132 : "memory", "cc");
1133}
1134
Avi Kivity543e4242008-05-13 16:22:47 +03001135static void vmclear_local_vcpus(void)
1136{
1137 int cpu = raw_smp_processor_id();
1138 struct vcpu_vmx *vmx, *n;
1139
1140 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1141 local_vcpus_link)
1142 __vcpu_clear(vmx);
1143}
1144
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001145
1146/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1147 * tricks.
1148 */
1149static void kvm_cpu_vmxoff(void)
1150{
1151 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1152 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1153}
1154
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155static void hardware_disable(void *garbage)
1156{
Avi Kivity543e4242008-05-13 16:22:47 +03001157 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001158 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159}
1160
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001161static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001162 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163{
1164 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001165 u32 ctl = ctl_min | ctl_opt;
1166
1167 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1168
1169 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1170 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1171
1172 /* Ensure minimum (required) set of control bits are supported. */
1173 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001174 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001175
1176 *result = ctl;
1177 return 0;
1178}
1179
Yang, Sheng002c7f72007-07-31 14:23:01 +03001180static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001181{
1182 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001183 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001184 u32 _pin_based_exec_control = 0;
1185 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001186 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001187 u32 _vmexit_control = 0;
1188 u32 _vmentry_control = 0;
1189
1190 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001191 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001192 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1193 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001194 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001195
1196 min = CPU_BASED_HLT_EXITING |
1197#ifdef CONFIG_X86_64
1198 CPU_BASED_CR8_LOAD_EXITING |
1199 CPU_BASED_CR8_STORE_EXITING |
1200#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001201 CPU_BASED_CR3_LOAD_EXITING |
1202 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001203 CPU_BASED_USE_IO_BITMAPS |
1204 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001205 CPU_BASED_USE_TSC_OFFSETING |
1206 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001207 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001208 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001209 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001210 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1211 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001212 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001213#ifdef CONFIG_X86_64
1214 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1215 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1216 ~CPU_BASED_CR8_STORE_EXITING;
1217#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001218 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001219 min2 = 0;
1220 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001221 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001222 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001223 SECONDARY_EXEC_ENABLE_EPT |
1224 SECONDARY_EXEC_UNRESTRICTED_GUEST;
Sheng Yangd56f5462008-04-25 10:13:16 +08001225 if (adjust_vmx_controls(min2, opt2,
1226 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001227 &_cpu_based_2nd_exec_control) < 0)
1228 return -EIO;
1229 }
1230#ifndef CONFIG_X86_64
1231 if (!(_cpu_based_2nd_exec_control &
1232 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1233 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1234#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001235 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001236 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1237 enabled */
Sheng Yangd56f5462008-04-25 10:13:16 +08001238 min &= ~(CPU_BASED_CR3_LOAD_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001239 CPU_BASED_CR3_STORE_EXITING |
1240 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001241 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1242 &_cpu_based_exec_control) < 0)
1243 return -EIO;
1244 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1245 vmx_capability.ept, vmx_capability.vpid);
1246 }
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001247
1248 min = 0;
1249#ifdef CONFIG_X86_64
1250 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1251#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001252 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001253 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1254 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001255 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001256
Sheng Yang468d4722008-10-09 16:01:55 +08001257 min = 0;
1258 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001259 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1260 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001261 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001263 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001264
1265 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1266 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001267 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001268
1269#ifdef CONFIG_X86_64
1270 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1271 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001272 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001273#endif
1274
1275 /* Require Write-Back (WB) memory type for VMCS accesses. */
1276 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001277 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001278
Yang, Sheng002c7f72007-07-31 14:23:01 +03001279 vmcs_conf->size = vmx_msr_high & 0x1fff;
1280 vmcs_conf->order = get_order(vmcs_config.size);
1281 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001282
Yang, Sheng002c7f72007-07-31 14:23:01 +03001283 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1284 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001285 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001286 vmcs_conf->vmexit_ctrl = _vmexit_control;
1287 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001288
1289 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001290}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291
1292static struct vmcs *alloc_vmcs_cpu(int cpu)
1293{
1294 int node = cpu_to_node(cpu);
1295 struct page *pages;
1296 struct vmcs *vmcs;
1297
Mel Gorman6484eb32009-06-16 15:31:54 -07001298 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299 if (!pages)
1300 return NULL;
1301 vmcs = page_address(pages);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001302 memset(vmcs, 0, vmcs_config.size);
1303 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001304 return vmcs;
1305}
1306
1307static struct vmcs *alloc_vmcs(void)
1308{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001309 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310}
1311
1312static void free_vmcs(struct vmcs *vmcs)
1313{
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001314 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001315}
1316
Sam Ravnborg39959582007-06-01 00:47:13 -07001317static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318{
1319 int cpu;
1320
1321 for_each_online_cpu(cpu)
1322 free_vmcs(per_cpu(vmxarea, cpu));
1323}
1324
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325static __init int alloc_kvm_area(void)
1326{
1327 int cpu;
1328
1329 for_each_online_cpu(cpu) {
1330 struct vmcs *vmcs;
1331
1332 vmcs = alloc_vmcs_cpu(cpu);
1333 if (!vmcs) {
1334 free_kvm_area();
1335 return -ENOMEM;
1336 }
1337
1338 per_cpu(vmxarea, cpu) = vmcs;
1339 }
1340 return 0;
1341}
1342
1343static __init int hardware_setup(void)
1344{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001345 if (setup_vmcs_config(&vmcs_config) < 0)
1346 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001347
1348 if (boot_cpu_has(X86_FEATURE_NX))
1349 kvm_enable_efer_bits(EFER_NX);
1350
Sheng Yang93ba03c2009-04-01 15:52:32 +08001351 if (!cpu_has_vmx_vpid())
1352 enable_vpid = 0;
1353
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001354 if (!cpu_has_vmx_ept()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001355 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001356 enable_unrestricted_guest = 0;
1357 }
1358
1359 if (!cpu_has_vmx_unrestricted_guest())
1360 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001361
1362 if (!cpu_has_vmx_flexpriority())
1363 flexpriority_enabled = 0;
1364
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001365 if (!cpu_has_vmx_tpr_shadow())
1366 kvm_x86_ops->update_cr8_intercept = NULL;
1367
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368 return alloc_kvm_area();
1369}
1370
1371static __exit void hardware_unsetup(void)
1372{
1373 free_kvm_area();
1374}
1375
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1377{
1378 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1379
Avi Kivity6af11b92007-03-19 13:18:10 +02001380 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381 vmcs_write16(sf->selector, save->selector);
1382 vmcs_writel(sf->base, save->base);
1383 vmcs_write32(sf->limit, save->limit);
1384 vmcs_write32(sf->ar_bytes, save->ar);
1385 } else {
1386 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1387 << AR_DPL_SHIFT;
1388 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1389 }
1390}
1391
1392static void enter_pmode(struct kvm_vcpu *vcpu)
1393{
1394 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001395 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001397 vmx->emulation_required = 1;
Nitin A Kamble56b237e2009-06-04 15:04:08 -07001398 vcpu->arch.rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001400 vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base);
1401 vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit);
1402 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001403
1404 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001405 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001406 flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407 vmcs_writel(GUEST_RFLAGS, flags);
1408
Rusty Russell66aee912007-07-17 23:34:16 +10001409 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1410 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411
1412 update_exception_bitmap(vcpu);
1413
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001414 if (emulate_invalid_guest_state)
1415 return;
1416
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001417 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1418 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1419 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1420 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421
1422 vmcs_write16(GUEST_SS_SELECTOR, 0);
1423 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1424
1425 vmcs_write16(GUEST_CS_SELECTOR,
1426 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1427 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1428}
1429
Mike Dayd77c26f2007-10-08 09:02:08 -04001430static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001432 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001433 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1434 kvm->memslots[0].npages - 3;
1435 return base_gfn << PAGE_SHIFT;
1436 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001437 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001438}
1439
1440static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1441{
1442 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1443
1444 save->selector = vmcs_read16(sf->selector);
1445 save->base = vmcs_readl(sf->base);
1446 save->limit = vmcs_read32(sf->limit);
1447 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001448 vmcs_write16(sf->selector, save->base >> 4);
1449 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450 vmcs_write32(sf->limit, 0xffff);
1451 vmcs_write32(sf->ar_bytes, 0xf3);
1452}
1453
1454static void enter_rmode(struct kvm_vcpu *vcpu)
1455{
1456 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001457 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001459 if (enable_unrestricted_guest)
1460 return;
1461
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001462 vmx->emulation_required = 1;
Nitin A Kamble56b237e2009-06-04 15:04:08 -07001463 vcpu->arch.rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001464
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001465 vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001466 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1467
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001468 vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001469 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1470
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001471 vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1473
1474 flags = vmcs_readl(GUEST_RFLAGS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001475 vcpu->arch.rmode.save_iopl
1476 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001477
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001478 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479
1480 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001481 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001482 update_exception_bitmap(vcpu);
1483
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001484 if (emulate_invalid_guest_state)
1485 goto continue_rmode;
1486
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1488 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1489 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1490
1491 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001492 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001493 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1494 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1496
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001497 fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1498 fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1499 fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1500 fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001501
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001502continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001503 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001504 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505}
1506
Amit Shah401d10d2009-02-20 22:53:37 +05301507static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1508{
1509 struct vcpu_vmx *vmx = to_vmx(vcpu);
1510 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1511
1512 vcpu->arch.shadow_efer = efer;
1513 if (!msr)
1514 return;
1515 if (efer & EFER_LMA) {
1516 vmcs_write32(VM_ENTRY_CONTROLS,
1517 vmcs_read32(VM_ENTRY_CONTROLS) |
1518 VM_ENTRY_IA32E_MODE);
1519 msr->data = efer;
1520 } else {
1521 vmcs_write32(VM_ENTRY_CONTROLS,
1522 vmcs_read32(VM_ENTRY_CONTROLS) &
1523 ~VM_ENTRY_IA32E_MODE);
1524
1525 msr->data = efer & ~EFER_LME;
1526 }
1527 setup_msrs(vmx);
1528}
1529
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001530#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001531
1532static void enter_lmode(struct kvm_vcpu *vcpu)
1533{
1534 u32 guest_tr_ar;
1535
1536 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1537 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1538 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001539 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001540 vmcs_write32(GUEST_TR_AR_BYTES,
1541 (guest_tr_ar & ~AR_TYPE_MASK)
1542 | AR_TYPE_BUSY_64_TSS);
1543 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001544 vcpu->arch.shadow_efer |= EFER_LMA;
Amit Shah401d10d2009-02-20 22:53:37 +05301545 vmx_set_efer(vcpu, vcpu->arch.shadow_efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001546}
1547
1548static void exit_lmode(struct kvm_vcpu *vcpu)
1549{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001550 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001551
1552 vmcs_write32(VM_ENTRY_CONTROLS,
1553 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001554 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001555}
1556
1557#endif
1558
Sheng Yang2384d2b2008-01-17 15:14:33 +08001559static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1560{
1561 vpid_sync_vcpu_all(to_vmx(vcpu));
Avi Kivity089d0342009-03-23 18:26:32 +02001562 if (enable_ept)
Sheng Yang4e1096d2008-07-06 19:16:51 +08001563 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001564}
1565
Anthony Liguori25c4c272007-04-27 09:29:21 +03001566static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001567{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001568 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1569 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001570}
1571
Sheng Yang14394422008-04-28 12:24:45 +08001572static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1573{
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001574 if (!test_bit(VCPU_EXREG_PDPTR,
1575 (unsigned long *)&vcpu->arch.regs_dirty))
1576 return;
1577
Sheng Yang14394422008-04-28 12:24:45 +08001578 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Sheng Yang14394422008-04-28 12:24:45 +08001579 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1580 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1581 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1582 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1583 }
1584}
1585
Avi Kivity8f5d5492009-05-31 18:41:29 +03001586static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
1587{
1588 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1589 vcpu->arch.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
1590 vcpu->arch.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
1591 vcpu->arch.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
1592 vcpu->arch.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
1593 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001594
1595 __set_bit(VCPU_EXREG_PDPTR,
1596 (unsigned long *)&vcpu->arch.regs_avail);
1597 __set_bit(VCPU_EXREG_PDPTR,
1598 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03001599}
1600
Sheng Yang14394422008-04-28 12:24:45 +08001601static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1602
1603static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1604 unsigned long cr0,
1605 struct kvm_vcpu *vcpu)
1606{
1607 if (!(cr0 & X86_CR0_PG)) {
1608 /* From paging/starting to nonpaging */
1609 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001610 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001611 (CPU_BASED_CR3_LOAD_EXITING |
1612 CPU_BASED_CR3_STORE_EXITING));
1613 vcpu->arch.cr0 = cr0;
1614 vmx_set_cr4(vcpu, vcpu->arch.cr4);
Sheng Yang14394422008-04-28 12:24:45 +08001615 *hw_cr0 &= ~X86_CR0_WP;
1616 } else if (!is_paging(vcpu)) {
1617 /* From nonpaging to paging */
1618 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001619 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001620 ~(CPU_BASED_CR3_LOAD_EXITING |
1621 CPU_BASED_CR3_STORE_EXITING));
1622 vcpu->arch.cr0 = cr0;
1623 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1624 if (!(vcpu->arch.cr0 & X86_CR0_WP))
1625 *hw_cr0 &= ~X86_CR0_WP;
1626 }
1627}
1628
1629static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1630 struct kvm_vcpu *vcpu)
1631{
1632 if (!is_paging(vcpu)) {
1633 *hw_cr4 &= ~X86_CR4_PAE;
1634 *hw_cr4 |= X86_CR4_PSE;
1635 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1636 *hw_cr4 &= ~X86_CR4_PAE;
1637}
1638
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1640{
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001641 unsigned long hw_cr0;
1642
1643 if (enable_unrestricted_guest)
1644 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
1645 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
1646 else
1647 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08001648
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001649 vmx_fpu_deactivate(vcpu);
1650
Nitin A Kamble56b237e2009-06-04 15:04:08 -07001651 if (vcpu->arch.rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652 enter_pmode(vcpu);
1653
Nitin A Kamble56b237e2009-06-04 15:04:08 -07001654 if (!vcpu->arch.rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655 enter_rmode(vcpu);
1656
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001657#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001658 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001659 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10001661 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662 exit_lmode(vcpu);
1663 }
1664#endif
1665
Avi Kivity089d0342009-03-23 18:26:32 +02001666 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001667 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1668
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001670 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001671 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001672
Rusty Russell707d92f2007-07-17 23:19:08 +10001673 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001674 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001675}
1676
Sheng Yang14394422008-04-28 12:24:45 +08001677static u64 construct_eptp(unsigned long root_hpa)
1678{
1679 u64 eptp;
1680
1681 /* TODO write the value reading from MSR */
1682 eptp = VMX_EPT_DEFAULT_MT |
1683 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1684 eptp |= (root_hpa & PAGE_MASK);
1685
1686 return eptp;
1687}
1688
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1690{
Sheng Yang14394422008-04-28 12:24:45 +08001691 unsigned long guest_cr3;
1692 u64 eptp;
1693
1694 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02001695 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001696 eptp = construct_eptp(cr3);
1697 vmcs_write64(EPT_POINTER, eptp);
Sheng Yang14394422008-04-28 12:24:45 +08001698 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
1699 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1700 }
1701
Sheng Yang2384d2b2008-01-17 15:14:33 +08001702 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001703 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001704 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001705 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706}
1707
1708static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1709{
Nitin A Kamble56b237e2009-06-04 15:04:08 -07001710 unsigned long hw_cr4 = cr4 | (vcpu->arch.rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08001711 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1712
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001713 vcpu->arch.cr4 = cr4;
Avi Kivity089d0342009-03-23 18:26:32 +02001714 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001715 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1716
1717 vmcs_writel(CR4_READ_SHADOW, cr4);
1718 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001719}
1720
Avi Kivity6aa8b732006-12-10 02:21:36 -08001721static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1722{
1723 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1724
1725 return vmcs_readl(sf->base);
1726}
1727
1728static void vmx_get_segment(struct kvm_vcpu *vcpu,
1729 struct kvm_segment *var, int seg)
1730{
1731 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1732 u32 ar;
1733
1734 var->base = vmcs_readl(sf->base);
1735 var->limit = vmcs_read32(sf->limit);
1736 var->selector = vmcs_read16(sf->selector);
1737 ar = vmcs_read32(sf->ar_bytes);
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02001738 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001739 ar = 0;
1740 var->type = ar & 15;
1741 var->s = (ar >> 4) & 1;
1742 var->dpl = (ar >> 5) & 3;
1743 var->present = (ar >> 7) & 1;
1744 var->avl = (ar >> 12) & 1;
1745 var->l = (ar >> 13) & 1;
1746 var->db = (ar >> 14) & 1;
1747 var->g = (ar >> 15) & 1;
1748 var->unusable = (ar >> 16) & 1;
1749}
1750
Izik Eidus2e4d2652008-03-24 19:38:34 +02001751static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1752{
1753 struct kvm_segment kvm_seg;
1754
1755 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1756 return 0;
1757
1758 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1759 return 3;
1760
1761 vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
1762 return kvm_seg.selector & 3;
1763}
1764
Avi Kivity653e3102007-05-07 10:55:37 +03001765static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001766{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001767 u32 ar;
1768
Avi Kivity653e3102007-05-07 10:55:37 +03001769 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001770 ar = 1 << 16;
1771 else {
1772 ar = var->type & 15;
1773 ar |= (var->s & 1) << 4;
1774 ar |= (var->dpl & 3) << 5;
1775 ar |= (var->present & 1) << 7;
1776 ar |= (var->avl & 1) << 12;
1777 ar |= (var->l & 1) << 13;
1778 ar |= (var->db & 1) << 14;
1779 ar |= (var->g & 1) << 15;
1780 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001781 if (ar == 0) /* a 0 value means unusable */
1782 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001783
1784 return ar;
1785}
1786
1787static void vmx_set_segment(struct kvm_vcpu *vcpu,
1788 struct kvm_segment *var, int seg)
1789{
1790 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1791 u32 ar;
1792
Nitin A Kamble56b237e2009-06-04 15:04:08 -07001793 if (vcpu->arch.rmode.vm86_active && seg == VCPU_SREG_TR) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001794 vcpu->arch.rmode.tr.selector = var->selector;
1795 vcpu->arch.rmode.tr.base = var->base;
1796 vcpu->arch.rmode.tr.limit = var->limit;
1797 vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001798 return;
1799 }
1800 vmcs_writel(sf->base, var->base);
1801 vmcs_write32(sf->limit, var->limit);
1802 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble56b237e2009-06-04 15:04:08 -07001803 if (vcpu->arch.rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001804 /*
1805 * Hack real-mode segments into vm86 compatibility.
1806 */
1807 if (var->base == 0xffff0000 && var->selector == 0xf000)
1808 vmcs_writel(sf->base, 0xf0000);
1809 ar = 0xf3;
1810 } else
1811 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001812
1813 /*
1814 * Fix the "Accessed" bit in AR field of segment registers for older
1815 * qemu binaries.
1816 * IA32 arch specifies that at the time of processor reset the
1817 * "Accessed" bit in the AR field of segment registers is 1. And qemu
1818 * is setting it to 0 in the usedland code. This causes invalid guest
1819 * state vmexit when "unrestricted guest" mode is turned on.
1820 * Fix for this setup issue in cpu_reset is being pushed in the qemu
1821 * tree. Newer qemu binaries with that qemu fix would not need this
1822 * kvm hack.
1823 */
1824 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
1825 ar |= 0x1; /* Accessed */
1826
Avi Kivity6aa8b732006-12-10 02:21:36 -08001827 vmcs_write32(sf->ar_bytes, ar);
1828}
1829
Avi Kivity6aa8b732006-12-10 02:21:36 -08001830static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1831{
1832 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1833
1834 *db = (ar >> 14) & 1;
1835 *l = (ar >> 13) & 1;
1836}
1837
1838static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1839{
1840 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1841 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1842}
1843
1844static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1845{
1846 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1847 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1848}
1849
1850static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1851{
1852 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1853 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1854}
1855
1856static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1857{
1858 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1859 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1860}
1861
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001862static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1863{
1864 struct kvm_segment var;
1865 u32 ar;
1866
1867 vmx_get_segment(vcpu, &var, seg);
1868 ar = vmx_segment_access_rights(&var);
1869
1870 if (var.base != (var.selector << 4))
1871 return false;
1872 if (var.limit != 0xffff)
1873 return false;
1874 if (ar != 0xf3)
1875 return false;
1876
1877 return true;
1878}
1879
1880static bool code_segment_valid(struct kvm_vcpu *vcpu)
1881{
1882 struct kvm_segment cs;
1883 unsigned int cs_rpl;
1884
1885 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1886 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1887
Avi Kivity1872a3f2009-01-04 23:26:52 +02001888 if (cs.unusable)
1889 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001890 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1891 return false;
1892 if (!cs.s)
1893 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001894 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001895 if (cs.dpl > cs_rpl)
1896 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001897 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001898 if (cs.dpl != cs_rpl)
1899 return false;
1900 }
1901 if (!cs.present)
1902 return false;
1903
1904 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1905 return true;
1906}
1907
1908static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1909{
1910 struct kvm_segment ss;
1911 unsigned int ss_rpl;
1912
1913 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1914 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1915
Avi Kivity1872a3f2009-01-04 23:26:52 +02001916 if (ss.unusable)
1917 return true;
1918 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001919 return false;
1920 if (!ss.s)
1921 return false;
1922 if (ss.dpl != ss_rpl) /* DPL != RPL */
1923 return false;
1924 if (!ss.present)
1925 return false;
1926
1927 return true;
1928}
1929
1930static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1931{
1932 struct kvm_segment var;
1933 unsigned int rpl;
1934
1935 vmx_get_segment(vcpu, &var, seg);
1936 rpl = var.selector & SELECTOR_RPL_MASK;
1937
Avi Kivity1872a3f2009-01-04 23:26:52 +02001938 if (var.unusable)
1939 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001940 if (!var.s)
1941 return false;
1942 if (!var.present)
1943 return false;
1944 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
1945 if (var.dpl < rpl) /* DPL < RPL */
1946 return false;
1947 }
1948
1949 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1950 * rights flags
1951 */
1952 return true;
1953}
1954
1955static bool tr_valid(struct kvm_vcpu *vcpu)
1956{
1957 struct kvm_segment tr;
1958
1959 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
1960
Avi Kivity1872a3f2009-01-04 23:26:52 +02001961 if (tr.unusable)
1962 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001963 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1964 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001965 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001966 return false;
1967 if (!tr.present)
1968 return false;
1969
1970 return true;
1971}
1972
1973static bool ldtr_valid(struct kvm_vcpu *vcpu)
1974{
1975 struct kvm_segment ldtr;
1976
1977 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
1978
Avi Kivity1872a3f2009-01-04 23:26:52 +02001979 if (ldtr.unusable)
1980 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001981 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1982 return false;
1983 if (ldtr.type != 2)
1984 return false;
1985 if (!ldtr.present)
1986 return false;
1987
1988 return true;
1989}
1990
1991static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
1992{
1993 struct kvm_segment cs, ss;
1994
1995 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1996 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1997
1998 return ((cs.selector & SELECTOR_RPL_MASK) ==
1999 (ss.selector & SELECTOR_RPL_MASK));
2000}
2001
2002/*
2003 * Check if guest state is valid. Returns true if valid, false if
2004 * not.
2005 * We assume that registers are always usable
2006 */
2007static bool guest_state_valid(struct kvm_vcpu *vcpu)
2008{
2009 /* real mode guest state checks */
2010 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
2011 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2012 return false;
2013 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2014 return false;
2015 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2016 return false;
2017 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2018 return false;
2019 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2020 return false;
2021 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2022 return false;
2023 } else {
2024 /* protected mode guest state checks */
2025 if (!cs_ss_rpl_check(vcpu))
2026 return false;
2027 if (!code_segment_valid(vcpu))
2028 return false;
2029 if (!stack_segment_valid(vcpu))
2030 return false;
2031 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2032 return false;
2033 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2034 return false;
2035 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2036 return false;
2037 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2038 return false;
2039 if (!tr_valid(vcpu))
2040 return false;
2041 if (!ldtr_valid(vcpu))
2042 return false;
2043 }
2044 /* TODO:
2045 * - Add checks on RIP
2046 * - Add checks on RFLAGS
2047 */
2048
2049 return true;
2050}
2051
Mike Dayd77c26f2007-10-08 09:02:08 -04002052static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002053{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002055 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002056 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02002057 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002058
Izik Eidus195aefd2007-10-01 22:14:18 +02002059 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2060 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002061 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002062 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002063 r = kvm_write_guest_page(kvm, fn++, &data,
2064 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002065 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002066 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002067 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2068 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002069 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002070 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2071 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002072 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002073 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002074 r = kvm_write_guest_page(kvm, fn, &data,
2075 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2076 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002077 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002078 goto out;
2079
2080 ret = 1;
2081out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002082 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002083}
2084
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002085static int init_rmode_identity_map(struct kvm *kvm)
2086{
2087 int i, r, ret;
2088 pfn_t identity_map_pfn;
2089 u32 tmp;
2090
Avi Kivity089d0342009-03-23 18:26:32 +02002091 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002092 return 1;
2093 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2094 printk(KERN_ERR "EPT: identity-mapping pagetable "
2095 "haven't been allocated!\n");
2096 return 0;
2097 }
2098 if (likely(kvm->arch.ept_identity_pagetable_done))
2099 return 1;
2100 ret = 0;
2101 identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT;
2102 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2103 if (r < 0)
2104 goto out;
2105 /* Set up identity-mapping pagetable for EPT in real mode */
2106 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2107 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2108 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2109 r = kvm_write_guest_page(kvm, identity_map_pfn,
2110 &tmp, i * sizeof(tmp), sizeof(tmp));
2111 if (r < 0)
2112 goto out;
2113 }
2114 kvm->arch.ept_identity_pagetable_done = true;
2115 ret = 1;
2116out:
2117 return ret;
2118}
2119
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120static void seg_setup(int seg)
2121{
2122 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002123 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002124
2125 vmcs_write16(sf->selector, 0);
2126 vmcs_writel(sf->base, 0);
2127 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002128 if (enable_unrestricted_guest) {
2129 ar = 0x93;
2130 if (seg == VCPU_SREG_CS)
2131 ar |= 0x08; /* code segment */
2132 } else
2133 ar = 0xf3;
2134
2135 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002136}
2137
Sheng Yangf78e0e22007-10-29 09:40:42 +08002138static int alloc_apic_access_page(struct kvm *kvm)
2139{
2140 struct kvm_userspace_memory_region kvm_userspace_mem;
2141 int r = 0;
2142
Izik Eidus72dc67a2008-02-10 18:04:15 +02002143 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002144 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002145 goto out;
2146 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2147 kvm_userspace_mem.flags = 0;
2148 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2149 kvm_userspace_mem.memory_size = PAGE_SIZE;
2150 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2151 if (r)
2152 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002153
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002154 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002155out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002156 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002157 return r;
2158}
2159
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002160static int alloc_identity_pagetable(struct kvm *kvm)
2161{
2162 struct kvm_userspace_memory_region kvm_userspace_mem;
2163 int r = 0;
2164
2165 down_write(&kvm->slots_lock);
2166 if (kvm->arch.ept_identity_pagetable)
2167 goto out;
2168 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2169 kvm_userspace_mem.flags = 0;
2170 kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
2171 kvm_userspace_mem.memory_size = PAGE_SIZE;
2172 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2173 if (r)
2174 goto out;
2175
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002176 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
2177 VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002178out:
2179 up_write(&kvm->slots_lock);
2180 return r;
2181}
2182
Sheng Yang2384d2b2008-01-17 15:14:33 +08002183static void allocate_vpid(struct vcpu_vmx *vmx)
2184{
2185 int vpid;
2186
2187 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002188 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002189 return;
2190 spin_lock(&vmx_vpid_lock);
2191 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2192 if (vpid < VMX_NR_VPIDS) {
2193 vmx->vpid = vpid;
2194 __set_bit(vpid, vmx_vpid_bitmap);
2195 }
2196 spin_unlock(&vmx_vpid_lock);
2197}
2198
Avi Kivity58972972009-02-24 22:26:47 +02002199static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002200{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002201 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002202
2203 if (!cpu_has_vmx_msr_bitmap())
2204 return;
2205
2206 /*
2207 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2208 * have the write-low and read-high bitmap offsets the wrong way round.
2209 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2210 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002211 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002212 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2213 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002214 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2215 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002216 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2217 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002218 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002219}
2220
Avi Kivity58972972009-02-24 22:26:47 +02002221static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2222{
2223 if (!longmode_only)
2224 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2225 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2226}
2227
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228/*
2229 * Sets up the vmcs for emulated real mode.
2230 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002231static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232{
Sheng Yang468d4722008-10-09 16:01:55 +08002233 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002234 u32 junk;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002235 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002236 unsigned long a;
2237 struct descriptor_table dt;
2238 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002239 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002240 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241
Avi Kivity6aa8b732006-12-10 02:21:36 -08002242 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002243 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2244 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002245
Sheng Yang25c5f222008-03-28 13:18:56 +08002246 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002247 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002248
Avi Kivity6aa8b732006-12-10 02:21:36 -08002249 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2250
Avi Kivity6aa8b732006-12-10 02:21:36 -08002251 /* Control */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002252 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2253 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002254
2255 exec_control = vmcs_config.cpu_based_exec_ctrl;
2256 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2257 exec_control &= ~CPU_BASED_TPR_SHADOW;
2258#ifdef CONFIG_X86_64
2259 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2260 CPU_BASED_CR8_LOAD_EXITING;
2261#endif
2262 }
Avi Kivity089d0342009-03-23 18:26:32 +02002263 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002264 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002265 CPU_BASED_CR3_LOAD_EXITING |
2266 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002267 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268
Sheng Yang83ff3b92007-11-21 14:33:25 +08002269 if (cpu_has_secondary_exec_ctrls()) {
2270 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2271 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2272 exec_control &=
2273 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002274 if (vmx->vpid == 0)
2275 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Avi Kivity089d0342009-03-23 18:26:32 +02002276 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002277 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002278 if (!enable_unrestricted_guest)
2279 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002280 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2281 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002282
Avi Kivityc7addb92007-09-16 18:58:32 +02002283 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2284 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2286
2287 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2288 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2289 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2290
2291 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2292 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2293 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002294 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2295 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002296 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002297#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298 rdmsrl(MSR_FS_BASE, a);
2299 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2300 rdmsrl(MSR_GS_BASE, a);
2301 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2302#else
2303 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2304 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2305#endif
2306
2307 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2308
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002309 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2311
Mike Dayd77c26f2007-10-08 09:02:08 -04002312 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002313 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002314 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2315 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2316 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002317
2318 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2319 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2320 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2321 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2322 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2323 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2324
Sheng Yang468d4722008-10-09 16:01:55 +08002325 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2326 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2327 host_pat = msr_low | ((u64) msr_high << 32);
2328 vmcs_write64(HOST_IA32_PAT, host_pat);
2329 }
2330 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2331 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2332 host_pat = msr_low | ((u64) msr_high << 32);
2333 /* Write the default value follow host pat */
2334 vmcs_write64(GUEST_IA32_PAT, host_pat);
2335 /* Keep arch.pat sync with GUEST_IA32_PAT */
2336 vmx->vcpu.arch.pat = host_pat;
2337 }
2338
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339 for (i = 0; i < NR_VMX_MSR; ++i) {
2340 u32 index = vmx_msr_index[i];
2341 u32 data_low, data_high;
2342 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002343 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344
2345 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2346 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002347 if (wrmsr_safe(index, data_low, data_high) < 0)
2348 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002350 vmx->host_msrs[j].index = index;
2351 vmx->host_msrs[j].reserved = 0;
2352 vmx->host_msrs[j].data = data;
2353 vmx->guest_msrs[j] = vmx->host_msrs[j];
2354 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002356
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002357 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002358
2359 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002360 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2361
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002362 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2363 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2364
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002365 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2366 rdtscll(tsc_this);
2367 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2368 tsc_base = tsc_this;
2369
2370 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002371
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002372 return 0;
2373}
2374
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002375static int init_rmode(struct kvm *kvm)
2376{
2377 if (!init_rmode_tss(kvm))
2378 return 0;
2379 if (!init_rmode_identity_map(kvm))
2380 return 0;
2381 return 1;
2382}
2383
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002384static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2385{
2386 struct vcpu_vmx *vmx = to_vmx(vcpu);
2387 u64 msr;
2388 int ret;
2389
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002390 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002391 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002392 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002393 ret = -ENOMEM;
2394 goto out;
2395 }
2396
Nitin A Kamble56b237e2009-06-04 15:04:08 -07002397 vmx->vcpu.arch.rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002398
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002399 vmx->soft_vnmi_blocked = 0;
2400
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002401 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002402 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002403 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
2404 if (vmx->vcpu.vcpu_id == 0)
2405 msr |= MSR_IA32_APICBASE_BSP;
2406 kvm_set_apic_base(&vmx->vcpu, msr);
2407
2408 fx_init(&vmx->vcpu);
2409
Avi Kivity5706be02008-08-20 15:07:31 +03002410 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002411 /*
2412 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2413 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2414 */
2415 if (vmx->vcpu.vcpu_id == 0) {
2416 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2417 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2418 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002419 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2420 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002421 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002422
2423 seg_setup(VCPU_SREG_DS);
2424 seg_setup(VCPU_SREG_ES);
2425 seg_setup(VCPU_SREG_FS);
2426 seg_setup(VCPU_SREG_GS);
2427 seg_setup(VCPU_SREG_SS);
2428
2429 vmcs_write16(GUEST_TR_SELECTOR, 0);
2430 vmcs_writel(GUEST_TR_BASE, 0);
2431 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2432 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2433
2434 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2435 vmcs_writel(GUEST_LDTR_BASE, 0);
2436 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2437 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2438
2439 vmcs_write32(GUEST_SYSENTER_CS, 0);
2440 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2441 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2442
2443 vmcs_writel(GUEST_RFLAGS, 0x02);
2444 if (vmx->vcpu.vcpu_id == 0)
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002445 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002446 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002447 kvm_rip_write(vcpu, 0);
2448 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002449
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002450 vmcs_writel(GUEST_DR7, 0x400);
2451
2452 vmcs_writel(GUEST_GDTR_BASE, 0);
2453 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2454
2455 vmcs_writel(GUEST_IDTR_BASE, 0);
2456 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2457
2458 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2459 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2460 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2461
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002462 /* Special registers */
2463 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2464
2465 setup_msrs(vmx);
2466
Avi Kivity6aa8b732006-12-10 02:21:36 -08002467 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2468
Sheng Yangf78e0e22007-10-29 09:40:42 +08002469 if (cpu_has_vmx_tpr_shadow()) {
2470 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2471 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2472 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002473 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002474 vmcs_write32(TPR_THRESHOLD, 0);
2475 }
2476
2477 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2478 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002479 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002480
Sheng Yang2384d2b2008-01-17 15:14:33 +08002481 if (vmx->vpid != 0)
2482 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2483
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002484 vmx->vcpu.arch.cr0 = 0x60000010;
2485 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002486 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002487 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002488 vmx_fpu_activate(&vmx->vcpu);
2489 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002490
Sheng Yang2384d2b2008-01-17 15:14:33 +08002491 vpid_sync_vcpu_all(vmx);
2492
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002493 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002494
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002495 /* HACK: Don't enable emulation on guest boot/reset */
2496 vmx->emulation_required = 0;
2497
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002499 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500 return ret;
2501}
2502
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002503static void enable_irq_window(struct kvm_vcpu *vcpu)
2504{
2505 u32 cpu_based_vm_exec_control;
2506
2507 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2508 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2509 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2510}
2511
2512static void enable_nmi_window(struct kvm_vcpu *vcpu)
2513{
2514 u32 cpu_based_vm_exec_control;
2515
2516 if (!cpu_has_virtual_nmis()) {
2517 enable_irq_window(vcpu);
2518 return;
2519 }
2520
2521 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2522 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2523 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2524}
2525
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002526static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03002527{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002528 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002529 uint32_t intr;
2530 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02002531
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002532 KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler);
2533
Avi Kivityfa89a812008-09-01 15:57:51 +03002534 ++vcpu->stat.irq_injections;
Nitin A Kamble56b237e2009-06-04 15:04:08 -07002535 if (vcpu->arch.rmode.vm86_active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002536 vmx->rmode.irq.pending = true;
2537 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002538 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +03002539 if (vcpu->arch.interrupt.soft)
2540 vmx->rmode.irq.rip +=
2541 vmx->vcpu.arch.event_exit_inst_len;
Avi Kivity9c5623e2007-11-08 18:19:20 +02002542 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2543 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2544 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002545 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002546 return;
2547 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002548 intr = irq | INTR_INFO_VALID_MASK;
2549 if (vcpu->arch.interrupt.soft) {
2550 intr |= INTR_TYPE_SOFT_INTR;
2551 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2552 vmx->vcpu.arch.event_exit_inst_len);
2553 } else
2554 intr |= INTR_TYPE_EXT_INTR;
2555 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03002556}
2557
Sheng Yangf08864b2008-05-15 18:23:25 +08002558static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2559{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002560 struct vcpu_vmx *vmx = to_vmx(vcpu);
2561
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002562 if (!cpu_has_virtual_nmis()) {
2563 /*
2564 * Tracking the NMI-blocked state in software is built upon
2565 * finding the next open IRQ window. This, in turn, depends on
2566 * well-behaving guests: They have to keep IRQs disabled at
2567 * least as long as the NMI handler runs. Otherwise we may
2568 * cause NMI nesting, maybe breaking the guest. But as this is
2569 * highly unlikely, we can live with the residual risk.
2570 */
2571 vmx->soft_vnmi_blocked = 1;
2572 vmx->vnmi_blocked_time = 0;
2573 }
2574
Jan Kiszka487b3912008-09-26 09:30:56 +02002575 ++vcpu->stat.nmi_injections;
Nitin A Kamble56b237e2009-06-04 15:04:08 -07002576 if (vcpu->arch.rmode.vm86_active) {
Jan Kiszka66a5a342008-09-26 09:30:51 +02002577 vmx->rmode.irq.pending = true;
2578 vmx->rmode.irq.vector = NMI_VECTOR;
2579 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2580 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2581 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2582 INTR_INFO_VALID_MASK);
2583 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2584 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2585 return;
2586 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002587 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2588 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002589}
2590
Gleb Natapovc4282df2009-04-21 17:45:07 +03002591static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02002592{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002593 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03002594 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002595
Gleb Natapovc4282df2009-04-21 17:45:07 +03002596 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2597 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS |
2598 GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02002599}
2600
Gleb Natapov78646122009-03-23 12:12:11 +02002601static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
2602{
Gleb Natapovc4282df2009-04-21 17:45:07 +03002603 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2604 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2605 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02002606}
2607
Izik Eiduscbc94022007-10-25 00:29:55 +02002608static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2609{
2610 int ret;
2611 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002612 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002613 .guest_phys_addr = addr,
2614 .memory_size = PAGE_SIZE * 3,
2615 .flags = 0,
2616 };
2617
2618 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2619 if (ret)
2620 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002621 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002622 return 0;
2623}
2624
Avi Kivity6aa8b732006-12-10 02:21:36 -08002625static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2626 int vec, u32 err_code)
2627{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002628 /*
2629 * Instruction with address size override prefix opcode 0x67
2630 * Cause the #SS fault with 0 error code in VM86 mode.
2631 */
2632 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02002633 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002635 /*
2636 * Forward all other exceptions that are valid in real mode.
2637 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2638 * the required debugging infrastructure rework.
2639 */
2640 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002641 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002642 if (vcpu->guest_debug &
2643 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2644 return 0;
2645 kvm_queue_exception(vcpu, vec);
2646 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002647 case BP_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002648 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2649 return 0;
2650 /* fall through */
2651 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002652 case OF_VECTOR:
2653 case BR_VECTOR:
2654 case UD_VECTOR:
2655 case DF_VECTOR:
2656 case SS_VECTOR:
2657 case GP_VECTOR:
2658 case MF_VECTOR:
2659 kvm_queue_exception(vcpu, vec);
2660 return 1;
2661 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662 return 0;
2663}
2664
Andi Kleena0861c02009-06-08 17:37:09 +08002665/*
2666 * Trigger machine check on the host. We assume all the MSRs are already set up
2667 * by the CPU and that we still run on the same CPU as the MCE occurred on.
2668 * We pass a fake environment to the machine check handler because we want
2669 * the guest to be always treated like user space, no matter what context
2670 * it used internally.
2671 */
2672static void kvm_machine_check(void)
2673{
2674#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
2675 struct pt_regs regs = {
2676 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
2677 .flags = X86_EFLAGS_IF,
2678 };
2679
2680 do_machine_check(&regs, 0);
2681#endif
2682}
2683
2684static int handle_machine_check(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2685{
2686 /* already handled by vcpu_run */
2687 return 1;
2688}
2689
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2691{
Avi Kivity1155f762007-11-22 11:30:47 +02002692 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002693 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002694 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695 u32 vect_info;
2696 enum emulation_result er;
2697
Avi Kivity1155f762007-11-22 11:30:47 +02002698 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002699 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2700
Andi Kleena0861c02009-06-08 17:37:09 +08002701 if (is_machine_check(intr_info))
2702 return handle_machine_check(vcpu, kvm_run);
2703
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002705 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002706 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002707 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002708
Jan Kiszkae4a41882008-09-26 09:30:46 +02002709 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002710 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002711
2712 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002713 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002714 return 1;
2715 }
2716
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002717 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08002718 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002719 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002720 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002721 return 1;
2722 }
2723
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002725 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002726 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2728 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002729 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02002730 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002731 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002733 KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2,
2734 (u32)((u64)cr2 >> 32), handler);
Gleb Natapov3298b752009-05-11 13:35:46 +03002735 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03002736 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002737 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002738 }
2739
Nitin A Kamble56b237e2009-06-04 15:04:08 -07002740 if (vcpu->arch.rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002741 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002742 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002743 if (vcpu->arch.halt_request) {
2744 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002745 return kvm_emulate_halt(vcpu);
2746 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002747 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002748 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002749
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002750 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002751 switch (ex_no) {
2752 case DB_VECTOR:
2753 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2754 if (!(vcpu->guest_debug &
2755 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2756 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2757 kvm_queue_exception(vcpu, DB_VECTOR);
2758 return 1;
2759 }
2760 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2761 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2762 /* fall through */
2763 case BP_VECTOR:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002765 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2766 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002767 break;
2768 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002769 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2770 kvm_run->ex.exception = ex_no;
2771 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002772 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002773 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002774 return 0;
2775}
2776
2777static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2778 struct kvm_run *kvm_run)
2779{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002780 ++vcpu->stat.irq_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002781 KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782 return 1;
2783}
2784
Avi Kivity988ad742007-02-12 00:54:36 -08002785static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2786{
2787 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2788 return 0;
2789}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790
Avi Kivity6aa8b732006-12-10 02:21:36 -08002791static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2792{
He, Qingbfdaab02007-09-12 14:18:28 +08002793 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01002794 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002795 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002796
Avi Kivity1165f5f2007-04-19 17:27:43 +03002797 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002798 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002799 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002800
2801 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002802 if (emulate_instruction(vcpu,
2803 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002804 return 0;
2805 return 1;
2806 }
2807
2808 size = (exit_qualification & 7) + 1;
2809 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002810 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002811
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002812 skip_emulated_instruction(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002813 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814}
2815
Ingo Molnar102d8322007-02-19 14:37:47 +02002816static void
2817vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2818{
2819 /*
2820 * Patch in the VMCALL instruction:
2821 */
2822 hypercall[0] = 0x0f;
2823 hypercall[1] = 0x01;
2824 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002825}
2826
Avi Kivity6aa8b732006-12-10 02:21:36 -08002827static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2828{
He, Qingbfdaab02007-09-12 14:18:28 +08002829 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002830 int cr;
2831 int reg;
2832
He, Qingbfdaab02007-09-12 14:18:28 +08002833 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002834 cr = exit_qualification & 15;
2835 reg = (exit_qualification >> 8) & 15;
2836 switch ((exit_qualification >> 4) & 3) {
2837 case 0: /* mov to cr */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002838 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr,
2839 (u32)kvm_register_read(vcpu, reg),
2840 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
2841 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842 switch (cr) {
2843 case 0:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002844 kvm_set_cr0(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845 skip_emulated_instruction(vcpu);
2846 return 1;
2847 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002848 kvm_set_cr3(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002849 skip_emulated_instruction(vcpu);
2850 return 1;
2851 case 4:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002852 kvm_set_cr4(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853 skip_emulated_instruction(vcpu);
2854 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002855 case 8: {
2856 u8 cr8_prev = kvm_get_cr8(vcpu);
2857 u8 cr8 = kvm_register_read(vcpu, reg);
2858 kvm_set_cr8(vcpu, cr8);
2859 skip_emulated_instruction(vcpu);
2860 if (irqchip_in_kernel(vcpu->kvm))
2861 return 1;
2862 if (cr8_prev <= cr8)
2863 return 1;
2864 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2865 return 0;
2866 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002867 };
2868 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002869 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002870 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002871 vcpu->arch.cr0 &= ~X86_CR0_TS;
2872 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002873 vmx_fpu_activate(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002874 KVMTRACE_0D(CLTS, vcpu, handler);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002875 skip_emulated_instruction(vcpu);
2876 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877 case 1: /*mov from cr*/
2878 switch (cr) {
2879 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002880 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002881 KVMTRACE_3D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002882 (u32)kvm_register_read(vcpu, reg),
2883 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002884 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885 skip_emulated_instruction(vcpu);
2886 return 1;
2887 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002888 kvm_register_write(vcpu, reg, kvm_get_cr8(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002889 KVMTRACE_2D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002890 (u32)kvm_register_read(vcpu, reg), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002891 skip_emulated_instruction(vcpu);
2892 return 1;
2893 }
2894 break;
2895 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002896 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002897
2898 skip_emulated_instruction(vcpu);
2899 return 1;
2900 default:
2901 break;
2902 }
2903 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002904 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002905 (int)(exit_qualification >> 4) & 3, cr);
2906 return 0;
2907}
2908
2909static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2910{
He, Qingbfdaab02007-09-12 14:18:28 +08002911 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002912 unsigned long val;
2913 int dr, reg;
2914
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002915 dr = vmcs_readl(GUEST_DR7);
2916 if (dr & DR7_GD) {
2917 /*
2918 * As the vm-exit takes precedence over the debug trap, we
2919 * need to emulate the latter, either for the host or the
2920 * guest debugging itself.
2921 */
2922 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
2923 kvm_run->debug.arch.dr6 = vcpu->arch.dr6;
2924 kvm_run->debug.arch.dr7 = dr;
2925 kvm_run->debug.arch.pc =
2926 vmcs_readl(GUEST_CS_BASE) +
2927 vmcs_readl(GUEST_RIP);
2928 kvm_run->debug.arch.exception = DB_VECTOR;
2929 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2930 return 0;
2931 } else {
2932 vcpu->arch.dr7 &= ~DR7_GD;
2933 vcpu->arch.dr6 |= DR6_BD;
2934 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2935 kvm_queue_exception(vcpu, DB_VECTOR);
2936 return 1;
2937 }
2938 }
2939
He, Qingbfdaab02007-09-12 14:18:28 +08002940 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002941 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
2942 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
2943 if (exit_qualification & TYPE_MOV_FROM_DR) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002944 switch (dr) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002945 case 0 ... 3:
2946 val = vcpu->arch.db[dr];
2947 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002948 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002949 val = vcpu->arch.dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002950 break;
2951 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002952 val = vcpu->arch.dr7;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002953 break;
2954 default:
2955 val = 0;
2956 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002957 kvm_register_write(vcpu, reg, val);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002958 KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959 } else {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002960 val = vcpu->arch.regs[reg];
2961 switch (dr) {
2962 case 0 ... 3:
2963 vcpu->arch.db[dr] = val;
2964 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
2965 vcpu->arch.eff_db[dr] = val;
2966 break;
2967 case 4 ... 5:
2968 if (vcpu->arch.cr4 & X86_CR4_DE)
2969 kvm_queue_exception(vcpu, UD_VECTOR);
2970 break;
2971 case 6:
2972 if (val & 0xffffffff00000000ULL) {
2973 kvm_queue_exception(vcpu, GP_VECTOR);
2974 break;
2975 }
2976 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
2977 break;
2978 case 7:
2979 if (val & 0xffffffff00000000ULL) {
2980 kvm_queue_exception(vcpu, GP_VECTOR);
2981 break;
2982 }
2983 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
2984 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
2985 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2986 vcpu->arch.switch_db_regs =
2987 (val & DR7_BP_EN_MASK);
2988 }
2989 break;
2990 }
2991 KVMTRACE_2D(DR_WRITE, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002992 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002993 skip_emulated_instruction(vcpu);
2994 return 1;
2995}
2996
2997static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2998{
Avi Kivity06465c52007-02-28 20:46:53 +02002999 kvm_emulate_cpuid(vcpu);
3000 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003001}
3002
3003static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3004{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003005 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003006 u64 data;
3007
3008 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003009 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003010 return 1;
3011 }
3012
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003013 KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32),
3014 handler);
3015
Avi Kivity6aa8b732006-12-10 02:21:36 -08003016 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003017 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3018 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003019 skip_emulated_instruction(vcpu);
3020 return 1;
3021}
3022
3023static int handle_wrmsr(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];
3026 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3027 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003028
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003029 KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32),
3030 handler);
3031
Avi Kivity6aa8b732006-12-10 02:21:36 -08003032 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003033 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003034 return 1;
3035 }
3036
3037 skip_emulated_instruction(vcpu);
3038 return 1;
3039}
3040
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003041static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
3042 struct kvm_run *kvm_run)
3043{
3044 return 1;
3045}
3046
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047static int handle_interrupt_window(struct kvm_vcpu *vcpu,
3048 struct kvm_run *kvm_run)
3049{
Eddie Dong85f455f2007-07-06 12:20:49 +03003050 u32 cpu_based_vm_exec_control;
3051
3052 /* clear pending irq */
3053 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3054 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3055 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003056
3057 KVMTRACE_0D(PEND_INTR, vcpu, handler);
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003058 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003059
Dor Laorc1150d82007-01-05 16:36:24 -08003060 /*
3061 * If the user space waits to inject interrupts, exit as soon as
3062 * possible
3063 */
Gleb Natapov80618232009-04-21 17:44:56 +03003064 if (!irqchip_in_kernel(vcpu->kvm) &&
3065 kvm_run->request_interrupt_window &&
3066 !kvm_cpu_has_interrupt(vcpu)) {
Dor Laorc1150d82007-01-05 16:36:24 -08003067 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003068 return 0;
3069 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003070 return 1;
3071}
3072
3073static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3074{
3075 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003076 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003077}
3078
Ingo Molnarc21415e2007-02-19 14:37:47 +02003079static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3080{
Dor Laor510043d2007-02-19 18:25:43 +02003081 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003082 kvm_emulate_hypercall(vcpu);
3083 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003084}
3085
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003086static int handle_vmx_insn(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3087{
3088 kvm_queue_exception(vcpu, UD_VECTOR);
3089 return 1;
3090}
3091
Marcelo Tosattia7052892008-09-23 13:18:35 -03003092static int handle_invlpg(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3093{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003094 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003095
3096 kvm_mmu_invlpg(vcpu, exit_qualification);
3097 skip_emulated_instruction(vcpu);
3098 return 1;
3099}
3100
Eddie Donge5edaa02007-11-11 12:28:35 +02003101static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3102{
3103 skip_emulated_instruction(vcpu);
3104 /* TODO: Add support for VT-d/pass-through device */
3105 return 1;
3106}
3107
Sheng Yangf78e0e22007-10-29 09:40:42 +08003108static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3109{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003110 unsigned long exit_qualification;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003111 enum emulation_result er;
3112 unsigned long offset;
3113
Sheng Yangf9c617f2009-03-25 10:08:52 +08003114 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003115 offset = exit_qualification & 0xffful;
3116
3117 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3118
3119 if (er != EMULATE_DONE) {
3120 printk(KERN_ERR
3121 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3122 offset);
3123 return -ENOTSUPP;
3124 }
3125 return 1;
3126}
3127
Izik Eidus37817f22008-03-24 23:14:53 +02003128static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3129{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003130 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003131 unsigned long exit_qualification;
3132 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003133 int reason, type, idt_v;
3134
3135 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3136 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003137
3138 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3139
3140 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003141 if (reason == TASK_SWITCH_GATE && idt_v) {
3142 switch (type) {
3143 case INTR_TYPE_NMI_INTR:
3144 vcpu->arch.nmi_injected = false;
3145 if (cpu_has_virtual_nmis())
3146 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3147 GUEST_INTR_STATE_NMI);
3148 break;
3149 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003150 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003151 kvm_clear_interrupt_queue(vcpu);
3152 break;
3153 case INTR_TYPE_HARD_EXCEPTION:
3154 case INTR_TYPE_SOFT_EXCEPTION:
3155 kvm_clear_exception_queue(vcpu);
3156 break;
3157 default:
3158 break;
3159 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003160 }
Izik Eidus37817f22008-03-24 23:14:53 +02003161 tss_selector = exit_qualification;
3162
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003163 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3164 type != INTR_TYPE_EXT_INTR &&
3165 type != INTR_TYPE_NMI_INTR))
3166 skip_emulated_instruction(vcpu);
3167
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003168 if (!kvm_task_switch(vcpu, tss_selector, reason))
3169 return 0;
3170
3171 /* clear all local breakpoint enable flags */
3172 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3173
3174 /*
3175 * TODO: What about debug traps on tss switch?
3176 * Are we supposed to inject them and update dr6?
3177 */
3178
3179 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003180}
3181
Sheng Yang14394422008-04-28 12:24:45 +08003182static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3183{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003184 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003185 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003186 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003187
Sheng Yangf9c617f2009-03-25 10:08:52 +08003188 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003189
3190 if (exit_qualification & (1 << 6)) {
3191 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
3192 return -ENOTSUPP;
3193 }
3194
3195 gla_validity = (exit_qualification >> 7) & 0x3;
3196 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3197 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3198 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3199 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003200 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003201 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3202 (long unsigned int)exit_qualification);
3203 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity596ae892009-06-03 14:12:10 +03003204 kvm_run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
3205 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003206 }
3207
3208 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Sheng Yang49cd7d22009-02-11 13:50:40 +08003209 return kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003210}
3211
Sheng Yangf08864b2008-05-15 18:23:25 +08003212static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3213{
3214 u32 cpu_based_vm_exec_control;
3215
3216 /* clear pending NMI */
3217 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3218 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3219 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3220 ++vcpu->stat.nmi_window_exits;
3221
3222 return 1;
3223}
3224
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003225static void handle_invalid_guest_state(struct kvm_vcpu *vcpu,
3226 struct kvm_run *kvm_run)
3227{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003228 struct vcpu_vmx *vmx = to_vmx(vcpu);
3229 enum emulation_result err = EMULATE_DONE;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003230
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003231 local_irq_enable();
Jan Kiszka34f0c1a2009-07-22 23:53:26 +02003232 preempt_enable();
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003233
3234 while (!guest_state_valid(vcpu)) {
3235 err = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3236
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003237 if (err == EMULATE_DO_MMIO)
3238 break;
3239
3240 if (err != EMULATE_DONE) {
3241 kvm_report_emulation_failure(vcpu, "emulation failure");
Jan Kiszka263799a2009-07-21 10:43:07 +02003242 break;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003243 }
3244
3245 if (signal_pending(current))
3246 break;
3247 if (need_resched())
3248 schedule();
3249 }
3250
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003251 preempt_disable();
Jan Kiszka34f0c1a2009-07-22 23:53:26 +02003252 local_irq_disable();
Avi Kivity8b3079a2009-01-05 12:10:54 +02003253
3254 vmx->invalid_state_emulation_result = err;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003255}
3256
Avi Kivity6aa8b732006-12-10 02:21:36 -08003257/*
3258 * The exit handlers return 1 if the exit was handled fully and guest execution
3259 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3260 * to be done to userspace and return 0.
3261 */
3262static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
3263 struct kvm_run *kvm_run) = {
3264 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3265 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003266 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003267 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003268 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003269 [EXIT_REASON_CR_ACCESS] = handle_cr,
3270 [EXIT_REASON_DR_ACCESS] = handle_dr,
3271 [EXIT_REASON_CPUID] = handle_cpuid,
3272 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3273 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3274 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3275 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003276 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003277 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003278 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3279 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3280 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3281 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3282 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3283 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3284 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3285 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3286 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003287 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3288 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003289 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003290 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Sheng Yang14394422008-04-28 12:24:45 +08003291 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
Andi Kleena0861c02009-06-08 17:37:09 +08003292 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003293};
3294
3295static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003296 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003297
3298/*
3299 * The guest has exited. See if we can fix it or if we need userspace
3300 * assistance.
3301 */
Avi Kivity6062d012009-03-23 17:35:17 +02003302static int vmx_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003303{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003304 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003305 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003306 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003307
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003308 KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)kvm_rip_read(vcpu),
3309 (u32)((u64)kvm_rip_read(vcpu) >> 32), entryexit);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003310
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003311 /* If we need to emulate an MMIO from handle_invalid_guest_state
3312 * we just return 0 */
Avi Kivity10f32d82009-01-05 00:53:19 +02003313 if (vmx->emulation_required && emulate_invalid_guest_state) {
3314 if (guest_state_valid(vcpu))
3315 vmx->emulation_required = 0;
Avi Kivity8b3079a2009-01-05 12:10:54 +02003316 return vmx->invalid_state_emulation_result != EMULATE_DO_MMIO;
Avi Kivity10f32d82009-01-05 00:53:19 +02003317 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003318
Sheng Yang14394422008-04-28 12:24:45 +08003319 /* Access CR3 don't cause VMExit in paging mode, so we need
3320 * to sync with guest real CR3. */
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003321 if (enable_ept && is_paging(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08003322 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003323
Avi Kivity29bd8a72007-09-10 17:27:03 +03003324 if (unlikely(vmx->fail)) {
3325 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3326 kvm_run->fail_entry.hardware_entry_failure_reason
3327 = vmcs_read32(VM_INSTRUCTION_ERROR);
3328 return 0;
3329 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003330
Mike Dayd77c26f2007-10-08 09:02:08 -04003331 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003332 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003333 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3334 exit_reason != EXIT_REASON_TASK_SWITCH))
3335 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3336 "(0x%x) and exit reason is 0x%x\n",
3337 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003338
3339 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03003340 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003341 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003342 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003343 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003344 /*
3345 * This CPU don't support us in finding the end of an
3346 * NMI-blocked window if the guest runs with IRQs
3347 * disabled. So we pull the trigger after 1 s of
3348 * futile waiting, but inform the user about this.
3349 */
3350 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3351 "state on VCPU %d after 1 s timeout\n",
3352 __func__, vcpu->vcpu_id);
3353 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003354 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003355 }
3356
Avi Kivity6aa8b732006-12-10 02:21:36 -08003357 if (exit_reason < kvm_vmx_max_exit_handlers
3358 && kvm_vmx_exit_handlers[exit_reason])
3359 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
3360 else {
3361 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3362 kvm_run->hw.hardware_exit_reason = exit_reason;
3363 }
3364 return 0;
3365}
3366
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003367static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003368{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003369 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003370 vmcs_write32(TPR_THRESHOLD, 0);
3371 return;
3372 }
3373
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003374 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003375}
3376
Avi Kivitycf393f72008-07-01 16:20:21 +03003377static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3378{
3379 u32 exit_intr_info;
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003380 u32 idt_vectoring_info = vmx->idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003381 bool unblock_nmi;
3382 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003383 int type;
3384 bool idtv_info_valid;
Avi Kivitycf393f72008-07-01 16:20:21 +03003385
3386 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Gleb Natapov20f65982009-05-11 13:35:55 +03003387
Andi Kleena0861c02009-06-08 17:37:09 +08003388 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
3389
3390 /* Handle machine checks before interrupts are enabled */
3391 if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
3392 || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
3393 && is_machine_check(exit_intr_info)))
3394 kvm_machine_check();
3395
Gleb Natapov20f65982009-05-11 13:35:55 +03003396 /* We need to handle NMIs before interrupts are enabled */
3397 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
3398 (exit_intr_info & INTR_INFO_VALID_MASK)) {
3399 KVMTRACE_0D(NMI, &vmx->vcpu, handler);
3400 asm("int $2");
3401 }
3402
3403 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3404
Avi Kivitycf393f72008-07-01 16:20:21 +03003405 if (cpu_has_virtual_nmis()) {
3406 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3407 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3408 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003409 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03003410 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3411 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003412 * SDM 3: 23.2.2 (September 2008)
3413 * Bit 12 is undefined in any of the following cases:
3414 * If the VM exit sets the valid bit in the IDT-vectoring
3415 * information field.
3416 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03003417 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003418 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
3419 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03003420 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3421 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003422 } else if (unlikely(vmx->soft_vnmi_blocked))
3423 vmx->vnmi_blocked_time +=
3424 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003425
Gleb Natapov37b96e92009-03-30 16:03:13 +03003426 vmx->vcpu.arch.nmi_injected = false;
3427 kvm_clear_exception_queue(&vmx->vcpu);
3428 kvm_clear_interrupt_queue(&vmx->vcpu);
3429
3430 if (!idtv_info_valid)
3431 return;
3432
Avi Kivity668f6122008-07-02 09:28:55 +03003433 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3434 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003435
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003436 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003437 case INTR_TYPE_NMI_INTR:
3438 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03003439 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003440 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03003441 * Clear bit "block by NMI" before VM entry if a NMI
3442 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03003443 */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003444 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3445 GUEST_INTR_STATE_NMI);
3446 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003447 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003448 vmx->vcpu.arch.event_exit_inst_len =
3449 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3450 /* fall through */
3451 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03003452 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003453 u32 err = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3454 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03003455 } else
3456 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003457 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003458 case INTR_TYPE_SOFT_INTR:
3459 vmx->vcpu.arch.event_exit_inst_len =
3460 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3461 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003462 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003463 kvm_queue_interrupt(&vmx->vcpu, vector,
3464 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003465 break;
3466 default:
3467 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03003468 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003469}
3470
Avi Kivity9c8cba32007-11-22 11:42:59 +02003471/*
3472 * Failure to inject an interrupt should give us the information
3473 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3474 * when fetching the interrupt redirection bitmap in the real-mode
3475 * tss, this doesn't happen. So we do it ourselves.
3476 */
3477static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3478{
3479 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003480 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003481 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003482 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003483 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3484 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3485 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3486 return;
3487 }
3488 vmx->idt_vectoring_info =
3489 VECTORING_INFO_VALID_MASK
3490 | INTR_TYPE_EXT_INTR
3491 | vmx->rmode.irq.vector;
3492}
3493
Avi Kivityc8019492008-07-14 14:44:59 +03003494#ifdef CONFIG_X86_64
3495#define R "r"
3496#define Q "q"
3497#else
3498#define R "e"
3499#define Q "l"
3500#endif
3501
Avi Kivity04d2cc72007-09-10 18:10:54 +03003502static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003503{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003504 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitye6adf282007-04-30 16:07:54 +03003505
Avi Kivity8f5d5492009-05-31 18:41:29 +03003506 if (enable_ept && is_paging(vcpu)) {
3507 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3508 ept_load_pdptrs(vcpu);
3509 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003510 /* Record the guest's net vcpu time for enforced NMI injections. */
3511 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3512 vmx->entry_time = ktime_get();
3513
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003514 /* Handle invalid guest state instead of entering VMX */
3515 if (vmx->emulation_required && emulate_invalid_guest_state) {
3516 handle_invalid_guest_state(vcpu, kvm_run);
3517 return;
3518 }
3519
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003520 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3521 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3522 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3523 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3524
Gleb Natapov787ff732009-05-18 11:44:06 +03003525 /* When single-stepping over STI and MOV SS, we must clear the
3526 * corresponding interruptibility bits in the guest state. Otherwise
3527 * vmentry fails as it then expects bit 14 (BS) in pending debug
3528 * exceptions being set, but that's not correct for the guest debugging
3529 * case. */
3530 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3531 vmx_set_interrupt_shadow(vcpu, 0);
3532
Avi Kivitye6adf282007-04-30 16:07:54 +03003533 /*
3534 * Loading guest fpu may have cleared host cr0.ts
3535 */
3536 vmcs_writel(HOST_CR0, read_cr0());
3537
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003538 set_debugreg(vcpu->arch.dr6, 6);
3539
Mike Dayd77c26f2007-10-08 09:02:08 -04003540 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003541 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003542 "push %%"R"dx; push %%"R"bp;"
3543 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003544 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3545 "je 1f \n\t"
3546 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003547 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003548 "1: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003549 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003550 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003551 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003552 "mov %c[cr2](%0), %%"R"ax \n\t"
3553 "mov %%"R"ax, %%cr2 \n\t"
3554 "mov %c[rax](%0), %%"R"ax \n\t"
3555 "mov %c[rbx](%0), %%"R"bx \n\t"
3556 "mov %c[rdx](%0), %%"R"dx \n\t"
3557 "mov %c[rsi](%0), %%"R"si \n\t"
3558 "mov %c[rdi](%0), %%"R"di \n\t"
3559 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003560#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003561 "mov %c[r8](%0), %%r8 \n\t"
3562 "mov %c[r9](%0), %%r9 \n\t"
3563 "mov %c[r10](%0), %%r10 \n\t"
3564 "mov %c[r11](%0), %%r11 \n\t"
3565 "mov %c[r12](%0), %%r12 \n\t"
3566 "mov %c[r13](%0), %%r13 \n\t"
3567 "mov %c[r14](%0), %%r14 \n\t"
3568 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003569#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003570 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3571
Avi Kivity6aa8b732006-12-10 02:21:36 -08003572 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003573 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003574 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003575 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003576 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003577 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003578 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003579 "xchg %0, (%%"R"sp) \n\t"
3580 "mov %%"R"ax, %c[rax](%0) \n\t"
3581 "mov %%"R"bx, %c[rbx](%0) \n\t"
3582 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3583 "mov %%"R"dx, %c[rdx](%0) \n\t"
3584 "mov %%"R"si, %c[rsi](%0) \n\t"
3585 "mov %%"R"di, %c[rdi](%0) \n\t"
3586 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003587#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003588 "mov %%r8, %c[r8](%0) \n\t"
3589 "mov %%r9, %c[r9](%0) \n\t"
3590 "mov %%r10, %c[r10](%0) \n\t"
3591 "mov %%r11, %c[r11](%0) \n\t"
3592 "mov %%r12, %c[r12](%0) \n\t"
3593 "mov %%r13, %c[r13](%0) \n\t"
3594 "mov %%r14, %c[r14](%0) \n\t"
3595 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003596#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003597 "mov %%cr2, %%"R"ax \n\t"
3598 "mov %%"R"ax, %c[cr2](%0) \n\t"
3599
3600 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003601 "setbe %c[fail](%0) \n\t"
3602 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3603 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3604 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03003605 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003606 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3607 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3608 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3609 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3610 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3611 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3612 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003613#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003614 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3615 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3616 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3617 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3618 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3619 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3620 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3621 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003622#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003623 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003624 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003625 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003626#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003627 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3628#endif
3629 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003630
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003631 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
3632 | (1 << VCPU_EXREG_PDPTR));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003633 vcpu->arch.regs_dirty = 0;
3634
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003635 get_debugreg(vcpu->arch.dr6, 6);
3636
Avi Kivity1155f762007-11-22 11:30:47 +02003637 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003638 if (vmx->rmode.irq.pending)
3639 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003640
Mike Dayd77c26f2007-10-08 09:02:08 -04003641 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003642 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003643
Avi Kivitycf393f72008-07-01 16:20:21 +03003644 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003645}
3646
Avi Kivityc8019492008-07-14 14:44:59 +03003647#undef R
3648#undef Q
3649
Avi Kivity6aa8b732006-12-10 02:21:36 -08003650static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3651{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003652 struct vcpu_vmx *vmx = to_vmx(vcpu);
3653
3654 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003655 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003656 free_vmcs(vmx->vmcs);
3657 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003658 }
3659}
3660
3661static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3662{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003663 struct vcpu_vmx *vmx = to_vmx(vcpu);
3664
Sheng Yang2384d2b2008-01-17 15:14:33 +08003665 spin_lock(&vmx_vpid_lock);
3666 if (vmx->vpid != 0)
3667 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3668 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003669 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003670 kfree(vmx->host_msrs);
3671 kfree(vmx->guest_msrs);
3672 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003673 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003674}
3675
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003676static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003677{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003678 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003679 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003680 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003681
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003682 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003683 return ERR_PTR(-ENOMEM);
3684
Sheng Yang2384d2b2008-01-17 15:14:33 +08003685 allocate_vpid(vmx);
3686
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003687 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3688 if (err)
3689 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003690
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003691 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003692 if (!vmx->guest_msrs) {
3693 err = -ENOMEM;
3694 goto uninit_vcpu;
3695 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003696
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003697 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3698 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003699 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003700
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003701 vmx->vmcs = alloc_vmcs();
3702 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003703 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003704
3705 vmcs_clear(vmx->vmcs);
3706
Avi Kivity15ad7142007-07-11 18:17:21 +03003707 cpu = get_cpu();
3708 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003709 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003710 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003711 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003712 if (err)
3713 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003714 if (vm_need_virtualize_apic_accesses(kvm))
3715 if (alloc_apic_access_page(kvm) != 0)
3716 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003717
Avi Kivity089d0342009-03-23 18:26:32 +02003718 if (enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003719 if (alloc_identity_pagetable(kvm) != 0)
3720 goto free_vmcs;
3721
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003722 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003723
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003724free_vmcs:
3725 free_vmcs(vmx->vmcs);
3726free_msrs:
3727 kfree(vmx->host_msrs);
3728free_guest_msrs:
3729 kfree(vmx->guest_msrs);
3730uninit_vcpu:
3731 kvm_vcpu_uninit(&vmx->vcpu);
3732free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003733 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003734 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003735}
3736
Yang, Sheng002c7f72007-07-31 14:23:01 +03003737static void __init vmx_check_processor_compat(void *rtn)
3738{
3739 struct vmcs_config vmcs_conf;
3740
3741 *(int *)rtn = 0;
3742 if (setup_vmcs_config(&vmcs_conf) < 0)
3743 *(int *)rtn = -EIO;
3744 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3745 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3746 smp_processor_id());
3747 *(int *)rtn = -EIO;
3748 }
3749}
3750
Sheng Yang67253af2008-04-25 10:20:22 +08003751static int get_ept_level(void)
3752{
3753 return VMX_EPT_DEFAULT_GAW + 1;
3754}
3755
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003756static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003757{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003758 u64 ret;
3759
Sheng Yang522c68c2009-04-27 20:35:43 +08003760 /* For VT-d and EPT combination
3761 * 1. MMIO: always map as UC
3762 * 2. EPT with VT-d:
3763 * a. VT-d without snooping control feature: can't guarantee the
3764 * result, try to trust guest.
3765 * b. VT-d with snooping control feature: snooping control feature of
3766 * VT-d engine can guarantee the cache correctness. Just set it
3767 * to WB to keep consistent with host. So the same as item 3.
3768 * 3. EPT without VT-d: always map as WB and set IGMT=1 to keep
3769 * consistent with host MTRR
3770 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003771 if (is_mmio)
3772 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08003773 else if (vcpu->kvm->arch.iommu_domain &&
3774 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
3775 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
3776 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003777 else
Sheng Yang522c68c2009-04-27 20:35:43 +08003778 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
3779 | VMX_EPT_IGMT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003780
3781 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08003782}
3783
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003784static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003785 .cpu_has_kvm_support = cpu_has_kvm_support,
3786 .disabled_by_bios = vmx_disabled_by_bios,
3787 .hardware_setup = hardware_setup,
3788 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003789 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003790 .hardware_enable = hardware_enable,
3791 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08003792 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003793
3794 .vcpu_create = vmx_create_vcpu,
3795 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003796 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003797
Avi Kivity04d2cc72007-09-10 18:10:54 +03003798 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003799 .vcpu_load = vmx_vcpu_load,
3800 .vcpu_put = vmx_vcpu_put,
3801
3802 .set_guest_debug = set_guest_debug,
3803 .get_msr = vmx_get_msr,
3804 .set_msr = vmx_set_msr,
3805 .get_segment_base = vmx_get_segment_base,
3806 .get_segment = vmx_get_segment,
3807 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003808 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003809 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003810 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003811 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003812 .set_cr3 = vmx_set_cr3,
3813 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003814 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003815 .get_idt = vmx_get_idt,
3816 .set_idt = vmx_set_idt,
3817 .get_gdt = vmx_get_gdt,
3818 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003819 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003820 .get_rflags = vmx_get_rflags,
3821 .set_rflags = vmx_set_rflags,
3822
3823 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003824
Avi Kivity6aa8b732006-12-10 02:21:36 -08003825 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02003826 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003827 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003828 .set_interrupt_shadow = vmx_set_interrupt_shadow,
3829 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003830 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003831 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003832 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003833 .queue_exception = vmx_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003834 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003835 .nmi_allowed = vmx_nmi_allowed,
3836 .enable_nmi_window = enable_nmi_window,
3837 .enable_irq_window = enable_irq_window,
3838 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003839
Izik Eiduscbc94022007-10-25 00:29:55 +02003840 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003841 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003842 .get_mt_mask = vmx_get_mt_mask,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003843};
3844
3845static int __init vmx_init(void)
3846{
He, Qingfdef3ad2007-04-30 09:45:24 +03003847 int r;
3848
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003849 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03003850 if (!vmx_io_bitmap_a)
3851 return -ENOMEM;
3852
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003853 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03003854 if (!vmx_io_bitmap_b) {
3855 r = -ENOMEM;
3856 goto out;
3857 }
3858
Avi Kivity58972972009-02-24 22:26:47 +02003859 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
3860 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08003861 r = -ENOMEM;
3862 goto out1;
3863 }
3864
Avi Kivity58972972009-02-24 22:26:47 +02003865 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
3866 if (!vmx_msr_bitmap_longmode) {
3867 r = -ENOMEM;
3868 goto out2;
3869 }
3870
He, Qingfdef3ad2007-04-30 09:45:24 +03003871 /*
3872 * Allow direct access to the PC debug port (it is often used for I/O
3873 * delays, but the vmexits simply slow things down).
3874 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003875 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
3876 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003877
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003878 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003879
Avi Kivity58972972009-02-24 22:26:47 +02003880 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
3881 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08003882
Sheng Yang2384d2b2008-01-17 15:14:33 +08003883 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
3884
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003885 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003886 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02003887 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08003888
Avi Kivity58972972009-02-24 22:26:47 +02003889 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
3890 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
3891 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
3892 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
3893 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
3894 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03003895
Avi Kivity089d0342009-03-23 18:26:32 +02003896 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003897 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003898 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08003899 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08003900 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003901 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003902 kvm_enable_tdp();
3903 } else
3904 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08003905
Avi Kivityc7addb92007-09-16 18:58:32 +02003906 if (bypass_guest_pf)
3907 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
3908
Sheng Yang14394422008-04-28 12:24:45 +08003909 ept_sync_global();
3910
He, Qingfdef3ad2007-04-30 09:45:24 +03003911 return 0;
3912
Avi Kivity58972972009-02-24 22:26:47 +02003913out3:
3914 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08003915out2:
Avi Kivity58972972009-02-24 22:26:47 +02003916 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03003917out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003918 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03003919out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003920 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003921 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003922}
3923
3924static void __exit vmx_exit(void)
3925{
Avi Kivity58972972009-02-24 22:26:47 +02003926 free_page((unsigned long)vmx_msr_bitmap_legacy);
3927 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003928 free_page((unsigned long)vmx_io_bitmap_b);
3929 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003930
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003931 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003932}
3933
3934module_init(vmx_init)
3935module_exit(vmx_exit)