blob: c6997c0e8ca643f8b67be4c0e2c5e970be58076f [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>
Avi Kivity6aa8b732006-12-10 02:21:36 -080035
Avi Kivity4ecac3f2008-05-13 13:23:38 +030036#define __ex(x) __kvm_handle_fault_on_reboot(x)
37
Avi Kivity6aa8b732006-12-10 02:21:36 -080038MODULE_AUTHOR("Qumranet");
39MODULE_LICENSE("GPL");
40
Avi Kivity4462d212009-03-23 17:53:37 +020041static int __read_mostly bypass_guest_pf = 1;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020042module_param(bypass_guest_pf, bool, S_IRUGO);
Avi Kivityc7addb92007-09-16 18:58:32 +020043
Avi Kivity4462d212009-03-23 17:53:37 +020044static int __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020045module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080046
Avi Kivity4462d212009-03-23 17:53:37 +020047static int __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020048module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020049
Avi Kivity4462d212009-03-23 17:53:37 +020050static int __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020051module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080052
Avi Kivity4462d212009-03-23 17:53:37 +020053static int __read_mostly emulate_invalid_guest_state = 0;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020054module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030055
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040056struct vmcs {
57 u32 revision_id;
58 u32 abort;
59 char data[0];
60};
61
62struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100063 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +030064 struct list_head local_vcpus_link;
Avi Kivity313dbd492008-07-17 18:04:30 +030065 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040066 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030067 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020068 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040069 struct kvm_msr_entry *guest_msrs;
70 struct kvm_msr_entry *host_msrs;
71 int nmsrs;
72 int save_nmsrs;
73 int msr_offset_efer;
74#ifdef CONFIG_X86_64
75 int msr_offset_kernel_gs_base;
76#endif
77 struct vmcs *vmcs;
78 struct {
79 int loaded;
80 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020081 int gs_ldt_reload_needed;
82 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030083 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040084 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +020085 struct {
86 struct {
87 bool pending;
88 u8 vector;
89 unsigned rip;
90 } irq;
91 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +080092 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030093 bool emulation_required;
Avi Kivity8b3079a2009-01-05 12:10:54 +020094 enum emulation_result invalid_state_emulation_result;
Jan Kiszka3b86cd92008-09-26 09:30:57 +020095
96 /* Support for vnmi-less CPUs */
97 int soft_vnmi_blocked;
98 ktime_t entry_time;
99 s64 vnmi_blocked_time;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400100};
101
102static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
103{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000104 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400105}
106
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800107static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800108static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300109
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110static DEFINE_PER_CPU(struct vmcs *, vmxarea);
111static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300112static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200114static unsigned long *vmx_io_bitmap_a;
115static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200116static unsigned long *vmx_msr_bitmap_legacy;
117static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300118
Sheng Yang2384d2b2008-01-17 15:14:33 +0800119static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
120static DEFINE_SPINLOCK(vmx_vpid_lock);
121
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300122static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123 int size;
124 int order;
125 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300126 u32 pin_based_exec_ctrl;
127 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800128 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300129 u32 vmexit_ctrl;
130 u32 vmentry_ctrl;
131} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132
Hannes Ederefff9e52008-11-28 17:02:06 +0100133static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800134 u32 ept;
135 u32 vpid;
136} vmx_capability;
137
Avi Kivity6aa8b732006-12-10 02:21:36 -0800138#define VMX_SEGMENT_FIELD(seg) \
139 [VCPU_SREG_##seg] = { \
140 .selector = GUEST_##seg##_SELECTOR, \
141 .base = GUEST_##seg##_BASE, \
142 .limit = GUEST_##seg##_LIMIT, \
143 .ar_bytes = GUEST_##seg##_AR_BYTES, \
144 }
145
146static struct kvm_vmx_segment_field {
147 unsigned selector;
148 unsigned base;
149 unsigned limit;
150 unsigned ar_bytes;
151} kvm_vmx_segment_fields[] = {
152 VMX_SEGMENT_FIELD(CS),
153 VMX_SEGMENT_FIELD(DS),
154 VMX_SEGMENT_FIELD(ES),
155 VMX_SEGMENT_FIELD(FS),
156 VMX_SEGMENT_FIELD(GS),
157 VMX_SEGMENT_FIELD(SS),
158 VMX_SEGMENT_FIELD(TR),
159 VMX_SEGMENT_FIELD(LDTR),
160};
161
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300162/*
163 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
164 * away by decrementing the array size.
165 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800167#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800168 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
169#endif
170 MSR_EFER, MSR_K6_STAR,
171};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200172#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400174static void load_msrs(struct kvm_msr_entry *e, int n)
175{
176 int i;
177
178 for (i = 0; i < n; ++i)
179 wrmsrl(e[i].index, e[i].data);
180}
181
182static void save_msrs(struct kvm_msr_entry *e, int n)
183{
184 int i;
185
186 for (i = 0; i < n; ++i)
187 rdmsrl(e[i].index, e[i].data);
188}
189
Avi Kivity6aa8b732006-12-10 02:21:36 -0800190static inline int is_page_fault(u32 intr_info)
191{
192 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
193 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100194 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800195}
196
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300197static inline int is_no_device(u32 intr_info)
198{
199 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
200 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100201 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300202}
203
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500204static inline int is_invalid_opcode(u32 intr_info)
205{
206 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
207 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100208 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500209}
210
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211static inline int is_external_interrupt(u32 intr_info)
212{
213 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
214 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
215}
216
Sheng Yang25c5f222008-03-28 13:18:56 +0800217static inline int cpu_has_vmx_msr_bitmap(void)
218{
Sheng Yang04547152009-04-01 15:52:31 +0800219 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800220}
221
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800222static inline int cpu_has_vmx_tpr_shadow(void)
223{
Sheng Yang04547152009-04-01 15:52:31 +0800224 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800225}
226
227static inline int vm_need_tpr_shadow(struct kvm *kvm)
228{
Sheng Yang04547152009-04-01 15:52:31 +0800229 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800230}
231
Sheng Yangf78e0e22007-10-29 09:40:42 +0800232static inline int cpu_has_secondary_exec_ctrls(void)
233{
Sheng Yang04547152009-04-01 15:52:31 +0800234 return vmcs_config.cpu_based_exec_ctrl &
235 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800236}
237
Avi Kivity774ead32007-12-26 13:57:04 +0200238static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800239{
Sheng Yang04547152009-04-01 15:52:31 +0800240 return vmcs_config.cpu_based_2nd_exec_ctrl &
241 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
242}
243
244static inline bool cpu_has_vmx_flexpriority(void)
245{
246 return cpu_has_vmx_tpr_shadow() &&
247 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800248}
249
Sheng Yangd56f5462008-04-25 10:13:16 +0800250static inline int cpu_has_vmx_invept_individual_addr(void)
251{
Sheng Yang04547152009-04-01 15:52:31 +0800252 return !!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800253}
254
255static inline int cpu_has_vmx_invept_context(void)
256{
Sheng Yang04547152009-04-01 15:52:31 +0800257 return !!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800258}
259
260static inline int cpu_has_vmx_invept_global(void)
261{
Sheng Yang04547152009-04-01 15:52:31 +0800262 return !!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800263}
264
265static inline int cpu_has_vmx_ept(void)
266{
Sheng Yang04547152009-04-01 15:52:31 +0800267 return vmcs_config.cpu_based_2nd_exec_ctrl &
268 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800269}
270
Sheng Yangf78e0e22007-10-29 09:40:42 +0800271static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
272{
Sheng Yang04547152009-04-01 15:52:31 +0800273 return flexpriority_enabled &&
274 (cpu_has_vmx_virtualize_apic_accesses()) &&
275 (irqchip_in_kernel(kvm));
Sheng Yangf78e0e22007-10-29 09:40:42 +0800276}
277
Sheng Yang2384d2b2008-01-17 15:14:33 +0800278static inline int cpu_has_vmx_vpid(void)
279{
Sheng Yang04547152009-04-01 15:52:31 +0800280 return vmcs_config.cpu_based_2nd_exec_ctrl &
281 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800282}
283
Sheng Yangf08864b2008-05-15 18:23:25 +0800284static inline int cpu_has_virtual_nmis(void)
285{
286 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
287}
288
Sheng Yang04547152009-04-01 15:52:31 +0800289static inline bool report_flexpriority(void)
290{
291 return flexpriority_enabled;
292}
293
Rusty Russell8b9cf982007-07-30 16:31:43 +1000294static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800295{
296 int i;
297
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400298 for (i = 0; i < vmx->nmsrs; ++i)
299 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300300 return i;
301 return -1;
302}
303
Sheng Yang2384d2b2008-01-17 15:14:33 +0800304static inline void __invvpid(int ext, u16 vpid, gva_t gva)
305{
306 struct {
307 u64 vpid : 16;
308 u64 rsvd : 48;
309 u64 gva;
310 } operand = { vpid, 0, gva };
311
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300312 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800313 /* CF==1 or ZF==1 --> rc = -1 */
314 "; ja 1f ; ud2 ; 1:"
315 : : "a"(&operand), "c"(ext) : "cc", "memory");
316}
317
Sheng Yang14394422008-04-28 12:24:45 +0800318static inline void __invept(int ext, u64 eptp, gpa_t gpa)
319{
320 struct {
321 u64 eptp, gpa;
322 } operand = {eptp, gpa};
323
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300324 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800325 /* CF==1 or ZF==1 --> rc = -1 */
326 "; ja 1f ; ud2 ; 1:\n"
327 : : "a" (&operand), "c" (ext) : "cc", "memory");
328}
329
Rusty Russell8b9cf982007-07-30 16:31:43 +1000330static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300331{
332 int i;
333
Rusty Russell8b9cf982007-07-30 16:31:43 +1000334 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300335 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400336 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000337 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800338}
339
Avi Kivity6aa8b732006-12-10 02:21:36 -0800340static void vmcs_clear(struct vmcs *vmcs)
341{
342 u64 phys_addr = __pa(vmcs);
343 u8 error;
344
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300345 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800346 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
347 : "cc", "memory");
348 if (error)
349 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
350 vmcs, phys_addr);
351}
352
353static void __vcpu_clear(void *arg)
354{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000355 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800356 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800357
Rusty Russell8b9cf982007-07-30 16:31:43 +1000358 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400359 vmcs_clear(vmx->vmcs);
360 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800362 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300363 list_del(&vmx->local_vcpus_link);
364 vmx->vcpu.cpu = -1;
365 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366}
367
Rusty Russell8b9cf982007-07-30 16:31:43 +1000368static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800369{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200370 if (vmx->vcpu.cpu == -1)
371 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200372 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800373}
374
Sheng Yang2384d2b2008-01-17 15:14:33 +0800375static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
376{
377 if (vmx->vpid == 0)
378 return;
379
380 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
381}
382
Sheng Yang14394422008-04-28 12:24:45 +0800383static inline void ept_sync_global(void)
384{
385 if (cpu_has_vmx_invept_global())
386 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
387}
388
389static inline void ept_sync_context(u64 eptp)
390{
Avi Kivity089d0342009-03-23 18:26:32 +0200391 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800392 if (cpu_has_vmx_invept_context())
393 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
394 else
395 ept_sync_global();
396 }
397}
398
399static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
400{
Avi Kivity089d0342009-03-23 18:26:32 +0200401 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800402 if (cpu_has_vmx_invept_individual_addr())
403 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
404 eptp, gpa);
405 else
406 ept_sync_context(eptp);
407 }
408}
409
Avi Kivity6aa8b732006-12-10 02:21:36 -0800410static unsigned long vmcs_readl(unsigned long field)
411{
412 unsigned long value;
413
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300414 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800415 : "=a"(value) : "d"(field) : "cc");
416 return value;
417}
418
419static u16 vmcs_read16(unsigned long field)
420{
421 return vmcs_readl(field);
422}
423
424static u32 vmcs_read32(unsigned long field)
425{
426 return vmcs_readl(field);
427}
428
429static u64 vmcs_read64(unsigned long field)
430{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800431#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800432 return vmcs_readl(field);
433#else
434 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
435#endif
436}
437
Avi Kivitye52de1b2007-01-05 16:36:56 -0800438static noinline void vmwrite_error(unsigned long field, unsigned long value)
439{
440 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
441 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
442 dump_stack();
443}
444
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445static void vmcs_writel(unsigned long field, unsigned long value)
446{
447 u8 error;
448
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300449 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400450 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800451 if (unlikely(error))
452 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800453}
454
455static void vmcs_write16(unsigned long field, u16 value)
456{
457 vmcs_writel(field, value);
458}
459
460static void vmcs_write32(unsigned long field, u32 value)
461{
462 vmcs_writel(field, value);
463}
464
465static void vmcs_write64(unsigned long field, u64 value)
466{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300468#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469 asm volatile ("");
470 vmcs_writel(field+1, value >> 32);
471#endif
472}
473
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300474static void vmcs_clear_bits(unsigned long field, u32 mask)
475{
476 vmcs_writel(field, vmcs_readl(field) & ~mask);
477}
478
479static void vmcs_set_bits(unsigned long field, u32 mask)
480{
481 vmcs_writel(field, vmcs_readl(field) | mask);
482}
483
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300484static void update_exception_bitmap(struct kvm_vcpu *vcpu)
485{
486 u32 eb;
487
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500488 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300489 if (!vcpu->fpu_active)
490 eb |= 1u << NM_VECTOR;
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100491 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
492 if (vcpu->guest_debug &
493 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
494 eb |= 1u << DB_VECTOR;
495 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
496 eb |= 1u << BP_VECTOR;
497 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800498 if (vcpu->arch.rmode.active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300499 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200500 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800501 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300502 vmcs_write32(EXCEPTION_BITMAP, eb);
503}
504
Avi Kivity33ed6322007-05-02 16:54:03 +0300505static void reload_tss(void)
506{
Avi Kivity33ed6322007-05-02 16:54:03 +0300507 /*
508 * VT restores TR but not its size. Useless.
509 */
510 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200511 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300512
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300513 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300514 descs = (void *)gdt.base;
515 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
516 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300517}
518
Rusty Russell8b9cf982007-07-30 16:31:43 +1000519static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300520{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400521 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300522 u64 host_efer = vmx->host_msrs[efer_offset].data;
523 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
524 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300525
Avi Kivity51c6cf62007-08-29 03:48:05 +0300526 if (efer_offset < 0)
527 return;
528 /*
529 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
530 * outside long mode
531 */
532 ignore_bits = EFER_NX | EFER_SCE;
533#ifdef CONFIG_X86_64
534 ignore_bits |= EFER_LMA | EFER_LME;
535 /* SCE is meaningful only in long mode on Intel */
536 if (guest_efer & EFER_LMA)
537 ignore_bits &= ~(u64)EFER_SCE;
538#endif
539 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
540 return;
541
542 vmx->host_state.guest_efer_loaded = 1;
543 guest_efer &= ~ignore_bits;
544 guest_efer |= host_efer & ignore_bits;
545 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000546 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300547}
548
Avi Kivity51c6cf62007-08-29 03:48:05 +0300549static void reload_host_efer(struct vcpu_vmx *vmx)
550{
551 if (vmx->host_state.guest_efer_loaded) {
552 vmx->host_state.guest_efer_loaded = 0;
553 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
554 }
555}
556
Avi Kivity04d2cc72007-09-10 18:10:54 +0300557static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300558{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300559 struct vcpu_vmx *vmx = to_vmx(vcpu);
560
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400561 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300562 return;
563
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400564 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300565 /*
566 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
567 * allow segment selectors with cpl > 0 or ti == 1.
568 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300569 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200570 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300571 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200572 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400573 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200574 vmx->host_state.fs_reload_needed = 0;
575 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300576 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200577 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300578 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300579 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400580 if (!(vmx->host_state.gs_sel & 7))
581 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300582 else {
583 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200584 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300585 }
586
587#ifdef CONFIG_X86_64
588 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
589 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
590#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400591 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
592 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300593#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300594
595#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400596 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400597 save_msrs(vmx->host_msrs +
598 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400599
Avi Kivity707c0872007-05-02 17:33:43 +0300600#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400601 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300602 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300603}
604
Avi Kivitya9b21b62008-06-24 11:48:49 +0300605static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300606{
Avi Kivity15ad7142007-07-11 18:17:21 +0300607 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300608
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400609 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300610 return;
611
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200612 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400613 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200614 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300615 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200616 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300617 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300618 /*
619 * If we have to reload gs, we must take care to
620 * preserve our gs base.
621 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300622 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300623 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300624#ifdef CONFIG_X86_64
625 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
626#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300627 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300628 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200629 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400630 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
631 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300632 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300633}
634
Avi Kivitya9b21b62008-06-24 11:48:49 +0300635static void vmx_load_host_state(struct vcpu_vmx *vmx)
636{
637 preempt_disable();
638 __vmx_load_host_state(vmx);
639 preempt_enable();
640}
641
Avi Kivity6aa8b732006-12-10 02:21:36 -0800642/*
643 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
644 * vcpu mutex is already taken.
645 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300646static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400648 struct vcpu_vmx *vmx = to_vmx(vcpu);
649 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200650 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800651
Eddie Donga3d7f852007-09-03 16:15:12 +0300652 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000653 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300654 kvm_migrate_timers(vcpu);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800655 vpid_sync_vcpu_all(vmx);
Avi Kivity543e4242008-05-13 16:22:47 +0300656 local_irq_disable();
657 list_add(&vmx->local_vcpus_link,
658 &per_cpu(vcpus_on_cpu, cpu));
659 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300660 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800661
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400662 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800663 u8 error;
664
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400665 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300666 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800667 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
668 : "cc");
669 if (error)
670 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400671 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672 }
673
674 if (vcpu->cpu != cpu) {
675 struct descriptor_table dt;
676 unsigned long sysenter_esp;
677
678 vcpu->cpu = cpu;
679 /*
680 * Linux uses per-cpu TSS and GDT, so set these when switching
681 * processors.
682 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300683 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
684 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
686
687 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
688 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300689
690 /*
691 * Make sure the time stamp counter is monotonous.
692 */
693 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200694 if (tsc_this < vcpu->arch.host_tsc) {
695 delta = vcpu->arch.host_tsc - tsc_this;
696 new_offset = vmcs_read64(TSC_OFFSET) + delta;
697 vmcs_write64(TSC_OFFSET, new_offset);
698 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700}
701
702static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
703{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300704 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800705}
706
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300707static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
708{
709 if (vcpu->fpu_active)
710 return;
711 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000712 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800713 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92fa2007-07-17 23:19:08 +1000714 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300715 update_exception_bitmap(vcpu);
716}
717
718static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
719{
720 if (!vcpu->fpu_active)
721 return;
722 vcpu->fpu_active = 0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000723 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300724 update_exception_bitmap(vcpu);
725}
726
Avi Kivity6aa8b732006-12-10 02:21:36 -0800727static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
728{
729 return vmcs_readl(GUEST_RFLAGS);
730}
731
732static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
733{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800734 if (vcpu->arch.rmode.active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100735 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800736 vmcs_writel(GUEST_RFLAGS, rflags);
737}
738
739static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
740{
741 unsigned long rip;
742 u32 interruptibility;
743
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300744 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800745 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300746 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747
748 /*
749 * We emulated an instruction, so temporary interrupt blocking
750 * should be removed, if set.
751 */
752 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
753 if (interruptibility & 3)
754 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
755 interruptibility & ~3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800756 vcpu->arch.interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757}
758
Avi Kivity298101d2007-11-25 13:41:11 +0200759static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
760 bool has_error_code, u32 error_code)
761{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200762 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100763 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200764
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100765 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200766 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100767 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
768 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200769
770 if (vcpu->arch.rmode.active) {
771 vmx->rmode.irq.pending = true;
772 vmx->rmode.irq.vector = nr;
773 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100774 if (nr == BP_VECTOR || nr == OF_VECTOR)
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200775 vmx->rmode.irq.rip++;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100776 intr_info |= INTR_TYPE_SOFT_INTR;
777 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200778 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
779 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
780 return;
781 }
782
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100783 if (nr == BP_VECTOR || nr == OF_VECTOR) {
784 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
785 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
786 } else
787 intr_info |= INTR_TYPE_HARD_EXCEPTION;
788
789 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200790}
791
792static bool vmx_exception_injected(struct kvm_vcpu *vcpu)
793{
Avi Kivity35920a32008-07-03 14:50:12 +0300794 return false;
Avi Kivity298101d2007-11-25 13:41:11 +0200795}
796
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797/*
Eddie Donga75beee2007-05-17 18:55:15 +0300798 * Swap MSR entry in host/guest MSR entry array.
799 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200800#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000801static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300802{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400803 struct kvm_msr_entry tmp;
804
805 tmp = vmx->guest_msrs[to];
806 vmx->guest_msrs[to] = vmx->guest_msrs[from];
807 vmx->guest_msrs[from] = tmp;
808 tmp = vmx->host_msrs[to];
809 vmx->host_msrs[to] = vmx->host_msrs[from];
810 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300811}
Gabriel C54e11fa2007-08-01 16:23:10 +0200812#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300813
814/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300815 * Set up the vmcs to automatically save and restore system
816 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
817 * mode, as fiddling with msrs is very expensive.
818 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000819static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300820{
Eddie Dong2cc51562007-05-21 07:28:09 +0300821 int save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +0200822 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +0300823
Avi Kivity33f9c502008-02-27 16:06:57 +0200824 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300825 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300826#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000827 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300828 int index;
829
Rusty Russell8b9cf982007-07-30 16:31:43 +1000830 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300831 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000832 move_msr_up(vmx, index, save_nmsrs++);
833 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300834 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000835 move_msr_up(vmx, index, save_nmsrs++);
836 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300837 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000838 move_msr_up(vmx, index, save_nmsrs++);
839 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300840 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000841 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300842 /*
843 * MSR_K6_STAR is only needed on long mode guests, and only
844 * if efer.sce is enabled.
845 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000846 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800847 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000848 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300849 }
Eddie Donga75beee2007-05-17 18:55:15 +0300850#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400851 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300852
Eddie Donga75beee2007-05-17 18:55:15 +0300853#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400854 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000855 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300856#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000857 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivity58972972009-02-24 22:26:47 +0200858
859 if (cpu_has_vmx_msr_bitmap()) {
860 if (is_long_mode(&vmx->vcpu))
861 msr_bitmap = vmx_msr_bitmap_longmode;
862 else
863 msr_bitmap = vmx_msr_bitmap_legacy;
864
865 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
866 }
Avi Kivitye38aea32007-04-19 13:22:48 +0300867}
868
869/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870 * reads and returns guest's timestamp counter "register"
871 * guest_tsc = host_tsc + tsc_offset -- 21.3
872 */
873static u64 guest_read_tsc(void)
874{
875 u64 host_tsc, tsc_offset;
876
877 rdtscll(host_tsc);
878 tsc_offset = vmcs_read64(TSC_OFFSET);
879 return host_tsc + tsc_offset;
880}
881
882/*
883 * writes 'guest_tsc' into guest's timestamp counter "register"
884 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
885 */
Marcelo Tosatti53f658b32008-12-11 20:45:05 +0100886static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
889}
890
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891/*
892 * Reads an msr value (of 'msr_index') into 'pdata'.
893 * Returns 0 on success, non-0 otherwise.
894 * Assumes vcpu_load() was already called.
895 */
896static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
897{
898 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400899 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900
901 if (!pdata) {
902 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
903 return -EINVAL;
904 }
905
906 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800907#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908 case MSR_FS_BASE:
909 data = vmcs_readl(GUEST_FS_BASE);
910 break;
911 case MSR_GS_BASE:
912 data = vmcs_readl(GUEST_GS_BASE);
913 break;
914 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800915 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916#endif
917 case MSR_IA32_TIME_STAMP_COUNTER:
918 data = guest_read_tsc();
919 break;
920 case MSR_IA32_SYSENTER_CS:
921 data = vmcs_read32(GUEST_SYSENTER_CS);
922 break;
923 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200924 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925 break;
926 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200927 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929 default:
Avi Kivity516a1a72009-02-15 02:32:07 +0200930 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +1000931 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800932 if (msr) {
933 data = msr->data;
934 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800936 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937 }
938
939 *pdata = data;
940 return 0;
941}
942
943/*
944 * Writes msr value into into the appropriate "register".
945 * Returns 0 on success, non-0 otherwise.
946 * Assumes vcpu_load() was already called.
947 */
948static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
949{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400950 struct vcpu_vmx *vmx = to_vmx(vcpu);
951 struct kvm_msr_entry *msr;
Marcelo Tosatti53f658b32008-12-11 20:45:05 +0100952 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +0300953 int ret = 0;
954
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -0800956 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300957 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +0300958 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +0300959 break;
Avi Kivity16175a72009-03-23 22:13:44 +0200960#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961 case MSR_FS_BASE:
962 vmcs_writel(GUEST_FS_BASE, data);
963 break;
964 case MSR_GS_BASE:
965 vmcs_writel(GUEST_GS_BASE, data);
966 break;
967#endif
968 case MSR_IA32_SYSENTER_CS:
969 vmcs_write32(GUEST_SYSENTER_CS, data);
970 break;
971 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200972 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973 break;
974 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200975 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200977 case MSR_IA32_TIME_STAMP_COUNTER:
Marcelo Tosatti53f658b32008-12-11 20:45:05 +0100978 rdtscll(host_tsc);
979 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980 break;
Chris Lalancetteefa67e02008-06-20 09:51:30 +0200981 case MSR_P6_PERFCTR0:
982 case MSR_P6_PERFCTR1:
983 case MSR_P6_EVNTSEL0:
984 case MSR_P6_EVNTSEL1:
985 /*
986 * Just discard all writes to the performance counters; this
987 * should keep both older linux and windows 64-bit guests
988 * happy
989 */
990 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", msr_index, data);
991
992 break;
Sheng Yang468d4722008-10-09 16:01:55 +0800993 case MSR_IA32_CR_PAT:
994 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
995 vmcs_write64(GUEST_IA32_PAT, data);
996 vcpu->arch.pat = data;
997 break;
998 }
999 /* Otherwise falls through to kvm_set_msr_common */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000 default:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001001 vmx_load_host_state(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +10001002 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001003 if (msr) {
1004 msr->data = data;
1005 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001007 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008 }
1009
Eddie Dong2cc51562007-05-21 07:28:09 +03001010 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011}
1012
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001013static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001015 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1016 switch (reg) {
1017 case VCPU_REGS_RSP:
1018 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1019 break;
1020 case VCPU_REGS_RIP:
1021 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1022 break;
1023 default:
1024 break;
1025 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026}
1027
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001028static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001030 int old_debug = vcpu->guest_debug;
1031 unsigned long flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001033 vcpu->guest_debug = dbg->control;
1034 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
1035 vcpu->guest_debug = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001037 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1038 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1039 else
1040 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1041
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001042 flags = vmcs_readl(GUEST_RFLAGS);
1043 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
1044 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1045 else if (old_debug & KVM_GUESTDBG_SINGLESTEP)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001047 vmcs_writel(GUEST_RFLAGS, flags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001049 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050
1051 return 0;
1052}
1053
Eddie Dong2a8067f2007-08-06 16:29:07 +03001054static int vmx_get_irq(struct kvm_vcpu *vcpu)
1055{
Avi Kivityf7d92382008-07-03 16:14:28 +03001056 if (!vcpu->arch.interrupt.pending)
1057 return -1;
1058 return vcpu->arch.interrupt.nr;
Eddie Dong2a8067f2007-08-06 16:29:07 +03001059}
1060
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061static __init int cpu_has_kvm_support(void)
1062{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001063 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064}
1065
1066static __init int vmx_disabled_by_bios(void)
1067{
1068 u64 msr;
1069
1070 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001071 return (msr & (FEATURE_CONTROL_LOCKED |
1072 FEATURE_CONTROL_VMXON_ENABLED))
1073 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001074 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075}
1076
Avi Kivity774c47f2007-02-12 00:54:47 -08001077static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078{
1079 int cpu = raw_smp_processor_id();
1080 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1081 u64 old;
1082
Avi Kivity543e4242008-05-13 16:22:47 +03001083 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001085 if ((old & (FEATURE_CONTROL_LOCKED |
1086 FEATURE_CONTROL_VMXON_ENABLED))
1087 != (FEATURE_CONTROL_LOCKED |
1088 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001090 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001091 FEATURE_CONTROL_LOCKED |
1092 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001093 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001094 asm volatile (ASM_VMX_VMXON_RAX
1095 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096 : "memory", "cc");
1097}
1098
Avi Kivity543e4242008-05-13 16:22:47 +03001099static void vmclear_local_vcpus(void)
1100{
1101 int cpu = raw_smp_processor_id();
1102 struct vcpu_vmx *vmx, *n;
1103
1104 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1105 local_vcpus_link)
1106 __vcpu_clear(vmx);
1107}
1108
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001109
1110/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1111 * tricks.
1112 */
1113static void kvm_cpu_vmxoff(void)
1114{
1115 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1116 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1117}
1118
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119static void hardware_disable(void *garbage)
1120{
Avi Kivity543e4242008-05-13 16:22:47 +03001121 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001122 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001123}
1124
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001125static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001126 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127{
1128 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001129 u32 ctl = ctl_min | ctl_opt;
1130
1131 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1132
1133 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1134 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1135
1136 /* Ensure minimum (required) set of control bits are supported. */
1137 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001138 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001139
1140 *result = ctl;
1141 return 0;
1142}
1143
Yang, Sheng002c7f72007-07-31 14:23:01 +03001144static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001145{
1146 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001147 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001148 u32 _pin_based_exec_control = 0;
1149 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001150 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001151 u32 _vmexit_control = 0;
1152 u32 _vmentry_control = 0;
1153
1154 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001155 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001156 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1157 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001158 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001159
1160 min = CPU_BASED_HLT_EXITING |
1161#ifdef CONFIG_X86_64
1162 CPU_BASED_CR8_LOAD_EXITING |
1163 CPU_BASED_CR8_STORE_EXITING |
1164#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001165 CPU_BASED_CR3_LOAD_EXITING |
1166 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001167 CPU_BASED_USE_IO_BITMAPS |
1168 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001169 CPU_BASED_USE_TSC_OFFSETING |
1170 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001171 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001172 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001173 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001174 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1175 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001176 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001177#ifdef CONFIG_X86_64
1178 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1179 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1180 ~CPU_BASED_CR8_STORE_EXITING;
1181#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001182 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001183 min2 = 0;
1184 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001185 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001186 SECONDARY_EXEC_ENABLE_VPID |
1187 SECONDARY_EXEC_ENABLE_EPT;
1188 if (adjust_vmx_controls(min2, opt2,
1189 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001190 &_cpu_based_2nd_exec_control) < 0)
1191 return -EIO;
1192 }
1193#ifndef CONFIG_X86_64
1194 if (!(_cpu_based_2nd_exec_control &
1195 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1196 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1197#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001198 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001199 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1200 enabled */
Sheng Yangd56f5462008-04-25 10:13:16 +08001201 min &= ~(CPU_BASED_CR3_LOAD_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001202 CPU_BASED_CR3_STORE_EXITING |
1203 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001204 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1205 &_cpu_based_exec_control) < 0)
1206 return -EIO;
1207 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1208 vmx_capability.ept, vmx_capability.vpid);
1209 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001210
1211 min = 0;
1212#ifdef CONFIG_X86_64
1213 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1214#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001215 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001216 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1217 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001218 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001219
Sheng Yang468d4722008-10-09 16:01:55 +08001220 min = 0;
1221 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001222 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1223 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001224 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001225
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001226 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001227
1228 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1229 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001230 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001231
1232#ifdef CONFIG_X86_64
1233 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1234 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001235 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001236#endif
1237
1238 /* Require Write-Back (WB) memory type for VMCS accesses. */
1239 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001240 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001241
Yang, Sheng002c7f72007-07-31 14:23:01 +03001242 vmcs_conf->size = vmx_msr_high & 0x1fff;
1243 vmcs_conf->order = get_order(vmcs_config.size);
1244 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001245
Yang, Sheng002c7f72007-07-31 14:23:01 +03001246 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1247 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001248 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001249 vmcs_conf->vmexit_ctrl = _vmexit_control;
1250 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001251
1252 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001253}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001254
1255static struct vmcs *alloc_vmcs_cpu(int cpu)
1256{
1257 int node = cpu_to_node(cpu);
1258 struct page *pages;
1259 struct vmcs *vmcs;
1260
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001261 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262 if (!pages)
1263 return NULL;
1264 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001265 memset(vmcs, 0, vmcs_config.size);
1266 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001267 return vmcs;
1268}
1269
1270static struct vmcs *alloc_vmcs(void)
1271{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001272 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001273}
1274
1275static void free_vmcs(struct vmcs *vmcs)
1276{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001277 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278}
1279
Sam Ravnborg39959582007-06-01 00:47:13 -07001280static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001281{
1282 int cpu;
1283
1284 for_each_online_cpu(cpu)
1285 free_vmcs(per_cpu(vmxarea, cpu));
1286}
1287
Avi Kivity6aa8b732006-12-10 02:21:36 -08001288static __init int alloc_kvm_area(void)
1289{
1290 int cpu;
1291
1292 for_each_online_cpu(cpu) {
1293 struct vmcs *vmcs;
1294
1295 vmcs = alloc_vmcs_cpu(cpu);
1296 if (!vmcs) {
1297 free_kvm_area();
1298 return -ENOMEM;
1299 }
1300
1301 per_cpu(vmxarea, cpu) = vmcs;
1302 }
1303 return 0;
1304}
1305
1306static __init int hardware_setup(void)
1307{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001308 if (setup_vmcs_config(&vmcs_config) < 0)
1309 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001310
1311 if (boot_cpu_has(X86_FEATURE_NX))
1312 kvm_enable_efer_bits(EFER_NX);
1313
Sheng Yang93ba03c2009-04-01 15:52:32 +08001314 if (!cpu_has_vmx_vpid())
1315 enable_vpid = 0;
1316
1317 if (!cpu_has_vmx_ept())
1318 enable_ept = 0;
1319
1320 if (!cpu_has_vmx_flexpriority())
1321 flexpriority_enabled = 0;
1322
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323 return alloc_kvm_area();
1324}
1325
1326static __exit void hardware_unsetup(void)
1327{
1328 free_kvm_area();
1329}
1330
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1332{
1333 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1334
Avi Kivity6af11b92007-03-19 13:18:10 +02001335 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 vmcs_write16(sf->selector, save->selector);
1337 vmcs_writel(sf->base, save->base);
1338 vmcs_write32(sf->limit, save->limit);
1339 vmcs_write32(sf->ar_bytes, save->ar);
1340 } else {
1341 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1342 << AR_DPL_SHIFT;
1343 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1344 }
1345}
1346
1347static void enter_pmode(struct kvm_vcpu *vcpu)
1348{
1349 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001350 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001351
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001352 vmx->emulation_required = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001353 vcpu->arch.rmode.active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001354
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001355 vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base);
1356 vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit);
1357 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358
1359 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001360 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001361 flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362 vmcs_writel(GUEST_RFLAGS, flags);
1363
Rusty Russell66aee912007-07-17 23:34:16 +10001364 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1365 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366
1367 update_exception_bitmap(vcpu);
1368
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001369 if (emulate_invalid_guest_state)
1370 return;
1371
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001372 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1373 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1374 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1375 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376
1377 vmcs_write16(GUEST_SS_SELECTOR, 0);
1378 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1379
1380 vmcs_write16(GUEST_CS_SELECTOR,
1381 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1382 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1383}
1384
Mike Dayd77c26f2007-10-08 09:02:08 -04001385static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001387 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001388 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1389 kvm->memslots[0].npages - 3;
1390 return base_gfn << PAGE_SHIFT;
1391 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001392 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393}
1394
1395static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1396{
1397 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1398
1399 save->selector = vmcs_read16(sf->selector);
1400 save->base = vmcs_readl(sf->base);
1401 save->limit = vmcs_read32(sf->limit);
1402 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001403 vmcs_write16(sf->selector, save->base >> 4);
1404 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405 vmcs_write32(sf->limit, 0xffff);
1406 vmcs_write32(sf->ar_bytes, 0xf3);
1407}
1408
1409static void enter_rmode(struct kvm_vcpu *vcpu)
1410{
1411 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001412 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001414 vmx->emulation_required = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001415 vcpu->arch.rmode.active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001417 vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1419
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001420 vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1422
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001423 vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1425
1426 flags = vmcs_readl(GUEST_RFLAGS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001427 vcpu->arch.rmode.save_iopl
1428 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001430 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431
1432 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001433 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001434 update_exception_bitmap(vcpu);
1435
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001436 if (emulate_invalid_guest_state)
1437 goto continue_rmode;
1438
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1440 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1441 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1442
1443 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001444 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001445 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1446 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001447 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1448
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001449 fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1450 fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1451 fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1452 fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001453
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001454continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001455 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001456 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457}
1458
Amit Shah401d10d2009-02-20 22:53:37 +05301459static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1460{
1461 struct vcpu_vmx *vmx = to_vmx(vcpu);
1462 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1463
1464 vcpu->arch.shadow_efer = efer;
1465 if (!msr)
1466 return;
1467 if (efer & EFER_LMA) {
1468 vmcs_write32(VM_ENTRY_CONTROLS,
1469 vmcs_read32(VM_ENTRY_CONTROLS) |
1470 VM_ENTRY_IA32E_MODE);
1471 msr->data = efer;
1472 } else {
1473 vmcs_write32(VM_ENTRY_CONTROLS,
1474 vmcs_read32(VM_ENTRY_CONTROLS) &
1475 ~VM_ENTRY_IA32E_MODE);
1476
1477 msr->data = efer & ~EFER_LME;
1478 }
1479 setup_msrs(vmx);
1480}
1481
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001482#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483
1484static void enter_lmode(struct kvm_vcpu *vcpu)
1485{
1486 u32 guest_tr_ar;
1487
1488 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1489 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1490 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001491 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001492 vmcs_write32(GUEST_TR_AR_BYTES,
1493 (guest_tr_ar & ~AR_TYPE_MASK)
1494 | AR_TYPE_BUSY_64_TSS);
1495 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001496 vcpu->arch.shadow_efer |= EFER_LMA;
Amit Shah401d10d2009-02-20 22:53:37 +05301497 vmx_set_efer(vcpu, vcpu->arch.shadow_efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498}
1499
1500static void exit_lmode(struct kvm_vcpu *vcpu)
1501{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001502 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503
1504 vmcs_write32(VM_ENTRY_CONTROLS,
1505 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001506 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507}
1508
1509#endif
1510
Sheng Yang2384d2b2008-01-17 15:14:33 +08001511static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1512{
1513 vpid_sync_vcpu_all(to_vmx(vcpu));
Avi Kivity089d0342009-03-23 18:26:32 +02001514 if (enable_ept)
Sheng Yang4e1096d2008-07-06 19:16:51 +08001515 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001516}
1517
Anthony Liguori25c4c272007-04-27 09:29:21 +03001518static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001519{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001520 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1521 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001522}
1523
Sheng Yang14394422008-04-28 12:24:45 +08001524static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1525{
1526 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1527 if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
1528 printk(KERN_ERR "EPT: Fail to load pdptrs!\n");
1529 return;
1530 }
1531 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1532 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1533 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1534 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1535 }
1536}
1537
1538static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1539
1540static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1541 unsigned long cr0,
1542 struct kvm_vcpu *vcpu)
1543{
1544 if (!(cr0 & X86_CR0_PG)) {
1545 /* From paging/starting to nonpaging */
1546 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001547 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001548 (CPU_BASED_CR3_LOAD_EXITING |
1549 CPU_BASED_CR3_STORE_EXITING));
1550 vcpu->arch.cr0 = cr0;
1551 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1552 *hw_cr0 |= X86_CR0_PE | X86_CR0_PG;
1553 *hw_cr0 &= ~X86_CR0_WP;
1554 } else if (!is_paging(vcpu)) {
1555 /* From nonpaging to paging */
1556 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001557 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001558 ~(CPU_BASED_CR3_LOAD_EXITING |
1559 CPU_BASED_CR3_STORE_EXITING));
1560 vcpu->arch.cr0 = cr0;
1561 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1562 if (!(vcpu->arch.cr0 & X86_CR0_WP))
1563 *hw_cr0 &= ~X86_CR0_WP;
1564 }
1565}
1566
1567static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1568 struct kvm_vcpu *vcpu)
1569{
1570 if (!is_paging(vcpu)) {
1571 *hw_cr4 &= ~X86_CR4_PAE;
1572 *hw_cr4 |= X86_CR4_PSE;
1573 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1574 *hw_cr4 &= ~X86_CR4_PAE;
1575}
1576
Avi Kivity6aa8b732006-12-10 02:21:36 -08001577static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1578{
Sheng Yang14394422008-04-28 12:24:45 +08001579 unsigned long hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) |
1580 KVM_VM_CR0_ALWAYS_ON;
1581
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001582 vmx_fpu_deactivate(vcpu);
1583
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001584 if (vcpu->arch.rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001585 enter_pmode(vcpu);
1586
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001587 if (!vcpu->arch.rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588 enter_rmode(vcpu);
1589
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001590#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001591 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001592 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001594 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595 exit_lmode(vcpu);
1596 }
1597#endif
1598
Avi Kivity089d0342009-03-23 18:26:32 +02001599 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001600 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1601
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001603 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001604 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001605
Rusty Russell707d92fa2007-07-17 23:19:08 +10001606 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001607 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001608}
1609
Sheng Yang14394422008-04-28 12:24:45 +08001610static u64 construct_eptp(unsigned long root_hpa)
1611{
1612 u64 eptp;
1613
1614 /* TODO write the value reading from MSR */
1615 eptp = VMX_EPT_DEFAULT_MT |
1616 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1617 eptp |= (root_hpa & PAGE_MASK);
1618
1619 return eptp;
1620}
1621
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1623{
Sheng Yang14394422008-04-28 12:24:45 +08001624 unsigned long guest_cr3;
1625 u64 eptp;
1626
1627 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02001628 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001629 eptp = construct_eptp(cr3);
1630 vmcs_write64(EPT_POINTER, eptp);
1631 ept_sync_context(eptp);
1632 ept_load_pdptrs(vcpu);
1633 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
1634 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1635 }
1636
Sheng Yang2384d2b2008-01-17 15:14:33 +08001637 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001638 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001639 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001640 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001641}
1642
1643static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1644{
Sheng Yang14394422008-04-28 12:24:45 +08001645 unsigned long hw_cr4 = cr4 | (vcpu->arch.rmode.active ?
1646 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1647
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001648 vcpu->arch.cr4 = cr4;
Avi Kivity089d0342009-03-23 18:26:32 +02001649 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001650 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1651
1652 vmcs_writel(CR4_READ_SHADOW, cr4);
1653 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001654}
1655
Avi Kivity6aa8b732006-12-10 02:21:36 -08001656static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1657{
1658 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1659
1660 return vmcs_readl(sf->base);
1661}
1662
1663static void vmx_get_segment(struct kvm_vcpu *vcpu,
1664 struct kvm_segment *var, int seg)
1665{
1666 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1667 u32 ar;
1668
1669 var->base = vmcs_readl(sf->base);
1670 var->limit = vmcs_read32(sf->limit);
1671 var->selector = vmcs_read16(sf->selector);
1672 ar = vmcs_read32(sf->ar_bytes);
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02001673 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674 ar = 0;
1675 var->type = ar & 15;
1676 var->s = (ar >> 4) & 1;
1677 var->dpl = (ar >> 5) & 3;
1678 var->present = (ar >> 7) & 1;
1679 var->avl = (ar >> 12) & 1;
1680 var->l = (ar >> 13) & 1;
1681 var->db = (ar >> 14) & 1;
1682 var->g = (ar >> 15) & 1;
1683 var->unusable = (ar >> 16) & 1;
1684}
1685
Izik Eidus2e4d2652008-03-24 19:38:34 +02001686static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1687{
1688 struct kvm_segment kvm_seg;
1689
1690 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1691 return 0;
1692
1693 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1694 return 3;
1695
1696 vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
1697 return kvm_seg.selector & 3;
1698}
1699
Avi Kivity653e3102007-05-07 10:55:37 +03001700static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001701{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001702 u32 ar;
1703
Avi Kivity653e3102007-05-07 10:55:37 +03001704 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705 ar = 1 << 16;
1706 else {
1707 ar = var->type & 15;
1708 ar |= (var->s & 1) << 4;
1709 ar |= (var->dpl & 3) << 5;
1710 ar |= (var->present & 1) << 7;
1711 ar |= (var->avl & 1) << 12;
1712 ar |= (var->l & 1) << 13;
1713 ar |= (var->db & 1) << 14;
1714 ar |= (var->g & 1) << 15;
1715 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001716 if (ar == 0) /* a 0 value means unusable */
1717 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001718
1719 return ar;
1720}
1721
1722static void vmx_set_segment(struct kvm_vcpu *vcpu,
1723 struct kvm_segment *var, int seg)
1724{
1725 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1726 u32 ar;
1727
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001728 if (vcpu->arch.rmode.active && seg == VCPU_SREG_TR) {
1729 vcpu->arch.rmode.tr.selector = var->selector;
1730 vcpu->arch.rmode.tr.base = var->base;
1731 vcpu->arch.rmode.tr.limit = var->limit;
1732 vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001733 return;
1734 }
1735 vmcs_writel(sf->base, var->base);
1736 vmcs_write32(sf->limit, var->limit);
1737 vmcs_write16(sf->selector, var->selector);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001738 if (vcpu->arch.rmode.active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001739 /*
1740 * Hack real-mode segments into vm86 compatibility.
1741 */
1742 if (var->base == 0xffff0000 && var->selector == 0xf000)
1743 vmcs_writel(sf->base, 0xf0000);
1744 ar = 0xf3;
1745 } else
1746 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001747 vmcs_write32(sf->ar_bytes, ar);
1748}
1749
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1751{
1752 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1753
1754 *db = (ar >> 14) & 1;
1755 *l = (ar >> 13) & 1;
1756}
1757
1758static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1759{
1760 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1761 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1762}
1763
1764static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1765{
1766 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1767 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1768}
1769
1770static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1771{
1772 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1773 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1774}
1775
1776static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1777{
1778 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1779 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1780}
1781
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001782static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1783{
1784 struct kvm_segment var;
1785 u32 ar;
1786
1787 vmx_get_segment(vcpu, &var, seg);
1788 ar = vmx_segment_access_rights(&var);
1789
1790 if (var.base != (var.selector << 4))
1791 return false;
1792 if (var.limit != 0xffff)
1793 return false;
1794 if (ar != 0xf3)
1795 return false;
1796
1797 return true;
1798}
1799
1800static bool code_segment_valid(struct kvm_vcpu *vcpu)
1801{
1802 struct kvm_segment cs;
1803 unsigned int cs_rpl;
1804
1805 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1806 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1807
Avi Kivity1872a3f2009-01-04 23:26:52 +02001808 if (cs.unusable)
1809 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001810 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1811 return false;
1812 if (!cs.s)
1813 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001814 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001815 if (cs.dpl > cs_rpl)
1816 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001817 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001818 if (cs.dpl != cs_rpl)
1819 return false;
1820 }
1821 if (!cs.present)
1822 return false;
1823
1824 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1825 return true;
1826}
1827
1828static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1829{
1830 struct kvm_segment ss;
1831 unsigned int ss_rpl;
1832
1833 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1834 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1835
Avi Kivity1872a3f2009-01-04 23:26:52 +02001836 if (ss.unusable)
1837 return true;
1838 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001839 return false;
1840 if (!ss.s)
1841 return false;
1842 if (ss.dpl != ss_rpl) /* DPL != RPL */
1843 return false;
1844 if (!ss.present)
1845 return false;
1846
1847 return true;
1848}
1849
1850static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1851{
1852 struct kvm_segment var;
1853 unsigned int rpl;
1854
1855 vmx_get_segment(vcpu, &var, seg);
1856 rpl = var.selector & SELECTOR_RPL_MASK;
1857
Avi Kivity1872a3f2009-01-04 23:26:52 +02001858 if (var.unusable)
1859 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001860 if (!var.s)
1861 return false;
1862 if (!var.present)
1863 return false;
1864 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
1865 if (var.dpl < rpl) /* DPL < RPL */
1866 return false;
1867 }
1868
1869 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1870 * rights flags
1871 */
1872 return true;
1873}
1874
1875static bool tr_valid(struct kvm_vcpu *vcpu)
1876{
1877 struct kvm_segment tr;
1878
1879 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
1880
Avi Kivity1872a3f2009-01-04 23:26:52 +02001881 if (tr.unusable)
1882 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001883 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1884 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001885 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001886 return false;
1887 if (!tr.present)
1888 return false;
1889
1890 return true;
1891}
1892
1893static bool ldtr_valid(struct kvm_vcpu *vcpu)
1894{
1895 struct kvm_segment ldtr;
1896
1897 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
1898
Avi Kivity1872a3f2009-01-04 23:26:52 +02001899 if (ldtr.unusable)
1900 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001901 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1902 return false;
1903 if (ldtr.type != 2)
1904 return false;
1905 if (!ldtr.present)
1906 return false;
1907
1908 return true;
1909}
1910
1911static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
1912{
1913 struct kvm_segment cs, ss;
1914
1915 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1916 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1917
1918 return ((cs.selector & SELECTOR_RPL_MASK) ==
1919 (ss.selector & SELECTOR_RPL_MASK));
1920}
1921
1922/*
1923 * Check if guest state is valid. Returns true if valid, false if
1924 * not.
1925 * We assume that registers are always usable
1926 */
1927static bool guest_state_valid(struct kvm_vcpu *vcpu)
1928{
1929 /* real mode guest state checks */
1930 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
1931 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
1932 return false;
1933 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
1934 return false;
1935 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
1936 return false;
1937 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
1938 return false;
1939 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
1940 return false;
1941 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
1942 return false;
1943 } else {
1944 /* protected mode guest state checks */
1945 if (!cs_ss_rpl_check(vcpu))
1946 return false;
1947 if (!code_segment_valid(vcpu))
1948 return false;
1949 if (!stack_segment_valid(vcpu))
1950 return false;
1951 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
1952 return false;
1953 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
1954 return false;
1955 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
1956 return false;
1957 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
1958 return false;
1959 if (!tr_valid(vcpu))
1960 return false;
1961 if (!ldtr_valid(vcpu))
1962 return false;
1963 }
1964 /* TODO:
1965 * - Add checks on RIP
1966 * - Add checks on RFLAGS
1967 */
1968
1969 return true;
1970}
1971
Mike Dayd77c26f2007-10-08 09:02:08 -04001972static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001974 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001975 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001976 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02001977 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001978
Izik Eidus195aefd2007-10-01 22:14:18 +02001979 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1980 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001981 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001982 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08001983 r = kvm_write_guest_page(kvm, fn++, &data,
1984 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02001985 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001986 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001987 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
1988 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001989 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001990 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1991 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001992 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001993 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001994 r = kvm_write_guest_page(kvm, fn, &data,
1995 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
1996 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02001997 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001998 goto out;
1999
2000 ret = 1;
2001out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002002 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002003}
2004
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002005static int init_rmode_identity_map(struct kvm *kvm)
2006{
2007 int i, r, ret;
2008 pfn_t identity_map_pfn;
2009 u32 tmp;
2010
Avi Kivity089d0342009-03-23 18:26:32 +02002011 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002012 return 1;
2013 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2014 printk(KERN_ERR "EPT: identity-mapping pagetable "
2015 "haven't been allocated!\n");
2016 return 0;
2017 }
2018 if (likely(kvm->arch.ept_identity_pagetable_done))
2019 return 1;
2020 ret = 0;
2021 identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT;
2022 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2023 if (r < 0)
2024 goto out;
2025 /* Set up identity-mapping pagetable for EPT in real mode */
2026 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2027 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2028 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2029 r = kvm_write_guest_page(kvm, identity_map_pfn,
2030 &tmp, i * sizeof(tmp), sizeof(tmp));
2031 if (r < 0)
2032 goto out;
2033 }
2034 kvm->arch.ept_identity_pagetable_done = true;
2035 ret = 1;
2036out:
2037 return ret;
2038}
2039
Avi Kivity6aa8b732006-12-10 02:21:36 -08002040static void seg_setup(int seg)
2041{
2042 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2043
2044 vmcs_write16(sf->selector, 0);
2045 vmcs_writel(sf->base, 0);
2046 vmcs_write32(sf->limit, 0xffff);
Avi Kivitya16b20d2008-08-20 15:48:27 +03002047 vmcs_write32(sf->ar_bytes, 0xf3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002048}
2049
Sheng Yangf78e0e22007-10-29 09:40:42 +08002050static int alloc_apic_access_page(struct kvm *kvm)
2051{
2052 struct kvm_userspace_memory_region kvm_userspace_mem;
2053 int r = 0;
2054
Izik Eidus72dc67a2008-02-10 18:04:15 +02002055 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002056 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002057 goto out;
2058 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2059 kvm_userspace_mem.flags = 0;
2060 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2061 kvm_userspace_mem.memory_size = PAGE_SIZE;
2062 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2063 if (r)
2064 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002065
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002066 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002067out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002068 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002069 return r;
2070}
2071
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002072static int alloc_identity_pagetable(struct kvm *kvm)
2073{
2074 struct kvm_userspace_memory_region kvm_userspace_mem;
2075 int r = 0;
2076
2077 down_write(&kvm->slots_lock);
2078 if (kvm->arch.ept_identity_pagetable)
2079 goto out;
2080 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2081 kvm_userspace_mem.flags = 0;
2082 kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
2083 kvm_userspace_mem.memory_size = PAGE_SIZE;
2084 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2085 if (r)
2086 goto out;
2087
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002088 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
2089 VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002090out:
2091 up_write(&kvm->slots_lock);
2092 return r;
2093}
2094
Sheng Yang2384d2b2008-01-17 15:14:33 +08002095static void allocate_vpid(struct vcpu_vmx *vmx)
2096{
2097 int vpid;
2098
2099 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002100 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002101 return;
2102 spin_lock(&vmx_vpid_lock);
2103 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2104 if (vpid < VMX_NR_VPIDS) {
2105 vmx->vpid = vpid;
2106 __set_bit(vpid, vmx_vpid_bitmap);
2107 }
2108 spin_unlock(&vmx_vpid_lock);
2109}
2110
Avi Kivity58972972009-02-24 22:26:47 +02002111static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002112{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002113 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002114
2115 if (!cpu_has_vmx_msr_bitmap())
2116 return;
2117
2118 /*
2119 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2120 * have the write-low and read-high bitmap offsets the wrong way round.
2121 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2122 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002123 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002124 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2125 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002126 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2127 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002128 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2129 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002130 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002131}
2132
Avi Kivity58972972009-02-24 22:26:47 +02002133static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2134{
2135 if (!longmode_only)
2136 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2137 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2138}
2139
Avi Kivity6aa8b732006-12-10 02:21:36 -08002140/*
2141 * Sets up the vmcs for emulated real mode.
2142 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002143static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002144{
Sheng Yang468d4722008-10-09 16:01:55 +08002145 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146 u32 junk;
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01002147 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148 unsigned long a;
2149 struct descriptor_table dt;
2150 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002151 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002152 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153
Avi Kivity6aa8b732006-12-10 02:21:36 -08002154 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002155 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2156 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002157
Sheng Yang25c5f222008-03-28 13:18:56 +08002158 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002159 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002160
Avi Kivity6aa8b732006-12-10 02:21:36 -08002161 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2162
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002164 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2165 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002166
2167 exec_control = vmcs_config.cpu_based_exec_ctrl;
2168 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2169 exec_control &= ~CPU_BASED_TPR_SHADOW;
2170#ifdef CONFIG_X86_64
2171 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2172 CPU_BASED_CR8_LOAD_EXITING;
2173#endif
2174 }
Avi Kivity089d0342009-03-23 18:26:32 +02002175 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002176 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002177 CPU_BASED_CR3_LOAD_EXITING |
2178 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002179 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180
Sheng Yang83ff3b92007-11-21 14:33:25 +08002181 if (cpu_has_secondary_exec_ctrls()) {
2182 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2183 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2184 exec_control &=
2185 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002186 if (vmx->vpid == 0)
2187 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Avi Kivity089d0342009-03-23 18:26:32 +02002188 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002189 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002190 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2191 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002192
Avi Kivityc7addb92007-09-16 18:58:32 +02002193 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2194 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002195 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2196
2197 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2198 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2199 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2200
2201 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2202 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2203 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002204 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2205 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002206 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002207#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002208 rdmsrl(MSR_FS_BASE, a);
2209 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2210 rdmsrl(MSR_GS_BASE, a);
2211 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2212#else
2213 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2214 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2215#endif
2216
2217 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2218
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002219 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002220 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2221
Mike Dayd77c26f2007-10-08 09:02:08 -04002222 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002223 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002224 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2225 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2226 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002227
2228 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2229 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2230 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2231 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2232 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2233 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2234
Sheng Yang468d4722008-10-09 16:01:55 +08002235 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2236 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2237 host_pat = msr_low | ((u64) msr_high << 32);
2238 vmcs_write64(HOST_IA32_PAT, host_pat);
2239 }
2240 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2241 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2242 host_pat = msr_low | ((u64) msr_high << 32);
2243 /* Write the default value follow host pat */
2244 vmcs_write64(GUEST_IA32_PAT, host_pat);
2245 /* Keep arch.pat sync with GUEST_IA32_PAT */
2246 vmx->vcpu.arch.pat = host_pat;
2247 }
2248
Avi Kivity6aa8b732006-12-10 02:21:36 -08002249 for (i = 0; i < NR_VMX_MSR; ++i) {
2250 u32 index = vmx_msr_index[i];
2251 u32 data_low, data_high;
2252 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002253 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002254
2255 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2256 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002257 if (wrmsr_safe(index, data_low, data_high) < 0)
2258 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002259 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002260 vmx->host_msrs[j].index = index;
2261 vmx->host_msrs[j].reserved = 0;
2262 vmx->host_msrs[j].data = data;
2263 vmx->guest_msrs[j] = vmx->host_msrs[j];
2264 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002267 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268
2269 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002270 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2271
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002272 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2273 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2274
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01002275 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2276 rdtscll(tsc_this);
2277 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2278 tsc_base = tsc_this;
2279
2280 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002281
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002282 return 0;
2283}
2284
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002285static int init_rmode(struct kvm *kvm)
2286{
2287 if (!init_rmode_tss(kvm))
2288 return 0;
2289 if (!init_rmode_identity_map(kvm))
2290 return 0;
2291 return 1;
2292}
2293
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002294static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2295{
2296 struct vcpu_vmx *vmx = to_vmx(vcpu);
2297 u64 msr;
2298 int ret;
2299
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002300 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002301 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002302 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002303 ret = -ENOMEM;
2304 goto out;
2305 }
2306
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002307 vmx->vcpu.arch.rmode.active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002308
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002309 vmx->soft_vnmi_blocked = 0;
2310
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002311 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002312 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002313 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
2314 if (vmx->vcpu.vcpu_id == 0)
2315 msr |= MSR_IA32_APICBASE_BSP;
2316 kvm_set_apic_base(&vmx->vcpu, msr);
2317
2318 fx_init(&vmx->vcpu);
2319
Avi Kivity5706be02008-08-20 15:07:31 +03002320 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002321 /*
2322 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2323 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2324 */
2325 if (vmx->vcpu.vcpu_id == 0) {
2326 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2327 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2328 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002329 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2330 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002331 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002332
2333 seg_setup(VCPU_SREG_DS);
2334 seg_setup(VCPU_SREG_ES);
2335 seg_setup(VCPU_SREG_FS);
2336 seg_setup(VCPU_SREG_GS);
2337 seg_setup(VCPU_SREG_SS);
2338
2339 vmcs_write16(GUEST_TR_SELECTOR, 0);
2340 vmcs_writel(GUEST_TR_BASE, 0);
2341 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2342 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2343
2344 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2345 vmcs_writel(GUEST_LDTR_BASE, 0);
2346 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2347 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2348
2349 vmcs_write32(GUEST_SYSENTER_CS, 0);
2350 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2351 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2352
2353 vmcs_writel(GUEST_RFLAGS, 0x02);
2354 if (vmx->vcpu.vcpu_id == 0)
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002355 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002356 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002357 kvm_rip_write(vcpu, 0);
2358 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002359
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002360 vmcs_writel(GUEST_DR7, 0x400);
2361
2362 vmcs_writel(GUEST_GDTR_BASE, 0);
2363 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2364
2365 vmcs_writel(GUEST_IDTR_BASE, 0);
2366 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2367
2368 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2369 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2370 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2371
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002372 /* Special registers */
2373 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2374
2375 setup_msrs(vmx);
2376
Avi Kivity6aa8b732006-12-10 02:21:36 -08002377 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2378
Sheng Yangf78e0e22007-10-29 09:40:42 +08002379 if (cpu_has_vmx_tpr_shadow()) {
2380 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2381 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2382 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002383 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002384 vmcs_write32(TPR_THRESHOLD, 0);
2385 }
2386
2387 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2388 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002389 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390
Sheng Yang2384d2b2008-01-17 15:14:33 +08002391 if (vmx->vpid != 0)
2392 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2393
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002394 vmx->vcpu.arch.cr0 = 0x60000010;
2395 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002396 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002397 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002398 vmx_fpu_activate(&vmx->vcpu);
2399 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002400
Sheng Yang2384d2b2008-01-17 15:14:33 +08002401 vpid_sync_vcpu_all(vmx);
2402
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002403 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002404
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002405 /* HACK: Don't enable emulation on guest boot/reset */
2406 vmx->emulation_required = 0;
2407
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002409 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002410 return ret;
2411}
2412
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002413static void enable_irq_window(struct kvm_vcpu *vcpu)
2414{
2415 u32 cpu_based_vm_exec_control;
2416
2417 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2418 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2419 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2420}
2421
2422static void enable_nmi_window(struct kvm_vcpu *vcpu)
2423{
2424 u32 cpu_based_vm_exec_control;
2425
2426 if (!cpu_has_virtual_nmis()) {
2427 enable_irq_window(vcpu);
2428 return;
2429 }
2430
2431 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2432 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2433 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2434}
2435
Eddie Dong85f455f2007-07-06 12:20:49 +03002436static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
2437{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002438 struct vcpu_vmx *vmx = to_vmx(vcpu);
2439
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002440 KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler);
2441
Avi Kivityfa89a812008-09-01 15:57:51 +03002442 ++vcpu->stat.irq_injections;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002443 if (vcpu->arch.rmode.active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002444 vmx->rmode.irq.pending = true;
2445 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002446 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Avi Kivity9c5623e2007-11-08 18:19:20 +02002447 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2448 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2449 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002450 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002451 return;
2452 }
2453 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2454 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
2455}
2456
Sheng Yangf08864b2008-05-15 18:23:25 +08002457static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2458{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002459 struct vcpu_vmx *vmx = to_vmx(vcpu);
2460
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002461 if (!cpu_has_virtual_nmis()) {
2462 /*
2463 * Tracking the NMI-blocked state in software is built upon
2464 * finding the next open IRQ window. This, in turn, depends on
2465 * well-behaving guests: They have to keep IRQs disabled at
2466 * least as long as the NMI handler runs. Otherwise we may
2467 * cause NMI nesting, maybe breaking the guest. But as this is
2468 * highly unlikely, we can live with the residual risk.
2469 */
2470 vmx->soft_vnmi_blocked = 1;
2471 vmx->vnmi_blocked_time = 0;
2472 }
2473
Jan Kiszka487b3912008-09-26 09:30:56 +02002474 ++vcpu->stat.nmi_injections;
Jan Kiszka66a5a342008-09-26 09:30:51 +02002475 if (vcpu->arch.rmode.active) {
2476 vmx->rmode.irq.pending = true;
2477 vmx->rmode.irq.vector = NMI_VECTOR;
2478 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2479 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2480 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2481 INTR_INFO_VALID_MASK);
2482 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2483 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2484 return;
2485 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002486 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2487 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002488}
2489
Jan Kiszka33f089c2008-09-26 09:30:49 +02002490static void vmx_update_window_states(struct kvm_vcpu *vcpu)
2491{
2492 u32 guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2493
2494 vcpu->arch.nmi_window_open =
2495 !(guest_intr & (GUEST_INTR_STATE_STI |
2496 GUEST_INTR_STATE_MOV_SS |
2497 GUEST_INTR_STATE_NMI));
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002498 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
2499 vcpu->arch.nmi_window_open = 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002500
2501 vcpu->arch.interrupt_window_open =
2502 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2503 !(guest_intr & (GUEST_INTR_STATE_STI |
2504 GUEST_INTR_STATE_MOV_SS)));
2505}
2506
Gleb Natapov78646122009-03-23 12:12:11 +02002507static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
2508{
2509 vmx_update_window_states(vcpu);
2510 return vcpu->arch.interrupt_window_open;
2511}
2512
Jan Kiszkaf460ee42008-09-26 09:30:50 +02002513static void do_interrupt_requests(struct kvm_vcpu *vcpu,
2514 struct kvm_run *kvm_run)
2515{
2516 vmx_update_window_states(vcpu);
2517
Jan Kiszka55934c02008-12-15 13:52:10 +01002518 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
2519 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
2520 GUEST_INTR_STATE_STI |
2521 GUEST_INTR_STATE_MOV_SS);
2522
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002523 if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
Jan Kiszka264ff01d2008-11-24 12:26:19 +01002524 if (vcpu->arch.interrupt.pending) {
2525 enable_nmi_window(vcpu);
2526 } else if (vcpu->arch.nmi_window_open) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002527 vcpu->arch.nmi_pending = false;
2528 vcpu->arch.nmi_injected = true;
2529 } else {
2530 enable_nmi_window(vcpu);
Jan Kiszka487b3912008-09-26 09:30:56 +02002531 return;
2532 }
Jan Kiszka487b3912008-09-26 09:30:56 +02002533 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002534 if (vcpu->arch.nmi_injected) {
2535 vmx_inject_nmi(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01002536 if (vcpu->arch.nmi_pending)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002537 enable_nmi_window(vcpu);
2538 else if (vcpu->arch.irq_summary
2539 || kvm_run->request_interrupt_window)
2540 enable_irq_window(vcpu);
2541 return;
2542 }
Jan Kiszka487b3912008-09-26 09:30:56 +02002543
Jan Kiszkaf460ee42008-09-26 09:30:50 +02002544 if (vcpu->arch.interrupt_window_open) {
2545 if (vcpu->arch.irq_summary && !vcpu->arch.interrupt.pending)
Gleb Natapovfe4c7b12009-03-23 11:23:18 +02002546 kvm_queue_interrupt(vcpu, kvm_pop_irq(vcpu));
Jan Kiszkaf460ee42008-09-26 09:30:50 +02002547
2548 if (vcpu->arch.interrupt.pending)
2549 vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
2550 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002551 if (!vcpu->arch.interrupt_window_open &&
2552 (vcpu->arch.irq_summary || kvm_run->request_interrupt_window))
Jan Kiszkaf460ee42008-09-26 09:30:50 +02002553 enable_irq_window(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002554}
2555
Izik Eiduscbc94022007-10-25 00:29:55 +02002556static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2557{
2558 int ret;
2559 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002560 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002561 .guest_phys_addr = addr,
2562 .memory_size = PAGE_SIZE * 3,
2563 .flags = 0,
2564 };
2565
2566 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2567 if (ret)
2568 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002569 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002570 return 0;
2571}
2572
Avi Kivity6aa8b732006-12-10 02:21:36 -08002573static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2574 int vec, u32 err_code)
2575{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002576 /*
2577 * Instruction with address size override prefix opcode 0x67
2578 * Cause the #SS fault with 0 error code in VM86 mode.
2579 */
2580 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02002581 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002583 /*
2584 * Forward all other exceptions that are valid in real mode.
2585 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2586 * the required debugging infrastructure rework.
2587 */
2588 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002589 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002590 if (vcpu->guest_debug &
2591 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2592 return 0;
2593 kvm_queue_exception(vcpu, vec);
2594 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002595 case BP_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002596 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2597 return 0;
2598 /* fall through */
2599 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002600 case OF_VECTOR:
2601 case BR_VECTOR:
2602 case UD_VECTOR:
2603 case DF_VECTOR:
2604 case SS_VECTOR:
2605 case GP_VECTOR:
2606 case MF_VECTOR:
2607 kvm_queue_exception(vcpu, vec);
2608 return 1;
2609 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610 return 0;
2611}
2612
2613static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2614{
Avi Kivity1155f762007-11-22 11:30:47 +02002615 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002616 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002617 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002618 u32 vect_info;
2619 enum emulation_result er;
2620
Avi Kivity1155f762007-11-22 11:30:47 +02002621 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002622 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2623
2624 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002625 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002627 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628
Jan Kiszkae4a41882008-09-26 09:30:46 +02002629 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002630 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002631
2632 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002633 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002634 return 1;
2635 }
2636
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002637 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08002638 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002639 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002640 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002641 return 1;
2642 }
2643
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002645 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002646 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2648 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002649 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02002650 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002651 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002653 KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2,
2654 (u32)((u64)cr2 >> 32), handler);
Avi Kivityf7d92382008-07-03 16:14:28 +03002655 if (vcpu->arch.interrupt.pending || vcpu->arch.exception.pending)
Avi Kivity577bdc42008-07-19 08:57:05 +03002656 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002657 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002658 }
2659
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002660 if (vcpu->arch.rmode.active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002661 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002662 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002663 if (vcpu->arch.halt_request) {
2664 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002665 return kvm_emulate_halt(vcpu);
2666 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002667 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002668 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002669
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002670 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002671 switch (ex_no) {
2672 case DB_VECTOR:
2673 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2674 if (!(vcpu->guest_debug &
2675 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2676 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2677 kvm_queue_exception(vcpu, DB_VECTOR);
2678 return 1;
2679 }
2680 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2681 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2682 /* fall through */
2683 case BP_VECTOR:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002685 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2686 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002687 break;
2688 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002689 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2690 kvm_run->ex.exception = ex_no;
2691 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002692 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002694 return 0;
2695}
2696
2697static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2698 struct kvm_run *kvm_run)
2699{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002700 ++vcpu->stat.irq_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002701 KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702 return 1;
2703}
2704
Avi Kivity988ad742007-02-12 00:54:36 -08002705static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2706{
2707 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2708 return 0;
2709}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2712{
He, Qingbfdaab02007-09-12 14:18:28 +08002713 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01002714 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002715 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002716
Avi Kivity1165f5f2007-04-19 17:27:43 +03002717 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002718 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002719 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002720
2721 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002722 if (emulate_instruction(vcpu,
2723 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002724 return 0;
2725 return 1;
2726 }
2727
2728 size = (exit_qualification & 7) + 1;
2729 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002730 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002731
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002732 skip_emulated_instruction(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002733 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002734}
2735
Ingo Molnar102d8322007-02-19 14:37:47 +02002736static void
2737vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2738{
2739 /*
2740 * Patch in the VMCALL instruction:
2741 */
2742 hypercall[0] = 0x0f;
2743 hypercall[1] = 0x01;
2744 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002745}
2746
Avi Kivity6aa8b732006-12-10 02:21:36 -08002747static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2748{
He, Qingbfdaab02007-09-12 14:18:28 +08002749 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750 int cr;
2751 int reg;
2752
He, Qingbfdaab02007-09-12 14:18:28 +08002753 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002754 cr = exit_qualification & 15;
2755 reg = (exit_qualification >> 8) & 15;
2756 switch ((exit_qualification >> 4) & 3) {
2757 case 0: /* mov to cr */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002758 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr,
2759 (u32)kvm_register_read(vcpu, reg),
2760 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
2761 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 switch (cr) {
2763 case 0:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002764 kvm_set_cr0(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002765 skip_emulated_instruction(vcpu);
2766 return 1;
2767 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002768 kvm_set_cr3(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002769 skip_emulated_instruction(vcpu);
2770 return 1;
2771 case 4:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002772 kvm_set_cr4(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002773 skip_emulated_instruction(vcpu);
2774 return 1;
2775 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002776 kvm_set_cr8(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777 skip_emulated_instruction(vcpu);
Avi Kivitye5314062007-12-06 16:32:45 +02002778 if (irqchip_in_kernel(vcpu->kvm))
2779 return 1;
Yang, Sheng253abde2007-08-16 13:01:00 +03002780 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2781 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782 };
2783 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002784 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002785 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002786 vcpu->arch.cr0 &= ~X86_CR0_TS;
2787 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002788 vmx_fpu_activate(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002789 KVMTRACE_0D(CLTS, vcpu, handler);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002790 skip_emulated_instruction(vcpu);
2791 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002792 case 1: /*mov from cr*/
2793 switch (cr) {
2794 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002795 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002796 KVMTRACE_3D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002797 (u32)kvm_register_read(vcpu, reg),
2798 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002799 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002800 skip_emulated_instruction(vcpu);
2801 return 1;
2802 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002803 kvm_register_write(vcpu, reg, kvm_get_cr8(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002804 KVMTRACE_2D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002805 (u32)kvm_register_read(vcpu, reg), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002806 skip_emulated_instruction(vcpu);
2807 return 1;
2808 }
2809 break;
2810 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002811 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812
2813 skip_emulated_instruction(vcpu);
2814 return 1;
2815 default:
2816 break;
2817 }
2818 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002819 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002820 (int)(exit_qualification >> 4) & 3, cr);
2821 return 0;
2822}
2823
2824static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2825{
He, Qingbfdaab02007-09-12 14:18:28 +08002826 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002827 unsigned long val;
2828 int dr, reg;
2829
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002830 dr = vmcs_readl(GUEST_DR7);
2831 if (dr & DR7_GD) {
2832 /*
2833 * As the vm-exit takes precedence over the debug trap, we
2834 * need to emulate the latter, either for the host or the
2835 * guest debugging itself.
2836 */
2837 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
2838 kvm_run->debug.arch.dr6 = vcpu->arch.dr6;
2839 kvm_run->debug.arch.dr7 = dr;
2840 kvm_run->debug.arch.pc =
2841 vmcs_readl(GUEST_CS_BASE) +
2842 vmcs_readl(GUEST_RIP);
2843 kvm_run->debug.arch.exception = DB_VECTOR;
2844 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2845 return 0;
2846 } else {
2847 vcpu->arch.dr7 &= ~DR7_GD;
2848 vcpu->arch.dr6 |= DR6_BD;
2849 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2850 kvm_queue_exception(vcpu, DB_VECTOR);
2851 return 1;
2852 }
2853 }
2854
He, Qingbfdaab02007-09-12 14:18:28 +08002855 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002856 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
2857 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
2858 if (exit_qualification & TYPE_MOV_FROM_DR) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002859 switch (dr) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002860 case 0 ... 3:
2861 val = vcpu->arch.db[dr];
2862 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002864 val = vcpu->arch.dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002865 break;
2866 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002867 val = vcpu->arch.dr7;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002868 break;
2869 default:
2870 val = 0;
2871 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002872 kvm_register_write(vcpu, reg, val);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002873 KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874 } else {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002875 val = vcpu->arch.regs[reg];
2876 switch (dr) {
2877 case 0 ... 3:
2878 vcpu->arch.db[dr] = val;
2879 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
2880 vcpu->arch.eff_db[dr] = val;
2881 break;
2882 case 4 ... 5:
2883 if (vcpu->arch.cr4 & X86_CR4_DE)
2884 kvm_queue_exception(vcpu, UD_VECTOR);
2885 break;
2886 case 6:
2887 if (val & 0xffffffff00000000ULL) {
2888 kvm_queue_exception(vcpu, GP_VECTOR);
2889 break;
2890 }
2891 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
2892 break;
2893 case 7:
2894 if (val & 0xffffffff00000000ULL) {
2895 kvm_queue_exception(vcpu, GP_VECTOR);
2896 break;
2897 }
2898 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
2899 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
2900 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2901 vcpu->arch.switch_db_regs =
2902 (val & DR7_BP_EN_MASK);
2903 }
2904 break;
2905 }
2906 KVMTRACE_2D(DR_WRITE, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908 skip_emulated_instruction(vcpu);
2909 return 1;
2910}
2911
2912static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2913{
Avi Kivity06465c52007-02-28 20:46:53 +02002914 kvm_emulate_cpuid(vcpu);
2915 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002916}
2917
2918static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2919{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002920 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002921 u64 data;
2922
2923 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002924 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002925 return 1;
2926 }
2927
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002928 KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32),
2929 handler);
2930
Avi Kivity6aa8b732006-12-10 02:21:36 -08002931 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002932 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
2933 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002934 skip_emulated_instruction(vcpu);
2935 return 1;
2936}
2937
2938static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2939{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002940 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
2941 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
2942 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002943
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002944 KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32),
2945 handler);
2946
Avi Kivity6aa8b732006-12-10 02:21:36 -08002947 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002948 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002949 return 1;
2950 }
2951
2952 skip_emulated_instruction(vcpu);
2953 return 1;
2954}
2955
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002956static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2957 struct kvm_run *kvm_run)
2958{
2959 return 1;
2960}
2961
Avi Kivity6aa8b732006-12-10 02:21:36 -08002962static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2963 struct kvm_run *kvm_run)
2964{
Eddie Dong85f455f2007-07-06 12:20:49 +03002965 u32 cpu_based_vm_exec_control;
2966
2967 /* clear pending irq */
2968 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2969 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2970 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002971
2972 KVMTRACE_0D(PEND_INTR, vcpu, handler);
Jan Kiszkaa26bf122008-09-26 09:30:45 +02002973 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002974
Dor Laorc1150d82007-01-05 16:36:24 -08002975 /*
2976 * If the user space waits to inject interrupts, exit as soon as
2977 * possible
2978 */
2979 if (kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002980 !vcpu->arch.irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002981 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08002982 return 0;
2983 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002984 return 1;
2985}
2986
2987static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2988{
2989 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002990 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002991}
2992
Ingo Molnarc21415e2007-02-19 14:37:47 +02002993static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2994{
Dor Laor510043d2007-02-19 18:25:43 +02002995 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002996 kvm_emulate_hypercall(vcpu);
2997 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02002998}
2999
Marcelo Tosattia7052892008-09-23 13:18:35 -03003000static int handle_invlpg(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3001{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003002 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003003
3004 kvm_mmu_invlpg(vcpu, exit_qualification);
3005 skip_emulated_instruction(vcpu);
3006 return 1;
3007}
3008
Eddie Donge5edaa02007-11-11 12:28:35 +02003009static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3010{
3011 skip_emulated_instruction(vcpu);
3012 /* TODO: Add support for VT-d/pass-through device */
3013 return 1;
3014}
3015
Sheng Yangf78e0e22007-10-29 09:40:42 +08003016static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3017{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003018 unsigned long exit_qualification;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003019 enum emulation_result er;
3020 unsigned long offset;
3021
Sheng Yangf9c617f2009-03-25 10:08:52 +08003022 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003023 offset = exit_qualification & 0xffful;
3024
3025 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3026
3027 if (er != EMULATE_DONE) {
3028 printk(KERN_ERR
3029 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3030 offset);
3031 return -ENOTSUPP;
3032 }
3033 return 1;
3034}
3035
Izik Eidus37817f22008-03-24 23:14:53 +02003036static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3037{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003038 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003039 unsigned long exit_qualification;
3040 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003041 int reason, type, idt_v;
3042
3043 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3044 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003045
3046 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3047
3048 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003049 if (reason == TASK_SWITCH_GATE && idt_v) {
3050 switch (type) {
3051 case INTR_TYPE_NMI_INTR:
3052 vcpu->arch.nmi_injected = false;
3053 if (cpu_has_virtual_nmis())
3054 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3055 GUEST_INTR_STATE_NMI);
3056 break;
3057 case INTR_TYPE_EXT_INTR:
3058 kvm_clear_interrupt_queue(vcpu);
3059 break;
3060 case INTR_TYPE_HARD_EXCEPTION:
3061 case INTR_TYPE_SOFT_EXCEPTION:
3062 kvm_clear_exception_queue(vcpu);
3063 break;
3064 default:
3065 break;
3066 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003067 }
Izik Eidus37817f22008-03-24 23:14:53 +02003068 tss_selector = exit_qualification;
3069
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003070 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3071 type != INTR_TYPE_EXT_INTR &&
3072 type != INTR_TYPE_NMI_INTR))
3073 skip_emulated_instruction(vcpu);
3074
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003075 if (!kvm_task_switch(vcpu, tss_selector, reason))
3076 return 0;
3077
3078 /* clear all local breakpoint enable flags */
3079 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3080
3081 /*
3082 * TODO: What about debug traps on tss switch?
3083 * Are we supposed to inject them and update dr6?
3084 */
3085
3086 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003087}
3088
Sheng Yang14394422008-04-28 12:24:45 +08003089static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3090{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003091 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003092 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003093 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003094
Sheng Yangf9c617f2009-03-25 10:08:52 +08003095 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003096
3097 if (exit_qualification & (1 << 6)) {
3098 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
3099 return -ENOTSUPP;
3100 }
3101
3102 gla_validity = (exit_qualification >> 7) & 0x3;
3103 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3104 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3105 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3106 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003107 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003108 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3109 (long unsigned int)exit_qualification);
3110 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3111 kvm_run->hw.hardware_exit_reason = 0;
3112 return -ENOTSUPP;
3113 }
3114
3115 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Sheng Yang49cd7d22009-02-11 13:50:40 +08003116 return kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003117}
3118
Sheng Yangf08864b2008-05-15 18:23:25 +08003119static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3120{
3121 u32 cpu_based_vm_exec_control;
3122
3123 /* clear pending NMI */
3124 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3125 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3126 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3127 ++vcpu->stat.nmi_window_exits;
3128
3129 return 1;
3130}
3131
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003132static void handle_invalid_guest_state(struct kvm_vcpu *vcpu,
3133 struct kvm_run *kvm_run)
3134{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003135 struct vcpu_vmx *vmx = to_vmx(vcpu);
3136 enum emulation_result err = EMULATE_DONE;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003137
3138 preempt_enable();
3139 local_irq_enable();
3140
3141 while (!guest_state_valid(vcpu)) {
3142 err = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3143
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003144 if (err == EMULATE_DO_MMIO)
3145 break;
3146
3147 if (err != EMULATE_DONE) {
3148 kvm_report_emulation_failure(vcpu, "emulation failure");
3149 return;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003150 }
3151
3152 if (signal_pending(current))
3153 break;
3154 if (need_resched())
3155 schedule();
3156 }
3157
3158 local_irq_disable();
3159 preempt_disable();
Avi Kivity8b3079a2009-01-05 12:10:54 +02003160
3161 vmx->invalid_state_emulation_result = err;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003162}
3163
Avi Kivity6aa8b732006-12-10 02:21:36 -08003164/*
3165 * The exit handlers return 1 if the exit was handled fully and guest execution
3166 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3167 * to be done to userspace and return 0.
3168 */
3169static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
3170 struct kvm_run *kvm_run) = {
3171 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3172 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003173 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003174 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003175 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003176 [EXIT_REASON_CR_ACCESS] = handle_cr,
3177 [EXIT_REASON_DR_ACCESS] = handle_dr,
3178 [EXIT_REASON_CPUID] = handle_cpuid,
3179 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3180 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3181 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3182 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003183 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003184 [EXIT_REASON_VMCALL] = handle_vmcall,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003185 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3186 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003187 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003188 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Sheng Yang14394422008-04-28 12:24:45 +08003189 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003190};
3191
3192static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003193 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003194
3195/*
3196 * The guest has exited. See if we can fix it or if we need userspace
3197 * assistance.
3198 */
Avi Kivity6062d012009-03-23 17:35:17 +02003199static int vmx_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003200{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003201 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity29bd8a72007-09-10 17:27:03 +03003202 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1155f762007-11-22 11:30:47 +02003203 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003204
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003205 KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)kvm_rip_read(vcpu),
3206 (u32)((u64)kvm_rip_read(vcpu) >> 32), entryexit);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003207
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003208 /* If we need to emulate an MMIO from handle_invalid_guest_state
3209 * we just return 0 */
Avi Kivity10f32d82009-01-05 00:53:19 +02003210 if (vmx->emulation_required && emulate_invalid_guest_state) {
3211 if (guest_state_valid(vcpu))
3212 vmx->emulation_required = 0;
Avi Kivity8b3079a2009-01-05 12:10:54 +02003213 return vmx->invalid_state_emulation_result != EMULATE_DO_MMIO;
Avi Kivity10f32d82009-01-05 00:53:19 +02003214 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003215
Sheng Yang14394422008-04-28 12:24:45 +08003216 /* Access CR3 don't cause VMExit in paging mode, so we need
3217 * to sync with guest real CR3. */
Avi Kivity089d0342009-03-23 18:26:32 +02003218 if (enable_ept && is_paging(vcpu)) {
Sheng Yang14394422008-04-28 12:24:45 +08003219 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3220 ept_load_pdptrs(vcpu);
3221 }
3222
Avi Kivity29bd8a72007-09-10 17:27:03 +03003223 if (unlikely(vmx->fail)) {
3224 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3225 kvm_run->fail_entry.hardware_entry_failure_reason
3226 = vmcs_read32(VM_INSTRUCTION_ERROR);
3227 return 0;
3228 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003229
Mike Dayd77c26f2007-10-08 09:02:08 -04003230 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003231 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003232 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3233 exit_reason != EXIT_REASON_TASK_SWITCH))
3234 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3235 "(0x%x) and exit reason is 0x%x\n",
3236 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003237
3238 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
3239 if (vcpu->arch.interrupt_window_open) {
3240 vmx->soft_vnmi_blocked = 0;
3241 vcpu->arch.nmi_window_open = 1;
3242 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003243 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003244 /*
3245 * This CPU don't support us in finding the end of an
3246 * NMI-blocked window if the guest runs with IRQs
3247 * disabled. So we pull the trigger after 1 s of
3248 * futile waiting, but inform the user about this.
3249 */
3250 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3251 "state on VCPU %d after 1 s timeout\n",
3252 __func__, vcpu->vcpu_id);
3253 vmx->soft_vnmi_blocked = 0;
3254 vmx->vcpu.arch.nmi_window_open = 1;
3255 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003256 }
3257
Avi Kivity6aa8b732006-12-10 02:21:36 -08003258 if (exit_reason < kvm_vmx_max_exit_handlers
3259 && kvm_vmx_exit_handlers[exit_reason])
3260 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
3261 else {
3262 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3263 kvm_run->hw.hardware_exit_reason = exit_reason;
3264 }
3265 return 0;
3266}
3267
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003268static void update_tpr_threshold(struct kvm_vcpu *vcpu)
3269{
3270 int max_irr, tpr;
3271
3272 if (!vm_need_tpr_shadow(vcpu->kvm))
3273 return;
3274
3275 if (!kvm_lapic_enabled(vcpu) ||
3276 ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
3277 vmcs_write32(TPR_THRESHOLD, 0);
3278 return;
3279 }
3280
3281 tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
3282 vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
3283}
3284
Avi Kivitycf393f72008-07-01 16:20:21 +03003285static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3286{
3287 u32 exit_intr_info;
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003288 u32 idt_vectoring_info = vmx->idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003289 bool unblock_nmi;
3290 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003291 int type;
3292 bool idtv_info_valid;
Avi Kivitycf393f72008-07-01 16:20:21 +03003293
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003294 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivitycf393f72008-07-01 16:20:21 +03003295 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
3296 if (cpu_has_virtual_nmis()) {
3297 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3298 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3299 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003300 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03003301 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3302 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003303 * SDM 3: 23.2.2 (September 2008)
3304 * Bit 12 is undefined in any of the following cases:
3305 * If the VM exit sets the valid bit in the IDT-vectoring
3306 * information field.
3307 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03003308 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003309 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
3310 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03003311 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3312 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003313 } else if (unlikely(vmx->soft_vnmi_blocked))
3314 vmx->vnmi_blocked_time +=
3315 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003316
Gleb Natapov37b96e92009-03-30 16:03:13 +03003317 vmx->vcpu.arch.nmi_injected = false;
3318 kvm_clear_exception_queue(&vmx->vcpu);
3319 kvm_clear_interrupt_queue(&vmx->vcpu);
3320
3321 if (!idtv_info_valid)
3322 return;
3323
Avi Kivity668f6122008-07-02 09:28:55 +03003324 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3325 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003326
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003327 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003328 case INTR_TYPE_NMI_INTR:
3329 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03003330 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003331 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03003332 * Clear bit "block by NMI" before VM entry if a NMI
3333 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03003334 */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003335 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3336 GUEST_INTR_STATE_NMI);
3337 break;
3338 case INTR_TYPE_HARD_EXCEPTION:
3339 case INTR_TYPE_SOFT_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03003340 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003341 u32 err = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3342 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03003343 } else
3344 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003345 break;
3346 case INTR_TYPE_EXT_INTR:
Avi Kivityf7d92382008-07-03 16:14:28 +03003347 kvm_queue_interrupt(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003348 break;
3349 default:
3350 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03003351 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003352}
3353
Eddie Dong85f455f2007-07-06 12:20:49 +03003354static void vmx_intr_assist(struct kvm_vcpu *vcpu)
3355{
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003356 update_tpr_threshold(vcpu);
3357
Jan Kiszka33f089c2008-09-26 09:30:49 +02003358 vmx_update_window_states(vcpu);
3359
Jan Kiszka55934c02008-12-15 13:52:10 +01003360 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3361 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3362 GUEST_INTR_STATE_STI |
3363 GUEST_INTR_STATE_MOV_SS);
3364
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003365 if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
3366 if (vcpu->arch.interrupt.pending) {
3367 enable_nmi_window(vcpu);
3368 } else if (vcpu->arch.nmi_window_open) {
3369 vcpu->arch.nmi_pending = false;
3370 vcpu->arch.nmi_injected = true;
3371 } else {
3372 enable_nmi_window(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003373 return;
3374 }
Sheng Yangf08864b2008-05-15 18:23:25 +08003375 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003376 if (vcpu->arch.nmi_injected) {
3377 vmx_inject_nmi(vcpu);
3378 if (vcpu->arch.nmi_pending)
3379 enable_nmi_window(vcpu);
3380 else if (kvm_cpu_has_interrupt(vcpu))
3381 enable_irq_window(vcpu);
3382 return;
3383 }
Avi Kivityf7d92382008-07-03 16:14:28 +03003384 if (!vcpu->arch.interrupt.pending && kvm_cpu_has_interrupt(vcpu)) {
Jan Kiszka33f089c2008-09-26 09:30:49 +02003385 if (vcpu->arch.interrupt_window_open)
Avi Kivityf7d92382008-07-03 16:14:28 +03003386 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu));
3387 else
3388 enable_irq_window(vcpu);
3389 }
3390 if (vcpu->arch.interrupt.pending) {
3391 vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
Avi Kivitydf203ec2008-11-23 18:08:57 +02003392 if (kvm_cpu_has_interrupt(vcpu))
3393 enable_irq_window(vcpu);
Avi Kivityf7d92382008-07-03 16:14:28 +03003394 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003395}
3396
Avi Kivity9c8cba32007-11-22 11:42:59 +02003397/*
3398 * Failure to inject an interrupt should give us the information
3399 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3400 * when fetching the interrupt redirection bitmap in the real-mode
3401 * tss, this doesn't happen. So we do it ourselves.
3402 */
3403static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3404{
3405 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003406 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003407 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003408 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003409 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3410 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3411 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3412 return;
3413 }
3414 vmx->idt_vectoring_info =
3415 VECTORING_INFO_VALID_MASK
3416 | INTR_TYPE_EXT_INTR
3417 | vmx->rmode.irq.vector;
3418}
3419
Avi Kivityc8019492008-07-14 14:44:59 +03003420#ifdef CONFIG_X86_64
3421#define R "r"
3422#define Q "q"
3423#else
3424#define R "e"
3425#define Q "l"
3426#endif
3427
Avi Kivity04d2cc72007-09-10 18:10:54 +03003428static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003429{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003430 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1b6269d2007-10-09 12:12:19 +02003431 u32 intr_info;
Avi Kivitye6adf282007-04-30 16:07:54 +03003432
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003433 /* Record the guest's net vcpu time for enforced NMI injections. */
3434 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3435 vmx->entry_time = ktime_get();
3436
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003437 /* Handle invalid guest state instead of entering VMX */
3438 if (vmx->emulation_required && emulate_invalid_guest_state) {
3439 handle_invalid_guest_state(vcpu, kvm_run);
3440 return;
3441 }
3442
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003443 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3444 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3445 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3446 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3447
Avi Kivitye6adf282007-04-30 16:07:54 +03003448 /*
3449 * Loading guest fpu may have cleared host cr0.ts
3450 */
3451 vmcs_writel(HOST_CR0, read_cr0());
3452
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003453 set_debugreg(vcpu->arch.dr6, 6);
3454
Mike Dayd77c26f2007-10-08 09:02:08 -04003455 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003456 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003457 "push %%"R"dx; push %%"R"bp;"
3458 "push %%"R"cx \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03003459 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3460 "je 1f \n\t"
3461 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003462 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03003463 "1: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003464 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003465 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003466 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003467 "mov %c[cr2](%0), %%"R"ax \n\t"
3468 "mov %%"R"ax, %%cr2 \n\t"
3469 "mov %c[rax](%0), %%"R"ax \n\t"
3470 "mov %c[rbx](%0), %%"R"bx \n\t"
3471 "mov %c[rdx](%0), %%"R"dx \n\t"
3472 "mov %c[rsi](%0), %%"R"si \n\t"
3473 "mov %c[rdi](%0), %%"R"di \n\t"
3474 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003475#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003476 "mov %c[r8](%0), %%r8 \n\t"
3477 "mov %c[r9](%0), %%r9 \n\t"
3478 "mov %c[r10](%0), %%r10 \n\t"
3479 "mov %c[r11](%0), %%r11 \n\t"
3480 "mov %c[r12](%0), %%r12 \n\t"
3481 "mov %c[r13](%0), %%r13 \n\t"
3482 "mov %c[r14](%0), %%r14 \n\t"
3483 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003484#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003485 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3486
Avi Kivity6aa8b732006-12-10 02:21:36 -08003487 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003488 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003489 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003490 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003491 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003492 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003493 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003494 "xchg %0, (%%"R"sp) \n\t"
3495 "mov %%"R"ax, %c[rax](%0) \n\t"
3496 "mov %%"R"bx, %c[rbx](%0) \n\t"
3497 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3498 "mov %%"R"dx, %c[rdx](%0) \n\t"
3499 "mov %%"R"si, %c[rsi](%0) \n\t"
3500 "mov %%"R"di, %c[rdi](%0) \n\t"
3501 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003502#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003503 "mov %%r8, %c[r8](%0) \n\t"
3504 "mov %%r9, %c[r9](%0) \n\t"
3505 "mov %%r10, %c[r10](%0) \n\t"
3506 "mov %%r11, %c[r11](%0) \n\t"
3507 "mov %%r12, %c[r12](%0) \n\t"
3508 "mov %%r13, %c[r13](%0) \n\t"
3509 "mov %%r14, %c[r14](%0) \n\t"
3510 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003511#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003512 "mov %%cr2, %%"R"ax \n\t"
3513 "mov %%"R"ax, %c[cr2](%0) \n\t"
3514
3515 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003516 "setbe %c[fail](%0) \n\t"
3517 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3518 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3519 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03003520 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003521 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3522 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3523 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3524 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3525 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3526 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3527 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003528#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003529 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3530 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3531 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3532 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3533 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3534 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3535 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3536 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003537#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003538 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003539 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003540 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003541#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003542 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3543#endif
3544 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003546 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
3547 vcpu->arch.regs_dirty = 0;
3548
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003549 get_debugreg(vcpu->arch.dr6, 6);
3550
Avi Kivity1155f762007-11-22 11:30:47 +02003551 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003552 if (vmx->rmode.irq.pending)
3553 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003554
Jan Kiszka33f089c2008-09-26 09:30:49 +02003555 vmx_update_window_states(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003556
Mike Dayd77c26f2007-10-08 09:02:08 -04003557 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003558 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003559
3560 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
3561
3562 /* We need to handle NMIs before interrupts are enabled */
Jan Kiszkae4a41882008-09-26 09:30:46 +02003563 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Sheng Yangf08864b2008-05-15 18:23:25 +08003564 (intr_info & INTR_INFO_VALID_MASK)) {
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003565 KVMTRACE_0D(NMI, vcpu, handler);
Avi Kivity1b6269d2007-10-09 12:12:19 +02003566 asm("int $2");
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003567 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003568
3569 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003570}
3571
Avi Kivityc8019492008-07-14 14:44:59 +03003572#undef R
3573#undef Q
3574
Avi Kivity6aa8b732006-12-10 02:21:36 -08003575static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3576{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003577 struct vcpu_vmx *vmx = to_vmx(vcpu);
3578
3579 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003580 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003581 free_vmcs(vmx->vmcs);
3582 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003583 }
3584}
3585
3586static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3587{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003588 struct vcpu_vmx *vmx = to_vmx(vcpu);
3589
Sheng Yang2384d2b2008-01-17 15:14:33 +08003590 spin_lock(&vmx_vpid_lock);
3591 if (vmx->vpid != 0)
3592 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3593 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003594 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003595 kfree(vmx->host_msrs);
3596 kfree(vmx->guest_msrs);
3597 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003598 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003599}
3600
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003601static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003602{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003603 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003604 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003605 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003606
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003607 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003608 return ERR_PTR(-ENOMEM);
3609
Sheng Yang2384d2b2008-01-17 15:14:33 +08003610 allocate_vpid(vmx);
3611
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003612 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3613 if (err)
3614 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003615
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003616 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003617 if (!vmx->guest_msrs) {
3618 err = -ENOMEM;
3619 goto uninit_vcpu;
3620 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003621
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003622 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3623 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003624 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003625
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003626 vmx->vmcs = alloc_vmcs();
3627 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003628 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003629
3630 vmcs_clear(vmx->vmcs);
3631
Avi Kivity15ad7142007-07-11 18:17:21 +03003632 cpu = get_cpu();
3633 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003634 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003635 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003636 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003637 if (err)
3638 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003639 if (vm_need_virtualize_apic_accesses(kvm))
3640 if (alloc_apic_access_page(kvm) != 0)
3641 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003642
Avi Kivity089d0342009-03-23 18:26:32 +02003643 if (enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003644 if (alloc_identity_pagetable(kvm) != 0)
3645 goto free_vmcs;
3646
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003647 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003648
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003649free_vmcs:
3650 free_vmcs(vmx->vmcs);
3651free_msrs:
3652 kfree(vmx->host_msrs);
3653free_guest_msrs:
3654 kfree(vmx->guest_msrs);
3655uninit_vcpu:
3656 kvm_vcpu_uninit(&vmx->vcpu);
3657free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003658 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003659 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003660}
3661
Yang, Sheng002c7f72007-07-31 14:23:01 +03003662static void __init vmx_check_processor_compat(void *rtn)
3663{
3664 struct vmcs_config vmcs_conf;
3665
3666 *(int *)rtn = 0;
3667 if (setup_vmcs_config(&vmcs_conf) < 0)
3668 *(int *)rtn = -EIO;
3669 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3670 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3671 smp_processor_id());
3672 *(int *)rtn = -EIO;
3673 }
3674}
3675
Sheng Yang67253af2008-04-25 10:20:22 +08003676static int get_ept_level(void)
3677{
3678 return VMX_EPT_DEFAULT_GAW + 1;
3679}
3680
Sheng Yang64d4d522008-10-09 16:01:57 +08003681static int vmx_get_mt_mask_shift(void)
3682{
3683 return VMX_EPT_MT_EPTE_SHIFT;
3684}
3685
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003686static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003687 .cpu_has_kvm_support = cpu_has_kvm_support,
3688 .disabled_by_bios = vmx_disabled_by_bios,
3689 .hardware_setup = hardware_setup,
3690 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003691 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003692 .hardware_enable = hardware_enable,
3693 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08003694 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003695
3696 .vcpu_create = vmx_create_vcpu,
3697 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003698 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003699
Avi Kivity04d2cc72007-09-10 18:10:54 +03003700 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003701 .vcpu_load = vmx_vcpu_load,
3702 .vcpu_put = vmx_vcpu_put,
3703
3704 .set_guest_debug = set_guest_debug,
3705 .get_msr = vmx_get_msr,
3706 .set_msr = vmx_set_msr,
3707 .get_segment_base = vmx_get_segment_base,
3708 .get_segment = vmx_get_segment,
3709 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003710 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003711 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003712 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003713 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003714 .set_cr3 = vmx_set_cr3,
3715 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003716 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003717 .get_idt = vmx_get_idt,
3718 .set_idt = vmx_set_idt,
3719 .get_gdt = vmx_get_gdt,
3720 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003721 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003722 .get_rflags = vmx_get_rflags,
3723 .set_rflags = vmx_set_rflags,
3724
3725 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003726
Avi Kivity6aa8b732006-12-10 02:21:36 -08003727 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02003728 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003729 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02003730 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003731 .get_irq = vmx_get_irq,
3732 .set_irq = vmx_inject_irq,
Avi Kivity298101d2007-11-25 13:41:11 +02003733 .queue_exception = vmx_queue_exception,
3734 .exception_injected = vmx_exception_injected,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003735 .inject_pending_irq = vmx_intr_assist,
3736 .inject_pending_vectors = do_interrupt_requests,
Gleb Natapov78646122009-03-23 12:12:11 +02003737 .interrupt_allowed = vmx_interrupt_allowed,
Izik Eiduscbc94022007-10-25 00:29:55 +02003738 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003739 .get_tdp_level = get_ept_level,
Sheng Yang64d4d522008-10-09 16:01:57 +08003740 .get_mt_mask_shift = vmx_get_mt_mask_shift,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003741};
3742
3743static int __init vmx_init(void)
3744{
He, Qingfdef3ad2007-04-30 09:45:24 +03003745 int r;
3746
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003747 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03003748 if (!vmx_io_bitmap_a)
3749 return -ENOMEM;
3750
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003751 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03003752 if (!vmx_io_bitmap_b) {
3753 r = -ENOMEM;
3754 goto out;
3755 }
3756
Avi Kivity58972972009-02-24 22:26:47 +02003757 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
3758 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08003759 r = -ENOMEM;
3760 goto out1;
3761 }
3762
Avi Kivity58972972009-02-24 22:26:47 +02003763 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
3764 if (!vmx_msr_bitmap_longmode) {
3765 r = -ENOMEM;
3766 goto out2;
3767 }
3768
He, Qingfdef3ad2007-04-30 09:45:24 +03003769 /*
3770 * Allow direct access to the PC debug port (it is often used for I/O
3771 * delays, but the vmexits simply slow things down).
3772 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003773 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
3774 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003775
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003776 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003777
Avi Kivity58972972009-02-24 22:26:47 +02003778 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
3779 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08003780
Sheng Yang2384d2b2008-01-17 15:14:33 +08003781 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
3782
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003783 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003784 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02003785 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08003786
Avi Kivity58972972009-02-24 22:26:47 +02003787 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
3788 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
3789 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
3790 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
3791 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
3792 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03003793
Avi Kivity089d0342009-03-23 18:26:32 +02003794 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003795 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003796 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08003797 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08003798 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang64d4d522008-10-09 16:01:57 +08003799 VMX_EPT_EXECUTABLE_MASK,
3800 VMX_EPT_DEFAULT_MT << VMX_EPT_MT_EPTE_SHIFT);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003801 kvm_enable_tdp();
3802 } else
3803 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08003804
Avi Kivityc7addb92007-09-16 18:58:32 +02003805 if (bypass_guest_pf)
3806 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
3807
Sheng Yang14394422008-04-28 12:24:45 +08003808 ept_sync_global();
3809
He, Qingfdef3ad2007-04-30 09:45:24 +03003810 return 0;
3811
Avi Kivity58972972009-02-24 22:26:47 +02003812out3:
3813 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08003814out2:
Avi Kivity58972972009-02-24 22:26:47 +02003815 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03003816out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003817 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03003818out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003819 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003820 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003821}
3822
3823static void __exit vmx_exit(void)
3824{
Avi Kivity58972972009-02-24 22:26:47 +02003825 free_page((unsigned long)vmx_msr_bitmap_legacy);
3826 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003827 free_page((unsigned long)vmx_io_bitmap_b);
3828 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003829
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003830 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003831}
3832
3833module_init(vmx_init)
3834module_exit(vmx_exit)