blob: ed4fe8e72ad093309aacc66d28b3ec2553dcd7b5 [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)
473 eb |= 1u << 1;
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{
738 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
739 nr | INTR_TYPE_EXCEPTION
Ryan Harper2e113842008-02-11 10:26:38 -0600740 | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0)
Avi Kivity298101d2007-11-25 13:41:11 +0200741 | INTR_INFO_VALID_MASK);
742 if (has_error_code)
743 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
744}
745
746static bool vmx_exception_injected(struct kvm_vcpu *vcpu)
747{
Avi Kivity35920a32008-07-03 14:50:12 +0300748 return false;
Avi Kivity298101d2007-11-25 13:41:11 +0200749}
750
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751/*
Eddie Donga75beee2007-05-17 18:55:15 +0300752 * Swap MSR entry in host/guest MSR entry array.
753 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200754#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000755static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300756{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400757 struct kvm_msr_entry tmp;
758
759 tmp = vmx->guest_msrs[to];
760 vmx->guest_msrs[to] = vmx->guest_msrs[from];
761 vmx->guest_msrs[from] = tmp;
762 tmp = vmx->host_msrs[to];
763 vmx->host_msrs[to] = vmx->host_msrs[from];
764 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300765}
Gabriel C54e11fa2007-08-01 16:23:10 +0200766#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300767
768/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300769 * Set up the vmcs to automatically save and restore system
770 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
771 * mode, as fiddling with msrs is very expensive.
772 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000773static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300774{
Eddie Dong2cc51562007-05-21 07:28:09 +0300775 int save_nmsrs;
Avi Kivitye38aea32007-04-19 13:22:48 +0300776
Avi Kivity33f9c502008-02-27 16:06:57 +0200777 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300778 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300779#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000780 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300781 int index;
782
Rusty Russell8b9cf982007-07-30 16:31:43 +1000783 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300784 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000785 move_msr_up(vmx, index, save_nmsrs++);
786 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300787 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000788 move_msr_up(vmx, index, save_nmsrs++);
789 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300790 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000791 move_msr_up(vmx, index, save_nmsrs++);
792 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300793 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000794 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300795 /*
796 * MSR_K6_STAR is only needed on long mode guests, and only
797 * if efer.sce is enabled.
798 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000799 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800800 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000801 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300802 }
Eddie Donga75beee2007-05-17 18:55:15 +0300803#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400804 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300805
Eddie Donga75beee2007-05-17 18:55:15 +0300806#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400807 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000808 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300809#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000810 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivitye38aea32007-04-19 13:22:48 +0300811}
812
813/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814 * reads and returns guest's timestamp counter "register"
815 * guest_tsc = host_tsc + tsc_offset -- 21.3
816 */
817static u64 guest_read_tsc(void)
818{
819 u64 host_tsc, tsc_offset;
820
821 rdtscll(host_tsc);
822 tsc_offset = vmcs_read64(TSC_OFFSET);
823 return host_tsc + tsc_offset;
824}
825
826/*
827 * writes 'guest_tsc' into guest's timestamp counter "register"
828 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
829 */
830static void guest_write_tsc(u64 guest_tsc)
831{
832 u64 host_tsc;
833
834 rdtscll(host_tsc);
835 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
836}
837
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838/*
839 * Reads an msr value (of 'msr_index') into 'pdata'.
840 * Returns 0 on success, non-0 otherwise.
841 * Assumes vcpu_load() was already called.
842 */
843static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
844{
845 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400846 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847
848 if (!pdata) {
849 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
850 return -EINVAL;
851 }
852
853 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800854#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855 case MSR_FS_BASE:
856 data = vmcs_readl(GUEST_FS_BASE);
857 break;
858 case MSR_GS_BASE:
859 data = vmcs_readl(GUEST_GS_BASE);
860 break;
861 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800862 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863#endif
864 case MSR_IA32_TIME_STAMP_COUNTER:
865 data = guest_read_tsc();
866 break;
867 case MSR_IA32_SYSENTER_CS:
868 data = vmcs_read32(GUEST_SYSENTER_CS);
869 break;
870 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200871 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800872 break;
873 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200874 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000877 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800878 if (msr) {
879 data = msr->data;
880 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800882 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883 }
884
885 *pdata = data;
886 return 0;
887}
888
889/*
890 * Writes msr value into into the appropriate "register".
891 * Returns 0 on success, non-0 otherwise.
892 * Assumes vcpu_load() was already called.
893 */
894static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
895{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400896 struct vcpu_vmx *vmx = to_vmx(vcpu);
897 struct kvm_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +0300898 int ret = 0;
899
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800901#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800902 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300903 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +0300904 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +0300905 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906 case MSR_FS_BASE:
907 vmcs_writel(GUEST_FS_BASE, data);
908 break;
909 case MSR_GS_BASE:
910 vmcs_writel(GUEST_GS_BASE, data);
911 break;
912#endif
913 case MSR_IA32_SYSENTER_CS:
914 vmcs_write32(GUEST_SYSENTER_CS, data);
915 break;
916 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200917 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918 break;
919 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200920 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200922 case MSR_IA32_TIME_STAMP_COUNTER:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923 guest_write_tsc(data);
924 break;
Chris Lalancetteefa67e02008-06-20 09:51:30 +0200925 case MSR_P6_PERFCTR0:
926 case MSR_P6_PERFCTR1:
927 case MSR_P6_EVNTSEL0:
928 case MSR_P6_EVNTSEL1:
929 /*
930 * Just discard all writes to the performance counters; this
931 * should keep both older linux and windows 64-bit guests
932 * happy
933 */
934 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", msr_index, data);
935
936 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937 default:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300938 vmx_load_host_state(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000939 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800940 if (msr) {
941 msr->data = data;
942 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300944 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 }
946
Eddie Dong2cc51562007-05-21 07:28:09 +0300947 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948}
949
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300950static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300952 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
953 switch (reg) {
954 case VCPU_REGS_RSP:
955 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
956 break;
957 case VCPU_REGS_RIP:
958 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
959 break;
960 default:
961 break;
962 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963}
964
965static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
966{
967 unsigned long dr7 = 0x400;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 int old_singlestep;
969
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 old_singlestep = vcpu->guest_debug.singlestep;
971
972 vcpu->guest_debug.enabled = dbg->enabled;
973 if (vcpu->guest_debug.enabled) {
974 int i;
975
976 dr7 |= 0x200; /* exact */
977 for (i = 0; i < 4; ++i) {
978 if (!dbg->breakpoints[i].enabled)
979 continue;
980 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
981 dr7 |= 2 << (i*2); /* global enable */
982 dr7 |= 0 << (i*4+16); /* execution breakpoint */
983 }
984
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 vcpu->guest_debug.singlestep = dbg->singlestep;
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300986 } else
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987 vcpu->guest_debug.singlestep = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988
989 if (old_singlestep && !vcpu->guest_debug.singlestep) {
990 unsigned long flags;
991
992 flags = vmcs_readl(GUEST_RFLAGS);
993 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
994 vmcs_writel(GUEST_RFLAGS, flags);
995 }
996
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300997 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 vmcs_writel(GUEST_DR7, dr7);
999
1000 return 0;
1001}
1002
Eddie Dong2a8067f2007-08-06 16:29:07 +03001003static int vmx_get_irq(struct kvm_vcpu *vcpu)
1004{
Avi Kivity1155f762007-11-22 11:30:47 +02001005 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +03001006 u32 idtv_info_field;
1007
Avi Kivity1155f762007-11-22 11:30:47 +02001008 idtv_info_field = vmx->idt_vectoring_info;
Eddie Dong2a8067f2007-08-06 16:29:07 +03001009 if (idtv_info_field & INTR_INFO_VALID_MASK) {
1010 if (is_external_interrupt(idtv_info_field))
1011 return idtv_info_field & VECTORING_INFO_VECTOR_MASK;
1012 else
Mike Dayd77c26f2007-10-08 09:02:08 -04001013 printk(KERN_DEBUG "pending exception: not handled yet\n");
Eddie Dong2a8067f2007-08-06 16:29:07 +03001014 }
1015 return -1;
1016}
1017
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018static __init int cpu_has_kvm_support(void)
1019{
1020 unsigned long ecx = cpuid_ecx(1);
1021 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
1022}
1023
1024static __init int vmx_disabled_by_bios(void)
1025{
1026 u64 msr;
1027
1028 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yangca60dfb2008-06-24 17:02:38 +08001029 return (msr & (IA32_FEATURE_CONTROL_LOCKED_BIT |
1030 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT))
1031 == IA32_FEATURE_CONTROL_LOCKED_BIT;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001032 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033}
1034
Avi Kivity774c47f2007-02-12 00:54:47 -08001035static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036{
1037 int cpu = raw_smp_processor_id();
1038 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1039 u64 old;
1040
Avi Kivity543e4242008-05-13 16:22:47 +03001041 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yangca60dfb2008-06-24 17:02:38 +08001043 if ((old & (IA32_FEATURE_CONTROL_LOCKED_BIT |
1044 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT))
1045 != (IA32_FEATURE_CONTROL_LOCKED_BIT |
1046 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001048 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yangca60dfb2008-06-24 17:02:38 +08001049 IA32_FEATURE_CONTROL_LOCKED_BIT |
1050 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT);
Rusty Russell66aee912007-07-17 23:34:16 +10001051 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001052 asm volatile (ASM_VMX_VMXON_RAX
1053 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001054 : "memory", "cc");
1055}
1056
Avi Kivity543e4242008-05-13 16:22:47 +03001057static void vmclear_local_vcpus(void)
1058{
1059 int cpu = raw_smp_processor_id();
1060 struct vcpu_vmx *vmx, *n;
1061
1062 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1063 local_vcpus_link)
1064 __vcpu_clear(vmx);
1065}
1066
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067static void hardware_disable(void *garbage)
1068{
Avi Kivity543e4242008-05-13 16:22:47 +03001069 vmclear_local_vcpus();
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001070 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eli Collinse693d712008-06-01 20:24:40 -07001071 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072}
1073
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001074static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001075 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076{
1077 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001078 u32 ctl = ctl_min | ctl_opt;
1079
1080 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1081
1082 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1083 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1084
1085 /* Ensure minimum (required) set of control bits are supported. */
1086 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001087 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001088
1089 *result = ctl;
1090 return 0;
1091}
1092
Yang, Sheng002c7f72007-07-31 14:23:01 +03001093static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001094{
1095 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001096 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001097 u32 _pin_based_exec_control = 0;
1098 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001099 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001100 u32 _vmexit_control = 0;
1101 u32 _vmentry_control = 0;
1102
1103 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001104 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001105 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1106 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001107 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001108
1109 min = CPU_BASED_HLT_EXITING |
1110#ifdef CONFIG_X86_64
1111 CPU_BASED_CR8_LOAD_EXITING |
1112 CPU_BASED_CR8_STORE_EXITING |
1113#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001114 CPU_BASED_CR3_LOAD_EXITING |
1115 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001116 CPU_BASED_USE_IO_BITMAPS |
1117 CPU_BASED_MOV_DR_EXITING |
1118 CPU_BASED_USE_TSC_OFFSETING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001119 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001120 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001121 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001122 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1123 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001124 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001125#ifdef CONFIG_X86_64
1126 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1127 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1128 ~CPU_BASED_CR8_STORE_EXITING;
1129#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001130 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001131 min2 = 0;
1132 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001133 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001134 SECONDARY_EXEC_ENABLE_VPID |
1135 SECONDARY_EXEC_ENABLE_EPT;
1136 if (adjust_vmx_controls(min2, opt2,
1137 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001138 &_cpu_based_2nd_exec_control) < 0)
1139 return -EIO;
1140 }
1141#ifndef CONFIG_X86_64
1142 if (!(_cpu_based_2nd_exec_control &
1143 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1144 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1145#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001146 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
1147 /* CR3 accesses don't need to cause VM Exits when EPT enabled */
1148 min &= ~(CPU_BASED_CR3_LOAD_EXITING |
1149 CPU_BASED_CR3_STORE_EXITING);
1150 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1151 &_cpu_based_exec_control) < 0)
1152 return -EIO;
1153 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1154 vmx_capability.ept, vmx_capability.vpid);
1155 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001156
1157 min = 0;
1158#ifdef CONFIG_X86_64
1159 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1160#endif
1161 opt = 0;
1162 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1163 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001164 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001165
1166 min = opt = 0;
1167 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1168 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001169 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001171 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001172
1173 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1174 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001175 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001176
1177#ifdef CONFIG_X86_64
1178 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1179 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001180 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001181#endif
1182
1183 /* Require Write-Back (WB) memory type for VMCS accesses. */
1184 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001185 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001186
Yang, Sheng002c7f72007-07-31 14:23:01 +03001187 vmcs_conf->size = vmx_msr_high & 0x1fff;
1188 vmcs_conf->order = get_order(vmcs_config.size);
1189 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001190
Yang, Sheng002c7f72007-07-31 14:23:01 +03001191 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1192 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001193 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001194 vmcs_conf->vmexit_ctrl = _vmexit_control;
1195 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001196
1197 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001198}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001199
1200static struct vmcs *alloc_vmcs_cpu(int cpu)
1201{
1202 int node = cpu_to_node(cpu);
1203 struct page *pages;
1204 struct vmcs *vmcs;
1205
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001206 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207 if (!pages)
1208 return NULL;
1209 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001210 memset(vmcs, 0, vmcs_config.size);
1211 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212 return vmcs;
1213}
1214
1215static struct vmcs *alloc_vmcs(void)
1216{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001217 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001218}
1219
1220static void free_vmcs(struct vmcs *vmcs)
1221{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001222 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223}
1224
Sam Ravnborg39959582007-06-01 00:47:13 -07001225static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226{
1227 int cpu;
1228
1229 for_each_online_cpu(cpu)
1230 free_vmcs(per_cpu(vmxarea, cpu));
1231}
1232
Avi Kivity6aa8b732006-12-10 02:21:36 -08001233static __init int alloc_kvm_area(void)
1234{
1235 int cpu;
1236
1237 for_each_online_cpu(cpu) {
1238 struct vmcs *vmcs;
1239
1240 vmcs = alloc_vmcs_cpu(cpu);
1241 if (!vmcs) {
1242 free_kvm_area();
1243 return -ENOMEM;
1244 }
1245
1246 per_cpu(vmxarea, cpu) = vmcs;
1247 }
1248 return 0;
1249}
1250
1251static __init int hardware_setup(void)
1252{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001253 if (setup_vmcs_config(&vmcs_config) < 0)
1254 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001255
1256 if (boot_cpu_has(X86_FEATURE_NX))
1257 kvm_enable_efer_bits(EFER_NX);
1258
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259 return alloc_kvm_area();
1260}
1261
1262static __exit void hardware_unsetup(void)
1263{
1264 free_kvm_area();
1265}
1266
Avi Kivity6aa8b732006-12-10 02:21:36 -08001267static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1268{
1269 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1270
Avi Kivity6af11b92007-03-19 13:18:10 +02001271 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272 vmcs_write16(sf->selector, save->selector);
1273 vmcs_writel(sf->base, save->base);
1274 vmcs_write32(sf->limit, save->limit);
1275 vmcs_write32(sf->ar_bytes, save->ar);
1276 } else {
1277 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1278 << AR_DPL_SHIFT;
1279 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1280 }
1281}
1282
1283static void enter_pmode(struct kvm_vcpu *vcpu)
1284{
1285 unsigned long flags;
1286
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001287 vcpu->arch.rmode.active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001288
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001289 vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base);
1290 vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit);
1291 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292
1293 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001294 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001295 flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296 vmcs_writel(GUEST_RFLAGS, flags);
1297
Rusty Russell66aee912007-07-17 23:34:16 +10001298 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1299 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300
1301 update_exception_bitmap(vcpu);
1302
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001303 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1304 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1305 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1306 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307
1308 vmcs_write16(GUEST_SS_SELECTOR, 0);
1309 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1310
1311 vmcs_write16(GUEST_CS_SELECTOR,
1312 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1313 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1314}
1315
Mike Dayd77c26f2007-10-08 09:02:08 -04001316static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001318 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001319 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1320 kvm->memslots[0].npages - 3;
1321 return base_gfn << PAGE_SHIFT;
1322 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001323 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324}
1325
1326static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1327{
1328 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1329
1330 save->selector = vmcs_read16(sf->selector);
1331 save->base = vmcs_readl(sf->base);
1332 save->limit = vmcs_read32(sf->limit);
1333 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001334 vmcs_write16(sf->selector, save->base >> 4);
1335 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 vmcs_write32(sf->limit, 0xffff);
1337 vmcs_write32(sf->ar_bytes, 0xf3);
1338}
1339
1340static void enter_rmode(struct kvm_vcpu *vcpu)
1341{
1342 unsigned long flags;
1343
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001344 vcpu->arch.rmode.active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001345
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001346 vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1348
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001349 vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1351
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001352 vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1354
1355 flags = vmcs_readl(GUEST_RFLAGS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001356 vcpu->arch.rmode.save_iopl
1357 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001359 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360
1361 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001362 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363 update_exception_bitmap(vcpu);
1364
1365 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1366 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1367 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1368
1369 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001370 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001371 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1372 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1374
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001375 fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1376 fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1377 fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1378 fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001379
Eddie Dong8668a3c2007-10-10 14:26:45 +08001380 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001381 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382}
1383
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001384#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385
1386static void enter_lmode(struct kvm_vcpu *vcpu)
1387{
1388 u32 guest_tr_ar;
1389
1390 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1391 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1392 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001393 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394 vmcs_write32(GUEST_TR_AR_BYTES,
1395 (guest_tr_ar & ~AR_TYPE_MASK)
1396 | AR_TYPE_BUSY_64_TSS);
1397 }
1398
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001399 vcpu->arch.shadow_efer |= EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400
Rusty Russell8b9cf982007-07-30 16:31:43 +10001401 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402 vmcs_write32(VM_ENTRY_CONTROLS,
1403 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001404 | VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405}
1406
1407static void exit_lmode(struct kvm_vcpu *vcpu)
1408{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001409 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410
1411 vmcs_write32(VM_ENTRY_CONTROLS,
1412 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001413 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001414}
1415
1416#endif
1417
Sheng Yang2384d2b2008-01-17 15:14:33 +08001418static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1419{
1420 vpid_sync_vcpu_all(to_vmx(vcpu));
Sheng Yang4e1096d2008-07-06 19:16:51 +08001421 if (vm_need_ept())
1422 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001423}
1424
Anthony Liguori25c4c272007-04-27 09:29:21 +03001425static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001426{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001427 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1428 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001429}
1430
Sheng Yang14394422008-04-28 12:24:45 +08001431static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1432{
1433 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1434 if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
1435 printk(KERN_ERR "EPT: Fail to load pdptrs!\n");
1436 return;
1437 }
1438 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1439 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1440 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1441 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1442 }
1443}
1444
1445static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1446
1447static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1448 unsigned long cr0,
1449 struct kvm_vcpu *vcpu)
1450{
1451 if (!(cr0 & X86_CR0_PG)) {
1452 /* From paging/starting to nonpaging */
1453 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001454 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001455 (CPU_BASED_CR3_LOAD_EXITING |
1456 CPU_BASED_CR3_STORE_EXITING));
1457 vcpu->arch.cr0 = cr0;
1458 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1459 *hw_cr0 |= X86_CR0_PE | X86_CR0_PG;
1460 *hw_cr0 &= ~X86_CR0_WP;
1461 } else if (!is_paging(vcpu)) {
1462 /* From nonpaging to paging */
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 if (!(vcpu->arch.cr0 & X86_CR0_WP))
1470 *hw_cr0 &= ~X86_CR0_WP;
1471 }
1472}
1473
1474static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1475 struct kvm_vcpu *vcpu)
1476{
1477 if (!is_paging(vcpu)) {
1478 *hw_cr4 &= ~X86_CR4_PAE;
1479 *hw_cr4 |= X86_CR4_PSE;
1480 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1481 *hw_cr4 &= ~X86_CR4_PAE;
1482}
1483
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1485{
Sheng Yang14394422008-04-28 12:24:45 +08001486 unsigned long hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) |
1487 KVM_VM_CR0_ALWAYS_ON;
1488
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001489 vmx_fpu_deactivate(vcpu);
1490
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001491 if (vcpu->arch.rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001492 enter_pmode(vcpu);
1493
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001494 if (!vcpu->arch.rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495 enter_rmode(vcpu);
1496
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001497#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001498 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001499 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001500 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001501 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502 exit_lmode(vcpu);
1503 }
1504#endif
1505
Sheng Yang14394422008-04-28 12:24:45 +08001506 if (vm_need_ept())
1507 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1508
Avi Kivity6aa8b732006-12-10 02:21:36 -08001509 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001510 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001511 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001512
Rusty Russell707d92fa2007-07-17 23:19:08 +10001513 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001514 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515}
1516
Sheng Yang14394422008-04-28 12:24:45 +08001517static u64 construct_eptp(unsigned long root_hpa)
1518{
1519 u64 eptp;
1520
1521 /* TODO write the value reading from MSR */
1522 eptp = VMX_EPT_DEFAULT_MT |
1523 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1524 eptp |= (root_hpa & PAGE_MASK);
1525
1526 return eptp;
1527}
1528
Avi Kivity6aa8b732006-12-10 02:21:36 -08001529static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1530{
Sheng Yang14394422008-04-28 12:24:45 +08001531 unsigned long guest_cr3;
1532 u64 eptp;
1533
1534 guest_cr3 = cr3;
1535 if (vm_need_ept()) {
1536 eptp = construct_eptp(cr3);
1537 vmcs_write64(EPT_POINTER, eptp);
1538 ept_sync_context(eptp);
1539 ept_load_pdptrs(vcpu);
1540 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
1541 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1542 }
1543
Sheng Yang2384d2b2008-01-17 15:14:33 +08001544 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001545 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001546 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001547 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001548}
1549
1550static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1551{
Sheng Yang14394422008-04-28 12:24:45 +08001552 unsigned long hw_cr4 = cr4 | (vcpu->arch.rmode.active ?
1553 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1554
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001555 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08001556 if (vm_need_ept())
1557 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1558
1559 vmcs_writel(CR4_READ_SHADOW, cr4);
1560 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001561}
1562
Avi Kivity6aa8b732006-12-10 02:21:36 -08001563static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1564{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001565 struct vcpu_vmx *vmx = to_vmx(vcpu);
1566 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001568 vcpu->arch.shadow_efer = efer;
Joerg Roedel9f62e192008-01-31 14:57:39 +01001569 if (!msr)
1570 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571 if (efer & EFER_LMA) {
1572 vmcs_write32(VM_ENTRY_CONTROLS,
1573 vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001574 VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001575 msr->data = efer;
1576
1577 } else {
1578 vmcs_write32(VM_ENTRY_CONTROLS,
1579 vmcs_read32(VM_ENTRY_CONTROLS) &
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001580 ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581
1582 msr->data = efer & ~EFER_LME;
1583 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001584 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001585}
1586
Avi Kivity6aa8b732006-12-10 02:21:36 -08001587static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1588{
1589 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1590
1591 return vmcs_readl(sf->base);
1592}
1593
1594static void vmx_get_segment(struct kvm_vcpu *vcpu,
1595 struct kvm_segment *var, int seg)
1596{
1597 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1598 u32 ar;
1599
1600 var->base = vmcs_readl(sf->base);
1601 var->limit = vmcs_read32(sf->limit);
1602 var->selector = vmcs_read16(sf->selector);
1603 ar = vmcs_read32(sf->ar_bytes);
1604 if (ar & AR_UNUSABLE_MASK)
1605 ar = 0;
1606 var->type = ar & 15;
1607 var->s = (ar >> 4) & 1;
1608 var->dpl = (ar >> 5) & 3;
1609 var->present = (ar >> 7) & 1;
1610 var->avl = (ar >> 12) & 1;
1611 var->l = (ar >> 13) & 1;
1612 var->db = (ar >> 14) & 1;
1613 var->g = (ar >> 15) & 1;
1614 var->unusable = (ar >> 16) & 1;
1615}
1616
Izik Eidus2e4d2652008-03-24 19:38:34 +02001617static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1618{
1619 struct kvm_segment kvm_seg;
1620
1621 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1622 return 0;
1623
1624 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1625 return 3;
1626
1627 vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
1628 return kvm_seg.selector & 3;
1629}
1630
Avi Kivity653e3102007-05-07 10:55:37 +03001631static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633 u32 ar;
1634
Avi Kivity653e3102007-05-07 10:55:37 +03001635 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001636 ar = 1 << 16;
1637 else {
1638 ar = var->type & 15;
1639 ar |= (var->s & 1) << 4;
1640 ar |= (var->dpl & 3) << 5;
1641 ar |= (var->present & 1) << 7;
1642 ar |= (var->avl & 1) << 12;
1643 ar |= (var->l & 1) << 13;
1644 ar |= (var->db & 1) << 14;
1645 ar |= (var->g & 1) << 15;
1646 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001647 if (ar == 0) /* a 0 value means unusable */
1648 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001649
1650 return ar;
1651}
1652
1653static void vmx_set_segment(struct kvm_vcpu *vcpu,
1654 struct kvm_segment *var, int seg)
1655{
1656 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1657 u32 ar;
1658
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001659 if (vcpu->arch.rmode.active && seg == VCPU_SREG_TR) {
1660 vcpu->arch.rmode.tr.selector = var->selector;
1661 vcpu->arch.rmode.tr.base = var->base;
1662 vcpu->arch.rmode.tr.limit = var->limit;
1663 vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001664 return;
1665 }
1666 vmcs_writel(sf->base, var->base);
1667 vmcs_write32(sf->limit, var->limit);
1668 vmcs_write16(sf->selector, var->selector);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001669 if (vcpu->arch.rmode.active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001670 /*
1671 * Hack real-mode segments into vm86 compatibility.
1672 */
1673 if (var->base == 0xffff0000 && var->selector == 0xf000)
1674 vmcs_writel(sf->base, 0xf0000);
1675 ar = 0xf3;
1676 } else
1677 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678 vmcs_write32(sf->ar_bytes, ar);
1679}
1680
Avi Kivity6aa8b732006-12-10 02:21:36 -08001681static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1682{
1683 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1684
1685 *db = (ar >> 14) & 1;
1686 *l = (ar >> 13) & 1;
1687}
1688
1689static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1690{
1691 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1692 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1693}
1694
1695static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1696{
1697 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1698 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1699}
1700
1701static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1702{
1703 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1704 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1705}
1706
1707static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1708{
1709 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1710 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1711}
1712
Mike Dayd77c26f2007-10-08 09:02:08 -04001713static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001715 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001716 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001717 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02001718 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001719
Izik Eidus195aefd2007-10-01 22:14:18 +02001720 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1721 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001722 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001723 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
1724 r = kvm_write_guest_page(kvm, fn++, &data, 0x66, sizeof(u16));
1725 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001726 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001727 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
1728 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001729 goto out;
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 = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001734 r = kvm_write_guest_page(kvm, fn, &data,
1735 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
1736 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02001737 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001738 goto out;
1739
1740 ret = 1;
1741out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001742 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001743}
1744
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001745static int init_rmode_identity_map(struct kvm *kvm)
1746{
1747 int i, r, ret;
1748 pfn_t identity_map_pfn;
1749 u32 tmp;
1750
1751 if (!vm_need_ept())
1752 return 1;
1753 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
1754 printk(KERN_ERR "EPT: identity-mapping pagetable "
1755 "haven't been allocated!\n");
1756 return 0;
1757 }
1758 if (likely(kvm->arch.ept_identity_pagetable_done))
1759 return 1;
1760 ret = 0;
1761 identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT;
1762 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
1763 if (r < 0)
1764 goto out;
1765 /* Set up identity-mapping pagetable for EPT in real mode */
1766 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
1767 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
1768 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
1769 r = kvm_write_guest_page(kvm, identity_map_pfn,
1770 &tmp, i * sizeof(tmp), sizeof(tmp));
1771 if (r < 0)
1772 goto out;
1773 }
1774 kvm->arch.ept_identity_pagetable_done = true;
1775 ret = 1;
1776out:
1777 return ret;
1778}
1779
Avi Kivity6aa8b732006-12-10 02:21:36 -08001780static void seg_setup(int seg)
1781{
1782 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1783
1784 vmcs_write16(sf->selector, 0);
1785 vmcs_writel(sf->base, 0);
1786 vmcs_write32(sf->limit, 0xffff);
1787 vmcs_write32(sf->ar_bytes, 0x93);
1788}
1789
Sheng Yangf78e0e22007-10-29 09:40:42 +08001790static int alloc_apic_access_page(struct kvm *kvm)
1791{
1792 struct kvm_userspace_memory_region kvm_userspace_mem;
1793 int r = 0;
1794
Izik Eidus72dc67a2008-02-10 18:04:15 +02001795 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001796 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001797 goto out;
1798 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
1799 kvm_userspace_mem.flags = 0;
1800 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
1801 kvm_userspace_mem.memory_size = PAGE_SIZE;
1802 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
1803 if (r)
1804 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001805
1806 down_read(&current->mm->mmap_sem);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001807 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001808 up_read(&current->mm->mmap_sem);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001809out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001810 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001811 return r;
1812}
1813
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001814static int alloc_identity_pagetable(struct kvm *kvm)
1815{
1816 struct kvm_userspace_memory_region kvm_userspace_mem;
1817 int r = 0;
1818
1819 down_write(&kvm->slots_lock);
1820 if (kvm->arch.ept_identity_pagetable)
1821 goto out;
1822 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
1823 kvm_userspace_mem.flags = 0;
1824 kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1825 kvm_userspace_mem.memory_size = PAGE_SIZE;
1826 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
1827 if (r)
1828 goto out;
1829
1830 down_read(&current->mm->mmap_sem);
1831 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
1832 VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT);
1833 up_read(&current->mm->mmap_sem);
1834out:
1835 up_write(&kvm->slots_lock);
1836 return r;
1837}
1838
Sheng Yang2384d2b2008-01-17 15:14:33 +08001839static void allocate_vpid(struct vcpu_vmx *vmx)
1840{
1841 int vpid;
1842
1843 vmx->vpid = 0;
1844 if (!enable_vpid || !cpu_has_vmx_vpid())
1845 return;
1846 spin_lock(&vmx_vpid_lock);
1847 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
1848 if (vpid < VMX_NR_VPIDS) {
1849 vmx->vpid = vpid;
1850 __set_bit(vpid, vmx_vpid_bitmap);
1851 }
1852 spin_unlock(&vmx_vpid_lock);
1853}
1854
Harvey Harrison8b2cf732008-04-27 12:14:13 -07001855static void vmx_disable_intercept_for_msr(struct page *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08001856{
1857 void *va;
1858
1859 if (!cpu_has_vmx_msr_bitmap())
1860 return;
1861
1862 /*
1863 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
1864 * have the write-low and read-high bitmap offsets the wrong way round.
1865 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
1866 */
1867 va = kmap(msr_bitmap);
1868 if (msr <= 0x1fff) {
1869 __clear_bit(msr, va + 0x000); /* read-low */
1870 __clear_bit(msr, va + 0x800); /* write-low */
1871 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
1872 msr &= 0x1fff;
1873 __clear_bit(msr, va + 0x400); /* read-high */
1874 __clear_bit(msr, va + 0xc00); /* write-high */
1875 }
1876 kunmap(msr_bitmap);
1877}
1878
Avi Kivity6aa8b732006-12-10 02:21:36 -08001879/*
1880 * Sets up the vmcs for emulated real mode.
1881 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001882static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001883{
1884 u32 host_sysenter_cs;
1885 u32 junk;
1886 unsigned long a;
1887 struct descriptor_table dt;
1888 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03001889 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001890 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001891
Avi Kivity6aa8b732006-12-10 02:21:36 -08001892 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03001893 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
1894 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001895
Sheng Yang25c5f222008-03-28 13:18:56 +08001896 if (cpu_has_vmx_msr_bitmap())
1897 vmcs_write64(MSR_BITMAP, page_to_phys(vmx_msr_bitmap));
1898
Avi Kivity6aa8b732006-12-10 02:21:36 -08001899 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
1900
Avi Kivity6aa8b732006-12-10 02:21:36 -08001901 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001902 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
1903 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001904
1905 exec_control = vmcs_config.cpu_based_exec_ctrl;
1906 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
1907 exec_control &= ~CPU_BASED_TPR_SHADOW;
1908#ifdef CONFIG_X86_64
1909 exec_control |= CPU_BASED_CR8_STORE_EXITING |
1910 CPU_BASED_CR8_LOAD_EXITING;
1911#endif
1912 }
Sheng Yangd56f5462008-04-25 10:13:16 +08001913 if (!vm_need_ept())
1914 exec_control |= CPU_BASED_CR3_STORE_EXITING |
1915 CPU_BASED_CR3_LOAD_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001916 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001917
Sheng Yang83ff3b92007-11-21 14:33:25 +08001918 if (cpu_has_secondary_exec_ctrls()) {
1919 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
1920 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
1921 exec_control &=
1922 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001923 if (vmx->vpid == 0)
1924 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yangd56f5462008-04-25 10:13:16 +08001925 if (!vm_need_ept())
1926 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang83ff3b92007-11-21 14:33:25 +08001927 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
1928 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08001929
Avi Kivityc7addb92007-09-16 18:58:32 +02001930 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
1931 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001932 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
1933
1934 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
1935 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
1936 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1937
1938 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
1939 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1940 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001941 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
1942 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001944#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001945 rdmsrl(MSR_FS_BASE, a);
1946 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
1947 rdmsrl(MSR_GS_BASE, a);
1948 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
1949#else
1950 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
1951 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
1952#endif
1953
1954 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
1955
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001956 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001957 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
1958
Mike Dayd77c26f2007-10-08 09:02:08 -04001959 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03001960 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03001961 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
1962 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
1963 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001964
1965 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
1966 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
1967 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
1968 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
1969 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
1970 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
1971
Avi Kivity6aa8b732006-12-10 02:21:36 -08001972 for (i = 0; i < NR_VMX_MSR; ++i) {
1973 u32 index = vmx_msr_index[i];
1974 u32 data_low, data_high;
1975 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001976 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001977
1978 if (rdmsr_safe(index, &data_low, &data_high) < 0)
1979 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08001980 if (wrmsr_safe(index, data_low, data_high) < 0)
1981 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001982 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001983 vmx->host_msrs[j].index = index;
1984 vmx->host_msrs[j].reserved = 0;
1985 vmx->host_msrs[j].data = data;
1986 vmx->guest_msrs[j] = vmx->host_msrs[j];
1987 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001988 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001989
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001990 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001991
1992 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001993 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
1994
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001995 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
1996 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
1997
Sheng Yangf78e0e22007-10-29 09:40:42 +08001998
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001999 return 0;
2000}
2001
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002002static int init_rmode(struct kvm *kvm)
2003{
2004 if (!init_rmode_tss(kvm))
2005 return 0;
2006 if (!init_rmode_identity_map(kvm))
2007 return 0;
2008 return 1;
2009}
2010
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002011static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2012{
2013 struct vcpu_vmx *vmx = to_vmx(vcpu);
2014 u64 msr;
2015 int ret;
2016
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002017 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002018 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002019 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002020 ret = -ENOMEM;
2021 goto out;
2022 }
2023
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002024 vmx->vcpu.arch.rmode.active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002025
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002026 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002027 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002028 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
2029 if (vmx->vcpu.vcpu_id == 0)
2030 msr |= MSR_IA32_APICBASE_BSP;
2031 kvm_set_apic_base(&vmx->vcpu, msr);
2032
2033 fx_init(&vmx->vcpu);
2034
2035 /*
2036 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2037 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2038 */
2039 if (vmx->vcpu.vcpu_id == 0) {
2040 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2041 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2042 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002043 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2044 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002045 }
2046 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
2047 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
2048
2049 seg_setup(VCPU_SREG_DS);
2050 seg_setup(VCPU_SREG_ES);
2051 seg_setup(VCPU_SREG_FS);
2052 seg_setup(VCPU_SREG_GS);
2053 seg_setup(VCPU_SREG_SS);
2054
2055 vmcs_write16(GUEST_TR_SELECTOR, 0);
2056 vmcs_writel(GUEST_TR_BASE, 0);
2057 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2058 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2059
2060 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2061 vmcs_writel(GUEST_LDTR_BASE, 0);
2062 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2063 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2064
2065 vmcs_write32(GUEST_SYSENTER_CS, 0);
2066 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2067 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2068
2069 vmcs_writel(GUEST_RFLAGS, 0x02);
2070 if (vmx->vcpu.vcpu_id == 0)
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002071 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002072 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002073 kvm_rip_write(vcpu, 0);
2074 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002075
2076 /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */
2077 vmcs_writel(GUEST_DR7, 0x400);
2078
2079 vmcs_writel(GUEST_GDTR_BASE, 0);
2080 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2081
2082 vmcs_writel(GUEST_IDTR_BASE, 0);
2083 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2084
2085 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2086 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2087 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2088
2089 guest_write_tsc(0);
2090
2091 /* Special registers */
2092 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2093
2094 setup_msrs(vmx);
2095
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2097
Sheng Yangf78e0e22007-10-29 09:40:42 +08002098 if (cpu_has_vmx_tpr_shadow()) {
2099 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2100 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2101 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002102 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002103 vmcs_write32(TPR_THRESHOLD, 0);
2104 }
2105
2106 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2107 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002108 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109
Sheng Yang2384d2b2008-01-17 15:14:33 +08002110 if (vmx->vpid != 0)
2111 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2112
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002113 vmx->vcpu.arch.cr0 = 0x60000010;
2114 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002115 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002116 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002117 vmx_fpu_activate(&vmx->vcpu);
2118 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119
Sheng Yang2384d2b2008-01-17 15:14:33 +08002120 vpid_sync_vcpu_all(vmx);
2121
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002122 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123
Avi Kivity6aa8b732006-12-10 02:21:36 -08002124out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002125 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002126 return ret;
2127}
2128
Eddie Dong85f455f2007-07-06 12:20:49 +03002129static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
2130{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002131 struct vcpu_vmx *vmx = to_vmx(vcpu);
2132
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002133 KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler);
2134
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002135 if (vcpu->arch.rmode.active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002136 vmx->rmode.irq.pending = true;
2137 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002138 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Avi Kivity9c5623e2007-11-08 18:19:20 +02002139 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2140 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2141 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002142 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002143 return;
2144 }
2145 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2146 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
2147}
2148
Sheng Yangf08864b2008-05-15 18:23:25 +08002149static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2150{
2151 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2152 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002153}
2154
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
2156{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002157 int word_index = __ffs(vcpu->arch.irq_summary);
2158 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159 int irq = word_index * BITS_PER_LONG + bit_index;
2160
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002161 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
2162 if (!vcpu->arch.irq_pending[word_index])
2163 clear_bit(word_index, &vcpu->arch.irq_summary);
Eddie Dong85f455f2007-07-06 12:20:49 +03002164 vmx_inject_irq(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165}
2166
Dor Laorc1150d82007-01-05 16:36:24 -08002167
2168static void do_interrupt_requests(struct kvm_vcpu *vcpu,
2169 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002170{
Dor Laorc1150d82007-01-05 16:36:24 -08002171 u32 cpu_based_vm_exec_control;
2172
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002173 vcpu->arch.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08002174 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2175 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
2176
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002177 if (vcpu->arch.interrupt_window_open &&
2178 vcpu->arch.irq_summary &&
Dor Laorc1150d82007-01-05 16:36:24 -08002179 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180 /*
Dor Laorc1150d82007-01-05 16:36:24 -08002181 * If interrupts enabled, and not blocked by sti or mov ss. Good.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002182 */
2183 kvm_do_inject_irq(vcpu);
Dor Laorc1150d82007-01-05 16:36:24 -08002184
2185 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002186 if (!vcpu->arch.interrupt_window_open &&
2187 (vcpu->arch.irq_summary || kvm_run->request_interrupt_window))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002188 /*
2189 * Interrupts blocked. Wait for unblock.
2190 */
Dor Laorc1150d82007-01-05 16:36:24 -08002191 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2192 else
2193 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2194 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002195}
2196
Izik Eiduscbc94022007-10-25 00:29:55 +02002197static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2198{
2199 int ret;
2200 struct kvm_userspace_memory_region tss_mem = {
2201 .slot = 8,
2202 .guest_phys_addr = addr,
2203 .memory_size = PAGE_SIZE * 3,
2204 .flags = 0,
2205 };
2206
2207 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2208 if (ret)
2209 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002210 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002211 return 0;
2212}
2213
Avi Kivity6aa8b732006-12-10 02:21:36 -08002214static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
2215{
2216 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
2217
2218 set_debugreg(dbg->bp[0], 0);
2219 set_debugreg(dbg->bp[1], 1);
2220 set_debugreg(dbg->bp[2], 2);
2221 set_debugreg(dbg->bp[3], 3);
2222
2223 if (dbg->singlestep) {
2224 unsigned long flags;
2225
2226 flags = vmcs_readl(GUEST_RFLAGS);
2227 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
2228 vmcs_writel(GUEST_RFLAGS, flags);
2229 }
2230}
2231
2232static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2233 int vec, u32 err_code)
2234{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002235 if (!vcpu->arch.rmode.active)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002236 return 0;
2237
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002238 /*
2239 * Instruction with address size override prefix opcode 0x67
2240 * Cause the #SS fault with 0 error code in VM86 mode.
2241 */
2242 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02002243 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244 return 1;
2245 return 0;
2246}
2247
2248static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2249{
Avi Kivity1155f762007-11-22 11:30:47 +02002250 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002251 u32 intr_info, error_code;
2252 unsigned long cr2, rip;
2253 u32 vect_info;
2254 enum emulation_result er;
2255
Avi Kivity1155f762007-11-22 11:30:47 +02002256 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2258
2259 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002260 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002262 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002263
Eddie Dong85f455f2007-07-06 12:20:49 +03002264 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002266 set_bit(irq, vcpu->arch.irq_pending);
2267 set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268 }
2269
Avi Kivity1b6269d2007-10-09 12:12:19 +02002270 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
2271 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002272
2273 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002274 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002275 return 1;
2276 }
2277
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002278 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08002279 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002280 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002281 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002282 return 1;
2283 }
2284
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002286 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002287 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002288 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2289 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002290 /* EPT won't cause page fault directly */
2291 if (vm_need_ept())
2292 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002293 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002294 KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2,
2295 (u32)((u64)cr2 >> 32), handler);
Avi Kivity577bdc42008-07-19 08:57:05 +03002296 if (vect_info & VECTORING_INFO_VALID_MASK)
2297 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002298 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299 }
2300
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002301 if (vcpu->arch.rmode.active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002302 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002303 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002304 if (vcpu->arch.halt_request) {
2305 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002306 return kvm_emulate_halt(vcpu);
2307 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002309 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310
Mike Dayd77c26f2007-10-08 09:02:08 -04002311 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) ==
2312 (INTR_TYPE_EXCEPTION | 1)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002313 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2314 return 0;
2315 }
2316 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2317 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
2318 kvm_run->ex.error_code = error_code;
2319 return 0;
2320}
2321
2322static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2323 struct kvm_run *kvm_run)
2324{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002325 ++vcpu->stat.irq_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002326 KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327 return 1;
2328}
2329
Avi Kivity988ad742007-02-12 00:54:36 -08002330static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2331{
2332 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2333 return 0;
2334}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002335
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2337{
He, Qingbfdaab02007-09-12 14:18:28 +08002338 unsigned long exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02002339 int size, down, in, string, rep;
2340 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341
Avi Kivity1165f5f2007-04-19 17:27:43 +03002342 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002343 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002344 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002345
2346 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002347 if (emulate_instruction(vcpu,
2348 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002349 return 0;
2350 return 1;
2351 }
2352
2353 size = (exit_qualification & 7) + 1;
2354 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002355 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002356 rep = (exit_qualification & 32) != 0;
2357 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002358
Laurent Vivier3090dd72007-08-05 10:43:32 +03002359 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360}
2361
Ingo Molnar102d8322007-02-19 14:37:47 +02002362static void
2363vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2364{
2365 /*
2366 * Patch in the VMCALL instruction:
2367 */
2368 hypercall[0] = 0x0f;
2369 hypercall[1] = 0x01;
2370 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002371}
2372
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2374{
He, Qingbfdaab02007-09-12 14:18:28 +08002375 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376 int cr;
2377 int reg;
2378
He, Qingbfdaab02007-09-12 14:18:28 +08002379 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380 cr = exit_qualification & 15;
2381 reg = (exit_qualification >> 8) & 15;
2382 switch ((exit_qualification >> 4) & 3) {
2383 case 0: /* mov to cr */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002384 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr,
2385 (u32)kvm_register_read(vcpu, reg),
2386 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
2387 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002388 switch (cr) {
2389 case 0:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002390 kvm_set_cr0(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391 skip_emulated_instruction(vcpu);
2392 return 1;
2393 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002394 kvm_set_cr3(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002395 skip_emulated_instruction(vcpu);
2396 return 1;
2397 case 4:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002398 kvm_set_cr4(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399 skip_emulated_instruction(vcpu);
2400 return 1;
2401 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002402 kvm_set_cr8(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002403 skip_emulated_instruction(vcpu);
Avi Kivitye5314062007-12-06 16:32:45 +02002404 if (irqchip_in_kernel(vcpu->kvm))
2405 return 1;
Yang, Sheng253abde2007-08-16 13:01:00 +03002406 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2407 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408 };
2409 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002410 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002411 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002412 vcpu->arch.cr0 &= ~X86_CR0_TS;
2413 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002414 vmx_fpu_activate(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002415 KVMTRACE_0D(CLTS, vcpu, handler);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002416 skip_emulated_instruction(vcpu);
2417 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002418 case 1: /*mov from cr*/
2419 switch (cr) {
2420 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002421 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002422 KVMTRACE_3D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002423 (u32)kvm_register_read(vcpu, reg),
2424 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002425 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426 skip_emulated_instruction(vcpu);
2427 return 1;
2428 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002429 kvm_register_write(vcpu, reg, kvm_get_cr8(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002430 KVMTRACE_2D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002431 (u32)kvm_register_read(vcpu, reg), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432 skip_emulated_instruction(vcpu);
2433 return 1;
2434 }
2435 break;
2436 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002437 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438
2439 skip_emulated_instruction(vcpu);
2440 return 1;
2441 default:
2442 break;
2443 }
2444 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002445 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002446 (int)(exit_qualification >> 4) & 3, cr);
2447 return 0;
2448}
2449
2450static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2451{
He, Qingbfdaab02007-09-12 14:18:28 +08002452 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453 unsigned long val;
2454 int dr, reg;
2455
2456 /*
2457 * FIXME: this code assumes the host is debugging the guest.
2458 * need to deal with guest debugging itself too.
2459 */
He, Qingbfdaab02007-09-12 14:18:28 +08002460 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002461 dr = exit_qualification & 7;
2462 reg = (exit_qualification >> 8) & 15;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463 if (exit_qualification & 16) {
2464 /* mov from dr */
2465 switch (dr) {
2466 case 6:
2467 val = 0xffff0ff0;
2468 break;
2469 case 7:
2470 val = 0x400;
2471 break;
2472 default:
2473 val = 0;
2474 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002475 kvm_register_write(vcpu, reg, val);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002476 KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002477 } else {
2478 /* mov to dr */
2479 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002480 skip_emulated_instruction(vcpu);
2481 return 1;
2482}
2483
2484static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2485{
Avi Kivity06465c52007-02-28 20:46:53 +02002486 kvm_emulate_cpuid(vcpu);
2487 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002488}
2489
2490static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2491{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002492 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002493 u64 data;
2494
2495 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002496 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002497 return 1;
2498 }
2499
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002500 KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32),
2501 handler);
2502
Avi Kivity6aa8b732006-12-10 02:21:36 -08002503 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002504 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
2505 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002506 skip_emulated_instruction(vcpu);
2507 return 1;
2508}
2509
2510static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2511{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002512 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
2513 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
2514 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002516 KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32),
2517 handler);
2518
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002520 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521 return 1;
2522 }
2523
2524 skip_emulated_instruction(vcpu);
2525 return 1;
2526}
2527
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002528static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2529 struct kvm_run *kvm_run)
2530{
2531 return 1;
2532}
2533
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2535 struct kvm_run *kvm_run)
2536{
Eddie Dong85f455f2007-07-06 12:20:49 +03002537 u32 cpu_based_vm_exec_control;
2538
2539 /* clear pending irq */
2540 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2541 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2542 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002543
2544 KVMTRACE_0D(PEND_INTR, vcpu, handler);
2545
Dor Laorc1150d82007-01-05 16:36:24 -08002546 /*
2547 * If the user space waits to inject interrupts, exit as soon as
2548 * possible
2549 */
2550 if (kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002551 !vcpu->arch.irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002552 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002553 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002554 return 0;
2555 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002556 return 1;
2557}
2558
2559static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2560{
2561 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002562 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002563}
2564
Ingo Molnarc21415e2007-02-19 14:37:47 +02002565static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2566{
Dor Laor510043d2007-02-19 18:25:43 +02002567 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002568 kvm_emulate_hypercall(vcpu);
2569 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02002570}
2571
Eddie Donge5edaa02007-11-11 12:28:35 +02002572static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2573{
2574 skip_emulated_instruction(vcpu);
2575 /* TODO: Add support for VT-d/pass-through device */
2576 return 1;
2577}
2578
Sheng Yangf78e0e22007-10-29 09:40:42 +08002579static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2580{
2581 u64 exit_qualification;
2582 enum emulation_result er;
2583 unsigned long offset;
2584
2585 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2586 offset = exit_qualification & 0xffful;
2587
2588 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
2589
2590 if (er != EMULATE_DONE) {
2591 printk(KERN_ERR
2592 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
2593 offset);
2594 return -ENOTSUPP;
2595 }
2596 return 1;
2597}
2598
Izik Eidus37817f22008-03-24 23:14:53 +02002599static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2600{
2601 unsigned long exit_qualification;
2602 u16 tss_selector;
2603 int reason;
2604
2605 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
2606
2607 reason = (u32)exit_qualification >> 30;
2608 tss_selector = exit_qualification;
2609
2610 return kvm_task_switch(vcpu, tss_selector, reason);
2611}
2612
Sheng Yang14394422008-04-28 12:24:45 +08002613static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2614{
2615 u64 exit_qualification;
2616 enum emulation_result er;
2617 gpa_t gpa;
2618 unsigned long hva;
2619 int gla_validity;
2620 int r;
2621
2622 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2623
2624 if (exit_qualification & (1 << 6)) {
2625 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
2626 return -ENOTSUPP;
2627 }
2628
2629 gla_validity = (exit_qualification >> 7) & 0x3;
2630 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
2631 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
2632 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
2633 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
2634 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
2635 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
2636 (long unsigned int)exit_qualification);
2637 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2638 kvm_run->hw.hardware_exit_reason = 0;
2639 return -ENOTSUPP;
2640 }
2641
2642 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
2643 hva = gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT);
2644 if (!kvm_is_error_hva(hva)) {
2645 r = kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
2646 if (r < 0) {
2647 printk(KERN_ERR "EPT: Not enough memory!\n");
2648 return -ENOMEM;
2649 }
2650 return 1;
2651 } else {
2652 /* must be MMIO */
2653 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
2654
2655 if (er == EMULATE_FAIL) {
2656 printk(KERN_ERR
2657 "EPT: Fail to handle EPT violation vmexit!er is %d\n",
2658 er);
2659 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
2660 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
2661 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
2662 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
2663 (long unsigned int)exit_qualification);
2664 return -ENOTSUPP;
2665 } else if (er == EMULATE_DO_MMIO)
2666 return 0;
2667 }
2668 return 1;
2669}
2670
Sheng Yangf08864b2008-05-15 18:23:25 +08002671static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2672{
2673 u32 cpu_based_vm_exec_control;
2674
2675 /* clear pending NMI */
2676 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2677 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
2678 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2679 ++vcpu->stat.nmi_window_exits;
2680
2681 return 1;
2682}
2683
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684/*
2685 * The exit handlers return 1 if the exit was handled fully and guest execution
2686 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2687 * to be done to userspace and return 0.
2688 */
2689static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
2690 struct kvm_run *kvm_run) = {
2691 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
2692 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08002693 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08002694 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002696 [EXIT_REASON_CR_ACCESS] = handle_cr,
2697 [EXIT_REASON_DR_ACCESS] = handle_dr,
2698 [EXIT_REASON_CPUID] = handle_cpuid,
2699 [EXIT_REASON_MSR_READ] = handle_rdmsr,
2700 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
2701 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
2702 [EXIT_REASON_HLT] = handle_halt,
Ingo Molnarc21415e2007-02-19 14:37:47 +02002703 [EXIT_REASON_VMCALL] = handle_vmcall,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002704 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
2705 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02002706 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02002707 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Sheng Yang14394422008-04-28 12:24:45 +08002708 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002709};
2710
2711static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04002712 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002713
2714/*
2715 * The guest has exited. See if we can fix it or if we need userspace
2716 * assistance.
2717 */
2718static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2719{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002720 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity29bd8a72007-09-10 17:27:03 +03002721 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1155f762007-11-22 11:30:47 +02002722 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03002723
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002724 KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)kvm_rip_read(vcpu),
2725 (u32)((u64)kvm_rip_read(vcpu) >> 32), entryexit);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002726
Sheng Yang14394422008-04-28 12:24:45 +08002727 /* Access CR3 don't cause VMExit in paging mode, so we need
2728 * to sync with guest real CR3. */
2729 if (vm_need_ept() && is_paging(vcpu)) {
2730 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2731 ept_load_pdptrs(vcpu);
2732 }
2733
Avi Kivity29bd8a72007-09-10 17:27:03 +03002734 if (unlikely(vmx->fail)) {
2735 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2736 kvm_run->fail_entry.hardware_entry_failure_reason
2737 = vmcs_read32(VM_INSTRUCTION_ERROR);
2738 return 0;
2739 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002740
Mike Dayd77c26f2007-10-08 09:02:08 -04002741 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08002742 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
2743 exit_reason != EXIT_REASON_EPT_VIOLATION))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002744 printk(KERN_WARNING "%s: unexpected, valid vectoring info and "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002745 "exit reason is 0x%x\n", __func__, exit_reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002746 if (exit_reason < kvm_vmx_max_exit_handlers
2747 && kvm_vmx_exit_handlers[exit_reason])
2748 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
2749 else {
2750 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2751 kvm_run->hw.hardware_exit_reason = exit_reason;
2752 }
2753 return 0;
2754}
2755
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002756static void update_tpr_threshold(struct kvm_vcpu *vcpu)
2757{
2758 int max_irr, tpr;
2759
2760 if (!vm_need_tpr_shadow(vcpu->kvm))
2761 return;
2762
2763 if (!kvm_lapic_enabled(vcpu) ||
2764 ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
2765 vmcs_write32(TPR_THRESHOLD, 0);
2766 return;
2767 }
2768
2769 tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
2770 vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
2771}
2772
Eddie Dong85f455f2007-07-06 12:20:49 +03002773static void enable_irq_window(struct kvm_vcpu *vcpu)
2774{
2775 u32 cpu_based_vm_exec_control;
2776
2777 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2778 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2779 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2780}
2781
Sheng Yangf08864b2008-05-15 18:23:25 +08002782static void enable_nmi_window(struct kvm_vcpu *vcpu)
2783{
2784 u32 cpu_based_vm_exec_control;
2785
2786 if (!cpu_has_virtual_nmis())
2787 return;
2788
2789 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2790 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2791 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2792}
2793
2794static int vmx_nmi_enabled(struct kvm_vcpu *vcpu)
2795{
2796 u32 guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2797 return !(guest_intr & (GUEST_INTR_STATE_NMI |
2798 GUEST_INTR_STATE_MOV_SS |
2799 GUEST_INTR_STATE_STI));
2800}
2801
2802static int vmx_irq_enabled(struct kvm_vcpu *vcpu)
2803{
2804 u32 guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2805 return (!(guest_intr & (GUEST_INTR_STATE_MOV_SS |
2806 GUEST_INTR_STATE_STI)) &&
2807 (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF));
2808}
2809
2810static void enable_intr_window(struct kvm_vcpu *vcpu)
2811{
2812 if (vcpu->arch.nmi_pending)
2813 enable_nmi_window(vcpu);
2814 else if (kvm_cpu_has_interrupt(vcpu))
2815 enable_irq_window(vcpu);
2816}
2817
Avi Kivitycf393f72008-07-01 16:20:21 +03002818static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
2819{
2820 u32 exit_intr_info;
Avi Kivity668f6122008-07-02 09:28:55 +03002821 u32 idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03002822 bool unblock_nmi;
2823 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03002824 int type;
2825 bool idtv_info_valid;
Avi Kivity35920a32008-07-03 14:50:12 +03002826 u32 error;
Avi Kivitycf393f72008-07-01 16:20:21 +03002827
2828 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2829 if (cpu_has_virtual_nmis()) {
2830 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
2831 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
2832 /*
2833 * SDM 3: 25.7.1.2
2834 * Re-set bit "block by NMI" before VM entry if vmexit caused by
2835 * a guest IRET fault.
2836 */
2837 if (unblock_nmi && vector != DF_VECTOR)
2838 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
2839 GUEST_INTR_STATE_NMI);
2840 }
Avi Kivity668f6122008-07-02 09:28:55 +03002841
2842 idt_vectoring_info = vmx->idt_vectoring_info;
2843 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
2844 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
2845 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
2846 if (vmx->vcpu.arch.nmi_injected) {
2847 /*
2848 * SDM 3: 25.7.1.2
2849 * Clear bit "block by NMI" before VM entry if a NMI delivery
2850 * faulted.
2851 */
2852 if (idtv_info_valid && type == INTR_TYPE_NMI_INTR)
2853 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
2854 GUEST_INTR_STATE_NMI);
2855 else
2856 vmx->vcpu.arch.nmi_injected = false;
2857 }
Avi Kivity35920a32008-07-03 14:50:12 +03002858 kvm_clear_exception_queue(&vmx->vcpu);
2859 if (idtv_info_valid && type == INTR_TYPE_EXCEPTION) {
2860 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
2861 error = vmcs_read32(IDT_VECTORING_ERROR_CODE);
2862 kvm_queue_exception_e(&vmx->vcpu, vector, error);
2863 } else
2864 kvm_queue_exception(&vmx->vcpu, vector);
2865 vmx->idt_vectoring_info = 0;
2866 }
Avi Kivitycf393f72008-07-01 16:20:21 +03002867}
2868
Eddie Dong85f455f2007-07-06 12:20:49 +03002869static void vmx_intr_assist(struct kvm_vcpu *vcpu)
2870{
Avi Kivity1155f762007-11-22 11:30:47 +02002871 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity668f6122008-07-02 09:28:55 +03002872 u32 idtv_info_field, intr_info_field;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002873 int vector;
Eddie Dong85f455f2007-07-06 12:20:49 +03002874
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002875 update_tpr_threshold(vcpu);
2876
Eddie Dong85f455f2007-07-06 12:20:49 +03002877 intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
Avi Kivity1155f762007-11-22 11:30:47 +02002878 idtv_info_field = vmx->idt_vectoring_info;
Eddie Dong85f455f2007-07-06 12:20:49 +03002879 if (intr_info_field & INTR_INFO_VALID_MASK) {
2880 if (idtv_info_field & INTR_INFO_VALID_MASK) {
2881 /* TODO: fault when IDT_Vectoring */
Ryan Harper9584bf22007-12-13 10:21:10 -06002882 if (printk_ratelimit())
2883 printk(KERN_ERR "Fault when IDT_Vectoring\n");
Eddie Dong85f455f2007-07-06 12:20:49 +03002884 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002885 enable_intr_window(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002886 return;
2887 }
2888 if (unlikely(idtv_info_field & INTR_INFO_VALID_MASK)) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002889 if ((idtv_info_field & VECTORING_INFO_TYPE_MASK)
2890 == INTR_TYPE_EXT_INTR
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002891 && vcpu->arch.rmode.active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002892 u8 vect = idtv_info_field & VECTORING_INFO_VECTOR_MASK;
2893
2894 vmx_inject_irq(vcpu, vect);
Sheng Yangf08864b2008-05-15 18:23:25 +08002895 enable_intr_window(vcpu);
Avi Kivity9c8cba32007-11-22 11:42:59 +02002896 return;
2897 }
2898
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002899 KVMTRACE_1D(REDELIVER_EVT, vcpu, idtv_info_field, handler);
2900
Sheng Yangf08864b2008-05-15 18:23:25 +08002901 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, idtv_info_field
2902 & ~INTR_INFO_RESVD_BITS_MASK);
Eddie Dong85f455f2007-07-06 12:20:49 +03002903 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2904 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
2905
Ryan Harper2e113842008-02-11 10:26:38 -06002906 if (unlikely(idtv_info_field & INTR_INFO_DELIVER_CODE_MASK))
Eddie Dong85f455f2007-07-06 12:20:49 +03002907 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2908 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Sheng Yangf08864b2008-05-15 18:23:25 +08002909 enable_intr_window(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002910 return;
2911 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002912 if (cpu_has_virtual_nmis()) {
Avi Kivity668f6122008-07-02 09:28:55 +03002913 if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
2914 if (vmx_nmi_enabled(vcpu)) {
2915 vcpu->arch.nmi_pending = false;
2916 vcpu->arch.nmi_injected = true;
2917 } else {
2918 enable_intr_window(vcpu);
2919 return;
2920 }
2921 }
2922 if (vcpu->arch.nmi_injected) {
2923 vmx_inject_nmi(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08002924 enable_intr_window(vcpu);
2925 return;
2926 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002927 }
2928 if (!kvm_cpu_has_interrupt(vcpu))
Eddie Dong85f455f2007-07-06 12:20:49 +03002929 return;
Sheng Yangf08864b2008-05-15 18:23:25 +08002930 if (vmx_irq_enabled(vcpu)) {
Eddie Dong1b9778d2007-09-03 16:56:58 +03002931 vector = kvm_cpu_get_interrupt(vcpu);
2932 vmx_inject_irq(vcpu, vector);
2933 kvm_timer_intr_post(vcpu, vector);
2934 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03002935 enable_irq_window(vcpu);
2936}
2937
Avi Kivity9c8cba32007-11-22 11:42:59 +02002938/*
2939 * Failure to inject an interrupt should give us the information
2940 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
2941 * when fetching the interrupt redirection bitmap in the real-mode
2942 * tss, this doesn't happen. So we do it ourselves.
2943 */
2944static void fixup_rmode_irq(struct vcpu_vmx *vmx)
2945{
2946 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002947 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02002948 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002949 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02002950 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
2951 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
2952 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
2953 return;
2954 }
2955 vmx->idt_vectoring_info =
2956 VECTORING_INFO_VALID_MASK
2957 | INTR_TYPE_EXT_INTR
2958 | vmx->rmode.irq.vector;
2959}
2960
Avi Kivity04d2cc72007-09-10 18:10:54 +03002961static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002962{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002963 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1b6269d2007-10-09 12:12:19 +02002964 u32 intr_info;
Avi Kivitye6adf282007-04-30 16:07:54 +03002965
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002966 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
2967 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
2968 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
2969 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
2970
Avi Kivitye6adf282007-04-30 16:07:54 +03002971 /*
2972 * Loading guest fpu may have cleared host cr0.ts
2973 */
2974 vmcs_writel(HOST_CR0, read_cr0());
2975
Mike Dayd77c26f2007-10-08 09:02:08 -04002976 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08002977 /* Store host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002978#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02002979 "push %%rdx; push %%rbp;"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002980 "push %%rcx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002981#else
Laurent Vivierff593e52007-10-25 14:18:55 +02002982 "push %%edx; push %%ebp;"
2983 "push %%ecx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002984#endif
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 Kivity05b3e0c2006-12-13 00:33:45 -08002989#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02002990 "mov %c[cr2](%0), %%rax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002991 "mov %%rax, %%cr2 \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02002992 "mov %c[rax](%0), %%rax \n\t"
2993 "mov %c[rbx](%0), %%rbx \n\t"
2994 "mov %c[rdx](%0), %%rdx \n\t"
2995 "mov %c[rsi](%0), %%rsi \n\t"
2996 "mov %c[rdi](%0), %%rdi \n\t"
2997 "mov %c[rbp](%0), %%rbp \n\t"
2998 "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"
3006 "mov %c[rcx](%0), %%rcx \n\t" /* kills %0 (rcx) */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003007#else
Avi Kivitye08aa782007-11-15 18:06:18 +02003008 "mov %c[cr2](%0), %%eax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003009 "mov %%eax, %%cr2 \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003010 "mov %c[rax](%0), %%eax \n\t"
3011 "mov %c[rbx](%0), %%ebx \n\t"
3012 "mov %c[rdx](%0), %%edx \n\t"
3013 "mov %c[rsi](%0), %%esi \n\t"
3014 "mov %c[rdi](%0), %%edi \n\t"
3015 "mov %c[rbp](%0), %%ebp \n\t"
3016 "mov %c[rcx](%0), %%ecx \n\t" /* kills %0 (ecx) */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003017#endif
3018 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003019 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003020 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003021 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003022 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003023 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003024 /* Save guest registers, load host registers, keep flags */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003025#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003026 "xchg %0, (%%rsp) \n\t"
3027 "mov %%rax, %c[rax](%0) \n\t"
3028 "mov %%rbx, %c[rbx](%0) \n\t"
3029 "pushq (%%rsp); popq %c[rcx](%0) \n\t"
3030 "mov %%rdx, %c[rdx](%0) \n\t"
3031 "mov %%rsi, %c[rsi](%0) \n\t"
3032 "mov %%rdi, %c[rdi](%0) \n\t"
3033 "mov %%rbp, %c[rbp](%0) \n\t"
3034 "mov %%r8, %c[r8](%0) \n\t"
3035 "mov %%r9, %c[r9](%0) \n\t"
3036 "mov %%r10, %c[r10](%0) \n\t"
3037 "mov %%r11, %c[r11](%0) \n\t"
3038 "mov %%r12, %c[r12](%0) \n\t"
3039 "mov %%r13, %c[r13](%0) \n\t"
3040 "mov %%r14, %c[r14](%0) \n\t"
3041 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003042 "mov %%cr2, %%rax \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003043 "mov %%rax, %c[cr2](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003044
Avi Kivitye08aa782007-11-15 18:06:18 +02003045 "pop %%rbp; pop %%rbp; pop %%rdx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003046#else
Avi Kivitye08aa782007-11-15 18:06:18 +02003047 "xchg %0, (%%esp) \n\t"
3048 "mov %%eax, %c[rax](%0) \n\t"
3049 "mov %%ebx, %c[rbx](%0) \n\t"
3050 "pushl (%%esp); popl %c[rcx](%0) \n\t"
3051 "mov %%edx, %c[rdx](%0) \n\t"
3052 "mov %%esi, %c[rsi](%0) \n\t"
3053 "mov %%edi, %c[rdi](%0) \n\t"
3054 "mov %%ebp, %c[rbp](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003055 "mov %%cr2, %%eax \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003056 "mov %%eax, %c[cr2](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003057
Avi Kivitye08aa782007-11-15 18:06:18 +02003058 "pop %%ebp; pop %%ebp; pop %%edx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003059#endif
Avi Kivitye08aa782007-11-15 18:06:18 +02003060 "setbe %c[fail](%0) \n\t"
3061 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3062 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3063 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003064 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3065 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3066 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3067 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3068 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3069 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3070 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003071#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003072 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3073 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3074 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3075 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3076 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3077 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3078 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3079 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003080#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003081 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003082 : "cc", "memory"
3083#ifdef CONFIG_X86_64
3084 , "rbx", "rdi", "rsi"
3085 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Laurent Vivierff593e52007-10-25 14:18:55 +02003086#else
3087 , "ebx", "edi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02003088#endif
3089 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003090
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003091 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
3092 vcpu->arch.regs_dirty = 0;
3093
Avi Kivity1155f762007-11-22 11:30:47 +02003094 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003095 if (vmx->rmode.irq.pending)
3096 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003097
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003098 vcpu->arch.interrupt_window_open =
Sheng Yangf08864b2008-05-15 18:23:25 +08003099 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
3100 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)) == 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003101
Mike Dayd77c26f2007-10-08 09:02:08 -04003102 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003103 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003104
3105 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
3106
3107 /* We need to handle NMIs before interrupts are enabled */
Sheng Yangf08864b2008-05-15 18:23:25 +08003108 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200 &&
3109 (intr_info & INTR_INFO_VALID_MASK)) {
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003110 KVMTRACE_0D(NMI, vcpu, handler);
Avi Kivity1b6269d2007-10-09 12:12:19 +02003111 asm("int $2");
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003112 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003113
3114 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003115}
3116
Avi Kivity6aa8b732006-12-10 02:21:36 -08003117static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3118{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003119 struct vcpu_vmx *vmx = to_vmx(vcpu);
3120
3121 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003122 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003123 free_vmcs(vmx->vmcs);
3124 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003125 }
3126}
3127
3128static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3129{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003130 struct vcpu_vmx *vmx = to_vmx(vcpu);
3131
Sheng Yang2384d2b2008-01-17 15:14:33 +08003132 spin_lock(&vmx_vpid_lock);
3133 if (vmx->vpid != 0)
3134 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3135 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003136 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003137 kfree(vmx->host_msrs);
3138 kfree(vmx->guest_msrs);
3139 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003140 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003141}
3142
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003143static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003144{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003145 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003146 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003147 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003149 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003150 return ERR_PTR(-ENOMEM);
3151
Sheng Yang2384d2b2008-01-17 15:14:33 +08003152 allocate_vpid(vmx);
3153
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003154 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3155 if (err)
3156 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003157
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003158 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003159 if (!vmx->guest_msrs) {
3160 err = -ENOMEM;
3161 goto uninit_vcpu;
3162 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003163
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003164 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3165 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003166 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003167
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003168 vmx->vmcs = alloc_vmcs();
3169 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003170 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003171
3172 vmcs_clear(vmx->vmcs);
3173
Avi Kivity15ad7142007-07-11 18:17:21 +03003174 cpu = get_cpu();
3175 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003176 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003177 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003178 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003179 if (err)
3180 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003181 if (vm_need_virtualize_apic_accesses(kvm))
3182 if (alloc_apic_access_page(kvm) != 0)
3183 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003184
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003185 if (vm_need_ept())
3186 if (alloc_identity_pagetable(kvm) != 0)
3187 goto free_vmcs;
3188
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003189 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003190
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003191free_vmcs:
3192 free_vmcs(vmx->vmcs);
3193free_msrs:
3194 kfree(vmx->host_msrs);
3195free_guest_msrs:
3196 kfree(vmx->guest_msrs);
3197uninit_vcpu:
3198 kvm_vcpu_uninit(&vmx->vcpu);
3199free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003200 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003201 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003202}
3203
Yang, Sheng002c7f72007-07-31 14:23:01 +03003204static void __init vmx_check_processor_compat(void *rtn)
3205{
3206 struct vmcs_config vmcs_conf;
3207
3208 *(int *)rtn = 0;
3209 if (setup_vmcs_config(&vmcs_conf) < 0)
3210 *(int *)rtn = -EIO;
3211 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3212 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3213 smp_processor_id());
3214 *(int *)rtn = -EIO;
3215 }
3216}
3217
Sheng Yang67253af2008-04-25 10:20:22 +08003218static int get_ept_level(void)
3219{
3220 return VMX_EPT_DEFAULT_GAW + 1;
3221}
3222
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003223static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003224 .cpu_has_kvm_support = cpu_has_kvm_support,
3225 .disabled_by_bios = vmx_disabled_by_bios,
3226 .hardware_setup = hardware_setup,
3227 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003228 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003229 .hardware_enable = hardware_enable,
3230 .hardware_disable = hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003231 .cpu_has_accelerated_tpr = cpu_has_vmx_virtualize_apic_accesses,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003232
3233 .vcpu_create = vmx_create_vcpu,
3234 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003235 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003236
Avi Kivity04d2cc72007-09-10 18:10:54 +03003237 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003238 .vcpu_load = vmx_vcpu_load,
3239 .vcpu_put = vmx_vcpu_put,
3240
3241 .set_guest_debug = set_guest_debug,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003242 .guest_debug_pre = kvm_guest_debug_pre,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003243 .get_msr = vmx_get_msr,
3244 .set_msr = vmx_set_msr,
3245 .get_segment_base = vmx_get_segment_base,
3246 .get_segment = vmx_get_segment,
3247 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003248 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003249 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003250 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003251 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003252 .set_cr3 = vmx_set_cr3,
3253 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003254 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003255 .get_idt = vmx_get_idt,
3256 .set_idt = vmx_set_idt,
3257 .get_gdt = vmx_get_gdt,
3258 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003259 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003260 .get_rflags = vmx_get_rflags,
3261 .set_rflags = vmx_set_rflags,
3262
3263 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003264
Avi Kivity6aa8b732006-12-10 02:21:36 -08003265 .run = vmx_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003266 .handle_exit = kvm_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02003268 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003269 .get_irq = vmx_get_irq,
3270 .set_irq = vmx_inject_irq,
Avi Kivity298101d2007-11-25 13:41:11 +02003271 .queue_exception = vmx_queue_exception,
3272 .exception_injected = vmx_exception_injected,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003273 .inject_pending_irq = vmx_intr_assist,
3274 .inject_pending_vectors = do_interrupt_requests,
Izik Eiduscbc94022007-10-25 00:29:55 +02003275
3276 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003277 .get_tdp_level = get_ept_level,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003278};
3279
3280static int __init vmx_init(void)
3281{
Sheng Yang25c5f222008-03-28 13:18:56 +08003282 void *va;
He, Qingfdef3ad2007-04-30 09:45:24 +03003283 int r;
3284
3285 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3286 if (!vmx_io_bitmap_a)
3287 return -ENOMEM;
3288
3289 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3290 if (!vmx_io_bitmap_b) {
3291 r = -ENOMEM;
3292 goto out;
3293 }
3294
Sheng Yang25c5f222008-03-28 13:18:56 +08003295 vmx_msr_bitmap = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3296 if (!vmx_msr_bitmap) {
3297 r = -ENOMEM;
3298 goto out1;
3299 }
3300
He, Qingfdef3ad2007-04-30 09:45:24 +03003301 /*
3302 * Allow direct access to the PC debug port (it is often used for I/O
3303 * delays, but the vmexits simply slow things down).
3304 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003305 va = kmap(vmx_io_bitmap_a);
3306 memset(va, 0xff, PAGE_SIZE);
3307 clear_bit(0x80, va);
Avi Kivitycd0536d2007-05-08 11:34:07 +03003308 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003309
Sheng Yang25c5f222008-03-28 13:18:56 +08003310 va = kmap(vmx_io_bitmap_b);
3311 memset(va, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03003312 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03003313
Sheng Yang25c5f222008-03-28 13:18:56 +08003314 va = kmap(vmx_msr_bitmap);
3315 memset(va, 0xff, PAGE_SIZE);
3316 kunmap(vmx_msr_bitmap);
3317
Sheng Yang2384d2b2008-01-17 15:14:33 +08003318 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
3319
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003320 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003321 if (r)
Sheng Yang25c5f222008-03-28 13:18:56 +08003322 goto out2;
3323
3324 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_FS_BASE);
3325 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_GS_BASE);
3326 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_CS);
3327 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_ESP);
3328 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_EIP);
He, Qingfdef3ad2007-04-30 09:45:24 +03003329
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003330 if (vm_need_ept()) {
Sheng Yang14394422008-04-28 12:24:45 +08003331 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003332 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
3333 VMX_EPT_WRITABLE_MASK |
3334 VMX_EPT_DEFAULT_MT << VMX_EPT_MT_EPTE_SHIFT);
Sheng Yang534e38b2008-09-08 15:12:30 +08003335 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003336 VMX_EPT_EXECUTABLE_MASK);
3337 kvm_enable_tdp();
3338 } else
3339 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08003340
Avi Kivityc7addb92007-09-16 18:58:32 +02003341 if (bypass_guest_pf)
3342 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
3343
Sheng Yang14394422008-04-28 12:24:45 +08003344 ept_sync_global();
3345
He, Qingfdef3ad2007-04-30 09:45:24 +03003346 return 0;
3347
Sheng Yang25c5f222008-03-28 13:18:56 +08003348out2:
3349 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03003350out1:
3351 __free_page(vmx_io_bitmap_b);
3352out:
3353 __free_page(vmx_io_bitmap_a);
3354 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003355}
3356
3357static void __exit vmx_exit(void)
3358{
Sheng Yang25c5f222008-03-28 13:18:56 +08003359 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03003360 __free_page(vmx_io_bitmap_b);
3361 __free_page(vmx_io_bitmap_a);
3362
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003363 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003364}
3365
3366module_init(vmx_init)
3367module_exit(vmx_exit)