blob: c6256b98f078ae265a5ed14b7208779dcf497c1f [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040020
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020023#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/mm.h>
25#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040026#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020027#include <linux/moduleparam.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030028#include <linux/ftrace_event.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030029#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030030#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080033#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020034#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020035#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080036#include <asm/mce.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080037
Marcelo Tosatti229456f2009-06-17 09:22:14 -030038#include "trace.h"
39
Avi Kivity4ecac3f2008-05-13 13:23:38 +030040#define __ex(x) __kvm_handle_fault_on_reboot(x)
41
Avi Kivity6aa8b732006-12-10 02:21:36 -080042MODULE_AUTHOR("Qumranet");
43MODULE_LICENSE("GPL");
44
Avi Kivity4462d212009-03-23 17:53:37 +020045static int __read_mostly bypass_guest_pf = 1;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020046module_param(bypass_guest_pf, bool, S_IRUGO);
Avi Kivityc7addb92007-09-16 18:58:32 +020047
Avi Kivity4462d212009-03-23 17:53:37 +020048static int __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020049module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080050
Avi Kivity4462d212009-03-23 17:53:37 +020051static int __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020052module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020053
Avi Kivity4462d212009-03-23 17:53:37 +020054static int __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020055module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080056
Nitin A Kamble3a624e22009-06-08 11:34:16 -070057static int __read_mostly enable_unrestricted_guest = 1;
58module_param_named(unrestricted_guest,
59 enable_unrestricted_guest, bool, S_IRUGO);
60
Avi Kivity4462d212009-03-23 17:53:37 +020061static int __read_mostly emulate_invalid_guest_state = 0;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020062module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030063
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040064struct vmcs {
65 u32 revision_id;
66 u32 abort;
67 char data[0];
68};
69
70struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100071 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +030072 struct list_head local_vcpus_link;
Avi Kivity313dbd42008-07-17 18:04:30 +030073 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040074 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030075 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020076 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040077 struct kvm_msr_entry *guest_msrs;
78 struct kvm_msr_entry *host_msrs;
79 int nmsrs;
80 int save_nmsrs;
81 int msr_offset_efer;
82#ifdef CONFIG_X86_64
83 int msr_offset_kernel_gs_base;
84#endif
85 struct vmcs *vmcs;
86 struct {
87 int loaded;
88 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020089 int gs_ldt_reload_needed;
90 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030091 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040092 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +020093 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +030094 int vm86_active;
95 u8 save_iopl;
96 struct kvm_save_segment {
97 u16 selector;
98 unsigned long base;
99 u32 limit;
100 u32 ar;
101 } tr, es, ds, fs, gs;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200102 struct {
103 bool pending;
104 u8 vector;
105 unsigned rip;
106 } irq;
107 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800108 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300109 bool emulation_required;
Avi Kivity8b3079a2009-01-05 12:10:54 +0200110 enum emulation_result invalid_state_emulation_result;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200111
112 /* Support for vnmi-less CPUs */
113 int soft_vnmi_blocked;
114 ktime_t entry_time;
115 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800116 u32 exit_reason;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400117};
118
119static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
120{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000121 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400122}
123
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800124static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800125static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300126
Avi Kivity6aa8b732006-12-10 02:21:36 -0800127static DEFINE_PER_CPU(struct vmcs *, vmxarea);
128static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300129static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200131static unsigned long *vmx_io_bitmap_a;
132static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200133static unsigned long *vmx_msr_bitmap_legacy;
134static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300135
Sheng Yang2384d2b2008-01-17 15:14:33 +0800136static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
137static DEFINE_SPINLOCK(vmx_vpid_lock);
138
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300139static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140 int size;
141 int order;
142 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300143 u32 pin_based_exec_ctrl;
144 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800145 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300146 u32 vmexit_ctrl;
147 u32 vmentry_ctrl;
148} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800149
Hannes Ederefff9e52008-11-28 17:02:06 +0100150static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800151 u32 ept;
152 u32 vpid;
153} vmx_capability;
154
Avi Kivity6aa8b732006-12-10 02:21:36 -0800155#define VMX_SEGMENT_FIELD(seg) \
156 [VCPU_SREG_##seg] = { \
157 .selector = GUEST_##seg##_SELECTOR, \
158 .base = GUEST_##seg##_BASE, \
159 .limit = GUEST_##seg##_LIMIT, \
160 .ar_bytes = GUEST_##seg##_AR_BYTES, \
161 }
162
163static struct kvm_vmx_segment_field {
164 unsigned selector;
165 unsigned base;
166 unsigned limit;
167 unsigned ar_bytes;
168} kvm_vmx_segment_fields[] = {
169 VMX_SEGMENT_FIELD(CS),
170 VMX_SEGMENT_FIELD(DS),
171 VMX_SEGMENT_FIELD(ES),
172 VMX_SEGMENT_FIELD(FS),
173 VMX_SEGMENT_FIELD(GS),
174 VMX_SEGMENT_FIELD(SS),
175 VMX_SEGMENT_FIELD(TR),
176 VMX_SEGMENT_FIELD(LDTR),
177};
178
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300179static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
180
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300181/*
182 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
183 * away by decrementing the array size.
184 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800186#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800187 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
188#endif
189 MSR_EFER, MSR_K6_STAR,
190};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200191#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400193static void load_msrs(struct kvm_msr_entry *e, int n)
194{
195 int i;
196
197 for (i = 0; i < n; ++i)
198 wrmsrl(e[i].index, e[i].data);
199}
200
201static void save_msrs(struct kvm_msr_entry *e, int n)
202{
203 int i;
204
205 for (i = 0; i < n; ++i)
206 rdmsrl(e[i].index, e[i].data);
207}
208
Avi Kivity6aa8b732006-12-10 02:21:36 -0800209static inline int is_page_fault(u32 intr_info)
210{
211 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
212 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100213 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800214}
215
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300216static inline int is_no_device(u32 intr_info)
217{
218 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
219 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100220 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300221}
222
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500223static inline int is_invalid_opcode(u32 intr_info)
224{
225 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
226 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100227 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500228}
229
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230static inline int is_external_interrupt(u32 intr_info)
231{
232 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
233 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
234}
235
Andi Kleena0861c02009-06-08 17:37:09 +0800236static inline int is_machine_check(u32 intr_info)
237{
238 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
239 INTR_INFO_VALID_MASK)) ==
240 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
241}
242
Sheng Yang25c5f222008-03-28 13:18:56 +0800243static inline int cpu_has_vmx_msr_bitmap(void)
244{
Sheng Yang04547152009-04-01 15:52:31 +0800245 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800246}
247
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800248static inline int cpu_has_vmx_tpr_shadow(void)
249{
Sheng Yang04547152009-04-01 15:52:31 +0800250 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800251}
252
253static inline int vm_need_tpr_shadow(struct kvm *kvm)
254{
Sheng Yang04547152009-04-01 15:52:31 +0800255 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800256}
257
Sheng Yangf78e0e22007-10-29 09:40:42 +0800258static inline int cpu_has_secondary_exec_ctrls(void)
259{
Sheng Yang04547152009-04-01 15:52:31 +0800260 return vmcs_config.cpu_based_exec_ctrl &
261 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800262}
263
Avi Kivity774ead32007-12-26 13:57:04 +0200264static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800265{
Sheng Yang04547152009-04-01 15:52:31 +0800266 return vmcs_config.cpu_based_2nd_exec_ctrl &
267 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
268}
269
270static inline bool cpu_has_vmx_flexpriority(void)
271{
272 return cpu_has_vmx_tpr_shadow() &&
273 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800274}
275
Marcelo Tosattie7997942009-06-11 12:07:40 -0300276static inline bool cpu_has_vmx_ept_execute_only(void)
277{
278 return !!(vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT);
279}
280
281static inline bool cpu_has_vmx_eptp_uncacheable(void)
282{
283 return !!(vmx_capability.ept & VMX_EPTP_UC_BIT);
284}
285
286static inline bool cpu_has_vmx_eptp_writeback(void)
287{
288 return !!(vmx_capability.ept & VMX_EPTP_WB_BIT);
289}
290
291static inline bool cpu_has_vmx_ept_2m_page(void)
292{
293 return !!(vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT);
294}
295
Sheng Yangd56f5462008-04-25 10:13:16 +0800296static inline int cpu_has_vmx_invept_individual_addr(void)
297{
Sheng Yang04547152009-04-01 15:52:31 +0800298 return !!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800299}
300
301static inline int cpu_has_vmx_invept_context(void)
302{
Sheng Yang04547152009-04-01 15:52:31 +0800303 return !!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800304}
305
306static inline int cpu_has_vmx_invept_global(void)
307{
Sheng Yang04547152009-04-01 15:52:31 +0800308 return !!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800309}
310
311static inline int cpu_has_vmx_ept(void)
312{
Sheng Yang04547152009-04-01 15:52:31 +0800313 return vmcs_config.cpu_based_2nd_exec_ctrl &
314 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800315}
316
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700317static inline int cpu_has_vmx_unrestricted_guest(void)
318{
319 return vmcs_config.cpu_based_2nd_exec_ctrl &
320 SECONDARY_EXEC_UNRESTRICTED_GUEST;
321}
322
Sheng Yangf78e0e22007-10-29 09:40:42 +0800323static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
324{
Sheng Yang04547152009-04-01 15:52:31 +0800325 return flexpriority_enabled &&
326 (cpu_has_vmx_virtualize_apic_accesses()) &&
327 (irqchip_in_kernel(kvm));
Sheng Yangf78e0e22007-10-29 09:40:42 +0800328}
329
Sheng Yang2384d2b2008-01-17 15:14:33 +0800330static inline int cpu_has_vmx_vpid(void)
331{
Sheng Yang04547152009-04-01 15:52:31 +0800332 return vmcs_config.cpu_based_2nd_exec_ctrl &
333 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800334}
335
Sheng Yangf08864b2008-05-15 18:23:25 +0800336static inline int cpu_has_virtual_nmis(void)
337{
338 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
339}
340
Sheng Yang04547152009-04-01 15:52:31 +0800341static inline bool report_flexpriority(void)
342{
343 return flexpriority_enabled;
344}
345
Rusty Russell8b9cf982007-07-30 16:31:43 +1000346static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800347{
348 int i;
349
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400350 for (i = 0; i < vmx->nmsrs; ++i)
351 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300352 return i;
353 return -1;
354}
355
Sheng Yang2384d2b2008-01-17 15:14:33 +0800356static inline void __invvpid(int ext, u16 vpid, gva_t gva)
357{
358 struct {
359 u64 vpid : 16;
360 u64 rsvd : 48;
361 u64 gva;
362 } operand = { vpid, 0, gva };
363
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300364 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800365 /* CF==1 or ZF==1 --> rc = -1 */
366 "; ja 1f ; ud2 ; 1:"
367 : : "a"(&operand), "c"(ext) : "cc", "memory");
368}
369
Sheng Yang14394422008-04-28 12:24:45 +0800370static inline void __invept(int ext, u64 eptp, gpa_t gpa)
371{
372 struct {
373 u64 eptp, gpa;
374 } operand = {eptp, gpa};
375
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300376 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800377 /* CF==1 or ZF==1 --> rc = -1 */
378 "; ja 1f ; ud2 ; 1:\n"
379 : : "a" (&operand), "c" (ext) : "cc", "memory");
380}
381
Rusty Russell8b9cf982007-07-30 16:31:43 +1000382static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300383{
384 int i;
385
Rusty Russell8b9cf982007-07-30 16:31:43 +1000386 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300387 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400388 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000389 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800390}
391
Avi Kivity6aa8b732006-12-10 02:21:36 -0800392static void vmcs_clear(struct vmcs *vmcs)
393{
394 u64 phys_addr = __pa(vmcs);
395 u8 error;
396
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300397 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
399 : "cc", "memory");
400 if (error)
401 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
402 vmcs, phys_addr);
403}
404
405static void __vcpu_clear(void *arg)
406{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000407 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800408 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800409
Rusty Russell8b9cf982007-07-30 16:31:43 +1000410 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400411 vmcs_clear(vmx->vmcs);
412 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800413 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800414 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300415 list_del(&vmx->local_vcpus_link);
416 vmx->vcpu.cpu = -1;
417 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418}
419
Rusty Russell8b9cf982007-07-30 16:31:43 +1000420static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800421{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200422 if (vmx->vcpu.cpu == -1)
423 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200424 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800425}
426
Sheng Yang2384d2b2008-01-17 15:14:33 +0800427static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
428{
429 if (vmx->vpid == 0)
430 return;
431
432 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
433}
434
Sheng Yang14394422008-04-28 12:24:45 +0800435static inline void ept_sync_global(void)
436{
437 if (cpu_has_vmx_invept_global())
438 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
439}
440
441static inline void ept_sync_context(u64 eptp)
442{
Avi Kivity089d0342009-03-23 18:26:32 +0200443 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800444 if (cpu_has_vmx_invept_context())
445 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
446 else
447 ept_sync_global();
448 }
449}
450
451static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
452{
Avi Kivity089d0342009-03-23 18:26:32 +0200453 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800454 if (cpu_has_vmx_invept_individual_addr())
455 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
456 eptp, gpa);
457 else
458 ept_sync_context(eptp);
459 }
460}
461
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462static unsigned long vmcs_readl(unsigned long field)
463{
464 unsigned long value;
465
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300466 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467 : "=a"(value) : "d"(field) : "cc");
468 return value;
469}
470
471static u16 vmcs_read16(unsigned long field)
472{
473 return vmcs_readl(field);
474}
475
476static u32 vmcs_read32(unsigned long field)
477{
478 return vmcs_readl(field);
479}
480
481static u64 vmcs_read64(unsigned long field)
482{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800483#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 return vmcs_readl(field);
485#else
486 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
487#endif
488}
489
Avi Kivitye52de1b2007-01-05 16:36:56 -0800490static noinline void vmwrite_error(unsigned long field, unsigned long value)
491{
492 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
493 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
494 dump_stack();
495}
496
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497static void vmcs_writel(unsigned long field, unsigned long value)
498{
499 u8 error;
500
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300501 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400502 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800503 if (unlikely(error))
504 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800505}
506
507static void vmcs_write16(unsigned long field, u16 value)
508{
509 vmcs_writel(field, value);
510}
511
512static void vmcs_write32(unsigned long field, u32 value)
513{
514 vmcs_writel(field, value);
515}
516
517static void vmcs_write64(unsigned long field, u64 value)
518{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300520#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521 asm volatile ("");
522 vmcs_writel(field+1, value >> 32);
523#endif
524}
525
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300526static void vmcs_clear_bits(unsigned long field, u32 mask)
527{
528 vmcs_writel(field, vmcs_readl(field) & ~mask);
529}
530
531static void vmcs_set_bits(unsigned long field, u32 mask)
532{
533 vmcs_writel(field, vmcs_readl(field) | mask);
534}
535
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300536static void update_exception_bitmap(struct kvm_vcpu *vcpu)
537{
538 u32 eb;
539
Andi Kleena0861c02009-06-08 17:37:09 +0800540 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300541 if (!vcpu->fpu_active)
542 eb |= 1u << NM_VECTOR;
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100543 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
544 if (vcpu->guest_debug &
545 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
546 eb |= 1u << DB_VECTOR;
547 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
548 eb |= 1u << BP_VECTOR;
549 }
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300550 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300551 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200552 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800553 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300554 vmcs_write32(EXCEPTION_BITMAP, eb);
555}
556
Avi Kivity33ed6322007-05-02 16:54:03 +0300557static void reload_tss(void)
558{
Avi Kivity33ed6322007-05-02 16:54:03 +0300559 /*
560 * VT restores TR but not its size. Useless.
561 */
562 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200563 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300564
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300565 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300566 descs = (void *)gdt.base;
567 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
568 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300569}
570
Rusty Russell8b9cf982007-07-30 16:31:43 +1000571static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300572{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400573 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300574 u64 host_efer = vmx->host_msrs[efer_offset].data;
575 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
576 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300577
Avi Kivity51c6cf62007-08-29 03:48:05 +0300578 if (efer_offset < 0)
579 return;
580 /*
581 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
582 * outside long mode
583 */
584 ignore_bits = EFER_NX | EFER_SCE;
585#ifdef CONFIG_X86_64
586 ignore_bits |= EFER_LMA | EFER_LME;
587 /* SCE is meaningful only in long mode on Intel */
588 if (guest_efer & EFER_LMA)
589 ignore_bits &= ~(u64)EFER_SCE;
590#endif
591 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
592 return;
593
594 vmx->host_state.guest_efer_loaded = 1;
595 guest_efer &= ~ignore_bits;
596 guest_efer |= host_efer & ignore_bits;
597 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000598 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300599}
600
Avi Kivity51c6cf62007-08-29 03:48:05 +0300601static void reload_host_efer(struct vcpu_vmx *vmx)
602{
603 if (vmx->host_state.guest_efer_loaded) {
604 vmx->host_state.guest_efer_loaded = 0;
605 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
606 }
607}
608
Avi Kivity04d2cc72007-09-10 18:10:54 +0300609static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300610{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300611 struct vcpu_vmx *vmx = to_vmx(vcpu);
612
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400613 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300614 return;
615
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400616 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300617 /*
618 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
619 * allow segment selectors with cpl > 0 or ti == 1.
620 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300621 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200622 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300623 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200624 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400625 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200626 vmx->host_state.fs_reload_needed = 0;
627 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300628 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200629 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300630 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300631 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400632 if (!(vmx->host_state.gs_sel & 7))
633 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300634 else {
635 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200636 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300637 }
638
639#ifdef CONFIG_X86_64
640 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
641 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
642#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400643 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
644 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300645#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300646
647#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400648 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400649 save_msrs(vmx->host_msrs +
650 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400651
Avi Kivity707c0872007-05-02 17:33:43 +0300652#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400653 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300654 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300655}
656
Avi Kivitya9b21b62008-06-24 11:48:49 +0300657static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300658{
Avi Kivity15ad7142007-07-11 18:17:21 +0300659 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300660
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400661 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300662 return;
663
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200664 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400665 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200666 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300667 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200668 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300669 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300670 /*
671 * If we have to reload gs, we must take care to
672 * preserve our gs base.
673 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300674 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300675 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300676#ifdef CONFIG_X86_64
677 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
678#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300679 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300680 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200681 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400682 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
683 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300684 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300685}
686
Avi Kivitya9b21b62008-06-24 11:48:49 +0300687static void vmx_load_host_state(struct vcpu_vmx *vmx)
688{
689 preempt_disable();
690 __vmx_load_host_state(vmx);
691 preempt_enable();
692}
693
Avi Kivity6aa8b732006-12-10 02:21:36 -0800694/*
695 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
696 * vcpu mutex is already taken.
697 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300698static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400700 struct vcpu_vmx *vmx = to_vmx(vcpu);
701 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200702 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703
Eddie Donga3d7f852007-09-03 16:15:12 +0300704 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000705 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300706 kvm_migrate_timers(vcpu);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800707 vpid_sync_vcpu_all(vmx);
Avi Kivity543e4242008-05-13 16:22:47 +0300708 local_irq_disable();
709 list_add(&vmx->local_vcpus_link,
710 &per_cpu(vcpus_on_cpu, cpu));
711 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300712 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400714 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715 u8 error;
716
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400717 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300718 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
720 : "cc");
721 if (error)
722 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400723 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800724 }
725
726 if (vcpu->cpu != cpu) {
727 struct descriptor_table dt;
728 unsigned long sysenter_esp;
729
730 vcpu->cpu = cpu;
731 /*
732 * Linux uses per-cpu TSS and GDT, so set these when switching
733 * processors.
734 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300735 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
736 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
738
739 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
740 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300741
742 /*
743 * Make sure the time stamp counter is monotonous.
744 */
745 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200746 if (tsc_this < vcpu->arch.host_tsc) {
747 delta = vcpu->arch.host_tsc - tsc_this;
748 new_offset = vmcs_read64(TSC_OFFSET) + delta;
749 vmcs_write64(TSC_OFFSET, new_offset);
750 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752}
753
754static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
755{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300756 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757}
758
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300759static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
760{
761 if (vcpu->fpu_active)
762 return;
763 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000764 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800765 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92fa2007-07-17 23:19:08 +1000766 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300767 update_exception_bitmap(vcpu);
768}
769
770static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
771{
772 if (!vcpu->fpu_active)
773 return;
774 vcpu->fpu_active = 0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000775 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300776 update_exception_bitmap(vcpu);
777}
778
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
780{
781 return vmcs_readl(GUEST_RFLAGS);
782}
783
784static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
785{
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300786 if (to_vmx(vcpu)->rmode.vm86_active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100787 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788 vmcs_writel(GUEST_RFLAGS, rflags);
789}
790
Glauber Costa2809f5d2009-05-12 16:21:05 -0400791static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
792{
793 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
794 int ret = 0;
795
796 if (interruptibility & GUEST_INTR_STATE_STI)
797 ret |= X86_SHADOW_INT_STI;
798 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
799 ret |= X86_SHADOW_INT_MOV_SS;
800
801 return ret & mask;
802}
803
804static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
805{
806 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
807 u32 interruptibility = interruptibility_old;
808
809 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
810
811 if (mask & X86_SHADOW_INT_MOV_SS)
812 interruptibility |= GUEST_INTR_STATE_MOV_SS;
813 if (mask & X86_SHADOW_INT_STI)
814 interruptibility |= GUEST_INTR_STATE_STI;
815
816 if ((interruptibility != interruptibility_old))
817 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
818}
819
Avi Kivity6aa8b732006-12-10 02:21:36 -0800820static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
821{
822 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300824 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300826 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827
Glauber Costa2809f5d2009-05-12 16:21:05 -0400828 /* skipping an emulated instruction also counts */
829 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830}
831
Avi Kivity298101d2007-11-25 13:41:11 +0200832static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
833 bool has_error_code, u32 error_code)
834{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200835 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100836 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200837
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100838 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200839 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100840 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
841 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200842
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300843 if (vmx->rmode.vm86_active) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200844 vmx->rmode.irq.pending = true;
845 vmx->rmode.irq.vector = nr;
846 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +0300847 if (kvm_exception_is_soft(nr))
848 vmx->rmode.irq.rip +=
849 vmx->vcpu.arch.event_exit_inst_len;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100850 intr_info |= INTR_TYPE_SOFT_INTR;
851 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200852 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
853 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
854 return;
855 }
856
Gleb Natapov66fd3f72009-05-11 13:35:50 +0300857 if (kvm_exception_is_soft(nr)) {
858 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
859 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100860 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
861 } else
862 intr_info |= INTR_TYPE_HARD_EXCEPTION;
863
864 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200865}
866
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867/*
Eddie Donga75beee2007-05-17 18:55:15 +0300868 * Swap MSR entry in host/guest MSR entry array.
869 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200870#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000871static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300872{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400873 struct kvm_msr_entry tmp;
874
875 tmp = vmx->guest_msrs[to];
876 vmx->guest_msrs[to] = vmx->guest_msrs[from];
877 vmx->guest_msrs[from] = tmp;
878 tmp = vmx->host_msrs[to];
879 vmx->host_msrs[to] = vmx->host_msrs[from];
880 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300881}
Gabriel C54e11fa2007-08-01 16:23:10 +0200882#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300883
884/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300885 * Set up the vmcs to automatically save and restore system
886 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
887 * mode, as fiddling with msrs is very expensive.
888 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000889static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300890{
Eddie Dong2cc51562007-05-21 07:28:09 +0300891 int save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +0200892 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +0300893
Avi Kivity33f9c502008-02-27 16:06:57 +0200894 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300895 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300896#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000897 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300898 int index;
899
Rusty Russell8b9cf982007-07-30 16:31:43 +1000900 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300901 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000902 move_msr_up(vmx, index, save_nmsrs++);
903 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300904 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000905 move_msr_up(vmx, index, save_nmsrs++);
906 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300907 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000908 move_msr_up(vmx, index, save_nmsrs++);
909 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300910 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000911 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300912 /*
913 * MSR_K6_STAR is only needed on long mode guests, and only
914 * if efer.sce is enabled.
915 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000916 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800917 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000918 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300919 }
Eddie Donga75beee2007-05-17 18:55:15 +0300920#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400921 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300922
Eddie Donga75beee2007-05-17 18:55:15 +0300923#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400924 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000925 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300926#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000927 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivity58972972009-02-24 22:26:47 +0200928
929 if (cpu_has_vmx_msr_bitmap()) {
930 if (is_long_mode(&vmx->vcpu))
931 msr_bitmap = vmx_msr_bitmap_longmode;
932 else
933 msr_bitmap = vmx_msr_bitmap_legacy;
934
935 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
936 }
Avi Kivitye38aea32007-04-19 13:22:48 +0300937}
938
939/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 * reads and returns guest's timestamp counter "register"
941 * guest_tsc = host_tsc + tsc_offset -- 21.3
942 */
943static u64 guest_read_tsc(void)
944{
945 u64 host_tsc, tsc_offset;
946
947 rdtscll(host_tsc);
948 tsc_offset = vmcs_read64(TSC_OFFSET);
949 return host_tsc + tsc_offset;
950}
951
952/*
953 * writes 'guest_tsc' into guest's timestamp counter "register"
954 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
955 */
Marcelo Tosatti53f658b32008-12-11 20:45:05 +0100956static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
959}
960
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961/*
962 * Reads an msr value (of 'msr_index') into 'pdata'.
963 * Returns 0 on success, non-0 otherwise.
964 * Assumes vcpu_load() was already called.
965 */
966static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
967{
968 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400969 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970
971 if (!pdata) {
972 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
973 return -EINVAL;
974 }
975
976 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800977#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978 case MSR_FS_BASE:
979 data = vmcs_readl(GUEST_FS_BASE);
980 break;
981 case MSR_GS_BASE:
982 data = vmcs_readl(GUEST_GS_BASE);
983 break;
984 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800985 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986#endif
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530987 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 data = guest_read_tsc();
989 break;
990 case MSR_IA32_SYSENTER_CS:
991 data = vmcs_read32(GUEST_SYSENTER_CS);
992 break;
993 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200994 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 break;
996 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200997 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800999 default:
Avi Kivity516a1a72009-02-15 02:32:07 +02001000 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +10001001 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001002 if (msr) {
1003 data = msr->data;
1004 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001006 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007 }
1008
1009 *pdata = data;
1010 return 0;
1011}
1012
1013/*
1014 * Writes msr value into into the appropriate "register".
1015 * Returns 0 on success, non-0 otherwise.
1016 * Assumes vcpu_load() was already called.
1017 */
1018static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1019{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001020 struct vcpu_vmx *vmx = to_vmx(vcpu);
1021 struct kvm_msr_entry *msr;
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01001022 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +03001023 int ret = 0;
1024
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001026 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001027 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001028 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001029 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001030#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031 case MSR_FS_BASE:
1032 vmcs_writel(GUEST_FS_BASE, data);
1033 break;
1034 case MSR_GS_BASE:
1035 vmcs_writel(GUEST_GS_BASE, data);
1036 break;
1037#endif
1038 case MSR_IA32_SYSENTER_CS:
1039 vmcs_write32(GUEST_SYSENTER_CS, data);
1040 break;
1041 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001042 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043 break;
1044 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001045 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301047 case MSR_IA32_TSC:
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01001048 rdtscll(host_tsc);
1049 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001051 case MSR_IA32_CR_PAT:
1052 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1053 vmcs_write64(GUEST_IA32_PAT, data);
1054 vcpu->arch.pat = data;
1055 break;
1056 }
1057 /* Otherwise falls through to kvm_set_msr_common */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058 default:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001059 vmx_load_host_state(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +10001060 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001061 if (msr) {
1062 msr->data = data;
1063 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001065 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066 }
1067
Eddie Dong2cc51562007-05-21 07:28:09 +03001068 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069}
1070
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001071static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001073 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1074 switch (reg) {
1075 case VCPU_REGS_RSP:
1076 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1077 break;
1078 case VCPU_REGS_RIP:
1079 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1080 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001081 case VCPU_EXREG_PDPTR:
1082 if (enable_ept)
1083 ept_save_pdptrs(vcpu);
1084 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001085 default:
1086 break;
1087 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088}
1089
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001090static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001092 int old_debug = vcpu->guest_debug;
1093 unsigned long flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001094
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001095 vcpu->guest_debug = dbg->control;
1096 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
1097 vcpu->guest_debug = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001099 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1100 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1101 else
1102 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1103
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001104 flags = vmcs_readl(GUEST_RFLAGS);
1105 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
1106 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1107 else if (old_debug & KVM_GUESTDBG_SINGLESTEP)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001109 vmcs_writel(GUEST_RFLAGS, flags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001111 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112
1113 return 0;
1114}
1115
1116static __init int cpu_has_kvm_support(void)
1117{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001118 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119}
1120
1121static __init int vmx_disabled_by_bios(void)
1122{
1123 u64 msr;
1124
1125 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001126 return (msr & (FEATURE_CONTROL_LOCKED |
1127 FEATURE_CONTROL_VMXON_ENABLED))
1128 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001129 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130}
1131
Avi Kivity774c47f2007-02-12 00:54:47 -08001132static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133{
1134 int cpu = raw_smp_processor_id();
1135 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1136 u64 old;
1137
Avi Kivity543e4242008-05-13 16:22:47 +03001138 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001140 if ((old & (FEATURE_CONTROL_LOCKED |
1141 FEATURE_CONTROL_VMXON_ENABLED))
1142 != (FEATURE_CONTROL_LOCKED |
1143 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001145 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001146 FEATURE_CONTROL_LOCKED |
1147 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001148 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001149 asm volatile (ASM_VMX_VMXON_RAX
1150 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151 : "memory", "cc");
1152}
1153
Avi Kivity543e4242008-05-13 16:22:47 +03001154static void vmclear_local_vcpus(void)
1155{
1156 int cpu = raw_smp_processor_id();
1157 struct vcpu_vmx *vmx, *n;
1158
1159 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1160 local_vcpus_link)
1161 __vcpu_clear(vmx);
1162}
1163
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001164
1165/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1166 * tricks.
1167 */
1168static void kvm_cpu_vmxoff(void)
1169{
1170 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1171 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1172}
1173
Avi Kivity6aa8b732006-12-10 02:21:36 -08001174static void hardware_disable(void *garbage)
1175{
Avi Kivity543e4242008-05-13 16:22:47 +03001176 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001177 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178}
1179
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001180static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001181 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001182{
1183 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001184 u32 ctl = ctl_min | ctl_opt;
1185
1186 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1187
1188 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1189 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1190
1191 /* Ensure minimum (required) set of control bits are supported. */
1192 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001193 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001194
1195 *result = ctl;
1196 return 0;
1197}
1198
Yang, Sheng002c7f72007-07-31 14:23:01 +03001199static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001200{
1201 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001202 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001203 u32 _pin_based_exec_control = 0;
1204 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001205 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001206 u32 _vmexit_control = 0;
1207 u32 _vmentry_control = 0;
1208
1209 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001210 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001211 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1212 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001213 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001214
1215 min = CPU_BASED_HLT_EXITING |
1216#ifdef CONFIG_X86_64
1217 CPU_BASED_CR8_LOAD_EXITING |
1218 CPU_BASED_CR8_STORE_EXITING |
1219#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001220 CPU_BASED_CR3_LOAD_EXITING |
1221 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001222 CPU_BASED_USE_IO_BITMAPS |
1223 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001224 CPU_BASED_USE_TSC_OFFSETING |
1225 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001226 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001227 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001228 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001229 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1230 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001231 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001232#ifdef CONFIG_X86_64
1233 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1234 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1235 ~CPU_BASED_CR8_STORE_EXITING;
1236#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001237 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001238 min2 = 0;
1239 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001240 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001241 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001242 SECONDARY_EXEC_ENABLE_EPT |
1243 SECONDARY_EXEC_UNRESTRICTED_GUEST;
Sheng Yangd56f5462008-04-25 10:13:16 +08001244 if (adjust_vmx_controls(min2, opt2,
1245 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001246 &_cpu_based_2nd_exec_control) < 0)
1247 return -EIO;
1248 }
1249#ifndef CONFIG_X86_64
1250 if (!(_cpu_based_2nd_exec_control &
1251 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1252 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1253#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001254 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001255 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1256 enabled */
Sheng Yangd56f5462008-04-25 10:13:16 +08001257 min &= ~(CPU_BASED_CR3_LOAD_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001258 CPU_BASED_CR3_STORE_EXITING |
1259 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001260 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1261 &_cpu_based_exec_control) < 0)
1262 return -EIO;
1263 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1264 vmx_capability.ept, vmx_capability.vpid);
1265 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001266
1267 min = 0;
1268#ifdef CONFIG_X86_64
1269 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1270#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001271 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001272 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1273 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001274 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001275
Sheng Yang468d4722008-10-09 16:01:55 +08001276 min = 0;
1277 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001278 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1279 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001280 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001281
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001282 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001283
1284 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1285 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001286 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001287
1288#ifdef CONFIG_X86_64
1289 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1290 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001291 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001292#endif
1293
1294 /* Require Write-Back (WB) memory type for VMCS accesses. */
1295 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001296 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001297
Yang, Sheng002c7f72007-07-31 14:23:01 +03001298 vmcs_conf->size = vmx_msr_high & 0x1fff;
1299 vmcs_conf->order = get_order(vmcs_config.size);
1300 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001301
Yang, Sheng002c7f72007-07-31 14:23:01 +03001302 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1303 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001304 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001305 vmcs_conf->vmexit_ctrl = _vmexit_control;
1306 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001307
1308 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001309}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310
1311static struct vmcs *alloc_vmcs_cpu(int cpu)
1312{
1313 int node = cpu_to_node(cpu);
1314 struct page *pages;
1315 struct vmcs *vmcs;
1316
Mel Gorman6484eb32009-06-16 15:31:54 -07001317 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318 if (!pages)
1319 return NULL;
1320 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001321 memset(vmcs, 0, vmcs_config.size);
1322 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323 return vmcs;
1324}
1325
1326static struct vmcs *alloc_vmcs(void)
1327{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001328 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329}
1330
1331static void free_vmcs(struct vmcs *vmcs)
1332{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001333 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001334}
1335
Sam Ravnborg39959582007-06-01 00:47:13 -07001336static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337{
1338 int cpu;
1339
1340 for_each_online_cpu(cpu)
1341 free_vmcs(per_cpu(vmxarea, cpu));
1342}
1343
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344static __init int alloc_kvm_area(void)
1345{
1346 int cpu;
1347
1348 for_each_online_cpu(cpu) {
1349 struct vmcs *vmcs;
1350
1351 vmcs = alloc_vmcs_cpu(cpu);
1352 if (!vmcs) {
1353 free_kvm_area();
1354 return -ENOMEM;
1355 }
1356
1357 per_cpu(vmxarea, cpu) = vmcs;
1358 }
1359 return 0;
1360}
1361
1362static __init int hardware_setup(void)
1363{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001364 if (setup_vmcs_config(&vmcs_config) < 0)
1365 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001366
1367 if (boot_cpu_has(X86_FEATURE_NX))
1368 kvm_enable_efer_bits(EFER_NX);
1369
Sheng Yang93ba03c2009-04-01 15:52:32 +08001370 if (!cpu_has_vmx_vpid())
1371 enable_vpid = 0;
1372
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001373 if (!cpu_has_vmx_ept()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001374 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001375 enable_unrestricted_guest = 0;
1376 }
1377
1378 if (!cpu_has_vmx_unrestricted_guest())
1379 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001380
1381 if (!cpu_has_vmx_flexpriority())
1382 flexpriority_enabled = 0;
1383
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001384 if (!cpu_has_vmx_tpr_shadow())
1385 kvm_x86_ops->update_cr8_intercept = NULL;
1386
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001387 if (enable_ept && !cpu_has_vmx_ept_2m_page())
1388 kvm_disable_largepages();
1389
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390 return alloc_kvm_area();
1391}
1392
1393static __exit void hardware_unsetup(void)
1394{
1395 free_kvm_area();
1396}
1397
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1399{
1400 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1401
Avi Kivity6af11b92007-03-19 13:18:10 +02001402 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001403 vmcs_write16(sf->selector, save->selector);
1404 vmcs_writel(sf->base, save->base);
1405 vmcs_write32(sf->limit, save->limit);
1406 vmcs_write32(sf->ar_bytes, save->ar);
1407 } else {
1408 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1409 << AR_DPL_SHIFT;
1410 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1411 }
1412}
1413
1414static void enter_pmode(struct kvm_vcpu *vcpu)
1415{
1416 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001417 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001419 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001420 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001422 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
1423 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
1424 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425
1426 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001427 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001428 flags |= (vmx->rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429 vmcs_writel(GUEST_RFLAGS, flags);
1430
Rusty Russell66aee912007-07-17 23:34:16 +10001431 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1432 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001433
1434 update_exception_bitmap(vcpu);
1435
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001436 if (emulate_invalid_guest_state)
1437 return;
1438
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001439 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
1440 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
1441 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
1442 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001443
1444 vmcs_write16(GUEST_SS_SELECTOR, 0);
1445 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1446
1447 vmcs_write16(GUEST_CS_SELECTOR,
1448 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1449 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1450}
1451
Mike Dayd77c26f2007-10-08 09:02:08 -04001452static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001454 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001455 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1456 kvm->memslots[0].npages - 3;
1457 return base_gfn << PAGE_SHIFT;
1458 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001459 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001460}
1461
1462static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1463{
1464 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1465
1466 save->selector = vmcs_read16(sf->selector);
1467 save->base = vmcs_readl(sf->base);
1468 save->limit = vmcs_read32(sf->limit);
1469 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001470 vmcs_write16(sf->selector, save->base >> 4);
1471 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472 vmcs_write32(sf->limit, 0xffff);
1473 vmcs_write32(sf->ar_bytes, 0xf3);
1474}
1475
1476static void enter_rmode(struct kvm_vcpu *vcpu)
1477{
1478 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001479 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001481 if (enable_unrestricted_guest)
1482 return;
1483
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001484 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001485 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001487 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1489
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001490 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1492
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001493 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1495
1496 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001497 vmx->rmode.save_iopl
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001498 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001500 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501
1502 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001503 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504 update_exception_bitmap(vcpu);
1505
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001506 if (emulate_invalid_guest_state)
1507 goto continue_rmode;
1508
Avi Kivity6aa8b732006-12-10 02:21:36 -08001509 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1510 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1511 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1512
1513 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001514 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001515 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1516 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1518
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001519 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
1520 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
1521 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
1522 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001523
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001524continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001525 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001526 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527}
1528
Amit Shah401d10d2009-02-20 22:53:37 +05301529static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1530{
1531 struct vcpu_vmx *vmx = to_vmx(vcpu);
1532 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1533
1534 vcpu->arch.shadow_efer = efer;
1535 if (!msr)
1536 return;
1537 if (efer & EFER_LMA) {
1538 vmcs_write32(VM_ENTRY_CONTROLS,
1539 vmcs_read32(VM_ENTRY_CONTROLS) |
1540 VM_ENTRY_IA32E_MODE);
1541 msr->data = efer;
1542 } else {
1543 vmcs_write32(VM_ENTRY_CONTROLS,
1544 vmcs_read32(VM_ENTRY_CONTROLS) &
1545 ~VM_ENTRY_IA32E_MODE);
1546
1547 msr->data = efer & ~EFER_LME;
1548 }
1549 setup_msrs(vmx);
1550}
1551
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001552#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001553
1554static void enter_lmode(struct kvm_vcpu *vcpu)
1555{
1556 u32 guest_tr_ar;
1557
1558 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1559 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1560 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001561 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001562 vmcs_write32(GUEST_TR_AR_BYTES,
1563 (guest_tr_ar & ~AR_TYPE_MASK)
1564 | AR_TYPE_BUSY_64_TSS);
1565 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001566 vcpu->arch.shadow_efer |= EFER_LMA;
Amit Shah401d10d2009-02-20 22:53:37 +05301567 vmx_set_efer(vcpu, vcpu->arch.shadow_efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001568}
1569
1570static void exit_lmode(struct kvm_vcpu *vcpu)
1571{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001572 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001573
1574 vmcs_write32(VM_ENTRY_CONTROLS,
1575 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001576 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001577}
1578
1579#endif
1580
Sheng Yang2384d2b2008-01-17 15:14:33 +08001581static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1582{
1583 vpid_sync_vcpu_all(to_vmx(vcpu));
Avi Kivity089d0342009-03-23 18:26:32 +02001584 if (enable_ept)
Sheng Yang4e1096d2008-07-06 19:16:51 +08001585 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001586}
1587
Anthony Liguori25c4c272007-04-27 09:29:21 +03001588static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001589{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001590 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1591 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001592}
1593
Sheng Yang14394422008-04-28 12:24:45 +08001594static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1595{
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001596 if (!test_bit(VCPU_EXREG_PDPTR,
1597 (unsigned long *)&vcpu->arch.regs_dirty))
1598 return;
1599
Sheng Yang14394422008-04-28 12:24:45 +08001600 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Sheng Yang14394422008-04-28 12:24:45 +08001601 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1602 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1603 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1604 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1605 }
1606}
1607
Avi Kivity8f5d5492009-05-31 18:41:29 +03001608static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
1609{
1610 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1611 vcpu->arch.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
1612 vcpu->arch.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
1613 vcpu->arch.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
1614 vcpu->arch.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
1615 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001616
1617 __set_bit(VCPU_EXREG_PDPTR,
1618 (unsigned long *)&vcpu->arch.regs_avail);
1619 __set_bit(VCPU_EXREG_PDPTR,
1620 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03001621}
1622
Sheng Yang14394422008-04-28 12:24:45 +08001623static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1624
1625static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1626 unsigned long cr0,
1627 struct kvm_vcpu *vcpu)
1628{
1629 if (!(cr0 & X86_CR0_PG)) {
1630 /* From paging/starting to nonpaging */
1631 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001632 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001633 (CPU_BASED_CR3_LOAD_EXITING |
1634 CPU_BASED_CR3_STORE_EXITING));
1635 vcpu->arch.cr0 = cr0;
1636 vmx_set_cr4(vcpu, vcpu->arch.cr4);
Sheng Yang14394422008-04-28 12:24:45 +08001637 *hw_cr0 &= ~X86_CR0_WP;
1638 } else if (!is_paging(vcpu)) {
1639 /* From nonpaging to paging */
1640 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001641 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001642 ~(CPU_BASED_CR3_LOAD_EXITING |
1643 CPU_BASED_CR3_STORE_EXITING));
1644 vcpu->arch.cr0 = cr0;
1645 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1646 if (!(vcpu->arch.cr0 & X86_CR0_WP))
1647 *hw_cr0 &= ~X86_CR0_WP;
1648 }
1649}
1650
1651static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1652 struct kvm_vcpu *vcpu)
1653{
1654 if (!is_paging(vcpu)) {
1655 *hw_cr4 &= ~X86_CR4_PAE;
1656 *hw_cr4 |= X86_CR4_PSE;
1657 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1658 *hw_cr4 &= ~X86_CR4_PAE;
1659}
1660
Avi Kivity6aa8b732006-12-10 02:21:36 -08001661static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1662{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001663 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001664 unsigned long hw_cr0;
1665
1666 if (enable_unrestricted_guest)
1667 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
1668 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
1669 else
1670 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08001671
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001672 vmx_fpu_deactivate(vcpu);
1673
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001674 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001675 enter_pmode(vcpu);
1676
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001677 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678 enter_rmode(vcpu);
1679
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001680#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001681 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001682 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001683 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001684 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001685 exit_lmode(vcpu);
1686 }
1687#endif
1688
Avi Kivity089d0342009-03-23 18:26:32 +02001689 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001690 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1691
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001693 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001694 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001695
Rusty Russell707d92fa2007-07-17 23:19:08 +10001696 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001697 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001698}
1699
Sheng Yang14394422008-04-28 12:24:45 +08001700static u64 construct_eptp(unsigned long root_hpa)
1701{
1702 u64 eptp;
1703
1704 /* TODO write the value reading from MSR */
1705 eptp = VMX_EPT_DEFAULT_MT |
1706 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1707 eptp |= (root_hpa & PAGE_MASK);
1708
1709 return eptp;
1710}
1711
Avi Kivity6aa8b732006-12-10 02:21:36 -08001712static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1713{
Sheng Yang14394422008-04-28 12:24:45 +08001714 unsigned long guest_cr3;
1715 u64 eptp;
1716
1717 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02001718 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001719 eptp = construct_eptp(cr3);
1720 vmcs_write64(EPT_POINTER, eptp);
Sheng Yang14394422008-04-28 12:24:45 +08001721 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
1722 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1723 }
1724
Sheng Yang2384d2b2008-01-17 15:14:33 +08001725 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001726 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001727 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001728 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729}
1730
1731static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1732{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001733 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08001734 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1735
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001736 vcpu->arch.cr4 = cr4;
Avi Kivity089d0342009-03-23 18:26:32 +02001737 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001738 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1739
1740 vmcs_writel(CR4_READ_SHADOW, cr4);
1741 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001742}
1743
Avi Kivity6aa8b732006-12-10 02:21:36 -08001744static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1745{
1746 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1747
1748 return vmcs_readl(sf->base);
1749}
1750
1751static void vmx_get_segment(struct kvm_vcpu *vcpu,
1752 struct kvm_segment *var, int seg)
1753{
1754 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1755 u32 ar;
1756
1757 var->base = vmcs_readl(sf->base);
1758 var->limit = vmcs_read32(sf->limit);
1759 var->selector = vmcs_read16(sf->selector);
1760 ar = vmcs_read32(sf->ar_bytes);
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02001761 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001762 ar = 0;
1763 var->type = ar & 15;
1764 var->s = (ar >> 4) & 1;
1765 var->dpl = (ar >> 5) & 3;
1766 var->present = (ar >> 7) & 1;
1767 var->avl = (ar >> 12) & 1;
1768 var->l = (ar >> 13) & 1;
1769 var->db = (ar >> 14) & 1;
1770 var->g = (ar >> 15) & 1;
1771 var->unusable = (ar >> 16) & 1;
1772}
1773
Izik Eidus2e4d2652008-03-24 19:38:34 +02001774static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1775{
1776 struct kvm_segment kvm_seg;
1777
1778 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1779 return 0;
1780
1781 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1782 return 3;
1783
1784 vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
1785 return kvm_seg.selector & 3;
1786}
1787
Avi Kivity653e3102007-05-07 10:55:37 +03001788static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001789{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790 u32 ar;
1791
Avi Kivity653e3102007-05-07 10:55:37 +03001792 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001793 ar = 1 << 16;
1794 else {
1795 ar = var->type & 15;
1796 ar |= (var->s & 1) << 4;
1797 ar |= (var->dpl & 3) << 5;
1798 ar |= (var->present & 1) << 7;
1799 ar |= (var->avl & 1) << 12;
1800 ar |= (var->l & 1) << 13;
1801 ar |= (var->db & 1) << 14;
1802 ar |= (var->g & 1) << 15;
1803 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001804 if (ar == 0) /* a 0 value means unusable */
1805 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001806
1807 return ar;
1808}
1809
1810static void vmx_set_segment(struct kvm_vcpu *vcpu,
1811 struct kvm_segment *var, int seg)
1812{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001813 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03001814 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1815 u32 ar;
1816
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001817 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
1818 vmx->rmode.tr.selector = var->selector;
1819 vmx->rmode.tr.base = var->base;
1820 vmx->rmode.tr.limit = var->limit;
1821 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001822 return;
1823 }
1824 vmcs_writel(sf->base, var->base);
1825 vmcs_write32(sf->limit, var->limit);
1826 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001827 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001828 /*
1829 * Hack real-mode segments into vm86 compatibility.
1830 */
1831 if (var->base == 0xffff0000 && var->selector == 0xf000)
1832 vmcs_writel(sf->base, 0xf0000);
1833 ar = 0xf3;
1834 } else
1835 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001836
1837 /*
1838 * Fix the "Accessed" bit in AR field of segment registers for older
1839 * qemu binaries.
1840 * IA32 arch specifies that at the time of processor reset the
1841 * "Accessed" bit in the AR field of segment registers is 1. And qemu
1842 * is setting it to 0 in the usedland code. This causes invalid guest
1843 * state vmexit when "unrestricted guest" mode is turned on.
1844 * Fix for this setup issue in cpu_reset is being pushed in the qemu
1845 * tree. Newer qemu binaries with that qemu fix would not need this
1846 * kvm hack.
1847 */
1848 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
1849 ar |= 0x1; /* Accessed */
1850
Avi Kivity6aa8b732006-12-10 02:21:36 -08001851 vmcs_write32(sf->ar_bytes, ar);
1852}
1853
Avi Kivity6aa8b732006-12-10 02:21:36 -08001854static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1855{
1856 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1857
1858 *db = (ar >> 14) & 1;
1859 *l = (ar >> 13) & 1;
1860}
1861
1862static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1863{
1864 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1865 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1866}
1867
1868static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1869{
1870 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1871 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1872}
1873
1874static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1875{
1876 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1877 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1878}
1879
1880static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1881{
1882 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1883 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1884}
1885
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001886static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1887{
1888 struct kvm_segment var;
1889 u32 ar;
1890
1891 vmx_get_segment(vcpu, &var, seg);
1892 ar = vmx_segment_access_rights(&var);
1893
1894 if (var.base != (var.selector << 4))
1895 return false;
1896 if (var.limit != 0xffff)
1897 return false;
1898 if (ar != 0xf3)
1899 return false;
1900
1901 return true;
1902}
1903
1904static bool code_segment_valid(struct kvm_vcpu *vcpu)
1905{
1906 struct kvm_segment cs;
1907 unsigned int cs_rpl;
1908
1909 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1910 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1911
Avi Kivity1872a3f2009-01-04 23:26:52 +02001912 if (cs.unusable)
1913 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001914 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1915 return false;
1916 if (!cs.s)
1917 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001918 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001919 if (cs.dpl > cs_rpl)
1920 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001921 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001922 if (cs.dpl != cs_rpl)
1923 return false;
1924 }
1925 if (!cs.present)
1926 return false;
1927
1928 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1929 return true;
1930}
1931
1932static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1933{
1934 struct kvm_segment ss;
1935 unsigned int ss_rpl;
1936
1937 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1938 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1939
Avi Kivity1872a3f2009-01-04 23:26:52 +02001940 if (ss.unusable)
1941 return true;
1942 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001943 return false;
1944 if (!ss.s)
1945 return false;
1946 if (ss.dpl != ss_rpl) /* DPL != RPL */
1947 return false;
1948 if (!ss.present)
1949 return false;
1950
1951 return true;
1952}
1953
1954static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1955{
1956 struct kvm_segment var;
1957 unsigned int rpl;
1958
1959 vmx_get_segment(vcpu, &var, seg);
1960 rpl = var.selector & SELECTOR_RPL_MASK;
1961
Avi Kivity1872a3f2009-01-04 23:26:52 +02001962 if (var.unusable)
1963 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001964 if (!var.s)
1965 return false;
1966 if (!var.present)
1967 return false;
1968 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
1969 if (var.dpl < rpl) /* DPL < RPL */
1970 return false;
1971 }
1972
1973 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1974 * rights flags
1975 */
1976 return true;
1977}
1978
1979static bool tr_valid(struct kvm_vcpu *vcpu)
1980{
1981 struct kvm_segment tr;
1982
1983 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
1984
Avi Kivity1872a3f2009-01-04 23:26:52 +02001985 if (tr.unusable)
1986 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001987 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1988 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001989 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001990 return false;
1991 if (!tr.present)
1992 return false;
1993
1994 return true;
1995}
1996
1997static bool ldtr_valid(struct kvm_vcpu *vcpu)
1998{
1999 struct kvm_segment ldtr;
2000
2001 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2002
Avi Kivity1872a3f2009-01-04 23:26:52 +02002003 if (ldtr.unusable)
2004 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002005 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2006 return false;
2007 if (ldtr.type != 2)
2008 return false;
2009 if (!ldtr.present)
2010 return false;
2011
2012 return true;
2013}
2014
2015static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2016{
2017 struct kvm_segment cs, ss;
2018
2019 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2020 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2021
2022 return ((cs.selector & SELECTOR_RPL_MASK) ==
2023 (ss.selector & SELECTOR_RPL_MASK));
2024}
2025
2026/*
2027 * Check if guest state is valid. Returns true if valid, false if
2028 * not.
2029 * We assume that registers are always usable
2030 */
2031static bool guest_state_valid(struct kvm_vcpu *vcpu)
2032{
2033 /* real mode guest state checks */
2034 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
2035 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2036 return false;
2037 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2038 return false;
2039 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2040 return false;
2041 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2042 return false;
2043 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2044 return false;
2045 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2046 return false;
2047 } else {
2048 /* protected mode guest state checks */
2049 if (!cs_ss_rpl_check(vcpu))
2050 return false;
2051 if (!code_segment_valid(vcpu))
2052 return false;
2053 if (!stack_segment_valid(vcpu))
2054 return false;
2055 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2056 return false;
2057 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2058 return false;
2059 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2060 return false;
2061 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2062 return false;
2063 if (!tr_valid(vcpu))
2064 return false;
2065 if (!ldtr_valid(vcpu))
2066 return false;
2067 }
2068 /* TODO:
2069 * - Add checks on RIP
2070 * - Add checks on RFLAGS
2071 */
2072
2073 return true;
2074}
2075
Mike Dayd77c26f2007-10-08 09:02:08 -04002076static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002077{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002079 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002080 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02002081 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002082
Izik Eidus195aefd2007-10-01 22:14:18 +02002083 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2084 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002085 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002086 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002087 r = kvm_write_guest_page(kvm, fn++, &data,
2088 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002089 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002090 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002091 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2092 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002093 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002094 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2095 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002096 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002097 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002098 r = kvm_write_guest_page(kvm, fn, &data,
2099 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2100 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002101 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002102 goto out;
2103
2104 ret = 1;
2105out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002106 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107}
2108
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002109static int init_rmode_identity_map(struct kvm *kvm)
2110{
2111 int i, r, ret;
2112 pfn_t identity_map_pfn;
2113 u32 tmp;
2114
Avi Kivity089d0342009-03-23 18:26:32 +02002115 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002116 return 1;
2117 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2118 printk(KERN_ERR "EPT: identity-mapping pagetable "
2119 "haven't been allocated!\n");
2120 return 0;
2121 }
2122 if (likely(kvm->arch.ept_identity_pagetable_done))
2123 return 1;
2124 ret = 0;
2125 identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT;
2126 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2127 if (r < 0)
2128 goto out;
2129 /* Set up identity-mapping pagetable for EPT in real mode */
2130 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2131 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2132 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2133 r = kvm_write_guest_page(kvm, identity_map_pfn,
2134 &tmp, i * sizeof(tmp), sizeof(tmp));
2135 if (r < 0)
2136 goto out;
2137 }
2138 kvm->arch.ept_identity_pagetable_done = true;
2139 ret = 1;
2140out:
2141 return ret;
2142}
2143
Avi Kivity6aa8b732006-12-10 02:21:36 -08002144static void seg_setup(int seg)
2145{
2146 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002147 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148
2149 vmcs_write16(sf->selector, 0);
2150 vmcs_writel(sf->base, 0);
2151 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002152 if (enable_unrestricted_guest) {
2153 ar = 0x93;
2154 if (seg == VCPU_SREG_CS)
2155 ar |= 0x08; /* code segment */
2156 } else
2157 ar = 0xf3;
2158
2159 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002160}
2161
Sheng Yangf78e0e22007-10-29 09:40:42 +08002162static int alloc_apic_access_page(struct kvm *kvm)
2163{
2164 struct kvm_userspace_memory_region kvm_userspace_mem;
2165 int r = 0;
2166
Izik Eidus72dc67a2008-02-10 18:04:15 +02002167 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002168 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002169 goto out;
2170 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2171 kvm_userspace_mem.flags = 0;
2172 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2173 kvm_userspace_mem.memory_size = PAGE_SIZE;
2174 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2175 if (r)
2176 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002177
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002178 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002179out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002180 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002181 return r;
2182}
2183
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002184static int alloc_identity_pagetable(struct kvm *kvm)
2185{
2186 struct kvm_userspace_memory_region kvm_userspace_mem;
2187 int r = 0;
2188
2189 down_write(&kvm->slots_lock);
2190 if (kvm->arch.ept_identity_pagetable)
2191 goto out;
2192 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2193 kvm_userspace_mem.flags = 0;
2194 kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
2195 kvm_userspace_mem.memory_size = PAGE_SIZE;
2196 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2197 if (r)
2198 goto out;
2199
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002200 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
2201 VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002202out:
2203 up_write(&kvm->slots_lock);
2204 return r;
2205}
2206
Sheng Yang2384d2b2008-01-17 15:14:33 +08002207static void allocate_vpid(struct vcpu_vmx *vmx)
2208{
2209 int vpid;
2210
2211 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002212 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002213 return;
2214 spin_lock(&vmx_vpid_lock);
2215 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2216 if (vpid < VMX_NR_VPIDS) {
2217 vmx->vpid = vpid;
2218 __set_bit(vpid, vmx_vpid_bitmap);
2219 }
2220 spin_unlock(&vmx_vpid_lock);
2221}
2222
Avi Kivity58972972009-02-24 22:26:47 +02002223static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002224{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002225 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002226
2227 if (!cpu_has_vmx_msr_bitmap())
2228 return;
2229
2230 /*
2231 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2232 * have the write-low and read-high bitmap offsets the wrong way round.
2233 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2234 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002235 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002236 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2237 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002238 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2239 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002240 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2241 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002242 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002243}
2244
Avi Kivity58972972009-02-24 22:26:47 +02002245static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2246{
2247 if (!longmode_only)
2248 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2249 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2250}
2251
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252/*
2253 * Sets up the vmcs for emulated real mode.
2254 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002255static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256{
Sheng Yang468d4722008-10-09 16:01:55 +08002257 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002258 u32 junk;
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01002259 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260 unsigned long a;
2261 struct descriptor_table dt;
2262 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002263 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002264 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002267 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2268 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269
Sheng Yang25c5f222008-03-28 13:18:56 +08002270 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002271 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002272
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2274
Avi Kivity6aa8b732006-12-10 02:21:36 -08002275 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002276 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2277 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002278
2279 exec_control = vmcs_config.cpu_based_exec_ctrl;
2280 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2281 exec_control &= ~CPU_BASED_TPR_SHADOW;
2282#ifdef CONFIG_X86_64
2283 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2284 CPU_BASED_CR8_LOAD_EXITING;
2285#endif
2286 }
Avi Kivity089d0342009-03-23 18:26:32 +02002287 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002288 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002289 CPU_BASED_CR3_LOAD_EXITING |
2290 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002291 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002292
Sheng Yang83ff3b92007-11-21 14:33:25 +08002293 if (cpu_has_secondary_exec_ctrls()) {
2294 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2295 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2296 exec_control &=
2297 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002298 if (vmx->vpid == 0)
2299 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Avi Kivity089d0342009-03-23 18:26:32 +02002300 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002301 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002302 if (!enable_unrestricted_guest)
2303 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002304 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2305 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002306
Avi Kivityc7addb92007-09-16 18:58:32 +02002307 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2308 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2310
2311 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2312 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2313 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2314
2315 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2316 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2317 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002318 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2319 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002320 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002321#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322 rdmsrl(MSR_FS_BASE, a);
2323 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2324 rdmsrl(MSR_GS_BASE, a);
2325 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2326#else
2327 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2328 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2329#endif
2330
2331 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2332
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002333 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002334 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2335
Mike Dayd77c26f2007-10-08 09:02:08 -04002336 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002337 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002338 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2339 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2340 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341
2342 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2343 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2344 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2345 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2346 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2347 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2348
Sheng Yang468d4722008-10-09 16:01:55 +08002349 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2350 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2351 host_pat = msr_low | ((u64) msr_high << 32);
2352 vmcs_write64(HOST_IA32_PAT, host_pat);
2353 }
2354 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2355 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2356 host_pat = msr_low | ((u64) msr_high << 32);
2357 /* Write the default value follow host pat */
2358 vmcs_write64(GUEST_IA32_PAT, host_pat);
2359 /* Keep arch.pat sync with GUEST_IA32_PAT */
2360 vmx->vcpu.arch.pat = host_pat;
2361 }
2362
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363 for (i = 0; i < NR_VMX_MSR; ++i) {
2364 u32 index = vmx_msr_index[i];
2365 u32 data_low, data_high;
2366 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002367 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368
2369 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2370 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002371 if (wrmsr_safe(index, data_low, data_high) < 0)
2372 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002374 vmx->host_msrs[j].index = index;
2375 vmx->host_msrs[j].reserved = 0;
2376 vmx->host_msrs[j].data = data;
2377 vmx->guest_msrs[j] = vmx->host_msrs[j];
2378 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002381 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002382
2383 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002384 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2385
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002386 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2387 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2388
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01002389 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2390 rdtscll(tsc_this);
2391 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2392 tsc_base = tsc_this;
2393
2394 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002395
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002396 return 0;
2397}
2398
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002399static int init_rmode(struct kvm *kvm)
2400{
2401 if (!init_rmode_tss(kvm))
2402 return 0;
2403 if (!init_rmode_identity_map(kvm))
2404 return 0;
2405 return 1;
2406}
2407
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002408static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2409{
2410 struct vcpu_vmx *vmx = to_vmx(vcpu);
2411 u64 msr;
2412 int ret;
2413
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002414 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002415 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002416 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002417 ret = -ENOMEM;
2418 goto out;
2419 }
2420
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002421 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002422
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002423 vmx->soft_vnmi_blocked = 0;
2424
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002425 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002426 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002427 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002428 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002429 msr |= MSR_IA32_APICBASE_BSP;
2430 kvm_set_apic_base(&vmx->vcpu, msr);
2431
2432 fx_init(&vmx->vcpu);
2433
Avi Kivity5706be02008-08-20 15:07:31 +03002434 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002435 /*
2436 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2437 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2438 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002439 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002440 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2441 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2442 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002443 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2444 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002445 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002446
2447 seg_setup(VCPU_SREG_DS);
2448 seg_setup(VCPU_SREG_ES);
2449 seg_setup(VCPU_SREG_FS);
2450 seg_setup(VCPU_SREG_GS);
2451 seg_setup(VCPU_SREG_SS);
2452
2453 vmcs_write16(GUEST_TR_SELECTOR, 0);
2454 vmcs_writel(GUEST_TR_BASE, 0);
2455 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2456 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2457
2458 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2459 vmcs_writel(GUEST_LDTR_BASE, 0);
2460 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2461 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2462
2463 vmcs_write32(GUEST_SYSENTER_CS, 0);
2464 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2465 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2466
2467 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002468 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002469 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002470 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002471 kvm_rip_write(vcpu, 0);
2472 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002473
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002474 vmcs_writel(GUEST_DR7, 0x400);
2475
2476 vmcs_writel(GUEST_GDTR_BASE, 0);
2477 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2478
2479 vmcs_writel(GUEST_IDTR_BASE, 0);
2480 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2481
2482 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2483 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2484 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2485
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002486 /* Special registers */
2487 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2488
2489 setup_msrs(vmx);
2490
Avi Kivity6aa8b732006-12-10 02:21:36 -08002491 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2492
Sheng Yangf78e0e22007-10-29 09:40:42 +08002493 if (cpu_has_vmx_tpr_shadow()) {
2494 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2495 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2496 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002497 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002498 vmcs_write32(TPR_THRESHOLD, 0);
2499 }
2500
2501 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2502 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002503 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002504
Sheng Yang2384d2b2008-01-17 15:14:33 +08002505 if (vmx->vpid != 0)
2506 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2507
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002508 vmx->vcpu.arch.cr0 = 0x60000010;
2509 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002510 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002511 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002512 vmx_fpu_activate(&vmx->vcpu);
2513 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002514
Sheng Yang2384d2b2008-01-17 15:14:33 +08002515 vpid_sync_vcpu_all(vmx);
2516
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002517 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002518
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002519 /* HACK: Don't enable emulation on guest boot/reset */
2520 vmx->emulation_required = 0;
2521
Avi Kivity6aa8b732006-12-10 02:21:36 -08002522out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002523 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002524 return ret;
2525}
2526
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002527static void enable_irq_window(struct kvm_vcpu *vcpu)
2528{
2529 u32 cpu_based_vm_exec_control;
2530
2531 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2532 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2533 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2534}
2535
2536static void enable_nmi_window(struct kvm_vcpu *vcpu)
2537{
2538 u32 cpu_based_vm_exec_control;
2539
2540 if (!cpu_has_virtual_nmis()) {
2541 enable_irq_window(vcpu);
2542 return;
2543 }
2544
2545 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2546 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2547 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2548}
2549
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002550static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03002551{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002552 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002553 uint32_t intr;
2554 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02002555
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002556 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002557
Avi Kivityfa89a812008-09-01 15:57:51 +03002558 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002559 if (vmx->rmode.vm86_active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002560 vmx->rmode.irq.pending = true;
2561 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002562 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +03002563 if (vcpu->arch.interrupt.soft)
2564 vmx->rmode.irq.rip +=
2565 vmx->vcpu.arch.event_exit_inst_len;
Avi Kivity9c5623e2007-11-08 18:19:20 +02002566 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2567 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2568 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002569 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002570 return;
2571 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002572 intr = irq | INTR_INFO_VALID_MASK;
2573 if (vcpu->arch.interrupt.soft) {
2574 intr |= INTR_TYPE_SOFT_INTR;
2575 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2576 vmx->vcpu.arch.event_exit_inst_len);
2577 } else
2578 intr |= INTR_TYPE_EXT_INTR;
2579 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03002580}
2581
Sheng Yangf08864b2008-05-15 18:23:25 +08002582static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2583{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002584 struct vcpu_vmx *vmx = to_vmx(vcpu);
2585
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002586 if (!cpu_has_virtual_nmis()) {
2587 /*
2588 * Tracking the NMI-blocked state in software is built upon
2589 * finding the next open IRQ window. This, in turn, depends on
2590 * well-behaving guests: They have to keep IRQs disabled at
2591 * least as long as the NMI handler runs. Otherwise we may
2592 * cause NMI nesting, maybe breaking the guest. But as this is
2593 * highly unlikely, we can live with the residual risk.
2594 */
2595 vmx->soft_vnmi_blocked = 1;
2596 vmx->vnmi_blocked_time = 0;
2597 }
2598
Jan Kiszka487b3912008-09-26 09:30:56 +02002599 ++vcpu->stat.nmi_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002600 if (vmx->rmode.vm86_active) {
Jan Kiszka66a5a342008-09-26 09:30:51 +02002601 vmx->rmode.irq.pending = true;
2602 vmx->rmode.irq.vector = NMI_VECTOR;
2603 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2604 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2605 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2606 INTR_INFO_VALID_MASK);
2607 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2608 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2609 return;
2610 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002611 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2612 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002613}
2614
Gleb Natapovc4282df2009-04-21 17:45:07 +03002615static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02002616{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002617 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03002618 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002619
Gleb Natapovc4282df2009-04-21 17:45:07 +03002620 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2621 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS |
2622 GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02002623}
2624
Gleb Natapov78646122009-03-23 12:12:11 +02002625static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
2626{
Gleb Natapovc4282df2009-04-21 17:45:07 +03002627 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2628 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2629 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02002630}
2631
Izik Eiduscbc94022007-10-25 00:29:55 +02002632static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2633{
2634 int ret;
2635 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002636 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002637 .guest_phys_addr = addr,
2638 .memory_size = PAGE_SIZE * 3,
2639 .flags = 0,
2640 };
2641
2642 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2643 if (ret)
2644 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002645 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002646 return 0;
2647}
2648
Avi Kivity6aa8b732006-12-10 02:21:36 -08002649static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2650 int vec, u32 err_code)
2651{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002652 /*
2653 * Instruction with address size override prefix opcode 0x67
2654 * Cause the #SS fault with 0 error code in VM86 mode.
2655 */
2656 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02002657 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002658 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002659 /*
2660 * Forward all other exceptions that are valid in real mode.
2661 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2662 * the required debugging infrastructure rework.
2663 */
2664 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002665 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002666 if (vcpu->guest_debug &
2667 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2668 return 0;
2669 kvm_queue_exception(vcpu, vec);
2670 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002671 case BP_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002672 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2673 return 0;
2674 /* fall through */
2675 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002676 case OF_VECTOR:
2677 case BR_VECTOR:
2678 case UD_VECTOR:
2679 case DF_VECTOR:
2680 case SS_VECTOR:
2681 case GP_VECTOR:
2682 case MF_VECTOR:
2683 kvm_queue_exception(vcpu, vec);
2684 return 1;
2685 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002686 return 0;
2687}
2688
Andi Kleena0861c02009-06-08 17:37:09 +08002689/*
2690 * Trigger machine check on the host. We assume all the MSRs are already set up
2691 * by the CPU and that we still run on the same CPU as the MCE occurred on.
2692 * We pass a fake environment to the machine check handler because we want
2693 * the guest to be always treated like user space, no matter what context
2694 * it used internally.
2695 */
2696static void kvm_machine_check(void)
2697{
2698#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
2699 struct pt_regs regs = {
2700 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
2701 .flags = X86_EFLAGS_IF,
2702 };
2703
2704 do_machine_check(&regs, 0);
2705#endif
2706}
2707
2708static int handle_machine_check(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2709{
2710 /* already handled by vcpu_run */
2711 return 1;
2712}
2713
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2715{
Avi Kivity1155f762007-11-22 11:30:47 +02002716 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002717 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002718 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719 u32 vect_info;
2720 enum emulation_result er;
2721
Avi Kivity1155f762007-11-22 11:30:47 +02002722 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2724
Andi Kleena0861c02009-06-08 17:37:09 +08002725 if (is_machine_check(intr_info))
2726 return handle_machine_check(vcpu, kvm_run);
2727
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002729 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002730 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002731 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732
Jan Kiszkae4a41882008-09-26 09:30:46 +02002733 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002734 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002735
2736 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002737 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002738 return 1;
2739 }
2740
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002741 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08002742 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002743 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002744 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002745 return 1;
2746 }
2747
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002749 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002750 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2752 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002753 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02002754 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002755 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002756 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002757 trace_kvm_page_fault(cr2, error_code);
2758
Gleb Natapov3298b752009-05-11 13:35:46 +03002759 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03002760 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002761 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 }
2763
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002764 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002765 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002766 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002767 if (vcpu->arch.halt_request) {
2768 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002769 return kvm_emulate_halt(vcpu);
2770 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002772 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002773
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002774 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002775 switch (ex_no) {
2776 case DB_VECTOR:
2777 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2778 if (!(vcpu->guest_debug &
2779 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2780 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2781 kvm_queue_exception(vcpu, DB_VECTOR);
2782 return 1;
2783 }
2784 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2785 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2786 /* fall through */
2787 case BP_VECTOR:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002788 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002789 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2790 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002791 break;
2792 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002793 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2794 kvm_run->ex.exception = ex_no;
2795 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002796 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002797 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002798 return 0;
2799}
2800
2801static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2802 struct kvm_run *kvm_run)
2803{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002804 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002805 return 1;
2806}
2807
Avi Kivity988ad742007-02-12 00:54:36 -08002808static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2809{
2810 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2811 return 0;
2812}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2815{
He, Qingbfdaab02007-09-12 14:18:28 +08002816 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01002817 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002818 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819
Avi Kivity1165f5f2007-04-19 17:27:43 +03002820 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002821 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002822 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002823
2824 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002825 if (emulate_instruction(vcpu,
2826 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002827 return 0;
2828 return 1;
2829 }
2830
2831 size = (exit_qualification & 7) + 1;
2832 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002833 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002834
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002835 skip_emulated_instruction(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002836 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002837}
2838
Ingo Molnar102d8322007-02-19 14:37:47 +02002839static void
2840vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2841{
2842 /*
2843 * Patch in the VMCALL instruction:
2844 */
2845 hypercall[0] = 0x0f;
2846 hypercall[1] = 0x01;
2847 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002848}
2849
Avi Kivity6aa8b732006-12-10 02:21:36 -08002850static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2851{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002852 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853 int cr;
2854 int reg;
2855
He, Qingbfdaab02007-09-12 14:18:28 +08002856 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002857 cr = exit_qualification & 15;
2858 reg = (exit_qualification >> 8) & 15;
2859 switch ((exit_qualification >> 4) & 3) {
2860 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002861 val = kvm_register_read(vcpu, reg);
2862 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863 switch (cr) {
2864 case 0:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002865 kvm_set_cr0(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866 skip_emulated_instruction(vcpu);
2867 return 1;
2868 case 3:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002869 kvm_set_cr3(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002870 skip_emulated_instruction(vcpu);
2871 return 1;
2872 case 4:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002873 kvm_set_cr4(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874 skip_emulated_instruction(vcpu);
2875 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002876 case 8: {
2877 u8 cr8_prev = kvm_get_cr8(vcpu);
2878 u8 cr8 = kvm_register_read(vcpu, reg);
2879 kvm_set_cr8(vcpu, cr8);
2880 skip_emulated_instruction(vcpu);
2881 if (irqchip_in_kernel(vcpu->kvm))
2882 return 1;
2883 if (cr8_prev <= cr8)
2884 return 1;
2885 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2886 return 0;
2887 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002888 };
2889 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002890 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002891 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002892 vcpu->arch.cr0 &= ~X86_CR0_TS;
2893 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002894 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002895 skip_emulated_instruction(vcpu);
2896 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002897 case 1: /*mov from cr*/
2898 switch (cr) {
2899 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002900 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002901 trace_kvm_cr_read(cr, vcpu->arch.cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902 skip_emulated_instruction(vcpu);
2903 return 1;
2904 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002905 val = kvm_get_cr8(vcpu);
2906 kvm_register_write(vcpu, reg, val);
2907 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908 skip_emulated_instruction(vcpu);
2909 return 1;
2910 }
2911 break;
2912 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002913 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002914
2915 skip_emulated_instruction(vcpu);
2916 return 1;
2917 default:
2918 break;
2919 }
2920 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002921 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002922 (int)(exit_qualification >> 4) & 3, cr);
2923 return 0;
2924}
2925
2926static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2927{
He, Qingbfdaab02007-09-12 14:18:28 +08002928 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002929 unsigned long val;
2930 int dr, reg;
2931
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002932 dr = vmcs_readl(GUEST_DR7);
2933 if (dr & DR7_GD) {
2934 /*
2935 * As the vm-exit takes precedence over the debug trap, we
2936 * need to emulate the latter, either for the host or the
2937 * guest debugging itself.
2938 */
2939 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
2940 kvm_run->debug.arch.dr6 = vcpu->arch.dr6;
2941 kvm_run->debug.arch.dr7 = dr;
2942 kvm_run->debug.arch.pc =
2943 vmcs_readl(GUEST_CS_BASE) +
2944 vmcs_readl(GUEST_RIP);
2945 kvm_run->debug.arch.exception = DB_VECTOR;
2946 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2947 return 0;
2948 } else {
2949 vcpu->arch.dr7 &= ~DR7_GD;
2950 vcpu->arch.dr6 |= DR6_BD;
2951 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2952 kvm_queue_exception(vcpu, DB_VECTOR);
2953 return 1;
2954 }
2955 }
2956
He, Qingbfdaab02007-09-12 14:18:28 +08002957 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002958 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
2959 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
2960 if (exit_qualification & TYPE_MOV_FROM_DR) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002961 switch (dr) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002962 case 0 ... 3:
2963 val = vcpu->arch.db[dr];
2964 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002966 val = vcpu->arch.dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 break;
2968 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002969 val = vcpu->arch.dr7;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002970 break;
2971 default:
2972 val = 0;
2973 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002974 kvm_register_write(vcpu, reg, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002975 } else {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002976 val = vcpu->arch.regs[reg];
2977 switch (dr) {
2978 case 0 ... 3:
2979 vcpu->arch.db[dr] = val;
2980 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
2981 vcpu->arch.eff_db[dr] = val;
2982 break;
2983 case 4 ... 5:
2984 if (vcpu->arch.cr4 & X86_CR4_DE)
2985 kvm_queue_exception(vcpu, UD_VECTOR);
2986 break;
2987 case 6:
2988 if (val & 0xffffffff00000000ULL) {
2989 kvm_queue_exception(vcpu, GP_VECTOR);
2990 break;
2991 }
2992 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
2993 break;
2994 case 7:
2995 if (val & 0xffffffff00000000ULL) {
2996 kvm_queue_exception(vcpu, GP_VECTOR);
2997 break;
2998 }
2999 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
3000 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
3001 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3002 vcpu->arch.switch_db_regs =
3003 (val & DR7_BP_EN_MASK);
3004 }
3005 break;
3006 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003007 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003008 skip_emulated_instruction(vcpu);
3009 return 1;
3010}
3011
3012static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3013{
Avi Kivity06465c52007-02-28 20:46:53 +02003014 kvm_emulate_cpuid(vcpu);
3015 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003016}
3017
3018static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3019{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003020 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021 u64 data;
3022
3023 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003024 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003025 return 1;
3026 }
3027
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003028 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003029
Avi Kivity6aa8b732006-12-10 02:21:36 -08003030 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003031 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3032 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003033 skip_emulated_instruction(vcpu);
3034 return 1;
3035}
3036
3037static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3038{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003039 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3040 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3041 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003042
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003043 trace_kvm_msr_write(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003044
Avi Kivity6aa8b732006-12-10 02:21:36 -08003045 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003046 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047 return 1;
3048 }
3049
3050 skip_emulated_instruction(vcpu);
3051 return 1;
3052}
3053
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003054static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
3055 struct kvm_run *kvm_run)
3056{
3057 return 1;
3058}
3059
Avi Kivity6aa8b732006-12-10 02:21:36 -08003060static int handle_interrupt_window(struct kvm_vcpu *vcpu,
3061 struct kvm_run *kvm_run)
3062{
Eddie Dong85f455f2007-07-06 12:20:49 +03003063 u32 cpu_based_vm_exec_control;
3064
3065 /* clear pending irq */
3066 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3067 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3068 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003069
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003070 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003071
Dor Laorc1150d82007-01-05 16:36:24 -08003072 /*
3073 * If the user space waits to inject interrupts, exit as soon as
3074 * possible
3075 */
Gleb Natapov80618232009-04-21 17:44:56 +03003076 if (!irqchip_in_kernel(vcpu->kvm) &&
3077 kvm_run->request_interrupt_window &&
3078 !kvm_cpu_has_interrupt(vcpu)) {
Dor Laorc1150d82007-01-05 16:36:24 -08003079 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003080 return 0;
3081 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003082 return 1;
3083}
3084
3085static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3086{
3087 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003088 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089}
3090
Ingo Molnarc21415e2007-02-19 14:37:47 +02003091static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3092{
Dor Laor510043d2007-02-19 18:25:43 +02003093 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003094 kvm_emulate_hypercall(vcpu);
3095 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003096}
3097
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003098static int handle_vmx_insn(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3099{
3100 kvm_queue_exception(vcpu, UD_VECTOR);
3101 return 1;
3102}
3103
Marcelo Tosattia7052892008-09-23 13:18:35 -03003104static int handle_invlpg(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3105{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003106 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003107
3108 kvm_mmu_invlpg(vcpu, exit_qualification);
3109 skip_emulated_instruction(vcpu);
3110 return 1;
3111}
3112
Eddie Donge5edaa02007-11-11 12:28:35 +02003113static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3114{
3115 skip_emulated_instruction(vcpu);
3116 /* TODO: Add support for VT-d/pass-through device */
3117 return 1;
3118}
3119
Sheng Yangf78e0e22007-10-29 09:40:42 +08003120static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3121{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003122 unsigned long exit_qualification;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003123 enum emulation_result er;
3124 unsigned long offset;
3125
Sheng Yangf9c617f2009-03-25 10:08:52 +08003126 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003127 offset = exit_qualification & 0xffful;
3128
3129 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3130
3131 if (er != EMULATE_DONE) {
3132 printk(KERN_ERR
3133 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3134 offset);
3135 return -ENOTSUPP;
3136 }
3137 return 1;
3138}
3139
Izik Eidus37817f22008-03-24 23:14:53 +02003140static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3141{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003142 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003143 unsigned long exit_qualification;
3144 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003145 int reason, type, idt_v;
3146
3147 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3148 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003149
3150 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3151
3152 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003153 if (reason == TASK_SWITCH_GATE && idt_v) {
3154 switch (type) {
3155 case INTR_TYPE_NMI_INTR:
3156 vcpu->arch.nmi_injected = false;
3157 if (cpu_has_virtual_nmis())
3158 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3159 GUEST_INTR_STATE_NMI);
3160 break;
3161 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003162 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003163 kvm_clear_interrupt_queue(vcpu);
3164 break;
3165 case INTR_TYPE_HARD_EXCEPTION:
3166 case INTR_TYPE_SOFT_EXCEPTION:
3167 kvm_clear_exception_queue(vcpu);
3168 break;
3169 default:
3170 break;
3171 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003172 }
Izik Eidus37817f22008-03-24 23:14:53 +02003173 tss_selector = exit_qualification;
3174
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003175 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3176 type != INTR_TYPE_EXT_INTR &&
3177 type != INTR_TYPE_NMI_INTR))
3178 skip_emulated_instruction(vcpu);
3179
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003180 if (!kvm_task_switch(vcpu, tss_selector, reason))
3181 return 0;
3182
3183 /* clear all local breakpoint enable flags */
3184 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3185
3186 /*
3187 * TODO: What about debug traps on tss switch?
3188 * Are we supposed to inject them and update dr6?
3189 */
3190
3191 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003192}
3193
Sheng Yang14394422008-04-28 12:24:45 +08003194static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3195{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003196 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003197 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003198 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003199
Sheng Yangf9c617f2009-03-25 10:08:52 +08003200 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003201
3202 if (exit_qualification & (1 << 6)) {
3203 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
3204 return -ENOTSUPP;
3205 }
3206
3207 gla_validity = (exit_qualification >> 7) & 0x3;
3208 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3209 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3210 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3211 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003212 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003213 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3214 (long unsigned int)exit_qualification);
3215 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity596ae892009-06-03 14:12:10 +03003216 kvm_run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
3217 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003218 }
3219
3220 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003221 trace_kvm_page_fault(gpa, exit_qualification);
Sheng Yang49cd7d22009-02-11 13:50:40 +08003222 return kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003223}
3224
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003225static u64 ept_rsvd_mask(u64 spte, int level)
3226{
3227 int i;
3228 u64 mask = 0;
3229
3230 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
3231 mask |= (1ULL << i);
3232
3233 if (level > 2)
3234 /* bits 7:3 reserved */
3235 mask |= 0xf8;
3236 else if (level == 2) {
3237 if (spte & (1ULL << 7))
3238 /* 2MB ref, bits 20:12 reserved */
3239 mask |= 0x1ff000;
3240 else
3241 /* bits 6:3 reserved */
3242 mask |= 0x78;
3243 }
3244
3245 return mask;
3246}
3247
3248static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
3249 int level)
3250{
3251 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
3252
3253 /* 010b (write-only) */
3254 WARN_ON((spte & 0x7) == 0x2);
3255
3256 /* 110b (write/execute) */
3257 WARN_ON((spte & 0x7) == 0x6);
3258
3259 /* 100b (execute-only) and value not supported by logical processor */
3260 if (!cpu_has_vmx_ept_execute_only())
3261 WARN_ON((spte & 0x7) == 0x4);
3262
3263 /* not 000b */
3264 if ((spte & 0x7)) {
3265 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
3266
3267 if (rsvd_bits != 0) {
3268 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
3269 __func__, rsvd_bits);
3270 WARN_ON(1);
3271 }
3272
3273 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
3274 u64 ept_mem_type = (spte & 0x38) >> 3;
3275
3276 if (ept_mem_type == 2 || ept_mem_type == 3 ||
3277 ept_mem_type == 7) {
3278 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
3279 __func__, ept_mem_type);
3280 WARN_ON(1);
3281 }
3282 }
3283 }
3284}
3285
3286static int handle_ept_misconfig(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3287{
3288 u64 sptes[4];
3289 int nr_sptes, i;
3290 gpa_t gpa;
3291
3292 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3293
3294 printk(KERN_ERR "EPT: Misconfiguration.\n");
3295 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
3296
3297 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
3298
3299 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
3300 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
3301
3302 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3303 kvm_run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
3304
3305 return 0;
3306}
3307
Sheng Yangf08864b2008-05-15 18:23:25 +08003308static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3309{
3310 u32 cpu_based_vm_exec_control;
3311
3312 /* clear pending NMI */
3313 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3314 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3315 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3316 ++vcpu->stat.nmi_window_exits;
3317
3318 return 1;
3319}
3320
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003321static void handle_invalid_guest_state(struct kvm_vcpu *vcpu,
3322 struct kvm_run *kvm_run)
3323{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003324 struct vcpu_vmx *vmx = to_vmx(vcpu);
3325 enum emulation_result err = EMULATE_DONE;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003326
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003327 local_irq_enable();
Jan Kiszka34f0c1a2009-07-22 23:53:26 +02003328 preempt_enable();
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003329
3330 while (!guest_state_valid(vcpu)) {
3331 err = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3332
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003333 if (err == EMULATE_DO_MMIO)
3334 break;
3335
3336 if (err != EMULATE_DONE) {
3337 kvm_report_emulation_failure(vcpu, "emulation failure");
Jan Kiszka263799a2009-07-21 10:43:07 +02003338 break;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003339 }
3340
3341 if (signal_pending(current))
3342 break;
3343 if (need_resched())
3344 schedule();
3345 }
3346
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003347 preempt_disable();
Jan Kiszka34f0c1a2009-07-22 23:53:26 +02003348 local_irq_disable();
Avi Kivity8b3079a2009-01-05 12:10:54 +02003349
3350 vmx->invalid_state_emulation_result = err;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003351}
3352
Avi Kivity6aa8b732006-12-10 02:21:36 -08003353/*
3354 * The exit handlers return 1 if the exit was handled fully and guest execution
3355 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3356 * to be done to userspace and return 0.
3357 */
3358static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
3359 struct kvm_run *kvm_run) = {
3360 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3361 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003362 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003363 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003364 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003365 [EXIT_REASON_CR_ACCESS] = handle_cr,
3366 [EXIT_REASON_DR_ACCESS] = handle_dr,
3367 [EXIT_REASON_CPUID] = handle_cpuid,
3368 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3369 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3370 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3371 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003372 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003373 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003374 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3375 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3376 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3377 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3378 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3379 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3380 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3381 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3382 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003383 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3384 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003385 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003386 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08003387 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003388 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
3389 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003390};
3391
3392static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003393 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003394
3395/*
3396 * The guest has exited. See if we can fix it or if we need userspace
3397 * assistance.
3398 */
Avi Kivity6062d012009-03-23 17:35:17 +02003399static int vmx_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003400{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003401 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003402 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003403 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003404
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003405 trace_kvm_exit(exit_reason, kvm_rip_read(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003406
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003407 /* If we need to emulate an MMIO from handle_invalid_guest_state
3408 * we just return 0 */
Avi Kivity10f32d82009-01-05 00:53:19 +02003409 if (vmx->emulation_required && emulate_invalid_guest_state) {
3410 if (guest_state_valid(vcpu))
3411 vmx->emulation_required = 0;
Avi Kivity8b3079a2009-01-05 12:10:54 +02003412 return vmx->invalid_state_emulation_result != EMULATE_DO_MMIO;
Avi Kivity10f32d82009-01-05 00:53:19 +02003413 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003414
Sheng Yang14394422008-04-28 12:24:45 +08003415 /* Access CR3 don't cause VMExit in paging mode, so we need
3416 * to sync with guest real CR3. */
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003417 if (enable_ept && is_paging(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08003418 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003419
Avi Kivity29bd8a72007-09-10 17:27:03 +03003420 if (unlikely(vmx->fail)) {
3421 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3422 kvm_run->fail_entry.hardware_entry_failure_reason
3423 = vmcs_read32(VM_INSTRUCTION_ERROR);
3424 return 0;
3425 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426
Mike Dayd77c26f2007-10-08 09:02:08 -04003427 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003428 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003429 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3430 exit_reason != EXIT_REASON_TASK_SWITCH))
3431 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3432 "(0x%x) and exit reason is 0x%x\n",
3433 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003434
3435 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03003436 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003437 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003438 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003439 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003440 /*
3441 * This CPU don't support us in finding the end of an
3442 * NMI-blocked window if the guest runs with IRQs
3443 * disabled. So we pull the trigger after 1 s of
3444 * futile waiting, but inform the user about this.
3445 */
3446 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3447 "state on VCPU %d after 1 s timeout\n",
3448 __func__, vcpu->vcpu_id);
3449 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003450 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003451 }
3452
Avi Kivity6aa8b732006-12-10 02:21:36 -08003453 if (exit_reason < kvm_vmx_max_exit_handlers
3454 && kvm_vmx_exit_handlers[exit_reason])
3455 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
3456 else {
3457 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3458 kvm_run->hw.hardware_exit_reason = exit_reason;
3459 }
3460 return 0;
3461}
3462
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003463static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003464{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003465 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003466 vmcs_write32(TPR_THRESHOLD, 0);
3467 return;
3468 }
3469
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003470 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003471}
3472
Avi Kivitycf393f72008-07-01 16:20:21 +03003473static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3474{
3475 u32 exit_intr_info;
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003476 u32 idt_vectoring_info = vmx->idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003477 bool unblock_nmi;
3478 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003479 int type;
3480 bool idtv_info_valid;
Avi Kivitycf393f72008-07-01 16:20:21 +03003481
3482 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Gleb Natapov20f65982009-05-11 13:35:55 +03003483
Andi Kleena0861c02009-06-08 17:37:09 +08003484 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
3485
3486 /* Handle machine checks before interrupts are enabled */
3487 if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
3488 || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
3489 && is_machine_check(exit_intr_info)))
3490 kvm_machine_check();
3491
Gleb Natapov20f65982009-05-11 13:35:55 +03003492 /* We need to handle NMIs before interrupts are enabled */
3493 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003494 (exit_intr_info & INTR_INFO_VALID_MASK))
Gleb Natapov20f65982009-05-11 13:35:55 +03003495 asm("int $2");
Gleb Natapov20f65982009-05-11 13:35:55 +03003496
3497 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3498
Avi Kivitycf393f72008-07-01 16:20:21 +03003499 if (cpu_has_virtual_nmis()) {
3500 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3501 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3502 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003503 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03003504 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3505 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003506 * SDM 3: 23.2.2 (September 2008)
3507 * Bit 12 is undefined in any of the following cases:
3508 * If the VM exit sets the valid bit in the IDT-vectoring
3509 * information field.
3510 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03003511 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003512 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
3513 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03003514 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3515 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003516 } else if (unlikely(vmx->soft_vnmi_blocked))
3517 vmx->vnmi_blocked_time +=
3518 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003519
Gleb Natapov37b96e92009-03-30 16:03:13 +03003520 vmx->vcpu.arch.nmi_injected = false;
3521 kvm_clear_exception_queue(&vmx->vcpu);
3522 kvm_clear_interrupt_queue(&vmx->vcpu);
3523
3524 if (!idtv_info_valid)
3525 return;
3526
Avi Kivity668f6122008-07-02 09:28:55 +03003527 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3528 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003529
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003530 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003531 case INTR_TYPE_NMI_INTR:
3532 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03003533 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003534 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03003535 * Clear bit "block by NMI" before VM entry if a NMI
3536 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03003537 */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003538 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3539 GUEST_INTR_STATE_NMI);
3540 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003541 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003542 vmx->vcpu.arch.event_exit_inst_len =
3543 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3544 /* fall through */
3545 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03003546 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003547 u32 err = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3548 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03003549 } else
3550 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003551 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003552 case INTR_TYPE_SOFT_INTR:
3553 vmx->vcpu.arch.event_exit_inst_len =
3554 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3555 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003556 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003557 kvm_queue_interrupt(&vmx->vcpu, vector,
3558 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003559 break;
3560 default:
3561 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03003562 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003563}
3564
Avi Kivity9c8cba32007-11-22 11:42:59 +02003565/*
3566 * Failure to inject an interrupt should give us the information
3567 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3568 * when fetching the interrupt redirection bitmap in the real-mode
3569 * tss, this doesn't happen. So we do it ourselves.
3570 */
3571static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3572{
3573 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003574 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003575 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003576 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003577 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3578 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3579 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3580 return;
3581 }
3582 vmx->idt_vectoring_info =
3583 VECTORING_INFO_VALID_MASK
3584 | INTR_TYPE_EXT_INTR
3585 | vmx->rmode.irq.vector;
3586}
3587
Avi Kivityc8019492008-07-14 14:44:59 +03003588#ifdef CONFIG_X86_64
3589#define R "r"
3590#define Q "q"
3591#else
3592#define R "e"
3593#define Q "l"
3594#endif
3595
Avi Kivity04d2cc72007-09-10 18:10:54 +03003596static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003597{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003598 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitye6adf282007-04-30 16:07:54 +03003599
Avi Kivity8f5d5492009-05-31 18:41:29 +03003600 if (enable_ept && is_paging(vcpu)) {
3601 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3602 ept_load_pdptrs(vcpu);
3603 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003604 /* Record the guest's net vcpu time for enforced NMI injections. */
3605 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3606 vmx->entry_time = ktime_get();
3607
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003608 /* Handle invalid guest state instead of entering VMX */
3609 if (vmx->emulation_required && emulate_invalid_guest_state) {
3610 handle_invalid_guest_state(vcpu, kvm_run);
3611 return;
3612 }
3613
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003614 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3615 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3616 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3617 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3618
Gleb Natapov787ff732009-05-18 11:44:06 +03003619 /* When single-stepping over STI and MOV SS, we must clear the
3620 * corresponding interruptibility bits in the guest state. Otherwise
3621 * vmentry fails as it then expects bit 14 (BS) in pending debug
3622 * exceptions being set, but that's not correct for the guest debugging
3623 * case. */
3624 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3625 vmx_set_interrupt_shadow(vcpu, 0);
3626
Avi Kivitye6adf282007-04-30 16:07:54 +03003627 /*
3628 * Loading guest fpu may have cleared host cr0.ts
3629 */
3630 vmcs_writel(HOST_CR0, read_cr0());
3631
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003632 set_debugreg(vcpu->arch.dr6, 6);
3633
Mike Dayd77c26f2007-10-08 09:02:08 -04003634 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003635 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003636 "push %%"R"dx; push %%"R"bp;"
3637 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003638 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3639 "je 1f \n\t"
3640 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003641 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003642 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03003643 /* Reload cr2 if changed */
3644 "mov %c[cr2](%0), %%"R"ax \n\t"
3645 "mov %%cr2, %%"R"dx \n\t"
3646 "cmp %%"R"ax, %%"R"dx \n\t"
3647 "je 2f \n\t"
3648 "mov %%"R"ax, %%cr2 \n\t"
3649 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003650 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003651 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003652 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003653 "mov %c[rax](%0), %%"R"ax \n\t"
3654 "mov %c[rbx](%0), %%"R"bx \n\t"
3655 "mov %c[rdx](%0), %%"R"dx \n\t"
3656 "mov %c[rsi](%0), %%"R"si \n\t"
3657 "mov %c[rdi](%0), %%"R"di \n\t"
3658 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003659#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003660 "mov %c[r8](%0), %%r8 \n\t"
3661 "mov %c[r9](%0), %%r9 \n\t"
3662 "mov %c[r10](%0), %%r10 \n\t"
3663 "mov %c[r11](%0), %%r11 \n\t"
3664 "mov %c[r12](%0), %%r12 \n\t"
3665 "mov %c[r13](%0), %%r13 \n\t"
3666 "mov %c[r14](%0), %%r14 \n\t"
3667 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003668#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003669 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3670
Avi Kivity6aa8b732006-12-10 02:21:36 -08003671 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003672 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003673 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003674 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003675 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003676 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003677 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003678 "xchg %0, (%%"R"sp) \n\t"
3679 "mov %%"R"ax, %c[rax](%0) \n\t"
3680 "mov %%"R"bx, %c[rbx](%0) \n\t"
3681 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3682 "mov %%"R"dx, %c[rdx](%0) \n\t"
3683 "mov %%"R"si, %c[rsi](%0) \n\t"
3684 "mov %%"R"di, %c[rdi](%0) \n\t"
3685 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003686#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003687 "mov %%r8, %c[r8](%0) \n\t"
3688 "mov %%r9, %c[r9](%0) \n\t"
3689 "mov %%r10, %c[r10](%0) \n\t"
3690 "mov %%r11, %c[r11](%0) \n\t"
3691 "mov %%r12, %c[r12](%0) \n\t"
3692 "mov %%r13, %c[r13](%0) \n\t"
3693 "mov %%r14, %c[r14](%0) \n\t"
3694 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003695#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003696 "mov %%cr2, %%"R"ax \n\t"
3697 "mov %%"R"ax, %c[cr2](%0) \n\t"
3698
3699 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003700 "setbe %c[fail](%0) \n\t"
3701 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3702 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3703 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03003704 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003705 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3706 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3707 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3708 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3709 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3710 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3711 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003712#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003713 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3714 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3715 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3716 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3717 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3718 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3719 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3720 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003721#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003722 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003723 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003724 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003725#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003726 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3727#endif
3728 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003729
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003730 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
3731 | (1 << VCPU_EXREG_PDPTR));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003732 vcpu->arch.regs_dirty = 0;
3733
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003734 get_debugreg(vcpu->arch.dr6, 6);
3735
Avi Kivity1155f762007-11-22 11:30:47 +02003736 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003737 if (vmx->rmode.irq.pending)
3738 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003739
Mike Dayd77c26f2007-10-08 09:02:08 -04003740 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003741 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003742
Avi Kivitycf393f72008-07-01 16:20:21 +03003743 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003744}
3745
Avi Kivityc8019492008-07-14 14:44:59 +03003746#undef R
3747#undef Q
3748
Avi Kivity6aa8b732006-12-10 02:21:36 -08003749static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3750{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003751 struct vcpu_vmx *vmx = to_vmx(vcpu);
3752
3753 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003754 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003755 free_vmcs(vmx->vmcs);
3756 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003757 }
3758}
3759
3760static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3761{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003762 struct vcpu_vmx *vmx = to_vmx(vcpu);
3763
Sheng Yang2384d2b2008-01-17 15:14:33 +08003764 spin_lock(&vmx_vpid_lock);
3765 if (vmx->vpid != 0)
3766 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3767 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003768 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003769 kfree(vmx->host_msrs);
3770 kfree(vmx->guest_msrs);
3771 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003772 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003773}
3774
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003775static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003776{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003777 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003778 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003779 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003780
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003781 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003782 return ERR_PTR(-ENOMEM);
3783
Sheng Yang2384d2b2008-01-17 15:14:33 +08003784 allocate_vpid(vmx);
3785
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003786 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3787 if (err)
3788 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003789
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003790 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003791 if (!vmx->guest_msrs) {
3792 err = -ENOMEM;
3793 goto uninit_vcpu;
3794 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003795
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003796 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3797 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003798 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003799
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003800 vmx->vmcs = alloc_vmcs();
3801 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003802 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003803
3804 vmcs_clear(vmx->vmcs);
3805
Avi Kivity15ad7142007-07-11 18:17:21 +03003806 cpu = get_cpu();
3807 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003808 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003809 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003810 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003811 if (err)
3812 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003813 if (vm_need_virtualize_apic_accesses(kvm))
3814 if (alloc_apic_access_page(kvm) != 0)
3815 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003816
Avi Kivity089d0342009-03-23 18:26:32 +02003817 if (enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003818 if (alloc_identity_pagetable(kvm) != 0)
3819 goto free_vmcs;
3820
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003821 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003822
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003823free_vmcs:
3824 free_vmcs(vmx->vmcs);
3825free_msrs:
3826 kfree(vmx->host_msrs);
3827free_guest_msrs:
3828 kfree(vmx->guest_msrs);
3829uninit_vcpu:
3830 kvm_vcpu_uninit(&vmx->vcpu);
3831free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003832 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003833 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003834}
3835
Yang, Sheng002c7f72007-07-31 14:23:01 +03003836static void __init vmx_check_processor_compat(void *rtn)
3837{
3838 struct vmcs_config vmcs_conf;
3839
3840 *(int *)rtn = 0;
3841 if (setup_vmcs_config(&vmcs_conf) < 0)
3842 *(int *)rtn = -EIO;
3843 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3844 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3845 smp_processor_id());
3846 *(int *)rtn = -EIO;
3847 }
3848}
3849
Sheng Yang67253af2008-04-25 10:20:22 +08003850static int get_ept_level(void)
3851{
3852 return VMX_EPT_DEFAULT_GAW + 1;
3853}
3854
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003855static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003856{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003857 u64 ret;
3858
Sheng Yang522c68c2009-04-27 20:35:43 +08003859 /* For VT-d and EPT combination
3860 * 1. MMIO: always map as UC
3861 * 2. EPT with VT-d:
3862 * a. VT-d without snooping control feature: can't guarantee the
3863 * result, try to trust guest.
3864 * b. VT-d with snooping control feature: snooping control feature of
3865 * VT-d engine can guarantee the cache correctness. Just set it
3866 * to WB to keep consistent with host. So the same as item 3.
3867 * 3. EPT without VT-d: always map as WB and set IGMT=1 to keep
3868 * consistent with host MTRR
3869 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003870 if (is_mmio)
3871 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08003872 else if (vcpu->kvm->arch.iommu_domain &&
3873 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
3874 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
3875 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003876 else
Sheng Yang522c68c2009-04-27 20:35:43 +08003877 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
3878 | VMX_EPT_IGMT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003879
3880 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08003881}
3882
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003883static const struct trace_print_flags vmx_exit_reasons_str[] = {
3884 { EXIT_REASON_EXCEPTION_NMI, "exception" },
3885 { EXIT_REASON_EXTERNAL_INTERRUPT, "ext_irq" },
3886 { EXIT_REASON_TRIPLE_FAULT, "triple_fault" },
3887 { EXIT_REASON_NMI_WINDOW, "nmi_window" },
3888 { EXIT_REASON_IO_INSTRUCTION, "io_instruction" },
3889 { EXIT_REASON_CR_ACCESS, "cr_access" },
3890 { EXIT_REASON_DR_ACCESS, "dr_access" },
3891 { EXIT_REASON_CPUID, "cpuid" },
3892 { EXIT_REASON_MSR_READ, "rdmsr" },
3893 { EXIT_REASON_MSR_WRITE, "wrmsr" },
3894 { EXIT_REASON_PENDING_INTERRUPT, "interrupt_window" },
3895 { EXIT_REASON_HLT, "halt" },
3896 { EXIT_REASON_INVLPG, "invlpg" },
3897 { EXIT_REASON_VMCALL, "hypercall" },
3898 { EXIT_REASON_TPR_BELOW_THRESHOLD, "tpr_below_thres" },
3899 { EXIT_REASON_APIC_ACCESS, "apic_access" },
3900 { EXIT_REASON_WBINVD, "wbinvd" },
3901 { EXIT_REASON_TASK_SWITCH, "task_switch" },
3902 { EXIT_REASON_EPT_VIOLATION, "ept_violation" },
3903 { -1, NULL }
3904};
3905
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003906static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003907 .cpu_has_kvm_support = cpu_has_kvm_support,
3908 .disabled_by_bios = vmx_disabled_by_bios,
3909 .hardware_setup = hardware_setup,
3910 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003911 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003912 .hardware_enable = hardware_enable,
3913 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08003914 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003915
3916 .vcpu_create = vmx_create_vcpu,
3917 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003918 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003919
Avi Kivity04d2cc72007-09-10 18:10:54 +03003920 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003921 .vcpu_load = vmx_vcpu_load,
3922 .vcpu_put = vmx_vcpu_put,
3923
3924 .set_guest_debug = set_guest_debug,
3925 .get_msr = vmx_get_msr,
3926 .set_msr = vmx_set_msr,
3927 .get_segment_base = vmx_get_segment_base,
3928 .get_segment = vmx_get_segment,
3929 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003930 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003931 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003932 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003933 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003934 .set_cr3 = vmx_set_cr3,
3935 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003936 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003937 .get_idt = vmx_get_idt,
3938 .set_idt = vmx_set_idt,
3939 .get_gdt = vmx_get_gdt,
3940 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003941 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003942 .get_rflags = vmx_get_rflags,
3943 .set_rflags = vmx_set_rflags,
3944
3945 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003946
Avi Kivity6aa8b732006-12-10 02:21:36 -08003947 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02003948 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003949 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003950 .set_interrupt_shadow = vmx_set_interrupt_shadow,
3951 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003952 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003953 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003954 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003955 .queue_exception = vmx_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003956 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003957 .nmi_allowed = vmx_nmi_allowed,
3958 .enable_nmi_window = enable_nmi_window,
3959 .enable_irq_window = enable_irq_window,
3960 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003961
Izik Eiduscbc94022007-10-25 00:29:55 +02003962 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003963 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003964 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003965
3966 .exit_reasons_str = vmx_exit_reasons_str,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003967};
3968
3969static int __init vmx_init(void)
3970{
He, Qingfdef3ad2007-04-30 09:45:24 +03003971 int r;
3972
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003973 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03003974 if (!vmx_io_bitmap_a)
3975 return -ENOMEM;
3976
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003977 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03003978 if (!vmx_io_bitmap_b) {
3979 r = -ENOMEM;
3980 goto out;
3981 }
3982
Avi Kivity58972972009-02-24 22:26:47 +02003983 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
3984 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08003985 r = -ENOMEM;
3986 goto out1;
3987 }
3988
Avi Kivity58972972009-02-24 22:26:47 +02003989 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
3990 if (!vmx_msr_bitmap_longmode) {
3991 r = -ENOMEM;
3992 goto out2;
3993 }
3994
He, Qingfdef3ad2007-04-30 09:45:24 +03003995 /*
3996 * Allow direct access to the PC debug port (it is often used for I/O
3997 * delays, but the vmexits simply slow things down).
3998 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003999 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
4000 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004001
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004002 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004003
Avi Kivity58972972009-02-24 22:26:47 +02004004 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
4005 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08004006
Sheng Yang2384d2b2008-01-17 15:14:33 +08004007 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
4008
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004009 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004010 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02004011 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08004012
Avi Kivity58972972009-02-24 22:26:47 +02004013 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
4014 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
4015 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
4016 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
4017 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
4018 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03004019
Avi Kivity089d0342009-03-23 18:26:32 +02004020 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004021 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004022 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08004023 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08004024 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004025 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004026 kvm_enable_tdp();
4027 } else
4028 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08004029
Avi Kivityc7addb92007-09-16 18:58:32 +02004030 if (bypass_guest_pf)
4031 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
4032
Sheng Yang14394422008-04-28 12:24:45 +08004033 ept_sync_global();
4034
He, Qingfdef3ad2007-04-30 09:45:24 +03004035 return 0;
4036
Avi Kivity58972972009-02-24 22:26:47 +02004037out3:
4038 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08004039out2:
Avi Kivity58972972009-02-24 22:26:47 +02004040 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03004041out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004042 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03004043out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004044 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004045 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004046}
4047
4048static void __exit vmx_exit(void)
4049{
Avi Kivity58972972009-02-24 22:26:47 +02004050 free_page((unsigned long)vmx_msr_bitmap_legacy);
4051 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004052 free_page((unsigned long)vmx_io_bitmap_b);
4053 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004054
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004055 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056}
4057
4058module_init(vmx_init)
4059module_exit(vmx_exit)