blob: c87c93fd129ae66275143a62b071cbef122a35ce [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040020
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020023#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/mm.h>
25#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040026#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020027#include <linux/moduleparam.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030028#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030029#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080032#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020033#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020034#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080035#include <asm/mce.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080036
Avi Kivity4ecac3f2008-05-13 13:23:38 +030037#define __ex(x) __kvm_handle_fault_on_reboot(x)
38
Avi Kivity6aa8b732006-12-10 02:21:36 -080039MODULE_AUTHOR("Qumranet");
40MODULE_LICENSE("GPL");
41
Avi Kivity4462d212009-03-23 17:53:37 +020042static int __read_mostly bypass_guest_pf = 1;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020043module_param(bypass_guest_pf, bool, S_IRUGO);
Avi Kivityc7addb92007-09-16 18:58:32 +020044
Avi Kivity4462d212009-03-23 17:53:37 +020045static int __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020046module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080047
Avi Kivity4462d212009-03-23 17:53:37 +020048static int __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020049module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020050
Avi Kivity4462d212009-03-23 17:53:37 +020051static int __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020052module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080053
Avi Kivity4462d212009-03-23 17:53:37 +020054static int __read_mostly emulate_invalid_guest_state = 0;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020055module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030056
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040057struct vmcs {
58 u32 revision_id;
59 u32 abort;
60 char data[0];
61};
62
63struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100064 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +030065 struct list_head local_vcpus_link;
Avi Kivity313dbd42008-07-17 18:04:30 +030066 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040067 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030068 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020069 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040070 struct kvm_msr_entry *guest_msrs;
71 struct kvm_msr_entry *host_msrs;
72 int nmsrs;
73 int save_nmsrs;
74 int msr_offset_efer;
75#ifdef CONFIG_X86_64
76 int msr_offset_kernel_gs_base;
77#endif
78 struct vmcs *vmcs;
79 struct {
80 int loaded;
81 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020082 int gs_ldt_reload_needed;
83 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030084 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040085 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +020086 struct {
87 struct {
88 bool pending;
89 u8 vector;
90 unsigned rip;
91 } irq;
92 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +080093 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030094 bool emulation_required;
Avi Kivity8b3079a2009-01-05 12:10:54 +020095 enum emulation_result invalid_state_emulation_result;
Jan Kiszka3b86cd92008-09-26 09:30:57 +020096
97 /* Support for vnmi-less CPUs */
98 int soft_vnmi_blocked;
99 ktime_t entry_time;
100 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800101 u32 exit_reason;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400102};
103
104static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
105{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000106 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400107}
108
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800109static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800110static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300111
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112static DEFINE_PER_CPU(struct vmcs *, vmxarea);
113static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300114static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800115
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200116static unsigned long *vmx_io_bitmap_a;
117static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200118static unsigned long *vmx_msr_bitmap_legacy;
119static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300120
Sheng Yang2384d2b2008-01-17 15:14:33 +0800121static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
122static DEFINE_SPINLOCK(vmx_vpid_lock);
123
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300124static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125 int size;
126 int order;
127 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300128 u32 pin_based_exec_ctrl;
129 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800130 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300131 u32 vmexit_ctrl;
132 u32 vmentry_ctrl;
133} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800134
Hannes Ederefff9e52008-11-28 17:02:06 +0100135static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800136 u32 ept;
137 u32 vpid;
138} vmx_capability;
139
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140#define VMX_SEGMENT_FIELD(seg) \
141 [VCPU_SREG_##seg] = { \
142 .selector = GUEST_##seg##_SELECTOR, \
143 .base = GUEST_##seg##_BASE, \
144 .limit = GUEST_##seg##_LIMIT, \
145 .ar_bytes = GUEST_##seg##_AR_BYTES, \
146 }
147
148static struct kvm_vmx_segment_field {
149 unsigned selector;
150 unsigned base;
151 unsigned limit;
152 unsigned ar_bytes;
153} kvm_vmx_segment_fields[] = {
154 VMX_SEGMENT_FIELD(CS),
155 VMX_SEGMENT_FIELD(DS),
156 VMX_SEGMENT_FIELD(ES),
157 VMX_SEGMENT_FIELD(FS),
158 VMX_SEGMENT_FIELD(GS),
159 VMX_SEGMENT_FIELD(SS),
160 VMX_SEGMENT_FIELD(TR),
161 VMX_SEGMENT_FIELD(LDTR),
162};
163
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300164/*
165 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
166 * away by decrementing the array size.
167 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800168static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800169#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800170 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
171#endif
172 MSR_EFER, MSR_K6_STAR,
173};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200174#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800175
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400176static void load_msrs(struct kvm_msr_entry *e, int n)
177{
178 int i;
179
180 for (i = 0; i < n; ++i)
181 wrmsrl(e[i].index, e[i].data);
182}
183
184static void save_msrs(struct kvm_msr_entry *e, int n)
185{
186 int i;
187
188 for (i = 0; i < n; ++i)
189 rdmsrl(e[i].index, e[i].data);
190}
191
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192static inline int is_page_fault(u32 intr_info)
193{
194 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
195 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100196 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800197}
198
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300199static inline int is_no_device(u32 intr_info)
200{
201 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
202 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100203 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300204}
205
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500206static inline int is_invalid_opcode(u32 intr_info)
207{
208 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
209 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100210 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500211}
212
Avi Kivity6aa8b732006-12-10 02:21:36 -0800213static inline int is_external_interrupt(u32 intr_info)
214{
215 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
216 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
217}
218
Andi Kleena0861c02009-06-08 17:37:09 +0800219static inline int is_machine_check(u32 intr_info)
220{
221 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
222 INTR_INFO_VALID_MASK)) ==
223 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
224}
225
Sheng Yang25c5f222008-03-28 13:18:56 +0800226static inline int cpu_has_vmx_msr_bitmap(void)
227{
Sheng Yang04547152009-04-01 15:52:31 +0800228 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800229}
230
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800231static inline int cpu_has_vmx_tpr_shadow(void)
232{
Sheng Yang04547152009-04-01 15:52:31 +0800233 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800234}
235
236static inline int vm_need_tpr_shadow(struct kvm *kvm)
237{
Sheng Yang04547152009-04-01 15:52:31 +0800238 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800239}
240
Sheng Yangf78e0e22007-10-29 09:40:42 +0800241static inline int cpu_has_secondary_exec_ctrls(void)
242{
Sheng Yang04547152009-04-01 15:52:31 +0800243 return vmcs_config.cpu_based_exec_ctrl &
244 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800245}
246
Avi Kivity774ead32007-12-26 13:57:04 +0200247static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800248{
Sheng Yang04547152009-04-01 15:52:31 +0800249 return vmcs_config.cpu_based_2nd_exec_ctrl &
250 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
251}
252
253static inline bool cpu_has_vmx_flexpriority(void)
254{
255 return cpu_has_vmx_tpr_shadow() &&
256 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800257}
258
Sheng Yangd56f5462008-04-25 10:13:16 +0800259static inline int cpu_has_vmx_invept_individual_addr(void)
260{
Sheng Yang04547152009-04-01 15:52:31 +0800261 return !!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800262}
263
264static inline int cpu_has_vmx_invept_context(void)
265{
Sheng Yang04547152009-04-01 15:52:31 +0800266 return !!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800267}
268
269static inline int cpu_has_vmx_invept_global(void)
270{
Sheng Yang04547152009-04-01 15:52:31 +0800271 return !!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800272}
273
274static inline int cpu_has_vmx_ept(void)
275{
Sheng Yang04547152009-04-01 15:52:31 +0800276 return vmcs_config.cpu_based_2nd_exec_ctrl &
277 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800278}
279
Sheng Yangf78e0e22007-10-29 09:40:42 +0800280static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
281{
Sheng Yang04547152009-04-01 15:52:31 +0800282 return flexpriority_enabled &&
283 (cpu_has_vmx_virtualize_apic_accesses()) &&
284 (irqchip_in_kernel(kvm));
Sheng Yangf78e0e22007-10-29 09:40:42 +0800285}
286
Sheng Yang2384d2b2008-01-17 15:14:33 +0800287static inline int cpu_has_vmx_vpid(void)
288{
Sheng Yang04547152009-04-01 15:52:31 +0800289 return vmcs_config.cpu_based_2nd_exec_ctrl &
290 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800291}
292
Sheng Yangf08864b2008-05-15 18:23:25 +0800293static inline int cpu_has_virtual_nmis(void)
294{
295 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
296}
297
Sheng Yang04547152009-04-01 15:52:31 +0800298static inline bool report_flexpriority(void)
299{
300 return flexpriority_enabled;
301}
302
Rusty Russell8b9cf982007-07-30 16:31:43 +1000303static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800304{
305 int i;
306
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400307 for (i = 0; i < vmx->nmsrs; ++i)
308 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300309 return i;
310 return -1;
311}
312
Sheng Yang2384d2b2008-01-17 15:14:33 +0800313static inline void __invvpid(int ext, u16 vpid, gva_t gva)
314{
315 struct {
316 u64 vpid : 16;
317 u64 rsvd : 48;
318 u64 gva;
319 } operand = { vpid, 0, gva };
320
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300321 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800322 /* CF==1 or ZF==1 --> rc = -1 */
323 "; ja 1f ; ud2 ; 1:"
324 : : "a"(&operand), "c"(ext) : "cc", "memory");
325}
326
Sheng Yang14394422008-04-28 12:24:45 +0800327static inline void __invept(int ext, u64 eptp, gpa_t gpa)
328{
329 struct {
330 u64 eptp, gpa;
331 } operand = {eptp, gpa};
332
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300333 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800334 /* CF==1 or ZF==1 --> rc = -1 */
335 "; ja 1f ; ud2 ; 1:\n"
336 : : "a" (&operand), "c" (ext) : "cc", "memory");
337}
338
Rusty Russell8b9cf982007-07-30 16:31:43 +1000339static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300340{
341 int i;
342
Rusty Russell8b9cf982007-07-30 16:31:43 +1000343 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300344 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400345 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000346 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800347}
348
Avi Kivity6aa8b732006-12-10 02:21:36 -0800349static void vmcs_clear(struct vmcs *vmcs)
350{
351 u64 phys_addr = __pa(vmcs);
352 u8 error;
353
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300354 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800355 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
356 : "cc", "memory");
357 if (error)
358 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
359 vmcs, phys_addr);
360}
361
362static void __vcpu_clear(void *arg)
363{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000364 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800365 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366
Rusty Russell8b9cf982007-07-30 16:31:43 +1000367 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400368 vmcs_clear(vmx->vmcs);
369 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800370 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800371 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300372 list_del(&vmx->local_vcpus_link);
373 vmx->vcpu.cpu = -1;
374 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375}
376
Rusty Russell8b9cf982007-07-30 16:31:43 +1000377static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800378{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200379 if (vmx->vcpu.cpu == -1)
380 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200381 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800382}
383
Sheng Yang2384d2b2008-01-17 15:14:33 +0800384static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
385{
386 if (vmx->vpid == 0)
387 return;
388
389 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
390}
391
Sheng Yang14394422008-04-28 12:24:45 +0800392static inline void ept_sync_global(void)
393{
394 if (cpu_has_vmx_invept_global())
395 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
396}
397
398static inline void ept_sync_context(u64 eptp)
399{
Avi Kivity089d0342009-03-23 18:26:32 +0200400 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800401 if (cpu_has_vmx_invept_context())
402 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
403 else
404 ept_sync_global();
405 }
406}
407
408static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
409{
Avi Kivity089d0342009-03-23 18:26:32 +0200410 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800411 if (cpu_has_vmx_invept_individual_addr())
412 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
413 eptp, gpa);
414 else
415 ept_sync_context(eptp);
416 }
417}
418
Avi Kivity6aa8b732006-12-10 02:21:36 -0800419static unsigned long vmcs_readl(unsigned long field)
420{
421 unsigned long value;
422
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300423 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424 : "=a"(value) : "d"(field) : "cc");
425 return value;
426}
427
428static u16 vmcs_read16(unsigned long field)
429{
430 return vmcs_readl(field);
431}
432
433static u32 vmcs_read32(unsigned long field)
434{
435 return vmcs_readl(field);
436}
437
438static u64 vmcs_read64(unsigned long field)
439{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800440#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441 return vmcs_readl(field);
442#else
443 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
444#endif
445}
446
Avi Kivitye52de1b2007-01-05 16:36:56 -0800447static noinline void vmwrite_error(unsigned long field, unsigned long value)
448{
449 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
450 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
451 dump_stack();
452}
453
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454static void vmcs_writel(unsigned long field, unsigned long value)
455{
456 u8 error;
457
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300458 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400459 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800460 if (unlikely(error))
461 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462}
463
464static void vmcs_write16(unsigned long field, u16 value)
465{
466 vmcs_writel(field, value);
467}
468
469static void vmcs_write32(unsigned long field, u32 value)
470{
471 vmcs_writel(field, value);
472}
473
474static void vmcs_write64(unsigned long field, u64 value)
475{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800476 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300477#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800478 asm volatile ("");
479 vmcs_writel(field+1, value >> 32);
480#endif
481}
482
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300483static void vmcs_clear_bits(unsigned long field, u32 mask)
484{
485 vmcs_writel(field, vmcs_readl(field) & ~mask);
486}
487
488static void vmcs_set_bits(unsigned long field, u32 mask)
489{
490 vmcs_writel(field, vmcs_readl(field) | mask);
491}
492
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300493static void update_exception_bitmap(struct kvm_vcpu *vcpu)
494{
495 u32 eb;
496
Andi Kleena0861c02009-06-08 17:37:09 +0800497 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300498 if (!vcpu->fpu_active)
499 eb |= 1u << NM_VECTOR;
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100500 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
501 if (vcpu->guest_debug &
502 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
503 eb |= 1u << DB_VECTOR;
504 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
505 eb |= 1u << BP_VECTOR;
506 }
Nitin A Kamble56b237e2009-06-04 15:04:08 -0700507 if (vcpu->arch.rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300508 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200509 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800510 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300511 vmcs_write32(EXCEPTION_BITMAP, eb);
512}
513
Avi Kivity33ed6322007-05-02 16:54:03 +0300514static void reload_tss(void)
515{
Avi Kivity33ed6322007-05-02 16:54:03 +0300516 /*
517 * VT restores TR but not its size. Useless.
518 */
519 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200520 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300521
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300522 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300523 descs = (void *)gdt.base;
524 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
525 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300526}
527
Rusty Russell8b9cf982007-07-30 16:31:43 +1000528static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300529{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400530 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300531 u64 host_efer = vmx->host_msrs[efer_offset].data;
532 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
533 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300534
Avi Kivity51c6cf62007-08-29 03:48:05 +0300535 if (efer_offset < 0)
536 return;
537 /*
538 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
539 * outside long mode
540 */
541 ignore_bits = EFER_NX | EFER_SCE;
542#ifdef CONFIG_X86_64
543 ignore_bits |= EFER_LMA | EFER_LME;
544 /* SCE is meaningful only in long mode on Intel */
545 if (guest_efer & EFER_LMA)
546 ignore_bits &= ~(u64)EFER_SCE;
547#endif
548 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
549 return;
550
551 vmx->host_state.guest_efer_loaded = 1;
552 guest_efer &= ~ignore_bits;
553 guest_efer |= host_efer & ignore_bits;
554 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000555 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300556}
557
Avi Kivity51c6cf62007-08-29 03:48:05 +0300558static void reload_host_efer(struct vcpu_vmx *vmx)
559{
560 if (vmx->host_state.guest_efer_loaded) {
561 vmx->host_state.guest_efer_loaded = 0;
562 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
563 }
564}
565
Avi Kivity04d2cc72007-09-10 18:10:54 +0300566static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300567{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300568 struct vcpu_vmx *vmx = to_vmx(vcpu);
569
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400570 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300571 return;
572
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400573 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300574 /*
575 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
576 * allow segment selectors with cpl > 0 or ti == 1.
577 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300578 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200579 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300580 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200581 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400582 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200583 vmx->host_state.fs_reload_needed = 0;
584 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300585 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200586 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300587 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300588 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400589 if (!(vmx->host_state.gs_sel & 7))
590 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300591 else {
592 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200593 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300594 }
595
596#ifdef CONFIG_X86_64
597 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
598 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
599#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400600 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
601 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300602#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300603
604#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400605 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400606 save_msrs(vmx->host_msrs +
607 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400608
Avi Kivity707c0872007-05-02 17:33:43 +0300609#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400610 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300611 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300612}
613
Avi Kivitya9b21b62008-06-24 11:48:49 +0300614static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300615{
Avi Kivity15ad7142007-07-11 18:17:21 +0300616 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300617
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400618 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300619 return;
620
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200621 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400622 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200623 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300624 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200625 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300626 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300627 /*
628 * If we have to reload gs, we must take care to
629 * preserve our gs base.
630 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300631 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300632 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300633#ifdef CONFIG_X86_64
634 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
635#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300636 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300637 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200638 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400639 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
640 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300641 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300642}
643
Avi Kivitya9b21b62008-06-24 11:48:49 +0300644static void vmx_load_host_state(struct vcpu_vmx *vmx)
645{
646 preempt_disable();
647 __vmx_load_host_state(vmx);
648 preempt_enable();
649}
650
Avi Kivity6aa8b732006-12-10 02:21:36 -0800651/*
652 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
653 * vcpu mutex is already taken.
654 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300655static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400657 struct vcpu_vmx *vmx = to_vmx(vcpu);
658 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200659 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660
Eddie Donga3d7f852007-09-03 16:15:12 +0300661 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000662 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300663 kvm_migrate_timers(vcpu);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800664 vpid_sync_vcpu_all(vmx);
Avi Kivity543e4242008-05-13 16:22:47 +0300665 local_irq_disable();
666 list_add(&vmx->local_vcpus_link,
667 &per_cpu(vcpus_on_cpu, cpu));
668 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300669 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800670
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400671 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672 u8 error;
673
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400674 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300675 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
677 : "cc");
678 if (error)
679 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400680 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800681 }
682
683 if (vcpu->cpu != cpu) {
684 struct descriptor_table dt;
685 unsigned long sysenter_esp;
686
687 vcpu->cpu = cpu;
688 /*
689 * Linux uses per-cpu TSS and GDT, so set these when switching
690 * processors.
691 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300692 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
693 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800694 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
695
696 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
697 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300698
699 /*
700 * Make sure the time stamp counter is monotonous.
701 */
702 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200703 if (tsc_this < vcpu->arch.host_tsc) {
704 delta = vcpu->arch.host_tsc - tsc_this;
705 new_offset = vmcs_read64(TSC_OFFSET) + delta;
706 vmcs_write64(TSC_OFFSET, new_offset);
707 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709}
710
711static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
712{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300713 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714}
715
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300716static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
717{
718 if (vcpu->fpu_active)
719 return;
720 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000721 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800722 if (vcpu->arch.cr0 & X86_CR0_TS)
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
727static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
728{
729 if (!vcpu->fpu_active)
730 return;
731 vcpu->fpu_active = 0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000732 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300733 update_exception_bitmap(vcpu);
734}
735
Avi Kivity6aa8b732006-12-10 02:21:36 -0800736static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
737{
738 return vmcs_readl(GUEST_RFLAGS);
739}
740
741static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
742{
Nitin A Kamble56b237e2009-06-04 15:04:08 -0700743 if (vcpu->arch.rmode.vm86_active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100744 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800745 vmcs_writel(GUEST_RFLAGS, rflags);
746}
747
Glauber Costa2809f5d2009-05-12 16:21:05 -0400748static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
749{
750 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
751 int ret = 0;
752
753 if (interruptibility & GUEST_INTR_STATE_STI)
754 ret |= X86_SHADOW_INT_STI;
755 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
756 ret |= X86_SHADOW_INT_MOV_SS;
757
758 return ret & mask;
759}
760
761static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
762{
763 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
764 u32 interruptibility = interruptibility_old;
765
766 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
767
768 if (mask & X86_SHADOW_INT_MOV_SS)
769 interruptibility |= GUEST_INTR_STATE_MOV_SS;
770 if (mask & X86_SHADOW_INT_STI)
771 interruptibility |= GUEST_INTR_STATE_STI;
772
773 if ((interruptibility != interruptibility_old))
774 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
775}
776
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
778{
779 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800780
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300781 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800782 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300783 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784
Glauber Costa2809f5d2009-05-12 16:21:05 -0400785 /* skipping an emulated instruction also counts */
786 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800787}
788
Avi Kivity298101d2007-11-25 13:41:11 +0200789static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
790 bool has_error_code, u32 error_code)
791{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200792 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100793 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200794
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100795 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200796 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100797 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
798 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200799
Nitin A Kamble56b237e2009-06-04 15:04:08 -0700800 if (vcpu->arch.rmode.vm86_active) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200801 vmx->rmode.irq.pending = true;
802 vmx->rmode.irq.vector = nr;
803 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +0300804 if (kvm_exception_is_soft(nr))
805 vmx->rmode.irq.rip +=
806 vmx->vcpu.arch.event_exit_inst_len;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100807 intr_info |= INTR_TYPE_SOFT_INTR;
808 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200809 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
810 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
811 return;
812 }
813
Gleb Natapov66fd3f72009-05-11 13:35:50 +0300814 if (kvm_exception_is_soft(nr)) {
815 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
816 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100817 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
818 } else
819 intr_info |= INTR_TYPE_HARD_EXCEPTION;
820
821 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200822}
823
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824/*
Eddie Donga75beee2007-05-17 18:55:15 +0300825 * Swap MSR entry in host/guest MSR entry array.
826 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200827#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000828static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300829{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400830 struct kvm_msr_entry tmp;
831
832 tmp = vmx->guest_msrs[to];
833 vmx->guest_msrs[to] = vmx->guest_msrs[from];
834 vmx->guest_msrs[from] = tmp;
835 tmp = vmx->host_msrs[to];
836 vmx->host_msrs[to] = vmx->host_msrs[from];
837 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300838}
Gabriel C54e11fa2007-08-01 16:23:10 +0200839#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300840
841/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300842 * Set up the vmcs to automatically save and restore system
843 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
844 * mode, as fiddling with msrs is very expensive.
845 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000846static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300847{
Eddie Dong2cc51562007-05-21 07:28:09 +0300848 int save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +0200849 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +0300850
Avi Kivity33f9c502008-02-27 16:06:57 +0200851 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300852 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300853#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000854 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300855 int index;
856
Rusty Russell8b9cf982007-07-30 16:31:43 +1000857 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300858 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000859 move_msr_up(vmx, index, save_nmsrs++);
860 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300861 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000862 move_msr_up(vmx, index, save_nmsrs++);
863 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300864 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000865 move_msr_up(vmx, index, save_nmsrs++);
866 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300867 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000868 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300869 /*
870 * MSR_K6_STAR is only needed on long mode guests, and only
871 * if efer.sce is enabled.
872 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000873 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800874 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000875 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300876 }
Eddie Donga75beee2007-05-17 18:55:15 +0300877#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400878 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300879
Eddie Donga75beee2007-05-17 18:55:15 +0300880#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400881 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000882 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300883#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000884 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivity58972972009-02-24 22:26:47 +0200885
886 if (cpu_has_vmx_msr_bitmap()) {
887 if (is_long_mode(&vmx->vcpu))
888 msr_bitmap = vmx_msr_bitmap_longmode;
889 else
890 msr_bitmap = vmx_msr_bitmap_legacy;
891
892 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
893 }
Avi Kivitye38aea32007-04-19 13:22:48 +0300894}
895
896/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897 * reads and returns guest's timestamp counter "register"
898 * guest_tsc = host_tsc + tsc_offset -- 21.3
899 */
900static u64 guest_read_tsc(void)
901{
902 u64 host_tsc, tsc_offset;
903
904 rdtscll(host_tsc);
905 tsc_offset = vmcs_read64(TSC_OFFSET);
906 return host_tsc + tsc_offset;
907}
908
909/*
910 * writes 'guest_tsc' into guest's timestamp counter "register"
911 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
912 */
Marcelo Tosatti53f658b32008-12-11 20:45:05 +0100913static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
916}
917
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918/*
919 * Reads an msr value (of 'msr_index') into 'pdata'.
920 * Returns 0 on success, non-0 otherwise.
921 * Assumes vcpu_load() was already called.
922 */
923static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
924{
925 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400926 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927
928 if (!pdata) {
929 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
930 return -EINVAL;
931 }
932
933 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800934#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935 case MSR_FS_BASE:
936 data = vmcs_readl(GUEST_FS_BASE);
937 break;
938 case MSR_GS_BASE:
939 data = vmcs_readl(GUEST_GS_BASE);
940 break;
941 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800942 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943#endif
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530944 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 data = guest_read_tsc();
946 break;
947 case MSR_IA32_SYSENTER_CS:
948 data = vmcs_read32(GUEST_SYSENTER_CS);
949 break;
950 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200951 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800952 break;
953 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200954 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956 default:
Avi Kivity516a1a72009-02-15 02:32:07 +0200957 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +1000958 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800959 if (msr) {
960 data = msr->data;
961 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800963 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964 }
965
966 *pdata = data;
967 return 0;
968}
969
970/*
971 * Writes msr value into into the appropriate "register".
972 * Returns 0 on success, non-0 otherwise.
973 * Assumes vcpu_load() was already called.
974 */
975static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
976{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400977 struct vcpu_vmx *vmx = to_vmx(vcpu);
978 struct kvm_msr_entry *msr;
Marcelo Tosatti53f658b32008-12-11 20:45:05 +0100979 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +0300980 int ret = 0;
981
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -0800983 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300984 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +0300985 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +0300986 break;
Avi Kivity16175a72009-03-23 22:13:44 +0200987#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 case MSR_FS_BASE:
989 vmcs_writel(GUEST_FS_BASE, data);
990 break;
991 case MSR_GS_BASE:
992 vmcs_writel(GUEST_GS_BASE, data);
993 break;
994#endif
995 case MSR_IA32_SYSENTER_CS:
996 vmcs_write32(GUEST_SYSENTER_CS, data);
997 break;
998 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200999 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000 break;
1001 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001002 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301004 case MSR_IA32_TSC:
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01001005 rdtscll(host_tsc);
1006 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007 break;
Chris Lalancetteefa67e02008-06-20 09:51:30 +02001008 case MSR_P6_PERFCTR0:
1009 case MSR_P6_PERFCTR1:
1010 case MSR_P6_EVNTSEL0:
1011 case MSR_P6_EVNTSEL1:
1012 /*
1013 * Just discard all writes to the performance counters; this
1014 * should keep both older linux and windows 64-bit guests
1015 * happy
1016 */
1017 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", msr_index, data);
1018
1019 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001020 case MSR_IA32_CR_PAT:
1021 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1022 vmcs_write64(GUEST_IA32_PAT, data);
1023 vcpu->arch.pat = data;
1024 break;
1025 }
1026 /* Otherwise falls through to kvm_set_msr_common */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027 default:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001028 vmx_load_host_state(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +10001029 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001030 if (msr) {
1031 msr->data = data;
1032 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001034 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035 }
1036
Eddie Dong2cc51562007-05-21 07:28:09 +03001037 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038}
1039
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001040static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001042 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1043 switch (reg) {
1044 case VCPU_REGS_RSP:
1045 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1046 break;
1047 case VCPU_REGS_RIP:
1048 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1049 break;
1050 default:
1051 break;
1052 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053}
1054
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001055static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001057 int old_debug = vcpu->guest_debug;
1058 unsigned long flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001060 vcpu->guest_debug = dbg->control;
1061 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
1062 vcpu->guest_debug = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001064 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1065 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1066 else
1067 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1068
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001069 flags = vmcs_readl(GUEST_RFLAGS);
1070 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
1071 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1072 else if (old_debug & KVM_GUESTDBG_SINGLESTEP)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001074 vmcs_writel(GUEST_RFLAGS, flags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001076 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077
1078 return 0;
1079}
1080
1081static __init int cpu_has_kvm_support(void)
1082{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001083 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084}
1085
1086static __init int vmx_disabled_by_bios(void)
1087{
1088 u64 msr;
1089
1090 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001091 return (msr & (FEATURE_CONTROL_LOCKED |
1092 FEATURE_CONTROL_VMXON_ENABLED))
1093 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001094 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095}
1096
Avi Kivity774c47f2007-02-12 00:54:47 -08001097static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098{
1099 int cpu = raw_smp_processor_id();
1100 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1101 u64 old;
1102
Avi Kivity543e4242008-05-13 16:22:47 +03001103 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001105 if ((old & (FEATURE_CONTROL_LOCKED |
1106 FEATURE_CONTROL_VMXON_ENABLED))
1107 != (FEATURE_CONTROL_LOCKED |
1108 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001110 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001111 FEATURE_CONTROL_LOCKED |
1112 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001113 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001114 asm volatile (ASM_VMX_VMXON_RAX
1115 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001116 : "memory", "cc");
1117}
1118
Avi Kivity543e4242008-05-13 16:22:47 +03001119static void vmclear_local_vcpus(void)
1120{
1121 int cpu = raw_smp_processor_id();
1122 struct vcpu_vmx *vmx, *n;
1123
1124 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1125 local_vcpus_link)
1126 __vcpu_clear(vmx);
1127}
1128
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001129
1130/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1131 * tricks.
1132 */
1133static void kvm_cpu_vmxoff(void)
1134{
1135 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1136 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1137}
1138
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139static void hardware_disable(void *garbage)
1140{
Avi Kivity543e4242008-05-13 16:22:47 +03001141 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001142 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143}
1144
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001145static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001146 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147{
1148 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001149 u32 ctl = ctl_min | ctl_opt;
1150
1151 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1152
1153 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1154 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1155
1156 /* Ensure minimum (required) set of control bits are supported. */
1157 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001158 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001159
1160 *result = ctl;
1161 return 0;
1162}
1163
Yang, Sheng002c7f72007-07-31 14:23:01 +03001164static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001165{
1166 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001167 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001168 u32 _pin_based_exec_control = 0;
1169 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001170 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001171 u32 _vmexit_control = 0;
1172 u32 _vmentry_control = 0;
1173
1174 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001175 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001176 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1177 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001178 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001179
1180 min = CPU_BASED_HLT_EXITING |
1181#ifdef CONFIG_X86_64
1182 CPU_BASED_CR8_LOAD_EXITING |
1183 CPU_BASED_CR8_STORE_EXITING |
1184#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001185 CPU_BASED_CR3_LOAD_EXITING |
1186 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001187 CPU_BASED_USE_IO_BITMAPS |
1188 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001189 CPU_BASED_USE_TSC_OFFSETING |
1190 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001191 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001192 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001193 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001194 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1195 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001196 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001197#ifdef CONFIG_X86_64
1198 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1199 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1200 ~CPU_BASED_CR8_STORE_EXITING;
1201#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001202 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001203 min2 = 0;
1204 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001205 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001206 SECONDARY_EXEC_ENABLE_VPID |
1207 SECONDARY_EXEC_ENABLE_EPT;
1208 if (adjust_vmx_controls(min2, opt2,
1209 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001210 &_cpu_based_2nd_exec_control) < 0)
1211 return -EIO;
1212 }
1213#ifndef CONFIG_X86_64
1214 if (!(_cpu_based_2nd_exec_control &
1215 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1216 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1217#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001218 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001219 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1220 enabled */
Sheng Yangd56f5462008-04-25 10:13:16 +08001221 min &= ~(CPU_BASED_CR3_LOAD_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001222 CPU_BASED_CR3_STORE_EXITING |
1223 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001224 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1225 &_cpu_based_exec_control) < 0)
1226 return -EIO;
1227 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1228 vmx_capability.ept, vmx_capability.vpid);
1229 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001230
1231 min = 0;
1232#ifdef CONFIG_X86_64
1233 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1234#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001235 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001236 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1237 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001238 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001239
Sheng Yang468d4722008-10-09 16:01:55 +08001240 min = 0;
1241 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001242 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1243 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001244 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001245
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001246 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001247
1248 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1249 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001250 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001251
1252#ifdef CONFIG_X86_64
1253 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1254 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001255 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001256#endif
1257
1258 /* Require Write-Back (WB) memory type for VMCS accesses. */
1259 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001260 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001261
Yang, Sheng002c7f72007-07-31 14:23:01 +03001262 vmcs_conf->size = vmx_msr_high & 0x1fff;
1263 vmcs_conf->order = get_order(vmcs_config.size);
1264 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001265
Yang, Sheng002c7f72007-07-31 14:23:01 +03001266 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1267 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001268 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001269 vmcs_conf->vmexit_ctrl = _vmexit_control;
1270 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001271
1272 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001273}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001274
1275static struct vmcs *alloc_vmcs_cpu(int cpu)
1276{
1277 int node = cpu_to_node(cpu);
1278 struct page *pages;
1279 struct vmcs *vmcs;
1280
Mel Gorman6484eb32009-06-16 15:31:54 -07001281 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282 if (!pages)
1283 return NULL;
1284 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001285 memset(vmcs, 0, vmcs_config.size);
1286 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287 return vmcs;
1288}
1289
1290static struct vmcs *alloc_vmcs(void)
1291{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001292 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293}
1294
1295static void free_vmcs(struct vmcs *vmcs)
1296{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001297 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001298}
1299
Sam Ravnborg39959582007-06-01 00:47:13 -07001300static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301{
1302 int cpu;
1303
1304 for_each_online_cpu(cpu)
1305 free_vmcs(per_cpu(vmxarea, cpu));
1306}
1307
Avi Kivity6aa8b732006-12-10 02:21:36 -08001308static __init int alloc_kvm_area(void)
1309{
1310 int cpu;
1311
1312 for_each_online_cpu(cpu) {
1313 struct vmcs *vmcs;
1314
1315 vmcs = alloc_vmcs_cpu(cpu);
1316 if (!vmcs) {
1317 free_kvm_area();
1318 return -ENOMEM;
1319 }
1320
1321 per_cpu(vmxarea, cpu) = vmcs;
1322 }
1323 return 0;
1324}
1325
1326static __init int hardware_setup(void)
1327{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001328 if (setup_vmcs_config(&vmcs_config) < 0)
1329 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001330
1331 if (boot_cpu_has(X86_FEATURE_NX))
1332 kvm_enable_efer_bits(EFER_NX);
1333
Sheng Yang93ba03c2009-04-01 15:52:32 +08001334 if (!cpu_has_vmx_vpid())
1335 enable_vpid = 0;
1336
1337 if (!cpu_has_vmx_ept())
1338 enable_ept = 0;
1339
1340 if (!cpu_has_vmx_flexpriority())
1341 flexpriority_enabled = 0;
1342
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001343 if (!cpu_has_vmx_tpr_shadow())
1344 kvm_x86_ops->update_cr8_intercept = NULL;
1345
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346 return alloc_kvm_area();
1347}
1348
1349static __exit void hardware_unsetup(void)
1350{
1351 free_kvm_area();
1352}
1353
Avi Kivity6aa8b732006-12-10 02:21:36 -08001354static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1355{
1356 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1357
Avi Kivity6af11b92007-03-19 13:18:10 +02001358 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359 vmcs_write16(sf->selector, save->selector);
1360 vmcs_writel(sf->base, save->base);
1361 vmcs_write32(sf->limit, save->limit);
1362 vmcs_write32(sf->ar_bytes, save->ar);
1363 } else {
1364 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1365 << AR_DPL_SHIFT;
1366 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1367 }
1368}
1369
1370static void enter_pmode(struct kvm_vcpu *vcpu)
1371{
1372 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001373 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001375 vmx->emulation_required = 1;
Nitin A Kamble56b237e2009-06-04 15:04:08 -07001376 vcpu->arch.rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001378 vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base);
1379 vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit);
1380 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381
1382 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001383 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001384 flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385 vmcs_writel(GUEST_RFLAGS, flags);
1386
Rusty Russell66aee912007-07-17 23:34:16 +10001387 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1388 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389
1390 update_exception_bitmap(vcpu);
1391
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001392 if (emulate_invalid_guest_state)
1393 return;
1394
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001395 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1396 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1397 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1398 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399
1400 vmcs_write16(GUEST_SS_SELECTOR, 0);
1401 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1402
1403 vmcs_write16(GUEST_CS_SELECTOR,
1404 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1405 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1406}
1407
Mike Dayd77c26f2007-10-08 09:02:08 -04001408static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001410 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001411 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1412 kvm->memslots[0].npages - 3;
1413 return base_gfn << PAGE_SHIFT;
1414 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001415 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416}
1417
1418static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1419{
1420 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1421
1422 save->selector = vmcs_read16(sf->selector);
1423 save->base = vmcs_readl(sf->base);
1424 save->limit = vmcs_read32(sf->limit);
1425 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001426 vmcs_write16(sf->selector, save->base >> 4);
1427 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428 vmcs_write32(sf->limit, 0xffff);
1429 vmcs_write32(sf->ar_bytes, 0xf3);
1430}
1431
1432static void enter_rmode(struct kvm_vcpu *vcpu)
1433{
1434 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001435 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001437 vmx->emulation_required = 1;
Nitin A Kamble56b237e2009-06-04 15:04:08 -07001438 vcpu->arch.rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001440 vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1442
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001443 vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1445
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001446 vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001447 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1448
1449 flags = vmcs_readl(GUEST_RFLAGS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001450 vcpu->arch.rmode.save_iopl
1451 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001452
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001453 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454
1455 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001456 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457 update_exception_bitmap(vcpu);
1458
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001459 if (emulate_invalid_guest_state)
1460 goto continue_rmode;
1461
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1463 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1464 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1465
1466 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001467 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001468 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1469 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001470 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1471
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001472 fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1473 fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1474 fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1475 fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001476
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001477continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001478 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001479 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480}
1481
Amit Shah401d10d2009-02-20 22:53:37 +05301482static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1483{
1484 struct vcpu_vmx *vmx = to_vmx(vcpu);
1485 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1486
1487 vcpu->arch.shadow_efer = efer;
1488 if (!msr)
1489 return;
1490 if (efer & EFER_LMA) {
1491 vmcs_write32(VM_ENTRY_CONTROLS,
1492 vmcs_read32(VM_ENTRY_CONTROLS) |
1493 VM_ENTRY_IA32E_MODE);
1494 msr->data = efer;
1495 } else {
1496 vmcs_write32(VM_ENTRY_CONTROLS,
1497 vmcs_read32(VM_ENTRY_CONTROLS) &
1498 ~VM_ENTRY_IA32E_MODE);
1499
1500 msr->data = efer & ~EFER_LME;
1501 }
1502 setup_msrs(vmx);
1503}
1504
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001505#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506
1507static void enter_lmode(struct kvm_vcpu *vcpu)
1508{
1509 u32 guest_tr_ar;
1510
1511 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1512 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1513 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001514 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515 vmcs_write32(GUEST_TR_AR_BYTES,
1516 (guest_tr_ar & ~AR_TYPE_MASK)
1517 | AR_TYPE_BUSY_64_TSS);
1518 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001519 vcpu->arch.shadow_efer |= EFER_LMA;
Amit Shah401d10d2009-02-20 22:53:37 +05301520 vmx_set_efer(vcpu, vcpu->arch.shadow_efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521}
1522
1523static void exit_lmode(struct kvm_vcpu *vcpu)
1524{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001525 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001526
1527 vmcs_write32(VM_ENTRY_CONTROLS,
1528 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001529 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001530}
1531
1532#endif
1533
Sheng Yang2384d2b2008-01-17 15:14:33 +08001534static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1535{
1536 vpid_sync_vcpu_all(to_vmx(vcpu));
Avi Kivity089d0342009-03-23 18:26:32 +02001537 if (enable_ept)
Sheng Yang4e1096d2008-07-06 19:16:51 +08001538 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001539}
1540
Anthony Liguori25c4c272007-04-27 09:29:21 +03001541static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001542{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001543 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1544 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001545}
1546
Sheng Yang14394422008-04-28 12:24:45 +08001547static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1548{
1549 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1550 if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
1551 printk(KERN_ERR "EPT: Fail to load pdptrs!\n");
1552 return;
1553 }
1554 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1555 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1556 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1557 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1558 }
1559}
1560
1561static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1562
1563static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1564 unsigned long cr0,
1565 struct kvm_vcpu *vcpu)
1566{
1567 if (!(cr0 & X86_CR0_PG)) {
1568 /* From paging/starting to nonpaging */
1569 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001570 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001571 (CPU_BASED_CR3_LOAD_EXITING |
1572 CPU_BASED_CR3_STORE_EXITING));
1573 vcpu->arch.cr0 = cr0;
1574 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1575 *hw_cr0 |= X86_CR0_PE | X86_CR0_PG;
1576 *hw_cr0 &= ~X86_CR0_WP;
1577 } else if (!is_paging(vcpu)) {
1578 /* From nonpaging to paging */
1579 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001580 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001581 ~(CPU_BASED_CR3_LOAD_EXITING |
1582 CPU_BASED_CR3_STORE_EXITING));
1583 vcpu->arch.cr0 = cr0;
1584 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1585 if (!(vcpu->arch.cr0 & X86_CR0_WP))
1586 *hw_cr0 &= ~X86_CR0_WP;
1587 }
1588}
1589
1590static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1591 struct kvm_vcpu *vcpu)
1592{
1593 if (!is_paging(vcpu)) {
1594 *hw_cr4 &= ~X86_CR4_PAE;
1595 *hw_cr4 |= X86_CR4_PSE;
1596 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1597 *hw_cr4 &= ~X86_CR4_PAE;
1598}
1599
Avi Kivity6aa8b732006-12-10 02:21:36 -08001600static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1601{
Sheng Yang14394422008-04-28 12:24:45 +08001602 unsigned long hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) |
1603 KVM_VM_CR0_ALWAYS_ON;
1604
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001605 vmx_fpu_deactivate(vcpu);
1606
Nitin A Kamble56b237e2009-06-04 15:04:08 -07001607 if (vcpu->arch.rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001608 enter_pmode(vcpu);
1609
Nitin A Kamble56b237e2009-06-04 15:04:08 -07001610 if (!vcpu->arch.rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611 enter_rmode(vcpu);
1612
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001613#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001614 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001615 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001617 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618 exit_lmode(vcpu);
1619 }
1620#endif
1621
Avi Kivity089d0342009-03-23 18:26:32 +02001622 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001623 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1624
Avi Kivity6aa8b732006-12-10 02:21:36 -08001625 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001626 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001627 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001628
Rusty Russell707d92fa2007-07-17 23:19:08 +10001629 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001630 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631}
1632
Sheng Yang14394422008-04-28 12:24:45 +08001633static u64 construct_eptp(unsigned long root_hpa)
1634{
1635 u64 eptp;
1636
1637 /* TODO write the value reading from MSR */
1638 eptp = VMX_EPT_DEFAULT_MT |
1639 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1640 eptp |= (root_hpa & PAGE_MASK);
1641
1642 return eptp;
1643}
1644
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1646{
Sheng Yang14394422008-04-28 12:24:45 +08001647 unsigned long guest_cr3;
1648 u64 eptp;
1649
1650 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02001651 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001652 eptp = construct_eptp(cr3);
1653 vmcs_write64(EPT_POINTER, eptp);
1654 ept_sync_context(eptp);
1655 ept_load_pdptrs(vcpu);
1656 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
1657 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1658 }
1659
Sheng Yang2384d2b2008-01-17 15:14:33 +08001660 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001661 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001662 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001663 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001664}
1665
1666static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1667{
Nitin A Kamble56b237e2009-06-04 15:04:08 -07001668 unsigned long hw_cr4 = cr4 | (vcpu->arch.rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08001669 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1670
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001671 vcpu->arch.cr4 = cr4;
Avi Kivity089d0342009-03-23 18:26:32 +02001672 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001673 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1674
1675 vmcs_writel(CR4_READ_SHADOW, cr4);
1676 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001677}
1678
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1680{
1681 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1682
1683 return vmcs_readl(sf->base);
1684}
1685
1686static void vmx_get_segment(struct kvm_vcpu *vcpu,
1687 struct kvm_segment *var, int seg)
1688{
1689 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1690 u32 ar;
1691
1692 var->base = vmcs_readl(sf->base);
1693 var->limit = vmcs_read32(sf->limit);
1694 var->selector = vmcs_read16(sf->selector);
1695 ar = vmcs_read32(sf->ar_bytes);
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02001696 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001697 ar = 0;
1698 var->type = ar & 15;
1699 var->s = (ar >> 4) & 1;
1700 var->dpl = (ar >> 5) & 3;
1701 var->present = (ar >> 7) & 1;
1702 var->avl = (ar >> 12) & 1;
1703 var->l = (ar >> 13) & 1;
1704 var->db = (ar >> 14) & 1;
1705 var->g = (ar >> 15) & 1;
1706 var->unusable = (ar >> 16) & 1;
1707}
1708
Izik Eidus2e4d2652008-03-24 19:38:34 +02001709static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1710{
1711 struct kvm_segment kvm_seg;
1712
1713 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1714 return 0;
1715
1716 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1717 return 3;
1718
1719 vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
1720 return kvm_seg.selector & 3;
1721}
1722
Avi Kivity653e3102007-05-07 10:55:37 +03001723static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001724{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725 u32 ar;
1726
Avi Kivity653e3102007-05-07 10:55:37 +03001727 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001728 ar = 1 << 16;
1729 else {
1730 ar = var->type & 15;
1731 ar |= (var->s & 1) << 4;
1732 ar |= (var->dpl & 3) << 5;
1733 ar |= (var->present & 1) << 7;
1734 ar |= (var->avl & 1) << 12;
1735 ar |= (var->l & 1) << 13;
1736 ar |= (var->db & 1) << 14;
1737 ar |= (var->g & 1) << 15;
1738 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001739 if (ar == 0) /* a 0 value means unusable */
1740 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001741
1742 return ar;
1743}
1744
1745static void vmx_set_segment(struct kvm_vcpu *vcpu,
1746 struct kvm_segment *var, int seg)
1747{
1748 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1749 u32 ar;
1750
Nitin A Kamble56b237e2009-06-04 15:04:08 -07001751 if (vcpu->arch.rmode.vm86_active && seg == VCPU_SREG_TR) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001752 vcpu->arch.rmode.tr.selector = var->selector;
1753 vcpu->arch.rmode.tr.base = var->base;
1754 vcpu->arch.rmode.tr.limit = var->limit;
1755 vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001756 return;
1757 }
1758 vmcs_writel(sf->base, var->base);
1759 vmcs_write32(sf->limit, var->limit);
1760 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble56b237e2009-06-04 15:04:08 -07001761 if (vcpu->arch.rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001762 /*
1763 * Hack real-mode segments into vm86 compatibility.
1764 */
1765 if (var->base == 0xffff0000 && var->selector == 0xf000)
1766 vmcs_writel(sf->base, 0xf0000);
1767 ar = 0xf3;
1768 } else
1769 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001770 vmcs_write32(sf->ar_bytes, ar);
1771}
1772
Avi Kivity6aa8b732006-12-10 02:21:36 -08001773static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1774{
1775 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1776
1777 *db = (ar >> 14) & 1;
1778 *l = (ar >> 13) & 1;
1779}
1780
1781static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1782{
1783 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1784 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1785}
1786
1787static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1788{
1789 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1790 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1791}
1792
1793static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1794{
1795 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1796 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1797}
1798
1799static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1800{
1801 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1802 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1803}
1804
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001805static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1806{
1807 struct kvm_segment var;
1808 u32 ar;
1809
1810 vmx_get_segment(vcpu, &var, seg);
1811 ar = vmx_segment_access_rights(&var);
1812
1813 if (var.base != (var.selector << 4))
1814 return false;
1815 if (var.limit != 0xffff)
1816 return false;
1817 if (ar != 0xf3)
1818 return false;
1819
1820 return true;
1821}
1822
1823static bool code_segment_valid(struct kvm_vcpu *vcpu)
1824{
1825 struct kvm_segment cs;
1826 unsigned int cs_rpl;
1827
1828 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1829 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1830
Avi Kivity1872a3f2009-01-04 23:26:52 +02001831 if (cs.unusable)
1832 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001833 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1834 return false;
1835 if (!cs.s)
1836 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001837 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001838 if (cs.dpl > cs_rpl)
1839 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001840 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001841 if (cs.dpl != cs_rpl)
1842 return false;
1843 }
1844 if (!cs.present)
1845 return false;
1846
1847 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1848 return true;
1849}
1850
1851static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1852{
1853 struct kvm_segment ss;
1854 unsigned int ss_rpl;
1855
1856 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1857 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1858
Avi Kivity1872a3f2009-01-04 23:26:52 +02001859 if (ss.unusable)
1860 return true;
1861 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001862 return false;
1863 if (!ss.s)
1864 return false;
1865 if (ss.dpl != ss_rpl) /* DPL != RPL */
1866 return false;
1867 if (!ss.present)
1868 return false;
1869
1870 return true;
1871}
1872
1873static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1874{
1875 struct kvm_segment var;
1876 unsigned int rpl;
1877
1878 vmx_get_segment(vcpu, &var, seg);
1879 rpl = var.selector & SELECTOR_RPL_MASK;
1880
Avi Kivity1872a3f2009-01-04 23:26:52 +02001881 if (var.unusable)
1882 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001883 if (!var.s)
1884 return false;
1885 if (!var.present)
1886 return false;
1887 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
1888 if (var.dpl < rpl) /* DPL < RPL */
1889 return false;
1890 }
1891
1892 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1893 * rights flags
1894 */
1895 return true;
1896}
1897
1898static bool tr_valid(struct kvm_vcpu *vcpu)
1899{
1900 struct kvm_segment tr;
1901
1902 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
1903
Avi Kivity1872a3f2009-01-04 23:26:52 +02001904 if (tr.unusable)
1905 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001906 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1907 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001908 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001909 return false;
1910 if (!tr.present)
1911 return false;
1912
1913 return true;
1914}
1915
1916static bool ldtr_valid(struct kvm_vcpu *vcpu)
1917{
1918 struct kvm_segment ldtr;
1919
1920 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
1921
Avi Kivity1872a3f2009-01-04 23:26:52 +02001922 if (ldtr.unusable)
1923 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001924 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1925 return false;
1926 if (ldtr.type != 2)
1927 return false;
1928 if (!ldtr.present)
1929 return false;
1930
1931 return true;
1932}
1933
1934static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
1935{
1936 struct kvm_segment cs, ss;
1937
1938 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1939 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1940
1941 return ((cs.selector & SELECTOR_RPL_MASK) ==
1942 (ss.selector & SELECTOR_RPL_MASK));
1943}
1944
1945/*
1946 * Check if guest state is valid. Returns true if valid, false if
1947 * not.
1948 * We assume that registers are always usable
1949 */
1950static bool guest_state_valid(struct kvm_vcpu *vcpu)
1951{
1952 /* real mode guest state checks */
1953 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
1954 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
1955 return false;
1956 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
1957 return false;
1958 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
1959 return false;
1960 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
1961 return false;
1962 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
1963 return false;
1964 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
1965 return false;
1966 } else {
1967 /* protected mode guest state checks */
1968 if (!cs_ss_rpl_check(vcpu))
1969 return false;
1970 if (!code_segment_valid(vcpu))
1971 return false;
1972 if (!stack_segment_valid(vcpu))
1973 return false;
1974 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
1975 return false;
1976 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
1977 return false;
1978 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
1979 return false;
1980 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
1981 return false;
1982 if (!tr_valid(vcpu))
1983 return false;
1984 if (!ldtr_valid(vcpu))
1985 return false;
1986 }
1987 /* TODO:
1988 * - Add checks on RIP
1989 * - Add checks on RFLAGS
1990 */
1991
1992 return true;
1993}
1994
Mike Dayd77c26f2007-10-08 09:02:08 -04001995static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001996{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001997 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001998 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001999 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02002000 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002001
Izik Eidus195aefd2007-10-01 22:14:18 +02002002 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2003 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002004 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002005 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002006 r = kvm_write_guest_page(kvm, fn++, &data,
2007 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002008 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002009 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002010 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2011 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002012 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002013 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2014 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002015 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002016 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002017 r = kvm_write_guest_page(kvm, fn, &data,
2018 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2019 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002020 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002021 goto out;
2022
2023 ret = 1;
2024out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002025 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002026}
2027
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002028static int init_rmode_identity_map(struct kvm *kvm)
2029{
2030 int i, r, ret;
2031 pfn_t identity_map_pfn;
2032 u32 tmp;
2033
Avi Kivity089d0342009-03-23 18:26:32 +02002034 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002035 return 1;
2036 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2037 printk(KERN_ERR "EPT: identity-mapping pagetable "
2038 "haven't been allocated!\n");
2039 return 0;
2040 }
2041 if (likely(kvm->arch.ept_identity_pagetable_done))
2042 return 1;
2043 ret = 0;
2044 identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT;
2045 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2046 if (r < 0)
2047 goto out;
2048 /* Set up identity-mapping pagetable for EPT in real mode */
2049 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2050 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2051 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2052 r = kvm_write_guest_page(kvm, identity_map_pfn,
2053 &tmp, i * sizeof(tmp), sizeof(tmp));
2054 if (r < 0)
2055 goto out;
2056 }
2057 kvm->arch.ept_identity_pagetable_done = true;
2058 ret = 1;
2059out:
2060 return ret;
2061}
2062
Avi Kivity6aa8b732006-12-10 02:21:36 -08002063static void seg_setup(int seg)
2064{
2065 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2066
2067 vmcs_write16(sf->selector, 0);
2068 vmcs_writel(sf->base, 0);
2069 vmcs_write32(sf->limit, 0xffff);
Avi Kivitya16b20d2008-08-20 15:48:27 +03002070 vmcs_write32(sf->ar_bytes, 0xf3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002071}
2072
Sheng Yangf78e0e22007-10-29 09:40:42 +08002073static int alloc_apic_access_page(struct kvm *kvm)
2074{
2075 struct kvm_userspace_memory_region kvm_userspace_mem;
2076 int r = 0;
2077
Izik Eidus72dc67a2008-02-10 18:04:15 +02002078 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002079 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002080 goto out;
2081 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2082 kvm_userspace_mem.flags = 0;
2083 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2084 kvm_userspace_mem.memory_size = PAGE_SIZE;
2085 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2086 if (r)
2087 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002088
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002089 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002090out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002091 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002092 return r;
2093}
2094
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002095static int alloc_identity_pagetable(struct kvm *kvm)
2096{
2097 struct kvm_userspace_memory_region kvm_userspace_mem;
2098 int r = 0;
2099
2100 down_write(&kvm->slots_lock);
2101 if (kvm->arch.ept_identity_pagetable)
2102 goto out;
2103 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2104 kvm_userspace_mem.flags = 0;
2105 kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
2106 kvm_userspace_mem.memory_size = PAGE_SIZE;
2107 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2108 if (r)
2109 goto out;
2110
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002111 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
2112 VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002113out:
2114 up_write(&kvm->slots_lock);
2115 return r;
2116}
2117
Sheng Yang2384d2b2008-01-17 15:14:33 +08002118static void allocate_vpid(struct vcpu_vmx *vmx)
2119{
2120 int vpid;
2121
2122 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002123 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002124 return;
2125 spin_lock(&vmx_vpid_lock);
2126 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2127 if (vpid < VMX_NR_VPIDS) {
2128 vmx->vpid = vpid;
2129 __set_bit(vpid, vmx_vpid_bitmap);
2130 }
2131 spin_unlock(&vmx_vpid_lock);
2132}
2133
Avi Kivity58972972009-02-24 22:26:47 +02002134static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002135{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002136 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002137
2138 if (!cpu_has_vmx_msr_bitmap())
2139 return;
2140
2141 /*
2142 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2143 * have the write-low and read-high bitmap offsets the wrong way round.
2144 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2145 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002146 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002147 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2148 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002149 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2150 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002151 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2152 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002153 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002154}
2155
Avi Kivity58972972009-02-24 22:26:47 +02002156static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2157{
2158 if (!longmode_only)
2159 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2160 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2161}
2162
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163/*
2164 * Sets up the vmcs for emulated real mode.
2165 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002166static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002167{
Sheng Yang468d4722008-10-09 16:01:55 +08002168 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002169 u32 junk;
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01002170 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002171 unsigned long a;
2172 struct descriptor_table dt;
2173 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002174 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002175 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002176
Avi Kivity6aa8b732006-12-10 02:21:36 -08002177 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002178 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2179 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180
Sheng Yang25c5f222008-03-28 13:18:56 +08002181 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002182 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002183
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2185
Avi Kivity6aa8b732006-12-10 02:21:36 -08002186 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002187 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2188 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002189
2190 exec_control = vmcs_config.cpu_based_exec_ctrl;
2191 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2192 exec_control &= ~CPU_BASED_TPR_SHADOW;
2193#ifdef CONFIG_X86_64
2194 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2195 CPU_BASED_CR8_LOAD_EXITING;
2196#endif
2197 }
Avi Kivity089d0342009-03-23 18:26:32 +02002198 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002199 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002200 CPU_BASED_CR3_LOAD_EXITING |
2201 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002202 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203
Sheng Yang83ff3b92007-11-21 14:33:25 +08002204 if (cpu_has_secondary_exec_ctrls()) {
2205 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2206 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2207 exec_control &=
2208 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002209 if (vmx->vpid == 0)
2210 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Avi Kivity089d0342009-03-23 18:26:32 +02002211 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002212 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002213 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2214 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002215
Avi Kivityc7addb92007-09-16 18:58:32 +02002216 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2217 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002218 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2219
2220 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2221 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2222 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2223
2224 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2225 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2226 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002227 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2228 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002229 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002230#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002231 rdmsrl(MSR_FS_BASE, a);
2232 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2233 rdmsrl(MSR_GS_BASE, a);
2234 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2235#else
2236 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2237 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2238#endif
2239
2240 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2241
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002242 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2244
Mike Dayd77c26f2007-10-08 09:02:08 -04002245 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002246 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002247 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2248 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2249 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250
2251 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2252 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2253 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2254 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2255 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2256 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2257
Sheng Yang468d4722008-10-09 16:01:55 +08002258 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2259 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2260 host_pat = msr_low | ((u64) msr_high << 32);
2261 vmcs_write64(HOST_IA32_PAT, host_pat);
2262 }
2263 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2264 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2265 host_pat = msr_low | ((u64) msr_high << 32);
2266 /* Write the default value follow host pat */
2267 vmcs_write64(GUEST_IA32_PAT, host_pat);
2268 /* Keep arch.pat sync with GUEST_IA32_PAT */
2269 vmx->vcpu.arch.pat = host_pat;
2270 }
2271
Avi Kivity6aa8b732006-12-10 02:21:36 -08002272 for (i = 0; i < NR_VMX_MSR; ++i) {
2273 u32 index = vmx_msr_index[i];
2274 u32 data_low, data_high;
2275 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002276 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002277
2278 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2279 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002280 if (wrmsr_safe(index, data_low, data_high) < 0)
2281 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002282 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002283 vmx->host_msrs[j].index = index;
2284 vmx->host_msrs[j].reserved = 0;
2285 vmx->host_msrs[j].data = data;
2286 vmx->guest_msrs[j] = vmx->host_msrs[j];
2287 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002288 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002290 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002291
2292 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002293 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2294
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002295 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2296 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2297
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01002298 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2299 rdtscll(tsc_this);
2300 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2301 tsc_base = tsc_this;
2302
2303 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002304
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002305 return 0;
2306}
2307
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002308static int init_rmode(struct kvm *kvm)
2309{
2310 if (!init_rmode_tss(kvm))
2311 return 0;
2312 if (!init_rmode_identity_map(kvm))
2313 return 0;
2314 return 1;
2315}
2316
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002317static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2318{
2319 struct vcpu_vmx *vmx = to_vmx(vcpu);
2320 u64 msr;
2321 int ret;
2322
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002323 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002324 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002325 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002326 ret = -ENOMEM;
2327 goto out;
2328 }
2329
Nitin A Kamble56b237e2009-06-04 15:04:08 -07002330 vmx->vcpu.arch.rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002331
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002332 vmx->soft_vnmi_blocked = 0;
2333
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002334 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002335 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002336 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
2337 if (vmx->vcpu.vcpu_id == 0)
2338 msr |= MSR_IA32_APICBASE_BSP;
2339 kvm_set_apic_base(&vmx->vcpu, msr);
2340
2341 fx_init(&vmx->vcpu);
2342
Avi Kivity5706be02008-08-20 15:07:31 +03002343 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002344 /*
2345 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2346 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2347 */
2348 if (vmx->vcpu.vcpu_id == 0) {
2349 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2350 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2351 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002352 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2353 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002354 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002355
2356 seg_setup(VCPU_SREG_DS);
2357 seg_setup(VCPU_SREG_ES);
2358 seg_setup(VCPU_SREG_FS);
2359 seg_setup(VCPU_SREG_GS);
2360 seg_setup(VCPU_SREG_SS);
2361
2362 vmcs_write16(GUEST_TR_SELECTOR, 0);
2363 vmcs_writel(GUEST_TR_BASE, 0);
2364 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2365 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2366
2367 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2368 vmcs_writel(GUEST_LDTR_BASE, 0);
2369 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2370 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2371
2372 vmcs_write32(GUEST_SYSENTER_CS, 0);
2373 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2374 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2375
2376 vmcs_writel(GUEST_RFLAGS, 0x02);
2377 if (vmx->vcpu.vcpu_id == 0)
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002378 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002379 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002380 kvm_rip_write(vcpu, 0);
2381 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002382
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002383 vmcs_writel(GUEST_DR7, 0x400);
2384
2385 vmcs_writel(GUEST_GDTR_BASE, 0);
2386 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2387
2388 vmcs_writel(GUEST_IDTR_BASE, 0);
2389 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2390
2391 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2392 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2393 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2394
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002395 /* Special registers */
2396 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2397
2398 setup_msrs(vmx);
2399
Avi Kivity6aa8b732006-12-10 02:21:36 -08002400 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2401
Sheng Yangf78e0e22007-10-29 09:40:42 +08002402 if (cpu_has_vmx_tpr_shadow()) {
2403 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2404 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2405 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002406 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002407 vmcs_write32(TPR_THRESHOLD, 0);
2408 }
2409
2410 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2411 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002412 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413
Sheng Yang2384d2b2008-01-17 15:14:33 +08002414 if (vmx->vpid != 0)
2415 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2416
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002417 vmx->vcpu.arch.cr0 = 0x60000010;
2418 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002419 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002420 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002421 vmx_fpu_activate(&vmx->vcpu);
2422 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002423
Sheng Yang2384d2b2008-01-17 15:14:33 +08002424 vpid_sync_vcpu_all(vmx);
2425
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002426 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002428 /* HACK: Don't enable emulation on guest boot/reset */
2429 vmx->emulation_required = 0;
2430
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002432 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433 return ret;
2434}
2435
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002436static void enable_irq_window(struct kvm_vcpu *vcpu)
2437{
2438 u32 cpu_based_vm_exec_control;
2439
2440 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2441 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2442 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2443}
2444
2445static void enable_nmi_window(struct kvm_vcpu *vcpu)
2446{
2447 u32 cpu_based_vm_exec_control;
2448
2449 if (!cpu_has_virtual_nmis()) {
2450 enable_irq_window(vcpu);
2451 return;
2452 }
2453
2454 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2455 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2456 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2457}
2458
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002459static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03002460{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002461 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002462 uint32_t intr;
2463 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02002464
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002465 KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler);
2466
Avi Kivityfa89a812008-09-01 15:57:51 +03002467 ++vcpu->stat.irq_injections;
Nitin A Kamble56b237e2009-06-04 15:04:08 -07002468 if (vcpu->arch.rmode.vm86_active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002469 vmx->rmode.irq.pending = true;
2470 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002471 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +03002472 if (vcpu->arch.interrupt.soft)
2473 vmx->rmode.irq.rip +=
2474 vmx->vcpu.arch.event_exit_inst_len;
Avi Kivity9c5623e2007-11-08 18:19:20 +02002475 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2476 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2477 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002478 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002479 return;
2480 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002481 intr = irq | INTR_INFO_VALID_MASK;
2482 if (vcpu->arch.interrupt.soft) {
2483 intr |= INTR_TYPE_SOFT_INTR;
2484 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2485 vmx->vcpu.arch.event_exit_inst_len);
2486 } else
2487 intr |= INTR_TYPE_EXT_INTR;
2488 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03002489}
2490
Sheng Yangf08864b2008-05-15 18:23:25 +08002491static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2492{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002493 struct vcpu_vmx *vmx = to_vmx(vcpu);
2494
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002495 if (!cpu_has_virtual_nmis()) {
2496 /*
2497 * Tracking the NMI-blocked state in software is built upon
2498 * finding the next open IRQ window. This, in turn, depends on
2499 * well-behaving guests: They have to keep IRQs disabled at
2500 * least as long as the NMI handler runs. Otherwise we may
2501 * cause NMI nesting, maybe breaking the guest. But as this is
2502 * highly unlikely, we can live with the residual risk.
2503 */
2504 vmx->soft_vnmi_blocked = 1;
2505 vmx->vnmi_blocked_time = 0;
2506 }
2507
Jan Kiszka487b3912008-09-26 09:30:56 +02002508 ++vcpu->stat.nmi_injections;
Nitin A Kamble56b237e2009-06-04 15:04:08 -07002509 if (vcpu->arch.rmode.vm86_active) {
Jan Kiszka66a5a342008-09-26 09:30:51 +02002510 vmx->rmode.irq.pending = true;
2511 vmx->rmode.irq.vector = NMI_VECTOR;
2512 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2513 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2514 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2515 INTR_INFO_VALID_MASK);
2516 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2517 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2518 return;
2519 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002520 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2521 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002522}
2523
Gleb Natapovc4282df2009-04-21 17:45:07 +03002524static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02002525{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002526 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03002527 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002528
Gleb Natapovc4282df2009-04-21 17:45:07 +03002529 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2530 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS |
2531 GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02002532}
2533
Gleb Natapov78646122009-03-23 12:12:11 +02002534static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
2535{
Gleb Natapovc4282df2009-04-21 17:45:07 +03002536 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2537 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2538 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02002539}
2540
Izik Eiduscbc94022007-10-25 00:29:55 +02002541static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2542{
2543 int ret;
2544 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002545 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002546 .guest_phys_addr = addr,
2547 .memory_size = PAGE_SIZE * 3,
2548 .flags = 0,
2549 };
2550
2551 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2552 if (ret)
2553 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002554 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002555 return 0;
2556}
2557
Avi Kivity6aa8b732006-12-10 02:21:36 -08002558static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2559 int vec, u32 err_code)
2560{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002561 /*
2562 * Instruction with address size override prefix opcode 0x67
2563 * Cause the #SS fault with 0 error code in VM86 mode.
2564 */
2565 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02002566 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002567 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002568 /*
2569 * Forward all other exceptions that are valid in real mode.
2570 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2571 * the required debugging infrastructure rework.
2572 */
2573 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002574 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002575 if (vcpu->guest_debug &
2576 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2577 return 0;
2578 kvm_queue_exception(vcpu, vec);
2579 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002580 case BP_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002581 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2582 return 0;
2583 /* fall through */
2584 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002585 case OF_VECTOR:
2586 case BR_VECTOR:
2587 case UD_VECTOR:
2588 case DF_VECTOR:
2589 case SS_VECTOR:
2590 case GP_VECTOR:
2591 case MF_VECTOR:
2592 kvm_queue_exception(vcpu, vec);
2593 return 1;
2594 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002595 return 0;
2596}
2597
Andi Kleena0861c02009-06-08 17:37:09 +08002598/*
2599 * Trigger machine check on the host. We assume all the MSRs are already set up
2600 * by the CPU and that we still run on the same CPU as the MCE occurred on.
2601 * We pass a fake environment to the machine check handler because we want
2602 * the guest to be always treated like user space, no matter what context
2603 * it used internally.
2604 */
2605static void kvm_machine_check(void)
2606{
2607#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
2608 struct pt_regs regs = {
2609 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
2610 .flags = X86_EFLAGS_IF,
2611 };
2612
2613 do_machine_check(&regs, 0);
2614#endif
2615}
2616
2617static int handle_machine_check(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2618{
2619 /* already handled by vcpu_run */
2620 return 1;
2621}
2622
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2624{
Avi Kivity1155f762007-11-22 11:30:47 +02002625 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002626 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002627 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628 u32 vect_info;
2629 enum emulation_result er;
2630
Avi Kivity1155f762007-11-22 11:30:47 +02002631 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2633
Andi Kleena0861c02009-06-08 17:37:09 +08002634 if (is_machine_check(intr_info))
2635 return handle_machine_check(vcpu, kvm_run);
2636
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002638 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002639 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002640 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641
Jan Kiszkae4a41882008-09-26 09:30:46 +02002642 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002643 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002644
2645 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002646 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002647 return 1;
2648 }
2649
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002650 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08002651 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002652 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002653 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002654 return 1;
2655 }
2656
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002658 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002659 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2661 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002662 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02002663 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002664 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002665 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002666 KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2,
2667 (u32)((u64)cr2 >> 32), handler);
Gleb Natapov3298b752009-05-11 13:35:46 +03002668 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03002669 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002670 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671 }
2672
Nitin A Kamble56b237e2009-06-04 15:04:08 -07002673 if (vcpu->arch.rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002675 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002676 if (vcpu->arch.halt_request) {
2677 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002678 return kvm_emulate_halt(vcpu);
2679 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002681 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002683 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002684 switch (ex_no) {
2685 case DB_VECTOR:
2686 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2687 if (!(vcpu->guest_debug &
2688 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2689 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2690 kvm_queue_exception(vcpu, DB_VECTOR);
2691 return 1;
2692 }
2693 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2694 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2695 /* fall through */
2696 case BP_VECTOR:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002697 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002698 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2699 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002700 break;
2701 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002702 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2703 kvm_run->ex.exception = ex_no;
2704 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002705 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002706 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707 return 0;
2708}
2709
2710static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2711 struct kvm_run *kvm_run)
2712{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002713 ++vcpu->stat.irq_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002714 KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715 return 1;
2716}
2717
Avi Kivity988ad742007-02-12 00:54:36 -08002718static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2719{
2720 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2721 return 0;
2722}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2725{
He, Qingbfdaab02007-09-12 14:18:28 +08002726 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01002727 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002728 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002729
Avi Kivity1165f5f2007-04-19 17:27:43 +03002730 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002731 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002732 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002733
2734 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002735 if (emulate_instruction(vcpu,
2736 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002737 return 0;
2738 return 1;
2739 }
2740
2741 size = (exit_qualification & 7) + 1;
2742 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002743 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002744
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002745 skip_emulated_instruction(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002746 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002747}
2748
Ingo Molnar102d8322007-02-19 14:37:47 +02002749static void
2750vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2751{
2752 /*
2753 * Patch in the VMCALL instruction:
2754 */
2755 hypercall[0] = 0x0f;
2756 hypercall[1] = 0x01;
2757 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002758}
2759
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2761{
He, Qingbfdaab02007-09-12 14:18:28 +08002762 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002763 int cr;
2764 int reg;
2765
He, Qingbfdaab02007-09-12 14:18:28 +08002766 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002767 cr = exit_qualification & 15;
2768 reg = (exit_qualification >> 8) & 15;
2769 switch ((exit_qualification >> 4) & 3) {
2770 case 0: /* mov to cr */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002771 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr,
2772 (u32)kvm_register_read(vcpu, reg),
2773 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
2774 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002775 switch (cr) {
2776 case 0:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002777 kvm_set_cr0(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002778 skip_emulated_instruction(vcpu);
2779 return 1;
2780 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002781 kvm_set_cr3(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782 skip_emulated_instruction(vcpu);
2783 return 1;
2784 case 4:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002785 kvm_set_cr4(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786 skip_emulated_instruction(vcpu);
2787 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002788 case 8: {
2789 u8 cr8_prev = kvm_get_cr8(vcpu);
2790 u8 cr8 = kvm_register_read(vcpu, reg);
2791 kvm_set_cr8(vcpu, cr8);
2792 skip_emulated_instruction(vcpu);
2793 if (irqchip_in_kernel(vcpu->kvm))
2794 return 1;
2795 if (cr8_prev <= cr8)
2796 return 1;
2797 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2798 return 0;
2799 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002800 };
2801 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002802 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002803 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002804 vcpu->arch.cr0 &= ~X86_CR0_TS;
2805 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002806 vmx_fpu_activate(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002807 KVMTRACE_0D(CLTS, vcpu, handler);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002808 skip_emulated_instruction(vcpu);
2809 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810 case 1: /*mov from cr*/
2811 switch (cr) {
2812 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002813 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002814 KVMTRACE_3D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002815 (u32)kvm_register_read(vcpu, reg),
2816 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002817 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818 skip_emulated_instruction(vcpu);
2819 return 1;
2820 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002821 kvm_register_write(vcpu, reg, kvm_get_cr8(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002822 KVMTRACE_2D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002823 (u32)kvm_register_read(vcpu, reg), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002824 skip_emulated_instruction(vcpu);
2825 return 1;
2826 }
2827 break;
2828 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002829 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002830
2831 skip_emulated_instruction(vcpu);
2832 return 1;
2833 default:
2834 break;
2835 }
2836 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002837 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838 (int)(exit_qualification >> 4) & 3, cr);
2839 return 0;
2840}
2841
2842static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2843{
He, Qingbfdaab02007-09-12 14:18:28 +08002844 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845 unsigned long val;
2846 int dr, reg;
2847
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002848 dr = vmcs_readl(GUEST_DR7);
2849 if (dr & DR7_GD) {
2850 /*
2851 * As the vm-exit takes precedence over the debug trap, we
2852 * need to emulate the latter, either for the host or the
2853 * guest debugging itself.
2854 */
2855 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
2856 kvm_run->debug.arch.dr6 = vcpu->arch.dr6;
2857 kvm_run->debug.arch.dr7 = dr;
2858 kvm_run->debug.arch.pc =
2859 vmcs_readl(GUEST_CS_BASE) +
2860 vmcs_readl(GUEST_RIP);
2861 kvm_run->debug.arch.exception = DB_VECTOR;
2862 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2863 return 0;
2864 } else {
2865 vcpu->arch.dr7 &= ~DR7_GD;
2866 vcpu->arch.dr6 |= DR6_BD;
2867 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2868 kvm_queue_exception(vcpu, DB_VECTOR);
2869 return 1;
2870 }
2871 }
2872
He, Qingbfdaab02007-09-12 14:18:28 +08002873 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002874 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
2875 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
2876 if (exit_qualification & TYPE_MOV_FROM_DR) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877 switch (dr) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002878 case 0 ... 3:
2879 val = vcpu->arch.db[dr];
2880 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002882 val = vcpu->arch.dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002883 break;
2884 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002885 val = vcpu->arch.dr7;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886 break;
2887 default:
2888 val = 0;
2889 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002890 kvm_register_write(vcpu, reg, val);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002891 KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892 } else {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002893 val = vcpu->arch.regs[reg];
2894 switch (dr) {
2895 case 0 ... 3:
2896 vcpu->arch.db[dr] = val;
2897 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
2898 vcpu->arch.eff_db[dr] = val;
2899 break;
2900 case 4 ... 5:
2901 if (vcpu->arch.cr4 & X86_CR4_DE)
2902 kvm_queue_exception(vcpu, UD_VECTOR);
2903 break;
2904 case 6:
2905 if (val & 0xffffffff00000000ULL) {
2906 kvm_queue_exception(vcpu, GP_VECTOR);
2907 break;
2908 }
2909 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
2910 break;
2911 case 7:
2912 if (val & 0xffffffff00000000ULL) {
2913 kvm_queue_exception(vcpu, GP_VECTOR);
2914 break;
2915 }
2916 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
2917 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
2918 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2919 vcpu->arch.switch_db_regs =
2920 (val & DR7_BP_EN_MASK);
2921 }
2922 break;
2923 }
2924 KVMTRACE_2D(DR_WRITE, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002925 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002926 skip_emulated_instruction(vcpu);
2927 return 1;
2928}
2929
2930static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2931{
Avi Kivity06465c52007-02-28 20:46:53 +02002932 kvm_emulate_cpuid(vcpu);
2933 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002934}
2935
2936static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2937{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002938 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002939 u64 data;
2940
2941 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002942 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002943 return 1;
2944 }
2945
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002946 KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32),
2947 handler);
2948
Avi Kivity6aa8b732006-12-10 02:21:36 -08002949 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002950 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
2951 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002952 skip_emulated_instruction(vcpu);
2953 return 1;
2954}
2955
2956static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2957{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002958 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
2959 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
2960 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002961
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002962 KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32),
2963 handler);
2964
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002966 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 return 1;
2968 }
2969
2970 skip_emulated_instruction(vcpu);
2971 return 1;
2972}
2973
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002974static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2975 struct kvm_run *kvm_run)
2976{
2977 return 1;
2978}
2979
Avi Kivity6aa8b732006-12-10 02:21:36 -08002980static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2981 struct kvm_run *kvm_run)
2982{
Eddie Dong85f455f2007-07-06 12:20:49 +03002983 u32 cpu_based_vm_exec_control;
2984
2985 /* clear pending irq */
2986 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2987 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2988 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002989
2990 KVMTRACE_0D(PEND_INTR, vcpu, handler);
Jan Kiszkaa26bf122008-09-26 09:30:45 +02002991 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002992
Dor Laorc1150d82007-01-05 16:36:24 -08002993 /*
2994 * If the user space waits to inject interrupts, exit as soon as
2995 * possible
2996 */
Gleb Natapov80618232009-04-21 17:44:56 +03002997 if (!irqchip_in_kernel(vcpu->kvm) &&
2998 kvm_run->request_interrupt_window &&
2999 !kvm_cpu_has_interrupt(vcpu)) {
Dor Laorc1150d82007-01-05 16:36:24 -08003000 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003001 return 0;
3002 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003003 return 1;
3004}
3005
3006static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3007{
3008 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003009 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003010}
3011
Ingo Molnarc21415e2007-02-19 14:37:47 +02003012static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3013{
Dor Laor510043d2007-02-19 18:25:43 +02003014 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003015 kvm_emulate_hypercall(vcpu);
3016 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003017}
3018
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003019static int handle_vmx_insn(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3020{
3021 kvm_queue_exception(vcpu, UD_VECTOR);
3022 return 1;
3023}
3024
Marcelo Tosattia7052892008-09-23 13:18:35 -03003025static int handle_invlpg(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3026{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003027 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003028
3029 kvm_mmu_invlpg(vcpu, exit_qualification);
3030 skip_emulated_instruction(vcpu);
3031 return 1;
3032}
3033
Eddie Donge5edaa02007-11-11 12:28:35 +02003034static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3035{
3036 skip_emulated_instruction(vcpu);
3037 /* TODO: Add support for VT-d/pass-through device */
3038 return 1;
3039}
3040
Sheng Yangf78e0e22007-10-29 09:40:42 +08003041static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3042{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003043 unsigned long exit_qualification;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003044 enum emulation_result er;
3045 unsigned long offset;
3046
Sheng Yangf9c617f2009-03-25 10:08:52 +08003047 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003048 offset = exit_qualification & 0xffful;
3049
3050 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3051
3052 if (er != EMULATE_DONE) {
3053 printk(KERN_ERR
3054 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3055 offset);
3056 return -ENOTSUPP;
3057 }
3058 return 1;
3059}
3060
Izik Eidus37817f22008-03-24 23:14:53 +02003061static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3062{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003063 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003064 unsigned long exit_qualification;
3065 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003066 int reason, type, idt_v;
3067
3068 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3069 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003070
3071 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3072
3073 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003074 if (reason == TASK_SWITCH_GATE && idt_v) {
3075 switch (type) {
3076 case INTR_TYPE_NMI_INTR:
3077 vcpu->arch.nmi_injected = false;
3078 if (cpu_has_virtual_nmis())
3079 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3080 GUEST_INTR_STATE_NMI);
3081 break;
3082 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003083 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003084 kvm_clear_interrupt_queue(vcpu);
3085 break;
3086 case INTR_TYPE_HARD_EXCEPTION:
3087 case INTR_TYPE_SOFT_EXCEPTION:
3088 kvm_clear_exception_queue(vcpu);
3089 break;
3090 default:
3091 break;
3092 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003093 }
Izik Eidus37817f22008-03-24 23:14:53 +02003094 tss_selector = exit_qualification;
3095
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003096 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3097 type != INTR_TYPE_EXT_INTR &&
3098 type != INTR_TYPE_NMI_INTR))
3099 skip_emulated_instruction(vcpu);
3100
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003101 if (!kvm_task_switch(vcpu, tss_selector, reason))
3102 return 0;
3103
3104 /* clear all local breakpoint enable flags */
3105 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3106
3107 /*
3108 * TODO: What about debug traps on tss switch?
3109 * Are we supposed to inject them and update dr6?
3110 */
3111
3112 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003113}
3114
Sheng Yang14394422008-04-28 12:24:45 +08003115static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3116{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003117 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003118 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003119 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003120
Sheng Yangf9c617f2009-03-25 10:08:52 +08003121 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003122
3123 if (exit_qualification & (1 << 6)) {
3124 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
3125 return -ENOTSUPP;
3126 }
3127
3128 gla_validity = (exit_qualification >> 7) & 0x3;
3129 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3130 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3131 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3132 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003133 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003134 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3135 (long unsigned int)exit_qualification);
3136 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3137 kvm_run->hw.hardware_exit_reason = 0;
3138 return -ENOTSUPP;
3139 }
3140
3141 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Sheng Yang49cd7d22009-02-11 13:50:40 +08003142 return kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003143}
3144
Sheng Yangf08864b2008-05-15 18:23:25 +08003145static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3146{
3147 u32 cpu_based_vm_exec_control;
3148
3149 /* clear pending NMI */
3150 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3151 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3152 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3153 ++vcpu->stat.nmi_window_exits;
3154
3155 return 1;
3156}
3157
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003158static void handle_invalid_guest_state(struct kvm_vcpu *vcpu,
3159 struct kvm_run *kvm_run)
3160{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003161 struct vcpu_vmx *vmx = to_vmx(vcpu);
3162 enum emulation_result err = EMULATE_DONE;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003163
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003164 local_irq_enable();
Jan Kiszka34f0c1a2009-07-22 23:53:26 +02003165 preempt_enable();
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003166
3167 while (!guest_state_valid(vcpu)) {
3168 err = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3169
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003170 if (err == EMULATE_DO_MMIO)
3171 break;
3172
3173 if (err != EMULATE_DONE) {
3174 kvm_report_emulation_failure(vcpu, "emulation failure");
Jan Kiszka263799a2009-07-21 10:43:07 +02003175 break;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003176 }
3177
3178 if (signal_pending(current))
3179 break;
3180 if (need_resched())
3181 schedule();
3182 }
3183
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003184 preempt_disable();
Jan Kiszka34f0c1a2009-07-22 23:53:26 +02003185 local_irq_disable();
Avi Kivity8b3079a2009-01-05 12:10:54 +02003186
3187 vmx->invalid_state_emulation_result = err;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003188}
3189
Avi Kivity6aa8b732006-12-10 02:21:36 -08003190/*
3191 * The exit handlers return 1 if the exit was handled fully and guest execution
3192 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3193 * to be done to userspace and return 0.
3194 */
3195static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
3196 struct kvm_run *kvm_run) = {
3197 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3198 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003199 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003200 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003201 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003202 [EXIT_REASON_CR_ACCESS] = handle_cr,
3203 [EXIT_REASON_DR_ACCESS] = handle_dr,
3204 [EXIT_REASON_CPUID] = handle_cpuid,
3205 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3206 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3207 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3208 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003209 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003210 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003211 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3212 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3213 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3214 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3215 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3216 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3217 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3218 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3219 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003220 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3221 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003222 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003223 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Sheng Yang14394422008-04-28 12:24:45 +08003224 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
Andi Kleena0861c02009-06-08 17:37:09 +08003225 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003226};
3227
3228static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003229 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003230
3231/*
3232 * The guest has exited. See if we can fix it or if we need userspace
3233 * assistance.
3234 */
Avi Kivity6062d012009-03-23 17:35:17 +02003235static int vmx_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003236{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003237 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003238 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003239 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003240
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003241 KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)kvm_rip_read(vcpu),
3242 (u32)((u64)kvm_rip_read(vcpu) >> 32), entryexit);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003243
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003244 /* If we need to emulate an MMIO from handle_invalid_guest_state
3245 * we just return 0 */
Avi Kivity10f32d82009-01-05 00:53:19 +02003246 if (vmx->emulation_required && emulate_invalid_guest_state) {
3247 if (guest_state_valid(vcpu))
3248 vmx->emulation_required = 0;
Avi Kivity8b3079a2009-01-05 12:10:54 +02003249 return vmx->invalid_state_emulation_result != EMULATE_DO_MMIO;
Avi Kivity10f32d82009-01-05 00:53:19 +02003250 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003251
Sheng Yang14394422008-04-28 12:24:45 +08003252 /* Access CR3 don't cause VMExit in paging mode, so we need
3253 * to sync with guest real CR3. */
Avi Kivity089d0342009-03-23 18:26:32 +02003254 if (enable_ept && is_paging(vcpu)) {
Sheng Yang14394422008-04-28 12:24:45 +08003255 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3256 ept_load_pdptrs(vcpu);
3257 }
3258
Avi Kivity29bd8a72007-09-10 17:27:03 +03003259 if (unlikely(vmx->fail)) {
3260 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3261 kvm_run->fail_entry.hardware_entry_failure_reason
3262 = vmcs_read32(VM_INSTRUCTION_ERROR);
3263 return 0;
3264 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003265
Mike Dayd77c26f2007-10-08 09:02:08 -04003266 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003267 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003268 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3269 exit_reason != EXIT_REASON_TASK_SWITCH))
3270 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3271 "(0x%x) and exit reason is 0x%x\n",
3272 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003273
3274 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03003275 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003276 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003277 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003278 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003279 /*
3280 * This CPU don't support us in finding the end of an
3281 * NMI-blocked window if the guest runs with IRQs
3282 * disabled. So we pull the trigger after 1 s of
3283 * futile waiting, but inform the user about this.
3284 */
3285 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3286 "state on VCPU %d after 1 s timeout\n",
3287 __func__, vcpu->vcpu_id);
3288 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003289 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003290 }
3291
Avi Kivity6aa8b732006-12-10 02:21:36 -08003292 if (exit_reason < kvm_vmx_max_exit_handlers
3293 && kvm_vmx_exit_handlers[exit_reason])
3294 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
3295 else {
3296 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3297 kvm_run->hw.hardware_exit_reason = exit_reason;
3298 }
3299 return 0;
3300}
3301
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003302static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003303{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003304 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003305 vmcs_write32(TPR_THRESHOLD, 0);
3306 return;
3307 }
3308
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003309 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003310}
3311
Avi Kivitycf393f72008-07-01 16:20:21 +03003312static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3313{
3314 u32 exit_intr_info;
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003315 u32 idt_vectoring_info = vmx->idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003316 bool unblock_nmi;
3317 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003318 int type;
3319 bool idtv_info_valid;
Avi Kivitycf393f72008-07-01 16:20:21 +03003320
3321 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Gleb Natapov20f65982009-05-11 13:35:55 +03003322
Andi Kleena0861c02009-06-08 17:37:09 +08003323 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
3324
3325 /* Handle machine checks before interrupts are enabled */
3326 if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
3327 || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
3328 && is_machine_check(exit_intr_info)))
3329 kvm_machine_check();
3330
Gleb Natapov20f65982009-05-11 13:35:55 +03003331 /* We need to handle NMIs before interrupts are enabled */
3332 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
3333 (exit_intr_info & INTR_INFO_VALID_MASK)) {
3334 KVMTRACE_0D(NMI, &vmx->vcpu, handler);
3335 asm("int $2");
3336 }
3337
3338 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3339
Avi Kivitycf393f72008-07-01 16:20:21 +03003340 if (cpu_has_virtual_nmis()) {
3341 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3342 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3343 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003344 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03003345 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3346 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003347 * SDM 3: 23.2.2 (September 2008)
3348 * Bit 12 is undefined in any of the following cases:
3349 * If the VM exit sets the valid bit in the IDT-vectoring
3350 * information field.
3351 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03003352 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003353 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
3354 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03003355 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3356 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003357 } else if (unlikely(vmx->soft_vnmi_blocked))
3358 vmx->vnmi_blocked_time +=
3359 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003360
Gleb Natapov37b96e92009-03-30 16:03:13 +03003361 vmx->vcpu.arch.nmi_injected = false;
3362 kvm_clear_exception_queue(&vmx->vcpu);
3363 kvm_clear_interrupt_queue(&vmx->vcpu);
3364
3365 if (!idtv_info_valid)
3366 return;
3367
Avi Kivity668f6122008-07-02 09:28:55 +03003368 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3369 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003370
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003371 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003372 case INTR_TYPE_NMI_INTR:
3373 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03003374 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003375 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03003376 * Clear bit "block by NMI" before VM entry if a NMI
3377 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03003378 */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003379 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3380 GUEST_INTR_STATE_NMI);
3381 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003382 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003383 vmx->vcpu.arch.event_exit_inst_len =
3384 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3385 /* fall through */
3386 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03003387 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003388 u32 err = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3389 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03003390 } else
3391 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003392 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003393 case INTR_TYPE_SOFT_INTR:
3394 vmx->vcpu.arch.event_exit_inst_len =
3395 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3396 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003397 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003398 kvm_queue_interrupt(&vmx->vcpu, vector,
3399 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003400 break;
3401 default:
3402 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03003403 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003404}
3405
Avi Kivity9c8cba32007-11-22 11:42:59 +02003406/*
3407 * Failure to inject an interrupt should give us the information
3408 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3409 * when fetching the interrupt redirection bitmap in the real-mode
3410 * tss, this doesn't happen. So we do it ourselves.
3411 */
3412static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3413{
3414 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003415 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003416 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003417 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003418 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3419 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3420 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3421 return;
3422 }
3423 vmx->idt_vectoring_info =
3424 VECTORING_INFO_VALID_MASK
3425 | INTR_TYPE_EXT_INTR
3426 | vmx->rmode.irq.vector;
3427}
3428
Avi Kivityc8019492008-07-14 14:44:59 +03003429#ifdef CONFIG_X86_64
3430#define R "r"
3431#define Q "q"
3432#else
3433#define R "e"
3434#define Q "l"
3435#endif
3436
Avi Kivity04d2cc72007-09-10 18:10:54 +03003437static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003439 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitye6adf282007-04-30 16:07:54 +03003440
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003441 /* Record the guest's net vcpu time for enforced NMI injections. */
3442 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3443 vmx->entry_time = ktime_get();
3444
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003445 /* Handle invalid guest state instead of entering VMX */
3446 if (vmx->emulation_required && emulate_invalid_guest_state) {
3447 handle_invalid_guest_state(vcpu, kvm_run);
3448 return;
3449 }
3450
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003451 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3452 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3453 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3454 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3455
Avi Kivitye6adf282007-04-30 16:07:54 +03003456 /*
3457 * Loading guest fpu may have cleared host cr0.ts
3458 */
3459 vmcs_writel(HOST_CR0, read_cr0());
3460
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003461 set_debugreg(vcpu->arch.dr6, 6);
3462
Mike Dayd77c26f2007-10-08 09:02:08 -04003463 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003464 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003465 "push %%"R"dx; push %%"R"bp;"
3466 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003467 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3468 "je 1f \n\t"
3469 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003470 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003471 "1: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003472 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003473 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003474 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003475 "mov %c[cr2](%0), %%"R"ax \n\t"
3476 "mov %%"R"ax, %%cr2 \n\t"
3477 "mov %c[rax](%0), %%"R"ax \n\t"
3478 "mov %c[rbx](%0), %%"R"bx \n\t"
3479 "mov %c[rdx](%0), %%"R"dx \n\t"
3480 "mov %c[rsi](%0), %%"R"si \n\t"
3481 "mov %c[rdi](%0), %%"R"di \n\t"
3482 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003483#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003484 "mov %c[r8](%0), %%r8 \n\t"
3485 "mov %c[r9](%0), %%r9 \n\t"
3486 "mov %c[r10](%0), %%r10 \n\t"
3487 "mov %c[r11](%0), %%r11 \n\t"
3488 "mov %c[r12](%0), %%r12 \n\t"
3489 "mov %c[r13](%0), %%r13 \n\t"
3490 "mov %c[r14](%0), %%r14 \n\t"
3491 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003493 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3494
Avi Kivity6aa8b732006-12-10 02:21:36 -08003495 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003496 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003497 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003498 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003499 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003500 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003501 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003502 "xchg %0, (%%"R"sp) \n\t"
3503 "mov %%"R"ax, %c[rax](%0) \n\t"
3504 "mov %%"R"bx, %c[rbx](%0) \n\t"
3505 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3506 "mov %%"R"dx, %c[rdx](%0) \n\t"
3507 "mov %%"R"si, %c[rsi](%0) \n\t"
3508 "mov %%"R"di, %c[rdi](%0) \n\t"
3509 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003510#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003511 "mov %%r8, %c[r8](%0) \n\t"
3512 "mov %%r9, %c[r9](%0) \n\t"
3513 "mov %%r10, %c[r10](%0) \n\t"
3514 "mov %%r11, %c[r11](%0) \n\t"
3515 "mov %%r12, %c[r12](%0) \n\t"
3516 "mov %%r13, %c[r13](%0) \n\t"
3517 "mov %%r14, %c[r14](%0) \n\t"
3518 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003519#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003520 "mov %%cr2, %%"R"ax \n\t"
3521 "mov %%"R"ax, %c[cr2](%0) \n\t"
3522
3523 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003524 "setbe %c[fail](%0) \n\t"
3525 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3526 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3527 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03003528 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003529 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3530 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3531 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3532 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3533 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3534 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3535 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003536#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003537 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3538 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3539 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3540 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3541 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3542 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3543 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3544 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003546 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003547 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003548 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003549#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003550 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3551#endif
3552 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003553
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003554 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
3555 vcpu->arch.regs_dirty = 0;
3556
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003557 get_debugreg(vcpu->arch.dr6, 6);
3558
Avi Kivity1155f762007-11-22 11:30:47 +02003559 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003560 if (vmx->rmode.irq.pending)
3561 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003562
Mike Dayd77c26f2007-10-08 09:02:08 -04003563 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003564 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003565
Avi Kivitycf393f72008-07-01 16:20:21 +03003566 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003567}
3568
Avi Kivityc8019492008-07-14 14:44:59 +03003569#undef R
3570#undef Q
3571
Avi Kivity6aa8b732006-12-10 02:21:36 -08003572static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3573{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003574 struct vcpu_vmx *vmx = to_vmx(vcpu);
3575
3576 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003577 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003578 free_vmcs(vmx->vmcs);
3579 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003580 }
3581}
3582
3583static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3584{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003585 struct vcpu_vmx *vmx = to_vmx(vcpu);
3586
Sheng Yang2384d2b2008-01-17 15:14:33 +08003587 spin_lock(&vmx_vpid_lock);
3588 if (vmx->vpid != 0)
3589 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3590 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003591 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003592 kfree(vmx->host_msrs);
3593 kfree(vmx->guest_msrs);
3594 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003595 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003596}
3597
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003598static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003599{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003600 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003601 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003602 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003603
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003604 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003605 return ERR_PTR(-ENOMEM);
3606
Sheng Yang2384d2b2008-01-17 15:14:33 +08003607 allocate_vpid(vmx);
3608
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003609 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3610 if (err)
3611 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003612
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003613 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003614 if (!vmx->guest_msrs) {
3615 err = -ENOMEM;
3616 goto uninit_vcpu;
3617 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003618
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003619 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3620 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003621 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003622
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003623 vmx->vmcs = alloc_vmcs();
3624 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003625 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003626
3627 vmcs_clear(vmx->vmcs);
3628
Avi Kivity15ad7142007-07-11 18:17:21 +03003629 cpu = get_cpu();
3630 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003631 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003632 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003633 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003634 if (err)
3635 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003636 if (vm_need_virtualize_apic_accesses(kvm))
3637 if (alloc_apic_access_page(kvm) != 0)
3638 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003639
Avi Kivity089d0342009-03-23 18:26:32 +02003640 if (enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003641 if (alloc_identity_pagetable(kvm) != 0)
3642 goto free_vmcs;
3643
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003644 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003645
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003646free_vmcs:
3647 free_vmcs(vmx->vmcs);
3648free_msrs:
3649 kfree(vmx->host_msrs);
3650free_guest_msrs:
3651 kfree(vmx->guest_msrs);
3652uninit_vcpu:
3653 kvm_vcpu_uninit(&vmx->vcpu);
3654free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003655 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003656 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003657}
3658
Yang, Sheng002c7f72007-07-31 14:23:01 +03003659static void __init vmx_check_processor_compat(void *rtn)
3660{
3661 struct vmcs_config vmcs_conf;
3662
3663 *(int *)rtn = 0;
3664 if (setup_vmcs_config(&vmcs_conf) < 0)
3665 *(int *)rtn = -EIO;
3666 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3667 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3668 smp_processor_id());
3669 *(int *)rtn = -EIO;
3670 }
3671}
3672
Sheng Yang67253af2008-04-25 10:20:22 +08003673static int get_ept_level(void)
3674{
3675 return VMX_EPT_DEFAULT_GAW + 1;
3676}
3677
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003678static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003679{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003680 u64 ret;
3681
Sheng Yang522c68c2009-04-27 20:35:43 +08003682 /* For VT-d and EPT combination
3683 * 1. MMIO: always map as UC
3684 * 2. EPT with VT-d:
3685 * a. VT-d without snooping control feature: can't guarantee the
3686 * result, try to trust guest.
3687 * b. VT-d with snooping control feature: snooping control feature of
3688 * VT-d engine can guarantee the cache correctness. Just set it
3689 * to WB to keep consistent with host. So the same as item 3.
3690 * 3. EPT without VT-d: always map as WB and set IGMT=1 to keep
3691 * consistent with host MTRR
3692 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003693 if (is_mmio)
3694 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08003695 else if (vcpu->kvm->arch.iommu_domain &&
3696 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
3697 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
3698 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003699 else
Sheng Yang522c68c2009-04-27 20:35:43 +08003700 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
3701 | VMX_EPT_IGMT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003702
3703 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08003704}
3705
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003706static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003707 .cpu_has_kvm_support = cpu_has_kvm_support,
3708 .disabled_by_bios = vmx_disabled_by_bios,
3709 .hardware_setup = hardware_setup,
3710 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003711 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003712 .hardware_enable = hardware_enable,
3713 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08003714 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003715
3716 .vcpu_create = vmx_create_vcpu,
3717 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003718 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003719
Avi Kivity04d2cc72007-09-10 18:10:54 +03003720 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003721 .vcpu_load = vmx_vcpu_load,
3722 .vcpu_put = vmx_vcpu_put,
3723
3724 .set_guest_debug = set_guest_debug,
3725 .get_msr = vmx_get_msr,
3726 .set_msr = vmx_set_msr,
3727 .get_segment_base = vmx_get_segment_base,
3728 .get_segment = vmx_get_segment,
3729 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003730 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003731 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003732 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003733 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003734 .set_cr3 = vmx_set_cr3,
3735 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003736 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003737 .get_idt = vmx_get_idt,
3738 .set_idt = vmx_set_idt,
3739 .get_gdt = vmx_get_gdt,
3740 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003741 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003742 .get_rflags = vmx_get_rflags,
3743 .set_rflags = vmx_set_rflags,
3744
3745 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003746
Avi Kivity6aa8b732006-12-10 02:21:36 -08003747 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02003748 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003749 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003750 .set_interrupt_shadow = vmx_set_interrupt_shadow,
3751 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003752 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003753 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003754 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003755 .queue_exception = vmx_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003756 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003757 .nmi_allowed = vmx_nmi_allowed,
3758 .enable_nmi_window = enable_nmi_window,
3759 .enable_irq_window = enable_irq_window,
3760 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003761
Izik Eiduscbc94022007-10-25 00:29:55 +02003762 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003763 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003764 .get_mt_mask = vmx_get_mt_mask,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003765};
3766
3767static int __init vmx_init(void)
3768{
He, Qingfdef3ad2007-04-30 09:45:24 +03003769 int r;
3770
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003771 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03003772 if (!vmx_io_bitmap_a)
3773 return -ENOMEM;
3774
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003775 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03003776 if (!vmx_io_bitmap_b) {
3777 r = -ENOMEM;
3778 goto out;
3779 }
3780
Avi Kivity58972972009-02-24 22:26:47 +02003781 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
3782 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08003783 r = -ENOMEM;
3784 goto out1;
3785 }
3786
Avi Kivity58972972009-02-24 22:26:47 +02003787 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
3788 if (!vmx_msr_bitmap_longmode) {
3789 r = -ENOMEM;
3790 goto out2;
3791 }
3792
He, Qingfdef3ad2007-04-30 09:45:24 +03003793 /*
3794 * Allow direct access to the PC debug port (it is often used for I/O
3795 * delays, but the vmexits simply slow things down).
3796 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003797 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
3798 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003799
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003800 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003801
Avi Kivity58972972009-02-24 22:26:47 +02003802 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
3803 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08003804
Sheng Yang2384d2b2008-01-17 15:14:33 +08003805 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
3806
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003807 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003808 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02003809 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08003810
Avi Kivity58972972009-02-24 22:26:47 +02003811 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
3812 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
3813 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
3814 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
3815 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
3816 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03003817
Avi Kivity089d0342009-03-23 18:26:32 +02003818 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003819 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003820 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08003821 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08003822 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003823 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003824 kvm_enable_tdp();
3825 } else
3826 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08003827
Avi Kivityc7addb92007-09-16 18:58:32 +02003828 if (bypass_guest_pf)
3829 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
3830
Sheng Yang14394422008-04-28 12:24:45 +08003831 ept_sync_global();
3832
He, Qingfdef3ad2007-04-30 09:45:24 +03003833 return 0;
3834
Avi Kivity58972972009-02-24 22:26:47 +02003835out3:
3836 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08003837out2:
Avi Kivity58972972009-02-24 22:26:47 +02003838 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03003839out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003840 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03003841out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003842 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003843 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003844}
3845
3846static void __exit vmx_exit(void)
3847{
Avi Kivity58972972009-02-24 22:26:47 +02003848 free_page((unsigned long)vmx_msr_bitmap_legacy);
3849 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003850 free_page((unsigned long)vmx_io_bitmap_b);
3851 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003852
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003853 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003854}
3855
3856module_init(vmx_init)
3857module_exit(vmx_exit)