blob: fc8996b1043bab5836d76296d86e63bab4932ee9 [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
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040052struct vmcs {
53 u32 revision_id;
54 u32 abort;
55 char data[0];
56};
57
58struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100059 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +030060 struct list_head local_vcpus_link;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040061 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030062 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020063 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040064 struct kvm_msr_entry *guest_msrs;
65 struct kvm_msr_entry *host_msrs;
66 int nmsrs;
67 int save_nmsrs;
68 int msr_offset_efer;
69#ifdef CONFIG_X86_64
70 int msr_offset_kernel_gs_base;
71#endif
72 struct vmcs *vmcs;
73 struct {
74 int loaded;
75 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020076 int gs_ldt_reload_needed;
77 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030078 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040079 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +020080 struct {
81 struct {
82 bool pending;
83 u8 vector;
84 unsigned rip;
85 } irq;
86 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +080087 int vpid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040088};
89
90static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
91{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100092 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040093}
94
Sheng Yangb7ebfb02008-04-25 21:44:52 +080095static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +080096static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +030097
Avi Kivity6aa8b732006-12-10 02:21:36 -080098static DEFINE_PER_CPU(struct vmcs *, vmxarea);
99static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300100static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101
He, Qingfdef3ad2007-04-30 09:45:24 +0300102static struct page *vmx_io_bitmap_a;
103static struct page *vmx_io_bitmap_b;
Sheng Yang25c5f222008-03-28 13:18:56 +0800104static struct page *vmx_msr_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300105
Sheng Yang2384d2b2008-01-17 15:14:33 +0800106static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
107static DEFINE_SPINLOCK(vmx_vpid_lock);
108
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300109static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110 int size;
111 int order;
112 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300113 u32 pin_based_exec_ctrl;
114 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800115 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300116 u32 vmexit_ctrl;
117 u32 vmentry_ctrl;
118} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119
Sheng Yangd56f5462008-04-25 10:13:16 +0800120struct vmx_capability {
121 u32 ept;
122 u32 vpid;
123} vmx_capability;
124
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125#define VMX_SEGMENT_FIELD(seg) \
126 [VCPU_SREG_##seg] = { \
127 .selector = GUEST_##seg##_SELECTOR, \
128 .base = GUEST_##seg##_BASE, \
129 .limit = GUEST_##seg##_LIMIT, \
130 .ar_bytes = GUEST_##seg##_AR_BYTES, \
131 }
132
133static struct kvm_vmx_segment_field {
134 unsigned selector;
135 unsigned base;
136 unsigned limit;
137 unsigned ar_bytes;
138} kvm_vmx_segment_fields[] = {
139 VMX_SEGMENT_FIELD(CS),
140 VMX_SEGMENT_FIELD(DS),
141 VMX_SEGMENT_FIELD(ES),
142 VMX_SEGMENT_FIELD(FS),
143 VMX_SEGMENT_FIELD(GS),
144 VMX_SEGMENT_FIELD(SS),
145 VMX_SEGMENT_FIELD(TR),
146 VMX_SEGMENT_FIELD(LDTR),
147};
148
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300149/*
150 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
151 * away by decrementing the array size.
152 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800153static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800154#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800155 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
156#endif
157 MSR_EFER, MSR_K6_STAR,
158};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200159#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800160
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400161static void load_msrs(struct kvm_msr_entry *e, int n)
162{
163 int i;
164
165 for (i = 0; i < n; ++i)
166 wrmsrl(e[i].index, e[i].data);
167}
168
169static void save_msrs(struct kvm_msr_entry *e, int n)
170{
171 int i;
172
173 for (i = 0; i < n; ++i)
174 rdmsrl(e[i].index, e[i].data);
175}
176
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177static inline int is_page_fault(u32 intr_info)
178{
179 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
180 INTR_INFO_VALID_MASK)) ==
181 (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
182}
183
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300184static inline int is_no_device(u32 intr_info)
185{
186 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
187 INTR_INFO_VALID_MASK)) ==
188 (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
189}
190
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500191static inline int is_invalid_opcode(u32 intr_info)
192{
193 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
194 INTR_INFO_VALID_MASK)) ==
195 (INTR_TYPE_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
196}
197
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198static inline int is_external_interrupt(u32 intr_info)
199{
200 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
201 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
202}
203
Sheng Yang25c5f222008-03-28 13:18:56 +0800204static inline int cpu_has_vmx_msr_bitmap(void)
205{
206 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS);
207}
208
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800209static inline int cpu_has_vmx_tpr_shadow(void)
210{
211 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW);
212}
213
214static inline int vm_need_tpr_shadow(struct kvm *kvm)
215{
216 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)));
217}
218
Sheng Yangf78e0e22007-10-29 09:40:42 +0800219static inline int cpu_has_secondary_exec_ctrls(void)
220{
221 return (vmcs_config.cpu_based_exec_ctrl &
222 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS);
223}
224
Avi Kivity774ead32007-12-26 13:57:04 +0200225static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800226{
Avi Kivity4c9fc8e2008-03-24 18:15:14 +0200227 return flexpriority_enabled
228 && (vmcs_config.cpu_based_2nd_exec_ctrl &
229 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800230}
231
Sheng Yangd56f5462008-04-25 10:13:16 +0800232static inline int cpu_has_vmx_invept_individual_addr(void)
233{
234 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT));
235}
236
237static inline int cpu_has_vmx_invept_context(void)
238{
239 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT));
240}
241
242static inline int cpu_has_vmx_invept_global(void)
243{
244 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT));
245}
246
247static inline int cpu_has_vmx_ept(void)
248{
249 return (vmcs_config.cpu_based_2nd_exec_ctrl &
250 SECONDARY_EXEC_ENABLE_EPT);
251}
252
253static inline int vm_need_ept(void)
254{
255 return (cpu_has_vmx_ept() && enable_ept);
256}
257
Sheng Yangf78e0e22007-10-29 09:40:42 +0800258static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
259{
260 return ((cpu_has_vmx_virtualize_apic_accesses()) &&
261 (irqchip_in_kernel(kvm)));
262}
263
Sheng Yang2384d2b2008-01-17 15:14:33 +0800264static inline int cpu_has_vmx_vpid(void)
265{
266 return (vmcs_config.cpu_based_2nd_exec_ctrl &
267 SECONDARY_EXEC_ENABLE_VPID);
268}
269
Sheng Yangf08864b2008-05-15 18:23:25 +0800270static inline int cpu_has_virtual_nmis(void)
271{
272 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
273}
274
Rusty Russell8b9cf982007-07-30 16:31:43 +1000275static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800276{
277 int i;
278
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400279 for (i = 0; i < vmx->nmsrs; ++i)
280 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300281 return i;
282 return -1;
283}
284
Sheng Yang2384d2b2008-01-17 15:14:33 +0800285static inline void __invvpid(int ext, u16 vpid, gva_t gva)
286{
287 struct {
288 u64 vpid : 16;
289 u64 rsvd : 48;
290 u64 gva;
291 } operand = { vpid, 0, gva };
292
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300293 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800294 /* CF==1 or ZF==1 --> rc = -1 */
295 "; ja 1f ; ud2 ; 1:"
296 : : "a"(&operand), "c"(ext) : "cc", "memory");
297}
298
Sheng Yang14394422008-04-28 12:24:45 +0800299static inline void __invept(int ext, u64 eptp, gpa_t gpa)
300{
301 struct {
302 u64 eptp, gpa;
303 } operand = {eptp, gpa};
304
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300305 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800306 /* CF==1 or ZF==1 --> rc = -1 */
307 "; ja 1f ; ud2 ; 1:\n"
308 : : "a" (&operand), "c" (ext) : "cc", "memory");
309}
310
Rusty Russell8b9cf982007-07-30 16:31:43 +1000311static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300312{
313 int i;
314
Rusty Russell8b9cf982007-07-30 16:31:43 +1000315 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300316 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400317 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000318 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800319}
320
Avi Kivity6aa8b732006-12-10 02:21:36 -0800321static void vmcs_clear(struct vmcs *vmcs)
322{
323 u64 phys_addr = __pa(vmcs);
324 u8 error;
325
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300326 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
328 : "cc", "memory");
329 if (error)
330 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
331 vmcs, phys_addr);
332}
333
334static void __vcpu_clear(void *arg)
335{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000336 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800337 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338
Rusty Russell8b9cf982007-07-30 16:31:43 +1000339 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400340 vmcs_clear(vmx->vmcs);
341 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800342 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800343 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300344 list_del(&vmx->local_vcpus_link);
345 vmx->vcpu.cpu = -1;
346 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347}
348
Rusty Russell8b9cf982007-07-30 16:31:43 +1000349static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800350{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200351 if (vmx->vcpu.cpu == -1)
352 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200353 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800354}
355
Sheng Yang2384d2b2008-01-17 15:14:33 +0800356static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
357{
358 if (vmx->vpid == 0)
359 return;
360
361 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
362}
363
Sheng Yang14394422008-04-28 12:24:45 +0800364static inline void ept_sync_global(void)
365{
366 if (cpu_has_vmx_invept_global())
367 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
368}
369
370static inline void ept_sync_context(u64 eptp)
371{
372 if (vm_need_ept()) {
373 if (cpu_has_vmx_invept_context())
374 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
375 else
376 ept_sync_global();
377 }
378}
379
380static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
381{
382 if (vm_need_ept()) {
383 if (cpu_has_vmx_invept_individual_addr())
384 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
385 eptp, gpa);
386 else
387 ept_sync_context(eptp);
388 }
389}
390
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391static unsigned long vmcs_readl(unsigned long field)
392{
393 unsigned long value;
394
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300395 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800396 : "=a"(value) : "d"(field) : "cc");
397 return value;
398}
399
400static u16 vmcs_read16(unsigned long field)
401{
402 return vmcs_readl(field);
403}
404
405static u32 vmcs_read32(unsigned long field)
406{
407 return vmcs_readl(field);
408}
409
410static u64 vmcs_read64(unsigned long field)
411{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800412#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800413 return vmcs_readl(field);
414#else
415 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
416#endif
417}
418
Avi Kivitye52de1b2007-01-05 16:36:56 -0800419static noinline void vmwrite_error(unsigned long field, unsigned long value)
420{
421 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
422 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
423 dump_stack();
424}
425
Avi Kivity6aa8b732006-12-10 02:21:36 -0800426static void vmcs_writel(unsigned long field, unsigned long value)
427{
428 u8 error;
429
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300430 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400431 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800432 if (unlikely(error))
433 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800434}
435
436static void vmcs_write16(unsigned long field, u16 value)
437{
438 vmcs_writel(field, value);
439}
440
441static void vmcs_write32(unsigned long field, u32 value)
442{
443 vmcs_writel(field, value);
444}
445
446static void vmcs_write64(unsigned long field, u64 value)
447{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800448 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300449#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450 asm volatile ("");
451 vmcs_writel(field+1, value >> 32);
452#endif
453}
454
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300455static void vmcs_clear_bits(unsigned long field, u32 mask)
456{
457 vmcs_writel(field, vmcs_readl(field) & ~mask);
458}
459
460static void vmcs_set_bits(unsigned long field, u32 mask)
461{
462 vmcs_writel(field, vmcs_readl(field) | mask);
463}
464
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300465static void update_exception_bitmap(struct kvm_vcpu *vcpu)
466{
467 u32 eb;
468
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500469 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300470 if (!vcpu->fpu_active)
471 eb |= 1u << NM_VECTOR;
472 if (vcpu->guest_debug.enabled)
Jan Kiszka19bd8af2008-07-13 13:40:55 +0200473 eb |= 1u << DB_VECTOR;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800474 if (vcpu->arch.rmode.active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300475 eb = ~0;
Sheng Yang14394422008-04-28 12:24:45 +0800476 if (vm_need_ept())
477 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300478 vmcs_write32(EXCEPTION_BITMAP, eb);
479}
480
Avi Kivity33ed6322007-05-02 16:54:03 +0300481static void reload_tss(void)
482{
Avi Kivity33ed6322007-05-02 16:54:03 +0300483 /*
484 * VT restores TR but not its size. Useless.
485 */
486 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200487 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300488
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300489 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300490 descs = (void *)gdt.base;
491 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
492 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300493}
494
Rusty Russell8b9cf982007-07-30 16:31:43 +1000495static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300496{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400497 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300498 u64 host_efer = vmx->host_msrs[efer_offset].data;
499 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
500 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300501
Avi Kivity51c6cf62007-08-29 03:48:05 +0300502 if (efer_offset < 0)
503 return;
504 /*
505 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
506 * outside long mode
507 */
508 ignore_bits = EFER_NX | EFER_SCE;
509#ifdef CONFIG_X86_64
510 ignore_bits |= EFER_LMA | EFER_LME;
511 /* SCE is meaningful only in long mode on Intel */
512 if (guest_efer & EFER_LMA)
513 ignore_bits &= ~(u64)EFER_SCE;
514#endif
515 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
516 return;
517
518 vmx->host_state.guest_efer_loaded = 1;
519 guest_efer &= ~ignore_bits;
520 guest_efer |= host_efer & ignore_bits;
521 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000522 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300523}
524
Avi Kivity51c6cf62007-08-29 03:48:05 +0300525static void reload_host_efer(struct vcpu_vmx *vmx)
526{
527 if (vmx->host_state.guest_efer_loaded) {
528 vmx->host_state.guest_efer_loaded = 0;
529 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
530 }
531}
532
Avi Kivity04d2cc72007-09-10 18:10:54 +0300533static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300534{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300535 struct vcpu_vmx *vmx = to_vmx(vcpu);
536
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400537 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300538 return;
539
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400540 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300541 /*
542 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
543 * allow segment selectors with cpl > 0 or ti == 1.
544 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300545 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200546 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300547 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200548 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400549 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200550 vmx->host_state.fs_reload_needed = 0;
551 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300552 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200553 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300554 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300555 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400556 if (!(vmx->host_state.gs_sel & 7))
557 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300558 else {
559 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200560 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300561 }
562
563#ifdef CONFIG_X86_64
564 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
565 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
566#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400567 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
568 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300569#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300570
571#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400572 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400573 save_msrs(vmx->host_msrs +
574 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400575
Avi Kivity707c0872007-05-02 17:33:43 +0300576#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400577 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300578 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300579}
580
Avi Kivitya9b21b62008-06-24 11:48:49 +0300581static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300582{
Avi Kivity15ad7142007-07-11 18:17:21 +0300583 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300584
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400585 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300586 return;
587
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200588 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400589 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200590 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300591 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200592 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300593 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300594 /*
595 * If we have to reload gs, we must take care to
596 * preserve our gs base.
597 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300598 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300599 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300600#ifdef CONFIG_X86_64
601 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
602#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300603 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300604 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200605 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400606 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
607 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300608 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300609}
610
Avi Kivitya9b21b62008-06-24 11:48:49 +0300611static void vmx_load_host_state(struct vcpu_vmx *vmx)
612{
613 preempt_disable();
614 __vmx_load_host_state(vmx);
615 preempt_enable();
616}
617
Avi Kivity6aa8b732006-12-10 02:21:36 -0800618/*
619 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
620 * vcpu mutex is already taken.
621 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300622static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400624 struct vcpu_vmx *vmx = to_vmx(vcpu);
625 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200626 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627
Eddie Donga3d7f852007-09-03 16:15:12 +0300628 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000629 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300630 kvm_migrate_timers(vcpu);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800631 vpid_sync_vcpu_all(vmx);
Avi Kivity543e4242008-05-13 16:22:47 +0300632 local_irq_disable();
633 list_add(&vmx->local_vcpus_link,
634 &per_cpu(vcpus_on_cpu, cpu));
635 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300636 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400638 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639 u8 error;
640
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400641 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300642 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800643 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
644 : "cc");
645 if (error)
646 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400647 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800648 }
649
650 if (vcpu->cpu != cpu) {
651 struct descriptor_table dt;
652 unsigned long sysenter_esp;
653
654 vcpu->cpu = cpu;
655 /*
656 * Linux uses per-cpu TSS and GDT, so set these when switching
657 * processors.
658 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300659 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
660 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800661 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
662
663 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
664 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300665
666 /*
667 * Make sure the time stamp counter is monotonous.
668 */
669 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200670 if (tsc_this < vcpu->arch.host_tsc) {
671 delta = vcpu->arch.host_tsc - tsc_this;
672 new_offset = vmcs_read64(TSC_OFFSET) + delta;
673 vmcs_write64(TSC_OFFSET, new_offset);
674 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676}
677
678static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
679{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300680 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800681}
682
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300683static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
684{
685 if (vcpu->fpu_active)
686 return;
687 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000688 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800689 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92fa2007-07-17 23:19:08 +1000690 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300691 update_exception_bitmap(vcpu);
692}
693
694static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
695{
696 if (!vcpu->fpu_active)
697 return;
698 vcpu->fpu_active = 0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000699 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300700 update_exception_bitmap(vcpu);
701}
702
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
704{
705 return vmcs_readl(GUEST_RFLAGS);
706}
707
708static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
709{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800710 if (vcpu->arch.rmode.active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100711 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712 vmcs_writel(GUEST_RFLAGS, rflags);
713}
714
715static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
716{
717 unsigned long rip;
718 u32 interruptibility;
719
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300720 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300722 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723
724 /*
725 * We emulated an instruction, so temporary interrupt blocking
726 * should be removed, if set.
727 */
728 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
729 if (interruptibility & 3)
730 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
731 interruptibility & ~3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800732 vcpu->arch.interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733}
734
Avi Kivity298101d2007-11-25 13:41:11 +0200735static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
736 bool has_error_code, u32 error_code)
737{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200738 struct vcpu_vmx *vmx = to_vmx(vcpu);
739
740 if (has_error_code)
741 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
742
743 if (vcpu->arch.rmode.active) {
744 vmx->rmode.irq.pending = true;
745 vmx->rmode.irq.vector = nr;
746 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
747 if (nr == BP_VECTOR)
748 vmx->rmode.irq.rip++;
749 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
750 nr | INTR_TYPE_SOFT_INTR
751 | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0)
752 | INTR_INFO_VALID_MASK);
753 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
754 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
755 return;
756 }
757
Avi Kivity298101d2007-11-25 13:41:11 +0200758 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
759 nr | INTR_TYPE_EXCEPTION
Ryan Harper2e113842008-02-11 10:26:38 -0600760 | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0)
Avi Kivity298101d2007-11-25 13:41:11 +0200761 | INTR_INFO_VALID_MASK);
Avi Kivity298101d2007-11-25 13:41:11 +0200762}
763
764static bool vmx_exception_injected(struct kvm_vcpu *vcpu)
765{
Avi Kivity35920a32008-07-03 14:50:12 +0300766 return false;
Avi Kivity298101d2007-11-25 13:41:11 +0200767}
768
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769/*
Eddie Donga75beee2007-05-17 18:55:15 +0300770 * Swap MSR entry in host/guest MSR entry array.
771 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200772#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000773static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300774{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400775 struct kvm_msr_entry tmp;
776
777 tmp = vmx->guest_msrs[to];
778 vmx->guest_msrs[to] = vmx->guest_msrs[from];
779 vmx->guest_msrs[from] = tmp;
780 tmp = vmx->host_msrs[to];
781 vmx->host_msrs[to] = vmx->host_msrs[from];
782 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300783}
Gabriel C54e11fa2007-08-01 16:23:10 +0200784#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300785
786/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300787 * Set up the vmcs to automatically save and restore system
788 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
789 * mode, as fiddling with msrs is very expensive.
790 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000791static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300792{
Eddie Dong2cc51562007-05-21 07:28:09 +0300793 int save_nmsrs;
Avi Kivitye38aea32007-04-19 13:22:48 +0300794
Avi Kivity33f9c502008-02-27 16:06:57 +0200795 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300796 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300797#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000798 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300799 int index;
800
Rusty Russell8b9cf982007-07-30 16:31:43 +1000801 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300802 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000803 move_msr_up(vmx, index, save_nmsrs++);
804 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300805 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000806 move_msr_up(vmx, index, save_nmsrs++);
807 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300808 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000809 move_msr_up(vmx, index, save_nmsrs++);
810 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300811 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000812 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300813 /*
814 * MSR_K6_STAR is only needed on long mode guests, and only
815 * if efer.sce is enabled.
816 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000817 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800818 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000819 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300820 }
Eddie Donga75beee2007-05-17 18:55:15 +0300821#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400822 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300823
Eddie Donga75beee2007-05-17 18:55:15 +0300824#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400825 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000826 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300827#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000828 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivitye38aea32007-04-19 13:22:48 +0300829}
830
831/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832 * reads and returns guest's timestamp counter "register"
833 * guest_tsc = host_tsc + tsc_offset -- 21.3
834 */
835static u64 guest_read_tsc(void)
836{
837 u64 host_tsc, tsc_offset;
838
839 rdtscll(host_tsc);
840 tsc_offset = vmcs_read64(TSC_OFFSET);
841 return host_tsc + tsc_offset;
842}
843
844/*
845 * writes 'guest_tsc' into guest's timestamp counter "register"
846 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
847 */
848static void guest_write_tsc(u64 guest_tsc)
849{
850 u64 host_tsc;
851
852 rdtscll(host_tsc);
853 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
854}
855
Avi Kivity6aa8b732006-12-10 02:21:36 -0800856/*
857 * Reads an msr value (of 'msr_index') into 'pdata'.
858 * Returns 0 on success, non-0 otherwise.
859 * Assumes vcpu_load() was already called.
860 */
861static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
862{
863 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400864 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865
866 if (!pdata) {
867 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
868 return -EINVAL;
869 }
870
871 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800872#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873 case MSR_FS_BASE:
874 data = vmcs_readl(GUEST_FS_BASE);
875 break;
876 case MSR_GS_BASE:
877 data = vmcs_readl(GUEST_GS_BASE);
878 break;
879 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800880 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881#endif
882 case MSR_IA32_TIME_STAMP_COUNTER:
883 data = guest_read_tsc();
884 break;
885 case MSR_IA32_SYSENTER_CS:
886 data = vmcs_read32(GUEST_SYSENTER_CS);
887 break;
888 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200889 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 break;
891 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200892 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000895 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800896 if (msr) {
897 data = msr->data;
898 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800900 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901 }
902
903 *pdata = data;
904 return 0;
905}
906
907/*
908 * Writes msr value into into the appropriate "register".
909 * Returns 0 on success, non-0 otherwise.
910 * Assumes vcpu_load() was already called.
911 */
912static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
913{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400914 struct vcpu_vmx *vmx = to_vmx(vcpu);
915 struct kvm_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +0300916 int ret = 0;
917
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800919#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800920 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300921 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +0300922 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +0300923 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924 case MSR_FS_BASE:
925 vmcs_writel(GUEST_FS_BASE, data);
926 break;
927 case MSR_GS_BASE:
928 vmcs_writel(GUEST_GS_BASE, data);
929 break;
930#endif
931 case MSR_IA32_SYSENTER_CS:
932 vmcs_write32(GUEST_SYSENTER_CS, data);
933 break;
934 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200935 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 break;
937 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200938 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200940 case MSR_IA32_TIME_STAMP_COUNTER:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941 guest_write_tsc(data);
942 break;
Chris Lalancetteefa67e02008-06-20 09:51:30 +0200943 case MSR_P6_PERFCTR0:
944 case MSR_P6_PERFCTR1:
945 case MSR_P6_EVNTSEL0:
946 case MSR_P6_EVNTSEL1:
947 /*
948 * Just discard all writes to the performance counters; this
949 * should keep both older linux and windows 64-bit guests
950 * happy
951 */
952 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", msr_index, data);
953
954 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955 default:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300956 vmx_load_host_state(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000957 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800958 if (msr) {
959 msr->data = data;
960 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300962 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963 }
964
Eddie Dong2cc51562007-05-21 07:28:09 +0300965 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966}
967
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300968static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300970 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
971 switch (reg) {
972 case VCPU_REGS_RSP:
973 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
974 break;
975 case VCPU_REGS_RIP:
976 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
977 break;
978 default:
979 break;
980 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981}
982
983static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
984{
985 unsigned long dr7 = 0x400;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 int old_singlestep;
987
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 old_singlestep = vcpu->guest_debug.singlestep;
989
990 vcpu->guest_debug.enabled = dbg->enabled;
991 if (vcpu->guest_debug.enabled) {
992 int i;
993
994 dr7 |= 0x200; /* exact */
995 for (i = 0; i < 4; ++i) {
996 if (!dbg->breakpoints[i].enabled)
997 continue;
998 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
999 dr7 |= 2 << (i*2); /* global enable */
1000 dr7 |= 0 << (i*4+16); /* execution breakpoint */
1001 }
1002
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 vcpu->guest_debug.singlestep = dbg->singlestep;
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001004 } else
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005 vcpu->guest_debug.singlestep = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006
1007 if (old_singlestep && !vcpu->guest_debug.singlestep) {
1008 unsigned long flags;
1009
1010 flags = vmcs_readl(GUEST_RFLAGS);
1011 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1012 vmcs_writel(GUEST_RFLAGS, flags);
1013 }
1014
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001015 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016 vmcs_writel(GUEST_DR7, dr7);
1017
1018 return 0;
1019}
1020
Eddie Dong2a8067f2007-08-06 16:29:07 +03001021static int vmx_get_irq(struct kvm_vcpu *vcpu)
1022{
Avi Kivityf7d92382008-07-03 16:14:28 +03001023 if (!vcpu->arch.interrupt.pending)
1024 return -1;
1025 return vcpu->arch.interrupt.nr;
Eddie Dong2a8067f2007-08-06 16:29:07 +03001026}
1027
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028static __init int cpu_has_kvm_support(void)
1029{
1030 unsigned long ecx = cpuid_ecx(1);
1031 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
1032}
1033
1034static __init int vmx_disabled_by_bios(void)
1035{
1036 u64 msr;
1037
1038 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yangca60dfb2008-06-24 17:02:38 +08001039 return (msr & (IA32_FEATURE_CONTROL_LOCKED_BIT |
1040 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT))
1041 == IA32_FEATURE_CONTROL_LOCKED_BIT;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001042 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043}
1044
Avi Kivity774c47f2007-02-12 00:54:47 -08001045static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046{
1047 int cpu = raw_smp_processor_id();
1048 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1049 u64 old;
1050
Avi Kivity543e4242008-05-13 16:22:47 +03001051 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001052 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yangca60dfb2008-06-24 17:02:38 +08001053 if ((old & (IA32_FEATURE_CONTROL_LOCKED_BIT |
1054 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT))
1055 != (IA32_FEATURE_CONTROL_LOCKED_BIT |
1056 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001058 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yangca60dfb2008-06-24 17:02:38 +08001059 IA32_FEATURE_CONTROL_LOCKED_BIT |
1060 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT);
Rusty Russell66aee912007-07-17 23:34:16 +10001061 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001062 asm volatile (ASM_VMX_VMXON_RAX
1063 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064 : "memory", "cc");
1065}
1066
Avi Kivity543e4242008-05-13 16:22:47 +03001067static void vmclear_local_vcpus(void)
1068{
1069 int cpu = raw_smp_processor_id();
1070 struct vcpu_vmx *vmx, *n;
1071
1072 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1073 local_vcpus_link)
1074 __vcpu_clear(vmx);
1075}
1076
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077static void hardware_disable(void *garbage)
1078{
Avi Kivity543e4242008-05-13 16:22:47 +03001079 vmclear_local_vcpus();
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001080 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eli Collinse693d712008-06-01 20:24:40 -07001081 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082}
1083
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001084static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001085 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086{
1087 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001088 u32 ctl = ctl_min | ctl_opt;
1089
1090 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1091
1092 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1093 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1094
1095 /* Ensure minimum (required) set of control bits are supported. */
1096 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001097 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001098
1099 *result = ctl;
1100 return 0;
1101}
1102
Yang, Sheng002c7f72007-07-31 14:23:01 +03001103static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001104{
1105 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001106 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001107 u32 _pin_based_exec_control = 0;
1108 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001109 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001110 u32 _vmexit_control = 0;
1111 u32 _vmentry_control = 0;
1112
1113 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001114 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001115 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1116 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001117 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001118
1119 min = CPU_BASED_HLT_EXITING |
1120#ifdef CONFIG_X86_64
1121 CPU_BASED_CR8_LOAD_EXITING |
1122 CPU_BASED_CR8_STORE_EXITING |
1123#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001124 CPU_BASED_CR3_LOAD_EXITING |
1125 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001126 CPU_BASED_USE_IO_BITMAPS |
1127 CPU_BASED_MOV_DR_EXITING |
1128 CPU_BASED_USE_TSC_OFFSETING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001129 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001130 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001131 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001132 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1133 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001134 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001135#ifdef CONFIG_X86_64
1136 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1137 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1138 ~CPU_BASED_CR8_STORE_EXITING;
1139#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001140 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001141 min2 = 0;
1142 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001143 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001144 SECONDARY_EXEC_ENABLE_VPID |
1145 SECONDARY_EXEC_ENABLE_EPT;
1146 if (adjust_vmx_controls(min2, opt2,
1147 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001148 &_cpu_based_2nd_exec_control) < 0)
1149 return -EIO;
1150 }
1151#ifndef CONFIG_X86_64
1152 if (!(_cpu_based_2nd_exec_control &
1153 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1154 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1155#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001156 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
1157 /* CR3 accesses don't need to cause VM Exits when EPT enabled */
1158 min &= ~(CPU_BASED_CR3_LOAD_EXITING |
1159 CPU_BASED_CR3_STORE_EXITING);
1160 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1161 &_cpu_based_exec_control) < 0)
1162 return -EIO;
1163 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1164 vmx_capability.ept, vmx_capability.vpid);
1165 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001166
1167 min = 0;
1168#ifdef CONFIG_X86_64
1169 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1170#endif
1171 opt = 0;
1172 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1173 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001174 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001175
1176 min = opt = 0;
1177 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1178 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001179 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001181 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001182
1183 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1184 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001185 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001186
1187#ifdef CONFIG_X86_64
1188 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1189 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001190 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001191#endif
1192
1193 /* Require Write-Back (WB) memory type for VMCS accesses. */
1194 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001195 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001196
Yang, Sheng002c7f72007-07-31 14:23:01 +03001197 vmcs_conf->size = vmx_msr_high & 0x1fff;
1198 vmcs_conf->order = get_order(vmcs_config.size);
1199 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001200
Yang, Sheng002c7f72007-07-31 14:23:01 +03001201 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1202 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001203 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001204 vmcs_conf->vmexit_ctrl = _vmexit_control;
1205 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001206
1207 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001208}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001209
1210static struct vmcs *alloc_vmcs_cpu(int cpu)
1211{
1212 int node = cpu_to_node(cpu);
1213 struct page *pages;
1214 struct vmcs *vmcs;
1215
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001216 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001217 if (!pages)
1218 return NULL;
1219 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001220 memset(vmcs, 0, vmcs_config.size);
1221 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222 return vmcs;
1223}
1224
1225static struct vmcs *alloc_vmcs(void)
1226{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001227 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228}
1229
1230static void free_vmcs(struct vmcs *vmcs)
1231{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001232 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001233}
1234
Sam Ravnborg39959582007-06-01 00:47:13 -07001235static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236{
1237 int cpu;
1238
1239 for_each_online_cpu(cpu)
1240 free_vmcs(per_cpu(vmxarea, cpu));
1241}
1242
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243static __init int alloc_kvm_area(void)
1244{
1245 int cpu;
1246
1247 for_each_online_cpu(cpu) {
1248 struct vmcs *vmcs;
1249
1250 vmcs = alloc_vmcs_cpu(cpu);
1251 if (!vmcs) {
1252 free_kvm_area();
1253 return -ENOMEM;
1254 }
1255
1256 per_cpu(vmxarea, cpu) = vmcs;
1257 }
1258 return 0;
1259}
1260
1261static __init int hardware_setup(void)
1262{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001263 if (setup_vmcs_config(&vmcs_config) < 0)
1264 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001265
1266 if (boot_cpu_has(X86_FEATURE_NX))
1267 kvm_enable_efer_bits(EFER_NX);
1268
Avi Kivity6aa8b732006-12-10 02:21:36 -08001269 return alloc_kvm_area();
1270}
1271
1272static __exit void hardware_unsetup(void)
1273{
1274 free_kvm_area();
1275}
1276
Avi Kivity6aa8b732006-12-10 02:21:36 -08001277static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1278{
1279 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1280
Avi Kivity6af11b92007-03-19 13:18:10 +02001281 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282 vmcs_write16(sf->selector, save->selector);
1283 vmcs_writel(sf->base, save->base);
1284 vmcs_write32(sf->limit, save->limit);
1285 vmcs_write32(sf->ar_bytes, save->ar);
1286 } else {
1287 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1288 << AR_DPL_SHIFT;
1289 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1290 }
1291}
1292
1293static void enter_pmode(struct kvm_vcpu *vcpu)
1294{
1295 unsigned long flags;
1296
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001297 vcpu->arch.rmode.active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001298
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001299 vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base);
1300 vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit);
1301 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302
1303 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001304 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001305 flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001306 vmcs_writel(GUEST_RFLAGS, flags);
1307
Rusty Russell66aee912007-07-17 23:34:16 +10001308 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1309 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310
1311 update_exception_bitmap(vcpu);
1312
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001313 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1314 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1315 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1316 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317
1318 vmcs_write16(GUEST_SS_SELECTOR, 0);
1319 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1320
1321 vmcs_write16(GUEST_CS_SELECTOR,
1322 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1323 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1324}
1325
Mike Dayd77c26f2007-10-08 09:02:08 -04001326static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001328 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001329 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1330 kvm->memslots[0].npages - 3;
1331 return base_gfn << PAGE_SHIFT;
1332 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001333 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001334}
1335
1336static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1337{
1338 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1339
1340 save->selector = vmcs_read16(sf->selector);
1341 save->base = vmcs_readl(sf->base);
1342 save->limit = vmcs_read32(sf->limit);
1343 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001344 vmcs_write16(sf->selector, save->base >> 4);
1345 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346 vmcs_write32(sf->limit, 0xffff);
1347 vmcs_write32(sf->ar_bytes, 0xf3);
1348}
1349
1350static void enter_rmode(struct kvm_vcpu *vcpu)
1351{
1352 unsigned long flags;
1353
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001354 vcpu->arch.rmode.active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001356 vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1358
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001359 vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1361
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001362 vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1364
1365 flags = vmcs_readl(GUEST_RFLAGS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001366 vcpu->arch.rmode.save_iopl
1367 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001369 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370
1371 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001372 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373 update_exception_bitmap(vcpu);
1374
1375 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1376 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1377 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1378
1379 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001380 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001381 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1382 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1384
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001385 fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1386 fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1387 fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1388 fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001389
Eddie Dong8668a3c2007-10-10 14:26:45 +08001390 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001391 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392}
1393
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001394#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395
1396static void enter_lmode(struct kvm_vcpu *vcpu)
1397{
1398 u32 guest_tr_ar;
1399
1400 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1401 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1402 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001403 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001404 vmcs_write32(GUEST_TR_AR_BYTES,
1405 (guest_tr_ar & ~AR_TYPE_MASK)
1406 | AR_TYPE_BUSY_64_TSS);
1407 }
1408
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001409 vcpu->arch.shadow_efer |= EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410
Rusty Russell8b9cf982007-07-30 16:31:43 +10001411 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412 vmcs_write32(VM_ENTRY_CONTROLS,
1413 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001414 | VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001415}
1416
1417static void exit_lmode(struct kvm_vcpu *vcpu)
1418{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001419 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420
1421 vmcs_write32(VM_ENTRY_CONTROLS,
1422 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001423 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424}
1425
1426#endif
1427
Sheng Yang2384d2b2008-01-17 15:14:33 +08001428static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1429{
1430 vpid_sync_vcpu_all(to_vmx(vcpu));
Sheng Yang4e1096d2008-07-06 19:16:51 +08001431 if (vm_need_ept())
1432 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001433}
1434
Anthony Liguori25c4c272007-04-27 09:29:21 +03001435static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001436{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001437 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1438 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001439}
1440
Sheng Yang14394422008-04-28 12:24:45 +08001441static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1442{
1443 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1444 if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
1445 printk(KERN_ERR "EPT: Fail to load pdptrs!\n");
1446 return;
1447 }
1448 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1449 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1450 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1451 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1452 }
1453}
1454
1455static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1456
1457static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1458 unsigned long cr0,
1459 struct kvm_vcpu *vcpu)
1460{
1461 if (!(cr0 & X86_CR0_PG)) {
1462 /* From paging/starting to nonpaging */
1463 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001464 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001465 (CPU_BASED_CR3_LOAD_EXITING |
1466 CPU_BASED_CR3_STORE_EXITING));
1467 vcpu->arch.cr0 = cr0;
1468 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1469 *hw_cr0 |= X86_CR0_PE | X86_CR0_PG;
1470 *hw_cr0 &= ~X86_CR0_WP;
1471 } else if (!is_paging(vcpu)) {
1472 /* From nonpaging to paging */
1473 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001474 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001475 ~(CPU_BASED_CR3_LOAD_EXITING |
1476 CPU_BASED_CR3_STORE_EXITING));
1477 vcpu->arch.cr0 = cr0;
1478 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1479 if (!(vcpu->arch.cr0 & X86_CR0_WP))
1480 *hw_cr0 &= ~X86_CR0_WP;
1481 }
1482}
1483
1484static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1485 struct kvm_vcpu *vcpu)
1486{
1487 if (!is_paging(vcpu)) {
1488 *hw_cr4 &= ~X86_CR4_PAE;
1489 *hw_cr4 |= X86_CR4_PSE;
1490 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1491 *hw_cr4 &= ~X86_CR4_PAE;
1492}
1493
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1495{
Sheng Yang14394422008-04-28 12:24:45 +08001496 unsigned long hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) |
1497 KVM_VM_CR0_ALWAYS_ON;
1498
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001499 vmx_fpu_deactivate(vcpu);
1500
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001501 if (vcpu->arch.rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502 enter_pmode(vcpu);
1503
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001504 if (!vcpu->arch.rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505 enter_rmode(vcpu);
1506
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001507#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001508 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001509 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001510 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001511 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512 exit_lmode(vcpu);
1513 }
1514#endif
1515
Sheng Yang14394422008-04-28 12:24:45 +08001516 if (vm_need_ept())
1517 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1518
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001520 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001521 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001522
Rusty Russell707d92fa2007-07-17 23:19:08 +10001523 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001524 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001525}
1526
Sheng Yang14394422008-04-28 12:24:45 +08001527static u64 construct_eptp(unsigned long root_hpa)
1528{
1529 u64 eptp;
1530
1531 /* TODO write the value reading from MSR */
1532 eptp = VMX_EPT_DEFAULT_MT |
1533 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1534 eptp |= (root_hpa & PAGE_MASK);
1535
1536 return eptp;
1537}
1538
Avi Kivity6aa8b732006-12-10 02:21:36 -08001539static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1540{
Sheng Yang14394422008-04-28 12:24:45 +08001541 unsigned long guest_cr3;
1542 u64 eptp;
1543
1544 guest_cr3 = cr3;
1545 if (vm_need_ept()) {
1546 eptp = construct_eptp(cr3);
1547 vmcs_write64(EPT_POINTER, eptp);
1548 ept_sync_context(eptp);
1549 ept_load_pdptrs(vcpu);
1550 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
1551 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1552 }
1553
Sheng Yang2384d2b2008-01-17 15:14:33 +08001554 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001555 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001556 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001557 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001558}
1559
1560static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1561{
Sheng Yang14394422008-04-28 12:24:45 +08001562 unsigned long hw_cr4 = cr4 | (vcpu->arch.rmode.active ?
1563 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1564
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001565 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08001566 if (vm_need_ept())
1567 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1568
1569 vmcs_writel(CR4_READ_SHADOW, cr4);
1570 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571}
1572
Avi Kivity6aa8b732006-12-10 02:21:36 -08001573static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1574{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001575 struct vcpu_vmx *vmx = to_vmx(vcpu);
1576 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001577
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001578 vcpu->arch.shadow_efer = efer;
Joerg Roedel9f62e192008-01-31 14:57:39 +01001579 if (!msr)
1580 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581 if (efer & EFER_LMA) {
1582 vmcs_write32(VM_ENTRY_CONTROLS,
1583 vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001584 VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001585 msr->data = efer;
1586
1587 } else {
1588 vmcs_write32(VM_ENTRY_CONTROLS,
1589 vmcs_read32(VM_ENTRY_CONTROLS) &
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001590 ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001591
1592 msr->data = efer & ~EFER_LME;
1593 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001594 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595}
1596
Avi Kivity6aa8b732006-12-10 02:21:36 -08001597static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1598{
1599 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1600
1601 return vmcs_readl(sf->base);
1602}
1603
1604static void vmx_get_segment(struct kvm_vcpu *vcpu,
1605 struct kvm_segment *var, int seg)
1606{
1607 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1608 u32 ar;
1609
1610 var->base = vmcs_readl(sf->base);
1611 var->limit = vmcs_read32(sf->limit);
1612 var->selector = vmcs_read16(sf->selector);
1613 ar = vmcs_read32(sf->ar_bytes);
1614 if (ar & AR_UNUSABLE_MASK)
1615 ar = 0;
1616 var->type = ar & 15;
1617 var->s = (ar >> 4) & 1;
1618 var->dpl = (ar >> 5) & 3;
1619 var->present = (ar >> 7) & 1;
1620 var->avl = (ar >> 12) & 1;
1621 var->l = (ar >> 13) & 1;
1622 var->db = (ar >> 14) & 1;
1623 var->g = (ar >> 15) & 1;
1624 var->unusable = (ar >> 16) & 1;
1625}
1626
Izik Eidus2e4d2652008-03-24 19:38:34 +02001627static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1628{
1629 struct kvm_segment kvm_seg;
1630
1631 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1632 return 0;
1633
1634 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1635 return 3;
1636
1637 vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
1638 return kvm_seg.selector & 3;
1639}
1640
Avi Kivity653e3102007-05-07 10:55:37 +03001641static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001643 u32 ar;
1644
Avi Kivity653e3102007-05-07 10:55:37 +03001645 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646 ar = 1 << 16;
1647 else {
1648 ar = var->type & 15;
1649 ar |= (var->s & 1) << 4;
1650 ar |= (var->dpl & 3) << 5;
1651 ar |= (var->present & 1) << 7;
1652 ar |= (var->avl & 1) << 12;
1653 ar |= (var->l & 1) << 13;
1654 ar |= (var->db & 1) << 14;
1655 ar |= (var->g & 1) << 15;
1656 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001657 if (ar == 0) /* a 0 value means unusable */
1658 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001659
1660 return ar;
1661}
1662
1663static void vmx_set_segment(struct kvm_vcpu *vcpu,
1664 struct kvm_segment *var, int seg)
1665{
1666 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1667 u32 ar;
1668
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001669 if (vcpu->arch.rmode.active && seg == VCPU_SREG_TR) {
1670 vcpu->arch.rmode.tr.selector = var->selector;
1671 vcpu->arch.rmode.tr.base = var->base;
1672 vcpu->arch.rmode.tr.limit = var->limit;
1673 vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001674 return;
1675 }
1676 vmcs_writel(sf->base, var->base);
1677 vmcs_write32(sf->limit, var->limit);
1678 vmcs_write16(sf->selector, var->selector);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001679 if (vcpu->arch.rmode.active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001680 /*
1681 * Hack real-mode segments into vm86 compatibility.
1682 */
1683 if (var->base == 0xffff0000 && var->selector == 0xf000)
1684 vmcs_writel(sf->base, 0xf0000);
1685 ar = 0xf3;
1686 } else
1687 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688 vmcs_write32(sf->ar_bytes, ar);
1689}
1690
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1692{
1693 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1694
1695 *db = (ar >> 14) & 1;
1696 *l = (ar >> 13) & 1;
1697}
1698
1699static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1700{
1701 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1702 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1703}
1704
1705static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1706{
1707 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1708 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1709}
1710
1711static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1712{
1713 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1714 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1715}
1716
1717static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1718{
1719 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1720 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1721}
1722
Mike Dayd77c26f2007-10-08 09:02:08 -04001723static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001724{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001726 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001727 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02001728 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729
Izik Eidus195aefd2007-10-01 22:14:18 +02001730 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1731 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001732 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001733 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
1734 r = kvm_write_guest_page(kvm, fn++, &data, 0x66, sizeof(u16));
1735 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001736 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001737 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
1738 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001739 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001740 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1741 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001742 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001743 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001744 r = kvm_write_guest_page(kvm, fn, &data,
1745 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
1746 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02001747 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001748 goto out;
1749
1750 ret = 1;
1751out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001752 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001753}
1754
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001755static int init_rmode_identity_map(struct kvm *kvm)
1756{
1757 int i, r, ret;
1758 pfn_t identity_map_pfn;
1759 u32 tmp;
1760
1761 if (!vm_need_ept())
1762 return 1;
1763 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
1764 printk(KERN_ERR "EPT: identity-mapping pagetable "
1765 "haven't been allocated!\n");
1766 return 0;
1767 }
1768 if (likely(kvm->arch.ept_identity_pagetable_done))
1769 return 1;
1770 ret = 0;
1771 identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT;
1772 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
1773 if (r < 0)
1774 goto out;
1775 /* Set up identity-mapping pagetable for EPT in real mode */
1776 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
1777 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
1778 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
1779 r = kvm_write_guest_page(kvm, identity_map_pfn,
1780 &tmp, i * sizeof(tmp), sizeof(tmp));
1781 if (r < 0)
1782 goto out;
1783 }
1784 kvm->arch.ept_identity_pagetable_done = true;
1785 ret = 1;
1786out:
1787 return ret;
1788}
1789
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790static void seg_setup(int seg)
1791{
1792 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1793
1794 vmcs_write16(sf->selector, 0);
1795 vmcs_writel(sf->base, 0);
1796 vmcs_write32(sf->limit, 0xffff);
1797 vmcs_write32(sf->ar_bytes, 0x93);
1798}
1799
Sheng Yangf78e0e22007-10-29 09:40:42 +08001800static int alloc_apic_access_page(struct kvm *kvm)
1801{
1802 struct kvm_userspace_memory_region kvm_userspace_mem;
1803 int r = 0;
1804
Izik Eidus72dc67a2008-02-10 18:04:15 +02001805 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001806 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001807 goto out;
1808 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
1809 kvm_userspace_mem.flags = 0;
1810 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
1811 kvm_userspace_mem.memory_size = PAGE_SIZE;
1812 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
1813 if (r)
1814 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001815
1816 down_read(&current->mm->mmap_sem);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001817 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001818 up_read(&current->mm->mmap_sem);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001819out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001820 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001821 return r;
1822}
1823
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001824static int alloc_identity_pagetable(struct kvm *kvm)
1825{
1826 struct kvm_userspace_memory_region kvm_userspace_mem;
1827 int r = 0;
1828
1829 down_write(&kvm->slots_lock);
1830 if (kvm->arch.ept_identity_pagetable)
1831 goto out;
1832 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
1833 kvm_userspace_mem.flags = 0;
1834 kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1835 kvm_userspace_mem.memory_size = PAGE_SIZE;
1836 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
1837 if (r)
1838 goto out;
1839
1840 down_read(&current->mm->mmap_sem);
1841 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
1842 VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT);
1843 up_read(&current->mm->mmap_sem);
1844out:
1845 up_write(&kvm->slots_lock);
1846 return r;
1847}
1848
Sheng Yang2384d2b2008-01-17 15:14:33 +08001849static void allocate_vpid(struct vcpu_vmx *vmx)
1850{
1851 int vpid;
1852
1853 vmx->vpid = 0;
1854 if (!enable_vpid || !cpu_has_vmx_vpid())
1855 return;
1856 spin_lock(&vmx_vpid_lock);
1857 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
1858 if (vpid < VMX_NR_VPIDS) {
1859 vmx->vpid = vpid;
1860 __set_bit(vpid, vmx_vpid_bitmap);
1861 }
1862 spin_unlock(&vmx_vpid_lock);
1863}
1864
Harvey Harrison8b2cf732008-04-27 12:14:13 -07001865static void vmx_disable_intercept_for_msr(struct page *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08001866{
1867 void *va;
1868
1869 if (!cpu_has_vmx_msr_bitmap())
1870 return;
1871
1872 /*
1873 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
1874 * have the write-low and read-high bitmap offsets the wrong way round.
1875 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
1876 */
1877 va = kmap(msr_bitmap);
1878 if (msr <= 0x1fff) {
1879 __clear_bit(msr, va + 0x000); /* read-low */
1880 __clear_bit(msr, va + 0x800); /* write-low */
1881 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
1882 msr &= 0x1fff;
1883 __clear_bit(msr, va + 0x400); /* read-high */
1884 __clear_bit(msr, va + 0xc00); /* write-high */
1885 }
1886 kunmap(msr_bitmap);
1887}
1888
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889/*
1890 * Sets up the vmcs for emulated real mode.
1891 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001892static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001893{
1894 u32 host_sysenter_cs;
1895 u32 junk;
1896 unsigned long a;
1897 struct descriptor_table dt;
1898 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03001899 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001900 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001901
Avi Kivity6aa8b732006-12-10 02:21:36 -08001902 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03001903 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
1904 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001905
Sheng Yang25c5f222008-03-28 13:18:56 +08001906 if (cpu_has_vmx_msr_bitmap())
1907 vmcs_write64(MSR_BITMAP, page_to_phys(vmx_msr_bitmap));
1908
Avi Kivity6aa8b732006-12-10 02:21:36 -08001909 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
1910
Avi Kivity6aa8b732006-12-10 02:21:36 -08001911 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001912 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
1913 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001914
1915 exec_control = vmcs_config.cpu_based_exec_ctrl;
1916 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
1917 exec_control &= ~CPU_BASED_TPR_SHADOW;
1918#ifdef CONFIG_X86_64
1919 exec_control |= CPU_BASED_CR8_STORE_EXITING |
1920 CPU_BASED_CR8_LOAD_EXITING;
1921#endif
1922 }
Sheng Yangd56f5462008-04-25 10:13:16 +08001923 if (!vm_need_ept())
1924 exec_control |= CPU_BASED_CR3_STORE_EXITING |
1925 CPU_BASED_CR3_LOAD_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001926 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001927
Sheng Yang83ff3b92007-11-21 14:33:25 +08001928 if (cpu_has_secondary_exec_ctrls()) {
1929 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
1930 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
1931 exec_control &=
1932 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001933 if (vmx->vpid == 0)
1934 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yangd56f5462008-04-25 10:13:16 +08001935 if (!vm_need_ept())
1936 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang83ff3b92007-11-21 14:33:25 +08001937 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
1938 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08001939
Avi Kivityc7addb92007-09-16 18:58:32 +02001940 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
1941 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
1943
1944 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
1945 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
1946 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1947
1948 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
1949 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1950 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001951 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
1952 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001953 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001954#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001955 rdmsrl(MSR_FS_BASE, a);
1956 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
1957 rdmsrl(MSR_GS_BASE, a);
1958 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
1959#else
1960 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
1961 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
1962#endif
1963
1964 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
1965
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001966 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001967 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
1968
Mike Dayd77c26f2007-10-08 09:02:08 -04001969 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03001970 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03001971 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
1972 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
1973 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001974
1975 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
1976 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
1977 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
1978 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
1979 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
1980 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
1981
Avi Kivity6aa8b732006-12-10 02:21:36 -08001982 for (i = 0; i < NR_VMX_MSR; ++i) {
1983 u32 index = vmx_msr_index[i];
1984 u32 data_low, data_high;
1985 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001986 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001987
1988 if (rdmsr_safe(index, &data_low, &data_high) < 0)
1989 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08001990 if (wrmsr_safe(index, data_low, data_high) < 0)
1991 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001992 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001993 vmx->host_msrs[j].index = index;
1994 vmx->host_msrs[j].reserved = 0;
1995 vmx->host_msrs[j].data = data;
1996 vmx->guest_msrs[j] = vmx->host_msrs[j];
1997 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001998 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001999
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002000 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002001
2002 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002003 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2004
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002005 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2006 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2007
Sheng Yangf78e0e22007-10-29 09:40:42 +08002008
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002009 return 0;
2010}
2011
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002012static int init_rmode(struct kvm *kvm)
2013{
2014 if (!init_rmode_tss(kvm))
2015 return 0;
2016 if (!init_rmode_identity_map(kvm))
2017 return 0;
2018 return 1;
2019}
2020
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002021static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2022{
2023 struct vcpu_vmx *vmx = to_vmx(vcpu);
2024 u64 msr;
2025 int ret;
2026
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002027 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002028 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002029 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002030 ret = -ENOMEM;
2031 goto out;
2032 }
2033
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002034 vmx->vcpu.arch.rmode.active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002035
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002036 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002037 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002038 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
2039 if (vmx->vcpu.vcpu_id == 0)
2040 msr |= MSR_IA32_APICBASE_BSP;
2041 kvm_set_apic_base(&vmx->vcpu, msr);
2042
2043 fx_init(&vmx->vcpu);
2044
2045 /*
2046 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2047 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2048 */
2049 if (vmx->vcpu.vcpu_id == 0) {
2050 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2051 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2052 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002053 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2054 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002055 }
2056 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
2057 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
2058
2059 seg_setup(VCPU_SREG_DS);
2060 seg_setup(VCPU_SREG_ES);
2061 seg_setup(VCPU_SREG_FS);
2062 seg_setup(VCPU_SREG_GS);
2063 seg_setup(VCPU_SREG_SS);
2064
2065 vmcs_write16(GUEST_TR_SELECTOR, 0);
2066 vmcs_writel(GUEST_TR_BASE, 0);
2067 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2068 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2069
2070 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2071 vmcs_writel(GUEST_LDTR_BASE, 0);
2072 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2073 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2074
2075 vmcs_write32(GUEST_SYSENTER_CS, 0);
2076 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2077 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2078
2079 vmcs_writel(GUEST_RFLAGS, 0x02);
2080 if (vmx->vcpu.vcpu_id == 0)
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002081 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002082 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002083 kvm_rip_write(vcpu, 0);
2084 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002085
2086 /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */
2087 vmcs_writel(GUEST_DR7, 0x400);
2088
2089 vmcs_writel(GUEST_GDTR_BASE, 0);
2090 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2091
2092 vmcs_writel(GUEST_IDTR_BASE, 0);
2093 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2094
2095 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2096 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2097 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2098
2099 guest_write_tsc(0);
2100
2101 /* Special registers */
2102 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2103
2104 setup_msrs(vmx);
2105
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2107
Sheng Yangf78e0e22007-10-29 09:40:42 +08002108 if (cpu_has_vmx_tpr_shadow()) {
2109 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2110 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2111 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002112 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002113 vmcs_write32(TPR_THRESHOLD, 0);
2114 }
2115
2116 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2117 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002118 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119
Sheng Yang2384d2b2008-01-17 15:14:33 +08002120 if (vmx->vpid != 0)
2121 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2122
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002123 vmx->vcpu.arch.cr0 = 0x60000010;
2124 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002125 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002126 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002127 vmx_fpu_activate(&vmx->vcpu);
2128 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002129
Sheng Yang2384d2b2008-01-17 15:14:33 +08002130 vpid_sync_vcpu_all(vmx);
2131
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002132 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002133
Avi Kivity6aa8b732006-12-10 02:21:36 -08002134out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002135 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002136 return ret;
2137}
2138
Eddie Dong85f455f2007-07-06 12:20:49 +03002139static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
2140{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002141 struct vcpu_vmx *vmx = to_vmx(vcpu);
2142
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002143 KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler);
2144
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002145 if (vcpu->arch.rmode.active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002146 vmx->rmode.irq.pending = true;
2147 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002148 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Avi Kivity9c5623e2007-11-08 18:19:20 +02002149 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2150 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2151 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002152 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002153 return;
2154 }
2155 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2156 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
2157}
2158
Sheng Yangf08864b2008-05-15 18:23:25 +08002159static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2160{
2161 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2162 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002163}
2164
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
2166{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002167 int word_index = __ffs(vcpu->arch.irq_summary);
2168 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002169 int irq = word_index * BITS_PER_LONG + bit_index;
2170
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002171 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
2172 if (!vcpu->arch.irq_pending[word_index])
2173 clear_bit(word_index, &vcpu->arch.irq_summary);
Eddie Dong85f455f2007-07-06 12:20:49 +03002174 vmx_inject_irq(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002175}
2176
Dor Laorc1150d82007-01-05 16:36:24 -08002177
2178static void do_interrupt_requests(struct kvm_vcpu *vcpu,
2179 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180{
Dor Laorc1150d82007-01-05 16:36:24 -08002181 u32 cpu_based_vm_exec_control;
2182
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002183 vcpu->arch.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08002184 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2185 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
2186
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002187 if (vcpu->arch.interrupt_window_open &&
2188 vcpu->arch.irq_summary &&
Dor Laorc1150d82007-01-05 16:36:24 -08002189 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002190 /*
Dor Laorc1150d82007-01-05 16:36:24 -08002191 * If interrupts enabled, and not blocked by sti or mov ss. Good.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002192 */
2193 kvm_do_inject_irq(vcpu);
Dor Laorc1150d82007-01-05 16:36:24 -08002194
2195 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002196 if (!vcpu->arch.interrupt_window_open &&
2197 (vcpu->arch.irq_summary || kvm_run->request_interrupt_window))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002198 /*
2199 * Interrupts blocked. Wait for unblock.
2200 */
Dor Laorc1150d82007-01-05 16:36:24 -08002201 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2202 else
2203 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2204 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002205}
2206
Izik Eiduscbc94022007-10-25 00:29:55 +02002207static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2208{
2209 int ret;
2210 struct kvm_userspace_memory_region tss_mem = {
2211 .slot = 8,
2212 .guest_phys_addr = addr,
2213 .memory_size = PAGE_SIZE * 3,
2214 .flags = 0,
2215 };
2216
2217 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2218 if (ret)
2219 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002220 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002221 return 0;
2222}
2223
Avi Kivity6aa8b732006-12-10 02:21:36 -08002224static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
2225{
2226 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
2227
2228 set_debugreg(dbg->bp[0], 0);
2229 set_debugreg(dbg->bp[1], 1);
2230 set_debugreg(dbg->bp[2], 2);
2231 set_debugreg(dbg->bp[3], 3);
2232
2233 if (dbg->singlestep) {
2234 unsigned long flags;
2235
2236 flags = vmcs_readl(GUEST_RFLAGS);
2237 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
2238 vmcs_writel(GUEST_RFLAGS, flags);
2239 }
2240}
2241
2242static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2243 int vec, u32 err_code)
2244{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002245 /*
2246 * Instruction with address size override prefix opcode 0x67
2247 * Cause the #SS fault with 0 error code in VM86 mode.
2248 */
2249 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02002250 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002251 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002252 /*
2253 * Forward all other exceptions that are valid in real mode.
2254 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2255 * the required debugging infrastructure rework.
2256 */
2257 switch (vec) {
2258 case DE_VECTOR:
2259 case DB_VECTOR:
2260 case BP_VECTOR:
2261 case OF_VECTOR:
2262 case BR_VECTOR:
2263 case UD_VECTOR:
2264 case DF_VECTOR:
2265 case SS_VECTOR:
2266 case GP_VECTOR:
2267 case MF_VECTOR:
2268 kvm_queue_exception(vcpu, vec);
2269 return 1;
2270 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271 return 0;
2272}
2273
2274static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2275{
Avi Kivity1155f762007-11-22 11:30:47 +02002276 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002277 u32 intr_info, error_code;
2278 unsigned long cr2, rip;
2279 u32 vect_info;
2280 enum emulation_result er;
2281
Avi Kivity1155f762007-11-22 11:30:47 +02002282 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002283 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2284
2285 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002286 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002287 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002288 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289
Eddie Dong85f455f2007-07-06 12:20:49 +03002290 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002291 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002292 set_bit(irq, vcpu->arch.irq_pending);
2293 set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294 }
2295
Avi Kivity1b6269d2007-10-09 12:12:19 +02002296 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
2297 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002298
2299 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002300 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002301 return 1;
2302 }
2303
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002304 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08002305 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002306 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002307 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002308 return 1;
2309 }
2310
Avi Kivity6aa8b732006-12-10 02:21:36 -08002311 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002312 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002313 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002314 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2315 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002316 /* EPT won't cause page fault directly */
2317 if (vm_need_ept())
2318 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002319 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002320 KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2,
2321 (u32)((u64)cr2 >> 32), handler);
Avi Kivityf7d92382008-07-03 16:14:28 +03002322 if (vcpu->arch.interrupt.pending || vcpu->arch.exception.pending)
Avi Kivity577bdc42008-07-19 08:57:05 +03002323 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002324 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325 }
2326
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002327 if (vcpu->arch.rmode.active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002328 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002329 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002330 if (vcpu->arch.halt_request) {
2331 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002332 return kvm_emulate_halt(vcpu);
2333 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002334 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002335 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336
Mike Dayd77c26f2007-10-08 09:02:08 -04002337 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) ==
2338 (INTR_TYPE_EXCEPTION | 1)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2340 return 0;
2341 }
2342 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2343 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
2344 kvm_run->ex.error_code = error_code;
2345 return 0;
2346}
2347
2348static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2349 struct kvm_run *kvm_run)
2350{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002351 ++vcpu->stat.irq_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002352 KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002353 return 1;
2354}
2355
Avi Kivity988ad742007-02-12 00:54:36 -08002356static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2357{
2358 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2359 return 0;
2360}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002361
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2363{
He, Qingbfdaab02007-09-12 14:18:28 +08002364 unsigned long exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02002365 int size, down, in, string, rep;
2366 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367
Avi Kivity1165f5f2007-04-19 17:27:43 +03002368 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002369 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002370 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002371
2372 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002373 if (emulate_instruction(vcpu,
2374 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002375 return 0;
2376 return 1;
2377 }
2378
2379 size = (exit_qualification & 7) + 1;
2380 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002381 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002382 rep = (exit_qualification & 32) != 0;
2383 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002384
Laurent Vivier3090dd72007-08-05 10:43:32 +03002385 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386}
2387
Ingo Molnar102d8322007-02-19 14:37:47 +02002388static void
2389vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2390{
2391 /*
2392 * Patch in the VMCALL instruction:
2393 */
2394 hypercall[0] = 0x0f;
2395 hypercall[1] = 0x01;
2396 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002397}
2398
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2400{
He, Qingbfdaab02007-09-12 14:18:28 +08002401 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402 int cr;
2403 int reg;
2404
He, Qingbfdaab02007-09-12 14:18:28 +08002405 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002406 cr = exit_qualification & 15;
2407 reg = (exit_qualification >> 8) & 15;
2408 switch ((exit_qualification >> 4) & 3) {
2409 case 0: /* mov to cr */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002410 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr,
2411 (u32)kvm_register_read(vcpu, reg),
2412 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
2413 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002414 switch (cr) {
2415 case 0:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002416 kvm_set_cr0(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002417 skip_emulated_instruction(vcpu);
2418 return 1;
2419 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002420 kvm_set_cr3(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421 skip_emulated_instruction(vcpu);
2422 return 1;
2423 case 4:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002424 kvm_set_cr4(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425 skip_emulated_instruction(vcpu);
2426 return 1;
2427 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002428 kvm_set_cr8(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429 skip_emulated_instruction(vcpu);
Avi Kivitye5314062007-12-06 16:32:45 +02002430 if (irqchip_in_kernel(vcpu->kvm))
2431 return 1;
Yang, Sheng253abde2007-08-16 13:01:00 +03002432 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2433 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002434 };
2435 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002436 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002437 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002438 vcpu->arch.cr0 &= ~X86_CR0_TS;
2439 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002440 vmx_fpu_activate(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002441 KVMTRACE_0D(CLTS, vcpu, handler);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002442 skip_emulated_instruction(vcpu);
2443 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444 case 1: /*mov from cr*/
2445 switch (cr) {
2446 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002447 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002448 KVMTRACE_3D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002449 (u32)kvm_register_read(vcpu, reg),
2450 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002451 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002452 skip_emulated_instruction(vcpu);
2453 return 1;
2454 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002455 kvm_register_write(vcpu, reg, kvm_get_cr8(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002456 KVMTRACE_2D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002457 (u32)kvm_register_read(vcpu, reg), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458 skip_emulated_instruction(vcpu);
2459 return 1;
2460 }
2461 break;
2462 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002463 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002464
2465 skip_emulated_instruction(vcpu);
2466 return 1;
2467 default:
2468 break;
2469 }
2470 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002471 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002472 (int)(exit_qualification >> 4) & 3, cr);
2473 return 0;
2474}
2475
2476static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2477{
He, Qingbfdaab02007-09-12 14:18:28 +08002478 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002479 unsigned long val;
2480 int dr, reg;
2481
2482 /*
2483 * FIXME: this code assumes the host is debugging the guest.
2484 * need to deal with guest debugging itself too.
2485 */
He, Qingbfdaab02007-09-12 14:18:28 +08002486 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002487 dr = exit_qualification & 7;
2488 reg = (exit_qualification >> 8) & 15;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002489 if (exit_qualification & 16) {
2490 /* mov from dr */
2491 switch (dr) {
2492 case 6:
2493 val = 0xffff0ff0;
2494 break;
2495 case 7:
2496 val = 0x400;
2497 break;
2498 default:
2499 val = 0;
2500 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002501 kvm_register_write(vcpu, reg, val);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002502 KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002503 } else {
2504 /* mov to dr */
2505 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002506 skip_emulated_instruction(vcpu);
2507 return 1;
2508}
2509
2510static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2511{
Avi Kivity06465c52007-02-28 20:46:53 +02002512 kvm_emulate_cpuid(vcpu);
2513 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002514}
2515
2516static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2517{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002518 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519 u64 data;
2520
2521 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002522 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523 return 1;
2524 }
2525
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002526 KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32),
2527 handler);
2528
Avi Kivity6aa8b732006-12-10 02:21:36 -08002529 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002530 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
2531 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532 skip_emulated_instruction(vcpu);
2533 return 1;
2534}
2535
2536static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2537{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002538 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
2539 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
2540 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002542 KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32),
2543 handler);
2544
Avi Kivity6aa8b732006-12-10 02:21:36 -08002545 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002546 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547 return 1;
2548 }
2549
2550 skip_emulated_instruction(vcpu);
2551 return 1;
2552}
2553
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002554static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2555 struct kvm_run *kvm_run)
2556{
2557 return 1;
2558}
2559
Avi Kivity6aa8b732006-12-10 02:21:36 -08002560static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2561 struct kvm_run *kvm_run)
2562{
Eddie Dong85f455f2007-07-06 12:20:49 +03002563 u32 cpu_based_vm_exec_control;
2564
2565 /* clear pending irq */
2566 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2567 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2568 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002569
2570 KVMTRACE_0D(PEND_INTR, vcpu, handler);
2571
Dor Laorc1150d82007-01-05 16:36:24 -08002572 /*
2573 * If the user space waits to inject interrupts, exit as soon as
2574 * possible
2575 */
2576 if (kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002577 !vcpu->arch.irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002578 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002579 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002580 return 0;
2581 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582 return 1;
2583}
2584
2585static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2586{
2587 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002588 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002589}
2590
Ingo Molnarc21415e2007-02-19 14:37:47 +02002591static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2592{
Dor Laor510043d2007-02-19 18:25:43 +02002593 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002594 kvm_emulate_hypercall(vcpu);
2595 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02002596}
2597
Eddie Donge5edaa02007-11-11 12:28:35 +02002598static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2599{
2600 skip_emulated_instruction(vcpu);
2601 /* TODO: Add support for VT-d/pass-through device */
2602 return 1;
2603}
2604
Sheng Yangf78e0e22007-10-29 09:40:42 +08002605static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2606{
2607 u64 exit_qualification;
2608 enum emulation_result er;
2609 unsigned long offset;
2610
2611 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2612 offset = exit_qualification & 0xffful;
2613
2614 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
2615
2616 if (er != EMULATE_DONE) {
2617 printk(KERN_ERR
2618 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
2619 offset);
2620 return -ENOTSUPP;
2621 }
2622 return 1;
2623}
2624
Izik Eidus37817f22008-03-24 23:14:53 +02002625static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2626{
2627 unsigned long exit_qualification;
2628 u16 tss_selector;
2629 int reason;
2630
2631 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
2632
2633 reason = (u32)exit_qualification >> 30;
2634 tss_selector = exit_qualification;
2635
2636 return kvm_task_switch(vcpu, tss_selector, reason);
2637}
2638
Sheng Yang14394422008-04-28 12:24:45 +08002639static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2640{
2641 u64 exit_qualification;
2642 enum emulation_result er;
2643 gpa_t gpa;
2644 unsigned long hva;
2645 int gla_validity;
2646 int r;
2647
2648 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2649
2650 if (exit_qualification & (1 << 6)) {
2651 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
2652 return -ENOTSUPP;
2653 }
2654
2655 gla_validity = (exit_qualification >> 7) & 0x3;
2656 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
2657 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
2658 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
2659 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
2660 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
2661 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
2662 (long unsigned int)exit_qualification);
2663 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2664 kvm_run->hw.hardware_exit_reason = 0;
2665 return -ENOTSUPP;
2666 }
2667
2668 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
2669 hva = gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT);
2670 if (!kvm_is_error_hva(hva)) {
2671 r = kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
2672 if (r < 0) {
2673 printk(KERN_ERR "EPT: Not enough memory!\n");
2674 return -ENOMEM;
2675 }
2676 return 1;
2677 } else {
2678 /* must be MMIO */
2679 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
2680
2681 if (er == EMULATE_FAIL) {
2682 printk(KERN_ERR
2683 "EPT: Fail to handle EPT violation vmexit!er is %d\n",
2684 er);
2685 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
2686 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
2687 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
2688 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
2689 (long unsigned int)exit_qualification);
2690 return -ENOTSUPP;
2691 } else if (er == EMULATE_DO_MMIO)
2692 return 0;
2693 }
2694 return 1;
2695}
2696
Sheng Yangf08864b2008-05-15 18:23:25 +08002697static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2698{
2699 u32 cpu_based_vm_exec_control;
2700
2701 /* clear pending NMI */
2702 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2703 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
2704 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2705 ++vcpu->stat.nmi_window_exits;
2706
2707 return 1;
2708}
2709
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710/*
2711 * The exit handlers return 1 if the exit was handled fully and guest execution
2712 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2713 * to be done to userspace and return 0.
2714 */
2715static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
2716 struct kvm_run *kvm_run) = {
2717 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
2718 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08002719 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08002720 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002722 [EXIT_REASON_CR_ACCESS] = handle_cr,
2723 [EXIT_REASON_DR_ACCESS] = handle_dr,
2724 [EXIT_REASON_CPUID] = handle_cpuid,
2725 [EXIT_REASON_MSR_READ] = handle_rdmsr,
2726 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
2727 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
2728 [EXIT_REASON_HLT] = handle_halt,
Ingo Molnarc21415e2007-02-19 14:37:47 +02002729 [EXIT_REASON_VMCALL] = handle_vmcall,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002730 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
2731 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02002732 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02002733 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Sheng Yang14394422008-04-28 12:24:45 +08002734 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735};
2736
2737static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04002738 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739
2740/*
2741 * The guest has exited. See if we can fix it or if we need userspace
2742 * assistance.
2743 */
2744static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2745{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002746 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity29bd8a72007-09-10 17:27:03 +03002747 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1155f762007-11-22 11:30:47 +02002748 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03002749
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002750 KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)kvm_rip_read(vcpu),
2751 (u32)((u64)kvm_rip_read(vcpu) >> 32), entryexit);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002752
Sheng Yang14394422008-04-28 12:24:45 +08002753 /* Access CR3 don't cause VMExit in paging mode, so we need
2754 * to sync with guest real CR3. */
2755 if (vm_need_ept() && is_paging(vcpu)) {
2756 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2757 ept_load_pdptrs(vcpu);
2758 }
2759
Avi Kivity29bd8a72007-09-10 17:27:03 +03002760 if (unlikely(vmx->fail)) {
2761 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2762 kvm_run->fail_entry.hardware_entry_failure_reason
2763 = vmcs_read32(VM_INSTRUCTION_ERROR);
2764 return 0;
2765 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766
Mike Dayd77c26f2007-10-08 09:02:08 -04002767 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08002768 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
2769 exit_reason != EXIT_REASON_EPT_VIOLATION))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 printk(KERN_WARNING "%s: unexpected, valid vectoring info and "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002771 "exit reason is 0x%x\n", __func__, exit_reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002772 if (exit_reason < kvm_vmx_max_exit_handlers
2773 && kvm_vmx_exit_handlers[exit_reason])
2774 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
2775 else {
2776 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2777 kvm_run->hw.hardware_exit_reason = exit_reason;
2778 }
2779 return 0;
2780}
2781
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002782static void update_tpr_threshold(struct kvm_vcpu *vcpu)
2783{
2784 int max_irr, tpr;
2785
2786 if (!vm_need_tpr_shadow(vcpu->kvm))
2787 return;
2788
2789 if (!kvm_lapic_enabled(vcpu) ||
2790 ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
2791 vmcs_write32(TPR_THRESHOLD, 0);
2792 return;
2793 }
2794
2795 tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
2796 vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
2797}
2798
Eddie Dong85f455f2007-07-06 12:20:49 +03002799static void enable_irq_window(struct kvm_vcpu *vcpu)
2800{
2801 u32 cpu_based_vm_exec_control;
2802
2803 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2804 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2805 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2806}
2807
Sheng Yangf08864b2008-05-15 18:23:25 +08002808static void enable_nmi_window(struct kvm_vcpu *vcpu)
2809{
2810 u32 cpu_based_vm_exec_control;
2811
2812 if (!cpu_has_virtual_nmis())
2813 return;
2814
2815 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2816 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2817 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2818}
2819
2820static int vmx_nmi_enabled(struct kvm_vcpu *vcpu)
2821{
2822 u32 guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2823 return !(guest_intr & (GUEST_INTR_STATE_NMI |
2824 GUEST_INTR_STATE_MOV_SS |
2825 GUEST_INTR_STATE_STI));
2826}
2827
2828static int vmx_irq_enabled(struct kvm_vcpu *vcpu)
2829{
2830 u32 guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2831 return (!(guest_intr & (GUEST_INTR_STATE_MOV_SS |
2832 GUEST_INTR_STATE_STI)) &&
2833 (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF));
2834}
2835
2836static void enable_intr_window(struct kvm_vcpu *vcpu)
2837{
2838 if (vcpu->arch.nmi_pending)
2839 enable_nmi_window(vcpu);
2840 else if (kvm_cpu_has_interrupt(vcpu))
2841 enable_irq_window(vcpu);
2842}
2843
Avi Kivitycf393f72008-07-01 16:20:21 +03002844static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
2845{
2846 u32 exit_intr_info;
Avi Kivity668f6122008-07-02 09:28:55 +03002847 u32 idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03002848 bool unblock_nmi;
2849 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03002850 int type;
2851 bool idtv_info_valid;
Avi Kivity35920a32008-07-03 14:50:12 +03002852 u32 error;
Avi Kivitycf393f72008-07-01 16:20:21 +03002853
2854 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2855 if (cpu_has_virtual_nmis()) {
2856 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
2857 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
2858 /*
2859 * SDM 3: 25.7.1.2
2860 * Re-set bit "block by NMI" before VM entry if vmexit caused by
2861 * a guest IRET fault.
2862 */
2863 if (unblock_nmi && vector != DF_VECTOR)
2864 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
2865 GUEST_INTR_STATE_NMI);
2866 }
Avi Kivity668f6122008-07-02 09:28:55 +03002867
2868 idt_vectoring_info = vmx->idt_vectoring_info;
2869 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
2870 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
2871 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
2872 if (vmx->vcpu.arch.nmi_injected) {
2873 /*
2874 * SDM 3: 25.7.1.2
2875 * Clear bit "block by NMI" before VM entry if a NMI delivery
2876 * faulted.
2877 */
2878 if (idtv_info_valid && type == INTR_TYPE_NMI_INTR)
2879 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
2880 GUEST_INTR_STATE_NMI);
2881 else
2882 vmx->vcpu.arch.nmi_injected = false;
2883 }
Avi Kivity35920a32008-07-03 14:50:12 +03002884 kvm_clear_exception_queue(&vmx->vcpu);
2885 if (idtv_info_valid && type == INTR_TYPE_EXCEPTION) {
2886 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
2887 error = vmcs_read32(IDT_VECTORING_ERROR_CODE);
2888 kvm_queue_exception_e(&vmx->vcpu, vector, error);
2889 } else
2890 kvm_queue_exception(&vmx->vcpu, vector);
2891 vmx->idt_vectoring_info = 0;
2892 }
Avi Kivityf7d92382008-07-03 16:14:28 +03002893 kvm_clear_interrupt_queue(&vmx->vcpu);
2894 if (idtv_info_valid && type == INTR_TYPE_EXT_INTR) {
2895 kvm_queue_interrupt(&vmx->vcpu, vector);
2896 vmx->idt_vectoring_info = 0;
2897 }
Avi Kivitycf393f72008-07-01 16:20:21 +03002898}
2899
Eddie Dong85f455f2007-07-06 12:20:49 +03002900static void vmx_intr_assist(struct kvm_vcpu *vcpu)
2901{
Avi Kivityf7d92382008-07-03 16:14:28 +03002902 u32 intr_info_field;
Eddie Dong85f455f2007-07-06 12:20:49 +03002903
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002904 update_tpr_threshold(vcpu);
2905
Eddie Dong85f455f2007-07-06 12:20:49 +03002906 intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
Sheng Yangf08864b2008-05-15 18:23:25 +08002907 if (cpu_has_virtual_nmis()) {
Avi Kivity668f6122008-07-02 09:28:55 +03002908 if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
2909 if (vmx_nmi_enabled(vcpu)) {
2910 vcpu->arch.nmi_pending = false;
2911 vcpu->arch.nmi_injected = true;
2912 } else {
2913 enable_intr_window(vcpu);
2914 return;
2915 }
2916 }
2917 if (vcpu->arch.nmi_injected) {
2918 vmx_inject_nmi(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08002919 enable_intr_window(vcpu);
2920 return;
2921 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002922 }
Avi Kivityf7d92382008-07-03 16:14:28 +03002923 if (!vcpu->arch.interrupt.pending && kvm_cpu_has_interrupt(vcpu)) {
2924 if (vmx_irq_enabled(vcpu))
2925 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu));
2926 else
2927 enable_irq_window(vcpu);
2928 }
2929 if (vcpu->arch.interrupt.pending) {
2930 vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
2931 kvm_timer_intr_post(vcpu, vcpu->arch.interrupt.nr);
2932 }
Eddie Dong85f455f2007-07-06 12:20:49 +03002933}
2934
Avi Kivity9c8cba32007-11-22 11:42:59 +02002935/*
2936 * Failure to inject an interrupt should give us the information
2937 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
2938 * when fetching the interrupt redirection bitmap in the real-mode
2939 * tss, this doesn't happen. So we do it ourselves.
2940 */
2941static void fixup_rmode_irq(struct vcpu_vmx *vmx)
2942{
2943 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002944 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02002945 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002946 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02002947 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
2948 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
2949 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
2950 return;
2951 }
2952 vmx->idt_vectoring_info =
2953 VECTORING_INFO_VALID_MASK
2954 | INTR_TYPE_EXT_INTR
2955 | vmx->rmode.irq.vector;
2956}
2957
Avi Kivityc8019492008-07-14 14:44:59 +03002958#ifdef CONFIG_X86_64
2959#define R "r"
2960#define Q "q"
2961#else
2962#define R "e"
2963#define Q "l"
2964#endif
2965
Avi Kivity04d2cc72007-09-10 18:10:54 +03002966static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002968 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1b6269d2007-10-09 12:12:19 +02002969 u32 intr_info;
Avi Kivitye6adf282007-04-30 16:07:54 +03002970
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002971 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
2972 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
2973 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
2974 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
2975
Avi Kivitye6adf282007-04-30 16:07:54 +03002976 /*
2977 * Loading guest fpu may have cleared host cr0.ts
2978 */
2979 vmcs_writel(HOST_CR0, read_cr0());
2980
Mike Dayd77c26f2007-10-08 09:02:08 -04002981 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08002982 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03002983 "push %%"R"dx; push %%"R"bp;"
2984 "push %%"R"cx \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002985 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002986 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02002987 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002988 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03002989 "mov %c[cr2](%0), %%"R"ax \n\t"
2990 "mov %%"R"ax, %%cr2 \n\t"
2991 "mov %c[rax](%0), %%"R"ax \n\t"
2992 "mov %c[rbx](%0), %%"R"bx \n\t"
2993 "mov %c[rdx](%0), %%"R"dx \n\t"
2994 "mov %c[rsi](%0), %%"R"si \n\t"
2995 "mov %c[rdi](%0), %%"R"di \n\t"
2996 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002997#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02002998 "mov %c[r8](%0), %%r8 \n\t"
2999 "mov %c[r9](%0), %%r9 \n\t"
3000 "mov %c[r10](%0), %%r10 \n\t"
3001 "mov %c[r11](%0), %%r11 \n\t"
3002 "mov %c[r12](%0), %%r12 \n\t"
3003 "mov %c[r13](%0), %%r13 \n\t"
3004 "mov %c[r14](%0), %%r14 \n\t"
3005 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003006#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003007 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3008
Avi Kivity6aa8b732006-12-10 02:21:36 -08003009 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003010 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003011 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003012 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003013 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003014 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003015 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003016 "xchg %0, (%%"R"sp) \n\t"
3017 "mov %%"R"ax, %c[rax](%0) \n\t"
3018 "mov %%"R"bx, %c[rbx](%0) \n\t"
3019 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3020 "mov %%"R"dx, %c[rdx](%0) \n\t"
3021 "mov %%"R"si, %c[rsi](%0) \n\t"
3022 "mov %%"R"di, %c[rdi](%0) \n\t"
3023 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003024#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003025 "mov %%r8, %c[r8](%0) \n\t"
3026 "mov %%r9, %c[r9](%0) \n\t"
3027 "mov %%r10, %c[r10](%0) \n\t"
3028 "mov %%r11, %c[r11](%0) \n\t"
3029 "mov %%r12, %c[r12](%0) \n\t"
3030 "mov %%r13, %c[r13](%0) \n\t"
3031 "mov %%r14, %c[r14](%0) \n\t"
3032 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003033#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003034 "mov %%cr2, %%"R"ax \n\t"
3035 "mov %%"R"ax, %c[cr2](%0) \n\t"
3036
3037 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003038 "setbe %c[fail](%0) \n\t"
3039 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3040 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3041 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003042 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3043 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3044 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3045 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3046 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3047 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3048 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003049#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003050 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3051 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3052 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3053 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3054 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3055 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3056 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3057 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003058#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003059 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003060 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003061 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003062#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003063 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3064#endif
3065 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003067 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
3068 vcpu->arch.regs_dirty = 0;
3069
Avi Kivity1155f762007-11-22 11:30:47 +02003070 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003071 if (vmx->rmode.irq.pending)
3072 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003073
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003074 vcpu->arch.interrupt_window_open =
Sheng Yangf08864b2008-05-15 18:23:25 +08003075 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
3076 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)) == 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003077
Mike Dayd77c26f2007-10-08 09:02:08 -04003078 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003079 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003080
3081 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
3082
3083 /* We need to handle NMIs before interrupts are enabled */
Sheng Yangf08864b2008-05-15 18:23:25 +08003084 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200 &&
3085 (intr_info & INTR_INFO_VALID_MASK)) {
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003086 KVMTRACE_0D(NMI, vcpu, handler);
Avi Kivity1b6269d2007-10-09 12:12:19 +02003087 asm("int $2");
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003088 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003089
3090 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003091}
3092
Avi Kivityc8019492008-07-14 14:44:59 +03003093#undef R
3094#undef Q
3095
Avi Kivity6aa8b732006-12-10 02:21:36 -08003096static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3097{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003098 struct vcpu_vmx *vmx = to_vmx(vcpu);
3099
3100 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003101 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003102 free_vmcs(vmx->vmcs);
3103 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003104 }
3105}
3106
3107static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3108{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003109 struct vcpu_vmx *vmx = to_vmx(vcpu);
3110
Sheng Yang2384d2b2008-01-17 15:14:33 +08003111 spin_lock(&vmx_vpid_lock);
3112 if (vmx->vpid != 0)
3113 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3114 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003115 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003116 kfree(vmx->host_msrs);
3117 kfree(vmx->guest_msrs);
3118 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003119 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003120}
3121
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003122static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003123{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003124 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003125 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003126 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003127
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003128 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003129 return ERR_PTR(-ENOMEM);
3130
Sheng Yang2384d2b2008-01-17 15:14:33 +08003131 allocate_vpid(vmx);
3132
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003133 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3134 if (err)
3135 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003136
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003137 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003138 if (!vmx->guest_msrs) {
3139 err = -ENOMEM;
3140 goto uninit_vcpu;
3141 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003142
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003143 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3144 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003145 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003146
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003147 vmx->vmcs = alloc_vmcs();
3148 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003149 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003150
3151 vmcs_clear(vmx->vmcs);
3152
Avi Kivity15ad7142007-07-11 18:17:21 +03003153 cpu = get_cpu();
3154 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003155 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003156 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003157 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003158 if (err)
3159 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003160 if (vm_need_virtualize_apic_accesses(kvm))
3161 if (alloc_apic_access_page(kvm) != 0)
3162 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003163
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003164 if (vm_need_ept())
3165 if (alloc_identity_pagetable(kvm) != 0)
3166 goto free_vmcs;
3167
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003168 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003169
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003170free_vmcs:
3171 free_vmcs(vmx->vmcs);
3172free_msrs:
3173 kfree(vmx->host_msrs);
3174free_guest_msrs:
3175 kfree(vmx->guest_msrs);
3176uninit_vcpu:
3177 kvm_vcpu_uninit(&vmx->vcpu);
3178free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003179 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003180 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003181}
3182
Yang, Sheng002c7f72007-07-31 14:23:01 +03003183static void __init vmx_check_processor_compat(void *rtn)
3184{
3185 struct vmcs_config vmcs_conf;
3186
3187 *(int *)rtn = 0;
3188 if (setup_vmcs_config(&vmcs_conf) < 0)
3189 *(int *)rtn = -EIO;
3190 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3191 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3192 smp_processor_id());
3193 *(int *)rtn = -EIO;
3194 }
3195}
3196
Sheng Yang67253af2008-04-25 10:20:22 +08003197static int get_ept_level(void)
3198{
3199 return VMX_EPT_DEFAULT_GAW + 1;
3200}
3201
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003202static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203 .cpu_has_kvm_support = cpu_has_kvm_support,
3204 .disabled_by_bios = vmx_disabled_by_bios,
3205 .hardware_setup = hardware_setup,
3206 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003207 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208 .hardware_enable = hardware_enable,
3209 .hardware_disable = hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003210 .cpu_has_accelerated_tpr = cpu_has_vmx_virtualize_apic_accesses,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003211
3212 .vcpu_create = vmx_create_vcpu,
3213 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003214 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003215
Avi Kivity04d2cc72007-09-10 18:10:54 +03003216 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217 .vcpu_load = vmx_vcpu_load,
3218 .vcpu_put = vmx_vcpu_put,
3219
3220 .set_guest_debug = set_guest_debug,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003221 .guest_debug_pre = kvm_guest_debug_pre,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003222 .get_msr = vmx_get_msr,
3223 .set_msr = vmx_set_msr,
3224 .get_segment_base = vmx_get_segment_base,
3225 .get_segment = vmx_get_segment,
3226 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003227 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003229 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003230 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003231 .set_cr3 = vmx_set_cr3,
3232 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003233 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003234 .get_idt = vmx_get_idt,
3235 .set_idt = vmx_set_idt,
3236 .get_gdt = vmx_get_gdt,
3237 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003238 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003239 .get_rflags = vmx_get_rflags,
3240 .set_rflags = vmx_set_rflags,
3241
3242 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003243
Avi Kivity6aa8b732006-12-10 02:21:36 -08003244 .run = vmx_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003245 .handle_exit = kvm_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02003247 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003248 .get_irq = vmx_get_irq,
3249 .set_irq = vmx_inject_irq,
Avi Kivity298101d2007-11-25 13:41:11 +02003250 .queue_exception = vmx_queue_exception,
3251 .exception_injected = vmx_exception_injected,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003252 .inject_pending_irq = vmx_intr_assist,
3253 .inject_pending_vectors = do_interrupt_requests,
Izik Eiduscbc94022007-10-25 00:29:55 +02003254
3255 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003256 .get_tdp_level = get_ept_level,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003257};
3258
3259static int __init vmx_init(void)
3260{
Sheng Yang25c5f222008-03-28 13:18:56 +08003261 void *va;
He, Qingfdef3ad2007-04-30 09:45:24 +03003262 int r;
3263
3264 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3265 if (!vmx_io_bitmap_a)
3266 return -ENOMEM;
3267
3268 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3269 if (!vmx_io_bitmap_b) {
3270 r = -ENOMEM;
3271 goto out;
3272 }
3273
Sheng Yang25c5f222008-03-28 13:18:56 +08003274 vmx_msr_bitmap = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3275 if (!vmx_msr_bitmap) {
3276 r = -ENOMEM;
3277 goto out1;
3278 }
3279
He, Qingfdef3ad2007-04-30 09:45:24 +03003280 /*
3281 * Allow direct access to the PC debug port (it is often used for I/O
3282 * delays, but the vmexits simply slow things down).
3283 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003284 va = kmap(vmx_io_bitmap_a);
3285 memset(va, 0xff, PAGE_SIZE);
3286 clear_bit(0x80, va);
Avi Kivitycd0536d2007-05-08 11:34:07 +03003287 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003288
Sheng Yang25c5f222008-03-28 13:18:56 +08003289 va = kmap(vmx_io_bitmap_b);
3290 memset(va, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03003291 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03003292
Sheng Yang25c5f222008-03-28 13:18:56 +08003293 va = kmap(vmx_msr_bitmap);
3294 memset(va, 0xff, PAGE_SIZE);
3295 kunmap(vmx_msr_bitmap);
3296
Sheng Yang2384d2b2008-01-17 15:14:33 +08003297 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
3298
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003299 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003300 if (r)
Sheng Yang25c5f222008-03-28 13:18:56 +08003301 goto out2;
3302
3303 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_FS_BASE);
3304 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_GS_BASE);
3305 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_CS);
3306 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_ESP);
3307 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_EIP);
He, Qingfdef3ad2007-04-30 09:45:24 +03003308
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003309 if (vm_need_ept()) {
Sheng Yang14394422008-04-28 12:24:45 +08003310 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003311 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
3312 VMX_EPT_WRITABLE_MASK |
3313 VMX_EPT_DEFAULT_MT << VMX_EPT_MT_EPTE_SHIFT);
Sheng Yang534e38b2008-09-08 15:12:30 +08003314 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003315 VMX_EPT_EXECUTABLE_MASK);
3316 kvm_enable_tdp();
3317 } else
3318 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08003319
Avi Kivityc7addb92007-09-16 18:58:32 +02003320 if (bypass_guest_pf)
3321 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
3322
Sheng Yang14394422008-04-28 12:24:45 +08003323 ept_sync_global();
3324
He, Qingfdef3ad2007-04-30 09:45:24 +03003325 return 0;
3326
Sheng Yang25c5f222008-03-28 13:18:56 +08003327out2:
3328 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03003329out1:
3330 __free_page(vmx_io_bitmap_b);
3331out:
3332 __free_page(vmx_io_bitmap_a);
3333 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003334}
3335
3336static void __exit vmx_exit(void)
3337{
Sheng Yang25c5f222008-03-28 13:18:56 +08003338 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03003339 __free_page(vmx_io_bitmap_b);
3340 __free_page(vmx_io_bitmap_a);
3341
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003342 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003343}
3344
3345module_init(vmx_init)
3346module_exit(vmx_exit)