blob: 1a84ca191cd11c180c1d9cb6276ced5eef2f7069 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040020
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020023#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/mm.h>
25#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040026#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020027#include <linux/moduleparam.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030028#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030029#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080032#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020033#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020034#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080035#include <asm/mce.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080036
Avi Kivity4ecac3f2008-05-13 13:23:38 +030037#define __ex(x) __kvm_handle_fault_on_reboot(x)
38
Avi Kivity6aa8b732006-12-10 02:21:36 -080039MODULE_AUTHOR("Qumranet");
40MODULE_LICENSE("GPL");
41
Avi Kivity4462d212009-03-23 17:53:37 +020042static int __read_mostly bypass_guest_pf = 1;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020043module_param(bypass_guest_pf, bool, S_IRUGO);
Avi Kivityc7addb92007-09-16 18:58:32 +020044
Avi Kivity4462d212009-03-23 17:53:37 +020045static int __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020046module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080047
Avi Kivity4462d212009-03-23 17:53:37 +020048static int __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020049module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020050
Avi Kivity4462d212009-03-23 17:53:37 +020051static int __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020052module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080053
Nitin A Kamble3a624e22009-06-08 11:34:16 -070054static int __read_mostly enable_unrestricted_guest = 1;
55module_param_named(unrestricted_guest,
56 enable_unrestricted_guest, bool, S_IRUGO);
57
Avi Kivity4462d212009-03-23 17:53:37 +020058static int __read_mostly emulate_invalid_guest_state = 0;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020059module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030060
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040061struct vmcs {
62 u32 revision_id;
63 u32 abort;
64 char data[0];
65};
66
67struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100068 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +030069 struct list_head local_vcpus_link;
Avi Kivity313dbd42008-07-17 18:04:30 +030070 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040071 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030072 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020073 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040074 struct kvm_msr_entry *guest_msrs;
75 struct kvm_msr_entry *host_msrs;
76 int nmsrs;
77 int save_nmsrs;
78 int msr_offset_efer;
79#ifdef CONFIG_X86_64
80 int msr_offset_kernel_gs_base;
81#endif
82 struct vmcs *vmcs;
83 struct {
84 int loaded;
85 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020086 int gs_ldt_reload_needed;
87 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030088 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040089 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +020090 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +030091 int vm86_active;
92 u8 save_iopl;
93 struct kvm_save_segment {
94 u16 selector;
95 unsigned long base;
96 u32 limit;
97 u32 ar;
98 } tr, es, ds, fs, gs;
Avi Kivity9c8cba32007-11-22 11:42:59 +020099 struct {
100 bool pending;
101 u8 vector;
102 unsigned rip;
103 } irq;
104 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800105 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300106 bool emulation_required;
Avi Kivity8b3079a2009-01-05 12:10:54 +0200107 enum emulation_result invalid_state_emulation_result;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200108
109 /* Support for vnmi-less CPUs */
110 int soft_vnmi_blocked;
111 ktime_t entry_time;
112 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800113 u32 exit_reason;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400114};
115
116static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
117{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000118 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400119}
120
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800121static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800122static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300123
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124static DEFINE_PER_CPU(struct vmcs *, vmxarea);
125static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300126static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800127
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200128static unsigned long *vmx_io_bitmap_a;
129static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200130static unsigned long *vmx_msr_bitmap_legacy;
131static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300132
Sheng Yang2384d2b2008-01-17 15:14:33 +0800133static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
134static DEFINE_SPINLOCK(vmx_vpid_lock);
135
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300136static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800137 int size;
138 int order;
139 u32 revision_id;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300140 u32 pin_based_exec_ctrl;
141 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800142 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300143 u32 vmexit_ctrl;
144 u32 vmentry_ctrl;
145} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146
Hannes Ederefff9e52008-11-28 17:02:06 +0100147static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800148 u32 ept;
149 u32 vpid;
150} vmx_capability;
151
Avi Kivity6aa8b732006-12-10 02:21:36 -0800152#define VMX_SEGMENT_FIELD(seg) \
153 [VCPU_SREG_##seg] = { \
154 .selector = GUEST_##seg##_SELECTOR, \
155 .base = GUEST_##seg##_BASE, \
156 .limit = GUEST_##seg##_LIMIT, \
157 .ar_bytes = GUEST_##seg##_AR_BYTES, \
158 }
159
160static struct kvm_vmx_segment_field {
161 unsigned selector;
162 unsigned base;
163 unsigned limit;
164 unsigned ar_bytes;
165} kvm_vmx_segment_fields[] = {
166 VMX_SEGMENT_FIELD(CS),
167 VMX_SEGMENT_FIELD(DS),
168 VMX_SEGMENT_FIELD(ES),
169 VMX_SEGMENT_FIELD(FS),
170 VMX_SEGMENT_FIELD(GS),
171 VMX_SEGMENT_FIELD(SS),
172 VMX_SEGMENT_FIELD(TR),
173 VMX_SEGMENT_FIELD(LDTR),
174};
175
Avi Kivity6de4f3ad2009-05-31 22:58:47 +0300176static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
177
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300178/*
179 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
180 * away by decrementing the array size.
181 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800183#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
185#endif
186 MSR_EFER, MSR_K6_STAR,
187};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200188#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400190static void load_msrs(struct kvm_msr_entry *e, int n)
191{
192 int i;
193
194 for (i = 0; i < n; ++i)
195 wrmsrl(e[i].index, e[i].data);
196}
197
198static void save_msrs(struct kvm_msr_entry *e, int n)
199{
200 int i;
201
202 for (i = 0; i < n; ++i)
203 rdmsrl(e[i].index, e[i].data);
204}
205
Avi Kivity6aa8b732006-12-10 02:21:36 -0800206static inline int is_page_fault(u32 intr_info)
207{
208 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
209 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100210 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211}
212
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300213static inline int is_no_device(u32 intr_info)
214{
215 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
216 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100217 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300218}
219
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500220static inline int is_invalid_opcode(u32 intr_info)
221{
222 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
223 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100224 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500225}
226
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227static inline int is_external_interrupt(u32 intr_info)
228{
229 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
230 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
231}
232
Andi Kleena0861c02009-06-08 17:37:09 +0800233static inline int is_machine_check(u32 intr_info)
234{
235 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
236 INTR_INFO_VALID_MASK)) ==
237 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
238}
239
Sheng Yang25c5f222008-03-28 13:18:56 +0800240static inline int cpu_has_vmx_msr_bitmap(void)
241{
Sheng Yang04547152009-04-01 15:52:31 +0800242 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800243}
244
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800245static inline int cpu_has_vmx_tpr_shadow(void)
246{
Sheng Yang04547152009-04-01 15:52:31 +0800247 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800248}
249
250static inline int vm_need_tpr_shadow(struct kvm *kvm)
251{
Sheng Yang04547152009-04-01 15:52:31 +0800252 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800253}
254
Sheng Yangf78e0e22007-10-29 09:40:42 +0800255static inline int cpu_has_secondary_exec_ctrls(void)
256{
Sheng Yang04547152009-04-01 15:52:31 +0800257 return vmcs_config.cpu_based_exec_ctrl &
258 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800259}
260
Avi Kivity774ead32007-12-26 13:57:04 +0200261static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800262{
Sheng Yang04547152009-04-01 15:52:31 +0800263 return vmcs_config.cpu_based_2nd_exec_ctrl &
264 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
265}
266
267static inline bool cpu_has_vmx_flexpriority(void)
268{
269 return cpu_has_vmx_tpr_shadow() &&
270 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800271}
272
Marcelo Tosattie7997942009-06-11 12:07:40 -0300273static inline bool cpu_has_vmx_ept_execute_only(void)
274{
275 return !!(vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT);
276}
277
278static inline bool cpu_has_vmx_eptp_uncacheable(void)
279{
280 return !!(vmx_capability.ept & VMX_EPTP_UC_BIT);
281}
282
283static inline bool cpu_has_vmx_eptp_writeback(void)
284{
285 return !!(vmx_capability.ept & VMX_EPTP_WB_BIT);
286}
287
288static inline bool cpu_has_vmx_ept_2m_page(void)
289{
290 return !!(vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT);
291}
292
Sheng Yangd56f5462008-04-25 10:13:16 +0800293static inline int cpu_has_vmx_invept_individual_addr(void)
294{
Sheng Yang04547152009-04-01 15:52:31 +0800295 return !!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800296}
297
298static inline int cpu_has_vmx_invept_context(void)
299{
Sheng Yang04547152009-04-01 15:52:31 +0800300 return !!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800301}
302
303static inline int cpu_has_vmx_invept_global(void)
304{
Sheng Yang04547152009-04-01 15:52:31 +0800305 return !!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800306}
307
308static inline int cpu_has_vmx_ept(void)
309{
Sheng Yang04547152009-04-01 15:52:31 +0800310 return vmcs_config.cpu_based_2nd_exec_ctrl &
311 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800312}
313
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700314static inline int cpu_has_vmx_unrestricted_guest(void)
315{
316 return vmcs_config.cpu_based_2nd_exec_ctrl &
317 SECONDARY_EXEC_UNRESTRICTED_GUEST;
318}
319
Sheng Yangf78e0e22007-10-29 09:40:42 +0800320static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
321{
Sheng Yang04547152009-04-01 15:52:31 +0800322 return flexpriority_enabled &&
323 (cpu_has_vmx_virtualize_apic_accesses()) &&
324 (irqchip_in_kernel(kvm));
Sheng Yangf78e0e22007-10-29 09:40:42 +0800325}
326
Sheng Yang2384d2b2008-01-17 15:14:33 +0800327static inline int cpu_has_vmx_vpid(void)
328{
Sheng Yang04547152009-04-01 15:52:31 +0800329 return vmcs_config.cpu_based_2nd_exec_ctrl &
330 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800331}
332
Sheng Yangf08864b2008-05-15 18:23:25 +0800333static inline int cpu_has_virtual_nmis(void)
334{
335 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
336}
337
Sheng Yang04547152009-04-01 15:52:31 +0800338static inline bool report_flexpriority(void)
339{
340 return flexpriority_enabled;
341}
342
Rusty Russell8b9cf982007-07-30 16:31:43 +1000343static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800344{
345 int i;
346
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400347 for (i = 0; i < vmx->nmsrs; ++i)
348 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300349 return i;
350 return -1;
351}
352
Sheng Yang2384d2b2008-01-17 15:14:33 +0800353static inline void __invvpid(int ext, u16 vpid, gva_t gva)
354{
355 struct {
356 u64 vpid : 16;
357 u64 rsvd : 48;
358 u64 gva;
359 } operand = { vpid, 0, gva };
360
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300361 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800362 /* CF==1 or ZF==1 --> rc = -1 */
363 "; ja 1f ; ud2 ; 1:"
364 : : "a"(&operand), "c"(ext) : "cc", "memory");
365}
366
Sheng Yang14394422008-04-28 12:24:45 +0800367static inline void __invept(int ext, u64 eptp, gpa_t gpa)
368{
369 struct {
370 u64 eptp, gpa;
371 } operand = {eptp, gpa};
372
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300373 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800374 /* CF==1 or ZF==1 --> rc = -1 */
375 "; ja 1f ; ud2 ; 1:\n"
376 : : "a" (&operand), "c" (ext) : "cc", "memory");
377}
378
Rusty Russell8b9cf982007-07-30 16:31:43 +1000379static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300380{
381 int i;
382
Rusty Russell8b9cf982007-07-30 16:31:43 +1000383 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300384 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400385 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000386 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800387}
388
Avi Kivity6aa8b732006-12-10 02:21:36 -0800389static void vmcs_clear(struct vmcs *vmcs)
390{
391 u64 phys_addr = __pa(vmcs);
392 u8 error;
393
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300394 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800395 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
396 : "cc", "memory");
397 if (error)
398 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
399 vmcs, phys_addr);
400}
401
402static void __vcpu_clear(void *arg)
403{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000404 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800405 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800406
Rusty Russell8b9cf982007-07-30 16:31:43 +1000407 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400408 vmcs_clear(vmx->vmcs);
409 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800410 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800411 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300412 list_del(&vmx->local_vcpus_link);
413 vmx->vcpu.cpu = -1;
414 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800415}
416
Rusty Russell8b9cf982007-07-30 16:31:43 +1000417static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800418{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200419 if (vmx->vcpu.cpu == -1)
420 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200421 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800422}
423
Sheng Yang2384d2b2008-01-17 15:14:33 +0800424static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
425{
426 if (vmx->vpid == 0)
427 return;
428
429 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
430}
431
Sheng Yang14394422008-04-28 12:24:45 +0800432static inline void ept_sync_global(void)
433{
434 if (cpu_has_vmx_invept_global())
435 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
436}
437
438static inline void ept_sync_context(u64 eptp)
439{
Avi Kivity089d0342009-03-23 18:26:32 +0200440 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800441 if (cpu_has_vmx_invept_context())
442 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
443 else
444 ept_sync_global();
445 }
446}
447
448static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
449{
Avi Kivity089d0342009-03-23 18:26:32 +0200450 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800451 if (cpu_has_vmx_invept_individual_addr())
452 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
453 eptp, gpa);
454 else
455 ept_sync_context(eptp);
456 }
457}
458
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459static unsigned long vmcs_readl(unsigned long field)
460{
461 unsigned long value;
462
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300463 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800464 : "=a"(value) : "d"(field) : "cc");
465 return value;
466}
467
468static u16 vmcs_read16(unsigned long field)
469{
470 return vmcs_readl(field);
471}
472
473static u32 vmcs_read32(unsigned long field)
474{
475 return vmcs_readl(field);
476}
477
478static u64 vmcs_read64(unsigned long field)
479{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800480#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481 return vmcs_readl(field);
482#else
483 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
484#endif
485}
486
Avi Kivitye52de1b2007-01-05 16:36:56 -0800487static noinline void vmwrite_error(unsigned long field, unsigned long value)
488{
489 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
490 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
491 dump_stack();
492}
493
Avi Kivity6aa8b732006-12-10 02:21:36 -0800494static void vmcs_writel(unsigned long field, unsigned long value)
495{
496 u8 error;
497
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300498 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400499 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800500 if (unlikely(error))
501 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502}
503
504static void vmcs_write16(unsigned long field, u16 value)
505{
506 vmcs_writel(field, value);
507}
508
509static void vmcs_write32(unsigned long field, u32 value)
510{
511 vmcs_writel(field, value);
512}
513
514static void vmcs_write64(unsigned long field, u64 value)
515{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800516 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300517#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800518 asm volatile ("");
519 vmcs_writel(field+1, value >> 32);
520#endif
521}
522
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300523static void vmcs_clear_bits(unsigned long field, u32 mask)
524{
525 vmcs_writel(field, vmcs_readl(field) & ~mask);
526}
527
528static void vmcs_set_bits(unsigned long field, u32 mask)
529{
530 vmcs_writel(field, vmcs_readl(field) | mask);
531}
532
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300533static void update_exception_bitmap(struct kvm_vcpu *vcpu)
534{
535 u32 eb;
536
Andi Kleena0861c02009-06-08 17:37:09 +0800537 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300538 if (!vcpu->fpu_active)
539 eb |= 1u << NM_VECTOR;
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100540 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
541 if (vcpu->guest_debug &
542 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
543 eb |= 1u << DB_VECTOR;
544 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
545 eb |= 1u << BP_VECTOR;
546 }
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300547 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300548 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200549 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800550 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300551 vmcs_write32(EXCEPTION_BITMAP, eb);
552}
553
Avi Kivity33ed6322007-05-02 16:54:03 +0300554static void reload_tss(void)
555{
Avi Kivity33ed6322007-05-02 16:54:03 +0300556 /*
557 * VT restores TR but not its size. Useless.
558 */
559 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200560 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300561
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300562 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300563 descs = (void *)gdt.base;
564 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
565 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300566}
567
Rusty Russell8b9cf982007-07-30 16:31:43 +1000568static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300569{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400570 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300571 u64 host_efer = vmx->host_msrs[efer_offset].data;
572 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
573 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300574
Avi Kivity51c6cf62007-08-29 03:48:05 +0300575 if (efer_offset < 0)
576 return;
577 /*
578 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
579 * outside long mode
580 */
581 ignore_bits = EFER_NX | EFER_SCE;
582#ifdef CONFIG_X86_64
583 ignore_bits |= EFER_LMA | EFER_LME;
584 /* SCE is meaningful only in long mode on Intel */
585 if (guest_efer & EFER_LMA)
586 ignore_bits &= ~(u64)EFER_SCE;
587#endif
588 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
589 return;
590
591 vmx->host_state.guest_efer_loaded = 1;
592 guest_efer &= ~ignore_bits;
593 guest_efer |= host_efer & ignore_bits;
594 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000595 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300596}
597
Avi Kivity51c6cf62007-08-29 03:48:05 +0300598static void reload_host_efer(struct vcpu_vmx *vmx)
599{
600 if (vmx->host_state.guest_efer_loaded) {
601 vmx->host_state.guest_efer_loaded = 0;
602 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
603 }
604}
605
Avi Kivity04d2cc72007-09-10 18:10:54 +0300606static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300607{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300608 struct vcpu_vmx *vmx = to_vmx(vcpu);
609
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400610 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300611 return;
612
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400613 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300614 /*
615 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
616 * allow segment selectors with cpl > 0 or ti == 1.
617 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300618 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200619 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300620 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200621 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400622 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200623 vmx->host_state.fs_reload_needed = 0;
624 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300625 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200626 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300627 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300628 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400629 if (!(vmx->host_state.gs_sel & 7))
630 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300631 else {
632 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200633 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300634 }
635
636#ifdef CONFIG_X86_64
637 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
638 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
639#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400640 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
641 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300642#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300643
644#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400645 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400646 save_msrs(vmx->host_msrs +
647 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400648
Avi Kivity707c0872007-05-02 17:33:43 +0300649#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400650 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300651 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300652}
653
Avi Kivitya9b21b62008-06-24 11:48:49 +0300654static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300655{
Avi Kivity15ad7142007-07-11 18:17:21 +0300656 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300657
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400658 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300659 return;
660
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200661 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400662 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200663 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300664 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200665 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300666 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300667 /*
668 * If we have to reload gs, we must take care to
669 * preserve our gs base.
670 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300671 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300672 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300673#ifdef CONFIG_X86_64
674 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
675#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300676 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300677 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200678 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400679 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
680 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300681 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300682}
683
Avi Kivitya9b21b62008-06-24 11:48:49 +0300684static void vmx_load_host_state(struct vcpu_vmx *vmx)
685{
686 preempt_disable();
687 __vmx_load_host_state(vmx);
688 preempt_enable();
689}
690
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691/*
692 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
693 * vcpu mutex is already taken.
694 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300695static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800696{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400697 struct vcpu_vmx *vmx = to_vmx(vcpu);
698 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200699 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700
Eddie Donga3d7f852007-09-03 16:15:12 +0300701 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000702 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300703 kvm_migrate_timers(vcpu);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800704 vpid_sync_vcpu_all(vmx);
Avi Kivity543e4242008-05-13 16:22:47 +0300705 local_irq_disable();
706 list_add(&vmx->local_vcpus_link,
707 &per_cpu(vcpus_on_cpu, cpu));
708 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300709 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800710
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400711 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712 u8 error;
713
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400714 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300715 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
717 : "cc");
718 if (error)
719 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400720 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721 }
722
723 if (vcpu->cpu != cpu) {
724 struct descriptor_table dt;
725 unsigned long sysenter_esp;
726
727 vcpu->cpu = cpu;
728 /*
729 * Linux uses per-cpu TSS and GDT, so set these when switching
730 * processors.
731 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300732 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
733 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
735
736 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
737 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300738
739 /*
740 * Make sure the time stamp counter is monotonous.
741 */
742 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200743 if (tsc_this < vcpu->arch.host_tsc) {
744 delta = vcpu->arch.host_tsc - tsc_this;
745 new_offset = vmcs_read64(TSC_OFFSET) + delta;
746 vmcs_write64(TSC_OFFSET, new_offset);
747 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800748 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749}
750
751static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
752{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300753 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800754}
755
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300756static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
757{
758 if (vcpu->fpu_active)
759 return;
760 vcpu->fpu_active = 1;
Rusty Russell707d92f2007-07-17 23:19:08 +1000761 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800762 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92f2007-07-17 23:19:08 +1000763 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300764 update_exception_bitmap(vcpu);
765}
766
767static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
768{
769 if (!vcpu->fpu_active)
770 return;
771 vcpu->fpu_active = 0;
Rusty Russell707d92f2007-07-17 23:19:08 +1000772 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300773 update_exception_bitmap(vcpu);
774}
775
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
777{
778 return vmcs_readl(GUEST_RFLAGS);
779}
780
781static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
782{
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300783 if (to_vmx(vcpu)->rmode.vm86_active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100784 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800785 vmcs_writel(GUEST_RFLAGS, rflags);
786}
787
Glauber Costa2809f5d2009-05-12 16:21:05 -0400788static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
789{
790 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
791 int ret = 0;
792
793 if (interruptibility & GUEST_INTR_STATE_STI)
794 ret |= X86_SHADOW_INT_STI;
795 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
796 ret |= X86_SHADOW_INT_MOV_SS;
797
798 return ret & mask;
799}
800
801static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
802{
803 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
804 u32 interruptibility = interruptibility_old;
805
806 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
807
808 if (mask & X86_SHADOW_INT_MOV_SS)
809 interruptibility |= GUEST_INTR_STATE_MOV_SS;
810 if (mask & X86_SHADOW_INT_STI)
811 interruptibility |= GUEST_INTR_STATE_STI;
812
813 if ((interruptibility != interruptibility_old))
814 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
815}
816
Avi Kivity6aa8b732006-12-10 02:21:36 -0800817static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
818{
819 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800820
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300821 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300823 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824
Glauber Costa2809f5d2009-05-12 16:21:05 -0400825 /* skipping an emulated instruction also counts */
826 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827}
828
Avi Kivity298101d2007-11-25 13:41:11 +0200829static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
830 bool has_error_code, u32 error_code)
831{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200832 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100833 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200834
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100835 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200836 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100837 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
838 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200839
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300840 if (vmx->rmode.vm86_active) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200841 vmx->rmode.irq.pending = true;
842 vmx->rmode.irq.vector = nr;
843 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +0300844 if (kvm_exception_is_soft(nr))
845 vmx->rmode.irq.rip +=
846 vmx->vcpu.arch.event_exit_inst_len;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100847 intr_info |= INTR_TYPE_SOFT_INTR;
848 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200849 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
850 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
851 return;
852 }
853
Gleb Natapov66fd3f72009-05-11 13:35:50 +0300854 if (kvm_exception_is_soft(nr)) {
855 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
856 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100857 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
858 } else
859 intr_info |= INTR_TYPE_HARD_EXCEPTION;
860
861 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200862}
863
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864/*
Eddie Donga75beee2007-05-17 18:55:15 +0300865 * Swap MSR entry in host/guest MSR entry array.
866 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200867#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000868static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300869{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400870 struct kvm_msr_entry tmp;
871
872 tmp = vmx->guest_msrs[to];
873 vmx->guest_msrs[to] = vmx->guest_msrs[from];
874 vmx->guest_msrs[from] = tmp;
875 tmp = vmx->host_msrs[to];
876 vmx->host_msrs[to] = vmx->host_msrs[from];
877 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300878}
Gabriel C54e11fa2007-08-01 16:23:10 +0200879#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300880
881/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300882 * Set up the vmcs to automatically save and restore system
883 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
884 * mode, as fiddling with msrs is very expensive.
885 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000886static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300887{
Eddie Dong2cc51562007-05-21 07:28:09 +0300888 int save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +0200889 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +0300890
Avi Kivity33f9c502008-02-27 16:06:57 +0200891 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300892 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300893#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000894 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300895 int index;
896
Rusty Russell8b9cf982007-07-30 16:31:43 +1000897 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300898 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000899 move_msr_up(vmx, index, save_nmsrs++);
900 index = __find_msr_index(vmx, MSR_LSTAR);
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_CSTAR);
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_KERNEL_GS_BASE);
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++);
Eddie Donga75beee2007-05-17 18:55:15 +0300909 /*
910 * MSR_K6_STAR is only needed on long mode guests, and only
911 * if efer.sce is enabled.
912 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000913 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800914 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000915 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300916 }
Eddie Donga75beee2007-05-17 18:55:15 +0300917#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400918 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300919
Eddie Donga75beee2007-05-17 18:55:15 +0300920#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400921 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000922 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300923#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000924 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivity58972972009-02-24 22:26:47 +0200925
926 if (cpu_has_vmx_msr_bitmap()) {
927 if (is_long_mode(&vmx->vcpu))
928 msr_bitmap = vmx_msr_bitmap_longmode;
929 else
930 msr_bitmap = vmx_msr_bitmap_legacy;
931
932 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
933 }
Avi Kivitye38aea32007-04-19 13:22:48 +0300934}
935
936/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937 * reads and returns guest's timestamp counter "register"
938 * guest_tsc = host_tsc + tsc_offset -- 21.3
939 */
940static u64 guest_read_tsc(void)
941{
942 u64 host_tsc, tsc_offset;
943
944 rdtscll(host_tsc);
945 tsc_offset = vmcs_read64(TSC_OFFSET);
946 return host_tsc + tsc_offset;
947}
948
949/*
950 * writes 'guest_tsc' into guest's timestamp counter "register"
951 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
952 */
Marcelo Tosatti53f658b2008-12-11 20:45:05 +0100953static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
956}
957
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958/*
959 * Reads an msr value (of 'msr_index') into 'pdata'.
960 * Returns 0 on success, non-0 otherwise.
961 * Assumes vcpu_load() was already called.
962 */
963static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
964{
965 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400966 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967
968 if (!pdata) {
969 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
970 return -EINVAL;
971 }
972
973 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800974#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975 case MSR_FS_BASE:
976 data = vmcs_readl(GUEST_FS_BASE);
977 break;
978 case MSR_GS_BASE:
979 data = vmcs_readl(GUEST_GS_BASE);
980 break;
981 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800982 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983#endif
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530984 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 data = guest_read_tsc();
986 break;
987 case MSR_IA32_SYSENTER_CS:
988 data = vmcs_read32(GUEST_SYSENTER_CS);
989 break;
990 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200991 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 break;
993 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200994 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996 default:
Avi Kivity516a1a72009-02-15 02:32:07 +0200997 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +1000998 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800999 if (msr) {
1000 data = msr->data;
1001 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001003 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004 }
1005
1006 *pdata = data;
1007 return 0;
1008}
1009
1010/*
1011 * Writes msr value into into the appropriate "register".
1012 * Returns 0 on success, non-0 otherwise.
1013 * Assumes vcpu_load() was already called.
1014 */
1015static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1016{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001017 struct vcpu_vmx *vmx = to_vmx(vcpu);
1018 struct kvm_msr_entry *msr;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01001019 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +03001020 int ret = 0;
1021
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001023 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001024 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001025 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001026 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001027#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028 case MSR_FS_BASE:
1029 vmcs_writel(GUEST_FS_BASE, data);
1030 break;
1031 case MSR_GS_BASE:
1032 vmcs_writel(GUEST_GS_BASE, data);
1033 break;
1034#endif
1035 case MSR_IA32_SYSENTER_CS:
1036 vmcs_write32(GUEST_SYSENTER_CS, data);
1037 break;
1038 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001039 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040 break;
1041 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001042 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301044 case MSR_IA32_TSC:
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01001045 rdtscll(host_tsc);
1046 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001048 case MSR_IA32_CR_PAT:
1049 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1050 vmcs_write64(GUEST_IA32_PAT, data);
1051 vcpu->arch.pat = data;
1052 break;
1053 }
1054 /* Otherwise falls through to kvm_set_msr_common */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001055 default:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001056 vmx_load_host_state(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +10001057 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001058 if (msr) {
1059 msr->data = data;
1060 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001062 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063 }
1064
Eddie Dong2cc51562007-05-21 07:28:09 +03001065 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066}
1067
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001068static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001070 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1071 switch (reg) {
1072 case VCPU_REGS_RSP:
1073 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1074 break;
1075 case VCPU_REGS_RIP:
1076 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1077 break;
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03001078 case VCPU_EXREG_PDPTR:
1079 if (enable_ept)
1080 ept_save_pdptrs(vcpu);
1081 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001082 default:
1083 break;
1084 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085}
1086
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001087static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001089 int old_debug = vcpu->guest_debug;
1090 unsigned long flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001092 vcpu->guest_debug = dbg->control;
1093 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
1094 vcpu->guest_debug = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001096 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1097 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1098 else
1099 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1100
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001101 flags = vmcs_readl(GUEST_RFLAGS);
1102 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
1103 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1104 else if (old_debug & KVM_GUESTDBG_SINGLESTEP)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001106 vmcs_writel(GUEST_RFLAGS, flags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001108 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109
1110 return 0;
1111}
1112
1113static __init int cpu_has_kvm_support(void)
1114{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001115 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001116}
1117
1118static __init int vmx_disabled_by_bios(void)
1119{
1120 u64 msr;
1121
1122 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001123 return (msr & (FEATURE_CONTROL_LOCKED |
1124 FEATURE_CONTROL_VMXON_ENABLED))
1125 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001126 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127}
1128
Avi Kivity774c47f2007-02-12 00:54:47 -08001129static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130{
1131 int cpu = raw_smp_processor_id();
1132 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1133 u64 old;
1134
Avi Kivity543e4242008-05-13 16:22:47 +03001135 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001137 if ((old & (FEATURE_CONTROL_LOCKED |
1138 FEATURE_CONTROL_VMXON_ENABLED))
1139 != (FEATURE_CONTROL_LOCKED |
1140 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001142 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001143 FEATURE_CONTROL_LOCKED |
1144 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001145 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001146 asm volatile (ASM_VMX_VMXON_RAX
1147 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148 : "memory", "cc");
1149}
1150
Avi Kivity543e4242008-05-13 16:22:47 +03001151static void vmclear_local_vcpus(void)
1152{
1153 int cpu = raw_smp_processor_id();
1154 struct vcpu_vmx *vmx, *n;
1155
1156 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1157 local_vcpus_link)
1158 __vcpu_clear(vmx);
1159}
1160
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001161
1162/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1163 * tricks.
1164 */
1165static void kvm_cpu_vmxoff(void)
1166{
1167 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1168 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1169}
1170
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171static void hardware_disable(void *garbage)
1172{
Avi Kivity543e4242008-05-13 16:22:47 +03001173 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001174 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001175}
1176
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001177static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001178 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179{
1180 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001181 u32 ctl = ctl_min | ctl_opt;
1182
1183 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1184
1185 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1186 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1187
1188 /* Ensure minimum (required) set of control bits are supported. */
1189 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001190 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001191
1192 *result = ctl;
1193 return 0;
1194}
1195
Yang, Sheng002c7f72007-07-31 14:23:01 +03001196static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001197{
1198 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001199 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001200 u32 _pin_based_exec_control = 0;
1201 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001202 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001203 u32 _vmexit_control = 0;
1204 u32 _vmentry_control = 0;
1205
1206 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001207 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001208 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1209 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001210 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001211
1212 min = CPU_BASED_HLT_EXITING |
1213#ifdef CONFIG_X86_64
1214 CPU_BASED_CR8_LOAD_EXITING |
1215 CPU_BASED_CR8_STORE_EXITING |
1216#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001217 CPU_BASED_CR3_LOAD_EXITING |
1218 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001219 CPU_BASED_USE_IO_BITMAPS |
1220 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001221 CPU_BASED_USE_TSC_OFFSETING |
1222 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001223 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001224 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001225 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001226 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1227 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001228 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001229#ifdef CONFIG_X86_64
1230 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1231 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1232 ~CPU_BASED_CR8_STORE_EXITING;
1233#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001234 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001235 min2 = 0;
1236 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001237 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001238 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001239 SECONDARY_EXEC_ENABLE_EPT |
1240 SECONDARY_EXEC_UNRESTRICTED_GUEST;
Sheng Yangd56f5462008-04-25 10:13:16 +08001241 if (adjust_vmx_controls(min2, opt2,
1242 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001243 &_cpu_based_2nd_exec_control) < 0)
1244 return -EIO;
1245 }
1246#ifndef CONFIG_X86_64
1247 if (!(_cpu_based_2nd_exec_control &
1248 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1249 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1250#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001251 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001252 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1253 enabled */
Sheng Yangd56f5462008-04-25 10:13:16 +08001254 min &= ~(CPU_BASED_CR3_LOAD_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001255 CPU_BASED_CR3_STORE_EXITING |
1256 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001257 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1258 &_cpu_based_exec_control) < 0)
1259 return -EIO;
1260 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1261 vmx_capability.ept, vmx_capability.vpid);
1262 }
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001263
1264 min = 0;
1265#ifdef CONFIG_X86_64
1266 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1267#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001268 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001269 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1270 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001271 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001272
Sheng Yang468d4722008-10-09 16:01:55 +08001273 min = 0;
1274 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001275 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1276 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001277 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001279 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001280
1281 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1282 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001283 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001284
1285#ifdef CONFIG_X86_64
1286 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1287 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001288 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001289#endif
1290
1291 /* Require Write-Back (WB) memory type for VMCS accesses. */
1292 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001293 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001294
Yang, Sheng002c7f72007-07-31 14:23:01 +03001295 vmcs_conf->size = vmx_msr_high & 0x1fff;
1296 vmcs_conf->order = get_order(vmcs_config.size);
1297 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001298
Yang, Sheng002c7f72007-07-31 14:23:01 +03001299 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1300 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001301 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001302 vmcs_conf->vmexit_ctrl = _vmexit_control;
1303 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001304
1305 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001306}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307
1308static struct vmcs *alloc_vmcs_cpu(int cpu)
1309{
1310 int node = cpu_to_node(cpu);
1311 struct page *pages;
1312 struct vmcs *vmcs;
1313
Mel Gorman6484eb32009-06-16 15:31:54 -07001314 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001315 if (!pages)
1316 return NULL;
1317 vmcs = page_address(pages);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001318 memset(vmcs, 0, vmcs_config.size);
1319 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320 return vmcs;
1321}
1322
1323static struct vmcs *alloc_vmcs(void)
1324{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001325 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326}
1327
1328static void free_vmcs(struct vmcs *vmcs)
1329{
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001330 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331}
1332
Sam Ravnborg39959582007-06-01 00:47:13 -07001333static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001334{
1335 int cpu;
1336
1337 for_each_online_cpu(cpu)
1338 free_vmcs(per_cpu(vmxarea, cpu));
1339}
1340
Avi Kivity6aa8b732006-12-10 02:21:36 -08001341static __init int alloc_kvm_area(void)
1342{
1343 int cpu;
1344
1345 for_each_online_cpu(cpu) {
1346 struct vmcs *vmcs;
1347
1348 vmcs = alloc_vmcs_cpu(cpu);
1349 if (!vmcs) {
1350 free_kvm_area();
1351 return -ENOMEM;
1352 }
1353
1354 per_cpu(vmxarea, cpu) = vmcs;
1355 }
1356 return 0;
1357}
1358
1359static __init int hardware_setup(void)
1360{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001361 if (setup_vmcs_config(&vmcs_config) < 0)
1362 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001363
1364 if (boot_cpu_has(X86_FEATURE_NX))
1365 kvm_enable_efer_bits(EFER_NX);
1366
Sheng Yang93ba03c2009-04-01 15:52:32 +08001367 if (!cpu_has_vmx_vpid())
1368 enable_vpid = 0;
1369
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001370 if (!cpu_has_vmx_ept()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001371 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001372 enable_unrestricted_guest = 0;
1373 }
1374
1375 if (!cpu_has_vmx_unrestricted_guest())
1376 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001377
1378 if (!cpu_has_vmx_flexpriority())
1379 flexpriority_enabled = 0;
1380
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001381 if (!cpu_has_vmx_tpr_shadow())
1382 kvm_x86_ops->update_cr8_intercept = NULL;
1383
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001384 if (enable_ept && !cpu_has_vmx_ept_2m_page())
1385 kvm_disable_largepages();
1386
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387 return alloc_kvm_area();
1388}
1389
1390static __exit void hardware_unsetup(void)
1391{
1392 free_kvm_area();
1393}
1394
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1396{
1397 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1398
Avi Kivity6af11b92007-03-19 13:18:10 +02001399 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400 vmcs_write16(sf->selector, save->selector);
1401 vmcs_writel(sf->base, save->base);
1402 vmcs_write32(sf->limit, save->limit);
1403 vmcs_write32(sf->ar_bytes, save->ar);
1404 } else {
1405 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1406 << AR_DPL_SHIFT;
1407 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1408 }
1409}
1410
1411static void enter_pmode(struct kvm_vcpu *vcpu)
1412{
1413 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001414 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001415
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001416 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001417 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001419 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
1420 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
1421 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001422
1423 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001424 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001425 flags |= (vmx->rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426 vmcs_writel(GUEST_RFLAGS, flags);
1427
Rusty Russell66aee912007-07-17 23:34:16 +10001428 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1429 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001430
1431 update_exception_bitmap(vcpu);
1432
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001433 if (emulate_invalid_guest_state)
1434 return;
1435
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001436 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
1437 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
1438 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
1439 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440
1441 vmcs_write16(GUEST_SS_SELECTOR, 0);
1442 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1443
1444 vmcs_write16(GUEST_CS_SELECTOR,
1445 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1446 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1447}
1448
Mike Dayd77c26f2007-10-08 09:02:08 -04001449static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001451 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001452 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1453 kvm->memslots[0].npages - 3;
1454 return base_gfn << PAGE_SHIFT;
1455 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001456 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457}
1458
1459static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1460{
1461 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1462
1463 save->selector = vmcs_read16(sf->selector);
1464 save->base = vmcs_readl(sf->base);
1465 save->limit = vmcs_read32(sf->limit);
1466 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001467 vmcs_write16(sf->selector, save->base >> 4);
1468 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001469 vmcs_write32(sf->limit, 0xffff);
1470 vmcs_write32(sf->ar_bytes, 0xf3);
1471}
1472
1473static void enter_rmode(struct kvm_vcpu *vcpu)
1474{
1475 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001476 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001477
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001478 if (enable_unrestricted_guest)
1479 return;
1480
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001481 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001482 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001484 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1486
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001487 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1489
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001490 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1492
1493 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001494 vmx->rmode.save_iopl
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001495 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001497 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498
1499 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001500 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501 update_exception_bitmap(vcpu);
1502
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001503 if (emulate_invalid_guest_state)
1504 goto continue_rmode;
1505
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1507 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1508 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1509
1510 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001511 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001512 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1513 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1515
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001516 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
1517 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
1518 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
1519 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001520
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001521continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001522 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001523 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524}
1525
Amit Shah401d10d2009-02-20 22:53:37 +05301526static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1527{
1528 struct vcpu_vmx *vmx = to_vmx(vcpu);
1529 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1530
1531 vcpu->arch.shadow_efer = efer;
1532 if (!msr)
1533 return;
1534 if (efer & EFER_LMA) {
1535 vmcs_write32(VM_ENTRY_CONTROLS,
1536 vmcs_read32(VM_ENTRY_CONTROLS) |
1537 VM_ENTRY_IA32E_MODE);
1538 msr->data = efer;
1539 } else {
1540 vmcs_write32(VM_ENTRY_CONTROLS,
1541 vmcs_read32(VM_ENTRY_CONTROLS) &
1542 ~VM_ENTRY_IA32E_MODE);
1543
1544 msr->data = efer & ~EFER_LME;
1545 }
1546 setup_msrs(vmx);
1547}
1548
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001549#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001550
1551static void enter_lmode(struct kvm_vcpu *vcpu)
1552{
1553 u32 guest_tr_ar;
1554
1555 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1556 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1557 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001558 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001559 vmcs_write32(GUEST_TR_AR_BYTES,
1560 (guest_tr_ar & ~AR_TYPE_MASK)
1561 | AR_TYPE_BUSY_64_TSS);
1562 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001563 vcpu->arch.shadow_efer |= EFER_LMA;
Amit Shah401d10d2009-02-20 22:53:37 +05301564 vmx_set_efer(vcpu, vcpu->arch.shadow_efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001565}
1566
1567static void exit_lmode(struct kvm_vcpu *vcpu)
1568{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001569 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001570
1571 vmcs_write32(VM_ENTRY_CONTROLS,
1572 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001573 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001574}
1575
1576#endif
1577
Sheng Yang2384d2b2008-01-17 15:14:33 +08001578static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1579{
1580 vpid_sync_vcpu_all(to_vmx(vcpu));
Avi Kivity089d0342009-03-23 18:26:32 +02001581 if (enable_ept)
Sheng Yang4e1096d2008-07-06 19:16:51 +08001582 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001583}
1584
Anthony Liguori25c4c272007-04-27 09:29:21 +03001585static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001586{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001587 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1588 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001589}
1590
Sheng Yang14394422008-04-28 12:24:45 +08001591static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1592{
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03001593 if (!test_bit(VCPU_EXREG_PDPTR,
1594 (unsigned long *)&vcpu->arch.regs_dirty))
1595 return;
1596
Sheng Yang14394422008-04-28 12:24:45 +08001597 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Sheng Yang14394422008-04-28 12:24:45 +08001598 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1599 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1600 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1601 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1602 }
1603}
1604
Avi Kivity8f5d5492009-05-31 18:41:29 +03001605static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
1606{
1607 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1608 vcpu->arch.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
1609 vcpu->arch.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
1610 vcpu->arch.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
1611 vcpu->arch.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
1612 }
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03001613
1614 __set_bit(VCPU_EXREG_PDPTR,
1615 (unsigned long *)&vcpu->arch.regs_avail);
1616 __set_bit(VCPU_EXREG_PDPTR,
1617 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03001618}
1619
Sheng Yang14394422008-04-28 12:24:45 +08001620static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1621
1622static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1623 unsigned long cr0,
1624 struct kvm_vcpu *vcpu)
1625{
1626 if (!(cr0 & X86_CR0_PG)) {
1627 /* From paging/starting to nonpaging */
1628 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001629 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001630 (CPU_BASED_CR3_LOAD_EXITING |
1631 CPU_BASED_CR3_STORE_EXITING));
1632 vcpu->arch.cr0 = cr0;
1633 vmx_set_cr4(vcpu, vcpu->arch.cr4);
Sheng Yang14394422008-04-28 12:24:45 +08001634 *hw_cr0 &= ~X86_CR0_WP;
1635 } else if (!is_paging(vcpu)) {
1636 /* From nonpaging to paging */
1637 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001638 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001639 ~(CPU_BASED_CR3_LOAD_EXITING |
1640 CPU_BASED_CR3_STORE_EXITING));
1641 vcpu->arch.cr0 = cr0;
1642 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1643 if (!(vcpu->arch.cr0 & X86_CR0_WP))
1644 *hw_cr0 &= ~X86_CR0_WP;
1645 }
1646}
1647
1648static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1649 struct kvm_vcpu *vcpu)
1650{
1651 if (!is_paging(vcpu)) {
1652 *hw_cr4 &= ~X86_CR4_PAE;
1653 *hw_cr4 |= X86_CR4_PSE;
1654 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1655 *hw_cr4 &= ~X86_CR4_PAE;
1656}
1657
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1659{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001660 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001661 unsigned long hw_cr0;
1662
1663 if (enable_unrestricted_guest)
1664 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
1665 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
1666 else
1667 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08001668
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001669 vmx_fpu_deactivate(vcpu);
1670
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001671 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001672 enter_pmode(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_rmode(vcpu);
1676
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001677#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001678 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001679 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10001681 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001682 exit_lmode(vcpu);
1683 }
1684#endif
1685
Avi Kivity089d0342009-03-23 18:26:32 +02001686 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001687 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1688
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001690 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001691 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001692
Rusty Russell707d92f2007-07-17 23:19:08 +10001693 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001694 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001695}
1696
Sheng Yang14394422008-04-28 12:24:45 +08001697static u64 construct_eptp(unsigned long root_hpa)
1698{
1699 u64 eptp;
1700
1701 /* TODO write the value reading from MSR */
1702 eptp = VMX_EPT_DEFAULT_MT |
1703 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1704 eptp |= (root_hpa & PAGE_MASK);
1705
1706 return eptp;
1707}
1708
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1710{
Sheng Yang14394422008-04-28 12:24:45 +08001711 unsigned long guest_cr3;
1712 u64 eptp;
1713
1714 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02001715 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001716 eptp = construct_eptp(cr3);
1717 vmcs_write64(EPT_POINTER, eptp);
Sheng Yang14394422008-04-28 12:24:45 +08001718 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
1719 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1720 }
1721
Sheng Yang2384d2b2008-01-17 15:14:33 +08001722 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001723 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001724 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001725 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726}
1727
1728static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1729{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001730 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08001731 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1732
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001733 vcpu->arch.cr4 = cr4;
Avi Kivity089d0342009-03-23 18:26:32 +02001734 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001735 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1736
1737 vmcs_writel(CR4_READ_SHADOW, cr4);
1738 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001739}
1740
Avi Kivity6aa8b732006-12-10 02:21:36 -08001741static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1742{
1743 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1744
1745 return vmcs_readl(sf->base);
1746}
1747
1748static void vmx_get_segment(struct kvm_vcpu *vcpu,
1749 struct kvm_segment *var, int seg)
1750{
1751 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1752 u32 ar;
1753
1754 var->base = vmcs_readl(sf->base);
1755 var->limit = vmcs_read32(sf->limit);
1756 var->selector = vmcs_read16(sf->selector);
1757 ar = vmcs_read32(sf->ar_bytes);
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02001758 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001759 ar = 0;
1760 var->type = ar & 15;
1761 var->s = (ar >> 4) & 1;
1762 var->dpl = (ar >> 5) & 3;
1763 var->present = (ar >> 7) & 1;
1764 var->avl = (ar >> 12) & 1;
1765 var->l = (ar >> 13) & 1;
1766 var->db = (ar >> 14) & 1;
1767 var->g = (ar >> 15) & 1;
1768 var->unusable = (ar >> 16) & 1;
1769}
1770
Izik Eidus2e4d2652008-03-24 19:38:34 +02001771static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1772{
1773 struct kvm_segment kvm_seg;
1774
1775 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1776 return 0;
1777
1778 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1779 return 3;
1780
1781 vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
1782 return kvm_seg.selector & 3;
1783}
1784
Avi Kivity653e3102007-05-07 10:55:37 +03001785static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001786{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001787 u32 ar;
1788
Avi Kivity653e3102007-05-07 10:55:37 +03001789 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790 ar = 1 << 16;
1791 else {
1792 ar = var->type & 15;
1793 ar |= (var->s & 1) << 4;
1794 ar |= (var->dpl & 3) << 5;
1795 ar |= (var->present & 1) << 7;
1796 ar |= (var->avl & 1) << 12;
1797 ar |= (var->l & 1) << 13;
1798 ar |= (var->db & 1) << 14;
1799 ar |= (var->g & 1) << 15;
1800 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001801 if (ar == 0) /* a 0 value means unusable */
1802 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001803
1804 return ar;
1805}
1806
1807static void vmx_set_segment(struct kvm_vcpu *vcpu,
1808 struct kvm_segment *var, int seg)
1809{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001810 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03001811 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1812 u32 ar;
1813
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001814 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
1815 vmx->rmode.tr.selector = var->selector;
1816 vmx->rmode.tr.base = var->base;
1817 vmx->rmode.tr.limit = var->limit;
1818 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001819 return;
1820 }
1821 vmcs_writel(sf->base, var->base);
1822 vmcs_write32(sf->limit, var->limit);
1823 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001824 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001825 /*
1826 * Hack real-mode segments into vm86 compatibility.
1827 */
1828 if (var->base == 0xffff0000 && var->selector == 0xf000)
1829 vmcs_writel(sf->base, 0xf0000);
1830 ar = 0xf3;
1831 } else
1832 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001833
1834 /*
1835 * Fix the "Accessed" bit in AR field of segment registers for older
1836 * qemu binaries.
1837 * IA32 arch specifies that at the time of processor reset the
1838 * "Accessed" bit in the AR field of segment registers is 1. And qemu
1839 * is setting it to 0 in the usedland code. This causes invalid guest
1840 * state vmexit when "unrestricted guest" mode is turned on.
1841 * Fix for this setup issue in cpu_reset is being pushed in the qemu
1842 * tree. Newer qemu binaries with that qemu fix would not need this
1843 * kvm hack.
1844 */
1845 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
1846 ar |= 0x1; /* Accessed */
1847
Avi Kivity6aa8b732006-12-10 02:21:36 -08001848 vmcs_write32(sf->ar_bytes, ar);
1849}
1850
Avi Kivity6aa8b732006-12-10 02:21:36 -08001851static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1852{
1853 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1854
1855 *db = (ar >> 14) & 1;
1856 *l = (ar >> 13) & 1;
1857}
1858
1859static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1860{
1861 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1862 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1863}
1864
1865static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1866{
1867 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1868 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1869}
1870
1871static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1872{
1873 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1874 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1875}
1876
1877static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1878{
1879 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1880 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1881}
1882
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001883static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1884{
1885 struct kvm_segment var;
1886 u32 ar;
1887
1888 vmx_get_segment(vcpu, &var, seg);
1889 ar = vmx_segment_access_rights(&var);
1890
1891 if (var.base != (var.selector << 4))
1892 return false;
1893 if (var.limit != 0xffff)
1894 return false;
1895 if (ar != 0xf3)
1896 return false;
1897
1898 return true;
1899}
1900
1901static bool code_segment_valid(struct kvm_vcpu *vcpu)
1902{
1903 struct kvm_segment cs;
1904 unsigned int cs_rpl;
1905
1906 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1907 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1908
Avi Kivity1872a3f2009-01-04 23:26:52 +02001909 if (cs.unusable)
1910 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001911 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1912 return false;
1913 if (!cs.s)
1914 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001915 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001916 if (cs.dpl > cs_rpl)
1917 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001918 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001919 if (cs.dpl != cs_rpl)
1920 return false;
1921 }
1922 if (!cs.present)
1923 return false;
1924
1925 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1926 return true;
1927}
1928
1929static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1930{
1931 struct kvm_segment ss;
1932 unsigned int ss_rpl;
1933
1934 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1935 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1936
Avi Kivity1872a3f2009-01-04 23:26:52 +02001937 if (ss.unusable)
1938 return true;
1939 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001940 return false;
1941 if (!ss.s)
1942 return false;
1943 if (ss.dpl != ss_rpl) /* DPL != RPL */
1944 return false;
1945 if (!ss.present)
1946 return false;
1947
1948 return true;
1949}
1950
1951static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1952{
1953 struct kvm_segment var;
1954 unsigned int rpl;
1955
1956 vmx_get_segment(vcpu, &var, seg);
1957 rpl = var.selector & SELECTOR_RPL_MASK;
1958
Avi Kivity1872a3f2009-01-04 23:26:52 +02001959 if (var.unusable)
1960 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001961 if (!var.s)
1962 return false;
1963 if (!var.present)
1964 return false;
1965 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
1966 if (var.dpl < rpl) /* DPL < RPL */
1967 return false;
1968 }
1969
1970 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1971 * rights flags
1972 */
1973 return true;
1974}
1975
1976static bool tr_valid(struct kvm_vcpu *vcpu)
1977{
1978 struct kvm_segment tr;
1979
1980 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
1981
Avi Kivity1872a3f2009-01-04 23:26:52 +02001982 if (tr.unusable)
1983 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001984 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1985 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001986 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001987 return false;
1988 if (!tr.present)
1989 return false;
1990
1991 return true;
1992}
1993
1994static bool ldtr_valid(struct kvm_vcpu *vcpu)
1995{
1996 struct kvm_segment ldtr;
1997
1998 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
1999
Avi Kivity1872a3f2009-01-04 23:26:52 +02002000 if (ldtr.unusable)
2001 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002002 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2003 return false;
2004 if (ldtr.type != 2)
2005 return false;
2006 if (!ldtr.present)
2007 return false;
2008
2009 return true;
2010}
2011
2012static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2013{
2014 struct kvm_segment cs, ss;
2015
2016 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2017 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2018
2019 return ((cs.selector & SELECTOR_RPL_MASK) ==
2020 (ss.selector & SELECTOR_RPL_MASK));
2021}
2022
2023/*
2024 * Check if guest state is valid. Returns true if valid, false if
2025 * not.
2026 * We assume that registers are always usable
2027 */
2028static bool guest_state_valid(struct kvm_vcpu *vcpu)
2029{
2030 /* real mode guest state checks */
2031 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
2032 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2033 return false;
2034 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2035 return false;
2036 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2037 return false;
2038 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2039 return false;
2040 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2041 return false;
2042 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2043 return false;
2044 } else {
2045 /* protected mode guest state checks */
2046 if (!cs_ss_rpl_check(vcpu))
2047 return false;
2048 if (!code_segment_valid(vcpu))
2049 return false;
2050 if (!stack_segment_valid(vcpu))
2051 return false;
2052 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2053 return false;
2054 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2055 return false;
2056 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2057 return false;
2058 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2059 return false;
2060 if (!tr_valid(vcpu))
2061 return false;
2062 if (!ldtr_valid(vcpu))
2063 return false;
2064 }
2065 /* TODO:
2066 * - Add checks on RIP
2067 * - Add checks on RFLAGS
2068 */
2069
2070 return true;
2071}
2072
Mike Dayd77c26f2007-10-08 09:02:08 -04002073static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002074{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002075 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002076 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002077 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02002078 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002079
Izik Eidus195aefd2007-10-01 22:14:18 +02002080 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2081 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002082 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002083 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002084 r = kvm_write_guest_page(kvm, fn++, &data,
2085 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002086 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002087 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002088 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2089 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002090 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002091 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 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002095 r = kvm_write_guest_page(kvm, fn, &data,
2096 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2097 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002098 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002099 goto out;
2100
2101 ret = 1;
2102out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002103 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104}
2105
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002106static int init_rmode_identity_map(struct kvm *kvm)
2107{
2108 int i, r, ret;
2109 pfn_t identity_map_pfn;
2110 u32 tmp;
2111
Avi Kivity089d0342009-03-23 18:26:32 +02002112 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002113 return 1;
2114 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2115 printk(KERN_ERR "EPT: identity-mapping pagetable "
2116 "haven't been allocated!\n");
2117 return 0;
2118 }
2119 if (likely(kvm->arch.ept_identity_pagetable_done))
2120 return 1;
2121 ret = 0;
2122 identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT;
2123 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2124 if (r < 0)
2125 goto out;
2126 /* Set up identity-mapping pagetable for EPT in real mode */
2127 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2128 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2129 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2130 r = kvm_write_guest_page(kvm, identity_map_pfn,
2131 &tmp, i * sizeof(tmp), sizeof(tmp));
2132 if (r < 0)
2133 goto out;
2134 }
2135 kvm->arch.ept_identity_pagetable_done = true;
2136 ret = 1;
2137out:
2138 return ret;
2139}
2140
Avi Kivity6aa8b732006-12-10 02:21:36 -08002141static void seg_setup(int seg)
2142{
2143 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002144 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002145
2146 vmcs_write16(sf->selector, 0);
2147 vmcs_writel(sf->base, 0);
2148 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002149 if (enable_unrestricted_guest) {
2150 ar = 0x93;
2151 if (seg == VCPU_SREG_CS)
2152 ar |= 0x08; /* code segment */
2153 } else
2154 ar = 0xf3;
2155
2156 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002157}
2158
Sheng Yangf78e0e22007-10-29 09:40:42 +08002159static int alloc_apic_access_page(struct kvm *kvm)
2160{
2161 struct kvm_userspace_memory_region kvm_userspace_mem;
2162 int r = 0;
2163
Izik Eidus72dc67a2008-02-10 18:04:15 +02002164 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002165 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002166 goto out;
2167 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2168 kvm_userspace_mem.flags = 0;
2169 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2170 kvm_userspace_mem.memory_size = PAGE_SIZE;
2171 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2172 if (r)
2173 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002174
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002175 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002176out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002177 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002178 return r;
2179}
2180
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002181static int alloc_identity_pagetable(struct kvm *kvm)
2182{
2183 struct kvm_userspace_memory_region kvm_userspace_mem;
2184 int r = 0;
2185
2186 down_write(&kvm->slots_lock);
2187 if (kvm->arch.ept_identity_pagetable)
2188 goto out;
2189 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2190 kvm_userspace_mem.flags = 0;
2191 kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
2192 kvm_userspace_mem.memory_size = PAGE_SIZE;
2193 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2194 if (r)
2195 goto out;
2196
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002197 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
2198 VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002199out:
2200 up_write(&kvm->slots_lock);
2201 return r;
2202}
2203
Sheng Yang2384d2b2008-01-17 15:14:33 +08002204static void allocate_vpid(struct vcpu_vmx *vmx)
2205{
2206 int vpid;
2207
2208 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002209 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002210 return;
2211 spin_lock(&vmx_vpid_lock);
2212 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2213 if (vpid < VMX_NR_VPIDS) {
2214 vmx->vpid = vpid;
2215 __set_bit(vpid, vmx_vpid_bitmap);
2216 }
2217 spin_unlock(&vmx_vpid_lock);
2218}
2219
Avi Kivity58972972009-02-24 22:26:47 +02002220static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002221{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002222 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002223
2224 if (!cpu_has_vmx_msr_bitmap())
2225 return;
2226
2227 /*
2228 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2229 * have the write-low and read-high bitmap offsets the wrong way round.
2230 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2231 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002232 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002233 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2234 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002235 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2236 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002237 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2238 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002239 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002240}
2241
Avi Kivity58972972009-02-24 22:26:47 +02002242static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2243{
2244 if (!longmode_only)
2245 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2246 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2247}
2248
Avi Kivity6aa8b732006-12-10 02:21:36 -08002249/*
2250 * Sets up the vmcs for emulated real mode.
2251 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002252static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253{
Sheng Yang468d4722008-10-09 16:01:55 +08002254 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255 u32 junk;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002256 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257 unsigned long a;
2258 struct descriptor_table dt;
2259 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002260 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002261 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262
Avi Kivity6aa8b732006-12-10 02:21:36 -08002263 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002264 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2265 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266
Sheng Yang25c5f222008-03-28 13:18:56 +08002267 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002268 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002269
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2271
Avi Kivity6aa8b732006-12-10 02:21:36 -08002272 /* Control */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002273 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2274 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002275
2276 exec_control = vmcs_config.cpu_based_exec_ctrl;
2277 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2278 exec_control &= ~CPU_BASED_TPR_SHADOW;
2279#ifdef CONFIG_X86_64
2280 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2281 CPU_BASED_CR8_LOAD_EXITING;
2282#endif
2283 }
Avi Kivity089d0342009-03-23 18:26:32 +02002284 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002285 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002286 CPU_BASED_CR3_LOAD_EXITING |
2287 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002288 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289
Sheng Yang83ff3b92007-11-21 14:33:25 +08002290 if (cpu_has_secondary_exec_ctrls()) {
2291 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2292 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2293 exec_control &=
2294 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002295 if (vmx->vpid == 0)
2296 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Avi Kivity089d0342009-03-23 18:26:32 +02002297 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002298 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002299 if (!enable_unrestricted_guest)
2300 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002301 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2302 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002303
Avi Kivityc7addb92007-09-16 18:58:32 +02002304 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2305 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002306 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2307
2308 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2309 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2310 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2311
2312 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2313 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2314 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002315 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2316 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002317 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002318#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002319 rdmsrl(MSR_FS_BASE, a);
2320 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2321 rdmsrl(MSR_GS_BASE, a);
2322 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2323#else
2324 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2325 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2326#endif
2327
2328 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2329
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002330 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2332
Mike Dayd77c26f2007-10-08 09:02:08 -04002333 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002334 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002335 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2336 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2337 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002338
2339 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2340 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2341 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2342 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2343 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2344 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2345
Sheng Yang468d4722008-10-09 16:01:55 +08002346 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2347 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2348 host_pat = msr_low | ((u64) msr_high << 32);
2349 vmcs_write64(HOST_IA32_PAT, host_pat);
2350 }
2351 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2352 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2353 host_pat = msr_low | ((u64) msr_high << 32);
2354 /* Write the default value follow host pat */
2355 vmcs_write64(GUEST_IA32_PAT, host_pat);
2356 /* Keep arch.pat sync with GUEST_IA32_PAT */
2357 vmx->vcpu.arch.pat = host_pat;
2358 }
2359
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360 for (i = 0; i < NR_VMX_MSR; ++i) {
2361 u32 index = vmx_msr_index[i];
2362 u32 data_low, data_high;
2363 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002364 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002365
2366 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2367 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002368 if (wrmsr_safe(index, data_low, data_high) < 0)
2369 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002371 vmx->host_msrs[j].index = index;
2372 vmx->host_msrs[j].reserved = 0;
2373 vmx->host_msrs[j].data = data;
2374 vmx->guest_msrs[j] = vmx->host_msrs[j];
2375 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002377
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002378 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379
2380 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002381 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2382
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002383 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2384 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2385
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002386 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2387 rdtscll(tsc_this);
2388 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2389 tsc_base = tsc_this;
2390
2391 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002392
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002393 return 0;
2394}
2395
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002396static int init_rmode(struct kvm *kvm)
2397{
2398 if (!init_rmode_tss(kvm))
2399 return 0;
2400 if (!init_rmode_identity_map(kvm))
2401 return 0;
2402 return 1;
2403}
2404
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002405static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2406{
2407 struct vcpu_vmx *vmx = to_vmx(vcpu);
2408 u64 msr;
2409 int ret;
2410
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002411 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002412 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002413 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002414 ret = -ENOMEM;
2415 goto out;
2416 }
2417
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002418 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002419
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002420 vmx->soft_vnmi_blocked = 0;
2421
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002422 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002423 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002424 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002425 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002426 msr |= MSR_IA32_APICBASE_BSP;
2427 kvm_set_apic_base(&vmx->vcpu, msr);
2428
2429 fx_init(&vmx->vcpu);
2430
Avi Kivity5706be02008-08-20 15:07:31 +03002431 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002432 /*
2433 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2434 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2435 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002436 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002437 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2438 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2439 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002440 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2441 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002442 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002443
2444 seg_setup(VCPU_SREG_DS);
2445 seg_setup(VCPU_SREG_ES);
2446 seg_setup(VCPU_SREG_FS);
2447 seg_setup(VCPU_SREG_GS);
2448 seg_setup(VCPU_SREG_SS);
2449
2450 vmcs_write16(GUEST_TR_SELECTOR, 0);
2451 vmcs_writel(GUEST_TR_BASE, 0);
2452 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2453 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2454
2455 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2456 vmcs_writel(GUEST_LDTR_BASE, 0);
2457 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2458 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2459
2460 vmcs_write32(GUEST_SYSENTER_CS, 0);
2461 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2462 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2463
2464 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002465 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002466 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002467 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002468 kvm_rip_write(vcpu, 0);
2469 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002470
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002471 vmcs_writel(GUEST_DR7, 0x400);
2472
2473 vmcs_writel(GUEST_GDTR_BASE, 0);
2474 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2475
2476 vmcs_writel(GUEST_IDTR_BASE, 0);
2477 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2478
2479 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2480 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2481 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2482
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002483 /* Special registers */
2484 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2485
2486 setup_msrs(vmx);
2487
Avi Kivity6aa8b732006-12-10 02:21:36 -08002488 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2489
Sheng Yangf78e0e22007-10-29 09:40:42 +08002490 if (cpu_has_vmx_tpr_shadow()) {
2491 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2492 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2493 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002494 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002495 vmcs_write32(TPR_THRESHOLD, 0);
2496 }
2497
2498 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2499 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002500 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501
Sheng Yang2384d2b2008-01-17 15:14:33 +08002502 if (vmx->vpid != 0)
2503 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2504
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002505 vmx->vcpu.arch.cr0 = 0x60000010;
2506 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002507 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002508 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002509 vmx_fpu_activate(&vmx->vcpu);
2510 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511
Sheng Yang2384d2b2008-01-17 15:14:33 +08002512 vpid_sync_vcpu_all(vmx);
2513
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002514 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002516 /* HACK: Don't enable emulation on guest boot/reset */
2517 vmx->emulation_required = 0;
2518
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002520 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521 return ret;
2522}
2523
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002524static void enable_irq_window(struct kvm_vcpu *vcpu)
2525{
2526 u32 cpu_based_vm_exec_control;
2527
2528 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2529 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2530 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2531}
2532
2533static void enable_nmi_window(struct kvm_vcpu *vcpu)
2534{
2535 u32 cpu_based_vm_exec_control;
2536
2537 if (!cpu_has_virtual_nmis()) {
2538 enable_irq_window(vcpu);
2539 return;
2540 }
2541
2542 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2543 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2544 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2545}
2546
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002547static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03002548{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002549 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002550 uint32_t intr;
2551 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02002552
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002553 KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler);
2554
Avi Kivityfa89a812008-09-01 15:57:51 +03002555 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002556 if (vmx->rmode.vm86_active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002557 vmx->rmode.irq.pending = true;
2558 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002559 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +03002560 if (vcpu->arch.interrupt.soft)
2561 vmx->rmode.irq.rip +=
2562 vmx->vcpu.arch.event_exit_inst_len;
Avi Kivity9c5623e2007-11-08 18:19:20 +02002563 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2564 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2565 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002566 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002567 return;
2568 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002569 intr = irq | INTR_INFO_VALID_MASK;
2570 if (vcpu->arch.interrupt.soft) {
2571 intr |= INTR_TYPE_SOFT_INTR;
2572 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2573 vmx->vcpu.arch.event_exit_inst_len);
2574 } else
2575 intr |= INTR_TYPE_EXT_INTR;
2576 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03002577}
2578
Sheng Yangf08864b2008-05-15 18:23:25 +08002579static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2580{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002581 struct vcpu_vmx *vmx = to_vmx(vcpu);
2582
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002583 if (!cpu_has_virtual_nmis()) {
2584 /*
2585 * Tracking the NMI-blocked state in software is built upon
2586 * finding the next open IRQ window. This, in turn, depends on
2587 * well-behaving guests: They have to keep IRQs disabled at
2588 * least as long as the NMI handler runs. Otherwise we may
2589 * cause NMI nesting, maybe breaking the guest. But as this is
2590 * highly unlikely, we can live with the residual risk.
2591 */
2592 vmx->soft_vnmi_blocked = 1;
2593 vmx->vnmi_blocked_time = 0;
2594 }
2595
Jan Kiszka487b3912008-09-26 09:30:56 +02002596 ++vcpu->stat.nmi_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002597 if (vmx->rmode.vm86_active) {
Jan Kiszka66a5a342008-09-26 09:30:51 +02002598 vmx->rmode.irq.pending = true;
2599 vmx->rmode.irq.vector = NMI_VECTOR;
2600 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2601 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2602 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2603 INTR_INFO_VALID_MASK);
2604 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2605 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2606 return;
2607 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002608 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2609 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002610}
2611
Gleb Natapovc4282df2009-04-21 17:45:07 +03002612static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02002613{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002614 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03002615 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002616
Gleb Natapovc4282df2009-04-21 17:45:07 +03002617 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2618 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS |
2619 GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02002620}
2621
Gleb Natapov78646122009-03-23 12:12:11 +02002622static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
2623{
Gleb Natapovc4282df2009-04-21 17:45:07 +03002624 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2625 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2626 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02002627}
2628
Izik Eiduscbc94022007-10-25 00:29:55 +02002629static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2630{
2631 int ret;
2632 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002633 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002634 .guest_phys_addr = addr,
2635 .memory_size = PAGE_SIZE * 3,
2636 .flags = 0,
2637 };
2638
2639 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2640 if (ret)
2641 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002642 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002643 return 0;
2644}
2645
Avi Kivity6aa8b732006-12-10 02:21:36 -08002646static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2647 int vec, u32 err_code)
2648{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002649 /*
2650 * Instruction with address size override prefix opcode 0x67
2651 * Cause the #SS fault with 0 error code in VM86 mode.
2652 */
2653 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02002654 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002655 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002656 /*
2657 * Forward all other exceptions that are valid in real mode.
2658 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2659 * the required debugging infrastructure rework.
2660 */
2661 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002662 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002663 if (vcpu->guest_debug &
2664 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2665 return 0;
2666 kvm_queue_exception(vcpu, vec);
2667 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002668 case BP_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002669 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2670 return 0;
2671 /* fall through */
2672 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002673 case OF_VECTOR:
2674 case BR_VECTOR:
2675 case UD_VECTOR:
2676 case DF_VECTOR:
2677 case SS_VECTOR:
2678 case GP_VECTOR:
2679 case MF_VECTOR:
2680 kvm_queue_exception(vcpu, vec);
2681 return 1;
2682 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002683 return 0;
2684}
2685
Andi Kleena0861c02009-06-08 17:37:09 +08002686/*
2687 * Trigger machine check on the host. We assume all the MSRs are already set up
2688 * by the CPU and that we still run on the same CPU as the MCE occurred on.
2689 * We pass a fake environment to the machine check handler because we want
2690 * the guest to be always treated like user space, no matter what context
2691 * it used internally.
2692 */
2693static void kvm_machine_check(void)
2694{
2695#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
2696 struct pt_regs regs = {
2697 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
2698 .flags = X86_EFLAGS_IF,
2699 };
2700
2701 do_machine_check(&regs, 0);
2702#endif
2703}
2704
2705static int handle_machine_check(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2706{
2707 /* already handled by vcpu_run */
2708 return 1;
2709}
2710
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2712{
Avi Kivity1155f762007-11-22 11:30:47 +02002713 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002714 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002715 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002716 u32 vect_info;
2717 enum emulation_result er;
2718
Avi Kivity1155f762007-11-22 11:30:47 +02002719 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002720 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2721
Andi Kleena0861c02009-06-08 17:37:09 +08002722 if (is_machine_check(intr_info))
2723 return handle_machine_check(vcpu, kvm_run);
2724
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002726 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002728 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002729
Jan Kiszkae4a41882008-09-26 09:30:46 +02002730 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002731 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002732
2733 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002734 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002735 return 1;
2736 }
2737
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002738 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08002739 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002740 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002741 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002742 return 1;
2743 }
2744
Avi Kivity6aa8b732006-12-10 02:21:36 -08002745 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002746 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002747 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2749 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002750 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02002751 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002752 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002754 KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2,
2755 (u32)((u64)cr2 >> 32), handler);
Gleb Natapov3298b752009-05-11 13:35:46 +03002756 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03002757 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002758 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002759 }
2760
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002761 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002763 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002764 if (vcpu->arch.halt_request) {
2765 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002766 return kvm_emulate_halt(vcpu);
2767 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002768 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002769 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002771 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002772 switch (ex_no) {
2773 case DB_VECTOR:
2774 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2775 if (!(vcpu->guest_debug &
2776 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2777 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2778 kvm_queue_exception(vcpu, DB_VECTOR);
2779 return 1;
2780 }
2781 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2782 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2783 /* fall through */
2784 case BP_VECTOR:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002786 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2787 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002788 break;
2789 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002790 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2791 kvm_run->ex.exception = ex_no;
2792 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002793 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002794 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002795 return 0;
2796}
2797
2798static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2799 struct kvm_run *kvm_run)
2800{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002801 ++vcpu->stat.irq_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002802 KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002803 return 1;
2804}
2805
Avi Kivity988ad742007-02-12 00:54:36 -08002806static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2807{
2808 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2809 return 0;
2810}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2813{
He, Qingbfdaab02007-09-12 14:18:28 +08002814 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01002815 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002816 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817
Avi Kivity1165f5f2007-04-19 17:27:43 +03002818 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002819 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002820 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002821
2822 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002823 if (emulate_instruction(vcpu,
2824 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002825 return 0;
2826 return 1;
2827 }
2828
2829 size = (exit_qualification & 7) + 1;
2830 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002831 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002832
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002833 skip_emulated_instruction(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002834 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835}
2836
Ingo Molnar102d8322007-02-19 14:37:47 +02002837static void
2838vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2839{
2840 /*
2841 * Patch in the VMCALL instruction:
2842 */
2843 hypercall[0] = 0x0f;
2844 hypercall[1] = 0x01;
2845 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002846}
2847
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2849{
He, Qingbfdaab02007-09-12 14:18:28 +08002850 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851 int cr;
2852 int reg;
2853
He, Qingbfdaab02007-09-12 14:18:28 +08002854 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002855 cr = exit_qualification & 15;
2856 reg = (exit_qualification >> 8) & 15;
2857 switch ((exit_qualification >> 4) & 3) {
2858 case 0: /* mov to cr */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002859 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr,
2860 (u32)kvm_register_read(vcpu, reg),
2861 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
2862 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863 switch (cr) {
2864 case 0:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002865 kvm_set_cr0(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866 skip_emulated_instruction(vcpu);
2867 return 1;
2868 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002869 kvm_set_cr3(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002870 skip_emulated_instruction(vcpu);
2871 return 1;
2872 case 4:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002873 kvm_set_cr4(vcpu, kvm_register_read(vcpu, reg));
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);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002895 KVMTRACE_0D(CLTS, vcpu, handler);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002896 skip_emulated_instruction(vcpu);
2897 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002898 case 1: /*mov from cr*/
2899 switch (cr) {
2900 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002901 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002902 KVMTRACE_3D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002903 (u32)kvm_register_read(vcpu, reg),
2904 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002905 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002906 skip_emulated_instruction(vcpu);
2907 return 1;
2908 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002909 kvm_register_write(vcpu, reg, kvm_get_cr8(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002910 KVMTRACE_2D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002911 (u32)kvm_register_read(vcpu, reg), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002912 skip_emulated_instruction(vcpu);
2913 return 1;
2914 }
2915 break;
2916 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002917 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002918
2919 skip_emulated_instruction(vcpu);
2920 return 1;
2921 default:
2922 break;
2923 }
2924 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002925 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002926 (int)(exit_qualification >> 4) & 3, cr);
2927 return 0;
2928}
2929
2930static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2931{
He, Qingbfdaab02007-09-12 14:18:28 +08002932 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933 unsigned long val;
2934 int dr, reg;
2935
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002936 dr = vmcs_readl(GUEST_DR7);
2937 if (dr & DR7_GD) {
2938 /*
2939 * As the vm-exit takes precedence over the debug trap, we
2940 * need to emulate the latter, either for the host or the
2941 * guest debugging itself.
2942 */
2943 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
2944 kvm_run->debug.arch.dr6 = vcpu->arch.dr6;
2945 kvm_run->debug.arch.dr7 = dr;
2946 kvm_run->debug.arch.pc =
2947 vmcs_readl(GUEST_CS_BASE) +
2948 vmcs_readl(GUEST_RIP);
2949 kvm_run->debug.arch.exception = DB_VECTOR;
2950 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2951 return 0;
2952 } else {
2953 vcpu->arch.dr7 &= ~DR7_GD;
2954 vcpu->arch.dr6 |= DR6_BD;
2955 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2956 kvm_queue_exception(vcpu, DB_VECTOR);
2957 return 1;
2958 }
2959 }
2960
He, Qingbfdaab02007-09-12 14:18:28 +08002961 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002962 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
2963 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
2964 if (exit_qualification & TYPE_MOV_FROM_DR) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965 switch (dr) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002966 case 0 ... 3:
2967 val = vcpu->arch.db[dr];
2968 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002969 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002970 val = vcpu->arch.dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002971 break;
2972 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002973 val = vcpu->arch.dr7;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002974 break;
2975 default:
2976 val = 0;
2977 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002978 kvm_register_write(vcpu, reg, val);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002979 KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002980 } else {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002981 val = vcpu->arch.regs[reg];
2982 switch (dr) {
2983 case 0 ... 3:
2984 vcpu->arch.db[dr] = val;
2985 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
2986 vcpu->arch.eff_db[dr] = val;
2987 break;
2988 case 4 ... 5:
2989 if (vcpu->arch.cr4 & X86_CR4_DE)
2990 kvm_queue_exception(vcpu, UD_VECTOR);
2991 break;
2992 case 6:
2993 if (val & 0xffffffff00000000ULL) {
2994 kvm_queue_exception(vcpu, GP_VECTOR);
2995 break;
2996 }
2997 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
2998 break;
2999 case 7:
3000 if (val & 0xffffffff00000000ULL) {
3001 kvm_queue_exception(vcpu, GP_VECTOR);
3002 break;
3003 }
3004 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
3005 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
3006 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3007 vcpu->arch.switch_db_regs =
3008 (val & DR7_BP_EN_MASK);
3009 }
3010 break;
3011 }
3012 KVMTRACE_2D(DR_WRITE, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003013 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003014 skip_emulated_instruction(vcpu);
3015 return 1;
3016}
3017
3018static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3019{
Avi Kivity06465c52007-02-28 20:46:53 +02003020 kvm_emulate_cpuid(vcpu);
3021 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003022}
3023
3024static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3025{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003026 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003027 u64 data;
3028
3029 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003030 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003031 return 1;
3032 }
3033
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003034 KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32),
3035 handler);
3036
Avi Kivity6aa8b732006-12-10 02:21:36 -08003037 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003038 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3039 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003040 skip_emulated_instruction(vcpu);
3041 return 1;
3042}
3043
3044static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3045{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003046 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3047 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3048 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003049
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003050 KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32),
3051 handler);
3052
Avi Kivity6aa8b732006-12-10 02:21:36 -08003053 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003054 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003055 return 1;
3056 }
3057
3058 skip_emulated_instruction(vcpu);
3059 return 1;
3060}
3061
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003062static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
3063 struct kvm_run *kvm_run)
3064{
3065 return 1;
3066}
3067
Avi Kivity6aa8b732006-12-10 02:21:36 -08003068static int handle_interrupt_window(struct kvm_vcpu *vcpu,
3069 struct kvm_run *kvm_run)
3070{
Eddie Dong85f455f2007-07-06 12:20:49 +03003071 u32 cpu_based_vm_exec_control;
3072
3073 /* clear pending irq */
3074 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3075 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3076 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003077
3078 KVMTRACE_0D(PEND_INTR, vcpu, handler);
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003079 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003080
Dor Laorc1150d82007-01-05 16:36:24 -08003081 /*
3082 * If the user space waits to inject interrupts, exit as soon as
3083 * possible
3084 */
Gleb Natapov80618232009-04-21 17:44:56 +03003085 if (!irqchip_in_kernel(vcpu->kvm) &&
3086 kvm_run->request_interrupt_window &&
3087 !kvm_cpu_has_interrupt(vcpu)) {
Dor Laorc1150d82007-01-05 16:36:24 -08003088 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003089 return 0;
3090 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003091 return 1;
3092}
3093
3094static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3095{
3096 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003097 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003098}
3099
Ingo Molnarc21415e2007-02-19 14:37:47 +02003100static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3101{
Dor Laor510043d2007-02-19 18:25:43 +02003102 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003103 kvm_emulate_hypercall(vcpu);
3104 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003105}
3106
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003107static int handle_vmx_insn(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3108{
3109 kvm_queue_exception(vcpu, UD_VECTOR);
3110 return 1;
3111}
3112
Marcelo Tosattia7052892008-09-23 13:18:35 -03003113static int handle_invlpg(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3114{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003115 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003116
3117 kvm_mmu_invlpg(vcpu, exit_qualification);
3118 skip_emulated_instruction(vcpu);
3119 return 1;
3120}
3121
Eddie Donge5edaa02007-11-11 12:28:35 +02003122static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3123{
3124 skip_emulated_instruction(vcpu);
3125 /* TODO: Add support for VT-d/pass-through device */
3126 return 1;
3127}
3128
Sheng Yangf78e0e22007-10-29 09:40:42 +08003129static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3130{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003131 unsigned long exit_qualification;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003132 enum emulation_result er;
3133 unsigned long offset;
3134
Sheng Yangf9c617f2009-03-25 10:08:52 +08003135 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003136 offset = exit_qualification & 0xffful;
3137
3138 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3139
3140 if (er != EMULATE_DONE) {
3141 printk(KERN_ERR
3142 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3143 offset);
3144 return -ENOTSUPP;
3145 }
3146 return 1;
3147}
3148
Izik Eidus37817f22008-03-24 23:14:53 +02003149static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3150{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003151 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003152 unsigned long exit_qualification;
3153 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003154 int reason, type, idt_v;
3155
3156 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3157 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003158
3159 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3160
3161 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003162 if (reason == TASK_SWITCH_GATE && idt_v) {
3163 switch (type) {
3164 case INTR_TYPE_NMI_INTR:
3165 vcpu->arch.nmi_injected = false;
3166 if (cpu_has_virtual_nmis())
3167 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3168 GUEST_INTR_STATE_NMI);
3169 break;
3170 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003171 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003172 kvm_clear_interrupt_queue(vcpu);
3173 break;
3174 case INTR_TYPE_HARD_EXCEPTION:
3175 case INTR_TYPE_SOFT_EXCEPTION:
3176 kvm_clear_exception_queue(vcpu);
3177 break;
3178 default:
3179 break;
3180 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003181 }
Izik Eidus37817f22008-03-24 23:14:53 +02003182 tss_selector = exit_qualification;
3183
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003184 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3185 type != INTR_TYPE_EXT_INTR &&
3186 type != INTR_TYPE_NMI_INTR))
3187 skip_emulated_instruction(vcpu);
3188
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003189 if (!kvm_task_switch(vcpu, tss_selector, reason))
3190 return 0;
3191
3192 /* clear all local breakpoint enable flags */
3193 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3194
3195 /*
3196 * TODO: What about debug traps on tss switch?
3197 * Are we supposed to inject them and update dr6?
3198 */
3199
3200 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003201}
3202
Sheng Yang14394422008-04-28 12:24:45 +08003203static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3204{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003205 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003206 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003207 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003208
Sheng Yangf9c617f2009-03-25 10:08:52 +08003209 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003210
3211 if (exit_qualification & (1 << 6)) {
3212 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
3213 return -ENOTSUPP;
3214 }
3215
3216 gla_validity = (exit_qualification >> 7) & 0x3;
3217 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3218 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3219 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3220 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003221 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003222 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3223 (long unsigned int)exit_qualification);
3224 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity596ae892009-06-03 14:12:10 +03003225 kvm_run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
3226 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003227 }
3228
3229 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Sheng Yang49cd7d22009-02-11 13:50:40 +08003230 return kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003231}
3232
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003233static u64 ept_rsvd_mask(u64 spte, int level)
3234{
3235 int i;
3236 u64 mask = 0;
3237
3238 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
3239 mask |= (1ULL << i);
3240
3241 if (level > 2)
3242 /* bits 7:3 reserved */
3243 mask |= 0xf8;
3244 else if (level == 2) {
3245 if (spte & (1ULL << 7))
3246 /* 2MB ref, bits 20:12 reserved */
3247 mask |= 0x1ff000;
3248 else
3249 /* bits 6:3 reserved */
3250 mask |= 0x78;
3251 }
3252
3253 return mask;
3254}
3255
3256static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
3257 int level)
3258{
3259 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
3260
3261 /* 010b (write-only) */
3262 WARN_ON((spte & 0x7) == 0x2);
3263
3264 /* 110b (write/execute) */
3265 WARN_ON((spte & 0x7) == 0x6);
3266
3267 /* 100b (execute-only) and value not supported by logical processor */
3268 if (!cpu_has_vmx_ept_execute_only())
3269 WARN_ON((spte & 0x7) == 0x4);
3270
3271 /* not 000b */
3272 if ((spte & 0x7)) {
3273 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
3274
3275 if (rsvd_bits != 0) {
3276 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
3277 __func__, rsvd_bits);
3278 WARN_ON(1);
3279 }
3280
3281 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
3282 u64 ept_mem_type = (spte & 0x38) >> 3;
3283
3284 if (ept_mem_type == 2 || ept_mem_type == 3 ||
3285 ept_mem_type == 7) {
3286 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
3287 __func__, ept_mem_type);
3288 WARN_ON(1);
3289 }
3290 }
3291 }
3292}
3293
3294static int handle_ept_misconfig(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3295{
3296 u64 sptes[4];
3297 int nr_sptes, i;
3298 gpa_t gpa;
3299
3300 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3301
3302 printk(KERN_ERR "EPT: Misconfiguration.\n");
3303 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
3304
3305 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
3306
3307 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
3308 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
3309
3310 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3311 kvm_run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
3312
3313 return 0;
3314}
3315
Sheng Yangf08864b2008-05-15 18:23:25 +08003316static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3317{
3318 u32 cpu_based_vm_exec_control;
3319
3320 /* clear pending NMI */
3321 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3322 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3323 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3324 ++vcpu->stat.nmi_window_exits;
3325
3326 return 1;
3327}
3328
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003329static void handle_invalid_guest_state(struct kvm_vcpu *vcpu,
3330 struct kvm_run *kvm_run)
3331{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003332 struct vcpu_vmx *vmx = to_vmx(vcpu);
3333 enum emulation_result err = EMULATE_DONE;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003334
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003335 local_irq_enable();
Jan Kiszka34f0c1a2009-07-22 23:53:26 +02003336 preempt_enable();
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003337
3338 while (!guest_state_valid(vcpu)) {
3339 err = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3340
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003341 if (err == EMULATE_DO_MMIO)
3342 break;
3343
3344 if (err != EMULATE_DONE) {
3345 kvm_report_emulation_failure(vcpu, "emulation failure");
Jan Kiszka263799a2009-07-21 10:43:07 +02003346 break;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003347 }
3348
3349 if (signal_pending(current))
3350 break;
3351 if (need_resched())
3352 schedule();
3353 }
3354
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003355 preempt_disable();
Jan Kiszka34f0c1a2009-07-22 23:53:26 +02003356 local_irq_disable();
Avi Kivity8b3079a2009-01-05 12:10:54 +02003357
3358 vmx->invalid_state_emulation_result = err;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003359}
3360
Avi Kivity6aa8b732006-12-10 02:21:36 -08003361/*
3362 * The exit handlers return 1 if the exit was handled fully and guest execution
3363 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3364 * to be done to userspace and return 0.
3365 */
3366static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
3367 struct kvm_run *kvm_run) = {
3368 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3369 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003370 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003371 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003372 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003373 [EXIT_REASON_CR_ACCESS] = handle_cr,
3374 [EXIT_REASON_DR_ACCESS] = handle_dr,
3375 [EXIT_REASON_CPUID] = handle_cpuid,
3376 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3377 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3378 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3379 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003380 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003381 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003382 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3383 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3384 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3385 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3386 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3387 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3388 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3389 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3390 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003391 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3392 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003393 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003394 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08003395 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003396 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
3397 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003398};
3399
3400static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003401 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003402
3403/*
3404 * The guest has exited. See if we can fix it or if we need userspace
3405 * assistance.
3406 */
Avi Kivity6062d012009-03-23 17:35:17 +02003407static int vmx_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003408{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003409 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003410 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003411 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003412
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003413 KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)kvm_rip_read(vcpu),
3414 (u32)((u64)kvm_rip_read(vcpu) >> 32), entryexit);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003415
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003416 /* If we need to emulate an MMIO from handle_invalid_guest_state
3417 * we just return 0 */
Avi Kivity10f32d82009-01-05 00:53:19 +02003418 if (vmx->emulation_required && emulate_invalid_guest_state) {
3419 if (guest_state_valid(vcpu))
3420 vmx->emulation_required = 0;
Avi Kivity8b3079a2009-01-05 12:10:54 +02003421 return vmx->invalid_state_emulation_result != EMULATE_DO_MMIO;
Avi Kivity10f32d82009-01-05 00:53:19 +02003422 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003423
Sheng Yang14394422008-04-28 12:24:45 +08003424 /* Access CR3 don't cause VMExit in paging mode, so we need
3425 * to sync with guest real CR3. */
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03003426 if (enable_ept && is_paging(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08003427 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003428
Avi Kivity29bd8a72007-09-10 17:27:03 +03003429 if (unlikely(vmx->fail)) {
3430 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3431 kvm_run->fail_entry.hardware_entry_failure_reason
3432 = vmcs_read32(VM_INSTRUCTION_ERROR);
3433 return 0;
3434 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003435
Mike Dayd77c26f2007-10-08 09:02:08 -04003436 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003437 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003438 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3439 exit_reason != EXIT_REASON_TASK_SWITCH))
3440 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3441 "(0x%x) and exit reason is 0x%x\n",
3442 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003443
3444 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03003445 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003446 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003447 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003448 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003449 /*
3450 * This CPU don't support us in finding the end of an
3451 * NMI-blocked window if the guest runs with IRQs
3452 * disabled. So we pull the trigger after 1 s of
3453 * futile waiting, but inform the user about this.
3454 */
3455 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3456 "state on VCPU %d after 1 s timeout\n",
3457 __func__, vcpu->vcpu_id);
3458 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003459 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003460 }
3461
Avi Kivity6aa8b732006-12-10 02:21:36 -08003462 if (exit_reason < kvm_vmx_max_exit_handlers
3463 && kvm_vmx_exit_handlers[exit_reason])
3464 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
3465 else {
3466 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3467 kvm_run->hw.hardware_exit_reason = exit_reason;
3468 }
3469 return 0;
3470}
3471
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003472static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003473{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003474 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003475 vmcs_write32(TPR_THRESHOLD, 0);
3476 return;
3477 }
3478
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003479 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003480}
3481
Avi Kivitycf393f72008-07-01 16:20:21 +03003482static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3483{
3484 u32 exit_intr_info;
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003485 u32 idt_vectoring_info = vmx->idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003486 bool unblock_nmi;
3487 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003488 int type;
3489 bool idtv_info_valid;
Avi Kivitycf393f72008-07-01 16:20:21 +03003490
3491 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Gleb Natapov20f65982009-05-11 13:35:55 +03003492
Andi Kleena0861c02009-06-08 17:37:09 +08003493 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
3494
3495 /* Handle machine checks before interrupts are enabled */
3496 if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
3497 || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
3498 && is_machine_check(exit_intr_info)))
3499 kvm_machine_check();
3500
Gleb Natapov20f65982009-05-11 13:35:55 +03003501 /* We need to handle NMIs before interrupts are enabled */
3502 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
3503 (exit_intr_info & INTR_INFO_VALID_MASK)) {
3504 KVMTRACE_0D(NMI, &vmx->vcpu, handler);
3505 asm("int $2");
3506 }
3507
3508 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3509
Avi Kivitycf393f72008-07-01 16:20:21 +03003510 if (cpu_has_virtual_nmis()) {
3511 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3512 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3513 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003514 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03003515 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3516 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003517 * SDM 3: 23.2.2 (September 2008)
3518 * Bit 12 is undefined in any of the following cases:
3519 * If the VM exit sets the valid bit in the IDT-vectoring
3520 * information field.
3521 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03003522 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003523 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
3524 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03003525 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3526 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003527 } else if (unlikely(vmx->soft_vnmi_blocked))
3528 vmx->vnmi_blocked_time +=
3529 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003530
Gleb Natapov37b96e92009-03-30 16:03:13 +03003531 vmx->vcpu.arch.nmi_injected = false;
3532 kvm_clear_exception_queue(&vmx->vcpu);
3533 kvm_clear_interrupt_queue(&vmx->vcpu);
3534
3535 if (!idtv_info_valid)
3536 return;
3537
Avi Kivity668f6122008-07-02 09:28:55 +03003538 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3539 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003540
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003541 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003542 case INTR_TYPE_NMI_INTR:
3543 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03003544 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003545 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03003546 * Clear bit "block by NMI" before VM entry if a NMI
3547 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03003548 */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003549 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3550 GUEST_INTR_STATE_NMI);
3551 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003552 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003553 vmx->vcpu.arch.event_exit_inst_len =
3554 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3555 /* fall through */
3556 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03003557 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003558 u32 err = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3559 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03003560 } else
3561 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003562 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003563 case INTR_TYPE_SOFT_INTR:
3564 vmx->vcpu.arch.event_exit_inst_len =
3565 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3566 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003567 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003568 kvm_queue_interrupt(&vmx->vcpu, vector,
3569 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003570 break;
3571 default:
3572 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03003573 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003574}
3575
Avi Kivity9c8cba32007-11-22 11:42:59 +02003576/*
3577 * Failure to inject an interrupt should give us the information
3578 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3579 * when fetching the interrupt redirection bitmap in the real-mode
3580 * tss, this doesn't happen. So we do it ourselves.
3581 */
3582static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3583{
3584 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003585 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003586 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003587 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003588 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3589 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3590 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3591 return;
3592 }
3593 vmx->idt_vectoring_info =
3594 VECTORING_INFO_VALID_MASK
3595 | INTR_TYPE_EXT_INTR
3596 | vmx->rmode.irq.vector;
3597}
3598
Avi Kivityc8019492008-07-14 14:44:59 +03003599#ifdef CONFIG_X86_64
3600#define R "r"
3601#define Q "q"
3602#else
3603#define R "e"
3604#define Q "l"
3605#endif
3606
Avi Kivity04d2cc72007-09-10 18:10:54 +03003607static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003608{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003609 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitye6adf282007-04-30 16:07:54 +03003610
Avi Kivity8f5d5492009-05-31 18:41:29 +03003611 if (enable_ept && is_paging(vcpu)) {
3612 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3613 ept_load_pdptrs(vcpu);
3614 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003615 /* Record the guest's net vcpu time for enforced NMI injections. */
3616 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3617 vmx->entry_time = ktime_get();
3618
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003619 /* Handle invalid guest state instead of entering VMX */
3620 if (vmx->emulation_required && emulate_invalid_guest_state) {
3621 handle_invalid_guest_state(vcpu, kvm_run);
3622 return;
3623 }
3624
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003625 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3626 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3627 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3628 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3629
Gleb Natapov787ff732009-05-18 11:44:06 +03003630 /* When single-stepping over STI and MOV SS, we must clear the
3631 * corresponding interruptibility bits in the guest state. Otherwise
3632 * vmentry fails as it then expects bit 14 (BS) in pending debug
3633 * exceptions being set, but that's not correct for the guest debugging
3634 * case. */
3635 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3636 vmx_set_interrupt_shadow(vcpu, 0);
3637
Avi Kivitye6adf282007-04-30 16:07:54 +03003638 /*
3639 * Loading guest fpu may have cleared host cr0.ts
3640 */
3641 vmcs_writel(HOST_CR0, read_cr0());
3642
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003643 set_debugreg(vcpu->arch.dr6, 6);
3644
Mike Dayd77c26f2007-10-08 09:02:08 -04003645 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003646 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003647 "push %%"R"dx; push %%"R"bp;"
3648 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003649 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3650 "je 1f \n\t"
3651 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003652 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003653 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03003654 /* Reload cr2 if changed */
3655 "mov %c[cr2](%0), %%"R"ax \n\t"
3656 "mov %%cr2, %%"R"dx \n\t"
3657 "cmp %%"R"ax, %%"R"dx \n\t"
3658 "je 2f \n\t"
3659 "mov %%"R"ax, %%cr2 \n\t"
3660 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003661 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003662 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003663 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003664 "mov %c[rax](%0), %%"R"ax \n\t"
3665 "mov %c[rbx](%0), %%"R"bx \n\t"
3666 "mov %c[rdx](%0), %%"R"dx \n\t"
3667 "mov %c[rsi](%0), %%"R"si \n\t"
3668 "mov %c[rdi](%0), %%"R"di \n\t"
3669 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003670#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003671 "mov %c[r8](%0), %%r8 \n\t"
3672 "mov %c[r9](%0), %%r9 \n\t"
3673 "mov %c[r10](%0), %%r10 \n\t"
3674 "mov %c[r11](%0), %%r11 \n\t"
3675 "mov %c[r12](%0), %%r12 \n\t"
3676 "mov %c[r13](%0), %%r13 \n\t"
3677 "mov %c[r14](%0), %%r14 \n\t"
3678 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003679#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003680 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3681
Avi Kivity6aa8b732006-12-10 02:21:36 -08003682 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003683 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003684 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003685 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003686 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003687 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003688 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003689 "xchg %0, (%%"R"sp) \n\t"
3690 "mov %%"R"ax, %c[rax](%0) \n\t"
3691 "mov %%"R"bx, %c[rbx](%0) \n\t"
3692 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3693 "mov %%"R"dx, %c[rdx](%0) \n\t"
3694 "mov %%"R"si, %c[rsi](%0) \n\t"
3695 "mov %%"R"di, %c[rdi](%0) \n\t"
3696 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003697#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003698 "mov %%r8, %c[r8](%0) \n\t"
3699 "mov %%r9, %c[r9](%0) \n\t"
3700 "mov %%r10, %c[r10](%0) \n\t"
3701 "mov %%r11, %c[r11](%0) \n\t"
3702 "mov %%r12, %c[r12](%0) \n\t"
3703 "mov %%r13, %c[r13](%0) \n\t"
3704 "mov %%r14, %c[r14](%0) \n\t"
3705 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003706#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003707 "mov %%cr2, %%"R"ax \n\t"
3708 "mov %%"R"ax, %c[cr2](%0) \n\t"
3709
3710 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003711 "setbe %c[fail](%0) \n\t"
3712 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3713 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3714 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03003715 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003716 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3717 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3718 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3719 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3720 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3721 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3722 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003723#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003724 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3725 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3726 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3727 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3728 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3729 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3730 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3731 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003732#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003733 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003734 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003735 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003736#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003737 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3738#endif
3739 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003740
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03003741 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
3742 | (1 << VCPU_EXREG_PDPTR));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003743 vcpu->arch.regs_dirty = 0;
3744
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003745 get_debugreg(vcpu->arch.dr6, 6);
3746
Avi Kivity1155f762007-11-22 11:30:47 +02003747 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003748 if (vmx->rmode.irq.pending)
3749 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003750
Mike Dayd77c26f2007-10-08 09:02:08 -04003751 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003752 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003753
Avi Kivitycf393f72008-07-01 16:20:21 +03003754 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003755}
3756
Avi Kivityc8019492008-07-14 14:44:59 +03003757#undef R
3758#undef Q
3759
Avi Kivity6aa8b732006-12-10 02:21:36 -08003760static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3761{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003762 struct vcpu_vmx *vmx = to_vmx(vcpu);
3763
3764 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003765 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003766 free_vmcs(vmx->vmcs);
3767 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003768 }
3769}
3770
3771static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3772{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003773 struct vcpu_vmx *vmx = to_vmx(vcpu);
3774
Sheng Yang2384d2b2008-01-17 15:14:33 +08003775 spin_lock(&vmx_vpid_lock);
3776 if (vmx->vpid != 0)
3777 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3778 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003779 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003780 kfree(vmx->host_msrs);
3781 kfree(vmx->guest_msrs);
3782 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003783 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003784}
3785
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003786static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003787{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003788 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003789 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003790 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003791
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003792 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003793 return ERR_PTR(-ENOMEM);
3794
Sheng Yang2384d2b2008-01-17 15:14:33 +08003795 allocate_vpid(vmx);
3796
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003797 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3798 if (err)
3799 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003800
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003801 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003802 if (!vmx->guest_msrs) {
3803 err = -ENOMEM;
3804 goto uninit_vcpu;
3805 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003806
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003807 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3808 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003809 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003810
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003811 vmx->vmcs = alloc_vmcs();
3812 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003813 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003814
3815 vmcs_clear(vmx->vmcs);
3816
Avi Kivity15ad7142007-07-11 18:17:21 +03003817 cpu = get_cpu();
3818 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003819 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003820 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003821 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003822 if (err)
3823 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003824 if (vm_need_virtualize_apic_accesses(kvm))
3825 if (alloc_apic_access_page(kvm) != 0)
3826 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003827
Avi Kivity089d0342009-03-23 18:26:32 +02003828 if (enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003829 if (alloc_identity_pagetable(kvm) != 0)
3830 goto free_vmcs;
3831
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003832 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003833
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003834free_vmcs:
3835 free_vmcs(vmx->vmcs);
3836free_msrs:
3837 kfree(vmx->host_msrs);
3838free_guest_msrs:
3839 kfree(vmx->guest_msrs);
3840uninit_vcpu:
3841 kvm_vcpu_uninit(&vmx->vcpu);
3842free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003843 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003844 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003845}
3846
Yang, Sheng002c7f72007-07-31 14:23:01 +03003847static void __init vmx_check_processor_compat(void *rtn)
3848{
3849 struct vmcs_config vmcs_conf;
3850
3851 *(int *)rtn = 0;
3852 if (setup_vmcs_config(&vmcs_conf) < 0)
3853 *(int *)rtn = -EIO;
3854 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3855 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3856 smp_processor_id());
3857 *(int *)rtn = -EIO;
3858 }
3859}
3860
Sheng Yang67253af2008-04-25 10:20:22 +08003861static int get_ept_level(void)
3862{
3863 return VMX_EPT_DEFAULT_GAW + 1;
3864}
3865
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003866static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003867{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003868 u64 ret;
3869
Sheng Yang522c68c2009-04-27 20:35:43 +08003870 /* For VT-d and EPT combination
3871 * 1. MMIO: always map as UC
3872 * 2. EPT with VT-d:
3873 * a. VT-d without snooping control feature: can't guarantee the
3874 * result, try to trust guest.
3875 * b. VT-d with snooping control feature: snooping control feature of
3876 * VT-d engine can guarantee the cache correctness. Just set it
3877 * to WB to keep consistent with host. So the same as item 3.
3878 * 3. EPT without VT-d: always map as WB and set IGMT=1 to keep
3879 * consistent with host MTRR
3880 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003881 if (is_mmio)
3882 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08003883 else if (vcpu->kvm->arch.iommu_domain &&
3884 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
3885 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
3886 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003887 else
Sheng Yang522c68c2009-04-27 20:35:43 +08003888 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
3889 | VMX_EPT_IGMT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003890
3891 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08003892}
3893
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003894static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003895 .cpu_has_kvm_support = cpu_has_kvm_support,
3896 .disabled_by_bios = vmx_disabled_by_bios,
3897 .hardware_setup = hardware_setup,
3898 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003899 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003900 .hardware_enable = hardware_enable,
3901 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08003902 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003903
3904 .vcpu_create = vmx_create_vcpu,
3905 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003906 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003907
Avi Kivity04d2cc72007-09-10 18:10:54 +03003908 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003909 .vcpu_load = vmx_vcpu_load,
3910 .vcpu_put = vmx_vcpu_put,
3911
3912 .set_guest_debug = set_guest_debug,
3913 .get_msr = vmx_get_msr,
3914 .set_msr = vmx_set_msr,
3915 .get_segment_base = vmx_get_segment_base,
3916 .get_segment = vmx_get_segment,
3917 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003918 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003919 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003920 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003921 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003922 .set_cr3 = vmx_set_cr3,
3923 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003924 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003925 .get_idt = vmx_get_idt,
3926 .set_idt = vmx_set_idt,
3927 .get_gdt = vmx_get_gdt,
3928 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003929 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003930 .get_rflags = vmx_get_rflags,
3931 .set_rflags = vmx_set_rflags,
3932
3933 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003934
Avi Kivity6aa8b732006-12-10 02:21:36 -08003935 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02003936 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003937 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003938 .set_interrupt_shadow = vmx_set_interrupt_shadow,
3939 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003940 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003941 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003942 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003943 .queue_exception = vmx_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003944 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003945 .nmi_allowed = vmx_nmi_allowed,
3946 .enable_nmi_window = enable_nmi_window,
3947 .enable_irq_window = enable_irq_window,
3948 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003949
Izik Eiduscbc94022007-10-25 00:29:55 +02003950 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003951 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003952 .get_mt_mask = vmx_get_mt_mask,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003953};
3954
3955static int __init vmx_init(void)
3956{
He, Qingfdef3ad2007-04-30 09:45:24 +03003957 int r;
3958
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003959 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03003960 if (!vmx_io_bitmap_a)
3961 return -ENOMEM;
3962
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003963 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03003964 if (!vmx_io_bitmap_b) {
3965 r = -ENOMEM;
3966 goto out;
3967 }
3968
Avi Kivity58972972009-02-24 22:26:47 +02003969 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
3970 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08003971 r = -ENOMEM;
3972 goto out1;
3973 }
3974
Avi Kivity58972972009-02-24 22:26:47 +02003975 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
3976 if (!vmx_msr_bitmap_longmode) {
3977 r = -ENOMEM;
3978 goto out2;
3979 }
3980
He, Qingfdef3ad2007-04-30 09:45:24 +03003981 /*
3982 * Allow direct access to the PC debug port (it is often used for I/O
3983 * delays, but the vmexits simply slow things down).
3984 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003985 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
3986 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003987
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003988 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003989
Avi Kivity58972972009-02-24 22:26:47 +02003990 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
3991 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08003992
Sheng Yang2384d2b2008-01-17 15:14:33 +08003993 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
3994
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003995 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003996 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02003997 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08003998
Avi Kivity58972972009-02-24 22:26:47 +02003999 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
4000 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
4001 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
4002 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
4003 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
4004 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03004005
Avi Kivity089d0342009-03-23 18:26:32 +02004006 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004007 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004008 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08004009 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08004010 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004011 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004012 kvm_enable_tdp();
4013 } else
4014 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08004015
Avi Kivityc7addb92007-09-16 18:58:32 +02004016 if (bypass_guest_pf)
4017 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
4018
Sheng Yang14394422008-04-28 12:24:45 +08004019 ept_sync_global();
4020
He, Qingfdef3ad2007-04-30 09:45:24 +03004021 return 0;
4022
Avi Kivity58972972009-02-24 22:26:47 +02004023out3:
4024 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08004025out2:
Avi Kivity58972972009-02-24 22:26:47 +02004026 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03004027out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004028 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03004029out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004030 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004031 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004032}
4033
4034static void __exit vmx_exit(void)
4035{
Avi Kivity58972972009-02-24 22:26:47 +02004036 free_page((unsigned long)vmx_msr_bitmap_legacy);
4037 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004038 free_page((unsigned long)vmx_io_bitmap_b);
4039 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004040
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004041 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004042}
4043
4044module_init(vmx_init)
4045module_exit(vmx_exit)