blob: e889b768c75171338cb3199c16518732dab281d8 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080019#include "vmx.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040021
Avi Kivityedf88412007-12-16 11:02:48 +020022#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080023#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020024#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/mm.h>
26#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040027#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020028#include <linux/moduleparam.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030029#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030030#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080033#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080034
Avi Kivity4ecac3f2008-05-13 13:23:38 +030035#define __ex(x) __kvm_handle_fault_on_reboot(x)
36
Avi Kivity6aa8b732006-12-10 02:21:36 -080037MODULE_AUTHOR("Qumranet");
38MODULE_LICENSE("GPL");
39
Avi Kivityc7addb92007-09-16 18:58:32 +020040static int bypass_guest_pf = 1;
41module_param(bypass_guest_pf, bool, 0);
42
Sheng Yang2384d2b2008-01-17 15:14:33 +080043static int enable_vpid = 1;
44module_param(enable_vpid, bool, 0);
45
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020046static int flexpriority_enabled = 1;
47module_param(flexpriority_enabled, bool, 0);
48
Sheng Yang14394422008-04-28 12:24:45 +080049static int enable_ept = 1;
Sheng Yangd56f5462008-04-25 10:13:16 +080050module_param(enable_ept, bool, 0);
51
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040052struct vmcs {
53 u32 revision_id;
54 u32 abort;
55 char data[0];
56};
57
58struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100059 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +030060 struct list_head local_vcpus_link;
Avi Kivity313dbd492008-07-17 18:04:30 +030061 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040062 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030063 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020064 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040065 struct kvm_msr_entry *guest_msrs;
66 struct kvm_msr_entry *host_msrs;
67 int nmsrs;
68 int save_nmsrs;
69 int msr_offset_efer;
70#ifdef CONFIG_X86_64
71 int msr_offset_kernel_gs_base;
72#endif
73 struct vmcs *vmcs;
74 struct {
75 int loaded;
76 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020077 int gs_ldt_reload_needed;
78 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030079 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040080 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +020081 struct {
82 struct {
83 bool pending;
84 u8 vector;
85 unsigned rip;
86 } irq;
87 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +080088 int vpid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040089};
90
91static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
92{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100093 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040094}
95
Sheng Yangb7ebfb02008-04-25 21:44:52 +080096static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +080097static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +030098
Avi Kivity6aa8b732006-12-10 02:21:36 -080099static DEFINE_PER_CPU(struct vmcs *, vmxarea);
100static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300101static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800102
He, Qingfdef3ad2007-04-30 09:45:24 +0300103static struct page *vmx_io_bitmap_a;
104static struct page *vmx_io_bitmap_b;
Sheng Yang25c5f222008-03-28 13:18:56 +0800105static struct page *vmx_msr_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300106
Sheng Yang2384d2b2008-01-17 15:14:33 +0800107static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
108static DEFINE_SPINLOCK(vmx_vpid_lock);
109
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300110static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800111 int size;
112 int order;
113 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300114 u32 pin_based_exec_ctrl;
115 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800116 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300117 u32 vmexit_ctrl;
118 u32 vmentry_ctrl;
119} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120
Sheng Yangd56f5462008-04-25 10:13:16 +0800121struct vmx_capability {
122 u32 ept;
123 u32 vpid;
124} vmx_capability;
125
Avi Kivity6aa8b732006-12-10 02:21:36 -0800126#define VMX_SEGMENT_FIELD(seg) \
127 [VCPU_SREG_##seg] = { \
128 .selector = GUEST_##seg##_SELECTOR, \
129 .base = GUEST_##seg##_BASE, \
130 .limit = GUEST_##seg##_LIMIT, \
131 .ar_bytes = GUEST_##seg##_AR_BYTES, \
132 }
133
134static struct kvm_vmx_segment_field {
135 unsigned selector;
136 unsigned base;
137 unsigned limit;
138 unsigned ar_bytes;
139} kvm_vmx_segment_fields[] = {
140 VMX_SEGMENT_FIELD(CS),
141 VMX_SEGMENT_FIELD(DS),
142 VMX_SEGMENT_FIELD(ES),
143 VMX_SEGMENT_FIELD(FS),
144 VMX_SEGMENT_FIELD(GS),
145 VMX_SEGMENT_FIELD(SS),
146 VMX_SEGMENT_FIELD(TR),
147 VMX_SEGMENT_FIELD(LDTR),
148};
149
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300150/*
151 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
152 * away by decrementing the array size.
153 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800154static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800155#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800156 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
157#endif
158 MSR_EFER, MSR_K6_STAR,
159};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200160#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800161
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400162static void load_msrs(struct kvm_msr_entry *e, int n)
163{
164 int i;
165
166 for (i = 0; i < n; ++i)
167 wrmsrl(e[i].index, e[i].data);
168}
169
170static void save_msrs(struct kvm_msr_entry *e, int n)
171{
172 int i;
173
174 for (i = 0; i < n; ++i)
175 rdmsrl(e[i].index, e[i].data);
176}
177
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178static inline int is_page_fault(u32 intr_info)
179{
180 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
181 INTR_INFO_VALID_MASK)) ==
182 (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
183}
184
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300185static inline int is_no_device(u32 intr_info)
186{
187 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
188 INTR_INFO_VALID_MASK)) ==
189 (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
190}
191
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500192static inline int is_invalid_opcode(u32 intr_info)
193{
194 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
195 INTR_INFO_VALID_MASK)) ==
196 (INTR_TYPE_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
197}
198
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199static inline int is_external_interrupt(u32 intr_info)
200{
201 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
202 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
203}
204
Sheng Yang25c5f222008-03-28 13:18:56 +0800205static inline int cpu_has_vmx_msr_bitmap(void)
206{
207 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS);
208}
209
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800210static inline int cpu_has_vmx_tpr_shadow(void)
211{
212 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW);
213}
214
215static inline int vm_need_tpr_shadow(struct kvm *kvm)
216{
217 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)));
218}
219
Sheng Yangf78e0e22007-10-29 09:40:42 +0800220static inline int cpu_has_secondary_exec_ctrls(void)
221{
222 return (vmcs_config.cpu_based_exec_ctrl &
223 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS);
224}
225
Avi Kivity774ead32007-12-26 13:57:04 +0200226static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800227{
Avi Kivity4c9fc8e2008-03-24 18:15:14 +0200228 return flexpriority_enabled
229 && (vmcs_config.cpu_based_2nd_exec_ctrl &
230 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800231}
232
Sheng Yangd56f5462008-04-25 10:13:16 +0800233static inline int cpu_has_vmx_invept_individual_addr(void)
234{
235 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT));
236}
237
238static inline int cpu_has_vmx_invept_context(void)
239{
240 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT));
241}
242
243static inline int cpu_has_vmx_invept_global(void)
244{
245 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT));
246}
247
248static inline int cpu_has_vmx_ept(void)
249{
250 return (vmcs_config.cpu_based_2nd_exec_ctrl &
251 SECONDARY_EXEC_ENABLE_EPT);
252}
253
254static inline int vm_need_ept(void)
255{
256 return (cpu_has_vmx_ept() && enable_ept);
257}
258
Sheng Yangf78e0e22007-10-29 09:40:42 +0800259static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
260{
261 return ((cpu_has_vmx_virtualize_apic_accesses()) &&
262 (irqchip_in_kernel(kvm)));
263}
264
Sheng Yang2384d2b2008-01-17 15:14:33 +0800265static inline int cpu_has_vmx_vpid(void)
266{
267 return (vmcs_config.cpu_based_2nd_exec_ctrl &
268 SECONDARY_EXEC_ENABLE_VPID);
269}
270
Sheng Yangf08864b2008-05-15 18:23:25 +0800271static inline int cpu_has_virtual_nmis(void)
272{
273 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
274}
275
Rusty Russell8b9cf982007-07-30 16:31:43 +1000276static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800277{
278 int i;
279
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400280 for (i = 0; i < vmx->nmsrs; ++i)
281 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300282 return i;
283 return -1;
284}
285
Sheng Yang2384d2b2008-01-17 15:14:33 +0800286static inline void __invvpid(int ext, u16 vpid, gva_t gva)
287{
288 struct {
289 u64 vpid : 16;
290 u64 rsvd : 48;
291 u64 gva;
292 } operand = { vpid, 0, gva };
293
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300294 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800295 /* CF==1 or ZF==1 --> rc = -1 */
296 "; ja 1f ; ud2 ; 1:"
297 : : "a"(&operand), "c"(ext) : "cc", "memory");
298}
299
Sheng Yang14394422008-04-28 12:24:45 +0800300static inline void __invept(int ext, u64 eptp, gpa_t gpa)
301{
302 struct {
303 u64 eptp, gpa;
304 } operand = {eptp, gpa};
305
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300306 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800307 /* CF==1 or ZF==1 --> rc = -1 */
308 "; ja 1f ; ud2 ; 1:\n"
309 : : "a" (&operand), "c" (ext) : "cc", "memory");
310}
311
Rusty Russell8b9cf982007-07-30 16:31:43 +1000312static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300313{
314 int i;
315
Rusty Russell8b9cf982007-07-30 16:31:43 +1000316 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300317 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400318 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000319 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800320}
321
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322static void vmcs_clear(struct vmcs *vmcs)
323{
324 u64 phys_addr = __pa(vmcs);
325 u8 error;
326
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300327 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800328 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
329 : "cc", "memory");
330 if (error)
331 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
332 vmcs, phys_addr);
333}
334
335static void __vcpu_clear(void *arg)
336{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000337 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800338 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339
Rusty Russell8b9cf982007-07-30 16:31:43 +1000340 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400341 vmcs_clear(vmx->vmcs);
342 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800343 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800344 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300345 list_del(&vmx->local_vcpus_link);
346 vmx->vcpu.cpu = -1;
347 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800348}
349
Rusty Russell8b9cf982007-07-30 16:31:43 +1000350static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800351{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200352 if (vmx->vcpu.cpu == -1)
353 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200354 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800355}
356
Sheng Yang2384d2b2008-01-17 15:14:33 +0800357static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
358{
359 if (vmx->vpid == 0)
360 return;
361
362 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
363}
364
Sheng Yang14394422008-04-28 12:24:45 +0800365static inline void ept_sync_global(void)
366{
367 if (cpu_has_vmx_invept_global())
368 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
369}
370
371static inline void ept_sync_context(u64 eptp)
372{
373 if (vm_need_ept()) {
374 if (cpu_has_vmx_invept_context())
375 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
376 else
377 ept_sync_global();
378 }
379}
380
381static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
382{
383 if (vm_need_ept()) {
384 if (cpu_has_vmx_invept_individual_addr())
385 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
386 eptp, gpa);
387 else
388 ept_sync_context(eptp);
389 }
390}
391
Avi Kivity6aa8b732006-12-10 02:21:36 -0800392static unsigned long vmcs_readl(unsigned long field)
393{
394 unsigned long value;
395
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300396 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800397 : "=a"(value) : "d"(field) : "cc");
398 return value;
399}
400
401static u16 vmcs_read16(unsigned long field)
402{
403 return vmcs_readl(field);
404}
405
406static u32 vmcs_read32(unsigned long field)
407{
408 return vmcs_readl(field);
409}
410
411static u64 vmcs_read64(unsigned long field)
412{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800413#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800414 return vmcs_readl(field);
415#else
416 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
417#endif
418}
419
Avi Kivitye52de1b2007-01-05 16:36:56 -0800420static noinline void vmwrite_error(unsigned long field, unsigned long value)
421{
422 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
423 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
424 dump_stack();
425}
426
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427static void vmcs_writel(unsigned long field, unsigned long value)
428{
429 u8 error;
430
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300431 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400432 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800433 if (unlikely(error))
434 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435}
436
437static void vmcs_write16(unsigned long field, u16 value)
438{
439 vmcs_writel(field, value);
440}
441
442static void vmcs_write32(unsigned long field, u32 value)
443{
444 vmcs_writel(field, value);
445}
446
447static void vmcs_write64(unsigned long field, u64 value)
448{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800449 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300450#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451 asm volatile ("");
452 vmcs_writel(field+1, value >> 32);
453#endif
454}
455
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300456static void vmcs_clear_bits(unsigned long field, u32 mask)
457{
458 vmcs_writel(field, vmcs_readl(field) & ~mask);
459}
460
461static void vmcs_set_bits(unsigned long field, u32 mask)
462{
463 vmcs_writel(field, vmcs_readl(field) | mask);
464}
465
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300466static void update_exception_bitmap(struct kvm_vcpu *vcpu)
467{
468 u32 eb;
469
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500470 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300471 if (!vcpu->fpu_active)
472 eb |= 1u << NM_VECTOR;
473 if (vcpu->guest_debug.enabled)
Jan Kiszka19bd8af2008-07-13 13:40:55 +0200474 eb |= 1u << DB_VECTOR;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800475 if (vcpu->arch.rmode.active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300476 eb = ~0;
Sheng Yang14394422008-04-28 12:24:45 +0800477 if (vm_need_ept())
478 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300479 vmcs_write32(EXCEPTION_BITMAP, eb);
480}
481
Avi Kivity33ed6322007-05-02 16:54:03 +0300482static void reload_tss(void)
483{
Avi Kivity33ed6322007-05-02 16:54:03 +0300484 /*
485 * VT restores TR but not its size. Useless.
486 */
487 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200488 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300489
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300490 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300491 descs = (void *)gdt.base;
492 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
493 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300494}
495
Rusty Russell8b9cf982007-07-30 16:31:43 +1000496static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300497{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400498 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300499 u64 host_efer = vmx->host_msrs[efer_offset].data;
500 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
501 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300502
Avi Kivity51c6cf62007-08-29 03:48:05 +0300503 if (efer_offset < 0)
504 return;
505 /*
506 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
507 * outside long mode
508 */
509 ignore_bits = EFER_NX | EFER_SCE;
510#ifdef CONFIG_X86_64
511 ignore_bits |= EFER_LMA | EFER_LME;
512 /* SCE is meaningful only in long mode on Intel */
513 if (guest_efer & EFER_LMA)
514 ignore_bits &= ~(u64)EFER_SCE;
515#endif
516 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
517 return;
518
519 vmx->host_state.guest_efer_loaded = 1;
520 guest_efer &= ~ignore_bits;
521 guest_efer |= host_efer & ignore_bits;
522 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000523 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300524}
525
Avi Kivity51c6cf62007-08-29 03:48:05 +0300526static void reload_host_efer(struct vcpu_vmx *vmx)
527{
528 if (vmx->host_state.guest_efer_loaded) {
529 vmx->host_state.guest_efer_loaded = 0;
530 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
531 }
532}
533
Avi Kivity04d2cc72007-09-10 18:10:54 +0300534static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300535{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300536 struct vcpu_vmx *vmx = to_vmx(vcpu);
537
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400538 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300539 return;
540
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400541 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300542 /*
543 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
544 * allow segment selectors with cpl > 0 or ti == 1.
545 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300546 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200547 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300548 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200549 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400550 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200551 vmx->host_state.fs_reload_needed = 0;
552 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300553 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200554 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300555 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300556 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400557 if (!(vmx->host_state.gs_sel & 7))
558 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300559 else {
560 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200561 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300562 }
563
564#ifdef CONFIG_X86_64
565 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
566 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
567#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400568 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
569 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300570#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300571
572#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400573 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400574 save_msrs(vmx->host_msrs +
575 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400576
Avi Kivity707c0872007-05-02 17:33:43 +0300577#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400578 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300579 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300580}
581
Avi Kivitya9b21b62008-06-24 11:48:49 +0300582static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300583{
Avi Kivity15ad7142007-07-11 18:17:21 +0300584 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300585
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400586 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300587 return;
588
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200589 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400590 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200591 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300592 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200593 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300594 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300595 /*
596 * If we have to reload gs, we must take care to
597 * preserve our gs base.
598 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300599 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300600 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300601#ifdef CONFIG_X86_64
602 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
603#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300604 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300605 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200606 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400607 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
608 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300609 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300610}
611
Avi Kivitya9b21b62008-06-24 11:48:49 +0300612static void vmx_load_host_state(struct vcpu_vmx *vmx)
613{
614 preempt_disable();
615 __vmx_load_host_state(vmx);
616 preempt_enable();
617}
618
Avi Kivity6aa8b732006-12-10 02:21:36 -0800619/*
620 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
621 * vcpu mutex is already taken.
622 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300623static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400625 struct vcpu_vmx *vmx = to_vmx(vcpu);
626 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200627 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800628
Eddie Donga3d7f852007-09-03 16:15:12 +0300629 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000630 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300631 kvm_migrate_timers(vcpu);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800632 vpid_sync_vcpu_all(vmx);
Avi Kivity543e4242008-05-13 16:22:47 +0300633 local_irq_disable();
634 list_add(&vmx->local_vcpus_link,
635 &per_cpu(vcpus_on_cpu, cpu));
636 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300637 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800638
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400639 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800640 u8 error;
641
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400642 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300643 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800644 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
645 : "cc");
646 if (error)
647 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400648 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800649 }
650
651 if (vcpu->cpu != cpu) {
652 struct descriptor_table dt;
653 unsigned long sysenter_esp;
654
655 vcpu->cpu = cpu;
656 /*
657 * Linux uses per-cpu TSS and GDT, so set these when switching
658 * processors.
659 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300660 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
661 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800662 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
663
664 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
665 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300666
667 /*
668 * Make sure the time stamp counter is monotonous.
669 */
670 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200671 if (tsc_this < vcpu->arch.host_tsc) {
672 delta = vcpu->arch.host_tsc - tsc_this;
673 new_offset = vmcs_read64(TSC_OFFSET) + delta;
674 vmcs_write64(TSC_OFFSET, new_offset);
675 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677}
678
679static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
680{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300681 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682}
683
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300684static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
685{
686 if (vcpu->fpu_active)
687 return;
688 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000689 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800690 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92fa2007-07-17 23:19:08 +1000691 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300692 update_exception_bitmap(vcpu);
693}
694
695static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
696{
697 if (!vcpu->fpu_active)
698 return;
699 vcpu->fpu_active = 0;
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
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
705{
706 return vmcs_readl(GUEST_RFLAGS);
707}
708
709static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
710{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800711 if (vcpu->arch.rmode.active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100712 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713 vmcs_writel(GUEST_RFLAGS, rflags);
714}
715
716static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
717{
718 unsigned long rip;
719 u32 interruptibility;
720
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300721 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800722 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300723 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800724
725 /*
726 * We emulated an instruction, so temporary interrupt blocking
727 * should be removed, if set.
728 */
729 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
730 if (interruptibility & 3)
731 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
732 interruptibility & ~3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800733 vcpu->arch.interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734}
735
Avi Kivity298101d2007-11-25 13:41:11 +0200736static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
737 bool has_error_code, u32 error_code)
738{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200739 struct vcpu_vmx *vmx = to_vmx(vcpu);
740
741 if (has_error_code)
742 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
743
744 if (vcpu->arch.rmode.active) {
745 vmx->rmode.irq.pending = true;
746 vmx->rmode.irq.vector = nr;
747 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
748 if (nr == BP_VECTOR)
749 vmx->rmode.irq.rip++;
750 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
751 nr | INTR_TYPE_SOFT_INTR
752 | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0)
753 | INTR_INFO_VALID_MASK);
754 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
755 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
756 return;
757 }
758
Avi Kivity298101d2007-11-25 13:41:11 +0200759 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
760 nr | INTR_TYPE_EXCEPTION
Ryan Harper2e113842008-02-11 10:26:38 -0600761 | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0)
Avi Kivity298101d2007-11-25 13:41:11 +0200762 | INTR_INFO_VALID_MASK);
Avi Kivity298101d2007-11-25 13:41:11 +0200763}
764
765static bool vmx_exception_injected(struct kvm_vcpu *vcpu)
766{
Avi Kivity35920a32008-07-03 14:50:12 +0300767 return false;
Avi Kivity298101d2007-11-25 13:41:11 +0200768}
769
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770/*
Eddie Donga75beee2007-05-17 18:55:15 +0300771 * Swap MSR entry in host/guest MSR entry array.
772 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200773#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000774static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300775{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400776 struct kvm_msr_entry tmp;
777
778 tmp = vmx->guest_msrs[to];
779 vmx->guest_msrs[to] = vmx->guest_msrs[from];
780 vmx->guest_msrs[from] = tmp;
781 tmp = vmx->host_msrs[to];
782 vmx->host_msrs[to] = vmx->host_msrs[from];
783 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300784}
Gabriel C54e11fa2007-08-01 16:23:10 +0200785#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300786
787/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300788 * Set up the vmcs to automatically save and restore system
789 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
790 * mode, as fiddling with msrs is very expensive.
791 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000792static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300793{
Eddie Dong2cc51562007-05-21 07:28:09 +0300794 int save_nmsrs;
Avi Kivitye38aea32007-04-19 13:22:48 +0300795
Avi Kivity33f9c502008-02-27 16:06:57 +0200796 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300797 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300798#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000799 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300800 int index;
801
Rusty Russell8b9cf982007-07-30 16:31:43 +1000802 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300803 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000804 move_msr_up(vmx, index, save_nmsrs++);
805 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300806 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000807 move_msr_up(vmx, index, save_nmsrs++);
808 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300809 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000810 move_msr_up(vmx, index, save_nmsrs++);
811 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
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++);
Eddie Donga75beee2007-05-17 18:55:15 +0300814 /*
815 * MSR_K6_STAR is only needed on long mode guests, and only
816 * if efer.sce is enabled.
817 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000818 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800819 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000820 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300821 }
Eddie Donga75beee2007-05-17 18:55:15 +0300822#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400823 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300824
Eddie Donga75beee2007-05-17 18:55:15 +0300825#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400826 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000827 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300828#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000829 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivitye38aea32007-04-19 13:22:48 +0300830}
831
832/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800833 * reads and returns guest's timestamp counter "register"
834 * guest_tsc = host_tsc + tsc_offset -- 21.3
835 */
836static u64 guest_read_tsc(void)
837{
838 u64 host_tsc, tsc_offset;
839
840 rdtscll(host_tsc);
841 tsc_offset = vmcs_read64(TSC_OFFSET);
842 return host_tsc + tsc_offset;
843}
844
845/*
846 * writes 'guest_tsc' into guest's timestamp counter "register"
847 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
848 */
849static void guest_write_tsc(u64 guest_tsc)
850{
851 u64 host_tsc;
852
853 rdtscll(host_tsc);
854 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
855}
856
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857/*
858 * Reads an msr value (of 'msr_index') into 'pdata'.
859 * Returns 0 on success, non-0 otherwise.
860 * Assumes vcpu_load() was already called.
861 */
862static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
863{
864 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400865 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866
867 if (!pdata) {
868 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
869 return -EINVAL;
870 }
871
872 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800873#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874 case MSR_FS_BASE:
875 data = vmcs_readl(GUEST_FS_BASE);
876 break;
877 case MSR_GS_BASE:
878 data = vmcs_readl(GUEST_GS_BASE);
879 break;
880 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800881 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800882#endif
883 case MSR_IA32_TIME_STAMP_COUNTER:
884 data = guest_read_tsc();
885 break;
886 case MSR_IA32_SYSENTER_CS:
887 data = vmcs_read32(GUEST_SYSENTER_CS);
888 break;
889 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200890 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 break;
892 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200893 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000896 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800897 if (msr) {
898 data = msr->data;
899 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800901 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902 }
903
904 *pdata = data;
905 return 0;
906}
907
908/*
909 * Writes msr value into into the appropriate "register".
910 * Returns 0 on success, non-0 otherwise.
911 * Assumes vcpu_load() was already called.
912 */
913static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
914{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400915 struct vcpu_vmx *vmx = to_vmx(vcpu);
916 struct kvm_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +0300917 int ret = 0;
918
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800920#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800921 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300922 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +0300923 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +0300924 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925 case MSR_FS_BASE:
926 vmcs_writel(GUEST_FS_BASE, data);
927 break;
928 case MSR_GS_BASE:
929 vmcs_writel(GUEST_GS_BASE, data);
930 break;
931#endif
932 case MSR_IA32_SYSENTER_CS:
933 vmcs_write32(GUEST_SYSENTER_CS, data);
934 break;
935 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200936 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937 break;
938 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200939 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200941 case MSR_IA32_TIME_STAMP_COUNTER:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942 guest_write_tsc(data);
943 break;
Chris Lalancetteefa67e02008-06-20 09:51:30 +0200944 case MSR_P6_PERFCTR0:
945 case MSR_P6_PERFCTR1:
946 case MSR_P6_EVNTSEL0:
947 case MSR_P6_EVNTSEL1:
948 /*
949 * Just discard all writes to the performance counters; this
950 * should keep both older linux and windows 64-bit guests
951 * happy
952 */
953 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", msr_index, data);
954
955 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956 default:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300957 vmx_load_host_state(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000958 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800959 if (msr) {
960 msr->data = data;
961 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300963 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964 }
965
Eddie Dong2cc51562007-05-21 07:28:09 +0300966 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967}
968
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300969static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300971 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
972 switch (reg) {
973 case VCPU_REGS_RSP:
974 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
975 break;
976 case VCPU_REGS_RIP:
977 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
978 break;
979 default:
980 break;
981 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982}
983
984static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
985{
986 unsigned long dr7 = 0x400;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987 int old_singlestep;
988
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989 old_singlestep = vcpu->guest_debug.singlestep;
990
991 vcpu->guest_debug.enabled = dbg->enabled;
992 if (vcpu->guest_debug.enabled) {
993 int i;
994
995 dr7 |= 0x200; /* exact */
996 for (i = 0; i < 4; ++i) {
997 if (!dbg->breakpoints[i].enabled)
998 continue;
999 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
1000 dr7 |= 2 << (i*2); /* global enable */
1001 dr7 |= 0 << (i*4+16); /* execution breakpoint */
1002 }
1003
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004 vcpu->guest_debug.singlestep = dbg->singlestep;
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001005 } else
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006 vcpu->guest_debug.singlestep = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007
1008 if (old_singlestep && !vcpu->guest_debug.singlestep) {
1009 unsigned long flags;
1010
1011 flags = vmcs_readl(GUEST_RFLAGS);
1012 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1013 vmcs_writel(GUEST_RFLAGS, flags);
1014 }
1015
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001016 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017 vmcs_writel(GUEST_DR7, dr7);
1018
1019 return 0;
1020}
1021
Eddie Dong2a8067f2007-08-06 16:29:07 +03001022static int vmx_get_irq(struct kvm_vcpu *vcpu)
1023{
Avi Kivityf7d92382008-07-03 16:14:28 +03001024 if (!vcpu->arch.interrupt.pending)
1025 return -1;
1026 return vcpu->arch.interrupt.nr;
Eddie Dong2a8067f2007-08-06 16:29:07 +03001027}
1028
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029static __init int cpu_has_kvm_support(void)
1030{
1031 unsigned long ecx = cpuid_ecx(1);
1032 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
1033}
1034
1035static __init int vmx_disabled_by_bios(void)
1036{
1037 u64 msr;
1038
1039 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yangca60dfb2008-06-24 17:02:38 +08001040 return (msr & (IA32_FEATURE_CONTROL_LOCKED_BIT |
1041 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT))
1042 == IA32_FEATURE_CONTROL_LOCKED_BIT;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001043 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044}
1045
Avi Kivity774c47f2007-02-12 00:54:47 -08001046static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047{
1048 int cpu = raw_smp_processor_id();
1049 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1050 u64 old;
1051
Avi Kivity543e4242008-05-13 16:22:47 +03001052 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yangca60dfb2008-06-24 17:02:38 +08001054 if ((old & (IA32_FEATURE_CONTROL_LOCKED_BIT |
1055 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT))
1056 != (IA32_FEATURE_CONTROL_LOCKED_BIT |
1057 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001059 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yangca60dfb2008-06-24 17:02:38 +08001060 IA32_FEATURE_CONTROL_LOCKED_BIT |
1061 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT);
Rusty Russell66aee912007-07-17 23:34:16 +10001062 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001063 asm volatile (ASM_VMX_VMXON_RAX
1064 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065 : "memory", "cc");
1066}
1067
Avi Kivity543e4242008-05-13 16:22:47 +03001068static void vmclear_local_vcpus(void)
1069{
1070 int cpu = raw_smp_processor_id();
1071 struct vcpu_vmx *vmx, *n;
1072
1073 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1074 local_vcpus_link)
1075 __vcpu_clear(vmx);
1076}
1077
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078static void hardware_disable(void *garbage)
1079{
Avi Kivity543e4242008-05-13 16:22:47 +03001080 vmclear_local_vcpus();
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001081 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eli Collinse693d712008-06-01 20:24:40 -07001082 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083}
1084
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001085static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001086 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087{
1088 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001089 u32 ctl = ctl_min | ctl_opt;
1090
1091 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1092
1093 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1094 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1095
1096 /* Ensure minimum (required) set of control bits are supported. */
1097 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001098 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001099
1100 *result = ctl;
1101 return 0;
1102}
1103
Yang, Sheng002c7f72007-07-31 14:23:01 +03001104static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001105{
1106 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001107 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001108 u32 _pin_based_exec_control = 0;
1109 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001110 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001111 u32 _vmexit_control = 0;
1112 u32 _vmentry_control = 0;
1113
1114 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001115 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001116 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1117 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001118 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001119
1120 min = CPU_BASED_HLT_EXITING |
1121#ifdef CONFIG_X86_64
1122 CPU_BASED_CR8_LOAD_EXITING |
1123 CPU_BASED_CR8_STORE_EXITING |
1124#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001125 CPU_BASED_CR3_LOAD_EXITING |
1126 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001127 CPU_BASED_USE_IO_BITMAPS |
1128 CPU_BASED_MOV_DR_EXITING |
1129 CPU_BASED_USE_TSC_OFFSETING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001130 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001131 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001132 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001133 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1134 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001135 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001136#ifdef CONFIG_X86_64
1137 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1138 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1139 ~CPU_BASED_CR8_STORE_EXITING;
1140#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001141 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001142 min2 = 0;
1143 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001144 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001145 SECONDARY_EXEC_ENABLE_VPID |
1146 SECONDARY_EXEC_ENABLE_EPT;
1147 if (adjust_vmx_controls(min2, opt2,
1148 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001149 &_cpu_based_2nd_exec_control) < 0)
1150 return -EIO;
1151 }
1152#ifndef CONFIG_X86_64
1153 if (!(_cpu_based_2nd_exec_control &
1154 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1155 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1156#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001157 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
1158 /* CR3 accesses don't need to cause VM Exits when EPT enabled */
1159 min &= ~(CPU_BASED_CR3_LOAD_EXITING |
1160 CPU_BASED_CR3_STORE_EXITING);
1161 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1162 &_cpu_based_exec_control) < 0)
1163 return -EIO;
1164 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1165 vmx_capability.ept, vmx_capability.vpid);
1166 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001167
1168 min = 0;
1169#ifdef CONFIG_X86_64
1170 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1171#endif
1172 opt = 0;
1173 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1174 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001175 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001176
1177 min = opt = 0;
1178 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1179 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001180 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001181
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001182 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001183
1184 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1185 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001186 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001187
1188#ifdef CONFIG_X86_64
1189 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1190 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001191 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001192#endif
1193
1194 /* Require Write-Back (WB) memory type for VMCS accesses. */
1195 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001196 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001197
Yang, Sheng002c7f72007-07-31 14:23:01 +03001198 vmcs_conf->size = vmx_msr_high & 0x1fff;
1199 vmcs_conf->order = get_order(vmcs_config.size);
1200 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001201
Yang, Sheng002c7f72007-07-31 14:23:01 +03001202 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1203 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001204 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001205 vmcs_conf->vmexit_ctrl = _vmexit_control;
1206 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001207
1208 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001209}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001210
1211static struct vmcs *alloc_vmcs_cpu(int cpu)
1212{
1213 int node = cpu_to_node(cpu);
1214 struct page *pages;
1215 struct vmcs *vmcs;
1216
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001217 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001218 if (!pages)
1219 return NULL;
1220 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001221 memset(vmcs, 0, vmcs_config.size);
1222 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223 return vmcs;
1224}
1225
1226static struct vmcs *alloc_vmcs(void)
1227{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001228 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229}
1230
1231static void free_vmcs(struct vmcs *vmcs)
1232{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001233 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001234}
1235
Sam Ravnborg39959582007-06-01 00:47:13 -07001236static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001237{
1238 int cpu;
1239
1240 for_each_online_cpu(cpu)
1241 free_vmcs(per_cpu(vmxarea, cpu));
1242}
1243
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244static __init int alloc_kvm_area(void)
1245{
1246 int cpu;
1247
1248 for_each_online_cpu(cpu) {
1249 struct vmcs *vmcs;
1250
1251 vmcs = alloc_vmcs_cpu(cpu);
1252 if (!vmcs) {
1253 free_kvm_area();
1254 return -ENOMEM;
1255 }
1256
1257 per_cpu(vmxarea, cpu) = vmcs;
1258 }
1259 return 0;
1260}
1261
1262static __init int hardware_setup(void)
1263{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001264 if (setup_vmcs_config(&vmcs_config) < 0)
1265 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001266
1267 if (boot_cpu_has(X86_FEATURE_NX))
1268 kvm_enable_efer_bits(EFER_NX);
1269
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270 return alloc_kvm_area();
1271}
1272
1273static __exit void hardware_unsetup(void)
1274{
1275 free_kvm_area();
1276}
1277
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1279{
1280 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1281
Avi Kivity6af11b92007-03-19 13:18:10 +02001282 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283 vmcs_write16(sf->selector, save->selector);
1284 vmcs_writel(sf->base, save->base);
1285 vmcs_write32(sf->limit, save->limit);
1286 vmcs_write32(sf->ar_bytes, save->ar);
1287 } else {
1288 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1289 << AR_DPL_SHIFT;
1290 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1291 }
1292}
1293
1294static void enter_pmode(struct kvm_vcpu *vcpu)
1295{
1296 unsigned long flags;
1297
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001298 vcpu->arch.rmode.active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001300 vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base);
1301 vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit);
1302 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303
1304 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001305 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001306 flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307 vmcs_writel(GUEST_RFLAGS, flags);
1308
Rusty Russell66aee912007-07-17 23:34:16 +10001309 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1310 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001311
1312 update_exception_bitmap(vcpu);
1313
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001314 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1315 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1316 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1317 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318
1319 vmcs_write16(GUEST_SS_SELECTOR, 0);
1320 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1321
1322 vmcs_write16(GUEST_CS_SELECTOR,
1323 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1324 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1325}
1326
Mike Dayd77c26f2007-10-08 09:02:08 -04001327static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001328{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001329 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001330 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1331 kvm->memslots[0].npages - 3;
1332 return base_gfn << PAGE_SHIFT;
1333 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001334 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335}
1336
1337static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1338{
1339 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1340
1341 save->selector = vmcs_read16(sf->selector);
1342 save->base = vmcs_readl(sf->base);
1343 save->limit = vmcs_read32(sf->limit);
1344 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001345 vmcs_write16(sf->selector, save->base >> 4);
1346 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 vmcs_write32(sf->limit, 0xffff);
1348 vmcs_write32(sf->ar_bytes, 0xf3);
1349}
1350
1351static void enter_rmode(struct kvm_vcpu *vcpu)
1352{
1353 unsigned long flags;
1354
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001355 vcpu->arch.rmode.active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001357 vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1359
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001360 vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001361 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1362
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001363 vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1365
1366 flags = vmcs_readl(GUEST_RFLAGS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001367 vcpu->arch.rmode.save_iopl
1368 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001369
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001370 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371
1372 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001373 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374 update_exception_bitmap(vcpu);
1375
1376 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1377 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1378 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1379
1380 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001381 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001382 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1383 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001384 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1385
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001386 fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1387 fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1388 fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1389 fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001390
Eddie Dong8668a3c2007-10-10 14:26:45 +08001391 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001392 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393}
1394
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001395#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396
1397static void enter_lmode(struct kvm_vcpu *vcpu)
1398{
1399 u32 guest_tr_ar;
1400
1401 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1402 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1403 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001404 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405 vmcs_write32(GUEST_TR_AR_BYTES,
1406 (guest_tr_ar & ~AR_TYPE_MASK)
1407 | AR_TYPE_BUSY_64_TSS);
1408 }
1409
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001410 vcpu->arch.shadow_efer |= EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411
Rusty Russell8b9cf982007-07-30 16:31:43 +10001412 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413 vmcs_write32(VM_ENTRY_CONTROLS,
1414 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001415 | VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416}
1417
1418static void exit_lmode(struct kvm_vcpu *vcpu)
1419{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001420 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421
1422 vmcs_write32(VM_ENTRY_CONTROLS,
1423 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001424 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425}
1426
1427#endif
1428
Sheng Yang2384d2b2008-01-17 15:14:33 +08001429static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1430{
1431 vpid_sync_vcpu_all(to_vmx(vcpu));
Sheng Yang4e1096d2008-07-06 19:16:51 +08001432 if (vm_need_ept())
1433 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001434}
1435
Anthony Liguori25c4c272007-04-27 09:29:21 +03001436static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001437{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001438 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1439 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001440}
1441
Sheng Yang14394422008-04-28 12:24:45 +08001442static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1443{
1444 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1445 if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
1446 printk(KERN_ERR "EPT: Fail to load pdptrs!\n");
1447 return;
1448 }
1449 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1450 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1451 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1452 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1453 }
1454}
1455
1456static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1457
1458static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1459 unsigned long cr0,
1460 struct kvm_vcpu *vcpu)
1461{
1462 if (!(cr0 & X86_CR0_PG)) {
1463 /* From paging/starting to nonpaging */
1464 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001465 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001466 (CPU_BASED_CR3_LOAD_EXITING |
1467 CPU_BASED_CR3_STORE_EXITING));
1468 vcpu->arch.cr0 = cr0;
1469 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1470 *hw_cr0 |= X86_CR0_PE | X86_CR0_PG;
1471 *hw_cr0 &= ~X86_CR0_WP;
1472 } else if (!is_paging(vcpu)) {
1473 /* From nonpaging to paging */
1474 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001475 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001476 ~(CPU_BASED_CR3_LOAD_EXITING |
1477 CPU_BASED_CR3_STORE_EXITING));
1478 vcpu->arch.cr0 = cr0;
1479 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1480 if (!(vcpu->arch.cr0 & X86_CR0_WP))
1481 *hw_cr0 &= ~X86_CR0_WP;
1482 }
1483}
1484
1485static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1486 struct kvm_vcpu *vcpu)
1487{
1488 if (!is_paging(vcpu)) {
1489 *hw_cr4 &= ~X86_CR4_PAE;
1490 *hw_cr4 |= X86_CR4_PSE;
1491 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1492 *hw_cr4 &= ~X86_CR4_PAE;
1493}
1494
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1496{
Sheng Yang14394422008-04-28 12:24:45 +08001497 unsigned long hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) |
1498 KVM_VM_CR0_ALWAYS_ON;
1499
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001500 vmx_fpu_deactivate(vcpu);
1501
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001502 if (vcpu->arch.rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503 enter_pmode(vcpu);
1504
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001505 if (!vcpu->arch.rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506 enter_rmode(vcpu);
1507
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001508#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001509 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001510 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001511 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001512 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001513 exit_lmode(vcpu);
1514 }
1515#endif
1516
Sheng Yang14394422008-04-28 12:24:45 +08001517 if (vm_need_ept())
1518 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1519
Avi Kivity6aa8b732006-12-10 02:21:36 -08001520 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001521 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001522 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001523
Rusty Russell707d92fa2007-07-17 23:19:08 +10001524 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001525 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001526}
1527
Sheng Yang14394422008-04-28 12:24:45 +08001528static u64 construct_eptp(unsigned long root_hpa)
1529{
1530 u64 eptp;
1531
1532 /* TODO write the value reading from MSR */
1533 eptp = VMX_EPT_DEFAULT_MT |
1534 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1535 eptp |= (root_hpa & PAGE_MASK);
1536
1537 return eptp;
1538}
1539
Avi Kivity6aa8b732006-12-10 02:21:36 -08001540static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1541{
Sheng Yang14394422008-04-28 12:24:45 +08001542 unsigned long guest_cr3;
1543 u64 eptp;
1544
1545 guest_cr3 = cr3;
1546 if (vm_need_ept()) {
1547 eptp = construct_eptp(cr3);
1548 vmcs_write64(EPT_POINTER, eptp);
1549 ept_sync_context(eptp);
1550 ept_load_pdptrs(vcpu);
1551 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
1552 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1553 }
1554
Sheng Yang2384d2b2008-01-17 15:14:33 +08001555 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001556 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001557 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001558 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001559}
1560
1561static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1562{
Sheng Yang14394422008-04-28 12:24:45 +08001563 unsigned long hw_cr4 = cr4 | (vcpu->arch.rmode.active ?
1564 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1565
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001566 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08001567 if (vm_need_ept())
1568 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1569
1570 vmcs_writel(CR4_READ_SHADOW, cr4);
1571 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001572}
1573
Avi Kivity6aa8b732006-12-10 02:21:36 -08001574static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1575{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001576 struct vcpu_vmx *vmx = to_vmx(vcpu);
1577 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001578
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001579 vcpu->arch.shadow_efer = efer;
Joerg Roedel9f62e192008-01-31 14:57:39 +01001580 if (!msr)
1581 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001582 if (efer & EFER_LMA) {
1583 vmcs_write32(VM_ENTRY_CONTROLS,
1584 vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001585 VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586 msr->data = efer;
1587
1588 } else {
1589 vmcs_write32(VM_ENTRY_CONTROLS,
1590 vmcs_read32(VM_ENTRY_CONTROLS) &
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001591 ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001592
1593 msr->data = efer & ~EFER_LME;
1594 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001595 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596}
1597
Avi Kivity6aa8b732006-12-10 02:21:36 -08001598static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1599{
1600 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1601
1602 return vmcs_readl(sf->base);
1603}
1604
1605static void vmx_get_segment(struct kvm_vcpu *vcpu,
1606 struct kvm_segment *var, int seg)
1607{
1608 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1609 u32 ar;
1610
1611 var->base = vmcs_readl(sf->base);
1612 var->limit = vmcs_read32(sf->limit);
1613 var->selector = vmcs_read16(sf->selector);
1614 ar = vmcs_read32(sf->ar_bytes);
1615 if (ar & AR_UNUSABLE_MASK)
1616 ar = 0;
1617 var->type = ar & 15;
1618 var->s = (ar >> 4) & 1;
1619 var->dpl = (ar >> 5) & 3;
1620 var->present = (ar >> 7) & 1;
1621 var->avl = (ar >> 12) & 1;
1622 var->l = (ar >> 13) & 1;
1623 var->db = (ar >> 14) & 1;
1624 var->g = (ar >> 15) & 1;
1625 var->unusable = (ar >> 16) & 1;
1626}
1627
Izik Eidus2e4d2652008-03-24 19:38:34 +02001628static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1629{
1630 struct kvm_segment kvm_seg;
1631
1632 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1633 return 0;
1634
1635 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1636 return 3;
1637
1638 vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
1639 return kvm_seg.selector & 3;
1640}
1641
Avi Kivity653e3102007-05-07 10:55:37 +03001642static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001643{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644 u32 ar;
1645
Avi Kivity653e3102007-05-07 10:55:37 +03001646 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001647 ar = 1 << 16;
1648 else {
1649 ar = var->type & 15;
1650 ar |= (var->s & 1) << 4;
1651 ar |= (var->dpl & 3) << 5;
1652 ar |= (var->present & 1) << 7;
1653 ar |= (var->avl & 1) << 12;
1654 ar |= (var->l & 1) << 13;
1655 ar |= (var->db & 1) << 14;
1656 ar |= (var->g & 1) << 15;
1657 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001658 if (ar == 0) /* a 0 value means unusable */
1659 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001660
1661 return ar;
1662}
1663
1664static void vmx_set_segment(struct kvm_vcpu *vcpu,
1665 struct kvm_segment *var, int seg)
1666{
1667 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1668 u32 ar;
1669
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001670 if (vcpu->arch.rmode.active && seg == VCPU_SREG_TR) {
1671 vcpu->arch.rmode.tr.selector = var->selector;
1672 vcpu->arch.rmode.tr.base = var->base;
1673 vcpu->arch.rmode.tr.limit = var->limit;
1674 vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001675 return;
1676 }
1677 vmcs_writel(sf->base, var->base);
1678 vmcs_write32(sf->limit, var->limit);
1679 vmcs_write16(sf->selector, var->selector);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001680 if (vcpu->arch.rmode.active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001681 /*
1682 * Hack real-mode segments into vm86 compatibility.
1683 */
1684 if (var->base == 0xffff0000 && var->selector == 0xf000)
1685 vmcs_writel(sf->base, 0xf0000);
1686 ar = 0xf3;
1687 } else
1688 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689 vmcs_write32(sf->ar_bytes, ar);
1690}
1691
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1693{
1694 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1695
1696 *db = (ar >> 14) & 1;
1697 *l = (ar >> 13) & 1;
1698}
1699
1700static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1701{
1702 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1703 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1704}
1705
1706static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1707{
1708 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1709 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1710}
1711
1712static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1713{
1714 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1715 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1716}
1717
1718static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1719{
1720 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1721 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1722}
1723
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001724static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1725{
1726 struct kvm_segment var;
1727 u32 ar;
1728
1729 vmx_get_segment(vcpu, &var, seg);
1730 ar = vmx_segment_access_rights(&var);
1731
1732 if (var.base != (var.selector << 4))
1733 return false;
1734 if (var.limit != 0xffff)
1735 return false;
1736 if (ar != 0xf3)
1737 return false;
1738
1739 return true;
1740}
1741
1742static bool code_segment_valid(struct kvm_vcpu *vcpu)
1743{
1744 struct kvm_segment cs;
1745 unsigned int cs_rpl;
1746
1747 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1748 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1749
1750 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1751 return false;
1752 if (!cs.s)
1753 return false;
1754 if (!(~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK))) {
1755 if (cs.dpl > cs_rpl)
1756 return false;
1757 } else if (cs.type & AR_TYPE_CODE_MASK) {
1758 if (cs.dpl != cs_rpl)
1759 return false;
1760 }
1761 if (!cs.present)
1762 return false;
1763
1764 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1765 return true;
1766}
1767
1768static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1769{
1770 struct kvm_segment ss;
1771 unsigned int ss_rpl;
1772
1773 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1774 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1775
1776 if ((ss.type != 3) || (ss.type != 7))
1777 return false;
1778 if (!ss.s)
1779 return false;
1780 if (ss.dpl != ss_rpl) /* DPL != RPL */
1781 return false;
1782 if (!ss.present)
1783 return false;
1784
1785 return true;
1786}
1787
1788static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1789{
1790 struct kvm_segment var;
1791 unsigned int rpl;
1792
1793 vmx_get_segment(vcpu, &var, seg);
1794 rpl = var.selector & SELECTOR_RPL_MASK;
1795
1796 if (!var.s)
1797 return false;
1798 if (!var.present)
1799 return false;
1800 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
1801 if (var.dpl < rpl) /* DPL < RPL */
1802 return false;
1803 }
1804
1805 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1806 * rights flags
1807 */
1808 return true;
1809}
1810
1811static bool tr_valid(struct kvm_vcpu *vcpu)
1812{
1813 struct kvm_segment tr;
1814
1815 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
1816
1817 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1818 return false;
1819 if ((tr.type != 3) || (tr.type != 11)) /* TODO: Check if guest is in IA32e mode */
1820 return false;
1821 if (!tr.present)
1822 return false;
1823
1824 return true;
1825}
1826
1827static bool ldtr_valid(struct kvm_vcpu *vcpu)
1828{
1829 struct kvm_segment ldtr;
1830
1831 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
1832
1833 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1834 return false;
1835 if (ldtr.type != 2)
1836 return false;
1837 if (!ldtr.present)
1838 return false;
1839
1840 return true;
1841}
1842
1843static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
1844{
1845 struct kvm_segment cs, ss;
1846
1847 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1848 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1849
1850 return ((cs.selector & SELECTOR_RPL_MASK) ==
1851 (ss.selector & SELECTOR_RPL_MASK));
1852}
1853
1854/*
1855 * Check if guest state is valid. Returns true if valid, false if
1856 * not.
1857 * We assume that registers are always usable
1858 */
1859static bool guest_state_valid(struct kvm_vcpu *vcpu)
1860{
1861 /* real mode guest state checks */
1862 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
1863 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
1864 return false;
1865 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
1866 return false;
1867 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
1868 return false;
1869 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
1870 return false;
1871 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
1872 return false;
1873 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
1874 return false;
1875 } else {
1876 /* protected mode guest state checks */
1877 if (!cs_ss_rpl_check(vcpu))
1878 return false;
1879 if (!code_segment_valid(vcpu))
1880 return false;
1881 if (!stack_segment_valid(vcpu))
1882 return false;
1883 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
1884 return false;
1885 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
1886 return false;
1887 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
1888 return false;
1889 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
1890 return false;
1891 if (!tr_valid(vcpu))
1892 return false;
1893 if (!ldtr_valid(vcpu))
1894 return false;
1895 }
1896 /* TODO:
1897 * - Add checks on RIP
1898 * - Add checks on RFLAGS
1899 */
1900
1901 return true;
1902}
1903
Mike Dayd77c26f2007-10-08 09:02:08 -04001904static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001905{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001906 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001907 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001908 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02001909 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001910
Izik Eidus195aefd2007-10-01 22:14:18 +02001911 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1912 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001913 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001914 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08001915 r = kvm_write_guest_page(kvm, fn++, &data,
1916 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02001917 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001918 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001919 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
1920 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001921 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001922 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1923 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001924 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001925 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001926 r = kvm_write_guest_page(kvm, fn, &data,
1927 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
1928 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02001929 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001930 goto out;
1931
1932 ret = 1;
1933out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001934 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001935}
1936
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001937static int init_rmode_identity_map(struct kvm *kvm)
1938{
1939 int i, r, ret;
1940 pfn_t identity_map_pfn;
1941 u32 tmp;
1942
1943 if (!vm_need_ept())
1944 return 1;
1945 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
1946 printk(KERN_ERR "EPT: identity-mapping pagetable "
1947 "haven't been allocated!\n");
1948 return 0;
1949 }
1950 if (likely(kvm->arch.ept_identity_pagetable_done))
1951 return 1;
1952 ret = 0;
1953 identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT;
1954 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
1955 if (r < 0)
1956 goto out;
1957 /* Set up identity-mapping pagetable for EPT in real mode */
1958 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
1959 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
1960 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
1961 r = kvm_write_guest_page(kvm, identity_map_pfn,
1962 &tmp, i * sizeof(tmp), sizeof(tmp));
1963 if (r < 0)
1964 goto out;
1965 }
1966 kvm->arch.ept_identity_pagetable_done = true;
1967 ret = 1;
1968out:
1969 return ret;
1970}
1971
Avi Kivity6aa8b732006-12-10 02:21:36 -08001972static void seg_setup(int seg)
1973{
1974 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1975
1976 vmcs_write16(sf->selector, 0);
1977 vmcs_writel(sf->base, 0);
1978 vmcs_write32(sf->limit, 0xffff);
1979 vmcs_write32(sf->ar_bytes, 0x93);
1980}
1981
Sheng Yangf78e0e22007-10-29 09:40:42 +08001982static int alloc_apic_access_page(struct kvm *kvm)
1983{
1984 struct kvm_userspace_memory_region kvm_userspace_mem;
1985 int r = 0;
1986
Izik Eidus72dc67a2008-02-10 18:04:15 +02001987 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001988 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001989 goto out;
1990 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
1991 kvm_userspace_mem.flags = 0;
1992 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
1993 kvm_userspace_mem.memory_size = PAGE_SIZE;
1994 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
1995 if (r)
1996 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001997
1998 down_read(&current->mm->mmap_sem);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001999 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002000 up_read(&current->mm->mmap_sem);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002001out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002002 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002003 return r;
2004}
2005
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002006static int alloc_identity_pagetable(struct kvm *kvm)
2007{
2008 struct kvm_userspace_memory_region kvm_userspace_mem;
2009 int r = 0;
2010
2011 down_write(&kvm->slots_lock);
2012 if (kvm->arch.ept_identity_pagetable)
2013 goto out;
2014 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2015 kvm_userspace_mem.flags = 0;
2016 kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
2017 kvm_userspace_mem.memory_size = PAGE_SIZE;
2018 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2019 if (r)
2020 goto out;
2021
2022 down_read(&current->mm->mmap_sem);
2023 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
2024 VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT);
2025 up_read(&current->mm->mmap_sem);
2026out:
2027 up_write(&kvm->slots_lock);
2028 return r;
2029}
2030
Sheng Yang2384d2b2008-01-17 15:14:33 +08002031static void allocate_vpid(struct vcpu_vmx *vmx)
2032{
2033 int vpid;
2034
2035 vmx->vpid = 0;
2036 if (!enable_vpid || !cpu_has_vmx_vpid())
2037 return;
2038 spin_lock(&vmx_vpid_lock);
2039 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2040 if (vpid < VMX_NR_VPIDS) {
2041 vmx->vpid = vpid;
2042 __set_bit(vpid, vmx_vpid_bitmap);
2043 }
2044 spin_unlock(&vmx_vpid_lock);
2045}
2046
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002047static void vmx_disable_intercept_for_msr(struct page *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002048{
2049 void *va;
2050
2051 if (!cpu_has_vmx_msr_bitmap())
2052 return;
2053
2054 /*
2055 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2056 * have the write-low and read-high bitmap offsets the wrong way round.
2057 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2058 */
2059 va = kmap(msr_bitmap);
2060 if (msr <= 0x1fff) {
2061 __clear_bit(msr, va + 0x000); /* read-low */
2062 __clear_bit(msr, va + 0x800); /* write-low */
2063 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2064 msr &= 0x1fff;
2065 __clear_bit(msr, va + 0x400); /* read-high */
2066 __clear_bit(msr, va + 0xc00); /* write-high */
2067 }
2068 kunmap(msr_bitmap);
2069}
2070
Avi Kivity6aa8b732006-12-10 02:21:36 -08002071/*
2072 * Sets up the vmcs for emulated real mode.
2073 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002074static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002075{
2076 u32 host_sysenter_cs;
2077 u32 junk;
2078 unsigned long a;
2079 struct descriptor_table dt;
2080 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002081 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002082 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002083
Avi Kivity6aa8b732006-12-10 02:21:36 -08002084 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03002085 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
2086 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087
Sheng Yang25c5f222008-03-28 13:18:56 +08002088 if (cpu_has_vmx_msr_bitmap())
2089 vmcs_write64(MSR_BITMAP, page_to_phys(vmx_msr_bitmap));
2090
Avi Kivity6aa8b732006-12-10 02:21:36 -08002091 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2092
Avi Kivity6aa8b732006-12-10 02:21:36 -08002093 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002094 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2095 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002096
2097 exec_control = vmcs_config.cpu_based_exec_ctrl;
2098 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2099 exec_control &= ~CPU_BASED_TPR_SHADOW;
2100#ifdef CONFIG_X86_64
2101 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2102 CPU_BASED_CR8_LOAD_EXITING;
2103#endif
2104 }
Sheng Yangd56f5462008-04-25 10:13:16 +08002105 if (!vm_need_ept())
2106 exec_control |= CPU_BASED_CR3_STORE_EXITING |
2107 CPU_BASED_CR3_LOAD_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002108 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109
Sheng Yang83ff3b92007-11-21 14:33:25 +08002110 if (cpu_has_secondary_exec_ctrls()) {
2111 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2112 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2113 exec_control &=
2114 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002115 if (vmx->vpid == 0)
2116 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yangd56f5462008-04-25 10:13:16 +08002117 if (!vm_need_ept())
2118 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002119 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2120 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002121
Avi Kivityc7addb92007-09-16 18:58:32 +02002122 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2123 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002124 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2125
2126 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2127 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2128 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2129
2130 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2131 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2132 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002133 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2134 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002136#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002137 rdmsrl(MSR_FS_BASE, a);
2138 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2139 rdmsrl(MSR_GS_BASE, a);
2140 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2141#else
2142 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2143 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2144#endif
2145
2146 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2147
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002148 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002149 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2150
Mike Dayd77c26f2007-10-08 09:02:08 -04002151 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002152 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002153 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2154 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2155 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002156
2157 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2158 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2159 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2160 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2161 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2162 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2163
Avi Kivity6aa8b732006-12-10 02:21:36 -08002164 for (i = 0; i < NR_VMX_MSR; ++i) {
2165 u32 index = vmx_msr_index[i];
2166 u32 data_low, data_high;
2167 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002168 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002169
2170 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2171 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002172 if (wrmsr_safe(index, data_low, data_high) < 0)
2173 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002174 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002175 vmx->host_msrs[j].index = index;
2176 vmx->host_msrs[j].reserved = 0;
2177 vmx->host_msrs[j].data = data;
2178 vmx->guest_msrs[j] = vmx->host_msrs[j];
2179 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002182 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183
2184 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002185 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2186
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002187 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2188 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2189
Sheng Yangf78e0e22007-10-29 09:40:42 +08002190
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002191 return 0;
2192}
2193
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002194static int init_rmode(struct kvm *kvm)
2195{
2196 if (!init_rmode_tss(kvm))
2197 return 0;
2198 if (!init_rmode_identity_map(kvm))
2199 return 0;
2200 return 1;
2201}
2202
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002203static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2204{
2205 struct vcpu_vmx *vmx = to_vmx(vcpu);
2206 u64 msr;
2207 int ret;
2208
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002209 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002210 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002211 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002212 ret = -ENOMEM;
2213 goto out;
2214 }
2215
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002216 vmx->vcpu.arch.rmode.active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002217
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002218 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002219 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002220 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
2221 if (vmx->vcpu.vcpu_id == 0)
2222 msr |= MSR_IA32_APICBASE_BSP;
2223 kvm_set_apic_base(&vmx->vcpu, msr);
2224
2225 fx_init(&vmx->vcpu);
2226
2227 /*
2228 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2229 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2230 */
2231 if (vmx->vcpu.vcpu_id == 0) {
2232 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2233 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2234 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002235 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2236 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002237 }
2238 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
2239 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
2240
2241 seg_setup(VCPU_SREG_DS);
2242 seg_setup(VCPU_SREG_ES);
2243 seg_setup(VCPU_SREG_FS);
2244 seg_setup(VCPU_SREG_GS);
2245 seg_setup(VCPU_SREG_SS);
2246
2247 vmcs_write16(GUEST_TR_SELECTOR, 0);
2248 vmcs_writel(GUEST_TR_BASE, 0);
2249 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2250 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2251
2252 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2253 vmcs_writel(GUEST_LDTR_BASE, 0);
2254 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2255 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2256
2257 vmcs_write32(GUEST_SYSENTER_CS, 0);
2258 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2259 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2260
2261 vmcs_writel(GUEST_RFLAGS, 0x02);
2262 if (vmx->vcpu.vcpu_id == 0)
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002263 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002264 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002265 kvm_rip_write(vcpu, 0);
2266 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002267
2268 /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */
2269 vmcs_writel(GUEST_DR7, 0x400);
2270
2271 vmcs_writel(GUEST_GDTR_BASE, 0);
2272 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2273
2274 vmcs_writel(GUEST_IDTR_BASE, 0);
2275 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2276
2277 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2278 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2279 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2280
2281 guest_write_tsc(0);
2282
2283 /* Special registers */
2284 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2285
2286 setup_msrs(vmx);
2287
Avi Kivity6aa8b732006-12-10 02:21:36 -08002288 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2289
Sheng Yangf78e0e22007-10-29 09:40:42 +08002290 if (cpu_has_vmx_tpr_shadow()) {
2291 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2292 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2293 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002294 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002295 vmcs_write32(TPR_THRESHOLD, 0);
2296 }
2297
2298 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2299 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002300 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002301
Sheng Yang2384d2b2008-01-17 15:14:33 +08002302 if (vmx->vpid != 0)
2303 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2304
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002305 vmx->vcpu.arch.cr0 = 0x60000010;
2306 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002307 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002308 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002309 vmx_fpu_activate(&vmx->vcpu);
2310 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002311
Sheng Yang2384d2b2008-01-17 15:14:33 +08002312 vpid_sync_vcpu_all(vmx);
2313
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002314 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002315
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002317 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318 return ret;
2319}
2320
Eddie Dong85f455f2007-07-06 12:20:49 +03002321static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
2322{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002323 struct vcpu_vmx *vmx = to_vmx(vcpu);
2324
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002325 KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler);
2326
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002327 if (vcpu->arch.rmode.active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002328 vmx->rmode.irq.pending = true;
2329 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002330 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Avi Kivity9c5623e2007-11-08 18:19:20 +02002331 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2332 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2333 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002334 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002335 return;
2336 }
2337 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2338 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
2339}
2340
Sheng Yangf08864b2008-05-15 18:23:25 +08002341static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2342{
2343 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2344 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002345}
2346
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
2348{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002349 int word_index = __ffs(vcpu->arch.irq_summary);
2350 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351 int irq = word_index * BITS_PER_LONG + bit_index;
2352
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002353 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
2354 if (!vcpu->arch.irq_pending[word_index])
2355 clear_bit(word_index, &vcpu->arch.irq_summary);
Avi Kivityecfc79c2008-08-14 11:13:16 +03002356 kvm_queue_interrupt(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002357}
2358
Dor Laorc1150d82007-01-05 16:36:24 -08002359
2360static void do_interrupt_requests(struct kvm_vcpu *vcpu,
2361 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362{
Dor Laorc1150d82007-01-05 16:36:24 -08002363 u32 cpu_based_vm_exec_control;
2364
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002365 vcpu->arch.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08002366 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2367 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
2368
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002369 if (vcpu->arch.interrupt_window_open &&
Avi Kivityecfc79c2008-08-14 11:13:16 +03002370 vcpu->arch.irq_summary && !vcpu->arch.interrupt.pending)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371 kvm_do_inject_irq(vcpu);
Dor Laorc1150d82007-01-05 16:36:24 -08002372
Avi Kivityecfc79c2008-08-14 11:13:16 +03002373 if (vcpu->arch.interrupt_window_open && vcpu->arch.interrupt.pending)
2374 vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
2375
Dor Laorc1150d82007-01-05 16:36:24 -08002376 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002377 if (!vcpu->arch.interrupt_window_open &&
2378 (vcpu->arch.irq_summary || kvm_run->request_interrupt_window))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379 /*
2380 * Interrupts blocked. Wait for unblock.
2381 */
Dor Laorc1150d82007-01-05 16:36:24 -08002382 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2383 else
2384 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2385 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386}
2387
Izik Eiduscbc94022007-10-25 00:29:55 +02002388static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2389{
2390 int ret;
2391 struct kvm_userspace_memory_region tss_mem = {
2392 .slot = 8,
2393 .guest_phys_addr = addr,
2394 .memory_size = PAGE_SIZE * 3,
2395 .flags = 0,
2396 };
2397
2398 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2399 if (ret)
2400 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002401 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002402 return 0;
2403}
2404
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
2406{
2407 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
2408
2409 set_debugreg(dbg->bp[0], 0);
2410 set_debugreg(dbg->bp[1], 1);
2411 set_debugreg(dbg->bp[2], 2);
2412 set_debugreg(dbg->bp[3], 3);
2413
2414 if (dbg->singlestep) {
2415 unsigned long flags;
2416
2417 flags = vmcs_readl(GUEST_RFLAGS);
2418 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
2419 vmcs_writel(GUEST_RFLAGS, flags);
2420 }
2421}
2422
2423static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2424 int vec, u32 err_code)
2425{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002426 /*
2427 * Instruction with address size override prefix opcode 0x67
2428 * Cause the #SS fault with 0 error code in VM86 mode.
2429 */
2430 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02002431 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002433 /*
2434 * Forward all other exceptions that are valid in real mode.
2435 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2436 * the required debugging infrastructure rework.
2437 */
2438 switch (vec) {
2439 case DE_VECTOR:
2440 case DB_VECTOR:
2441 case BP_VECTOR:
2442 case OF_VECTOR:
2443 case BR_VECTOR:
2444 case UD_VECTOR:
2445 case DF_VECTOR:
2446 case SS_VECTOR:
2447 case GP_VECTOR:
2448 case MF_VECTOR:
2449 kvm_queue_exception(vcpu, vec);
2450 return 1;
2451 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002452 return 0;
2453}
2454
2455static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2456{
Avi Kivity1155f762007-11-22 11:30:47 +02002457 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458 u32 intr_info, error_code;
2459 unsigned long cr2, rip;
2460 u32 vect_info;
2461 enum emulation_result er;
2462
Avi Kivity1155f762007-11-22 11:30:47 +02002463 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002464 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2465
2466 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002467 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002469 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002470
Eddie Dong85f455f2007-07-06 12:20:49 +03002471 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002472 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002473 set_bit(irq, vcpu->arch.irq_pending);
2474 set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002475 }
2476
Avi Kivity1b6269d2007-10-09 12:12:19 +02002477 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
2478 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002479
2480 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002481 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002482 return 1;
2483 }
2484
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002485 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08002486 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002487 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002488 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002489 return 1;
2490 }
2491
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002493 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002494 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002495 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2496 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002497 /* EPT won't cause page fault directly */
2498 if (vm_need_ept())
2499 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002501 KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2,
2502 (u32)((u64)cr2 >> 32), handler);
Avi Kivityf7d92382008-07-03 16:14:28 +03002503 if (vcpu->arch.interrupt.pending || vcpu->arch.exception.pending)
Avi Kivity577bdc42008-07-19 08:57:05 +03002504 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002505 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002506 }
2507
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002508 if (vcpu->arch.rmode.active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002510 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002511 if (vcpu->arch.halt_request) {
2512 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002513 return kvm_emulate_halt(vcpu);
2514 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002516 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517
Mike Dayd77c26f2007-10-08 09:02:08 -04002518 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) ==
2519 (INTR_TYPE_EXCEPTION | 1)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002520 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2521 return 0;
2522 }
2523 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2524 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
2525 kvm_run->ex.error_code = error_code;
2526 return 0;
2527}
2528
2529static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2530 struct kvm_run *kvm_run)
2531{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002532 ++vcpu->stat.irq_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002533 KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534 return 1;
2535}
2536
Avi Kivity988ad742007-02-12 00:54:36 -08002537static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2538{
2539 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2540 return 0;
2541}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002542
Avi Kivity6aa8b732006-12-10 02:21:36 -08002543static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2544{
He, Qingbfdaab02007-09-12 14:18:28 +08002545 unsigned long exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02002546 int size, down, in, string, rep;
2547 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002548
Avi Kivity1165f5f2007-04-19 17:27:43 +03002549 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002550 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002551 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002552
2553 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002554 if (emulate_instruction(vcpu,
2555 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002556 return 0;
2557 return 1;
2558 }
2559
2560 size = (exit_qualification & 7) + 1;
2561 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002562 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002563 rep = (exit_qualification & 32) != 0;
2564 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002565
Laurent Vivier3090dd72007-08-05 10:43:32 +03002566 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002567}
2568
Ingo Molnar102d8322007-02-19 14:37:47 +02002569static void
2570vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2571{
2572 /*
2573 * Patch in the VMCALL instruction:
2574 */
2575 hypercall[0] = 0x0f;
2576 hypercall[1] = 0x01;
2577 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002578}
2579
Avi Kivity6aa8b732006-12-10 02:21:36 -08002580static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2581{
He, Qingbfdaab02007-09-12 14:18:28 +08002582 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002583 int cr;
2584 int reg;
2585
He, Qingbfdaab02007-09-12 14:18:28 +08002586 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002587 cr = exit_qualification & 15;
2588 reg = (exit_qualification >> 8) & 15;
2589 switch ((exit_qualification >> 4) & 3) {
2590 case 0: /* mov to cr */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002591 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr,
2592 (u32)kvm_register_read(vcpu, reg),
2593 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
2594 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002595 switch (cr) {
2596 case 0:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002597 kvm_set_cr0(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002598 skip_emulated_instruction(vcpu);
2599 return 1;
2600 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002601 kvm_set_cr3(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602 skip_emulated_instruction(vcpu);
2603 return 1;
2604 case 4:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002605 kvm_set_cr4(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002606 skip_emulated_instruction(vcpu);
2607 return 1;
2608 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002609 kvm_set_cr8(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610 skip_emulated_instruction(vcpu);
Avi Kivitye5314062007-12-06 16:32:45 +02002611 if (irqchip_in_kernel(vcpu->kvm))
2612 return 1;
Yang, Sheng253abde2007-08-16 13:01:00 +03002613 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2614 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002615 };
2616 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002617 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002618 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002619 vcpu->arch.cr0 &= ~X86_CR0_TS;
2620 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002621 vmx_fpu_activate(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002622 KVMTRACE_0D(CLTS, vcpu, handler);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002623 skip_emulated_instruction(vcpu);
2624 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002625 case 1: /*mov from cr*/
2626 switch (cr) {
2627 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002628 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002629 KVMTRACE_3D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002630 (u32)kvm_register_read(vcpu, reg),
2631 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002632 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002633 skip_emulated_instruction(vcpu);
2634 return 1;
2635 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002636 kvm_register_write(vcpu, reg, kvm_get_cr8(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002637 KVMTRACE_2D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002638 (u32)kvm_register_read(vcpu, reg), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002639 skip_emulated_instruction(vcpu);
2640 return 1;
2641 }
2642 break;
2643 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002644 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002645
2646 skip_emulated_instruction(vcpu);
2647 return 1;
2648 default:
2649 break;
2650 }
2651 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002652 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002653 (int)(exit_qualification >> 4) & 3, cr);
2654 return 0;
2655}
2656
2657static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2658{
He, Qingbfdaab02007-09-12 14:18:28 +08002659 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660 unsigned long val;
2661 int dr, reg;
2662
2663 /*
2664 * FIXME: this code assumes the host is debugging the guest.
2665 * need to deal with guest debugging itself too.
2666 */
He, Qingbfdaab02007-09-12 14:18:28 +08002667 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002668 dr = exit_qualification & 7;
2669 reg = (exit_qualification >> 8) & 15;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002670 if (exit_qualification & 16) {
2671 /* mov from dr */
2672 switch (dr) {
2673 case 6:
2674 val = 0xffff0ff0;
2675 break;
2676 case 7:
2677 val = 0x400;
2678 break;
2679 default:
2680 val = 0;
2681 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002682 kvm_register_write(vcpu, reg, val);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002683 KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684 } else {
2685 /* mov to dr */
2686 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687 skip_emulated_instruction(vcpu);
2688 return 1;
2689}
2690
2691static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2692{
Avi Kivity06465c52007-02-28 20:46:53 +02002693 kvm_emulate_cpuid(vcpu);
2694 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695}
2696
2697static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2698{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002699 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700 u64 data;
2701
2702 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002703 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704 return 1;
2705 }
2706
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002707 KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32),
2708 handler);
2709
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002711 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
2712 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002713 skip_emulated_instruction(vcpu);
2714 return 1;
2715}
2716
2717static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2718{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002719 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
2720 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
2721 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002722
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002723 KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32),
2724 handler);
2725
Avi Kivity6aa8b732006-12-10 02:21:36 -08002726 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002727 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728 return 1;
2729 }
2730
2731 skip_emulated_instruction(vcpu);
2732 return 1;
2733}
2734
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002735static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2736 struct kvm_run *kvm_run)
2737{
2738 return 1;
2739}
2740
Avi Kivity6aa8b732006-12-10 02:21:36 -08002741static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2742 struct kvm_run *kvm_run)
2743{
Eddie Dong85f455f2007-07-06 12:20:49 +03002744 u32 cpu_based_vm_exec_control;
2745
2746 /* clear pending irq */
2747 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2748 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2749 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002750
2751 KVMTRACE_0D(PEND_INTR, vcpu, handler);
2752
Dor Laorc1150d82007-01-05 16:36:24 -08002753 /*
2754 * If the user space waits to inject interrupts, exit as soon as
2755 * possible
2756 */
2757 if (kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002758 !vcpu->arch.irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002759 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002760 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002761 return 0;
2762 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002763 return 1;
2764}
2765
2766static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2767{
2768 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002769 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770}
2771
Ingo Molnarc21415e2007-02-19 14:37:47 +02002772static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2773{
Dor Laor510043d2007-02-19 18:25:43 +02002774 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002775 kvm_emulate_hypercall(vcpu);
2776 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02002777}
2778
Eddie Donge5edaa02007-11-11 12:28:35 +02002779static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2780{
2781 skip_emulated_instruction(vcpu);
2782 /* TODO: Add support for VT-d/pass-through device */
2783 return 1;
2784}
2785
Sheng Yangf78e0e22007-10-29 09:40:42 +08002786static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2787{
2788 u64 exit_qualification;
2789 enum emulation_result er;
2790 unsigned long offset;
2791
2792 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2793 offset = exit_qualification & 0xffful;
2794
2795 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
2796
2797 if (er != EMULATE_DONE) {
2798 printk(KERN_ERR
2799 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
2800 offset);
2801 return -ENOTSUPP;
2802 }
2803 return 1;
2804}
2805
Izik Eidus37817f22008-03-24 23:14:53 +02002806static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2807{
2808 unsigned long exit_qualification;
2809 u16 tss_selector;
2810 int reason;
2811
2812 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
2813
2814 reason = (u32)exit_qualification >> 30;
2815 tss_selector = exit_qualification;
2816
2817 return kvm_task_switch(vcpu, tss_selector, reason);
2818}
2819
Sheng Yang14394422008-04-28 12:24:45 +08002820static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2821{
2822 u64 exit_qualification;
2823 enum emulation_result er;
2824 gpa_t gpa;
2825 unsigned long hva;
2826 int gla_validity;
2827 int r;
2828
2829 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2830
2831 if (exit_qualification & (1 << 6)) {
2832 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
2833 return -ENOTSUPP;
2834 }
2835
2836 gla_validity = (exit_qualification >> 7) & 0x3;
2837 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
2838 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
2839 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
2840 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
2841 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
2842 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
2843 (long unsigned int)exit_qualification);
2844 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2845 kvm_run->hw.hardware_exit_reason = 0;
2846 return -ENOTSUPP;
2847 }
2848
2849 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
2850 hva = gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT);
2851 if (!kvm_is_error_hva(hva)) {
2852 r = kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
2853 if (r < 0) {
2854 printk(KERN_ERR "EPT: Not enough memory!\n");
2855 return -ENOMEM;
2856 }
2857 return 1;
2858 } else {
2859 /* must be MMIO */
2860 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
2861
2862 if (er == EMULATE_FAIL) {
2863 printk(KERN_ERR
2864 "EPT: Fail to handle EPT violation vmexit!er is %d\n",
2865 er);
2866 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
2867 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
2868 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
2869 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
2870 (long unsigned int)exit_qualification);
2871 return -ENOTSUPP;
2872 } else if (er == EMULATE_DO_MMIO)
2873 return 0;
2874 }
2875 return 1;
2876}
2877
Sheng Yangf08864b2008-05-15 18:23:25 +08002878static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2879{
2880 u32 cpu_based_vm_exec_control;
2881
2882 /* clear pending NMI */
2883 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2884 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
2885 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2886 ++vcpu->stat.nmi_window_exits;
2887
2888 return 1;
2889}
2890
Avi Kivity6aa8b732006-12-10 02:21:36 -08002891/*
2892 * The exit handlers return 1 if the exit was handled fully and guest execution
2893 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2894 * to be done to userspace and return 0.
2895 */
2896static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
2897 struct kvm_run *kvm_run) = {
2898 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
2899 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08002900 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08002901 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002903 [EXIT_REASON_CR_ACCESS] = handle_cr,
2904 [EXIT_REASON_DR_ACCESS] = handle_dr,
2905 [EXIT_REASON_CPUID] = handle_cpuid,
2906 [EXIT_REASON_MSR_READ] = handle_rdmsr,
2907 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
2908 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
2909 [EXIT_REASON_HLT] = handle_halt,
Ingo Molnarc21415e2007-02-19 14:37:47 +02002910 [EXIT_REASON_VMCALL] = handle_vmcall,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002911 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
2912 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02002913 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02002914 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Sheng Yang14394422008-04-28 12:24:45 +08002915 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002916};
2917
2918static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04002919 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002920
2921/*
2922 * The guest has exited. See if we can fix it or if we need userspace
2923 * assistance.
2924 */
2925static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2926{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002927 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity29bd8a72007-09-10 17:27:03 +03002928 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1155f762007-11-22 11:30:47 +02002929 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03002930
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002931 KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)kvm_rip_read(vcpu),
2932 (u32)((u64)kvm_rip_read(vcpu) >> 32), entryexit);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002933
Sheng Yang14394422008-04-28 12:24:45 +08002934 /* Access CR3 don't cause VMExit in paging mode, so we need
2935 * to sync with guest real CR3. */
2936 if (vm_need_ept() && is_paging(vcpu)) {
2937 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2938 ept_load_pdptrs(vcpu);
2939 }
2940
Avi Kivity29bd8a72007-09-10 17:27:03 +03002941 if (unlikely(vmx->fail)) {
2942 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2943 kvm_run->fail_entry.hardware_entry_failure_reason
2944 = vmcs_read32(VM_INSTRUCTION_ERROR);
2945 return 0;
2946 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002947
Mike Dayd77c26f2007-10-08 09:02:08 -04002948 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08002949 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
2950 exit_reason != EXIT_REASON_EPT_VIOLATION))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002951 printk(KERN_WARNING "%s: unexpected, valid vectoring info and "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002952 "exit reason is 0x%x\n", __func__, exit_reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002953 if (exit_reason < kvm_vmx_max_exit_handlers
2954 && kvm_vmx_exit_handlers[exit_reason])
2955 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
2956 else {
2957 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2958 kvm_run->hw.hardware_exit_reason = exit_reason;
2959 }
2960 return 0;
2961}
2962
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002963static void update_tpr_threshold(struct kvm_vcpu *vcpu)
2964{
2965 int max_irr, tpr;
2966
2967 if (!vm_need_tpr_shadow(vcpu->kvm))
2968 return;
2969
2970 if (!kvm_lapic_enabled(vcpu) ||
2971 ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
2972 vmcs_write32(TPR_THRESHOLD, 0);
2973 return;
2974 }
2975
2976 tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
2977 vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
2978}
2979
Eddie Dong85f455f2007-07-06 12:20:49 +03002980static void enable_irq_window(struct kvm_vcpu *vcpu)
2981{
2982 u32 cpu_based_vm_exec_control;
2983
2984 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2985 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2986 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2987}
2988
Sheng Yangf08864b2008-05-15 18:23:25 +08002989static void enable_nmi_window(struct kvm_vcpu *vcpu)
2990{
2991 u32 cpu_based_vm_exec_control;
2992
2993 if (!cpu_has_virtual_nmis())
2994 return;
2995
2996 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2997 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2998 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2999}
3000
3001static int vmx_nmi_enabled(struct kvm_vcpu *vcpu)
3002{
3003 u32 guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3004 return !(guest_intr & (GUEST_INTR_STATE_NMI |
3005 GUEST_INTR_STATE_MOV_SS |
3006 GUEST_INTR_STATE_STI));
3007}
3008
3009static int vmx_irq_enabled(struct kvm_vcpu *vcpu)
3010{
3011 u32 guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3012 return (!(guest_intr & (GUEST_INTR_STATE_MOV_SS |
3013 GUEST_INTR_STATE_STI)) &&
3014 (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF));
3015}
3016
3017static void enable_intr_window(struct kvm_vcpu *vcpu)
3018{
3019 if (vcpu->arch.nmi_pending)
3020 enable_nmi_window(vcpu);
3021 else if (kvm_cpu_has_interrupt(vcpu))
3022 enable_irq_window(vcpu);
3023}
3024
Avi Kivitycf393f72008-07-01 16:20:21 +03003025static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3026{
3027 u32 exit_intr_info;
Avi Kivity668f6122008-07-02 09:28:55 +03003028 u32 idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003029 bool unblock_nmi;
3030 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003031 int type;
3032 bool idtv_info_valid;
Avi Kivity35920a32008-07-03 14:50:12 +03003033 u32 error;
Avi Kivitycf393f72008-07-01 16:20:21 +03003034
3035 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
3036 if (cpu_has_virtual_nmis()) {
3037 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3038 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3039 /*
3040 * SDM 3: 25.7.1.2
3041 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3042 * a guest IRET fault.
3043 */
3044 if (unblock_nmi && vector != DF_VECTOR)
3045 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3046 GUEST_INTR_STATE_NMI);
3047 }
Avi Kivity668f6122008-07-02 09:28:55 +03003048
3049 idt_vectoring_info = vmx->idt_vectoring_info;
3050 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3051 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3052 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
3053 if (vmx->vcpu.arch.nmi_injected) {
3054 /*
3055 * SDM 3: 25.7.1.2
3056 * Clear bit "block by NMI" before VM entry if a NMI delivery
3057 * faulted.
3058 */
3059 if (idtv_info_valid && type == INTR_TYPE_NMI_INTR)
3060 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3061 GUEST_INTR_STATE_NMI);
3062 else
3063 vmx->vcpu.arch.nmi_injected = false;
3064 }
Avi Kivity35920a32008-07-03 14:50:12 +03003065 kvm_clear_exception_queue(&vmx->vcpu);
3066 if (idtv_info_valid && type == INTR_TYPE_EXCEPTION) {
3067 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
3068 error = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3069 kvm_queue_exception_e(&vmx->vcpu, vector, error);
3070 } else
3071 kvm_queue_exception(&vmx->vcpu, vector);
3072 vmx->idt_vectoring_info = 0;
3073 }
Avi Kivityf7d92382008-07-03 16:14:28 +03003074 kvm_clear_interrupt_queue(&vmx->vcpu);
3075 if (idtv_info_valid && type == INTR_TYPE_EXT_INTR) {
3076 kvm_queue_interrupt(&vmx->vcpu, vector);
3077 vmx->idt_vectoring_info = 0;
3078 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003079}
3080
Eddie Dong85f455f2007-07-06 12:20:49 +03003081static void vmx_intr_assist(struct kvm_vcpu *vcpu)
3082{
Avi Kivityf7d92382008-07-03 16:14:28 +03003083 u32 intr_info_field;
Eddie Dong85f455f2007-07-06 12:20:49 +03003084
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003085 update_tpr_threshold(vcpu);
3086
Eddie Dong85f455f2007-07-06 12:20:49 +03003087 intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
Sheng Yangf08864b2008-05-15 18:23:25 +08003088 if (cpu_has_virtual_nmis()) {
Avi Kivity668f6122008-07-02 09:28:55 +03003089 if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
3090 if (vmx_nmi_enabled(vcpu)) {
3091 vcpu->arch.nmi_pending = false;
3092 vcpu->arch.nmi_injected = true;
3093 } else {
3094 enable_intr_window(vcpu);
3095 return;
3096 }
3097 }
3098 if (vcpu->arch.nmi_injected) {
3099 vmx_inject_nmi(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003100 enable_intr_window(vcpu);
3101 return;
3102 }
Sheng Yangf08864b2008-05-15 18:23:25 +08003103 }
Avi Kivityf7d92382008-07-03 16:14:28 +03003104 if (!vcpu->arch.interrupt.pending && kvm_cpu_has_interrupt(vcpu)) {
3105 if (vmx_irq_enabled(vcpu))
3106 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu));
3107 else
3108 enable_irq_window(vcpu);
3109 }
3110 if (vcpu->arch.interrupt.pending) {
3111 vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
3112 kvm_timer_intr_post(vcpu, vcpu->arch.interrupt.nr);
3113 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003114}
3115
Avi Kivity9c8cba32007-11-22 11:42:59 +02003116/*
3117 * Failure to inject an interrupt should give us the information
3118 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3119 * when fetching the interrupt redirection bitmap in the real-mode
3120 * tss, this doesn't happen. So we do it ourselves.
3121 */
3122static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3123{
3124 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003125 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003126 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003127 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003128 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3129 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3130 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3131 return;
3132 }
3133 vmx->idt_vectoring_info =
3134 VECTORING_INFO_VALID_MASK
3135 | INTR_TYPE_EXT_INTR
3136 | vmx->rmode.irq.vector;
3137}
3138
Avi Kivityc8019492008-07-14 14:44:59 +03003139#ifdef CONFIG_X86_64
3140#define R "r"
3141#define Q "q"
3142#else
3143#define R "e"
3144#define Q "l"
3145#endif
3146
Avi Kivity04d2cc72007-09-10 18:10:54 +03003147static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003149 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1b6269d2007-10-09 12:12:19 +02003150 u32 intr_info;
Avi Kivitye6adf282007-04-30 16:07:54 +03003151
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003152 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3153 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3154 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3155 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3156
Avi Kivitye6adf282007-04-30 16:07:54 +03003157 /*
3158 * Loading guest fpu may have cleared host cr0.ts
3159 */
3160 vmcs_writel(HOST_CR0, read_cr0());
3161
Mike Dayd77c26f2007-10-08 09:02:08 -04003162 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003164 "push %%"R"dx; push %%"R"bp;"
3165 "push %%"R"cx \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03003166 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3167 "je 1f \n\t"
3168 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003169 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03003170 "1: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003171 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003172 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003174 "mov %c[cr2](%0), %%"R"ax \n\t"
3175 "mov %%"R"ax, %%cr2 \n\t"
3176 "mov %c[rax](%0), %%"R"ax \n\t"
3177 "mov %c[rbx](%0), %%"R"bx \n\t"
3178 "mov %c[rdx](%0), %%"R"dx \n\t"
3179 "mov %c[rsi](%0), %%"R"si \n\t"
3180 "mov %c[rdi](%0), %%"R"di \n\t"
3181 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003182#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003183 "mov %c[r8](%0), %%r8 \n\t"
3184 "mov %c[r9](%0), %%r9 \n\t"
3185 "mov %c[r10](%0), %%r10 \n\t"
3186 "mov %c[r11](%0), %%r11 \n\t"
3187 "mov %c[r12](%0), %%r12 \n\t"
3188 "mov %c[r13](%0), %%r13 \n\t"
3189 "mov %c[r14](%0), %%r14 \n\t"
3190 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003192 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3193
Avi Kivity6aa8b732006-12-10 02:21:36 -08003194 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003195 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003196 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003197 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003198 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003199 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003200 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003201 "xchg %0, (%%"R"sp) \n\t"
3202 "mov %%"R"ax, %c[rax](%0) \n\t"
3203 "mov %%"R"bx, %c[rbx](%0) \n\t"
3204 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3205 "mov %%"R"dx, %c[rdx](%0) \n\t"
3206 "mov %%"R"si, %c[rsi](%0) \n\t"
3207 "mov %%"R"di, %c[rdi](%0) \n\t"
3208 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003209#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003210 "mov %%r8, %c[r8](%0) \n\t"
3211 "mov %%r9, %c[r9](%0) \n\t"
3212 "mov %%r10, %c[r10](%0) \n\t"
3213 "mov %%r11, %c[r11](%0) \n\t"
3214 "mov %%r12, %c[r12](%0) \n\t"
3215 "mov %%r13, %c[r13](%0) \n\t"
3216 "mov %%r14, %c[r14](%0) \n\t"
3217 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003219 "mov %%cr2, %%"R"ax \n\t"
3220 "mov %%"R"ax, %c[cr2](%0) \n\t"
3221
3222 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003223 "setbe %c[fail](%0) \n\t"
3224 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3225 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3226 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03003227 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003228 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3229 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3230 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3231 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3232 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3233 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3234 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003235#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003236 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3237 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3238 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3239 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3240 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3241 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3242 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3243 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003244#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003245 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003246 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003247 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003248#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003249 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3250#endif
3251 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003252
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003253 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
3254 vcpu->arch.regs_dirty = 0;
3255
Avi Kivity1155f762007-11-22 11:30:47 +02003256 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003257 if (vmx->rmode.irq.pending)
3258 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003259
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003260 vcpu->arch.interrupt_window_open =
Sheng Yangf08864b2008-05-15 18:23:25 +08003261 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
3262 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)) == 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003263
Mike Dayd77c26f2007-10-08 09:02:08 -04003264 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003265 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003266
3267 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
3268
3269 /* We need to handle NMIs before interrupts are enabled */
Sheng Yangf08864b2008-05-15 18:23:25 +08003270 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200 &&
3271 (intr_info & INTR_INFO_VALID_MASK)) {
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003272 KVMTRACE_0D(NMI, vcpu, handler);
Avi Kivity1b6269d2007-10-09 12:12:19 +02003273 asm("int $2");
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003274 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003275
3276 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277}
3278
Avi Kivityc8019492008-07-14 14:44:59 +03003279#undef R
3280#undef Q
3281
Avi Kivity6aa8b732006-12-10 02:21:36 -08003282static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3283{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003284 struct vcpu_vmx *vmx = to_vmx(vcpu);
3285
3286 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003287 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003288 free_vmcs(vmx->vmcs);
3289 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003290 }
3291}
3292
3293static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3294{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003295 struct vcpu_vmx *vmx = to_vmx(vcpu);
3296
Sheng Yang2384d2b2008-01-17 15:14:33 +08003297 spin_lock(&vmx_vpid_lock);
3298 if (vmx->vpid != 0)
3299 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3300 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003301 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003302 kfree(vmx->host_msrs);
3303 kfree(vmx->guest_msrs);
3304 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003305 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003306}
3307
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003308static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003309{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003310 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003311 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003312 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003313
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003314 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003315 return ERR_PTR(-ENOMEM);
3316
Sheng Yang2384d2b2008-01-17 15:14:33 +08003317 allocate_vpid(vmx);
3318
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003319 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3320 if (err)
3321 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003322
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003323 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003324 if (!vmx->guest_msrs) {
3325 err = -ENOMEM;
3326 goto uninit_vcpu;
3327 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003328
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003329 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3330 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003331 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003332
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003333 vmx->vmcs = alloc_vmcs();
3334 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003335 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003336
3337 vmcs_clear(vmx->vmcs);
3338
Avi Kivity15ad7142007-07-11 18:17:21 +03003339 cpu = get_cpu();
3340 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003341 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003342 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003343 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003344 if (err)
3345 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003346 if (vm_need_virtualize_apic_accesses(kvm))
3347 if (alloc_apic_access_page(kvm) != 0)
3348 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003349
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003350 if (vm_need_ept())
3351 if (alloc_identity_pagetable(kvm) != 0)
3352 goto free_vmcs;
3353
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003354 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003355
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003356free_vmcs:
3357 free_vmcs(vmx->vmcs);
3358free_msrs:
3359 kfree(vmx->host_msrs);
3360free_guest_msrs:
3361 kfree(vmx->guest_msrs);
3362uninit_vcpu:
3363 kvm_vcpu_uninit(&vmx->vcpu);
3364free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003365 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003366 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003367}
3368
Yang, Sheng002c7f72007-07-31 14:23:01 +03003369static void __init vmx_check_processor_compat(void *rtn)
3370{
3371 struct vmcs_config vmcs_conf;
3372
3373 *(int *)rtn = 0;
3374 if (setup_vmcs_config(&vmcs_conf) < 0)
3375 *(int *)rtn = -EIO;
3376 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3377 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3378 smp_processor_id());
3379 *(int *)rtn = -EIO;
3380 }
3381}
3382
Sheng Yang67253af2008-04-25 10:20:22 +08003383static int get_ept_level(void)
3384{
3385 return VMX_EPT_DEFAULT_GAW + 1;
3386}
3387
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003388static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003389 .cpu_has_kvm_support = cpu_has_kvm_support,
3390 .disabled_by_bios = vmx_disabled_by_bios,
3391 .hardware_setup = hardware_setup,
3392 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003393 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003394 .hardware_enable = hardware_enable,
3395 .hardware_disable = hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003396 .cpu_has_accelerated_tpr = cpu_has_vmx_virtualize_apic_accesses,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003397
3398 .vcpu_create = vmx_create_vcpu,
3399 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003400 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003401
Avi Kivity04d2cc72007-09-10 18:10:54 +03003402 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003403 .vcpu_load = vmx_vcpu_load,
3404 .vcpu_put = vmx_vcpu_put,
3405
3406 .set_guest_debug = set_guest_debug,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003407 .guest_debug_pre = kvm_guest_debug_pre,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003408 .get_msr = vmx_get_msr,
3409 .set_msr = vmx_set_msr,
3410 .get_segment_base = vmx_get_segment_base,
3411 .get_segment = vmx_get_segment,
3412 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003413 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003414 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003415 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003416 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003417 .set_cr3 = vmx_set_cr3,
3418 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003419 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003420 .get_idt = vmx_get_idt,
3421 .set_idt = vmx_set_idt,
3422 .get_gdt = vmx_get_gdt,
3423 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003424 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003425 .get_rflags = vmx_get_rflags,
3426 .set_rflags = vmx_set_rflags,
3427
3428 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003429
Avi Kivity6aa8b732006-12-10 02:21:36 -08003430 .run = vmx_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003431 .handle_exit = kvm_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003432 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02003433 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003434 .get_irq = vmx_get_irq,
3435 .set_irq = vmx_inject_irq,
Avi Kivity298101d2007-11-25 13:41:11 +02003436 .queue_exception = vmx_queue_exception,
3437 .exception_injected = vmx_exception_injected,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003438 .inject_pending_irq = vmx_intr_assist,
3439 .inject_pending_vectors = do_interrupt_requests,
Izik Eiduscbc94022007-10-25 00:29:55 +02003440
3441 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003442 .get_tdp_level = get_ept_level,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003443};
3444
3445static int __init vmx_init(void)
3446{
Sheng Yang25c5f222008-03-28 13:18:56 +08003447 void *va;
He, Qingfdef3ad2007-04-30 09:45:24 +03003448 int r;
3449
3450 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3451 if (!vmx_io_bitmap_a)
3452 return -ENOMEM;
3453
3454 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3455 if (!vmx_io_bitmap_b) {
3456 r = -ENOMEM;
3457 goto out;
3458 }
3459
Sheng Yang25c5f222008-03-28 13:18:56 +08003460 vmx_msr_bitmap = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3461 if (!vmx_msr_bitmap) {
3462 r = -ENOMEM;
3463 goto out1;
3464 }
3465
He, Qingfdef3ad2007-04-30 09:45:24 +03003466 /*
3467 * Allow direct access to the PC debug port (it is often used for I/O
3468 * delays, but the vmexits simply slow things down).
3469 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003470 va = kmap(vmx_io_bitmap_a);
3471 memset(va, 0xff, PAGE_SIZE);
3472 clear_bit(0x80, va);
Avi Kivitycd0536d2007-05-08 11:34:07 +03003473 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003474
Sheng Yang25c5f222008-03-28 13:18:56 +08003475 va = kmap(vmx_io_bitmap_b);
3476 memset(va, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03003477 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03003478
Sheng Yang25c5f222008-03-28 13:18:56 +08003479 va = kmap(vmx_msr_bitmap);
3480 memset(va, 0xff, PAGE_SIZE);
3481 kunmap(vmx_msr_bitmap);
3482
Sheng Yang2384d2b2008-01-17 15:14:33 +08003483 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
3484
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003485 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003486 if (r)
Sheng Yang25c5f222008-03-28 13:18:56 +08003487 goto out2;
3488
3489 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_FS_BASE);
3490 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_GS_BASE);
3491 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_CS);
3492 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_ESP);
3493 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_EIP);
He, Qingfdef3ad2007-04-30 09:45:24 +03003494
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003495 if (vm_need_ept()) {
Sheng Yang14394422008-04-28 12:24:45 +08003496 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003497 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
3498 VMX_EPT_WRITABLE_MASK |
3499 VMX_EPT_DEFAULT_MT << VMX_EPT_MT_EPTE_SHIFT);
Sheng Yang534e38b2008-09-08 15:12:30 +08003500 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003501 VMX_EPT_EXECUTABLE_MASK);
3502 kvm_enable_tdp();
3503 } else
3504 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08003505
Avi Kivityc7addb92007-09-16 18:58:32 +02003506 if (bypass_guest_pf)
3507 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
3508
Sheng Yang14394422008-04-28 12:24:45 +08003509 ept_sync_global();
3510
He, Qingfdef3ad2007-04-30 09:45:24 +03003511 return 0;
3512
Sheng Yang25c5f222008-03-28 13:18:56 +08003513out2:
3514 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03003515out1:
3516 __free_page(vmx_io_bitmap_b);
3517out:
3518 __free_page(vmx_io_bitmap_a);
3519 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003520}
3521
3522static void __exit vmx_exit(void)
3523{
Sheng Yang25c5f222008-03-28 13:18:56 +08003524 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03003525 __free_page(vmx_io_bitmap_b);
3526 __free_page(vmx_io_bitmap_a);
3527
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003528 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003529}
3530
3531module_init(vmx_init)
3532module_exit(vmx_exit)