blob: 71e57ae1cab70ea739d452ecd0a3a5d041559802 [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"
Avi Kivity6aa8b732006-12-10 02:21:36 -080019#include "vmx.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040021
Avi Kivityedf88412007-12-16 11:02:48 +020022#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080023#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020024#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/mm.h>
26#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040027#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020028#include <linux/moduleparam.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030029#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030030#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080033#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080034
Avi Kivity4ecac3f2008-05-13 13:23:38 +030035#define __ex(x) __kvm_handle_fault_on_reboot(x)
36
Avi Kivity6aa8b732006-12-10 02:21:36 -080037MODULE_AUTHOR("Qumranet");
38MODULE_LICENSE("GPL");
39
Avi Kivityc7addb92007-09-16 18:58:32 +020040static int bypass_guest_pf = 1;
41module_param(bypass_guest_pf, bool, 0);
42
Sheng Yang2384d2b2008-01-17 15:14:33 +080043static int enable_vpid = 1;
44module_param(enable_vpid, bool, 0);
45
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020046static int flexpriority_enabled = 1;
47module_param(flexpriority_enabled, bool, 0);
48
Sheng Yang14394422008-04-28 12:24:45 +080049static int enable_ept = 1;
Sheng Yangd56f5462008-04-25 10:13:16 +080050module_param(enable_ept, bool, 0);
51
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030052static int emulate_invalid_guest_state = 0;
53module_param(emulate_invalid_guest_state, bool, 0);
54
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040055struct vmcs {
56 u32 revision_id;
57 u32 abort;
58 char data[0];
59};
60
61struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100062 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +030063 struct list_head local_vcpus_link;
Avi Kivity313dbd492008-07-17 18:04:30 +030064 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040065 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030066 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020067 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040068 struct kvm_msr_entry *guest_msrs;
69 struct kvm_msr_entry *host_msrs;
70 int nmsrs;
71 int save_nmsrs;
72 int msr_offset_efer;
73#ifdef CONFIG_X86_64
74 int msr_offset_kernel_gs_base;
75#endif
76 struct vmcs *vmcs;
77 struct {
78 int loaded;
79 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020080 int gs_ldt_reload_needed;
81 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030082 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040083 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +020084 struct {
85 struct {
86 bool pending;
87 u8 vector;
88 unsigned rip;
89 } irq;
90 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +080091 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030092 bool emulation_required;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040093};
94
95static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
96{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100097 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040098}
99
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800100static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800101static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300102
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103static DEFINE_PER_CPU(struct vmcs *, vmxarea);
104static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300105static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800106
He, Qingfdef3ad2007-04-30 09:45:24 +0300107static struct page *vmx_io_bitmap_a;
108static struct page *vmx_io_bitmap_b;
Sheng Yang25c5f222008-03-28 13:18:56 +0800109static struct page *vmx_msr_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300110
Sheng Yang2384d2b2008-01-17 15:14:33 +0800111static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
112static DEFINE_SPINLOCK(vmx_vpid_lock);
113
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300114static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800115 int size;
116 int order;
117 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300118 u32 pin_based_exec_ctrl;
119 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800120 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300121 u32 vmexit_ctrl;
122 u32 vmentry_ctrl;
123} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124
Sheng Yangd56f5462008-04-25 10:13:16 +0800125struct vmx_capability {
126 u32 ept;
127 u32 vpid;
128} vmx_capability;
129
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130#define VMX_SEGMENT_FIELD(seg) \
131 [VCPU_SREG_##seg] = { \
132 .selector = GUEST_##seg##_SELECTOR, \
133 .base = GUEST_##seg##_BASE, \
134 .limit = GUEST_##seg##_LIMIT, \
135 .ar_bytes = GUEST_##seg##_AR_BYTES, \
136 }
137
138static struct kvm_vmx_segment_field {
139 unsigned selector;
140 unsigned base;
141 unsigned limit;
142 unsigned ar_bytes;
143} kvm_vmx_segment_fields[] = {
144 VMX_SEGMENT_FIELD(CS),
145 VMX_SEGMENT_FIELD(DS),
146 VMX_SEGMENT_FIELD(ES),
147 VMX_SEGMENT_FIELD(FS),
148 VMX_SEGMENT_FIELD(GS),
149 VMX_SEGMENT_FIELD(SS),
150 VMX_SEGMENT_FIELD(TR),
151 VMX_SEGMENT_FIELD(LDTR),
152};
153
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300154/*
155 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
156 * away by decrementing the array size.
157 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800158static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800159#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800160 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
161#endif
162 MSR_EFER, MSR_K6_STAR,
163};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200164#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800165
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400166static void load_msrs(struct kvm_msr_entry *e, int n)
167{
168 int i;
169
170 for (i = 0; i < n; ++i)
171 wrmsrl(e[i].index, e[i].data);
172}
173
174static void save_msrs(struct kvm_msr_entry *e, int n)
175{
176 int i;
177
178 for (i = 0; i < n; ++i)
179 rdmsrl(e[i].index, e[i].data);
180}
181
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182static inline int is_page_fault(u32 intr_info)
183{
184 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
185 INTR_INFO_VALID_MASK)) ==
186 (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
187}
188
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300189static inline int is_no_device(u32 intr_info)
190{
191 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
192 INTR_INFO_VALID_MASK)) ==
193 (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
194}
195
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500196static inline int is_invalid_opcode(u32 intr_info)
197{
198 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
199 INTR_INFO_VALID_MASK)) ==
200 (INTR_TYPE_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
201}
202
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203static inline int is_external_interrupt(u32 intr_info)
204{
205 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
206 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
207}
208
Sheng Yang25c5f222008-03-28 13:18:56 +0800209static inline int cpu_has_vmx_msr_bitmap(void)
210{
211 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS);
212}
213
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800214static inline int cpu_has_vmx_tpr_shadow(void)
215{
216 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW);
217}
218
219static inline int vm_need_tpr_shadow(struct kvm *kvm)
220{
221 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)));
222}
223
Sheng Yangf78e0e22007-10-29 09:40:42 +0800224static inline int cpu_has_secondary_exec_ctrls(void)
225{
226 return (vmcs_config.cpu_based_exec_ctrl &
227 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS);
228}
229
Avi Kivity774ead32007-12-26 13:57:04 +0200230static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800231{
Avi Kivity4c9fc8e2008-03-24 18:15:14 +0200232 return flexpriority_enabled
233 && (vmcs_config.cpu_based_2nd_exec_ctrl &
234 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800235}
236
Sheng Yangd56f5462008-04-25 10:13:16 +0800237static inline int cpu_has_vmx_invept_individual_addr(void)
238{
239 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT));
240}
241
242static inline int cpu_has_vmx_invept_context(void)
243{
244 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT));
245}
246
247static inline int cpu_has_vmx_invept_global(void)
248{
249 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT));
250}
251
252static inline int cpu_has_vmx_ept(void)
253{
254 return (vmcs_config.cpu_based_2nd_exec_ctrl &
255 SECONDARY_EXEC_ENABLE_EPT);
256}
257
258static inline int vm_need_ept(void)
259{
260 return (cpu_has_vmx_ept() && enable_ept);
261}
262
Sheng Yangf78e0e22007-10-29 09:40:42 +0800263static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
264{
265 return ((cpu_has_vmx_virtualize_apic_accesses()) &&
266 (irqchip_in_kernel(kvm)));
267}
268
Sheng Yang2384d2b2008-01-17 15:14:33 +0800269static inline int cpu_has_vmx_vpid(void)
270{
271 return (vmcs_config.cpu_based_2nd_exec_ctrl &
272 SECONDARY_EXEC_ENABLE_VPID);
273}
274
Sheng Yangf08864b2008-05-15 18:23:25 +0800275static inline int cpu_has_virtual_nmis(void)
276{
277 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
278}
279
Rusty Russell8b9cf982007-07-30 16:31:43 +1000280static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800281{
282 int i;
283
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400284 for (i = 0; i < vmx->nmsrs; ++i)
285 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300286 return i;
287 return -1;
288}
289
Sheng Yang2384d2b2008-01-17 15:14:33 +0800290static inline void __invvpid(int ext, u16 vpid, gva_t gva)
291{
292 struct {
293 u64 vpid : 16;
294 u64 rsvd : 48;
295 u64 gva;
296 } operand = { vpid, 0, gva };
297
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300298 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800299 /* CF==1 or ZF==1 --> rc = -1 */
300 "; ja 1f ; ud2 ; 1:"
301 : : "a"(&operand), "c"(ext) : "cc", "memory");
302}
303
Sheng Yang14394422008-04-28 12:24:45 +0800304static inline void __invept(int ext, u64 eptp, gpa_t gpa)
305{
306 struct {
307 u64 eptp, gpa;
308 } operand = {eptp, gpa};
309
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300310 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800311 /* CF==1 or ZF==1 --> rc = -1 */
312 "; ja 1f ; ud2 ; 1:\n"
313 : : "a" (&operand), "c" (ext) : "cc", "memory");
314}
315
Rusty Russell8b9cf982007-07-30 16:31:43 +1000316static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300317{
318 int i;
319
Rusty Russell8b9cf982007-07-30 16:31:43 +1000320 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300321 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400322 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000323 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800324}
325
Avi Kivity6aa8b732006-12-10 02:21:36 -0800326static void vmcs_clear(struct vmcs *vmcs)
327{
328 u64 phys_addr = __pa(vmcs);
329 u8 error;
330
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300331 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800332 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
333 : "cc", "memory");
334 if (error)
335 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
336 vmcs, phys_addr);
337}
338
339static void __vcpu_clear(void *arg)
340{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000341 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800342 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800343
Rusty Russell8b9cf982007-07-30 16:31:43 +1000344 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400345 vmcs_clear(vmx->vmcs);
346 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800348 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300349 list_del(&vmx->local_vcpus_link);
350 vmx->vcpu.cpu = -1;
351 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800352}
353
Rusty Russell8b9cf982007-07-30 16:31:43 +1000354static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800355{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200356 if (vmx->vcpu.cpu == -1)
357 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200358 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800359}
360
Sheng Yang2384d2b2008-01-17 15:14:33 +0800361static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
362{
363 if (vmx->vpid == 0)
364 return;
365
366 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
367}
368
Sheng Yang14394422008-04-28 12:24:45 +0800369static inline void ept_sync_global(void)
370{
371 if (cpu_has_vmx_invept_global())
372 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
373}
374
375static inline void ept_sync_context(u64 eptp)
376{
377 if (vm_need_ept()) {
378 if (cpu_has_vmx_invept_context())
379 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
380 else
381 ept_sync_global();
382 }
383}
384
385static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
386{
387 if (vm_need_ept()) {
388 if (cpu_has_vmx_invept_individual_addr())
389 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
390 eptp, gpa);
391 else
392 ept_sync_context(eptp);
393 }
394}
395
Avi Kivity6aa8b732006-12-10 02:21:36 -0800396static unsigned long vmcs_readl(unsigned long field)
397{
398 unsigned long value;
399
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300400 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800401 : "=a"(value) : "d"(field) : "cc");
402 return value;
403}
404
405static u16 vmcs_read16(unsigned long field)
406{
407 return vmcs_readl(field);
408}
409
410static u32 vmcs_read32(unsigned long field)
411{
412 return vmcs_readl(field);
413}
414
415static u64 vmcs_read64(unsigned long field)
416{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800417#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418 return vmcs_readl(field);
419#else
420 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
421#endif
422}
423
Avi Kivitye52de1b2007-01-05 16:36:56 -0800424static noinline void vmwrite_error(unsigned long field, unsigned long value)
425{
426 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
427 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
428 dump_stack();
429}
430
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431static void vmcs_writel(unsigned long field, unsigned long value)
432{
433 u8 error;
434
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300435 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400436 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800437 if (unlikely(error))
438 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800439}
440
441static void vmcs_write16(unsigned long field, u16 value)
442{
443 vmcs_writel(field, value);
444}
445
446static void vmcs_write32(unsigned long field, u32 value)
447{
448 vmcs_writel(field, value);
449}
450
451static void vmcs_write64(unsigned long field, u64 value)
452{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800453 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300454#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455 asm volatile ("");
456 vmcs_writel(field+1, value >> 32);
457#endif
458}
459
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300460static void vmcs_clear_bits(unsigned long field, u32 mask)
461{
462 vmcs_writel(field, vmcs_readl(field) & ~mask);
463}
464
465static void vmcs_set_bits(unsigned long field, u32 mask)
466{
467 vmcs_writel(field, vmcs_readl(field) | mask);
468}
469
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300470static void update_exception_bitmap(struct kvm_vcpu *vcpu)
471{
472 u32 eb;
473
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500474 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300475 if (!vcpu->fpu_active)
476 eb |= 1u << NM_VECTOR;
477 if (vcpu->guest_debug.enabled)
Jan Kiszka19bd8af2008-07-13 13:40:55 +0200478 eb |= 1u << DB_VECTOR;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800479 if (vcpu->arch.rmode.active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300480 eb = ~0;
Sheng Yang14394422008-04-28 12:24:45 +0800481 if (vm_need_ept())
482 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300483 vmcs_write32(EXCEPTION_BITMAP, eb);
484}
485
Avi Kivity33ed6322007-05-02 16:54:03 +0300486static void reload_tss(void)
487{
Avi Kivity33ed6322007-05-02 16:54:03 +0300488 /*
489 * VT restores TR but not its size. Useless.
490 */
491 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200492 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300493
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300494 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300495 descs = (void *)gdt.base;
496 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
497 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300498}
499
Rusty Russell8b9cf982007-07-30 16:31:43 +1000500static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300501{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400502 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300503 u64 host_efer = vmx->host_msrs[efer_offset].data;
504 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
505 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300506
Avi Kivity51c6cf62007-08-29 03:48:05 +0300507 if (efer_offset < 0)
508 return;
509 /*
510 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
511 * outside long mode
512 */
513 ignore_bits = EFER_NX | EFER_SCE;
514#ifdef CONFIG_X86_64
515 ignore_bits |= EFER_LMA | EFER_LME;
516 /* SCE is meaningful only in long mode on Intel */
517 if (guest_efer & EFER_LMA)
518 ignore_bits &= ~(u64)EFER_SCE;
519#endif
520 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
521 return;
522
523 vmx->host_state.guest_efer_loaded = 1;
524 guest_efer &= ~ignore_bits;
525 guest_efer |= host_efer & ignore_bits;
526 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000527 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300528}
529
Avi Kivity51c6cf62007-08-29 03:48:05 +0300530static void reload_host_efer(struct vcpu_vmx *vmx)
531{
532 if (vmx->host_state.guest_efer_loaded) {
533 vmx->host_state.guest_efer_loaded = 0;
534 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
535 }
536}
537
Avi Kivity04d2cc72007-09-10 18:10:54 +0300538static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300539{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300540 struct vcpu_vmx *vmx = to_vmx(vcpu);
541
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400542 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300543 return;
544
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400545 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300546 /*
547 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
548 * allow segment selectors with cpl > 0 or ti == 1.
549 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300550 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200551 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300552 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200553 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400554 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200555 vmx->host_state.fs_reload_needed = 0;
556 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300557 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200558 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300559 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300560 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400561 if (!(vmx->host_state.gs_sel & 7))
562 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300563 else {
564 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200565 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300566 }
567
568#ifdef CONFIG_X86_64
569 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
570 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
571#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400572 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
573 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300574#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300575
576#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400577 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400578 save_msrs(vmx->host_msrs +
579 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400580
Avi Kivity707c0872007-05-02 17:33:43 +0300581#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400582 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300583 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300584}
585
Avi Kivitya9b21b62008-06-24 11:48:49 +0300586static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300587{
Avi Kivity15ad7142007-07-11 18:17:21 +0300588 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300589
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400590 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300591 return;
592
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200593 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400594 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200595 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300596 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200597 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300598 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300599 /*
600 * If we have to reload gs, we must take care to
601 * preserve our gs base.
602 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300603 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300604 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300605#ifdef CONFIG_X86_64
606 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
607#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300608 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300609 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200610 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400611 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
612 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300613 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300614}
615
Avi Kivitya9b21b62008-06-24 11:48:49 +0300616static void vmx_load_host_state(struct vcpu_vmx *vmx)
617{
618 preempt_disable();
619 __vmx_load_host_state(vmx);
620 preempt_enable();
621}
622
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623/*
624 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
625 * vcpu mutex is already taken.
626 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300627static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800628{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400629 struct vcpu_vmx *vmx = to_vmx(vcpu);
630 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200631 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800632
Eddie Donga3d7f852007-09-03 16:15:12 +0300633 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000634 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300635 kvm_migrate_timers(vcpu);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800636 vpid_sync_vcpu_all(vmx);
Avi Kivity543e4242008-05-13 16:22:47 +0300637 local_irq_disable();
638 list_add(&vmx->local_vcpus_link,
639 &per_cpu(vcpus_on_cpu, cpu));
640 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300641 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800642
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400643 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800644 u8 error;
645
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400646 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300647 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800648 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
649 : "cc");
650 if (error)
651 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400652 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653 }
654
655 if (vcpu->cpu != cpu) {
656 struct descriptor_table dt;
657 unsigned long sysenter_esp;
658
659 vcpu->cpu = cpu;
660 /*
661 * Linux uses per-cpu TSS and GDT, so set these when switching
662 * processors.
663 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300664 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
665 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
667
668 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
669 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300670
671 /*
672 * Make sure the time stamp counter is monotonous.
673 */
674 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200675 if (tsc_this < vcpu->arch.host_tsc) {
676 delta = vcpu->arch.host_tsc - tsc_this;
677 new_offset = vmcs_read64(TSC_OFFSET) + delta;
678 vmcs_write64(TSC_OFFSET, new_offset);
679 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800680 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800681}
682
683static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
684{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300685 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800686}
687
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300688static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
689{
690 if (vcpu->fpu_active)
691 return;
692 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000693 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800694 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92fa2007-07-17 23:19:08 +1000695 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300696 update_exception_bitmap(vcpu);
697}
698
699static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
700{
701 if (!vcpu->fpu_active)
702 return;
703 vcpu->fpu_active = 0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000704 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300705 update_exception_bitmap(vcpu);
706}
707
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
709{
710 return vmcs_readl(GUEST_RFLAGS);
711}
712
713static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
714{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800715 if (vcpu->arch.rmode.active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100716 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800717 vmcs_writel(GUEST_RFLAGS, rflags);
718}
719
720static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
721{
722 unsigned long rip;
723 u32 interruptibility;
724
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300725 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800726 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300727 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728
729 /*
730 * We emulated an instruction, so temporary interrupt blocking
731 * should be removed, if set.
732 */
733 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
734 if (interruptibility & 3)
735 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
736 interruptibility & ~3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800737 vcpu->arch.interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800738}
739
Avi Kivity298101d2007-11-25 13:41:11 +0200740static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
741 bool has_error_code, u32 error_code)
742{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200743 struct vcpu_vmx *vmx = to_vmx(vcpu);
744
745 if (has_error_code)
746 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
747
748 if (vcpu->arch.rmode.active) {
749 vmx->rmode.irq.pending = true;
750 vmx->rmode.irq.vector = nr;
751 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
752 if (nr == BP_VECTOR)
753 vmx->rmode.irq.rip++;
754 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
755 nr | INTR_TYPE_SOFT_INTR
756 | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0)
757 | INTR_INFO_VALID_MASK);
758 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
759 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
760 return;
761 }
762
Avi Kivity298101d2007-11-25 13:41:11 +0200763 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
764 nr | INTR_TYPE_EXCEPTION
Ryan Harper2e113842008-02-11 10:26:38 -0600765 | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0)
Avi Kivity298101d2007-11-25 13:41:11 +0200766 | INTR_INFO_VALID_MASK);
Avi Kivity298101d2007-11-25 13:41:11 +0200767}
768
769static bool vmx_exception_injected(struct kvm_vcpu *vcpu)
770{
Avi Kivity35920a32008-07-03 14:50:12 +0300771 return false;
Avi Kivity298101d2007-11-25 13:41:11 +0200772}
773
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774/*
Eddie Donga75beee2007-05-17 18:55:15 +0300775 * Swap MSR entry in host/guest MSR entry array.
776 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200777#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000778static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300779{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400780 struct kvm_msr_entry tmp;
781
782 tmp = vmx->guest_msrs[to];
783 vmx->guest_msrs[to] = vmx->guest_msrs[from];
784 vmx->guest_msrs[from] = tmp;
785 tmp = vmx->host_msrs[to];
786 vmx->host_msrs[to] = vmx->host_msrs[from];
787 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300788}
Gabriel C54e11fa2007-08-01 16:23:10 +0200789#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300790
791/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300792 * Set up the vmcs to automatically save and restore system
793 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
794 * mode, as fiddling with msrs is very expensive.
795 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000796static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300797{
Eddie Dong2cc51562007-05-21 07:28:09 +0300798 int save_nmsrs;
Avi Kivitye38aea32007-04-19 13:22:48 +0300799
Avi Kivity33f9c502008-02-27 16:06:57 +0200800 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300801 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300802#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000803 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300804 int index;
805
Rusty Russell8b9cf982007-07-30 16:31:43 +1000806 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300807 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000808 move_msr_up(vmx, index, save_nmsrs++);
809 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300810 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000811 move_msr_up(vmx, index, save_nmsrs++);
812 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300813 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000814 move_msr_up(vmx, index, save_nmsrs++);
815 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300816 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000817 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300818 /*
819 * MSR_K6_STAR is only needed on long mode guests, and only
820 * if efer.sce is enabled.
821 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000822 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800823 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000824 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300825 }
Eddie Donga75beee2007-05-17 18:55:15 +0300826#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400827 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300828
Eddie Donga75beee2007-05-17 18:55:15 +0300829#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400830 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000831 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300832#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000833 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivitye38aea32007-04-19 13:22:48 +0300834}
835
836/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837 * reads and returns guest's timestamp counter "register"
838 * guest_tsc = host_tsc + tsc_offset -- 21.3
839 */
840static u64 guest_read_tsc(void)
841{
842 u64 host_tsc, tsc_offset;
843
844 rdtscll(host_tsc);
845 tsc_offset = vmcs_read64(TSC_OFFSET);
846 return host_tsc + tsc_offset;
847}
848
849/*
850 * writes 'guest_tsc' into guest's timestamp counter "register"
851 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
852 */
853static void guest_write_tsc(u64 guest_tsc)
854{
855 u64 host_tsc;
856
857 rdtscll(host_tsc);
858 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
859}
860
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861/*
862 * Reads an msr value (of 'msr_index') into 'pdata'.
863 * Returns 0 on success, non-0 otherwise.
864 * Assumes vcpu_load() was already called.
865 */
866static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
867{
868 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400869 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870
871 if (!pdata) {
872 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
873 return -EINVAL;
874 }
875
876 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800877#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878 case MSR_FS_BASE:
879 data = vmcs_readl(GUEST_FS_BASE);
880 break;
881 case MSR_GS_BASE:
882 data = vmcs_readl(GUEST_GS_BASE);
883 break;
884 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800885 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886#endif
887 case MSR_IA32_TIME_STAMP_COUNTER:
888 data = guest_read_tsc();
889 break;
890 case MSR_IA32_SYSENTER_CS:
891 data = vmcs_read32(GUEST_SYSENTER_CS);
892 break;
893 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200894 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895 break;
896 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200897 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000900 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800901 if (msr) {
902 data = msr->data;
903 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800905 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906 }
907
908 *pdata = data;
909 return 0;
910}
911
912/*
913 * Writes msr value into into the appropriate "register".
914 * Returns 0 on success, non-0 otherwise.
915 * Assumes vcpu_load() was already called.
916 */
917static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
918{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400919 struct vcpu_vmx *vmx = to_vmx(vcpu);
920 struct kvm_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +0300921 int ret = 0;
922
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800924#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800925 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300926 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +0300927 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +0300928 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929 case MSR_FS_BASE:
930 vmcs_writel(GUEST_FS_BASE, data);
931 break;
932 case MSR_GS_BASE:
933 vmcs_writel(GUEST_GS_BASE, data);
934 break;
935#endif
936 case MSR_IA32_SYSENTER_CS:
937 vmcs_write32(GUEST_SYSENTER_CS, data);
938 break;
939 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200940 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941 break;
942 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200943 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200945 case MSR_IA32_TIME_STAMP_COUNTER:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946 guest_write_tsc(data);
947 break;
Chris Lalancetteefa67e02008-06-20 09:51:30 +0200948 case MSR_P6_PERFCTR0:
949 case MSR_P6_PERFCTR1:
950 case MSR_P6_EVNTSEL0:
951 case MSR_P6_EVNTSEL1:
952 /*
953 * Just discard all writes to the performance counters; this
954 * should keep both older linux and windows 64-bit guests
955 * happy
956 */
957 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", msr_index, data);
958
959 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 default:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300961 vmx_load_host_state(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000962 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800963 if (msr) {
964 msr->data = data;
965 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300967 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 }
969
Eddie Dong2cc51562007-05-21 07:28:09 +0300970 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971}
972
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300973static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300975 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
976 switch (reg) {
977 case VCPU_REGS_RSP:
978 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
979 break;
980 case VCPU_REGS_RIP:
981 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
982 break;
983 default:
984 break;
985 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986}
987
988static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
989{
990 unsigned long dr7 = 0x400;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991 int old_singlestep;
992
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 old_singlestep = vcpu->guest_debug.singlestep;
994
995 vcpu->guest_debug.enabled = dbg->enabled;
996 if (vcpu->guest_debug.enabled) {
997 int i;
998
999 dr7 |= 0x200; /* exact */
1000 for (i = 0; i < 4; ++i) {
1001 if (!dbg->breakpoints[i].enabled)
1002 continue;
1003 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
1004 dr7 |= 2 << (i*2); /* global enable */
1005 dr7 |= 0 << (i*4+16); /* execution breakpoint */
1006 }
1007
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008 vcpu->guest_debug.singlestep = dbg->singlestep;
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001009 } else
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010 vcpu->guest_debug.singlestep = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011
1012 if (old_singlestep && !vcpu->guest_debug.singlestep) {
1013 unsigned long flags;
1014
1015 flags = vmcs_readl(GUEST_RFLAGS);
1016 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1017 vmcs_writel(GUEST_RFLAGS, flags);
1018 }
1019
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001020 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021 vmcs_writel(GUEST_DR7, dr7);
1022
1023 return 0;
1024}
1025
Eddie Dong2a8067f2007-08-06 16:29:07 +03001026static int vmx_get_irq(struct kvm_vcpu *vcpu)
1027{
Avi Kivityf7d92382008-07-03 16:14:28 +03001028 if (!vcpu->arch.interrupt.pending)
1029 return -1;
1030 return vcpu->arch.interrupt.nr;
Eddie Dong2a8067f2007-08-06 16:29:07 +03001031}
1032
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033static __init int cpu_has_kvm_support(void)
1034{
1035 unsigned long ecx = cpuid_ecx(1);
1036 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
1037}
1038
1039static __init int vmx_disabled_by_bios(void)
1040{
1041 u64 msr;
1042
1043 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yangca60dfb2008-06-24 17:02:38 +08001044 return (msr & (IA32_FEATURE_CONTROL_LOCKED_BIT |
1045 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT))
1046 == IA32_FEATURE_CONTROL_LOCKED_BIT;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001047 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048}
1049
Avi Kivity774c47f2007-02-12 00:54:47 -08001050static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051{
1052 int cpu = raw_smp_processor_id();
1053 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1054 u64 old;
1055
Avi Kivity543e4242008-05-13 16:22:47 +03001056 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yangca60dfb2008-06-24 17:02:38 +08001058 if ((old & (IA32_FEATURE_CONTROL_LOCKED_BIT |
1059 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT))
1060 != (IA32_FEATURE_CONTROL_LOCKED_BIT |
1061 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001062 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001063 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yangca60dfb2008-06-24 17:02:38 +08001064 IA32_FEATURE_CONTROL_LOCKED_BIT |
1065 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT);
Rusty Russell66aee912007-07-17 23:34:16 +10001066 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001067 asm volatile (ASM_VMX_VMXON_RAX
1068 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 : "memory", "cc");
1070}
1071
Avi Kivity543e4242008-05-13 16:22:47 +03001072static void vmclear_local_vcpus(void)
1073{
1074 int cpu = raw_smp_processor_id();
1075 struct vcpu_vmx *vmx, *n;
1076
1077 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1078 local_vcpus_link)
1079 __vcpu_clear(vmx);
1080}
1081
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082static void hardware_disable(void *garbage)
1083{
Avi Kivity543e4242008-05-13 16:22:47 +03001084 vmclear_local_vcpus();
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001085 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eli Collinse693d712008-06-01 20:24:40 -07001086 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087}
1088
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001089static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001090 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091{
1092 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001093 u32 ctl = ctl_min | ctl_opt;
1094
1095 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1096
1097 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1098 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1099
1100 /* Ensure minimum (required) set of control bits are supported. */
1101 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001102 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001103
1104 *result = ctl;
1105 return 0;
1106}
1107
Yang, Sheng002c7f72007-07-31 14:23:01 +03001108static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001109{
1110 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001111 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001112 u32 _pin_based_exec_control = 0;
1113 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001114 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001115 u32 _vmexit_control = 0;
1116 u32 _vmentry_control = 0;
1117
1118 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001119 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001120 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1121 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001122 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001123
1124 min = CPU_BASED_HLT_EXITING |
1125#ifdef CONFIG_X86_64
1126 CPU_BASED_CR8_LOAD_EXITING |
1127 CPU_BASED_CR8_STORE_EXITING |
1128#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001129 CPU_BASED_CR3_LOAD_EXITING |
1130 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001131 CPU_BASED_USE_IO_BITMAPS |
1132 CPU_BASED_MOV_DR_EXITING |
1133 CPU_BASED_USE_TSC_OFFSETING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001134 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001135 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001136 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001137 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1138 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001139 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001140#ifdef CONFIG_X86_64
1141 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1142 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1143 ~CPU_BASED_CR8_STORE_EXITING;
1144#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001145 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001146 min2 = 0;
1147 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001148 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001149 SECONDARY_EXEC_ENABLE_VPID |
1150 SECONDARY_EXEC_ENABLE_EPT;
1151 if (adjust_vmx_controls(min2, opt2,
1152 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001153 &_cpu_based_2nd_exec_control) < 0)
1154 return -EIO;
1155 }
1156#ifndef CONFIG_X86_64
1157 if (!(_cpu_based_2nd_exec_control &
1158 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1159 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1160#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001161 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
1162 /* CR3 accesses don't need to cause VM Exits when EPT enabled */
1163 min &= ~(CPU_BASED_CR3_LOAD_EXITING |
1164 CPU_BASED_CR3_STORE_EXITING);
1165 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1166 &_cpu_based_exec_control) < 0)
1167 return -EIO;
1168 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1169 vmx_capability.ept, vmx_capability.vpid);
1170 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001171
1172 min = 0;
1173#ifdef CONFIG_X86_64
1174 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1175#endif
1176 opt = 0;
1177 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1178 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001179 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001180
1181 min = opt = 0;
1182 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1183 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001184 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001186 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001187
1188 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1189 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001190 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001191
1192#ifdef CONFIG_X86_64
1193 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1194 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001195 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001196#endif
1197
1198 /* Require Write-Back (WB) memory type for VMCS accesses. */
1199 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001200 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001201
Yang, Sheng002c7f72007-07-31 14:23:01 +03001202 vmcs_conf->size = vmx_msr_high & 0x1fff;
1203 vmcs_conf->order = get_order(vmcs_config.size);
1204 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001205
Yang, Sheng002c7f72007-07-31 14:23:01 +03001206 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1207 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001208 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001209 vmcs_conf->vmexit_ctrl = _vmexit_control;
1210 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001211
1212 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001213}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214
1215static struct vmcs *alloc_vmcs_cpu(int cpu)
1216{
1217 int node = cpu_to_node(cpu);
1218 struct page *pages;
1219 struct vmcs *vmcs;
1220
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001221 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222 if (!pages)
1223 return NULL;
1224 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001225 memset(vmcs, 0, vmcs_config.size);
1226 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001227 return vmcs;
1228}
1229
1230static struct vmcs *alloc_vmcs(void)
1231{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001232 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001233}
1234
1235static void free_vmcs(struct vmcs *vmcs)
1236{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001237 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001238}
1239
Sam Ravnborg39959582007-06-01 00:47:13 -07001240static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001241{
1242 int cpu;
1243
1244 for_each_online_cpu(cpu)
1245 free_vmcs(per_cpu(vmxarea, cpu));
1246}
1247
Avi Kivity6aa8b732006-12-10 02:21:36 -08001248static __init int alloc_kvm_area(void)
1249{
1250 int cpu;
1251
1252 for_each_online_cpu(cpu) {
1253 struct vmcs *vmcs;
1254
1255 vmcs = alloc_vmcs_cpu(cpu);
1256 if (!vmcs) {
1257 free_kvm_area();
1258 return -ENOMEM;
1259 }
1260
1261 per_cpu(vmxarea, cpu) = vmcs;
1262 }
1263 return 0;
1264}
1265
1266static __init int hardware_setup(void)
1267{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001268 if (setup_vmcs_config(&vmcs_config) < 0)
1269 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001270
1271 if (boot_cpu_has(X86_FEATURE_NX))
1272 kvm_enable_efer_bits(EFER_NX);
1273
Avi Kivity6aa8b732006-12-10 02:21:36 -08001274 return alloc_kvm_area();
1275}
1276
1277static __exit void hardware_unsetup(void)
1278{
1279 free_kvm_area();
1280}
1281
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1283{
1284 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1285
Avi Kivity6af11b92007-03-19 13:18:10 +02001286 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287 vmcs_write16(sf->selector, save->selector);
1288 vmcs_writel(sf->base, save->base);
1289 vmcs_write32(sf->limit, save->limit);
1290 vmcs_write32(sf->ar_bytes, save->ar);
1291 } else {
1292 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1293 << AR_DPL_SHIFT;
1294 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1295 }
1296}
1297
1298static void enter_pmode(struct kvm_vcpu *vcpu)
1299{
1300 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001301 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001303 vmx->emulation_required = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001304 vcpu->arch.rmode.active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001305
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001306 vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base);
1307 vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit);
1308 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309
1310 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001311 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001312 flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313 vmcs_writel(GUEST_RFLAGS, flags);
1314
Rusty Russell66aee912007-07-17 23:34:16 +10001315 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1316 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317
1318 update_exception_bitmap(vcpu);
1319
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001320 if (emulate_invalid_guest_state)
1321 return;
1322
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001323 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1324 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1325 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1326 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327
1328 vmcs_write16(GUEST_SS_SELECTOR, 0);
1329 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1330
1331 vmcs_write16(GUEST_CS_SELECTOR,
1332 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1333 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1334}
1335
Mike Dayd77c26f2007-10-08 09:02:08 -04001336static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001338 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001339 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1340 kvm->memslots[0].npages - 3;
1341 return base_gfn << PAGE_SHIFT;
1342 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001343 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344}
1345
1346static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1347{
1348 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1349
1350 save->selector = vmcs_read16(sf->selector);
1351 save->base = vmcs_readl(sf->base);
1352 save->limit = vmcs_read32(sf->limit);
1353 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001354 vmcs_write16(sf->selector, save->base >> 4);
1355 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356 vmcs_write32(sf->limit, 0xffff);
1357 vmcs_write32(sf->ar_bytes, 0xf3);
1358}
1359
1360static void enter_rmode(struct kvm_vcpu *vcpu)
1361{
1362 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001363 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001365 vmx->emulation_required = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001366 vcpu->arch.rmode.active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001368 vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001369 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1370
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001371 vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1373
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001374 vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1376
1377 flags = vmcs_readl(GUEST_RFLAGS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001378 vcpu->arch.rmode.save_iopl
1379 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001381 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382
1383 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001384 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385 update_exception_bitmap(vcpu);
1386
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001387 if (emulate_invalid_guest_state)
1388 goto continue_rmode;
1389
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1391 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1392 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1393
1394 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001395 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001396 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1397 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1399
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001400 fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1401 fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1402 fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1403 fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001404
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001405continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001406 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001407 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408}
1409
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001410#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411
1412static void enter_lmode(struct kvm_vcpu *vcpu)
1413{
1414 u32 guest_tr_ar;
1415
1416 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1417 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1418 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001419 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420 vmcs_write32(GUEST_TR_AR_BYTES,
1421 (guest_tr_ar & ~AR_TYPE_MASK)
1422 | AR_TYPE_BUSY_64_TSS);
1423 }
1424
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001425 vcpu->arch.shadow_efer |= EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426
Rusty Russell8b9cf982007-07-30 16:31:43 +10001427 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428 vmcs_write32(VM_ENTRY_CONTROLS,
1429 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001430 | VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431}
1432
1433static void exit_lmode(struct kvm_vcpu *vcpu)
1434{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001435 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436
1437 vmcs_write32(VM_ENTRY_CONTROLS,
1438 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001439 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440}
1441
1442#endif
1443
Sheng Yang2384d2b2008-01-17 15:14:33 +08001444static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1445{
1446 vpid_sync_vcpu_all(to_vmx(vcpu));
Sheng Yang4e1096d2008-07-06 19:16:51 +08001447 if (vm_need_ept())
1448 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001449}
1450
Anthony Liguori25c4c272007-04-27 09:29:21 +03001451static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001452{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001453 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1454 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001455}
1456
Sheng Yang14394422008-04-28 12:24:45 +08001457static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1458{
1459 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1460 if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
1461 printk(KERN_ERR "EPT: Fail to load pdptrs!\n");
1462 return;
1463 }
1464 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1465 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1466 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1467 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1468 }
1469}
1470
1471static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1472
1473static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1474 unsigned long cr0,
1475 struct kvm_vcpu *vcpu)
1476{
1477 if (!(cr0 & X86_CR0_PG)) {
1478 /* From paging/starting to nonpaging */
1479 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001480 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001481 (CPU_BASED_CR3_LOAD_EXITING |
1482 CPU_BASED_CR3_STORE_EXITING));
1483 vcpu->arch.cr0 = cr0;
1484 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1485 *hw_cr0 |= X86_CR0_PE | X86_CR0_PG;
1486 *hw_cr0 &= ~X86_CR0_WP;
1487 } else if (!is_paging(vcpu)) {
1488 /* From nonpaging to paging */
1489 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001490 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001491 ~(CPU_BASED_CR3_LOAD_EXITING |
1492 CPU_BASED_CR3_STORE_EXITING));
1493 vcpu->arch.cr0 = cr0;
1494 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1495 if (!(vcpu->arch.cr0 & X86_CR0_WP))
1496 *hw_cr0 &= ~X86_CR0_WP;
1497 }
1498}
1499
1500static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1501 struct kvm_vcpu *vcpu)
1502{
1503 if (!is_paging(vcpu)) {
1504 *hw_cr4 &= ~X86_CR4_PAE;
1505 *hw_cr4 |= X86_CR4_PSE;
1506 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1507 *hw_cr4 &= ~X86_CR4_PAE;
1508}
1509
Avi Kivity6aa8b732006-12-10 02:21:36 -08001510static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1511{
Sheng Yang14394422008-04-28 12:24:45 +08001512 unsigned long hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) |
1513 KVM_VM_CR0_ALWAYS_ON;
1514
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001515 vmx_fpu_deactivate(vcpu);
1516
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001517 if (vcpu->arch.rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001518 enter_pmode(vcpu);
1519
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001520 if (!vcpu->arch.rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521 enter_rmode(vcpu);
1522
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001523#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001524 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001525 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001526 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001527 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528 exit_lmode(vcpu);
1529 }
1530#endif
1531
Sheng Yang14394422008-04-28 12:24:45 +08001532 if (vm_need_ept())
1533 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1534
Avi Kivity6aa8b732006-12-10 02:21:36 -08001535 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001536 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001537 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001538
Rusty Russell707d92fa2007-07-17 23:19:08 +10001539 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001540 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001541}
1542
Sheng Yang14394422008-04-28 12:24:45 +08001543static u64 construct_eptp(unsigned long root_hpa)
1544{
1545 u64 eptp;
1546
1547 /* TODO write the value reading from MSR */
1548 eptp = VMX_EPT_DEFAULT_MT |
1549 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1550 eptp |= (root_hpa & PAGE_MASK);
1551
1552 return eptp;
1553}
1554
Avi Kivity6aa8b732006-12-10 02:21:36 -08001555static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1556{
Sheng Yang14394422008-04-28 12:24:45 +08001557 unsigned long guest_cr3;
1558 u64 eptp;
1559
1560 guest_cr3 = cr3;
1561 if (vm_need_ept()) {
1562 eptp = construct_eptp(cr3);
1563 vmcs_write64(EPT_POINTER, eptp);
1564 ept_sync_context(eptp);
1565 ept_load_pdptrs(vcpu);
1566 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
1567 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1568 }
1569
Sheng Yang2384d2b2008-01-17 15:14:33 +08001570 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001571 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001572 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001573 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001574}
1575
1576static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1577{
Sheng Yang14394422008-04-28 12:24:45 +08001578 unsigned long hw_cr4 = cr4 | (vcpu->arch.rmode.active ?
1579 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1580
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001581 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08001582 if (vm_need_ept())
1583 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1584
1585 vmcs_writel(CR4_READ_SHADOW, cr4);
1586 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001587}
1588
Avi Kivity6aa8b732006-12-10 02:21:36 -08001589static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1590{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001591 struct vcpu_vmx *vmx = to_vmx(vcpu);
1592 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001594 vcpu->arch.shadow_efer = efer;
Joerg Roedel9f62e192008-01-31 14:57:39 +01001595 if (!msr)
1596 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001597 if (efer & EFER_LMA) {
1598 vmcs_write32(VM_ENTRY_CONTROLS,
1599 vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001600 VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001601 msr->data = efer;
1602
1603 } else {
1604 vmcs_write32(VM_ENTRY_CONTROLS,
1605 vmcs_read32(VM_ENTRY_CONTROLS) &
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001606 ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607
1608 msr->data = efer & ~EFER_LME;
1609 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001610 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611}
1612
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1614{
1615 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1616
1617 return vmcs_readl(sf->base);
1618}
1619
1620static void vmx_get_segment(struct kvm_vcpu *vcpu,
1621 struct kvm_segment *var, int seg)
1622{
1623 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1624 u32 ar;
1625
1626 var->base = vmcs_readl(sf->base);
1627 var->limit = vmcs_read32(sf->limit);
1628 var->selector = vmcs_read16(sf->selector);
1629 ar = vmcs_read32(sf->ar_bytes);
1630 if (ar & AR_UNUSABLE_MASK)
1631 ar = 0;
1632 var->type = ar & 15;
1633 var->s = (ar >> 4) & 1;
1634 var->dpl = (ar >> 5) & 3;
1635 var->present = (ar >> 7) & 1;
1636 var->avl = (ar >> 12) & 1;
1637 var->l = (ar >> 13) & 1;
1638 var->db = (ar >> 14) & 1;
1639 var->g = (ar >> 15) & 1;
1640 var->unusable = (ar >> 16) & 1;
1641}
1642
Izik Eidus2e4d2652008-03-24 19:38:34 +02001643static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1644{
1645 struct kvm_segment kvm_seg;
1646
1647 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1648 return 0;
1649
1650 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1651 return 3;
1652
1653 vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
1654 return kvm_seg.selector & 3;
1655}
1656
Avi Kivity653e3102007-05-07 10:55:37 +03001657static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659 u32 ar;
1660
Avi Kivity653e3102007-05-07 10:55:37 +03001661 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662 ar = 1 << 16;
1663 else {
1664 ar = var->type & 15;
1665 ar |= (var->s & 1) << 4;
1666 ar |= (var->dpl & 3) << 5;
1667 ar |= (var->present & 1) << 7;
1668 ar |= (var->avl & 1) << 12;
1669 ar |= (var->l & 1) << 13;
1670 ar |= (var->db & 1) << 14;
1671 ar |= (var->g & 1) << 15;
1672 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001673 if (ar == 0) /* a 0 value means unusable */
1674 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001675
1676 return ar;
1677}
1678
1679static void vmx_set_segment(struct kvm_vcpu *vcpu,
1680 struct kvm_segment *var, int seg)
1681{
1682 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1683 u32 ar;
1684
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001685 if (vcpu->arch.rmode.active && seg == VCPU_SREG_TR) {
1686 vcpu->arch.rmode.tr.selector = var->selector;
1687 vcpu->arch.rmode.tr.base = var->base;
1688 vcpu->arch.rmode.tr.limit = var->limit;
1689 vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001690 return;
1691 }
1692 vmcs_writel(sf->base, var->base);
1693 vmcs_write32(sf->limit, var->limit);
1694 vmcs_write16(sf->selector, var->selector);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001695 if (vcpu->arch.rmode.active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001696 /*
1697 * Hack real-mode segments into vm86 compatibility.
1698 */
1699 if (var->base == 0xffff0000 && var->selector == 0xf000)
1700 vmcs_writel(sf->base, 0xf0000);
1701 ar = 0xf3;
1702 } else
1703 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001704 vmcs_write32(sf->ar_bytes, ar);
1705}
1706
Avi Kivity6aa8b732006-12-10 02:21:36 -08001707static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1708{
1709 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1710
1711 *db = (ar >> 14) & 1;
1712 *l = (ar >> 13) & 1;
1713}
1714
1715static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1716{
1717 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1718 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1719}
1720
1721static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1722{
1723 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1724 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1725}
1726
1727static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1728{
1729 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1730 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1731}
1732
1733static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1734{
1735 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1736 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1737}
1738
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001739static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1740{
1741 struct kvm_segment var;
1742 u32 ar;
1743
1744 vmx_get_segment(vcpu, &var, seg);
1745 ar = vmx_segment_access_rights(&var);
1746
1747 if (var.base != (var.selector << 4))
1748 return false;
1749 if (var.limit != 0xffff)
1750 return false;
1751 if (ar != 0xf3)
1752 return false;
1753
1754 return true;
1755}
1756
1757static bool code_segment_valid(struct kvm_vcpu *vcpu)
1758{
1759 struct kvm_segment cs;
1760 unsigned int cs_rpl;
1761
1762 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1763 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1764
1765 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1766 return false;
1767 if (!cs.s)
1768 return false;
1769 if (!(~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK))) {
1770 if (cs.dpl > cs_rpl)
1771 return false;
1772 } else if (cs.type & AR_TYPE_CODE_MASK) {
1773 if (cs.dpl != cs_rpl)
1774 return false;
1775 }
1776 if (!cs.present)
1777 return false;
1778
1779 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1780 return true;
1781}
1782
1783static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1784{
1785 struct kvm_segment ss;
1786 unsigned int ss_rpl;
1787
1788 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1789 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1790
1791 if ((ss.type != 3) || (ss.type != 7))
1792 return false;
1793 if (!ss.s)
1794 return false;
1795 if (ss.dpl != ss_rpl) /* DPL != RPL */
1796 return false;
1797 if (!ss.present)
1798 return false;
1799
1800 return true;
1801}
1802
1803static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1804{
1805 struct kvm_segment var;
1806 unsigned int rpl;
1807
1808 vmx_get_segment(vcpu, &var, seg);
1809 rpl = var.selector & SELECTOR_RPL_MASK;
1810
1811 if (!var.s)
1812 return false;
1813 if (!var.present)
1814 return false;
1815 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
1816 if (var.dpl < rpl) /* DPL < RPL */
1817 return false;
1818 }
1819
1820 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1821 * rights flags
1822 */
1823 return true;
1824}
1825
1826static bool tr_valid(struct kvm_vcpu *vcpu)
1827{
1828 struct kvm_segment tr;
1829
1830 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
1831
1832 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1833 return false;
1834 if ((tr.type != 3) || (tr.type != 11)) /* TODO: Check if guest is in IA32e mode */
1835 return false;
1836 if (!tr.present)
1837 return false;
1838
1839 return true;
1840}
1841
1842static bool ldtr_valid(struct kvm_vcpu *vcpu)
1843{
1844 struct kvm_segment ldtr;
1845
1846 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
1847
1848 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1849 return false;
1850 if (ldtr.type != 2)
1851 return false;
1852 if (!ldtr.present)
1853 return false;
1854
1855 return true;
1856}
1857
1858static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
1859{
1860 struct kvm_segment cs, ss;
1861
1862 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1863 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1864
1865 return ((cs.selector & SELECTOR_RPL_MASK) ==
1866 (ss.selector & SELECTOR_RPL_MASK));
1867}
1868
1869/*
1870 * Check if guest state is valid. Returns true if valid, false if
1871 * not.
1872 * We assume that registers are always usable
1873 */
1874static bool guest_state_valid(struct kvm_vcpu *vcpu)
1875{
1876 /* real mode guest state checks */
1877 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
1878 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
1879 return false;
1880 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
1881 return false;
1882 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
1883 return false;
1884 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
1885 return false;
1886 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
1887 return false;
1888 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
1889 return false;
1890 } else {
1891 /* protected mode guest state checks */
1892 if (!cs_ss_rpl_check(vcpu))
1893 return false;
1894 if (!code_segment_valid(vcpu))
1895 return false;
1896 if (!stack_segment_valid(vcpu))
1897 return false;
1898 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
1899 return false;
1900 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
1901 return false;
1902 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
1903 return false;
1904 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
1905 return false;
1906 if (!tr_valid(vcpu))
1907 return false;
1908 if (!ldtr_valid(vcpu))
1909 return false;
1910 }
1911 /* TODO:
1912 * - Add checks on RIP
1913 * - Add checks on RFLAGS
1914 */
1915
1916 return true;
1917}
1918
Mike Dayd77c26f2007-10-08 09:02:08 -04001919static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001920{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001922 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001923 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02001924 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001925
Izik Eidus195aefd2007-10-01 22:14:18 +02001926 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1927 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001928 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001929 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08001930 r = kvm_write_guest_page(kvm, fn++, &data,
1931 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02001932 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001933 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001934 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
1935 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001936 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001937 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1938 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001939 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001940 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001941 r = kvm_write_guest_page(kvm, fn, &data,
1942 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
1943 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02001944 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001945 goto out;
1946
1947 ret = 1;
1948out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001949 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001950}
1951
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001952static int init_rmode_identity_map(struct kvm *kvm)
1953{
1954 int i, r, ret;
1955 pfn_t identity_map_pfn;
1956 u32 tmp;
1957
1958 if (!vm_need_ept())
1959 return 1;
1960 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
1961 printk(KERN_ERR "EPT: identity-mapping pagetable "
1962 "haven't been allocated!\n");
1963 return 0;
1964 }
1965 if (likely(kvm->arch.ept_identity_pagetable_done))
1966 return 1;
1967 ret = 0;
1968 identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT;
1969 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
1970 if (r < 0)
1971 goto out;
1972 /* Set up identity-mapping pagetable for EPT in real mode */
1973 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
1974 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
1975 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
1976 r = kvm_write_guest_page(kvm, identity_map_pfn,
1977 &tmp, i * sizeof(tmp), sizeof(tmp));
1978 if (r < 0)
1979 goto out;
1980 }
1981 kvm->arch.ept_identity_pagetable_done = true;
1982 ret = 1;
1983out:
1984 return ret;
1985}
1986
Avi Kivity6aa8b732006-12-10 02:21:36 -08001987static void seg_setup(int seg)
1988{
1989 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1990
1991 vmcs_write16(sf->selector, 0);
1992 vmcs_writel(sf->base, 0);
1993 vmcs_write32(sf->limit, 0xffff);
Avi Kivitya16b20d2008-08-20 15:48:27 +03001994 vmcs_write32(sf->ar_bytes, 0xf3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001995}
1996
Sheng Yangf78e0e22007-10-29 09:40:42 +08001997static int alloc_apic_access_page(struct kvm *kvm)
1998{
1999 struct kvm_userspace_memory_region kvm_userspace_mem;
2000 int r = 0;
2001
Izik Eidus72dc67a2008-02-10 18:04:15 +02002002 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002003 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002004 goto out;
2005 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2006 kvm_userspace_mem.flags = 0;
2007 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2008 kvm_userspace_mem.memory_size = PAGE_SIZE;
2009 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2010 if (r)
2011 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002012
2013 down_read(&current->mm->mmap_sem);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002014 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002015 up_read(&current->mm->mmap_sem);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002016out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002017 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002018 return r;
2019}
2020
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002021static int alloc_identity_pagetable(struct kvm *kvm)
2022{
2023 struct kvm_userspace_memory_region kvm_userspace_mem;
2024 int r = 0;
2025
2026 down_write(&kvm->slots_lock);
2027 if (kvm->arch.ept_identity_pagetable)
2028 goto out;
2029 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2030 kvm_userspace_mem.flags = 0;
2031 kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
2032 kvm_userspace_mem.memory_size = PAGE_SIZE;
2033 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2034 if (r)
2035 goto out;
2036
2037 down_read(&current->mm->mmap_sem);
2038 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
2039 VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT);
2040 up_read(&current->mm->mmap_sem);
2041out:
2042 up_write(&kvm->slots_lock);
2043 return r;
2044}
2045
Sheng Yang2384d2b2008-01-17 15:14:33 +08002046static void allocate_vpid(struct vcpu_vmx *vmx)
2047{
2048 int vpid;
2049
2050 vmx->vpid = 0;
2051 if (!enable_vpid || !cpu_has_vmx_vpid())
2052 return;
2053 spin_lock(&vmx_vpid_lock);
2054 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2055 if (vpid < VMX_NR_VPIDS) {
2056 vmx->vpid = vpid;
2057 __set_bit(vpid, vmx_vpid_bitmap);
2058 }
2059 spin_unlock(&vmx_vpid_lock);
2060}
2061
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002062static void vmx_disable_intercept_for_msr(struct page *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002063{
2064 void *va;
2065
2066 if (!cpu_has_vmx_msr_bitmap())
2067 return;
2068
2069 /*
2070 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2071 * have the write-low and read-high bitmap offsets the wrong way round.
2072 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2073 */
2074 va = kmap(msr_bitmap);
2075 if (msr <= 0x1fff) {
2076 __clear_bit(msr, va + 0x000); /* read-low */
2077 __clear_bit(msr, va + 0x800); /* write-low */
2078 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2079 msr &= 0x1fff;
2080 __clear_bit(msr, va + 0x400); /* read-high */
2081 __clear_bit(msr, va + 0xc00); /* write-high */
2082 }
2083 kunmap(msr_bitmap);
2084}
2085
Avi Kivity6aa8b732006-12-10 02:21:36 -08002086/*
2087 * Sets up the vmcs for emulated real mode.
2088 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002089static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090{
2091 u32 host_sysenter_cs;
2092 u32 junk;
2093 unsigned long a;
2094 struct descriptor_table dt;
2095 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002096 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002097 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002098
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03002100 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
2101 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002102
Sheng Yang25c5f222008-03-28 13:18:56 +08002103 if (cpu_has_vmx_msr_bitmap())
2104 vmcs_write64(MSR_BITMAP, page_to_phys(vmx_msr_bitmap));
2105
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2107
Avi Kivity6aa8b732006-12-10 02:21:36 -08002108 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002109 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2110 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002111
2112 exec_control = vmcs_config.cpu_based_exec_ctrl;
2113 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2114 exec_control &= ~CPU_BASED_TPR_SHADOW;
2115#ifdef CONFIG_X86_64
2116 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2117 CPU_BASED_CR8_LOAD_EXITING;
2118#endif
2119 }
Sheng Yangd56f5462008-04-25 10:13:16 +08002120 if (!vm_need_ept())
2121 exec_control |= CPU_BASED_CR3_STORE_EXITING |
2122 CPU_BASED_CR3_LOAD_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002123 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002124
Sheng Yang83ff3b92007-11-21 14:33:25 +08002125 if (cpu_has_secondary_exec_ctrls()) {
2126 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2127 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2128 exec_control &=
2129 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002130 if (vmx->vpid == 0)
2131 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yangd56f5462008-04-25 10:13:16 +08002132 if (!vm_need_ept())
2133 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002134 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2135 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002136
Avi Kivityc7addb92007-09-16 18:58:32 +02002137 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2138 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002139 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2140
2141 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2142 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2143 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2144
2145 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2146 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2147 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002148 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2149 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002151#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002152 rdmsrl(MSR_FS_BASE, a);
2153 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2154 rdmsrl(MSR_GS_BASE, a);
2155 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2156#else
2157 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2158 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2159#endif
2160
2161 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2162
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002163 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002164 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2165
Mike Dayd77c26f2007-10-08 09:02:08 -04002166 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002167 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002168 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2169 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2170 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002171
2172 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2173 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2174 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2175 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2176 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2177 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2178
Avi Kivity6aa8b732006-12-10 02:21:36 -08002179 for (i = 0; i < NR_VMX_MSR; ++i) {
2180 u32 index = vmx_msr_index[i];
2181 u32 data_low, data_high;
2182 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002183 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184
2185 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2186 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002187 if (wrmsr_safe(index, data_low, data_high) < 0)
2188 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002190 vmx->host_msrs[j].index = index;
2191 vmx->host_msrs[j].reserved = 0;
2192 vmx->host_msrs[j].data = data;
2193 vmx->guest_msrs[j] = vmx->host_msrs[j];
2194 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002195 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002196
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002197 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002198
2199 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002200 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2201
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002202 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2203 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2204
Sheng Yangf78e0e22007-10-29 09:40:42 +08002205
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002206 return 0;
2207}
2208
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002209static int init_rmode(struct kvm *kvm)
2210{
2211 if (!init_rmode_tss(kvm))
2212 return 0;
2213 if (!init_rmode_identity_map(kvm))
2214 return 0;
2215 return 1;
2216}
2217
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002218static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2219{
2220 struct vcpu_vmx *vmx = to_vmx(vcpu);
2221 u64 msr;
2222 int ret;
2223
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002224 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002225 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002226 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002227 ret = -ENOMEM;
2228 goto out;
2229 }
2230
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002231 vmx->vcpu.arch.rmode.active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002232
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002233 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002234 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002235 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
2236 if (vmx->vcpu.vcpu_id == 0)
2237 msr |= MSR_IA32_APICBASE_BSP;
2238 kvm_set_apic_base(&vmx->vcpu, msr);
2239
2240 fx_init(&vmx->vcpu);
2241
Avi Kivity5706be02008-08-20 15:07:31 +03002242 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002243 /*
2244 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2245 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2246 */
2247 if (vmx->vcpu.vcpu_id == 0) {
2248 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2249 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2250 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002251 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2252 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002253 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002254
2255 seg_setup(VCPU_SREG_DS);
2256 seg_setup(VCPU_SREG_ES);
2257 seg_setup(VCPU_SREG_FS);
2258 seg_setup(VCPU_SREG_GS);
2259 seg_setup(VCPU_SREG_SS);
2260
2261 vmcs_write16(GUEST_TR_SELECTOR, 0);
2262 vmcs_writel(GUEST_TR_BASE, 0);
2263 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2264 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2265
2266 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2267 vmcs_writel(GUEST_LDTR_BASE, 0);
2268 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2269 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2270
2271 vmcs_write32(GUEST_SYSENTER_CS, 0);
2272 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2273 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2274
2275 vmcs_writel(GUEST_RFLAGS, 0x02);
2276 if (vmx->vcpu.vcpu_id == 0)
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002277 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002278 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002279 kvm_rip_write(vcpu, 0);
2280 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002281
2282 /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */
2283 vmcs_writel(GUEST_DR7, 0x400);
2284
2285 vmcs_writel(GUEST_GDTR_BASE, 0);
2286 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2287
2288 vmcs_writel(GUEST_IDTR_BASE, 0);
2289 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2290
2291 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2292 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2293 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2294
2295 guest_write_tsc(0);
2296
2297 /* Special registers */
2298 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2299
2300 setup_msrs(vmx);
2301
Avi Kivity6aa8b732006-12-10 02:21:36 -08002302 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2303
Sheng Yangf78e0e22007-10-29 09:40:42 +08002304 if (cpu_has_vmx_tpr_shadow()) {
2305 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2306 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2307 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002308 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002309 vmcs_write32(TPR_THRESHOLD, 0);
2310 }
2311
2312 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2313 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002314 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002315
Sheng Yang2384d2b2008-01-17 15:14:33 +08002316 if (vmx->vpid != 0)
2317 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2318
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002319 vmx->vcpu.arch.cr0 = 0x60000010;
2320 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002321 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002322 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002323 vmx_fpu_activate(&vmx->vcpu);
2324 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325
Sheng Yang2384d2b2008-01-17 15:14:33 +08002326 vpid_sync_vcpu_all(vmx);
2327
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002328 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002329
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002330 /* HACK: Don't enable emulation on guest boot/reset */
2331 vmx->emulation_required = 0;
2332
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002334 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002335 return ret;
2336}
2337
Eddie Dong85f455f2007-07-06 12:20:49 +03002338static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
2339{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002340 struct vcpu_vmx *vmx = to_vmx(vcpu);
2341
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002342 KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler);
2343
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002344 if (vcpu->arch.rmode.active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002345 vmx->rmode.irq.pending = true;
2346 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002347 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Avi Kivity9c5623e2007-11-08 18:19:20 +02002348 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2349 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2350 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002351 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002352 return;
2353 }
2354 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2355 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
2356}
2357
Sheng Yangf08864b2008-05-15 18:23:25 +08002358static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2359{
2360 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2361 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002362}
2363
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
2365{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002366 int word_index = __ffs(vcpu->arch.irq_summary);
2367 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368 int irq = word_index * BITS_PER_LONG + bit_index;
2369
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002370 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
2371 if (!vcpu->arch.irq_pending[word_index])
2372 clear_bit(word_index, &vcpu->arch.irq_summary);
Avi Kivityecfc79c2008-08-14 11:13:16 +03002373 kvm_queue_interrupt(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374}
2375
Dor Laorc1150d82007-01-05 16:36:24 -08002376
2377static void do_interrupt_requests(struct kvm_vcpu *vcpu,
2378 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379{
Dor Laorc1150d82007-01-05 16:36:24 -08002380 u32 cpu_based_vm_exec_control;
2381
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002382 vcpu->arch.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08002383 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2384 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
2385
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002386 if (vcpu->arch.interrupt_window_open &&
Avi Kivityecfc79c2008-08-14 11:13:16 +03002387 vcpu->arch.irq_summary && !vcpu->arch.interrupt.pending)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002388 kvm_do_inject_irq(vcpu);
Dor Laorc1150d82007-01-05 16:36:24 -08002389
Avi Kivityecfc79c2008-08-14 11:13:16 +03002390 if (vcpu->arch.interrupt_window_open && vcpu->arch.interrupt.pending)
2391 vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
2392
Dor Laorc1150d82007-01-05 16:36:24 -08002393 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002394 if (!vcpu->arch.interrupt_window_open &&
2395 (vcpu->arch.irq_summary || kvm_run->request_interrupt_window))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396 /*
2397 * Interrupts blocked. Wait for unblock.
2398 */
Dor Laorc1150d82007-01-05 16:36:24 -08002399 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2400 else
2401 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2402 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002403}
2404
Izik Eiduscbc94022007-10-25 00:29:55 +02002405static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2406{
2407 int ret;
2408 struct kvm_userspace_memory_region tss_mem = {
2409 .slot = 8,
2410 .guest_phys_addr = addr,
2411 .memory_size = PAGE_SIZE * 3,
2412 .flags = 0,
2413 };
2414
2415 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2416 if (ret)
2417 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002418 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002419 return 0;
2420}
2421
Avi Kivity6aa8b732006-12-10 02:21:36 -08002422static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
2423{
2424 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
2425
2426 set_debugreg(dbg->bp[0], 0);
2427 set_debugreg(dbg->bp[1], 1);
2428 set_debugreg(dbg->bp[2], 2);
2429 set_debugreg(dbg->bp[3], 3);
2430
2431 if (dbg->singlestep) {
2432 unsigned long flags;
2433
2434 flags = vmcs_readl(GUEST_RFLAGS);
2435 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
2436 vmcs_writel(GUEST_RFLAGS, flags);
2437 }
2438}
2439
2440static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2441 int vec, u32 err_code)
2442{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002443 /*
2444 * Instruction with address size override prefix opcode 0x67
2445 * Cause the #SS fault with 0 error code in VM86 mode.
2446 */
2447 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02002448 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002449 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002450 /*
2451 * Forward all other exceptions that are valid in real mode.
2452 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2453 * the required debugging infrastructure rework.
2454 */
2455 switch (vec) {
2456 case DE_VECTOR:
2457 case DB_VECTOR:
2458 case BP_VECTOR:
2459 case OF_VECTOR:
2460 case BR_VECTOR:
2461 case UD_VECTOR:
2462 case DF_VECTOR:
2463 case SS_VECTOR:
2464 case GP_VECTOR:
2465 case MF_VECTOR:
2466 kvm_queue_exception(vcpu, vec);
2467 return 1;
2468 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469 return 0;
2470}
2471
2472static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2473{
Avi Kivity1155f762007-11-22 11:30:47 +02002474 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002475 u32 intr_info, error_code;
2476 unsigned long cr2, rip;
2477 u32 vect_info;
2478 enum emulation_result er;
2479
Avi Kivity1155f762007-11-22 11:30:47 +02002480 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002481 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2482
2483 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002484 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002485 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002486 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002487
Eddie Dong85f455f2007-07-06 12:20:49 +03002488 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002489 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002490 set_bit(irq, vcpu->arch.irq_pending);
2491 set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492 }
2493
Avi Kivity1b6269d2007-10-09 12:12:19 +02002494 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
2495 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002496
2497 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002498 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002499 return 1;
2500 }
2501
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002502 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08002503 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002504 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002505 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002506 return 1;
2507 }
2508
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002510 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002511 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002512 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2513 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002514 /* EPT won't cause page fault directly */
2515 if (vm_need_ept())
2516 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002518 KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2,
2519 (u32)((u64)cr2 >> 32), handler);
Avi Kivityf7d92382008-07-03 16:14:28 +03002520 if (vcpu->arch.interrupt.pending || vcpu->arch.exception.pending)
Avi Kivity577bdc42008-07-19 08:57:05 +03002521 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002522 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523 }
2524
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002525 if (vcpu->arch.rmode.active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002526 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002527 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002528 if (vcpu->arch.halt_request) {
2529 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002530 return kvm_emulate_halt(vcpu);
2531 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002533 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534
Mike Dayd77c26f2007-10-08 09:02:08 -04002535 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) ==
2536 (INTR_TYPE_EXCEPTION | 1)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2538 return 0;
2539 }
2540 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2541 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
2542 kvm_run->ex.error_code = error_code;
2543 return 0;
2544}
2545
2546static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2547 struct kvm_run *kvm_run)
2548{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002549 ++vcpu->stat.irq_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002550 KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002551 return 1;
2552}
2553
Avi Kivity988ad742007-02-12 00:54:36 -08002554static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2555{
2556 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2557 return 0;
2558}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559
Avi Kivity6aa8b732006-12-10 02:21:36 -08002560static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2561{
He, Qingbfdaab02007-09-12 14:18:28 +08002562 unsigned long exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02002563 int size, down, in, string, rep;
2564 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002565
Avi Kivity1165f5f2007-04-19 17:27:43 +03002566 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002567 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002568 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002569
2570 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002571 if (emulate_instruction(vcpu,
2572 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002573 return 0;
2574 return 1;
2575 }
2576
2577 size = (exit_qualification & 7) + 1;
2578 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002579 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002580 rep = (exit_qualification & 32) != 0;
2581 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002582
Laurent Vivier3090dd72007-08-05 10:43:32 +03002583 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584}
2585
Ingo Molnar102d8322007-02-19 14:37:47 +02002586static void
2587vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2588{
2589 /*
2590 * Patch in the VMCALL instruction:
2591 */
2592 hypercall[0] = 0x0f;
2593 hypercall[1] = 0x01;
2594 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002595}
2596
Avi Kivity6aa8b732006-12-10 02:21:36 -08002597static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2598{
He, Qingbfdaab02007-09-12 14:18:28 +08002599 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002600 int cr;
2601 int reg;
2602
He, Qingbfdaab02007-09-12 14:18:28 +08002603 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002604 cr = exit_qualification & 15;
2605 reg = (exit_qualification >> 8) & 15;
2606 switch ((exit_qualification >> 4) & 3) {
2607 case 0: /* mov to cr */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002608 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr,
2609 (u32)kvm_register_read(vcpu, reg),
2610 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
2611 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002612 switch (cr) {
2613 case 0:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002614 kvm_set_cr0(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002615 skip_emulated_instruction(vcpu);
2616 return 1;
2617 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002618 kvm_set_cr3(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002619 skip_emulated_instruction(vcpu);
2620 return 1;
2621 case 4:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002622 kvm_set_cr4(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623 skip_emulated_instruction(vcpu);
2624 return 1;
2625 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002626 kvm_set_cr8(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002627 skip_emulated_instruction(vcpu);
Avi Kivitye5314062007-12-06 16:32:45 +02002628 if (irqchip_in_kernel(vcpu->kvm))
2629 return 1;
Yang, Sheng253abde2007-08-16 13:01:00 +03002630 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2631 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632 };
2633 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002634 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002635 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002636 vcpu->arch.cr0 &= ~X86_CR0_TS;
2637 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002638 vmx_fpu_activate(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002639 KVMTRACE_0D(CLTS, vcpu, handler);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002640 skip_emulated_instruction(vcpu);
2641 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642 case 1: /*mov from cr*/
2643 switch (cr) {
2644 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002645 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002646 KVMTRACE_3D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002647 (u32)kvm_register_read(vcpu, reg),
2648 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002649 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650 skip_emulated_instruction(vcpu);
2651 return 1;
2652 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002653 kvm_register_write(vcpu, reg, kvm_get_cr8(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002654 KVMTRACE_2D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002655 (u32)kvm_register_read(vcpu, reg), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002656 skip_emulated_instruction(vcpu);
2657 return 1;
2658 }
2659 break;
2660 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002661 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662
2663 skip_emulated_instruction(vcpu);
2664 return 1;
2665 default:
2666 break;
2667 }
2668 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002669 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002670 (int)(exit_qualification >> 4) & 3, cr);
2671 return 0;
2672}
2673
2674static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2675{
He, Qingbfdaab02007-09-12 14:18:28 +08002676 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677 unsigned long val;
2678 int dr, reg;
2679
2680 /*
2681 * FIXME: this code assumes the host is debugging the guest.
2682 * need to deal with guest debugging itself too.
2683 */
He, Qingbfdaab02007-09-12 14:18:28 +08002684 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685 dr = exit_qualification & 7;
2686 reg = (exit_qualification >> 8) & 15;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687 if (exit_qualification & 16) {
2688 /* mov from dr */
2689 switch (dr) {
2690 case 6:
2691 val = 0xffff0ff0;
2692 break;
2693 case 7:
2694 val = 0x400;
2695 break;
2696 default:
2697 val = 0;
2698 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002699 kvm_register_write(vcpu, reg, val);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002700 KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002701 } else {
2702 /* mov to dr */
2703 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704 skip_emulated_instruction(vcpu);
2705 return 1;
2706}
2707
2708static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2709{
Avi Kivity06465c52007-02-28 20:46:53 +02002710 kvm_emulate_cpuid(vcpu);
2711 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712}
2713
2714static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2715{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002716 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002717 u64 data;
2718
2719 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002720 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721 return 1;
2722 }
2723
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002724 KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32),
2725 handler);
2726
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002728 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
2729 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002730 skip_emulated_instruction(vcpu);
2731 return 1;
2732}
2733
2734static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2735{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002736 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
2737 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
2738 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002740 KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32),
2741 handler);
2742
Avi Kivity6aa8b732006-12-10 02:21:36 -08002743 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002744 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002745 return 1;
2746 }
2747
2748 skip_emulated_instruction(vcpu);
2749 return 1;
2750}
2751
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002752static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2753 struct kvm_run *kvm_run)
2754{
2755 return 1;
2756}
2757
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2759 struct kvm_run *kvm_run)
2760{
Eddie Dong85f455f2007-07-06 12:20:49 +03002761 u32 cpu_based_vm_exec_control;
2762
2763 /* clear pending irq */
2764 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2765 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2766 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002767
2768 KVMTRACE_0D(PEND_INTR, vcpu, handler);
2769
Dor Laorc1150d82007-01-05 16:36:24 -08002770 /*
2771 * If the user space waits to inject interrupts, exit as soon as
2772 * possible
2773 */
2774 if (kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002775 !vcpu->arch.irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002776 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002777 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002778 return 0;
2779 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002780 return 1;
2781}
2782
2783static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2784{
2785 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002786 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002787}
2788
Ingo Molnarc21415e2007-02-19 14:37:47 +02002789static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2790{
Dor Laor510043d2007-02-19 18:25:43 +02002791 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002792 kvm_emulate_hypercall(vcpu);
2793 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02002794}
2795
Eddie Donge5edaa02007-11-11 12:28:35 +02002796static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2797{
2798 skip_emulated_instruction(vcpu);
2799 /* TODO: Add support for VT-d/pass-through device */
2800 return 1;
2801}
2802
Sheng Yangf78e0e22007-10-29 09:40:42 +08002803static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2804{
2805 u64 exit_qualification;
2806 enum emulation_result er;
2807 unsigned long offset;
2808
2809 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2810 offset = exit_qualification & 0xffful;
2811
2812 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
2813
2814 if (er != EMULATE_DONE) {
2815 printk(KERN_ERR
2816 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
2817 offset);
2818 return -ENOTSUPP;
2819 }
2820 return 1;
2821}
2822
Izik Eidus37817f22008-03-24 23:14:53 +02002823static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2824{
2825 unsigned long exit_qualification;
2826 u16 tss_selector;
2827 int reason;
2828
2829 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
2830
2831 reason = (u32)exit_qualification >> 30;
2832 tss_selector = exit_qualification;
2833
2834 return kvm_task_switch(vcpu, tss_selector, reason);
2835}
2836
Sheng Yang14394422008-04-28 12:24:45 +08002837static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2838{
2839 u64 exit_qualification;
2840 enum emulation_result er;
2841 gpa_t gpa;
2842 unsigned long hva;
2843 int gla_validity;
2844 int r;
2845
2846 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2847
2848 if (exit_qualification & (1 << 6)) {
2849 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
2850 return -ENOTSUPP;
2851 }
2852
2853 gla_validity = (exit_qualification >> 7) & 0x3;
2854 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
2855 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
2856 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
2857 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
2858 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
2859 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
2860 (long unsigned int)exit_qualification);
2861 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2862 kvm_run->hw.hardware_exit_reason = 0;
2863 return -ENOTSUPP;
2864 }
2865
2866 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
2867 hva = gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT);
2868 if (!kvm_is_error_hva(hva)) {
2869 r = kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
2870 if (r < 0) {
2871 printk(KERN_ERR "EPT: Not enough memory!\n");
2872 return -ENOMEM;
2873 }
2874 return 1;
2875 } else {
2876 /* must be MMIO */
2877 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
2878
2879 if (er == EMULATE_FAIL) {
2880 printk(KERN_ERR
2881 "EPT: Fail to handle EPT violation vmexit!er is %d\n",
2882 er);
2883 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
2884 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
2885 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
2886 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
2887 (long unsigned int)exit_qualification);
2888 return -ENOTSUPP;
2889 } else if (er == EMULATE_DO_MMIO)
2890 return 0;
2891 }
2892 return 1;
2893}
2894
Sheng Yangf08864b2008-05-15 18:23:25 +08002895static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2896{
2897 u32 cpu_based_vm_exec_control;
2898
2899 /* clear pending NMI */
2900 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2901 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
2902 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2903 ++vcpu->stat.nmi_window_exits;
2904
2905 return 1;
2906}
2907
Mohammed Gamalea953ef2008-08-17 16:47:05 +03002908static void handle_invalid_guest_state(struct kvm_vcpu *vcpu,
2909 struct kvm_run *kvm_run)
2910{
2911 struct vcpu_vmx *vmx = to_vmx(vcpu);
2912 int err;
2913
2914 preempt_enable();
2915 local_irq_enable();
2916
2917 while (!guest_state_valid(vcpu)) {
2918 err = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
2919
2920 switch (err) {
2921 case EMULATE_DONE:
2922 break;
2923 case EMULATE_DO_MMIO:
2924 kvm_report_emulation_failure(vcpu, "mmio");
2925 /* TODO: Handle MMIO */
2926 return;
2927 default:
2928 kvm_report_emulation_failure(vcpu, "emulation failure");
2929 return;
2930 }
2931
2932 if (signal_pending(current))
2933 break;
2934 if (need_resched())
2935 schedule();
2936 }
2937
2938 local_irq_disable();
2939 preempt_disable();
2940
2941 /* Guest state should be valid now, no more emulation should be needed */
2942 vmx->emulation_required = 0;
2943}
2944
Avi Kivity6aa8b732006-12-10 02:21:36 -08002945/*
2946 * The exit handlers return 1 if the exit was handled fully and guest execution
2947 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2948 * to be done to userspace and return 0.
2949 */
2950static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
2951 struct kvm_run *kvm_run) = {
2952 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
2953 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08002954 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08002955 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002956 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002957 [EXIT_REASON_CR_ACCESS] = handle_cr,
2958 [EXIT_REASON_DR_ACCESS] = handle_dr,
2959 [EXIT_REASON_CPUID] = handle_cpuid,
2960 [EXIT_REASON_MSR_READ] = handle_rdmsr,
2961 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
2962 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
2963 [EXIT_REASON_HLT] = handle_halt,
Ingo Molnarc21415e2007-02-19 14:37:47 +02002964 [EXIT_REASON_VMCALL] = handle_vmcall,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002965 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
2966 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02002967 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02002968 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Sheng Yang14394422008-04-28 12:24:45 +08002969 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002970};
2971
2972static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04002973 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002974
2975/*
2976 * The guest has exited. See if we can fix it or if we need userspace
2977 * assistance.
2978 */
2979static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2980{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002981 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity29bd8a72007-09-10 17:27:03 +03002982 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1155f762007-11-22 11:30:47 +02002983 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03002984
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002985 KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)kvm_rip_read(vcpu),
2986 (u32)((u64)kvm_rip_read(vcpu) >> 32), entryexit);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002987
Sheng Yang14394422008-04-28 12:24:45 +08002988 /* Access CR3 don't cause VMExit in paging mode, so we need
2989 * to sync with guest real CR3. */
2990 if (vm_need_ept() && is_paging(vcpu)) {
2991 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2992 ept_load_pdptrs(vcpu);
2993 }
2994
Avi Kivity29bd8a72007-09-10 17:27:03 +03002995 if (unlikely(vmx->fail)) {
2996 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2997 kvm_run->fail_entry.hardware_entry_failure_reason
2998 = vmcs_read32(VM_INSTRUCTION_ERROR);
2999 return 0;
3000 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003001
Mike Dayd77c26f2007-10-08 09:02:08 -04003002 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003003 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
3004 exit_reason != EXIT_REASON_EPT_VIOLATION))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003005 printk(KERN_WARNING "%s: unexpected, valid vectoring info and "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003006 "exit reason is 0x%x\n", __func__, exit_reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003007 if (exit_reason < kvm_vmx_max_exit_handlers
3008 && kvm_vmx_exit_handlers[exit_reason])
3009 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
3010 else {
3011 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3012 kvm_run->hw.hardware_exit_reason = exit_reason;
3013 }
3014 return 0;
3015}
3016
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003017static void update_tpr_threshold(struct kvm_vcpu *vcpu)
3018{
3019 int max_irr, tpr;
3020
3021 if (!vm_need_tpr_shadow(vcpu->kvm))
3022 return;
3023
3024 if (!kvm_lapic_enabled(vcpu) ||
3025 ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
3026 vmcs_write32(TPR_THRESHOLD, 0);
3027 return;
3028 }
3029
3030 tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
3031 vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
3032}
3033
Eddie Dong85f455f2007-07-06 12:20:49 +03003034static void enable_irq_window(struct kvm_vcpu *vcpu)
3035{
3036 u32 cpu_based_vm_exec_control;
3037
3038 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3039 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
3040 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3041}
3042
Sheng Yangf08864b2008-05-15 18:23:25 +08003043static void enable_nmi_window(struct kvm_vcpu *vcpu)
3044{
3045 u32 cpu_based_vm_exec_control;
3046
3047 if (!cpu_has_virtual_nmis())
3048 return;
3049
3050 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3051 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
3052 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3053}
3054
3055static int vmx_nmi_enabled(struct kvm_vcpu *vcpu)
3056{
3057 u32 guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3058 return !(guest_intr & (GUEST_INTR_STATE_NMI |
3059 GUEST_INTR_STATE_MOV_SS |
3060 GUEST_INTR_STATE_STI));
3061}
3062
3063static int vmx_irq_enabled(struct kvm_vcpu *vcpu)
3064{
3065 u32 guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3066 return (!(guest_intr & (GUEST_INTR_STATE_MOV_SS |
3067 GUEST_INTR_STATE_STI)) &&
3068 (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF));
3069}
3070
3071static void enable_intr_window(struct kvm_vcpu *vcpu)
3072{
3073 if (vcpu->arch.nmi_pending)
3074 enable_nmi_window(vcpu);
3075 else if (kvm_cpu_has_interrupt(vcpu))
3076 enable_irq_window(vcpu);
3077}
3078
Avi Kivitycf393f72008-07-01 16:20:21 +03003079static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3080{
3081 u32 exit_intr_info;
Avi Kivity668f6122008-07-02 09:28:55 +03003082 u32 idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003083 bool unblock_nmi;
3084 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003085 int type;
3086 bool idtv_info_valid;
Avi Kivity35920a32008-07-03 14:50:12 +03003087 u32 error;
Avi Kivitycf393f72008-07-01 16:20:21 +03003088
3089 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
3090 if (cpu_has_virtual_nmis()) {
3091 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3092 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3093 /*
3094 * SDM 3: 25.7.1.2
3095 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3096 * a guest IRET fault.
3097 */
3098 if (unblock_nmi && vector != DF_VECTOR)
3099 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3100 GUEST_INTR_STATE_NMI);
3101 }
Avi Kivity668f6122008-07-02 09:28:55 +03003102
3103 idt_vectoring_info = vmx->idt_vectoring_info;
3104 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3105 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3106 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
3107 if (vmx->vcpu.arch.nmi_injected) {
3108 /*
3109 * SDM 3: 25.7.1.2
3110 * Clear bit "block by NMI" before VM entry if a NMI delivery
3111 * faulted.
3112 */
3113 if (idtv_info_valid && type == INTR_TYPE_NMI_INTR)
3114 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3115 GUEST_INTR_STATE_NMI);
3116 else
3117 vmx->vcpu.arch.nmi_injected = false;
3118 }
Avi Kivity35920a32008-07-03 14:50:12 +03003119 kvm_clear_exception_queue(&vmx->vcpu);
3120 if (idtv_info_valid && type == INTR_TYPE_EXCEPTION) {
3121 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
3122 error = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3123 kvm_queue_exception_e(&vmx->vcpu, vector, error);
3124 } else
3125 kvm_queue_exception(&vmx->vcpu, vector);
3126 vmx->idt_vectoring_info = 0;
3127 }
Avi Kivityf7d92382008-07-03 16:14:28 +03003128 kvm_clear_interrupt_queue(&vmx->vcpu);
3129 if (idtv_info_valid && type == INTR_TYPE_EXT_INTR) {
3130 kvm_queue_interrupt(&vmx->vcpu, vector);
3131 vmx->idt_vectoring_info = 0;
3132 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003133}
3134
Eddie Dong85f455f2007-07-06 12:20:49 +03003135static void vmx_intr_assist(struct kvm_vcpu *vcpu)
3136{
Avi Kivityf7d92382008-07-03 16:14:28 +03003137 u32 intr_info_field;
Eddie Dong85f455f2007-07-06 12:20:49 +03003138
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003139 update_tpr_threshold(vcpu);
3140
Eddie Dong85f455f2007-07-06 12:20:49 +03003141 intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
Sheng Yangf08864b2008-05-15 18:23:25 +08003142 if (cpu_has_virtual_nmis()) {
Avi Kivity668f6122008-07-02 09:28:55 +03003143 if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
3144 if (vmx_nmi_enabled(vcpu)) {
3145 vcpu->arch.nmi_pending = false;
3146 vcpu->arch.nmi_injected = true;
3147 } else {
3148 enable_intr_window(vcpu);
3149 return;
3150 }
3151 }
3152 if (vcpu->arch.nmi_injected) {
3153 vmx_inject_nmi(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003154 enable_intr_window(vcpu);
3155 return;
3156 }
Sheng Yangf08864b2008-05-15 18:23:25 +08003157 }
Avi Kivityf7d92382008-07-03 16:14:28 +03003158 if (!vcpu->arch.interrupt.pending && kvm_cpu_has_interrupt(vcpu)) {
3159 if (vmx_irq_enabled(vcpu))
3160 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu));
3161 else
3162 enable_irq_window(vcpu);
3163 }
3164 if (vcpu->arch.interrupt.pending) {
3165 vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
3166 kvm_timer_intr_post(vcpu, vcpu->arch.interrupt.nr);
3167 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003168}
3169
Avi Kivity9c8cba32007-11-22 11:42:59 +02003170/*
3171 * Failure to inject an interrupt should give us the information
3172 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3173 * when fetching the interrupt redirection bitmap in the real-mode
3174 * tss, this doesn't happen. So we do it ourselves.
3175 */
3176static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3177{
3178 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003179 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003180 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003181 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003182 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3183 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3184 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3185 return;
3186 }
3187 vmx->idt_vectoring_info =
3188 VECTORING_INFO_VALID_MASK
3189 | INTR_TYPE_EXT_INTR
3190 | vmx->rmode.irq.vector;
3191}
3192
Avi Kivityc8019492008-07-14 14:44:59 +03003193#ifdef CONFIG_X86_64
3194#define R "r"
3195#define Q "q"
3196#else
3197#define R "e"
3198#define Q "l"
3199#endif
3200
Avi Kivity04d2cc72007-09-10 18:10:54 +03003201static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003202{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003203 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1b6269d2007-10-09 12:12:19 +02003204 u32 intr_info;
Avi Kivitye6adf282007-04-30 16:07:54 +03003205
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003206 /* Handle invalid guest state instead of entering VMX */
3207 if (vmx->emulation_required && emulate_invalid_guest_state) {
3208 handle_invalid_guest_state(vcpu, kvm_run);
3209 return;
3210 }
3211
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003212 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3213 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3214 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3215 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3216
Avi Kivitye6adf282007-04-30 16:07:54 +03003217 /*
3218 * Loading guest fpu may have cleared host cr0.ts
3219 */
3220 vmcs_writel(HOST_CR0, read_cr0());
3221
Mike Dayd77c26f2007-10-08 09:02:08 -04003222 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003223 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003224 "push %%"R"dx; push %%"R"bp;"
3225 "push %%"R"cx \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03003226 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3227 "je 1f \n\t"
3228 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003229 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03003230 "1: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003231 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003232 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003233 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003234 "mov %c[cr2](%0), %%"R"ax \n\t"
3235 "mov %%"R"ax, %%cr2 \n\t"
3236 "mov %c[rax](%0), %%"R"ax \n\t"
3237 "mov %c[rbx](%0), %%"R"bx \n\t"
3238 "mov %c[rdx](%0), %%"R"dx \n\t"
3239 "mov %c[rsi](%0), %%"R"si \n\t"
3240 "mov %c[rdi](%0), %%"R"di \n\t"
3241 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003242#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003243 "mov %c[r8](%0), %%r8 \n\t"
3244 "mov %c[r9](%0), %%r9 \n\t"
3245 "mov %c[r10](%0), %%r10 \n\t"
3246 "mov %c[r11](%0), %%r11 \n\t"
3247 "mov %c[r12](%0), %%r12 \n\t"
3248 "mov %c[r13](%0), %%r13 \n\t"
3249 "mov %c[r14](%0), %%r14 \n\t"
3250 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003251#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003252 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3253
Avi Kivity6aa8b732006-12-10 02:21:36 -08003254 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003255 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003256 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003257 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003258 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003259 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003260 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003261 "xchg %0, (%%"R"sp) \n\t"
3262 "mov %%"R"ax, %c[rax](%0) \n\t"
3263 "mov %%"R"bx, %c[rbx](%0) \n\t"
3264 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3265 "mov %%"R"dx, %c[rdx](%0) \n\t"
3266 "mov %%"R"si, %c[rsi](%0) \n\t"
3267 "mov %%"R"di, %c[rdi](%0) \n\t"
3268 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003269#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003270 "mov %%r8, %c[r8](%0) \n\t"
3271 "mov %%r9, %c[r9](%0) \n\t"
3272 "mov %%r10, %c[r10](%0) \n\t"
3273 "mov %%r11, %c[r11](%0) \n\t"
3274 "mov %%r12, %c[r12](%0) \n\t"
3275 "mov %%r13, %c[r13](%0) \n\t"
3276 "mov %%r14, %c[r14](%0) \n\t"
3277 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003278#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003279 "mov %%cr2, %%"R"ax \n\t"
3280 "mov %%"R"ax, %c[cr2](%0) \n\t"
3281
3282 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003283 "setbe %c[fail](%0) \n\t"
3284 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3285 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3286 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03003287 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003288 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3289 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3290 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3291 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3292 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3293 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3294 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003295#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003296 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3297 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3298 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3299 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3300 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3301 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3302 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3303 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003304#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003305 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003306 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003307 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003308#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003309 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3310#endif
3311 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003312
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003313 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
3314 vcpu->arch.regs_dirty = 0;
3315
Avi Kivity1155f762007-11-22 11:30:47 +02003316 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003317 if (vmx->rmode.irq.pending)
3318 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003319
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003320 vcpu->arch.interrupt_window_open =
Sheng Yangf08864b2008-05-15 18:23:25 +08003321 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
3322 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)) == 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003323
Mike Dayd77c26f2007-10-08 09:02:08 -04003324 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003325 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003326
3327 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
3328
3329 /* We need to handle NMIs before interrupts are enabled */
Sheng Yangf08864b2008-05-15 18:23:25 +08003330 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200 &&
3331 (intr_info & INTR_INFO_VALID_MASK)) {
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003332 KVMTRACE_0D(NMI, vcpu, handler);
Avi Kivity1b6269d2007-10-09 12:12:19 +02003333 asm("int $2");
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003334 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003335
3336 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003337}
3338
Avi Kivityc8019492008-07-14 14:44:59 +03003339#undef R
3340#undef Q
3341
Avi Kivity6aa8b732006-12-10 02:21:36 -08003342static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3343{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003344 struct vcpu_vmx *vmx = to_vmx(vcpu);
3345
3346 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003347 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003348 free_vmcs(vmx->vmcs);
3349 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003350 }
3351}
3352
3353static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3354{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003355 struct vcpu_vmx *vmx = to_vmx(vcpu);
3356
Sheng Yang2384d2b2008-01-17 15:14:33 +08003357 spin_lock(&vmx_vpid_lock);
3358 if (vmx->vpid != 0)
3359 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3360 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003361 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003362 kfree(vmx->host_msrs);
3363 kfree(vmx->guest_msrs);
3364 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003365 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003366}
3367
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003368static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003369{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003370 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003371 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003372 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003373
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003374 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003375 return ERR_PTR(-ENOMEM);
3376
Sheng Yang2384d2b2008-01-17 15:14:33 +08003377 allocate_vpid(vmx);
3378
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003379 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3380 if (err)
3381 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003382
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003383 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003384 if (!vmx->guest_msrs) {
3385 err = -ENOMEM;
3386 goto uninit_vcpu;
3387 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003388
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003389 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3390 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003391 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003392
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003393 vmx->vmcs = alloc_vmcs();
3394 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003395 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003396
3397 vmcs_clear(vmx->vmcs);
3398
Avi Kivity15ad7142007-07-11 18:17:21 +03003399 cpu = get_cpu();
3400 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003401 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003402 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003403 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003404 if (err)
3405 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003406 if (vm_need_virtualize_apic_accesses(kvm))
3407 if (alloc_apic_access_page(kvm) != 0)
3408 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003409
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003410 if (vm_need_ept())
3411 if (alloc_identity_pagetable(kvm) != 0)
3412 goto free_vmcs;
3413
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003414 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003415
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003416free_vmcs:
3417 free_vmcs(vmx->vmcs);
3418free_msrs:
3419 kfree(vmx->host_msrs);
3420free_guest_msrs:
3421 kfree(vmx->guest_msrs);
3422uninit_vcpu:
3423 kvm_vcpu_uninit(&vmx->vcpu);
3424free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003425 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003426 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003427}
3428
Yang, Sheng002c7f72007-07-31 14:23:01 +03003429static void __init vmx_check_processor_compat(void *rtn)
3430{
3431 struct vmcs_config vmcs_conf;
3432
3433 *(int *)rtn = 0;
3434 if (setup_vmcs_config(&vmcs_conf) < 0)
3435 *(int *)rtn = -EIO;
3436 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3437 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3438 smp_processor_id());
3439 *(int *)rtn = -EIO;
3440 }
3441}
3442
Sheng Yang67253af2008-04-25 10:20:22 +08003443static int get_ept_level(void)
3444{
3445 return VMX_EPT_DEFAULT_GAW + 1;
3446}
3447
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003448static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003449 .cpu_has_kvm_support = cpu_has_kvm_support,
3450 .disabled_by_bios = vmx_disabled_by_bios,
3451 .hardware_setup = hardware_setup,
3452 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003453 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003454 .hardware_enable = hardware_enable,
3455 .hardware_disable = hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003456 .cpu_has_accelerated_tpr = cpu_has_vmx_virtualize_apic_accesses,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003457
3458 .vcpu_create = vmx_create_vcpu,
3459 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003460 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003461
Avi Kivity04d2cc72007-09-10 18:10:54 +03003462 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003463 .vcpu_load = vmx_vcpu_load,
3464 .vcpu_put = vmx_vcpu_put,
3465
3466 .set_guest_debug = set_guest_debug,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003467 .guest_debug_pre = kvm_guest_debug_pre,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003468 .get_msr = vmx_get_msr,
3469 .set_msr = vmx_set_msr,
3470 .get_segment_base = vmx_get_segment_base,
3471 .get_segment = vmx_get_segment,
3472 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003473 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003474 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003475 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003476 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003477 .set_cr3 = vmx_set_cr3,
3478 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003479 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003480 .get_idt = vmx_get_idt,
3481 .set_idt = vmx_set_idt,
3482 .get_gdt = vmx_get_gdt,
3483 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003484 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003485 .get_rflags = vmx_get_rflags,
3486 .set_rflags = vmx_set_rflags,
3487
3488 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003489
Avi Kivity6aa8b732006-12-10 02:21:36 -08003490 .run = vmx_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003491 .handle_exit = kvm_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02003493 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003494 .get_irq = vmx_get_irq,
3495 .set_irq = vmx_inject_irq,
Avi Kivity298101d2007-11-25 13:41:11 +02003496 .queue_exception = vmx_queue_exception,
3497 .exception_injected = vmx_exception_injected,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003498 .inject_pending_irq = vmx_intr_assist,
3499 .inject_pending_vectors = do_interrupt_requests,
Izik Eiduscbc94022007-10-25 00:29:55 +02003500
3501 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003502 .get_tdp_level = get_ept_level,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003503};
3504
3505static int __init vmx_init(void)
3506{
Sheng Yang25c5f222008-03-28 13:18:56 +08003507 void *va;
He, Qingfdef3ad2007-04-30 09:45:24 +03003508 int r;
3509
3510 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3511 if (!vmx_io_bitmap_a)
3512 return -ENOMEM;
3513
3514 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3515 if (!vmx_io_bitmap_b) {
3516 r = -ENOMEM;
3517 goto out;
3518 }
3519
Sheng Yang25c5f222008-03-28 13:18:56 +08003520 vmx_msr_bitmap = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3521 if (!vmx_msr_bitmap) {
3522 r = -ENOMEM;
3523 goto out1;
3524 }
3525
He, Qingfdef3ad2007-04-30 09:45:24 +03003526 /*
3527 * Allow direct access to the PC debug port (it is often used for I/O
3528 * delays, but the vmexits simply slow things down).
3529 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003530 va = kmap(vmx_io_bitmap_a);
3531 memset(va, 0xff, PAGE_SIZE);
3532 clear_bit(0x80, va);
Avi Kivitycd0536d2007-05-08 11:34:07 +03003533 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003534
Sheng Yang25c5f222008-03-28 13:18:56 +08003535 va = kmap(vmx_io_bitmap_b);
3536 memset(va, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03003537 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03003538
Sheng Yang25c5f222008-03-28 13:18:56 +08003539 va = kmap(vmx_msr_bitmap);
3540 memset(va, 0xff, PAGE_SIZE);
3541 kunmap(vmx_msr_bitmap);
3542
Sheng Yang2384d2b2008-01-17 15:14:33 +08003543 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
3544
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003545 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003546 if (r)
Sheng Yang25c5f222008-03-28 13:18:56 +08003547 goto out2;
3548
3549 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_FS_BASE);
3550 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_GS_BASE);
3551 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_CS);
3552 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_ESP);
3553 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_EIP);
He, Qingfdef3ad2007-04-30 09:45:24 +03003554
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003555 if (vm_need_ept()) {
Sheng Yang14394422008-04-28 12:24:45 +08003556 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003557 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
3558 VMX_EPT_WRITABLE_MASK |
3559 VMX_EPT_DEFAULT_MT << VMX_EPT_MT_EPTE_SHIFT);
Sheng Yang534e38b2008-09-08 15:12:30 +08003560 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003561 VMX_EPT_EXECUTABLE_MASK);
3562 kvm_enable_tdp();
3563 } else
3564 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08003565
Avi Kivityc7addb92007-09-16 18:58:32 +02003566 if (bypass_guest_pf)
3567 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
3568
Sheng Yang14394422008-04-28 12:24:45 +08003569 ept_sync_global();
3570
He, Qingfdef3ad2007-04-30 09:45:24 +03003571 return 0;
3572
Sheng Yang25c5f222008-03-28 13:18:56 +08003573out2:
3574 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03003575out1:
3576 __free_page(vmx_io_bitmap_b);
3577out:
3578 __free_page(vmx_io_bitmap_a);
3579 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003580}
3581
3582static void __exit vmx_exit(void)
3583{
Sheng Yang25c5f222008-03-28 13:18:56 +08003584 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03003585 __free_page(vmx_io_bitmap_b);
3586 __free_page(vmx_io_bitmap_a);
3587
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003588 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003589}
3590
3591module_init(vmx_init)
3592module_exit(vmx_exit)