blob: b4c95a501ccadcbec55a443d1a69a5192e2c1253 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080019#include "vmx.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040021
Avi Kivityedf88412007-12-16 11:02:48 +020022#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080023#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020024#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/mm.h>
26#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040027#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020028#include <linux/moduleparam.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030029#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030030#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080033#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080034
Avi Kivity4ecac3f2008-05-13 13:23:38 +030035#define __ex(x) __kvm_handle_fault_on_reboot(x)
36
Avi Kivity6aa8b732006-12-10 02:21:36 -080037MODULE_AUTHOR("Qumranet");
38MODULE_LICENSE("GPL");
39
Avi Kivityc7addb92007-09-16 18:58:32 +020040static int bypass_guest_pf = 1;
41module_param(bypass_guest_pf, bool, 0);
42
Sheng Yang2384d2b2008-01-17 15:14:33 +080043static int enable_vpid = 1;
44module_param(enable_vpid, bool, 0);
45
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020046static int flexpriority_enabled = 1;
47module_param(flexpriority_enabled, bool, 0);
48
Sheng Yang14394422008-04-28 12:24:45 +080049static int enable_ept = 1;
Sheng Yangd56f5462008-04-25 10:13:16 +080050module_param(enable_ept, bool, 0);
51
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030052static int emulate_invalid_guest_state = 0;
53module_param(emulate_invalid_guest_state, bool, 0);
54
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040055struct vmcs {
56 u32 revision_id;
57 u32 abort;
58 char data[0];
59};
60
61struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100062 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +030063 struct list_head local_vcpus_link;
Avi Kivity313dbd42008-07-17 18:04:30 +030064 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040065 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030066 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020067 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040068 struct kvm_msr_entry *guest_msrs;
69 struct kvm_msr_entry *host_msrs;
70 int nmsrs;
71 int save_nmsrs;
72 int msr_offset_efer;
73#ifdef CONFIG_X86_64
74 int msr_offset_kernel_gs_base;
75#endif
76 struct vmcs *vmcs;
77 struct {
78 int loaded;
79 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020080 int gs_ldt_reload_needed;
81 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030082 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040083 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +020084 struct {
85 struct {
86 bool pending;
87 u8 vector;
88 unsigned rip;
89 } irq;
90 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +080091 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030092 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +020093
94 /* Support for vnmi-less CPUs */
95 int soft_vnmi_blocked;
96 ktime_t entry_time;
97 s64 vnmi_blocked_time;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040098};
99
100static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
101{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000102 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400103}
104
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800105static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800106static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300107
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108static DEFINE_PER_CPU(struct vmcs *, vmxarea);
109static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300110static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800111
He, Qingfdef3ad2007-04-30 09:45:24 +0300112static struct page *vmx_io_bitmap_a;
113static struct page *vmx_io_bitmap_b;
Sheng Yang25c5f222008-03-28 13:18:56 +0800114static struct page *vmx_msr_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300115
Sheng Yang2384d2b2008-01-17 15:14:33 +0800116static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
117static DEFINE_SPINLOCK(vmx_vpid_lock);
118
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300119static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120 int size;
121 int order;
122 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300123 u32 pin_based_exec_ctrl;
124 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800125 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300126 u32 vmexit_ctrl;
127 u32 vmentry_ctrl;
128} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800129
Sheng Yangd56f5462008-04-25 10:13:16 +0800130struct vmx_capability {
131 u32 ept;
132 u32 vpid;
133} vmx_capability;
134
Avi Kivity6aa8b732006-12-10 02:21:36 -0800135#define VMX_SEGMENT_FIELD(seg) \
136 [VCPU_SREG_##seg] = { \
137 .selector = GUEST_##seg##_SELECTOR, \
138 .base = GUEST_##seg##_BASE, \
139 .limit = GUEST_##seg##_LIMIT, \
140 .ar_bytes = GUEST_##seg##_AR_BYTES, \
141 }
142
143static struct kvm_vmx_segment_field {
144 unsigned selector;
145 unsigned base;
146 unsigned limit;
147 unsigned ar_bytes;
148} kvm_vmx_segment_fields[] = {
149 VMX_SEGMENT_FIELD(CS),
150 VMX_SEGMENT_FIELD(DS),
151 VMX_SEGMENT_FIELD(ES),
152 VMX_SEGMENT_FIELD(FS),
153 VMX_SEGMENT_FIELD(GS),
154 VMX_SEGMENT_FIELD(SS),
155 VMX_SEGMENT_FIELD(TR),
156 VMX_SEGMENT_FIELD(LDTR),
157};
158
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300159/*
160 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
161 * away by decrementing the array size.
162 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800163static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800164#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800165 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
166#endif
167 MSR_EFER, MSR_K6_STAR,
168};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200169#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800170
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400171static void load_msrs(struct kvm_msr_entry *e, int n)
172{
173 int i;
174
175 for (i = 0; i < n; ++i)
176 wrmsrl(e[i].index, e[i].data);
177}
178
179static void save_msrs(struct kvm_msr_entry *e, int n)
180{
181 int i;
182
183 for (i = 0; i < n; ++i)
184 rdmsrl(e[i].index, e[i].data);
185}
186
Avi Kivity6aa8b732006-12-10 02:21:36 -0800187static inline int is_page_fault(u32 intr_info)
188{
189 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
190 INTR_INFO_VALID_MASK)) ==
191 (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
192}
193
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300194static inline int is_no_device(u32 intr_info)
195{
196 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
197 INTR_INFO_VALID_MASK)) ==
198 (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
199}
200
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500201static inline int is_invalid_opcode(u32 intr_info)
202{
203 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
204 INTR_INFO_VALID_MASK)) ==
205 (INTR_TYPE_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
206}
207
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208static inline int is_external_interrupt(u32 intr_info)
209{
210 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
211 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
212}
213
Sheng Yang25c5f222008-03-28 13:18:56 +0800214static inline int cpu_has_vmx_msr_bitmap(void)
215{
216 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS);
217}
218
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800219static inline int cpu_has_vmx_tpr_shadow(void)
220{
221 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW);
222}
223
224static inline int vm_need_tpr_shadow(struct kvm *kvm)
225{
226 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)));
227}
228
Sheng Yangf78e0e22007-10-29 09:40:42 +0800229static inline int cpu_has_secondary_exec_ctrls(void)
230{
231 return (vmcs_config.cpu_based_exec_ctrl &
232 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS);
233}
234
Avi Kivity774ead32007-12-26 13:57:04 +0200235static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800236{
Avi Kivity4c9fc8e2008-03-24 18:15:14 +0200237 return flexpriority_enabled
238 && (vmcs_config.cpu_based_2nd_exec_ctrl &
239 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800240}
241
Sheng Yangd56f5462008-04-25 10:13:16 +0800242static inline int cpu_has_vmx_invept_individual_addr(void)
243{
244 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT));
245}
246
247static inline int cpu_has_vmx_invept_context(void)
248{
249 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT));
250}
251
252static inline int cpu_has_vmx_invept_global(void)
253{
254 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT));
255}
256
257static inline int cpu_has_vmx_ept(void)
258{
259 return (vmcs_config.cpu_based_2nd_exec_ctrl &
260 SECONDARY_EXEC_ENABLE_EPT);
261}
262
263static inline int vm_need_ept(void)
264{
265 return (cpu_has_vmx_ept() && enable_ept);
266}
267
Sheng Yangf78e0e22007-10-29 09:40:42 +0800268static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
269{
270 return ((cpu_has_vmx_virtualize_apic_accesses()) &&
271 (irqchip_in_kernel(kvm)));
272}
273
Sheng Yang2384d2b2008-01-17 15:14:33 +0800274static inline int cpu_has_vmx_vpid(void)
275{
276 return (vmcs_config.cpu_based_2nd_exec_ctrl &
277 SECONDARY_EXEC_ENABLE_VPID);
278}
279
Sheng Yangf08864b2008-05-15 18:23:25 +0800280static inline int cpu_has_virtual_nmis(void)
281{
282 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
283}
284
Rusty Russell8b9cf982007-07-30 16:31:43 +1000285static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800286{
287 int i;
288
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400289 for (i = 0; i < vmx->nmsrs; ++i)
290 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300291 return i;
292 return -1;
293}
294
Sheng Yang2384d2b2008-01-17 15:14:33 +0800295static inline void __invvpid(int ext, u16 vpid, gva_t gva)
296{
297 struct {
298 u64 vpid : 16;
299 u64 rsvd : 48;
300 u64 gva;
301 } operand = { vpid, 0, gva };
302
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300303 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800304 /* CF==1 or ZF==1 --> rc = -1 */
305 "; ja 1f ; ud2 ; 1:"
306 : : "a"(&operand), "c"(ext) : "cc", "memory");
307}
308
Sheng Yang14394422008-04-28 12:24:45 +0800309static inline void __invept(int ext, u64 eptp, gpa_t gpa)
310{
311 struct {
312 u64 eptp, gpa;
313 } operand = {eptp, gpa};
314
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300315 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800316 /* CF==1 or ZF==1 --> rc = -1 */
317 "; ja 1f ; ud2 ; 1:\n"
318 : : "a" (&operand), "c" (ext) : "cc", "memory");
319}
320
Rusty Russell8b9cf982007-07-30 16:31:43 +1000321static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300322{
323 int i;
324
Rusty Russell8b9cf982007-07-30 16:31:43 +1000325 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300326 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400327 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000328 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800329}
330
Avi Kivity6aa8b732006-12-10 02:21:36 -0800331static void vmcs_clear(struct vmcs *vmcs)
332{
333 u64 phys_addr = __pa(vmcs);
334 u8 error;
335
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300336 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800337 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
338 : "cc", "memory");
339 if (error)
340 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
341 vmcs, phys_addr);
342}
343
344static void __vcpu_clear(void *arg)
345{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000346 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800347 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800348
Rusty Russell8b9cf982007-07-30 16:31:43 +1000349 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400350 vmcs_clear(vmx->vmcs);
351 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800352 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800353 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300354 list_del(&vmx->local_vcpus_link);
355 vmx->vcpu.cpu = -1;
356 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800357}
358
Rusty Russell8b9cf982007-07-30 16:31:43 +1000359static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800360{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200361 if (vmx->vcpu.cpu == -1)
362 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200363 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800364}
365
Sheng Yang2384d2b2008-01-17 15:14:33 +0800366static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
367{
368 if (vmx->vpid == 0)
369 return;
370
371 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
372}
373
Sheng Yang14394422008-04-28 12:24:45 +0800374static inline void ept_sync_global(void)
375{
376 if (cpu_has_vmx_invept_global())
377 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
378}
379
380static inline void ept_sync_context(u64 eptp)
381{
382 if (vm_need_ept()) {
383 if (cpu_has_vmx_invept_context())
384 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
385 else
386 ept_sync_global();
387 }
388}
389
390static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
391{
392 if (vm_need_ept()) {
393 if (cpu_has_vmx_invept_individual_addr())
394 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
395 eptp, gpa);
396 else
397 ept_sync_context(eptp);
398 }
399}
400
Avi Kivity6aa8b732006-12-10 02:21:36 -0800401static unsigned long vmcs_readl(unsigned long field)
402{
403 unsigned long value;
404
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300405 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800406 : "=a"(value) : "d"(field) : "cc");
407 return value;
408}
409
410static u16 vmcs_read16(unsigned long field)
411{
412 return vmcs_readl(field);
413}
414
415static u32 vmcs_read32(unsigned long field)
416{
417 return vmcs_readl(field);
418}
419
420static u64 vmcs_read64(unsigned long field)
421{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800422#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800423 return vmcs_readl(field);
424#else
425 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
426#endif
427}
428
Avi Kivitye52de1b2007-01-05 16:36:56 -0800429static noinline void vmwrite_error(unsigned long field, unsigned long value)
430{
431 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
432 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
433 dump_stack();
434}
435
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436static void vmcs_writel(unsigned long field, unsigned long value)
437{
438 u8 error;
439
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300440 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400441 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800442 if (unlikely(error))
443 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800444}
445
446static void vmcs_write16(unsigned long field, u16 value)
447{
448 vmcs_writel(field, value);
449}
450
451static void vmcs_write32(unsigned long field, u32 value)
452{
453 vmcs_writel(field, value);
454}
455
456static void vmcs_write64(unsigned long field, u64 value)
457{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300459#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460 asm volatile ("");
461 vmcs_writel(field+1, value >> 32);
462#endif
463}
464
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300465static void vmcs_clear_bits(unsigned long field, u32 mask)
466{
467 vmcs_writel(field, vmcs_readl(field) & ~mask);
468}
469
470static void vmcs_set_bits(unsigned long field, u32 mask)
471{
472 vmcs_writel(field, vmcs_readl(field) | mask);
473}
474
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300475static void update_exception_bitmap(struct kvm_vcpu *vcpu)
476{
477 u32 eb;
478
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500479 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300480 if (!vcpu->fpu_active)
481 eb |= 1u << NM_VECTOR;
482 if (vcpu->guest_debug.enabled)
Jan Kiszka19bd8af2008-07-13 13:40:55 +0200483 eb |= 1u << DB_VECTOR;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800484 if (vcpu->arch.rmode.active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300485 eb = ~0;
Sheng Yang14394422008-04-28 12:24:45 +0800486 if (vm_need_ept())
487 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300488 vmcs_write32(EXCEPTION_BITMAP, eb);
489}
490
Avi Kivity33ed6322007-05-02 16:54:03 +0300491static void reload_tss(void)
492{
Avi Kivity33ed6322007-05-02 16:54:03 +0300493 /*
494 * VT restores TR but not its size. Useless.
495 */
496 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200497 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300498
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300499 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300500 descs = (void *)gdt.base;
501 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
502 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300503}
504
Rusty Russell8b9cf982007-07-30 16:31:43 +1000505static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300506{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400507 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300508 u64 host_efer = vmx->host_msrs[efer_offset].data;
509 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
510 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300511
Avi Kivity51c6cf62007-08-29 03:48:05 +0300512 if (efer_offset < 0)
513 return;
514 /*
515 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
516 * outside long mode
517 */
518 ignore_bits = EFER_NX | EFER_SCE;
519#ifdef CONFIG_X86_64
520 ignore_bits |= EFER_LMA | EFER_LME;
521 /* SCE is meaningful only in long mode on Intel */
522 if (guest_efer & EFER_LMA)
523 ignore_bits &= ~(u64)EFER_SCE;
524#endif
525 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
526 return;
527
528 vmx->host_state.guest_efer_loaded = 1;
529 guest_efer &= ~ignore_bits;
530 guest_efer |= host_efer & ignore_bits;
531 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000532 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300533}
534
Avi Kivity51c6cf62007-08-29 03:48:05 +0300535static void reload_host_efer(struct vcpu_vmx *vmx)
536{
537 if (vmx->host_state.guest_efer_loaded) {
538 vmx->host_state.guest_efer_loaded = 0;
539 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
540 }
541}
542
Avi Kivity04d2cc72007-09-10 18:10:54 +0300543static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300544{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300545 struct vcpu_vmx *vmx = to_vmx(vcpu);
546
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400547 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300548 return;
549
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400550 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300551 /*
552 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
553 * allow segment selectors with cpl > 0 or ti == 1.
554 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300555 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200556 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300557 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200558 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400559 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200560 vmx->host_state.fs_reload_needed = 0;
561 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300562 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200563 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300564 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300565 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400566 if (!(vmx->host_state.gs_sel & 7))
567 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300568 else {
569 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200570 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300571 }
572
573#ifdef CONFIG_X86_64
574 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
575 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
576#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400577 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
578 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300579#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300580
581#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400582 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400583 save_msrs(vmx->host_msrs +
584 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400585
Avi Kivity707c0872007-05-02 17:33:43 +0300586#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400587 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300588 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300589}
590
Avi Kivitya9b21b62008-06-24 11:48:49 +0300591static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300592{
Avi Kivity15ad7142007-07-11 18:17:21 +0300593 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300594
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400595 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300596 return;
597
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200598 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400599 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200600 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300601 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200602 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300603 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300604 /*
605 * If we have to reload gs, we must take care to
606 * preserve our gs base.
607 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300608 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300609 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300610#ifdef CONFIG_X86_64
611 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
612#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300613 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300614 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200615 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400616 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
617 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300618 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300619}
620
Avi Kivitya9b21b62008-06-24 11:48:49 +0300621static void vmx_load_host_state(struct vcpu_vmx *vmx)
622{
623 preempt_disable();
624 __vmx_load_host_state(vmx);
625 preempt_enable();
626}
627
Avi Kivity6aa8b732006-12-10 02:21:36 -0800628/*
629 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
630 * vcpu mutex is already taken.
631 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300632static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400634 struct vcpu_vmx *vmx = to_vmx(vcpu);
635 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200636 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637
Eddie Donga3d7f852007-09-03 16:15:12 +0300638 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000639 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300640 kvm_migrate_timers(vcpu);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800641 vpid_sync_vcpu_all(vmx);
Avi Kivity543e4242008-05-13 16:22:47 +0300642 local_irq_disable();
643 list_add(&vmx->local_vcpus_link,
644 &per_cpu(vcpus_on_cpu, cpu));
645 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300646 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400648 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800649 u8 error;
650
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400651 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300652 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
654 : "cc");
655 if (error)
656 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400657 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658 }
659
660 if (vcpu->cpu != cpu) {
661 struct descriptor_table dt;
662 unsigned long sysenter_esp;
663
664 vcpu->cpu = cpu;
665 /*
666 * Linux uses per-cpu TSS and GDT, so set these when switching
667 * processors.
668 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300669 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
670 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
672
673 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
674 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300675
676 /*
677 * Make sure the time stamp counter is monotonous.
678 */
679 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200680 if (tsc_this < vcpu->arch.host_tsc) {
681 delta = vcpu->arch.host_tsc - tsc_this;
682 new_offset = vmcs_read64(TSC_OFFSET) + delta;
683 vmcs_write64(TSC_OFFSET, new_offset);
684 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800686}
687
688static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
689{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300690 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691}
692
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300693static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
694{
695 if (vcpu->fpu_active)
696 return;
697 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000698 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800699 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92fa2007-07-17 23:19:08 +1000700 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300701 update_exception_bitmap(vcpu);
702}
703
704static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
705{
706 if (!vcpu->fpu_active)
707 return;
708 vcpu->fpu_active = 0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000709 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300710 update_exception_bitmap(vcpu);
711}
712
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
714{
715 return vmcs_readl(GUEST_RFLAGS);
716}
717
718static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
719{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800720 if (vcpu->arch.rmode.active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100721 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800722 vmcs_writel(GUEST_RFLAGS, rflags);
723}
724
725static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
726{
727 unsigned long rip;
728 u32 interruptibility;
729
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300730 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800731 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300732 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733
734 /*
735 * We emulated an instruction, so temporary interrupt blocking
736 * should be removed, if set.
737 */
738 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
739 if (interruptibility & 3)
740 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
741 interruptibility & ~3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800742 vcpu->arch.interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743}
744
Avi Kivity298101d2007-11-25 13:41:11 +0200745static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
746 bool has_error_code, u32 error_code)
747{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200748 struct vcpu_vmx *vmx = to_vmx(vcpu);
749
750 if (has_error_code)
751 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
752
753 if (vcpu->arch.rmode.active) {
754 vmx->rmode.irq.pending = true;
755 vmx->rmode.irq.vector = nr;
756 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
757 if (nr == BP_VECTOR)
758 vmx->rmode.irq.rip++;
759 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
760 nr | INTR_TYPE_SOFT_INTR
761 | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0)
762 | INTR_INFO_VALID_MASK);
763 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
764 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
765 return;
766 }
767
Avi Kivity298101d2007-11-25 13:41:11 +0200768 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
769 nr | INTR_TYPE_EXCEPTION
Ryan Harper2e113842008-02-11 10:26:38 -0600770 | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0)
Avi Kivity298101d2007-11-25 13:41:11 +0200771 | INTR_INFO_VALID_MASK);
Avi Kivity298101d2007-11-25 13:41:11 +0200772}
773
774static bool vmx_exception_injected(struct kvm_vcpu *vcpu)
775{
Avi Kivity35920a32008-07-03 14:50:12 +0300776 return false;
Avi Kivity298101d2007-11-25 13:41:11 +0200777}
778
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779/*
Eddie Donga75beee2007-05-17 18:55:15 +0300780 * Swap MSR entry in host/guest MSR entry array.
781 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200782#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000783static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300784{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400785 struct kvm_msr_entry tmp;
786
787 tmp = vmx->guest_msrs[to];
788 vmx->guest_msrs[to] = vmx->guest_msrs[from];
789 vmx->guest_msrs[from] = tmp;
790 tmp = vmx->host_msrs[to];
791 vmx->host_msrs[to] = vmx->host_msrs[from];
792 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300793}
Gabriel C54e11fa2007-08-01 16:23:10 +0200794#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300795
796/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300797 * Set up the vmcs to automatically save and restore system
798 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
799 * mode, as fiddling with msrs is very expensive.
800 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000801static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300802{
Eddie Dong2cc51562007-05-21 07:28:09 +0300803 int save_nmsrs;
Avi Kivitye38aea32007-04-19 13:22:48 +0300804
Avi Kivity33f9c502008-02-27 16:06:57 +0200805 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300806 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300807#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000808 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300809 int index;
810
Rusty Russell8b9cf982007-07-30 16:31:43 +1000811 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300812 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000813 move_msr_up(vmx, index, save_nmsrs++);
814 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300815 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000816 move_msr_up(vmx, index, save_nmsrs++);
817 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300818 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000819 move_msr_up(vmx, index, save_nmsrs++);
820 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300821 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000822 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300823 /*
824 * MSR_K6_STAR is only needed on long mode guests, and only
825 * if efer.sce is enabled.
826 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000827 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800828 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000829 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300830 }
Eddie Donga75beee2007-05-17 18:55:15 +0300831#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400832 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300833
Eddie Donga75beee2007-05-17 18:55:15 +0300834#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400835 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000836 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300837#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000838 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivitye38aea32007-04-19 13:22:48 +0300839}
840
841/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800842 * reads and returns guest's timestamp counter "register"
843 * guest_tsc = host_tsc + tsc_offset -- 21.3
844 */
845static u64 guest_read_tsc(void)
846{
847 u64 host_tsc, tsc_offset;
848
849 rdtscll(host_tsc);
850 tsc_offset = vmcs_read64(TSC_OFFSET);
851 return host_tsc + tsc_offset;
852}
853
854/*
855 * writes 'guest_tsc' into guest's timestamp counter "register"
856 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
857 */
858static void guest_write_tsc(u64 guest_tsc)
859{
860 u64 host_tsc;
861
862 rdtscll(host_tsc);
863 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
864}
865
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866/*
867 * Reads an msr value (of 'msr_index') into 'pdata'.
868 * Returns 0 on success, non-0 otherwise.
869 * Assumes vcpu_load() was already called.
870 */
871static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
872{
873 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400874 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875
876 if (!pdata) {
877 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
878 return -EINVAL;
879 }
880
881 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800882#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883 case MSR_FS_BASE:
884 data = vmcs_readl(GUEST_FS_BASE);
885 break;
886 case MSR_GS_BASE:
887 data = vmcs_readl(GUEST_GS_BASE);
888 break;
889 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800890 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891#endif
892 case MSR_IA32_TIME_STAMP_COUNTER:
893 data = guest_read_tsc();
894 break;
895 case MSR_IA32_SYSENTER_CS:
896 data = vmcs_read32(GUEST_SYSENTER_CS);
897 break;
898 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200899 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900 break;
901 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200902 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000905 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800906 if (msr) {
907 data = msr->data;
908 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800910 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911 }
912
913 *pdata = data;
914 return 0;
915}
916
917/*
918 * Writes msr value into into the appropriate "register".
919 * Returns 0 on success, non-0 otherwise.
920 * Assumes vcpu_load() was already called.
921 */
922static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
923{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400924 struct vcpu_vmx *vmx = to_vmx(vcpu);
925 struct kvm_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +0300926 int ret = 0;
927
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800929#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800930 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300931 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +0300932 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +0300933 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934 case MSR_FS_BASE:
935 vmcs_writel(GUEST_FS_BASE, data);
936 break;
937 case MSR_GS_BASE:
938 vmcs_writel(GUEST_GS_BASE, data);
939 break;
940#endif
941 case MSR_IA32_SYSENTER_CS:
942 vmcs_write32(GUEST_SYSENTER_CS, data);
943 break;
944 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200945 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946 break;
947 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200948 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200950 case MSR_IA32_TIME_STAMP_COUNTER:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951 guest_write_tsc(data);
952 break;
Chris Lalancetteefa67e02008-06-20 09:51:30 +0200953 case MSR_P6_PERFCTR0:
954 case MSR_P6_PERFCTR1:
955 case MSR_P6_EVNTSEL0:
956 case MSR_P6_EVNTSEL1:
957 /*
958 * Just discard all writes to the performance counters; this
959 * should keep both older linux and windows 64-bit guests
960 * happy
961 */
962 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", msr_index, data);
963
964 break;
Sheng Yang468d4722008-10-09 16:01:55 +0800965 case MSR_IA32_CR_PAT:
966 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
967 vmcs_write64(GUEST_IA32_PAT, data);
968 vcpu->arch.pat = data;
969 break;
970 }
971 /* Otherwise falls through to kvm_set_msr_common */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 default:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300973 vmx_load_host_state(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000974 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800975 if (msr) {
976 msr->data = data;
977 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300979 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980 }
981
Eddie Dong2cc51562007-05-21 07:28:09 +0300982 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983}
984
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300985static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300987 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
988 switch (reg) {
989 case VCPU_REGS_RSP:
990 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
991 break;
992 case VCPU_REGS_RIP:
993 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
994 break;
995 default:
996 break;
997 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998}
999
1000static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
1001{
1002 unsigned long dr7 = 0x400;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 int old_singlestep;
1004
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005 old_singlestep = vcpu->guest_debug.singlestep;
1006
1007 vcpu->guest_debug.enabled = dbg->enabled;
1008 if (vcpu->guest_debug.enabled) {
1009 int i;
1010
1011 dr7 |= 0x200; /* exact */
1012 for (i = 0; i < 4; ++i) {
1013 if (!dbg->breakpoints[i].enabled)
1014 continue;
1015 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
1016 dr7 |= 2 << (i*2); /* global enable */
1017 dr7 |= 0 << (i*4+16); /* execution breakpoint */
1018 }
1019
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020 vcpu->guest_debug.singlestep = dbg->singlestep;
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001021 } else
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022 vcpu->guest_debug.singlestep = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023
1024 if (old_singlestep && !vcpu->guest_debug.singlestep) {
1025 unsigned long flags;
1026
1027 flags = vmcs_readl(GUEST_RFLAGS);
1028 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1029 vmcs_writel(GUEST_RFLAGS, flags);
1030 }
1031
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001032 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 vmcs_writel(GUEST_DR7, dr7);
1034
1035 return 0;
1036}
1037
Eddie Dong2a8067f2007-08-06 16:29:07 +03001038static int vmx_get_irq(struct kvm_vcpu *vcpu)
1039{
Avi Kivityf7d92382008-07-03 16:14:28 +03001040 if (!vcpu->arch.interrupt.pending)
1041 return -1;
1042 return vcpu->arch.interrupt.nr;
Eddie Dong2a8067f2007-08-06 16:29:07 +03001043}
1044
Avi Kivity6aa8b732006-12-10 02:21:36 -08001045static __init int cpu_has_kvm_support(void)
1046{
1047 unsigned long ecx = cpuid_ecx(1);
1048 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
1049}
1050
1051static __init int vmx_disabled_by_bios(void)
1052{
1053 u64 msr;
1054
1055 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001056 return (msr & (FEATURE_CONTROL_LOCKED |
1057 FEATURE_CONTROL_VMXON_ENABLED))
1058 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001059 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060}
1061
Avi Kivity774c47f2007-02-12 00:54:47 -08001062static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063{
1064 int cpu = raw_smp_processor_id();
1065 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1066 u64 old;
1067
Avi Kivity543e4242008-05-13 16:22:47 +03001068 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001070 if ((old & (FEATURE_CONTROL_LOCKED |
1071 FEATURE_CONTROL_VMXON_ENABLED))
1072 != (FEATURE_CONTROL_LOCKED |
1073 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001075 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001076 FEATURE_CONTROL_LOCKED |
1077 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001078 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001079 asm volatile (ASM_VMX_VMXON_RAX
1080 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081 : "memory", "cc");
1082}
1083
Avi Kivity543e4242008-05-13 16:22:47 +03001084static void vmclear_local_vcpus(void)
1085{
1086 int cpu = raw_smp_processor_id();
1087 struct vcpu_vmx *vmx, *n;
1088
1089 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1090 local_vcpus_link)
1091 __vcpu_clear(vmx);
1092}
1093
Avi Kivity6aa8b732006-12-10 02:21:36 -08001094static void hardware_disable(void *garbage)
1095{
Avi Kivity543e4242008-05-13 16:22:47 +03001096 vmclear_local_vcpus();
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001097 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eli Collinse693d712008-06-01 20:24:40 -07001098 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001099}
1100
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001101static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001102 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001103{
1104 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001105 u32 ctl = ctl_min | ctl_opt;
1106
1107 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1108
1109 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1110 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1111
1112 /* Ensure minimum (required) set of control bits are supported. */
1113 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001114 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001115
1116 *result = ctl;
1117 return 0;
1118}
1119
Yang, Sheng002c7f72007-07-31 14:23:01 +03001120static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001121{
1122 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001123 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001124 u32 _pin_based_exec_control = 0;
1125 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001126 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001127 u32 _vmexit_control = 0;
1128 u32 _vmentry_control = 0;
1129
1130 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001131 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001132 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1133 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001134 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001135
1136 min = CPU_BASED_HLT_EXITING |
1137#ifdef CONFIG_X86_64
1138 CPU_BASED_CR8_LOAD_EXITING |
1139 CPU_BASED_CR8_STORE_EXITING |
1140#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001141 CPU_BASED_CR3_LOAD_EXITING |
1142 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001143 CPU_BASED_USE_IO_BITMAPS |
1144 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001145 CPU_BASED_USE_TSC_OFFSETING |
1146 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001147 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001148 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001149 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001150 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1151 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001152 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001153#ifdef CONFIG_X86_64
1154 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1155 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1156 ~CPU_BASED_CR8_STORE_EXITING;
1157#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001158 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001159 min2 = 0;
1160 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001161 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001162 SECONDARY_EXEC_ENABLE_VPID |
1163 SECONDARY_EXEC_ENABLE_EPT;
1164 if (adjust_vmx_controls(min2, opt2,
1165 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001166 &_cpu_based_2nd_exec_control) < 0)
1167 return -EIO;
1168 }
1169#ifndef CONFIG_X86_64
1170 if (!(_cpu_based_2nd_exec_control &
1171 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1172 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1173#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001174 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001175 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1176 enabled */
Sheng Yangd56f5462008-04-25 10:13:16 +08001177 min &= ~(CPU_BASED_CR3_LOAD_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001178 CPU_BASED_CR3_STORE_EXITING |
1179 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001180 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1181 &_cpu_based_exec_control) < 0)
1182 return -EIO;
1183 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1184 vmx_capability.ept, vmx_capability.vpid);
1185 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001186
1187 min = 0;
1188#ifdef CONFIG_X86_64
1189 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1190#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001191 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001192 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1193 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001194 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001195
Sheng Yang468d4722008-10-09 16:01:55 +08001196 min = 0;
1197 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001198 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1199 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001200 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001201
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001202 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001203
1204 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1205 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001206 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001207
1208#ifdef CONFIG_X86_64
1209 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1210 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001211 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001212#endif
1213
1214 /* Require Write-Back (WB) memory type for VMCS accesses. */
1215 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001216 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001217
Yang, Sheng002c7f72007-07-31 14:23:01 +03001218 vmcs_conf->size = vmx_msr_high & 0x1fff;
1219 vmcs_conf->order = get_order(vmcs_config.size);
1220 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001221
Yang, Sheng002c7f72007-07-31 14:23:01 +03001222 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1223 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001224 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001225 vmcs_conf->vmexit_ctrl = _vmexit_control;
1226 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001227
1228 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001229}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001230
1231static struct vmcs *alloc_vmcs_cpu(int cpu)
1232{
1233 int node = cpu_to_node(cpu);
1234 struct page *pages;
1235 struct vmcs *vmcs;
1236
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001237 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001238 if (!pages)
1239 return NULL;
1240 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001241 memset(vmcs, 0, vmcs_config.size);
1242 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243 return vmcs;
1244}
1245
1246static struct vmcs *alloc_vmcs(void)
1247{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001248 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249}
1250
1251static void free_vmcs(struct vmcs *vmcs)
1252{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001253 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001254}
1255
Sam Ravnborg39959582007-06-01 00:47:13 -07001256static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001257{
1258 int cpu;
1259
1260 for_each_online_cpu(cpu)
1261 free_vmcs(per_cpu(vmxarea, cpu));
1262}
1263
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264static __init int alloc_kvm_area(void)
1265{
1266 int cpu;
1267
1268 for_each_online_cpu(cpu) {
1269 struct vmcs *vmcs;
1270
1271 vmcs = alloc_vmcs_cpu(cpu);
1272 if (!vmcs) {
1273 free_kvm_area();
1274 return -ENOMEM;
1275 }
1276
1277 per_cpu(vmxarea, cpu) = vmcs;
1278 }
1279 return 0;
1280}
1281
1282static __init int hardware_setup(void)
1283{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001284 if (setup_vmcs_config(&vmcs_config) < 0)
1285 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001286
1287 if (boot_cpu_has(X86_FEATURE_NX))
1288 kvm_enable_efer_bits(EFER_NX);
1289
Avi Kivity6aa8b732006-12-10 02:21:36 -08001290 return alloc_kvm_area();
1291}
1292
1293static __exit void hardware_unsetup(void)
1294{
1295 free_kvm_area();
1296}
1297
Avi Kivity6aa8b732006-12-10 02:21:36 -08001298static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1299{
1300 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1301
Avi Kivity6af11b92007-03-19 13:18:10 +02001302 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303 vmcs_write16(sf->selector, save->selector);
1304 vmcs_writel(sf->base, save->base);
1305 vmcs_write32(sf->limit, save->limit);
1306 vmcs_write32(sf->ar_bytes, save->ar);
1307 } else {
1308 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1309 << AR_DPL_SHIFT;
1310 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1311 }
1312}
1313
1314static void enter_pmode(struct kvm_vcpu *vcpu)
1315{
1316 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001317 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001319 vmx->emulation_required = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001320 vcpu->arch.rmode.active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001321
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001322 vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base);
1323 vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit);
1324 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325
1326 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001327 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001328 flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329 vmcs_writel(GUEST_RFLAGS, flags);
1330
Rusty Russell66aee912007-07-17 23:34:16 +10001331 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1332 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333
1334 update_exception_bitmap(vcpu);
1335
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001336 if (emulate_invalid_guest_state)
1337 return;
1338
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001339 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1340 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1341 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1342 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343
1344 vmcs_write16(GUEST_SS_SELECTOR, 0);
1345 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1346
1347 vmcs_write16(GUEST_CS_SELECTOR,
1348 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1349 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1350}
1351
Mike Dayd77c26f2007-10-08 09:02:08 -04001352static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001354 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001355 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1356 kvm->memslots[0].npages - 3;
1357 return base_gfn << PAGE_SHIFT;
1358 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001359 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360}
1361
1362static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1363{
1364 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1365
1366 save->selector = vmcs_read16(sf->selector);
1367 save->base = vmcs_readl(sf->base);
1368 save->limit = vmcs_read32(sf->limit);
1369 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001370 vmcs_write16(sf->selector, save->base >> 4);
1371 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372 vmcs_write32(sf->limit, 0xffff);
1373 vmcs_write32(sf->ar_bytes, 0xf3);
1374}
1375
1376static void enter_rmode(struct kvm_vcpu *vcpu)
1377{
1378 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001379 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001381 vmx->emulation_required = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001382 vcpu->arch.rmode.active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001384 vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1386
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001387 vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1389
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001390 vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1392
1393 flags = vmcs_readl(GUEST_RFLAGS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001394 vcpu->arch.rmode.save_iopl
1395 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001397 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398
1399 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001400 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401 update_exception_bitmap(vcpu);
1402
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001403 if (emulate_invalid_guest_state)
1404 goto continue_rmode;
1405
Avi Kivity6aa8b732006-12-10 02:21:36 -08001406 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1407 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1408 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1409
1410 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001411 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001412 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1413 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001414 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1415
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001416 fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1417 fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1418 fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1419 fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001420
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001421continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001422 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001423 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424}
1425
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001426#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001427
1428static void enter_lmode(struct kvm_vcpu *vcpu)
1429{
1430 u32 guest_tr_ar;
1431
1432 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1433 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1434 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001435 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436 vmcs_write32(GUEST_TR_AR_BYTES,
1437 (guest_tr_ar & ~AR_TYPE_MASK)
1438 | AR_TYPE_BUSY_64_TSS);
1439 }
1440
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001441 vcpu->arch.shadow_efer |= EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001442
Rusty Russell8b9cf982007-07-30 16:31:43 +10001443 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444 vmcs_write32(VM_ENTRY_CONTROLS,
1445 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001446 | VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001447}
1448
1449static void exit_lmode(struct kvm_vcpu *vcpu)
1450{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001451 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001452
1453 vmcs_write32(VM_ENTRY_CONTROLS,
1454 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001455 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001456}
1457
1458#endif
1459
Sheng Yang2384d2b2008-01-17 15:14:33 +08001460static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1461{
1462 vpid_sync_vcpu_all(to_vmx(vcpu));
Sheng Yang4e1096d2008-07-06 19:16:51 +08001463 if (vm_need_ept())
1464 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001465}
1466
Anthony Liguori25c4c272007-04-27 09:29:21 +03001467static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001468{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001469 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1470 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001471}
1472
Sheng Yang14394422008-04-28 12:24:45 +08001473static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1474{
1475 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1476 if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
1477 printk(KERN_ERR "EPT: Fail to load pdptrs!\n");
1478 return;
1479 }
1480 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1481 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1482 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1483 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1484 }
1485}
1486
1487static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1488
1489static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1490 unsigned long cr0,
1491 struct kvm_vcpu *vcpu)
1492{
1493 if (!(cr0 & X86_CR0_PG)) {
1494 /* From paging/starting to nonpaging */
1495 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001496 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001497 (CPU_BASED_CR3_LOAD_EXITING |
1498 CPU_BASED_CR3_STORE_EXITING));
1499 vcpu->arch.cr0 = cr0;
1500 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1501 *hw_cr0 |= X86_CR0_PE | X86_CR0_PG;
1502 *hw_cr0 &= ~X86_CR0_WP;
1503 } else if (!is_paging(vcpu)) {
1504 /* From nonpaging to paging */
1505 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001506 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001507 ~(CPU_BASED_CR3_LOAD_EXITING |
1508 CPU_BASED_CR3_STORE_EXITING));
1509 vcpu->arch.cr0 = cr0;
1510 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1511 if (!(vcpu->arch.cr0 & X86_CR0_WP))
1512 *hw_cr0 &= ~X86_CR0_WP;
1513 }
1514}
1515
1516static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1517 struct kvm_vcpu *vcpu)
1518{
1519 if (!is_paging(vcpu)) {
1520 *hw_cr4 &= ~X86_CR4_PAE;
1521 *hw_cr4 |= X86_CR4_PSE;
1522 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1523 *hw_cr4 &= ~X86_CR4_PAE;
1524}
1525
Avi Kivity6aa8b732006-12-10 02:21:36 -08001526static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1527{
Sheng Yang14394422008-04-28 12:24:45 +08001528 unsigned long hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) |
1529 KVM_VM_CR0_ALWAYS_ON;
1530
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001531 vmx_fpu_deactivate(vcpu);
1532
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001533 if (vcpu->arch.rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001534 enter_pmode(vcpu);
1535
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001536 if (!vcpu->arch.rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537 enter_rmode(vcpu);
1538
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001539#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001540 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001541 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001542 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001543 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001544 exit_lmode(vcpu);
1545 }
1546#endif
1547
Sheng Yang14394422008-04-28 12:24:45 +08001548 if (vm_need_ept())
1549 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1550
Avi Kivity6aa8b732006-12-10 02:21:36 -08001551 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001552 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001553 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001554
Rusty Russell707d92fa2007-07-17 23:19:08 +10001555 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001556 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557}
1558
Sheng Yang14394422008-04-28 12:24:45 +08001559static u64 construct_eptp(unsigned long root_hpa)
1560{
1561 u64 eptp;
1562
1563 /* TODO write the value reading from MSR */
1564 eptp = VMX_EPT_DEFAULT_MT |
1565 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1566 eptp |= (root_hpa & PAGE_MASK);
1567
1568 return eptp;
1569}
1570
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1572{
Sheng Yang14394422008-04-28 12:24:45 +08001573 unsigned long guest_cr3;
1574 u64 eptp;
1575
1576 guest_cr3 = cr3;
1577 if (vm_need_ept()) {
1578 eptp = construct_eptp(cr3);
1579 vmcs_write64(EPT_POINTER, eptp);
1580 ept_sync_context(eptp);
1581 ept_load_pdptrs(vcpu);
1582 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
1583 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1584 }
1585
Sheng Yang2384d2b2008-01-17 15:14:33 +08001586 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001587 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001588 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001589 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001590}
1591
1592static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1593{
Sheng Yang14394422008-04-28 12:24:45 +08001594 unsigned long hw_cr4 = cr4 | (vcpu->arch.rmode.active ?
1595 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1596
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001597 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08001598 if (vm_need_ept())
1599 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1600
1601 vmcs_writel(CR4_READ_SHADOW, cr4);
1602 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603}
1604
Avi Kivity6aa8b732006-12-10 02:21:36 -08001605static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1606{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001607 struct vcpu_vmx *vmx = to_vmx(vcpu);
1608 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001610 vcpu->arch.shadow_efer = efer;
Joerg Roedel9f62e192008-01-31 14:57:39 +01001611 if (!msr)
1612 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613 if (efer & EFER_LMA) {
1614 vmcs_write32(VM_ENTRY_CONTROLS,
1615 vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001616 VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001617 msr->data = efer;
1618
1619 } else {
1620 vmcs_write32(VM_ENTRY_CONTROLS,
1621 vmcs_read32(VM_ENTRY_CONTROLS) &
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001622 ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001623
1624 msr->data = efer & ~EFER_LME;
1625 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001626 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627}
1628
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1630{
1631 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1632
1633 return vmcs_readl(sf->base);
1634}
1635
1636static void vmx_get_segment(struct kvm_vcpu *vcpu,
1637 struct kvm_segment *var, int seg)
1638{
1639 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1640 u32 ar;
1641
1642 var->base = vmcs_readl(sf->base);
1643 var->limit = vmcs_read32(sf->limit);
1644 var->selector = vmcs_read16(sf->selector);
1645 ar = vmcs_read32(sf->ar_bytes);
1646 if (ar & AR_UNUSABLE_MASK)
1647 ar = 0;
1648 var->type = ar & 15;
1649 var->s = (ar >> 4) & 1;
1650 var->dpl = (ar >> 5) & 3;
1651 var->present = (ar >> 7) & 1;
1652 var->avl = (ar >> 12) & 1;
1653 var->l = (ar >> 13) & 1;
1654 var->db = (ar >> 14) & 1;
1655 var->g = (ar >> 15) & 1;
1656 var->unusable = (ar >> 16) & 1;
1657}
1658
Izik Eidus2e4d2652008-03-24 19:38:34 +02001659static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1660{
1661 struct kvm_segment kvm_seg;
1662
1663 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1664 return 0;
1665
1666 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1667 return 3;
1668
1669 vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
1670 return kvm_seg.selector & 3;
1671}
1672
Avi Kivity653e3102007-05-07 10:55:37 +03001673static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001675 u32 ar;
1676
Avi Kivity653e3102007-05-07 10:55:37 +03001677 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678 ar = 1 << 16;
1679 else {
1680 ar = var->type & 15;
1681 ar |= (var->s & 1) << 4;
1682 ar |= (var->dpl & 3) << 5;
1683 ar |= (var->present & 1) << 7;
1684 ar |= (var->avl & 1) << 12;
1685 ar |= (var->l & 1) << 13;
1686 ar |= (var->db & 1) << 14;
1687 ar |= (var->g & 1) << 15;
1688 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001689 if (ar == 0) /* a 0 value means unusable */
1690 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001691
1692 return ar;
1693}
1694
1695static void vmx_set_segment(struct kvm_vcpu *vcpu,
1696 struct kvm_segment *var, int seg)
1697{
1698 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1699 u32 ar;
1700
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001701 if (vcpu->arch.rmode.active && seg == VCPU_SREG_TR) {
1702 vcpu->arch.rmode.tr.selector = var->selector;
1703 vcpu->arch.rmode.tr.base = var->base;
1704 vcpu->arch.rmode.tr.limit = var->limit;
1705 vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001706 return;
1707 }
1708 vmcs_writel(sf->base, var->base);
1709 vmcs_write32(sf->limit, var->limit);
1710 vmcs_write16(sf->selector, var->selector);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001711 if (vcpu->arch.rmode.active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001712 /*
1713 * Hack real-mode segments into vm86 compatibility.
1714 */
1715 if (var->base == 0xffff0000 && var->selector == 0xf000)
1716 vmcs_writel(sf->base, 0xf0000);
1717 ar = 0xf3;
1718 } else
1719 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720 vmcs_write32(sf->ar_bytes, ar);
1721}
1722
Avi Kivity6aa8b732006-12-10 02:21:36 -08001723static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1724{
1725 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1726
1727 *db = (ar >> 14) & 1;
1728 *l = (ar >> 13) & 1;
1729}
1730
1731static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1732{
1733 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1734 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1735}
1736
1737static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1738{
1739 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1740 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1741}
1742
1743static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1744{
1745 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1746 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1747}
1748
1749static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1750{
1751 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1752 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1753}
1754
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001755static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1756{
1757 struct kvm_segment var;
1758 u32 ar;
1759
1760 vmx_get_segment(vcpu, &var, seg);
1761 ar = vmx_segment_access_rights(&var);
1762
1763 if (var.base != (var.selector << 4))
1764 return false;
1765 if (var.limit != 0xffff)
1766 return false;
1767 if (ar != 0xf3)
1768 return false;
1769
1770 return true;
1771}
1772
1773static bool code_segment_valid(struct kvm_vcpu *vcpu)
1774{
1775 struct kvm_segment cs;
1776 unsigned int cs_rpl;
1777
1778 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1779 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1780
1781 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1782 return false;
1783 if (!cs.s)
1784 return false;
1785 if (!(~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK))) {
1786 if (cs.dpl > cs_rpl)
1787 return false;
1788 } else if (cs.type & AR_TYPE_CODE_MASK) {
1789 if (cs.dpl != cs_rpl)
1790 return false;
1791 }
1792 if (!cs.present)
1793 return false;
1794
1795 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1796 return true;
1797}
1798
1799static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1800{
1801 struct kvm_segment ss;
1802 unsigned int ss_rpl;
1803
1804 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1805 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1806
1807 if ((ss.type != 3) || (ss.type != 7))
1808 return false;
1809 if (!ss.s)
1810 return false;
1811 if (ss.dpl != ss_rpl) /* DPL != RPL */
1812 return false;
1813 if (!ss.present)
1814 return false;
1815
1816 return true;
1817}
1818
1819static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1820{
1821 struct kvm_segment var;
1822 unsigned int rpl;
1823
1824 vmx_get_segment(vcpu, &var, seg);
1825 rpl = var.selector & SELECTOR_RPL_MASK;
1826
1827 if (!var.s)
1828 return false;
1829 if (!var.present)
1830 return false;
1831 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
1832 if (var.dpl < rpl) /* DPL < RPL */
1833 return false;
1834 }
1835
1836 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1837 * rights flags
1838 */
1839 return true;
1840}
1841
1842static bool tr_valid(struct kvm_vcpu *vcpu)
1843{
1844 struct kvm_segment tr;
1845
1846 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
1847
1848 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1849 return false;
1850 if ((tr.type != 3) || (tr.type != 11)) /* TODO: Check if guest is in IA32e mode */
1851 return false;
1852 if (!tr.present)
1853 return false;
1854
1855 return true;
1856}
1857
1858static bool ldtr_valid(struct kvm_vcpu *vcpu)
1859{
1860 struct kvm_segment ldtr;
1861
1862 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
1863
1864 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1865 return false;
1866 if (ldtr.type != 2)
1867 return false;
1868 if (!ldtr.present)
1869 return false;
1870
1871 return true;
1872}
1873
1874static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
1875{
1876 struct kvm_segment cs, ss;
1877
1878 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1879 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1880
1881 return ((cs.selector & SELECTOR_RPL_MASK) ==
1882 (ss.selector & SELECTOR_RPL_MASK));
1883}
1884
1885/*
1886 * Check if guest state is valid. Returns true if valid, false if
1887 * not.
1888 * We assume that registers are always usable
1889 */
1890static bool guest_state_valid(struct kvm_vcpu *vcpu)
1891{
1892 /* real mode guest state checks */
1893 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
1894 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
1895 return false;
1896 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
1897 return false;
1898 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
1899 return false;
1900 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
1901 return false;
1902 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
1903 return false;
1904 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
1905 return false;
1906 } else {
1907 /* protected mode guest state checks */
1908 if (!cs_ss_rpl_check(vcpu))
1909 return false;
1910 if (!code_segment_valid(vcpu))
1911 return false;
1912 if (!stack_segment_valid(vcpu))
1913 return false;
1914 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
1915 return false;
1916 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
1917 return false;
1918 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
1919 return false;
1920 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
1921 return false;
1922 if (!tr_valid(vcpu))
1923 return false;
1924 if (!ldtr_valid(vcpu))
1925 return false;
1926 }
1927 /* TODO:
1928 * - Add checks on RIP
1929 * - Add checks on RFLAGS
1930 */
1931
1932 return true;
1933}
1934
Mike Dayd77c26f2007-10-08 09:02:08 -04001935static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001937 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001938 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001939 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02001940 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001941
Izik Eidus195aefd2007-10-01 22:14:18 +02001942 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1943 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001944 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001945 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08001946 r = kvm_write_guest_page(kvm, fn++, &data,
1947 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02001948 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001949 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001950 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
1951 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001952 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001953 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1954 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001955 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001956 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001957 r = kvm_write_guest_page(kvm, fn, &data,
1958 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
1959 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02001960 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001961 goto out;
1962
1963 ret = 1;
1964out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001965 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966}
1967
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001968static int init_rmode_identity_map(struct kvm *kvm)
1969{
1970 int i, r, ret;
1971 pfn_t identity_map_pfn;
1972 u32 tmp;
1973
1974 if (!vm_need_ept())
1975 return 1;
1976 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
1977 printk(KERN_ERR "EPT: identity-mapping pagetable "
1978 "haven't been allocated!\n");
1979 return 0;
1980 }
1981 if (likely(kvm->arch.ept_identity_pagetable_done))
1982 return 1;
1983 ret = 0;
1984 identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT;
1985 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
1986 if (r < 0)
1987 goto out;
1988 /* Set up identity-mapping pagetable for EPT in real mode */
1989 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
1990 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
1991 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
1992 r = kvm_write_guest_page(kvm, identity_map_pfn,
1993 &tmp, i * sizeof(tmp), sizeof(tmp));
1994 if (r < 0)
1995 goto out;
1996 }
1997 kvm->arch.ept_identity_pagetable_done = true;
1998 ret = 1;
1999out:
2000 return ret;
2001}
2002
Avi Kivity6aa8b732006-12-10 02:21:36 -08002003static void seg_setup(int seg)
2004{
2005 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2006
2007 vmcs_write16(sf->selector, 0);
2008 vmcs_writel(sf->base, 0);
2009 vmcs_write32(sf->limit, 0xffff);
Avi Kivitya16b20d2008-08-20 15:48:27 +03002010 vmcs_write32(sf->ar_bytes, 0xf3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002011}
2012
Sheng Yangf78e0e22007-10-29 09:40:42 +08002013static int alloc_apic_access_page(struct kvm *kvm)
2014{
2015 struct kvm_userspace_memory_region kvm_userspace_mem;
2016 int r = 0;
2017
Izik Eidus72dc67a2008-02-10 18:04:15 +02002018 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002019 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002020 goto out;
2021 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2022 kvm_userspace_mem.flags = 0;
2023 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2024 kvm_userspace_mem.memory_size = PAGE_SIZE;
2025 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2026 if (r)
2027 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002028
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002029 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002030out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002031 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002032 return r;
2033}
2034
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002035static int alloc_identity_pagetable(struct kvm *kvm)
2036{
2037 struct kvm_userspace_memory_region kvm_userspace_mem;
2038 int r = 0;
2039
2040 down_write(&kvm->slots_lock);
2041 if (kvm->arch.ept_identity_pagetable)
2042 goto out;
2043 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2044 kvm_userspace_mem.flags = 0;
2045 kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
2046 kvm_userspace_mem.memory_size = PAGE_SIZE;
2047 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2048 if (r)
2049 goto out;
2050
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002051 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
2052 VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002053out:
2054 up_write(&kvm->slots_lock);
2055 return r;
2056}
2057
Sheng Yang2384d2b2008-01-17 15:14:33 +08002058static void allocate_vpid(struct vcpu_vmx *vmx)
2059{
2060 int vpid;
2061
2062 vmx->vpid = 0;
2063 if (!enable_vpid || !cpu_has_vmx_vpid())
2064 return;
2065 spin_lock(&vmx_vpid_lock);
2066 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2067 if (vpid < VMX_NR_VPIDS) {
2068 vmx->vpid = vpid;
2069 __set_bit(vpid, vmx_vpid_bitmap);
2070 }
2071 spin_unlock(&vmx_vpid_lock);
2072}
2073
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002074static void vmx_disable_intercept_for_msr(struct page *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002075{
2076 void *va;
2077
2078 if (!cpu_has_vmx_msr_bitmap())
2079 return;
2080
2081 /*
2082 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2083 * have the write-low and read-high bitmap offsets the wrong way round.
2084 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2085 */
2086 va = kmap(msr_bitmap);
2087 if (msr <= 0x1fff) {
2088 __clear_bit(msr, va + 0x000); /* read-low */
2089 __clear_bit(msr, va + 0x800); /* write-low */
2090 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2091 msr &= 0x1fff;
2092 __clear_bit(msr, va + 0x400); /* read-high */
2093 __clear_bit(msr, va + 0xc00); /* write-high */
2094 }
2095 kunmap(msr_bitmap);
2096}
2097
Avi Kivity6aa8b732006-12-10 02:21:36 -08002098/*
2099 * Sets up the vmcs for emulated real mode.
2100 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002101static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002102{
Sheng Yang468d4722008-10-09 16:01:55 +08002103 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104 u32 junk;
Sheng Yang468d4722008-10-09 16:01:55 +08002105 u64 host_pat;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106 unsigned long a;
2107 struct descriptor_table dt;
2108 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002109 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002110 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03002113 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
2114 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115
Sheng Yang25c5f222008-03-28 13:18:56 +08002116 if (cpu_has_vmx_msr_bitmap())
2117 vmcs_write64(MSR_BITMAP, page_to_phys(vmx_msr_bitmap));
2118
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2120
Avi Kivity6aa8b732006-12-10 02:21:36 -08002121 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002122 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2123 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002124
2125 exec_control = vmcs_config.cpu_based_exec_ctrl;
2126 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2127 exec_control &= ~CPU_BASED_TPR_SHADOW;
2128#ifdef CONFIG_X86_64
2129 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2130 CPU_BASED_CR8_LOAD_EXITING;
2131#endif
2132 }
Sheng Yangd56f5462008-04-25 10:13:16 +08002133 if (!vm_need_ept())
2134 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002135 CPU_BASED_CR3_LOAD_EXITING |
2136 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002137 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002138
Sheng Yang83ff3b92007-11-21 14:33:25 +08002139 if (cpu_has_secondary_exec_ctrls()) {
2140 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2141 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2142 exec_control &=
2143 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002144 if (vmx->vpid == 0)
2145 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yangd56f5462008-04-25 10:13:16 +08002146 if (!vm_need_ept())
2147 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002148 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2149 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002150
Avi Kivityc7addb92007-09-16 18:58:32 +02002151 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2152 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2154
2155 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2156 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2157 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2158
2159 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2160 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2161 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002162 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2163 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002164 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002165#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002166 rdmsrl(MSR_FS_BASE, a);
2167 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2168 rdmsrl(MSR_GS_BASE, a);
2169 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2170#else
2171 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2172 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2173#endif
2174
2175 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2176
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002177 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002178 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2179
Mike Dayd77c26f2007-10-08 09:02:08 -04002180 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002181 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002182 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2183 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2184 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002185
2186 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2187 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2188 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2189 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2190 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2191 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2192
Sheng Yang468d4722008-10-09 16:01:55 +08002193 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2194 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2195 host_pat = msr_low | ((u64) msr_high << 32);
2196 vmcs_write64(HOST_IA32_PAT, host_pat);
2197 }
2198 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2199 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2200 host_pat = msr_low | ((u64) msr_high << 32);
2201 /* Write the default value follow host pat */
2202 vmcs_write64(GUEST_IA32_PAT, host_pat);
2203 /* Keep arch.pat sync with GUEST_IA32_PAT */
2204 vmx->vcpu.arch.pat = host_pat;
2205 }
2206
Avi Kivity6aa8b732006-12-10 02:21:36 -08002207 for (i = 0; i < NR_VMX_MSR; ++i) {
2208 u32 index = vmx_msr_index[i];
2209 u32 data_low, data_high;
2210 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002211 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002212
2213 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2214 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002215 if (wrmsr_safe(index, data_low, data_high) < 0)
2216 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002217 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002218 vmx->host_msrs[j].index = index;
2219 vmx->host_msrs[j].reserved = 0;
2220 vmx->host_msrs[j].data = data;
2221 vmx->guest_msrs[j] = vmx->host_msrs[j];
2222 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002224
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002225 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002226
2227 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002228 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2229
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002230 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2231 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2232
Sheng Yangf78e0e22007-10-29 09:40:42 +08002233
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002234 return 0;
2235}
2236
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002237static int init_rmode(struct kvm *kvm)
2238{
2239 if (!init_rmode_tss(kvm))
2240 return 0;
2241 if (!init_rmode_identity_map(kvm))
2242 return 0;
2243 return 1;
2244}
2245
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002246static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2247{
2248 struct vcpu_vmx *vmx = to_vmx(vcpu);
2249 u64 msr;
2250 int ret;
2251
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002252 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002253 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002254 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002255 ret = -ENOMEM;
2256 goto out;
2257 }
2258
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002259 vmx->vcpu.arch.rmode.active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002260
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002261 vmx->soft_vnmi_blocked = 0;
2262
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002263 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002264 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002265 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
2266 if (vmx->vcpu.vcpu_id == 0)
2267 msr |= MSR_IA32_APICBASE_BSP;
2268 kvm_set_apic_base(&vmx->vcpu, msr);
2269
2270 fx_init(&vmx->vcpu);
2271
Avi Kivity5706be02008-08-20 15:07:31 +03002272 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002273 /*
2274 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2275 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2276 */
2277 if (vmx->vcpu.vcpu_id == 0) {
2278 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2279 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2280 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002281 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2282 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002283 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002284
2285 seg_setup(VCPU_SREG_DS);
2286 seg_setup(VCPU_SREG_ES);
2287 seg_setup(VCPU_SREG_FS);
2288 seg_setup(VCPU_SREG_GS);
2289 seg_setup(VCPU_SREG_SS);
2290
2291 vmcs_write16(GUEST_TR_SELECTOR, 0);
2292 vmcs_writel(GUEST_TR_BASE, 0);
2293 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2294 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2295
2296 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2297 vmcs_writel(GUEST_LDTR_BASE, 0);
2298 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2299 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2300
2301 vmcs_write32(GUEST_SYSENTER_CS, 0);
2302 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2303 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2304
2305 vmcs_writel(GUEST_RFLAGS, 0x02);
2306 if (vmx->vcpu.vcpu_id == 0)
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002307 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002308 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002309 kvm_rip_write(vcpu, 0);
2310 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002311
2312 /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */
2313 vmcs_writel(GUEST_DR7, 0x400);
2314
2315 vmcs_writel(GUEST_GDTR_BASE, 0);
2316 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2317
2318 vmcs_writel(GUEST_IDTR_BASE, 0);
2319 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2320
2321 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2322 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2323 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2324
2325 guest_write_tsc(0);
2326
2327 /* Special registers */
2328 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2329
2330 setup_msrs(vmx);
2331
Avi Kivity6aa8b732006-12-10 02:21:36 -08002332 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2333
Sheng Yangf78e0e22007-10-29 09:40:42 +08002334 if (cpu_has_vmx_tpr_shadow()) {
2335 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2336 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2337 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002338 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002339 vmcs_write32(TPR_THRESHOLD, 0);
2340 }
2341
2342 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2343 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002344 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002345
Sheng Yang2384d2b2008-01-17 15:14:33 +08002346 if (vmx->vpid != 0)
2347 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2348
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002349 vmx->vcpu.arch.cr0 = 0x60000010;
2350 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002351 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002352 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002353 vmx_fpu_activate(&vmx->vcpu);
2354 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355
Sheng Yang2384d2b2008-01-17 15:14:33 +08002356 vpid_sync_vcpu_all(vmx);
2357
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002358 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002360 /* HACK: Don't enable emulation on guest boot/reset */
2361 vmx->emulation_required = 0;
2362
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002364 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002365 return ret;
2366}
2367
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002368static void enable_irq_window(struct kvm_vcpu *vcpu)
2369{
2370 u32 cpu_based_vm_exec_control;
2371
2372 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2373 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2374 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2375}
2376
2377static void enable_nmi_window(struct kvm_vcpu *vcpu)
2378{
2379 u32 cpu_based_vm_exec_control;
2380
2381 if (!cpu_has_virtual_nmis()) {
2382 enable_irq_window(vcpu);
2383 return;
2384 }
2385
2386 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2387 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2388 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2389}
2390
Eddie Dong85f455f2007-07-06 12:20:49 +03002391static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
2392{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002393 struct vcpu_vmx *vmx = to_vmx(vcpu);
2394
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002395 KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler);
2396
Avi Kivityfa89a812008-09-01 15:57:51 +03002397 ++vcpu->stat.irq_injections;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002398 if (vcpu->arch.rmode.active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002399 vmx->rmode.irq.pending = true;
2400 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002401 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Avi Kivity9c5623e2007-11-08 18:19:20 +02002402 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2403 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2404 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002405 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002406 return;
2407 }
2408 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2409 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
2410}
2411
Sheng Yangf08864b2008-05-15 18:23:25 +08002412static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2413{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002414 struct vcpu_vmx *vmx = to_vmx(vcpu);
2415
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002416 if (!cpu_has_virtual_nmis()) {
2417 /*
2418 * Tracking the NMI-blocked state in software is built upon
2419 * finding the next open IRQ window. This, in turn, depends on
2420 * well-behaving guests: They have to keep IRQs disabled at
2421 * least as long as the NMI handler runs. Otherwise we may
2422 * cause NMI nesting, maybe breaking the guest. But as this is
2423 * highly unlikely, we can live with the residual risk.
2424 */
2425 vmx->soft_vnmi_blocked = 1;
2426 vmx->vnmi_blocked_time = 0;
2427 }
2428
Jan Kiszka487b3912008-09-26 09:30:56 +02002429 ++vcpu->stat.nmi_injections;
Jan Kiszka66a5a342008-09-26 09:30:51 +02002430 if (vcpu->arch.rmode.active) {
2431 vmx->rmode.irq.pending = true;
2432 vmx->rmode.irq.vector = NMI_VECTOR;
2433 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2434 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2435 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2436 INTR_INFO_VALID_MASK);
2437 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2438 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2439 return;
2440 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002441 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2442 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002443}
2444
Jan Kiszka33f089c2008-09-26 09:30:49 +02002445static void vmx_update_window_states(struct kvm_vcpu *vcpu)
2446{
2447 u32 guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2448
2449 vcpu->arch.nmi_window_open =
2450 !(guest_intr & (GUEST_INTR_STATE_STI |
2451 GUEST_INTR_STATE_MOV_SS |
2452 GUEST_INTR_STATE_NMI));
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002453 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
2454 vcpu->arch.nmi_window_open = 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002455
2456 vcpu->arch.interrupt_window_open =
2457 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2458 !(guest_intr & (GUEST_INTR_STATE_STI |
2459 GUEST_INTR_STATE_MOV_SS)));
2460}
2461
Avi Kivity6aa8b732006-12-10 02:21:36 -08002462static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
2463{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002464 int word_index = __ffs(vcpu->arch.irq_summary);
2465 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466 int irq = word_index * BITS_PER_LONG + bit_index;
2467
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002468 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
2469 if (!vcpu->arch.irq_pending[word_index])
2470 clear_bit(word_index, &vcpu->arch.irq_summary);
Avi Kivityecfc79c2008-08-14 11:13:16 +03002471 kvm_queue_interrupt(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002472}
2473
Jan Kiszkaf460ee42008-09-26 09:30:50 +02002474static void do_interrupt_requests(struct kvm_vcpu *vcpu,
2475 struct kvm_run *kvm_run)
2476{
2477 vmx_update_window_states(vcpu);
2478
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002479 if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
2480 if (vcpu->arch.nmi_window_open) {
2481 vcpu->arch.nmi_pending = false;
2482 vcpu->arch.nmi_injected = true;
2483 } else {
2484 enable_nmi_window(vcpu);
Jan Kiszka487b3912008-09-26 09:30:56 +02002485 return;
2486 }
Jan Kiszka487b3912008-09-26 09:30:56 +02002487 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002488 if (vcpu->arch.nmi_injected) {
2489 vmx_inject_nmi(vcpu);
2490 if (vcpu->arch.nmi_pending || kvm_run->request_nmi_window)
2491 enable_nmi_window(vcpu);
2492 else if (vcpu->arch.irq_summary
2493 || kvm_run->request_interrupt_window)
2494 enable_irq_window(vcpu);
2495 return;
2496 }
2497 if (!vcpu->arch.nmi_window_open || kvm_run->request_nmi_window)
2498 enable_nmi_window(vcpu);
Jan Kiszka487b3912008-09-26 09:30:56 +02002499
Jan Kiszkaf460ee42008-09-26 09:30:50 +02002500 if (vcpu->arch.interrupt_window_open) {
2501 if (vcpu->arch.irq_summary && !vcpu->arch.interrupt.pending)
2502 kvm_do_inject_irq(vcpu);
2503
2504 if (vcpu->arch.interrupt.pending)
2505 vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
2506 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002507 if (!vcpu->arch.interrupt_window_open &&
2508 (vcpu->arch.irq_summary || kvm_run->request_interrupt_window))
Jan Kiszkaf460ee42008-09-26 09:30:50 +02002509 enable_irq_window(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002510}
2511
Izik Eiduscbc94022007-10-25 00:29:55 +02002512static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2513{
2514 int ret;
2515 struct kvm_userspace_memory_region tss_mem = {
2516 .slot = 8,
2517 .guest_phys_addr = addr,
2518 .memory_size = PAGE_SIZE * 3,
2519 .flags = 0,
2520 };
2521
2522 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2523 if (ret)
2524 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002525 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002526 return 0;
2527}
2528
Avi Kivity6aa8b732006-12-10 02:21:36 -08002529static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
2530{
2531 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
2532
2533 set_debugreg(dbg->bp[0], 0);
2534 set_debugreg(dbg->bp[1], 1);
2535 set_debugreg(dbg->bp[2], 2);
2536 set_debugreg(dbg->bp[3], 3);
2537
2538 if (dbg->singlestep) {
2539 unsigned long flags;
2540
2541 flags = vmcs_readl(GUEST_RFLAGS);
2542 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
2543 vmcs_writel(GUEST_RFLAGS, flags);
2544 }
2545}
2546
2547static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2548 int vec, u32 err_code)
2549{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002550 /*
2551 * Instruction with address size override prefix opcode 0x67
2552 * Cause the #SS fault with 0 error code in VM86 mode.
2553 */
2554 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02002555 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002556 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002557 /*
2558 * Forward all other exceptions that are valid in real mode.
2559 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2560 * the required debugging infrastructure rework.
2561 */
2562 switch (vec) {
2563 case DE_VECTOR:
2564 case DB_VECTOR:
2565 case BP_VECTOR:
2566 case OF_VECTOR:
2567 case BR_VECTOR:
2568 case UD_VECTOR:
2569 case DF_VECTOR:
2570 case SS_VECTOR:
2571 case GP_VECTOR:
2572 case MF_VECTOR:
2573 kvm_queue_exception(vcpu, vec);
2574 return 1;
2575 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002576 return 0;
2577}
2578
2579static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2580{
Avi Kivity1155f762007-11-22 11:30:47 +02002581 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582 u32 intr_info, error_code;
2583 unsigned long cr2, rip;
2584 u32 vect_info;
2585 enum emulation_result er;
2586
Avi Kivity1155f762007-11-22 11:30:47 +02002587 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002588 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2589
2590 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002591 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002592 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002593 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002594
Eddie Dong85f455f2007-07-06 12:20:49 +03002595 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002596 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002597 set_bit(irq, vcpu->arch.irq_pending);
2598 set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002599 }
2600
Jan Kiszkae4a41882008-09-26 09:30:46 +02002601 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002602 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002603
2604 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002605 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002606 return 1;
2607 }
2608
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002609 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08002610 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002611 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002612 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002613 return 1;
2614 }
2615
Avi Kivity6aa8b732006-12-10 02:21:36 -08002616 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002617 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002618 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002619 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2620 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002621 /* EPT won't cause page fault directly */
2622 if (vm_need_ept())
2623 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002624 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002625 KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2,
2626 (u32)((u64)cr2 >> 32), handler);
Avi Kivityf7d92382008-07-03 16:14:28 +03002627 if (vcpu->arch.interrupt.pending || vcpu->arch.exception.pending)
Avi Kivity577bdc42008-07-19 08:57:05 +03002628 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002629 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630 }
2631
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002632 if (vcpu->arch.rmode.active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002633 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002634 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002635 if (vcpu->arch.halt_request) {
2636 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002637 return kvm_emulate_halt(vcpu);
2638 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002639 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002640 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641
Mike Dayd77c26f2007-10-08 09:02:08 -04002642 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) ==
2643 (INTR_TYPE_EXCEPTION | 1)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2645 return 0;
2646 }
2647 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2648 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
2649 kvm_run->ex.error_code = error_code;
2650 return 0;
2651}
2652
2653static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2654 struct kvm_run *kvm_run)
2655{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002656 ++vcpu->stat.irq_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002657 KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002658 return 1;
2659}
2660
Avi Kivity988ad742007-02-12 00:54:36 -08002661static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2662{
2663 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2664 return 0;
2665}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002666
Avi Kivity6aa8b732006-12-10 02:21:36 -08002667static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2668{
He, Qingbfdaab02007-09-12 14:18:28 +08002669 unsigned long exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02002670 int size, down, in, string, rep;
2671 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672
Avi Kivity1165f5f2007-04-19 17:27:43 +03002673 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002674 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002675 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002676
2677 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002678 if (emulate_instruction(vcpu,
2679 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002680 return 0;
2681 return 1;
2682 }
2683
2684 size = (exit_qualification & 7) + 1;
2685 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002686 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002687 rep = (exit_qualification & 32) != 0;
2688 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002689
Laurent Vivier3090dd72007-08-05 10:43:32 +03002690 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002691}
2692
Ingo Molnar102d8322007-02-19 14:37:47 +02002693static void
2694vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2695{
2696 /*
2697 * Patch in the VMCALL instruction:
2698 */
2699 hypercall[0] = 0x0f;
2700 hypercall[1] = 0x01;
2701 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002702}
2703
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2705{
He, Qingbfdaab02007-09-12 14:18:28 +08002706 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707 int cr;
2708 int reg;
2709
He, Qingbfdaab02007-09-12 14:18:28 +08002710 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711 cr = exit_qualification & 15;
2712 reg = (exit_qualification >> 8) & 15;
2713 switch ((exit_qualification >> 4) & 3) {
2714 case 0: /* mov to cr */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002715 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr,
2716 (u32)kvm_register_read(vcpu, reg),
2717 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
2718 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719 switch (cr) {
2720 case 0:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002721 kvm_set_cr0(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002722 skip_emulated_instruction(vcpu);
2723 return 1;
2724 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002725 kvm_set_cr3(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002726 skip_emulated_instruction(vcpu);
2727 return 1;
2728 case 4:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002729 kvm_set_cr4(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002730 skip_emulated_instruction(vcpu);
2731 return 1;
2732 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002733 kvm_set_cr8(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002734 skip_emulated_instruction(vcpu);
Avi Kivitye5314062007-12-06 16:32:45 +02002735 if (irqchip_in_kernel(vcpu->kvm))
2736 return 1;
Yang, Sheng253abde2007-08-16 13:01:00 +03002737 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2738 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739 };
2740 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002741 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002742 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002743 vcpu->arch.cr0 &= ~X86_CR0_TS;
2744 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002745 vmx_fpu_activate(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002746 KVMTRACE_0D(CLTS, vcpu, handler);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002747 skip_emulated_instruction(vcpu);
2748 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002749 case 1: /*mov from cr*/
2750 switch (cr) {
2751 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002752 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002753 KVMTRACE_3D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002754 (u32)kvm_register_read(vcpu, reg),
2755 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002756 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002757 skip_emulated_instruction(vcpu);
2758 return 1;
2759 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002760 kvm_register_write(vcpu, reg, kvm_get_cr8(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002761 KVMTRACE_2D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002762 (u32)kvm_register_read(vcpu, reg), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002763 skip_emulated_instruction(vcpu);
2764 return 1;
2765 }
2766 break;
2767 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002768 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002769
2770 skip_emulated_instruction(vcpu);
2771 return 1;
2772 default:
2773 break;
2774 }
2775 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002776 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777 (int)(exit_qualification >> 4) & 3, cr);
2778 return 0;
2779}
2780
2781static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2782{
He, Qingbfdaab02007-09-12 14:18:28 +08002783 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002784 unsigned long val;
2785 int dr, reg;
2786
2787 /*
2788 * FIXME: this code assumes the host is debugging the guest.
2789 * need to deal with guest debugging itself too.
2790 */
He, Qingbfdaab02007-09-12 14:18:28 +08002791 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002792 dr = exit_qualification & 7;
2793 reg = (exit_qualification >> 8) & 15;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002794 if (exit_qualification & 16) {
2795 /* mov from dr */
2796 switch (dr) {
2797 case 6:
2798 val = 0xffff0ff0;
2799 break;
2800 case 7:
2801 val = 0x400;
2802 break;
2803 default:
2804 val = 0;
2805 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002806 kvm_register_write(vcpu, reg, val);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002807 KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808 } else {
2809 /* mov to dr */
2810 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811 skip_emulated_instruction(vcpu);
2812 return 1;
2813}
2814
2815static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2816{
Avi Kivity06465c52007-02-28 20:46:53 +02002817 kvm_emulate_cpuid(vcpu);
2818 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819}
2820
2821static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2822{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002823 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002824 u64 data;
2825
2826 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002827 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828 return 1;
2829 }
2830
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002831 KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32),
2832 handler);
2833
Avi Kivity6aa8b732006-12-10 02:21:36 -08002834 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002835 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
2836 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002837 skip_emulated_instruction(vcpu);
2838 return 1;
2839}
2840
2841static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2842{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002843 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
2844 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
2845 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002847 KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32),
2848 handler);
2849
Avi Kivity6aa8b732006-12-10 02:21:36 -08002850 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002851 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002852 return 1;
2853 }
2854
2855 skip_emulated_instruction(vcpu);
2856 return 1;
2857}
2858
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002859static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2860 struct kvm_run *kvm_run)
2861{
2862 return 1;
2863}
2864
Avi Kivity6aa8b732006-12-10 02:21:36 -08002865static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2866 struct kvm_run *kvm_run)
2867{
Eddie Dong85f455f2007-07-06 12:20:49 +03002868 u32 cpu_based_vm_exec_control;
2869
2870 /* clear pending irq */
2871 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2872 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2873 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002874
2875 KVMTRACE_0D(PEND_INTR, vcpu, handler);
Jan Kiszkaa26bf122008-09-26 09:30:45 +02002876 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002877
Dor Laorc1150d82007-01-05 16:36:24 -08002878 /*
2879 * If the user space waits to inject interrupts, exit as soon as
2880 * possible
2881 */
2882 if (kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002883 !vcpu->arch.irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002884 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08002885 return 0;
2886 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887 return 1;
2888}
2889
2890static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2891{
2892 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002893 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002894}
2895
Ingo Molnarc21415e2007-02-19 14:37:47 +02002896static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2897{
Dor Laor510043d2007-02-19 18:25:43 +02002898 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002899 kvm_emulate_hypercall(vcpu);
2900 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02002901}
2902
Marcelo Tosattia7052892008-09-23 13:18:35 -03002903static int handle_invlpg(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2904{
2905 u64 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2906
2907 kvm_mmu_invlpg(vcpu, exit_qualification);
2908 skip_emulated_instruction(vcpu);
2909 return 1;
2910}
2911
Eddie Donge5edaa02007-11-11 12:28:35 +02002912static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2913{
2914 skip_emulated_instruction(vcpu);
2915 /* TODO: Add support for VT-d/pass-through device */
2916 return 1;
2917}
2918
Sheng Yangf78e0e22007-10-29 09:40:42 +08002919static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2920{
2921 u64 exit_qualification;
2922 enum emulation_result er;
2923 unsigned long offset;
2924
2925 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2926 offset = exit_qualification & 0xffful;
2927
2928 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
2929
2930 if (er != EMULATE_DONE) {
2931 printk(KERN_ERR
2932 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
2933 offset);
2934 return -ENOTSUPP;
2935 }
2936 return 1;
2937}
2938
Izik Eidus37817f22008-03-24 23:14:53 +02002939static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2940{
Jan Kiszka60637aa2008-09-26 09:30:47 +02002941 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02002942 unsigned long exit_qualification;
2943 u16 tss_selector;
2944 int reason;
2945
2946 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
2947
2948 reason = (u32)exit_qualification >> 30;
Jan Kiszka60637aa2008-09-26 09:30:47 +02002949 if (reason == TASK_SWITCH_GATE && vmx->vcpu.arch.nmi_injected &&
2950 (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
2951 (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK)
2952 == INTR_TYPE_NMI_INTR) {
2953 vcpu->arch.nmi_injected = false;
2954 if (cpu_has_virtual_nmis())
2955 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
2956 GUEST_INTR_STATE_NMI);
2957 }
Izik Eidus37817f22008-03-24 23:14:53 +02002958 tss_selector = exit_qualification;
2959
2960 return kvm_task_switch(vcpu, tss_selector, reason);
2961}
2962
Sheng Yang14394422008-04-28 12:24:45 +08002963static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2964{
2965 u64 exit_qualification;
2966 enum emulation_result er;
2967 gpa_t gpa;
2968 unsigned long hva;
2969 int gla_validity;
2970 int r;
2971
2972 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2973
2974 if (exit_qualification & (1 << 6)) {
2975 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
2976 return -ENOTSUPP;
2977 }
2978
2979 gla_validity = (exit_qualification >> 7) & 0x3;
2980 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
2981 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
2982 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
2983 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
2984 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
2985 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
2986 (long unsigned int)exit_qualification);
2987 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2988 kvm_run->hw.hardware_exit_reason = 0;
2989 return -ENOTSUPP;
2990 }
2991
2992 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
2993 hva = gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT);
2994 if (!kvm_is_error_hva(hva)) {
2995 r = kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
2996 if (r < 0) {
2997 printk(KERN_ERR "EPT: Not enough memory!\n");
2998 return -ENOMEM;
2999 }
3000 return 1;
3001 } else {
3002 /* must be MMIO */
3003 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3004
3005 if (er == EMULATE_FAIL) {
3006 printk(KERN_ERR
3007 "EPT: Fail to handle EPT violation vmexit!er is %d\n",
3008 er);
3009 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3010 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
3011 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
3012 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3013 (long unsigned int)exit_qualification);
3014 return -ENOTSUPP;
3015 } else if (er == EMULATE_DO_MMIO)
3016 return 0;
3017 }
3018 return 1;
3019}
3020
Sheng Yangf08864b2008-05-15 18:23:25 +08003021static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3022{
3023 u32 cpu_based_vm_exec_control;
3024
3025 /* clear pending NMI */
3026 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3027 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3028 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3029 ++vcpu->stat.nmi_window_exits;
3030
Jan Kiszka487b3912008-09-26 09:30:56 +02003031 /*
3032 * If the user space waits to inject a NMI, exit as soon as possible
3033 */
3034 if (kvm_run->request_nmi_window && !vcpu->arch.nmi_pending) {
3035 kvm_run->exit_reason = KVM_EXIT_NMI_WINDOW_OPEN;
3036 return 0;
3037 }
3038
Sheng Yangf08864b2008-05-15 18:23:25 +08003039 return 1;
3040}
3041
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003042static void handle_invalid_guest_state(struct kvm_vcpu *vcpu,
3043 struct kvm_run *kvm_run)
3044{
3045 struct vcpu_vmx *vmx = to_vmx(vcpu);
3046 int err;
3047
3048 preempt_enable();
3049 local_irq_enable();
3050
3051 while (!guest_state_valid(vcpu)) {
3052 err = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3053
3054 switch (err) {
3055 case EMULATE_DONE:
3056 break;
3057 case EMULATE_DO_MMIO:
3058 kvm_report_emulation_failure(vcpu, "mmio");
3059 /* TODO: Handle MMIO */
3060 return;
3061 default:
3062 kvm_report_emulation_failure(vcpu, "emulation failure");
3063 return;
3064 }
3065
3066 if (signal_pending(current))
3067 break;
3068 if (need_resched())
3069 schedule();
3070 }
3071
3072 local_irq_disable();
3073 preempt_disable();
3074
3075 /* Guest state should be valid now, no more emulation should be needed */
3076 vmx->emulation_required = 0;
3077}
3078
Avi Kivity6aa8b732006-12-10 02:21:36 -08003079/*
3080 * The exit handlers return 1 if the exit was handled fully and guest execution
3081 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3082 * to be done to userspace and return 0.
3083 */
3084static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
3085 struct kvm_run *kvm_run) = {
3086 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3087 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003088 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003089 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003090 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003091 [EXIT_REASON_CR_ACCESS] = handle_cr,
3092 [EXIT_REASON_DR_ACCESS] = handle_dr,
3093 [EXIT_REASON_CPUID] = handle_cpuid,
3094 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3095 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3096 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3097 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003098 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003099 [EXIT_REASON_VMCALL] = handle_vmcall,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003100 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3101 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003102 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003103 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Sheng Yang14394422008-04-28 12:24:45 +08003104 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003105};
3106
3107static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003108 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003109
3110/*
3111 * The guest has exited. See if we can fix it or if we need userspace
3112 * assistance.
3113 */
3114static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
3115{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003116 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity29bd8a72007-09-10 17:27:03 +03003117 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1155f762007-11-22 11:30:47 +02003118 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003119
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003120 KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)kvm_rip_read(vcpu),
3121 (u32)((u64)kvm_rip_read(vcpu) >> 32), entryexit);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003122
Sheng Yang14394422008-04-28 12:24:45 +08003123 /* Access CR3 don't cause VMExit in paging mode, so we need
3124 * to sync with guest real CR3. */
3125 if (vm_need_ept() && is_paging(vcpu)) {
3126 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3127 ept_load_pdptrs(vcpu);
3128 }
3129
Avi Kivity29bd8a72007-09-10 17:27:03 +03003130 if (unlikely(vmx->fail)) {
3131 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3132 kvm_run->fail_entry.hardware_entry_failure_reason
3133 = vmcs_read32(VM_INSTRUCTION_ERROR);
3134 return 0;
3135 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003136
Mike Dayd77c26f2007-10-08 09:02:08 -04003137 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003138 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003139 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3140 exit_reason != EXIT_REASON_TASK_SWITCH))
3141 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3142 "(0x%x) and exit reason is 0x%x\n",
3143 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003144
3145 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
3146 if (vcpu->arch.interrupt_window_open) {
3147 vmx->soft_vnmi_blocked = 0;
3148 vcpu->arch.nmi_window_open = 1;
3149 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
3150 (kvm_run->request_nmi_window || vcpu->arch.nmi_pending)) {
3151 /*
3152 * This CPU don't support us in finding the end of an
3153 * NMI-blocked window if the guest runs with IRQs
3154 * disabled. So we pull the trigger after 1 s of
3155 * futile waiting, but inform the user about this.
3156 */
3157 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3158 "state on VCPU %d after 1 s timeout\n",
3159 __func__, vcpu->vcpu_id);
3160 vmx->soft_vnmi_blocked = 0;
3161 vmx->vcpu.arch.nmi_window_open = 1;
3162 }
3163
3164 /*
3165 * If the user space waits to inject an NNI, exit ASAP
3166 */
3167 if (vcpu->arch.nmi_window_open && kvm_run->request_nmi_window
3168 && !vcpu->arch.nmi_pending) {
3169 kvm_run->exit_reason = KVM_EXIT_NMI_WINDOW_OPEN;
3170 ++vcpu->stat.nmi_window_exits;
3171 return 0;
3172 }
3173 }
3174
Avi Kivity6aa8b732006-12-10 02:21:36 -08003175 if (exit_reason < kvm_vmx_max_exit_handlers
3176 && kvm_vmx_exit_handlers[exit_reason])
3177 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
3178 else {
3179 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3180 kvm_run->hw.hardware_exit_reason = exit_reason;
3181 }
3182 return 0;
3183}
3184
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003185static void update_tpr_threshold(struct kvm_vcpu *vcpu)
3186{
3187 int max_irr, tpr;
3188
3189 if (!vm_need_tpr_shadow(vcpu->kvm))
3190 return;
3191
3192 if (!kvm_lapic_enabled(vcpu) ||
3193 ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
3194 vmcs_write32(TPR_THRESHOLD, 0);
3195 return;
3196 }
3197
3198 tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
3199 vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
3200}
3201
Avi Kivitycf393f72008-07-01 16:20:21 +03003202static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3203{
3204 u32 exit_intr_info;
Avi Kivity668f6122008-07-02 09:28:55 +03003205 u32 idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003206 bool unblock_nmi;
3207 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003208 int type;
3209 bool idtv_info_valid;
Avi Kivity35920a32008-07-03 14:50:12 +03003210 u32 error;
Avi Kivitycf393f72008-07-01 16:20:21 +03003211
3212 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
3213 if (cpu_has_virtual_nmis()) {
3214 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3215 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3216 /*
3217 * SDM 3: 25.7.1.2
3218 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3219 * a guest IRET fault.
3220 */
3221 if (unblock_nmi && vector != DF_VECTOR)
3222 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3223 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003224 } else if (unlikely(vmx->soft_vnmi_blocked))
3225 vmx->vnmi_blocked_time +=
3226 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003227
3228 idt_vectoring_info = vmx->idt_vectoring_info;
3229 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3230 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3231 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
3232 if (vmx->vcpu.arch.nmi_injected) {
3233 /*
3234 * SDM 3: 25.7.1.2
3235 * Clear bit "block by NMI" before VM entry if a NMI delivery
3236 * faulted.
3237 */
3238 if (idtv_info_valid && type == INTR_TYPE_NMI_INTR)
3239 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3240 GUEST_INTR_STATE_NMI);
3241 else
3242 vmx->vcpu.arch.nmi_injected = false;
3243 }
Avi Kivity35920a32008-07-03 14:50:12 +03003244 kvm_clear_exception_queue(&vmx->vcpu);
3245 if (idtv_info_valid && type == INTR_TYPE_EXCEPTION) {
3246 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
3247 error = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3248 kvm_queue_exception_e(&vmx->vcpu, vector, error);
3249 } else
3250 kvm_queue_exception(&vmx->vcpu, vector);
3251 vmx->idt_vectoring_info = 0;
3252 }
Avi Kivityf7d92382008-07-03 16:14:28 +03003253 kvm_clear_interrupt_queue(&vmx->vcpu);
3254 if (idtv_info_valid && type == INTR_TYPE_EXT_INTR) {
3255 kvm_queue_interrupt(&vmx->vcpu, vector);
3256 vmx->idt_vectoring_info = 0;
3257 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003258}
3259
Eddie Dong85f455f2007-07-06 12:20:49 +03003260static void vmx_intr_assist(struct kvm_vcpu *vcpu)
3261{
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003262 update_tpr_threshold(vcpu);
3263
Jan Kiszka33f089c2008-09-26 09:30:49 +02003264 vmx_update_window_states(vcpu);
3265
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003266 if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
3267 if (vcpu->arch.interrupt.pending) {
3268 enable_nmi_window(vcpu);
3269 } else if (vcpu->arch.nmi_window_open) {
3270 vcpu->arch.nmi_pending = false;
3271 vcpu->arch.nmi_injected = true;
3272 } else {
3273 enable_nmi_window(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003274 return;
3275 }
Sheng Yangf08864b2008-05-15 18:23:25 +08003276 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003277 if (vcpu->arch.nmi_injected) {
3278 vmx_inject_nmi(vcpu);
3279 if (vcpu->arch.nmi_pending)
3280 enable_nmi_window(vcpu);
3281 else if (kvm_cpu_has_interrupt(vcpu))
3282 enable_irq_window(vcpu);
3283 return;
3284 }
Avi Kivityf7d92382008-07-03 16:14:28 +03003285 if (!vcpu->arch.interrupt.pending && kvm_cpu_has_interrupt(vcpu)) {
Jan Kiszka33f089c2008-09-26 09:30:49 +02003286 if (vcpu->arch.interrupt_window_open)
Avi Kivityf7d92382008-07-03 16:14:28 +03003287 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu));
3288 else
3289 enable_irq_window(vcpu);
3290 }
3291 if (vcpu->arch.interrupt.pending) {
3292 vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
3293 kvm_timer_intr_post(vcpu, vcpu->arch.interrupt.nr);
3294 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003295}
3296
Avi Kivity9c8cba32007-11-22 11:42:59 +02003297/*
3298 * Failure to inject an interrupt should give us the information
3299 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3300 * when fetching the interrupt redirection bitmap in the real-mode
3301 * tss, this doesn't happen. So we do it ourselves.
3302 */
3303static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3304{
3305 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003306 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003307 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003308 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003309 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3310 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3311 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3312 return;
3313 }
3314 vmx->idt_vectoring_info =
3315 VECTORING_INFO_VALID_MASK
3316 | INTR_TYPE_EXT_INTR
3317 | vmx->rmode.irq.vector;
3318}
3319
Avi Kivityc8019492008-07-14 14:44:59 +03003320#ifdef CONFIG_X86_64
3321#define R "r"
3322#define Q "q"
3323#else
3324#define R "e"
3325#define Q "l"
3326#endif
3327
Avi Kivity04d2cc72007-09-10 18:10:54 +03003328static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003329{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003330 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1b6269d2007-10-09 12:12:19 +02003331 u32 intr_info;
Avi Kivitye6adf282007-04-30 16:07:54 +03003332
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003333 /* Record the guest's net vcpu time for enforced NMI injections. */
3334 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3335 vmx->entry_time = ktime_get();
3336
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003337 /* Handle invalid guest state instead of entering VMX */
3338 if (vmx->emulation_required && emulate_invalid_guest_state) {
3339 handle_invalid_guest_state(vcpu, kvm_run);
3340 return;
3341 }
3342
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003343 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3344 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3345 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3346 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3347
Avi Kivitye6adf282007-04-30 16:07:54 +03003348 /*
3349 * Loading guest fpu may have cleared host cr0.ts
3350 */
3351 vmcs_writel(HOST_CR0, read_cr0());
3352
Mike Dayd77c26f2007-10-08 09:02:08 -04003353 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003354 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003355 "push %%"R"dx; push %%"R"bp;"
3356 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003357 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3358 "je 1f \n\t"
3359 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003360 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003361 "1: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003362 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003363 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003364 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003365 "mov %c[cr2](%0), %%"R"ax \n\t"
3366 "mov %%"R"ax, %%cr2 \n\t"
3367 "mov %c[rax](%0), %%"R"ax \n\t"
3368 "mov %c[rbx](%0), %%"R"bx \n\t"
3369 "mov %c[rdx](%0), %%"R"dx \n\t"
3370 "mov %c[rsi](%0), %%"R"si \n\t"
3371 "mov %c[rdi](%0), %%"R"di \n\t"
3372 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003373#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003374 "mov %c[r8](%0), %%r8 \n\t"
3375 "mov %c[r9](%0), %%r9 \n\t"
3376 "mov %c[r10](%0), %%r10 \n\t"
3377 "mov %c[r11](%0), %%r11 \n\t"
3378 "mov %c[r12](%0), %%r12 \n\t"
3379 "mov %c[r13](%0), %%r13 \n\t"
3380 "mov %c[r14](%0), %%r14 \n\t"
3381 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003382#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003383 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3384
Avi Kivity6aa8b732006-12-10 02:21:36 -08003385 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003386 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003387 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003388 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003389 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003390 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003391 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003392 "xchg %0, (%%"R"sp) \n\t"
3393 "mov %%"R"ax, %c[rax](%0) \n\t"
3394 "mov %%"R"bx, %c[rbx](%0) \n\t"
3395 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3396 "mov %%"R"dx, %c[rdx](%0) \n\t"
3397 "mov %%"R"si, %c[rsi](%0) \n\t"
3398 "mov %%"R"di, %c[rdi](%0) \n\t"
3399 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003400#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003401 "mov %%r8, %c[r8](%0) \n\t"
3402 "mov %%r9, %c[r9](%0) \n\t"
3403 "mov %%r10, %c[r10](%0) \n\t"
3404 "mov %%r11, %c[r11](%0) \n\t"
3405 "mov %%r12, %c[r12](%0) \n\t"
3406 "mov %%r13, %c[r13](%0) \n\t"
3407 "mov %%r14, %c[r14](%0) \n\t"
3408 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003409#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003410 "mov %%cr2, %%"R"ax \n\t"
3411 "mov %%"R"ax, %c[cr2](%0) \n\t"
3412
3413 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003414 "setbe %c[fail](%0) \n\t"
3415 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3416 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3417 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03003418 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003419 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3420 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3421 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3422 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3423 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3424 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3425 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003426#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003427 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3428 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3429 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3430 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3431 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3432 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3433 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3434 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003435#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003436 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003437 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003438 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003439#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003440 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3441#endif
3442 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003443
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003444 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
3445 vcpu->arch.regs_dirty = 0;
3446
Avi Kivity1155f762007-11-22 11:30:47 +02003447 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003448 if (vmx->rmode.irq.pending)
3449 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003450
Jan Kiszka33f089c2008-09-26 09:30:49 +02003451 vmx_update_window_states(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003452
Mike Dayd77c26f2007-10-08 09:02:08 -04003453 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003454 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003455
3456 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
3457
3458 /* We need to handle NMIs before interrupts are enabled */
Jan Kiszkae4a41882008-09-26 09:30:46 +02003459 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Sheng Yangf08864b2008-05-15 18:23:25 +08003460 (intr_info & INTR_INFO_VALID_MASK)) {
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003461 KVMTRACE_0D(NMI, vcpu, handler);
Avi Kivity1b6269d2007-10-09 12:12:19 +02003462 asm("int $2");
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003463 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003464
3465 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003466}
3467
Avi Kivityc8019492008-07-14 14:44:59 +03003468#undef R
3469#undef Q
3470
Avi Kivity6aa8b732006-12-10 02:21:36 -08003471static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3472{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003473 struct vcpu_vmx *vmx = to_vmx(vcpu);
3474
3475 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003476 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003477 free_vmcs(vmx->vmcs);
3478 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003479 }
3480}
3481
3482static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3483{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003484 struct vcpu_vmx *vmx = to_vmx(vcpu);
3485
Sheng Yang2384d2b2008-01-17 15:14:33 +08003486 spin_lock(&vmx_vpid_lock);
3487 if (vmx->vpid != 0)
3488 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3489 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003490 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003491 kfree(vmx->host_msrs);
3492 kfree(vmx->guest_msrs);
3493 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003494 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003495}
3496
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003497static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003498{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003499 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003500 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003501 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003502
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003503 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003504 return ERR_PTR(-ENOMEM);
3505
Sheng Yang2384d2b2008-01-17 15:14:33 +08003506 allocate_vpid(vmx);
3507
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003508 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3509 if (err)
3510 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003511
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003512 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003513 if (!vmx->guest_msrs) {
3514 err = -ENOMEM;
3515 goto uninit_vcpu;
3516 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003517
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003518 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3519 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003520 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003521
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003522 vmx->vmcs = alloc_vmcs();
3523 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003524 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003525
3526 vmcs_clear(vmx->vmcs);
3527
Avi Kivity15ad7142007-07-11 18:17:21 +03003528 cpu = get_cpu();
3529 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003530 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003531 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003532 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003533 if (err)
3534 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003535 if (vm_need_virtualize_apic_accesses(kvm))
3536 if (alloc_apic_access_page(kvm) != 0)
3537 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003538
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003539 if (vm_need_ept())
3540 if (alloc_identity_pagetable(kvm) != 0)
3541 goto free_vmcs;
3542
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003543 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003544
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003545free_vmcs:
3546 free_vmcs(vmx->vmcs);
3547free_msrs:
3548 kfree(vmx->host_msrs);
3549free_guest_msrs:
3550 kfree(vmx->guest_msrs);
3551uninit_vcpu:
3552 kvm_vcpu_uninit(&vmx->vcpu);
3553free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003554 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003555 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003556}
3557
Yang, Sheng002c7f72007-07-31 14:23:01 +03003558static void __init vmx_check_processor_compat(void *rtn)
3559{
3560 struct vmcs_config vmcs_conf;
3561
3562 *(int *)rtn = 0;
3563 if (setup_vmcs_config(&vmcs_conf) < 0)
3564 *(int *)rtn = -EIO;
3565 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3566 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3567 smp_processor_id());
3568 *(int *)rtn = -EIO;
3569 }
3570}
3571
Sheng Yang67253af2008-04-25 10:20:22 +08003572static int get_ept_level(void)
3573{
3574 return VMX_EPT_DEFAULT_GAW + 1;
3575}
3576
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003577static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003578 .cpu_has_kvm_support = cpu_has_kvm_support,
3579 .disabled_by_bios = vmx_disabled_by_bios,
3580 .hardware_setup = hardware_setup,
3581 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003582 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003583 .hardware_enable = hardware_enable,
3584 .hardware_disable = hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003585 .cpu_has_accelerated_tpr = cpu_has_vmx_virtualize_apic_accesses,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003586
3587 .vcpu_create = vmx_create_vcpu,
3588 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003589 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003590
Avi Kivity04d2cc72007-09-10 18:10:54 +03003591 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003592 .vcpu_load = vmx_vcpu_load,
3593 .vcpu_put = vmx_vcpu_put,
3594
3595 .set_guest_debug = set_guest_debug,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003596 .guest_debug_pre = kvm_guest_debug_pre,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003597 .get_msr = vmx_get_msr,
3598 .set_msr = vmx_set_msr,
3599 .get_segment_base = vmx_get_segment_base,
3600 .get_segment = vmx_get_segment,
3601 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003602 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003603 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003604 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003605 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003606 .set_cr3 = vmx_set_cr3,
3607 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003608 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003609 .get_idt = vmx_get_idt,
3610 .set_idt = vmx_set_idt,
3611 .get_gdt = vmx_get_gdt,
3612 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003613 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003614 .get_rflags = vmx_get_rflags,
3615 .set_rflags = vmx_set_rflags,
3616
3617 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003618
Avi Kivity6aa8b732006-12-10 02:21:36 -08003619 .run = vmx_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003620 .handle_exit = kvm_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003621 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02003622 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003623 .get_irq = vmx_get_irq,
3624 .set_irq = vmx_inject_irq,
Avi Kivity298101d2007-11-25 13:41:11 +02003625 .queue_exception = vmx_queue_exception,
3626 .exception_injected = vmx_exception_injected,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003627 .inject_pending_irq = vmx_intr_assist,
3628 .inject_pending_vectors = do_interrupt_requests,
Izik Eiduscbc94022007-10-25 00:29:55 +02003629
3630 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003631 .get_tdp_level = get_ept_level,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003632};
3633
3634static int __init vmx_init(void)
3635{
Sheng Yang25c5f222008-03-28 13:18:56 +08003636 void *va;
He, Qingfdef3ad2007-04-30 09:45:24 +03003637 int r;
3638
3639 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3640 if (!vmx_io_bitmap_a)
3641 return -ENOMEM;
3642
3643 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3644 if (!vmx_io_bitmap_b) {
3645 r = -ENOMEM;
3646 goto out;
3647 }
3648
Sheng Yang25c5f222008-03-28 13:18:56 +08003649 vmx_msr_bitmap = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3650 if (!vmx_msr_bitmap) {
3651 r = -ENOMEM;
3652 goto out1;
3653 }
3654
He, Qingfdef3ad2007-04-30 09:45:24 +03003655 /*
3656 * Allow direct access to the PC debug port (it is often used for I/O
3657 * delays, but the vmexits simply slow things down).
3658 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003659 va = kmap(vmx_io_bitmap_a);
3660 memset(va, 0xff, PAGE_SIZE);
3661 clear_bit(0x80, va);
Avi Kivitycd0536d2007-05-08 11:34:07 +03003662 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003663
Sheng Yang25c5f222008-03-28 13:18:56 +08003664 va = kmap(vmx_io_bitmap_b);
3665 memset(va, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03003666 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03003667
Sheng Yang25c5f222008-03-28 13:18:56 +08003668 va = kmap(vmx_msr_bitmap);
3669 memset(va, 0xff, PAGE_SIZE);
3670 kunmap(vmx_msr_bitmap);
3671
Sheng Yang2384d2b2008-01-17 15:14:33 +08003672 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
3673
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003674 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003675 if (r)
Sheng Yang25c5f222008-03-28 13:18:56 +08003676 goto out2;
3677
3678 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_FS_BASE);
3679 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_GS_BASE);
3680 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_CS);
3681 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_ESP);
3682 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_EIP);
He, Qingfdef3ad2007-04-30 09:45:24 +03003683
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003684 if (vm_need_ept()) {
Sheng Yang14394422008-04-28 12:24:45 +08003685 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003686 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
3687 VMX_EPT_WRITABLE_MASK |
Sheng Yang928d4bf2008-11-06 14:55:45 +08003688 VMX_EPT_DEFAULT_MT << VMX_EPT_MT_EPTE_SHIFT |
3689 VMX_EPT_IGMT_BIT);
Sheng Yang534e38b2008-09-08 15:12:30 +08003690 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003691 VMX_EPT_EXECUTABLE_MASK);
3692 kvm_enable_tdp();
3693 } else
3694 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08003695
Avi Kivityc7addb92007-09-16 18:58:32 +02003696 if (bypass_guest_pf)
3697 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
3698
Sheng Yang14394422008-04-28 12:24:45 +08003699 ept_sync_global();
3700
He, Qingfdef3ad2007-04-30 09:45:24 +03003701 return 0;
3702
Sheng Yang25c5f222008-03-28 13:18:56 +08003703out2:
3704 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03003705out1:
3706 __free_page(vmx_io_bitmap_b);
3707out:
3708 __free_page(vmx_io_bitmap_a);
3709 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003710}
3711
3712static void __exit vmx_exit(void)
3713{
Sheng Yang25c5f222008-03-28 13:18:56 +08003714 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03003715 __free_page(vmx_io_bitmap_b);
3716 __free_page(vmx_io_bitmap_a);
3717
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003718 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003719}
3720
3721module_init(vmx_init)
3722module_exit(vmx_exit)