blob: b80b4d1416374997cbd8efb563dd37993b056c09 [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>
Avi Kivitye4956062007-06-28 14:15:57 -040029
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080031#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032
Avi Kivity4ecac3f2008-05-13 13:23:38 +030033#define __ex(x) __kvm_handle_fault_on_reboot(x)
34
Avi Kivity6aa8b732006-12-10 02:21:36 -080035MODULE_AUTHOR("Qumranet");
36MODULE_LICENSE("GPL");
37
Avi Kivityc7addb92007-09-16 18:58:32 +020038static int bypass_guest_pf = 1;
39module_param(bypass_guest_pf, bool, 0);
40
Sheng Yang2384d2b2008-01-17 15:14:33 +080041static int enable_vpid = 1;
42module_param(enable_vpid, bool, 0);
43
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020044static int flexpriority_enabled = 1;
45module_param(flexpriority_enabled, bool, 0);
46
Sheng Yang14394422008-04-28 12:24:45 +080047static int enable_ept = 1;
Sheng Yangd56f5462008-04-25 10:13:16 +080048module_param(enable_ept, bool, 0);
49
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040050struct vmcs {
51 u32 revision_id;
52 u32 abort;
53 char data[0];
54};
55
56struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100057 struct kvm_vcpu vcpu;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040058 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030059 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020060 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040061 struct kvm_msr_entry *guest_msrs;
62 struct kvm_msr_entry *host_msrs;
63 int nmsrs;
64 int save_nmsrs;
65 int msr_offset_efer;
66#ifdef CONFIG_X86_64
67 int msr_offset_kernel_gs_base;
68#endif
69 struct vmcs *vmcs;
70 struct {
71 int loaded;
72 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020073 int gs_ldt_reload_needed;
74 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030075 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040076 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +020077 struct {
78 struct {
79 bool pending;
80 u8 vector;
81 unsigned rip;
82 } irq;
83 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +080084 int vpid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040085};
86
87static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
88{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100089 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040090}
91
Sheng Yangb7ebfb02008-04-25 21:44:52 +080092static int init_rmode(struct kvm *kvm);
Avi Kivity75880a02007-06-20 11:20:04 +030093
Avi Kivity6aa8b732006-12-10 02:21:36 -080094static DEFINE_PER_CPU(struct vmcs *, vmxarea);
95static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
96
He, Qingfdef3ad2007-04-30 09:45:24 +030097static struct page *vmx_io_bitmap_a;
98static struct page *vmx_io_bitmap_b;
Sheng Yang25c5f222008-03-28 13:18:56 +080099static struct page *vmx_msr_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300100
Sheng Yang2384d2b2008-01-17 15:14:33 +0800101static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
102static DEFINE_SPINLOCK(vmx_vpid_lock);
103
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300104static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800105 int size;
106 int order;
107 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300108 u32 pin_based_exec_ctrl;
109 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800110 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300111 u32 vmexit_ctrl;
112 u32 vmentry_ctrl;
113} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800114
Sheng Yangd56f5462008-04-25 10:13:16 +0800115struct vmx_capability {
116 u32 ept;
117 u32 vpid;
118} vmx_capability;
119
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120#define VMX_SEGMENT_FIELD(seg) \
121 [VCPU_SREG_##seg] = { \
122 .selector = GUEST_##seg##_SELECTOR, \
123 .base = GUEST_##seg##_BASE, \
124 .limit = GUEST_##seg##_LIMIT, \
125 .ar_bytes = GUEST_##seg##_AR_BYTES, \
126 }
127
128static struct kvm_vmx_segment_field {
129 unsigned selector;
130 unsigned base;
131 unsigned limit;
132 unsigned ar_bytes;
133} kvm_vmx_segment_fields[] = {
134 VMX_SEGMENT_FIELD(CS),
135 VMX_SEGMENT_FIELD(DS),
136 VMX_SEGMENT_FIELD(ES),
137 VMX_SEGMENT_FIELD(FS),
138 VMX_SEGMENT_FIELD(GS),
139 VMX_SEGMENT_FIELD(SS),
140 VMX_SEGMENT_FIELD(TR),
141 VMX_SEGMENT_FIELD(LDTR),
142};
143
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300144/*
145 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
146 * away by decrementing the array size.
147 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800148static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800149#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800150 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
151#endif
152 MSR_EFER, MSR_K6_STAR,
153};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200154#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800155
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400156static void load_msrs(struct kvm_msr_entry *e, int n)
157{
158 int i;
159
160 for (i = 0; i < n; ++i)
161 wrmsrl(e[i].index, e[i].data);
162}
163
164static void save_msrs(struct kvm_msr_entry *e, int n)
165{
166 int i;
167
168 for (i = 0; i < n; ++i)
169 rdmsrl(e[i].index, e[i].data);
170}
171
Avi Kivity6aa8b732006-12-10 02:21:36 -0800172static inline int is_page_fault(u32 intr_info)
173{
174 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
175 INTR_INFO_VALID_MASK)) ==
176 (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
177}
178
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300179static inline int is_no_device(u32 intr_info)
180{
181 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
182 INTR_INFO_VALID_MASK)) ==
183 (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
184}
185
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500186static inline int is_invalid_opcode(u32 intr_info)
187{
188 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
189 INTR_INFO_VALID_MASK)) ==
190 (INTR_TYPE_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
191}
192
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193static inline int is_external_interrupt(u32 intr_info)
194{
195 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
196 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
197}
198
Sheng Yang25c5f222008-03-28 13:18:56 +0800199static inline int cpu_has_vmx_msr_bitmap(void)
200{
201 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS);
202}
203
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800204static inline int cpu_has_vmx_tpr_shadow(void)
205{
206 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW);
207}
208
209static inline int vm_need_tpr_shadow(struct kvm *kvm)
210{
211 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)));
212}
213
Sheng Yangf78e0e22007-10-29 09:40:42 +0800214static inline int cpu_has_secondary_exec_ctrls(void)
215{
216 return (vmcs_config.cpu_based_exec_ctrl &
217 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS);
218}
219
Avi Kivity774ead32007-12-26 13:57:04 +0200220static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800221{
Avi Kivity4c9fc8e2008-03-24 18:15:14 +0200222 return flexpriority_enabled
223 && (vmcs_config.cpu_based_2nd_exec_ctrl &
224 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800225}
226
Sheng Yangd56f5462008-04-25 10:13:16 +0800227static inline int cpu_has_vmx_invept_individual_addr(void)
228{
229 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT));
230}
231
232static inline int cpu_has_vmx_invept_context(void)
233{
234 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT));
235}
236
237static inline int cpu_has_vmx_invept_global(void)
238{
239 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT));
240}
241
242static inline int cpu_has_vmx_ept(void)
243{
244 return (vmcs_config.cpu_based_2nd_exec_ctrl &
245 SECONDARY_EXEC_ENABLE_EPT);
246}
247
248static inline int vm_need_ept(void)
249{
250 return (cpu_has_vmx_ept() && enable_ept);
251}
252
Sheng Yangf78e0e22007-10-29 09:40:42 +0800253static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
254{
255 return ((cpu_has_vmx_virtualize_apic_accesses()) &&
256 (irqchip_in_kernel(kvm)));
257}
258
Sheng Yang2384d2b2008-01-17 15:14:33 +0800259static inline int cpu_has_vmx_vpid(void)
260{
261 return (vmcs_config.cpu_based_2nd_exec_ctrl &
262 SECONDARY_EXEC_ENABLE_VPID);
263}
264
Rusty Russell8b9cf982007-07-30 16:31:43 +1000265static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800266{
267 int i;
268
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400269 for (i = 0; i < vmx->nmsrs; ++i)
270 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300271 return i;
272 return -1;
273}
274
Sheng Yang2384d2b2008-01-17 15:14:33 +0800275static inline void __invvpid(int ext, u16 vpid, gva_t gva)
276{
277 struct {
278 u64 vpid : 16;
279 u64 rsvd : 48;
280 u64 gva;
281 } operand = { vpid, 0, gva };
282
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300283 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800284 /* CF==1 or ZF==1 --> rc = -1 */
285 "; ja 1f ; ud2 ; 1:"
286 : : "a"(&operand), "c"(ext) : "cc", "memory");
287}
288
Sheng Yang14394422008-04-28 12:24:45 +0800289static inline void __invept(int ext, u64 eptp, gpa_t gpa)
290{
291 struct {
292 u64 eptp, gpa;
293 } operand = {eptp, gpa};
294
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300295 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800296 /* CF==1 or ZF==1 --> rc = -1 */
297 "; ja 1f ; ud2 ; 1:\n"
298 : : "a" (&operand), "c" (ext) : "cc", "memory");
299}
300
Rusty Russell8b9cf982007-07-30 16:31:43 +1000301static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300302{
303 int i;
304
Rusty Russell8b9cf982007-07-30 16:31:43 +1000305 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300306 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400307 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000308 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800309}
310
Avi Kivity6aa8b732006-12-10 02:21:36 -0800311static void vmcs_clear(struct vmcs *vmcs)
312{
313 u64 phys_addr = __pa(vmcs);
314 u8 error;
315
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300316 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800317 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
318 : "cc", "memory");
319 if (error)
320 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
321 vmcs, phys_addr);
322}
323
324static void __vcpu_clear(void *arg)
325{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000326 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800327 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800328
Rusty Russell8b9cf982007-07-30 16:31:43 +1000329 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400330 vmcs_clear(vmx->vmcs);
331 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800332 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800333 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800334}
335
Rusty Russell8b9cf982007-07-30 16:31:43 +1000336static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800337{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200338 if (vmx->vcpu.cpu == -1)
339 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200340 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000341 vmx->launched = 0;
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800342}
343
Sheng Yang2384d2b2008-01-17 15:14:33 +0800344static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
345{
346 if (vmx->vpid == 0)
347 return;
348
349 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
350}
351
Sheng Yang14394422008-04-28 12:24:45 +0800352static inline void ept_sync_global(void)
353{
354 if (cpu_has_vmx_invept_global())
355 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
356}
357
358static inline void ept_sync_context(u64 eptp)
359{
360 if (vm_need_ept()) {
361 if (cpu_has_vmx_invept_context())
362 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
363 else
364 ept_sync_global();
365 }
366}
367
368static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
369{
370 if (vm_need_ept()) {
371 if (cpu_has_vmx_invept_individual_addr())
372 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
373 eptp, gpa);
374 else
375 ept_sync_context(eptp);
376 }
377}
378
Avi Kivity6aa8b732006-12-10 02:21:36 -0800379static unsigned long vmcs_readl(unsigned long field)
380{
381 unsigned long value;
382
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300383 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384 : "=a"(value) : "d"(field) : "cc");
385 return value;
386}
387
388static u16 vmcs_read16(unsigned long field)
389{
390 return vmcs_readl(field);
391}
392
393static u32 vmcs_read32(unsigned long field)
394{
395 return vmcs_readl(field);
396}
397
398static u64 vmcs_read64(unsigned long field)
399{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800400#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800401 return vmcs_readl(field);
402#else
403 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
404#endif
405}
406
Avi Kivitye52de1b2007-01-05 16:36:56 -0800407static noinline void vmwrite_error(unsigned long field, unsigned long value)
408{
409 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
410 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
411 dump_stack();
412}
413
Avi Kivity6aa8b732006-12-10 02:21:36 -0800414static void vmcs_writel(unsigned long field, unsigned long value)
415{
416 u8 error;
417
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300418 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400419 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800420 if (unlikely(error))
421 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422}
423
424static void vmcs_write16(unsigned long field, u16 value)
425{
426 vmcs_writel(field, value);
427}
428
429static void vmcs_write32(unsigned long field, u32 value)
430{
431 vmcs_writel(field, value);
432}
433
434static void vmcs_write64(unsigned long field, u64 value)
435{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300437#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438 asm volatile ("");
439 vmcs_writel(field+1, value >> 32);
440#endif
441}
442
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300443static void vmcs_clear_bits(unsigned long field, u32 mask)
444{
445 vmcs_writel(field, vmcs_readl(field) & ~mask);
446}
447
448static void vmcs_set_bits(unsigned long field, u32 mask)
449{
450 vmcs_writel(field, vmcs_readl(field) | mask);
451}
452
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300453static void update_exception_bitmap(struct kvm_vcpu *vcpu)
454{
455 u32 eb;
456
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500457 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300458 if (!vcpu->fpu_active)
459 eb |= 1u << NM_VECTOR;
460 if (vcpu->guest_debug.enabled)
461 eb |= 1u << 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800462 if (vcpu->arch.rmode.active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300463 eb = ~0;
Sheng Yang14394422008-04-28 12:24:45 +0800464 if (vm_need_ept())
465 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300466 vmcs_write32(EXCEPTION_BITMAP, eb);
467}
468
Avi Kivity33ed6322007-05-02 16:54:03 +0300469static void reload_tss(void)
470{
Avi Kivity33ed6322007-05-02 16:54:03 +0300471 /*
472 * VT restores TR but not its size. Useless.
473 */
474 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200475 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300476
477 get_gdt(&gdt);
478 descs = (void *)gdt.base;
479 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
480 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300481}
482
Rusty Russell8b9cf982007-07-30 16:31:43 +1000483static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300484{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400485 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300486 u64 host_efer = vmx->host_msrs[efer_offset].data;
487 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
488 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300489
Avi Kivity51c6cf62007-08-29 03:48:05 +0300490 if (efer_offset < 0)
491 return;
492 /*
493 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
494 * outside long mode
495 */
496 ignore_bits = EFER_NX | EFER_SCE;
497#ifdef CONFIG_X86_64
498 ignore_bits |= EFER_LMA | EFER_LME;
499 /* SCE is meaningful only in long mode on Intel */
500 if (guest_efer & EFER_LMA)
501 ignore_bits &= ~(u64)EFER_SCE;
502#endif
503 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
504 return;
505
506 vmx->host_state.guest_efer_loaded = 1;
507 guest_efer &= ~ignore_bits;
508 guest_efer |= host_efer & ignore_bits;
509 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000510 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300511}
512
Avi Kivity51c6cf62007-08-29 03:48:05 +0300513static void reload_host_efer(struct vcpu_vmx *vmx)
514{
515 if (vmx->host_state.guest_efer_loaded) {
516 vmx->host_state.guest_efer_loaded = 0;
517 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
518 }
519}
520
Avi Kivity04d2cc72007-09-10 18:10:54 +0300521static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300522{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300523 struct vcpu_vmx *vmx = to_vmx(vcpu);
524
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400525 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300526 return;
527
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400528 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300529 /*
530 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
531 * allow segment selectors with cpl > 0 or ti == 1.
532 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400533 vmx->host_state.ldt_sel = read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200534 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400535 vmx->host_state.fs_sel = read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200536 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400537 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200538 vmx->host_state.fs_reload_needed = 0;
539 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300540 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200541 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300542 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400543 vmx->host_state.gs_sel = read_gs();
544 if (!(vmx->host_state.gs_sel & 7))
545 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300546 else {
547 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200548 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300549 }
550
551#ifdef CONFIG_X86_64
552 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
553 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
554#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400555 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
556 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300557#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300558
559#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400560 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400561 save_msrs(vmx->host_msrs +
562 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400563
Avi Kivity707c0872007-05-02 17:33:43 +0300564#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400565 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300566 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300567}
568
Avi Kivitya9b21b62008-06-24 11:48:49 +0300569static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300570{
Avi Kivity15ad7142007-07-11 18:17:21 +0300571 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300572
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400573 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300574 return;
575
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200576 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400577 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200578 if (vmx->host_state.fs_reload_needed)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400579 load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200580 if (vmx->host_state.gs_ldt_reload_needed) {
581 load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300582 /*
583 * If we have to reload gs, we must take care to
584 * preserve our gs base.
585 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300586 local_irq_save(flags);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400587 load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300588#ifdef CONFIG_X86_64
589 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
590#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300591 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300592 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200593 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400594 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
595 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300596 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300597}
598
Avi Kivitya9b21b62008-06-24 11:48:49 +0300599static void vmx_load_host_state(struct vcpu_vmx *vmx)
600{
601 preempt_disable();
602 __vmx_load_host_state(vmx);
603 preempt_enable();
604}
605
Avi Kivity6aa8b732006-12-10 02:21:36 -0800606/*
607 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
608 * vcpu mutex is already taken.
609 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300610static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800611{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400612 struct vcpu_vmx *vmx = to_vmx(vcpu);
613 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200614 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800615
Eddie Donga3d7f852007-09-03 16:15:12 +0300616 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000617 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300618 kvm_migrate_timers(vcpu);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800619 vpid_sync_vcpu_all(vmx);
Eddie Donga3d7f852007-09-03 16:15:12 +0300620 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800621
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400622 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623 u8 error;
624
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400625 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300626 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
628 : "cc");
629 if (error)
630 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400631 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800632 }
633
634 if (vcpu->cpu != cpu) {
635 struct descriptor_table dt;
636 unsigned long sysenter_esp;
637
638 vcpu->cpu = cpu;
639 /*
640 * Linux uses per-cpu TSS and GDT, so set these when switching
641 * processors.
642 */
643 vmcs_writel(HOST_TR_BASE, read_tr_base()); /* 22.2.4 */
644 get_gdt(&dt);
645 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
646
647 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
648 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300649
650 /*
651 * Make sure the time stamp counter is monotonous.
652 */
653 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200654 if (tsc_this < vcpu->arch.host_tsc) {
655 delta = vcpu->arch.host_tsc - tsc_this;
656 new_offset = vmcs_read64(TSC_OFFSET) + delta;
657 vmcs_write64(TSC_OFFSET, new_offset);
658 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800659 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660}
661
662static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
663{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300664 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800665}
666
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300667static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
668{
669 if (vcpu->fpu_active)
670 return;
671 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000672 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800673 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92fa2007-07-17 23:19:08 +1000674 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300675 update_exception_bitmap(vcpu);
676}
677
678static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
679{
680 if (!vcpu->fpu_active)
681 return;
682 vcpu->fpu_active = 0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000683 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300684 update_exception_bitmap(vcpu);
685}
686
Avi Kivity774c47f2007-02-12 00:54:47 -0800687static void vmx_vcpu_decache(struct kvm_vcpu *vcpu)
688{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000689 vcpu_clear(to_vmx(vcpu));
Avi Kivity774c47f2007-02-12 00:54:47 -0800690}
691
Avi Kivity6aa8b732006-12-10 02:21:36 -0800692static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
693{
694 return vmcs_readl(GUEST_RFLAGS);
695}
696
697static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
698{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800699 if (vcpu->arch.rmode.active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100700 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701 vmcs_writel(GUEST_RFLAGS, rflags);
702}
703
704static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
705{
706 unsigned long rip;
707 u32 interruptibility;
708
709 rip = vmcs_readl(GUEST_RIP);
710 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
711 vmcs_writel(GUEST_RIP, rip);
712
713 /*
714 * We emulated an instruction, so temporary interrupt blocking
715 * should be removed, if set.
716 */
717 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
718 if (interruptibility & 3)
719 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
720 interruptibility & ~3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800721 vcpu->arch.interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800722}
723
Avi Kivity298101d2007-11-25 13:41:11 +0200724static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
725 bool has_error_code, u32 error_code)
726{
727 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
728 nr | INTR_TYPE_EXCEPTION
Ryan Harper2e113842008-02-11 10:26:38 -0600729 | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0)
Avi Kivity298101d2007-11-25 13:41:11 +0200730 | INTR_INFO_VALID_MASK);
731 if (has_error_code)
732 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
733}
734
735static bool vmx_exception_injected(struct kvm_vcpu *vcpu)
736{
737 struct vcpu_vmx *vmx = to_vmx(vcpu);
738
739 return !(vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
740}
741
Avi Kivity6aa8b732006-12-10 02:21:36 -0800742/*
Eddie Donga75beee2007-05-17 18:55:15 +0300743 * Swap MSR entry in host/guest MSR entry array.
744 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200745#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000746static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300747{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400748 struct kvm_msr_entry tmp;
749
750 tmp = vmx->guest_msrs[to];
751 vmx->guest_msrs[to] = vmx->guest_msrs[from];
752 vmx->guest_msrs[from] = tmp;
753 tmp = vmx->host_msrs[to];
754 vmx->host_msrs[to] = vmx->host_msrs[from];
755 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300756}
Gabriel C54e11fa2007-08-01 16:23:10 +0200757#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300758
759/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300760 * Set up the vmcs to automatically save and restore system
761 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
762 * mode, as fiddling with msrs is very expensive.
763 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000764static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300765{
Eddie Dong2cc51562007-05-21 07:28:09 +0300766 int save_nmsrs;
Avi Kivitye38aea32007-04-19 13:22:48 +0300767
Avi Kivity33f9c502008-02-27 16:06:57 +0200768 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300769 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300770#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000771 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300772 int index;
773
Rusty Russell8b9cf982007-07-30 16:31:43 +1000774 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300775 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000776 move_msr_up(vmx, index, save_nmsrs++);
777 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300778 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000779 move_msr_up(vmx, index, save_nmsrs++);
780 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300781 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000782 move_msr_up(vmx, index, save_nmsrs++);
783 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300784 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000785 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300786 /*
787 * MSR_K6_STAR is only needed on long mode guests, and only
788 * if efer.sce is enabled.
789 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000790 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800791 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000792 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300793 }
Eddie Donga75beee2007-05-17 18:55:15 +0300794#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400795 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300796
Eddie Donga75beee2007-05-17 18:55:15 +0300797#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400798 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000799 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300800#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000801 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivitye38aea32007-04-19 13:22:48 +0300802}
803
804/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800805 * reads and returns guest's timestamp counter "register"
806 * guest_tsc = host_tsc + tsc_offset -- 21.3
807 */
808static u64 guest_read_tsc(void)
809{
810 u64 host_tsc, tsc_offset;
811
812 rdtscll(host_tsc);
813 tsc_offset = vmcs_read64(TSC_OFFSET);
814 return host_tsc + tsc_offset;
815}
816
817/*
818 * writes 'guest_tsc' into guest's timestamp counter "register"
819 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
820 */
821static void guest_write_tsc(u64 guest_tsc)
822{
823 u64 host_tsc;
824
825 rdtscll(host_tsc);
826 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
827}
828
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829/*
830 * Reads an msr value (of 'msr_index') into 'pdata'.
831 * Returns 0 on success, non-0 otherwise.
832 * Assumes vcpu_load() was already called.
833 */
834static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
835{
836 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400837 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838
839 if (!pdata) {
840 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
841 return -EINVAL;
842 }
843
844 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800845#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800846 case MSR_FS_BASE:
847 data = vmcs_readl(GUEST_FS_BASE);
848 break;
849 case MSR_GS_BASE:
850 data = vmcs_readl(GUEST_GS_BASE);
851 break;
852 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800853 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800854#endif
855 case MSR_IA32_TIME_STAMP_COUNTER:
856 data = guest_read_tsc();
857 break;
858 case MSR_IA32_SYSENTER_CS:
859 data = vmcs_read32(GUEST_SYSENTER_CS);
860 break;
861 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200862 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863 break;
864 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200865 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000868 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800869 if (msr) {
870 data = msr->data;
871 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800872 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800873 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874 }
875
876 *pdata = data;
877 return 0;
878}
879
880/*
881 * Writes msr value into into the appropriate "register".
882 * Returns 0 on success, non-0 otherwise.
883 * Assumes vcpu_load() was already called.
884 */
885static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
886{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400887 struct vcpu_vmx *vmx = to_vmx(vcpu);
888 struct kvm_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +0300889 int ret = 0;
890
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800892#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800893 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300894 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +0300895 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +0300896 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897 case MSR_FS_BASE:
898 vmcs_writel(GUEST_FS_BASE, data);
899 break;
900 case MSR_GS_BASE:
901 vmcs_writel(GUEST_GS_BASE, data);
902 break;
903#endif
904 case MSR_IA32_SYSENTER_CS:
905 vmcs_write32(GUEST_SYSENTER_CS, data);
906 break;
907 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200908 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909 break;
910 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200911 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200913 case MSR_IA32_TIME_STAMP_COUNTER:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 guest_write_tsc(data);
915 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916 default:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300917 vmx_load_host_state(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000918 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800919 if (msr) {
920 msr->data = data;
921 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300923 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924 }
925
Eddie Dong2cc51562007-05-21 07:28:09 +0300926 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927}
928
929/*
930 * Sync the rsp and rip registers into the vcpu structure. This allows
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800931 * registers to be accessed by indexing vcpu->arch.regs.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932 */
933static void vcpu_load_rsp_rip(struct kvm_vcpu *vcpu)
934{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800935 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
936 vcpu->arch.rip = vmcs_readl(GUEST_RIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937}
938
939/*
940 * Syncs rsp and rip back into the vmcs. Should be called after possible
941 * modification.
942 */
943static void vcpu_put_rsp_rip(struct kvm_vcpu *vcpu)
944{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800945 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
946 vmcs_writel(GUEST_RIP, vcpu->arch.rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947}
948
949static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
950{
951 unsigned long dr7 = 0x400;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800952 int old_singlestep;
953
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954 old_singlestep = vcpu->guest_debug.singlestep;
955
956 vcpu->guest_debug.enabled = dbg->enabled;
957 if (vcpu->guest_debug.enabled) {
958 int i;
959
960 dr7 |= 0x200; /* exact */
961 for (i = 0; i < 4; ++i) {
962 if (!dbg->breakpoints[i].enabled)
963 continue;
964 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
965 dr7 |= 2 << (i*2); /* global enable */
966 dr7 |= 0 << (i*4+16); /* execution breakpoint */
967 }
968
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969 vcpu->guest_debug.singlestep = dbg->singlestep;
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300970 } else
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971 vcpu->guest_debug.singlestep = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972
973 if (old_singlestep && !vcpu->guest_debug.singlestep) {
974 unsigned long flags;
975
976 flags = vmcs_readl(GUEST_RFLAGS);
977 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
978 vmcs_writel(GUEST_RFLAGS, flags);
979 }
980
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300981 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982 vmcs_writel(GUEST_DR7, dr7);
983
984 return 0;
985}
986
Eddie Dong2a8067f2007-08-06 16:29:07 +0300987static int vmx_get_irq(struct kvm_vcpu *vcpu)
988{
Avi Kivity1155f762007-11-22 11:30:47 +0200989 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +0300990 u32 idtv_info_field;
991
Avi Kivity1155f762007-11-22 11:30:47 +0200992 idtv_info_field = vmx->idt_vectoring_info;
Eddie Dong2a8067f2007-08-06 16:29:07 +0300993 if (idtv_info_field & INTR_INFO_VALID_MASK) {
994 if (is_external_interrupt(idtv_info_field))
995 return idtv_info_field & VECTORING_INFO_VECTOR_MASK;
996 else
Mike Dayd77c26f2007-10-08 09:02:08 -0400997 printk(KERN_DEBUG "pending exception: not handled yet\n");
Eddie Dong2a8067f2007-08-06 16:29:07 +0300998 }
999 return -1;
1000}
1001
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002static __init int cpu_has_kvm_support(void)
1003{
1004 unsigned long ecx = cpuid_ecx(1);
1005 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
1006}
1007
1008static __init int vmx_disabled_by_bios(void)
1009{
1010 u64 msr;
1011
1012 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001013 return (msr & (MSR_IA32_FEATURE_CONTROL_LOCKED |
1014 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
1015 == MSR_IA32_FEATURE_CONTROL_LOCKED;
1016 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017}
1018
Avi Kivity774c47f2007-02-12 00:54:47 -08001019static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020{
1021 int cpu = raw_smp_processor_id();
1022 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1023 u64 old;
1024
1025 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001026 if ((old & (MSR_IA32_FEATURE_CONTROL_LOCKED |
1027 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
1028 != (MSR_IA32_FEATURE_CONTROL_LOCKED |
1029 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001031 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
1032 MSR_IA32_FEATURE_CONTROL_LOCKED |
1033 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001034 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001035 asm volatile (ASM_VMX_VMXON_RAX
1036 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037 : "memory", "cc");
1038}
1039
1040static void hardware_disable(void *garbage)
1041{
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001042 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eli Collinse693d712008-06-01 20:24:40 -07001043 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044}
1045
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001046static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001047 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048{
1049 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001050 u32 ctl = ctl_min | ctl_opt;
1051
1052 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1053
1054 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1055 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1056
1057 /* Ensure minimum (required) set of control bits are supported. */
1058 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001059 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001060
1061 *result = ctl;
1062 return 0;
1063}
1064
Yang, Sheng002c7f72007-07-31 14:23:01 +03001065static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001066{
1067 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001068 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001069 u32 _pin_based_exec_control = 0;
1070 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001071 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001072 u32 _vmexit_control = 0;
1073 u32 _vmentry_control = 0;
1074
1075 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
1076 opt = 0;
1077 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1078 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001079 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001080
1081 min = CPU_BASED_HLT_EXITING |
1082#ifdef CONFIG_X86_64
1083 CPU_BASED_CR8_LOAD_EXITING |
1084 CPU_BASED_CR8_STORE_EXITING |
1085#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001086 CPU_BASED_CR3_LOAD_EXITING |
1087 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001088 CPU_BASED_USE_IO_BITMAPS |
1089 CPU_BASED_MOV_DR_EXITING |
1090 CPU_BASED_USE_TSC_OFFSETING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001091 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001092 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001093 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001094 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1095 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001096 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001097#ifdef CONFIG_X86_64
1098 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1099 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1100 ~CPU_BASED_CR8_STORE_EXITING;
1101#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001102 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001103 min2 = 0;
1104 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001105 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001106 SECONDARY_EXEC_ENABLE_VPID |
1107 SECONDARY_EXEC_ENABLE_EPT;
1108 if (adjust_vmx_controls(min2, opt2,
1109 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001110 &_cpu_based_2nd_exec_control) < 0)
1111 return -EIO;
1112 }
1113#ifndef CONFIG_X86_64
1114 if (!(_cpu_based_2nd_exec_control &
1115 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1116 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1117#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001118 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
1119 /* CR3 accesses don't need to cause VM Exits when EPT enabled */
1120 min &= ~(CPU_BASED_CR3_LOAD_EXITING |
1121 CPU_BASED_CR3_STORE_EXITING);
1122 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1123 &_cpu_based_exec_control) < 0)
1124 return -EIO;
1125 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1126 vmx_capability.ept, vmx_capability.vpid);
1127 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001128
1129 min = 0;
1130#ifdef CONFIG_X86_64
1131 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1132#endif
1133 opt = 0;
1134 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1135 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001136 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001137
1138 min = opt = 0;
1139 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1140 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001141 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001143 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001144
1145 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1146 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001147 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001148
1149#ifdef CONFIG_X86_64
1150 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1151 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001152 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001153#endif
1154
1155 /* Require Write-Back (WB) memory type for VMCS accesses. */
1156 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001157 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001158
Yang, Sheng002c7f72007-07-31 14:23:01 +03001159 vmcs_conf->size = vmx_msr_high & 0x1fff;
1160 vmcs_conf->order = get_order(vmcs_config.size);
1161 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001162
Yang, Sheng002c7f72007-07-31 14:23:01 +03001163 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1164 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001165 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001166 vmcs_conf->vmexit_ctrl = _vmexit_control;
1167 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001168
1169 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001170}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171
1172static struct vmcs *alloc_vmcs_cpu(int cpu)
1173{
1174 int node = cpu_to_node(cpu);
1175 struct page *pages;
1176 struct vmcs *vmcs;
1177
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001178 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179 if (!pages)
1180 return NULL;
1181 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001182 memset(vmcs, 0, vmcs_config.size);
1183 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184 return vmcs;
1185}
1186
1187static struct vmcs *alloc_vmcs(void)
1188{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001189 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001190}
1191
1192static void free_vmcs(struct vmcs *vmcs)
1193{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001194 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001195}
1196
Sam Ravnborg39959582007-06-01 00:47:13 -07001197static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198{
1199 int cpu;
1200
1201 for_each_online_cpu(cpu)
1202 free_vmcs(per_cpu(vmxarea, cpu));
1203}
1204
Avi Kivity6aa8b732006-12-10 02:21:36 -08001205static __init int alloc_kvm_area(void)
1206{
1207 int cpu;
1208
1209 for_each_online_cpu(cpu) {
1210 struct vmcs *vmcs;
1211
1212 vmcs = alloc_vmcs_cpu(cpu);
1213 if (!vmcs) {
1214 free_kvm_area();
1215 return -ENOMEM;
1216 }
1217
1218 per_cpu(vmxarea, cpu) = vmcs;
1219 }
1220 return 0;
1221}
1222
1223static __init int hardware_setup(void)
1224{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001225 if (setup_vmcs_config(&vmcs_config) < 0)
1226 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001227
1228 if (boot_cpu_has(X86_FEATURE_NX))
1229 kvm_enable_efer_bits(EFER_NX);
1230
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231 return alloc_kvm_area();
1232}
1233
1234static __exit void hardware_unsetup(void)
1235{
1236 free_kvm_area();
1237}
1238
Avi Kivity6aa8b732006-12-10 02:21:36 -08001239static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1240{
1241 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1242
Avi Kivity6af11b92007-03-19 13:18:10 +02001243 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244 vmcs_write16(sf->selector, save->selector);
1245 vmcs_writel(sf->base, save->base);
1246 vmcs_write32(sf->limit, save->limit);
1247 vmcs_write32(sf->ar_bytes, save->ar);
1248 } else {
1249 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1250 << AR_DPL_SHIFT;
1251 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1252 }
1253}
1254
1255static void enter_pmode(struct kvm_vcpu *vcpu)
1256{
1257 unsigned long flags;
1258
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001259 vcpu->arch.rmode.active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001261 vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base);
1262 vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit);
1263 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264
1265 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001266 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001267 flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268 vmcs_writel(GUEST_RFLAGS, flags);
1269
Rusty Russell66aee912007-07-17 23:34:16 +10001270 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1271 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272
1273 update_exception_bitmap(vcpu);
1274
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001275 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1276 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1277 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1278 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001279
1280 vmcs_write16(GUEST_SS_SELECTOR, 0);
1281 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1282
1283 vmcs_write16(GUEST_CS_SELECTOR,
1284 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1285 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1286}
1287
Mike Dayd77c26f2007-10-08 09:02:08 -04001288static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001290 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001291 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1292 kvm->memslots[0].npages - 3;
1293 return base_gfn << PAGE_SHIFT;
1294 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001295 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296}
1297
1298static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1299{
1300 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1301
1302 save->selector = vmcs_read16(sf->selector);
1303 save->base = vmcs_readl(sf->base);
1304 save->limit = vmcs_read32(sf->limit);
1305 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001306 vmcs_write16(sf->selector, save->base >> 4);
1307 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001308 vmcs_write32(sf->limit, 0xffff);
1309 vmcs_write32(sf->ar_bytes, 0xf3);
1310}
1311
1312static void enter_rmode(struct kvm_vcpu *vcpu)
1313{
1314 unsigned long flags;
1315
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001316 vcpu->arch.rmode.active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001318 vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1320
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001321 vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1323
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001324 vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1326
1327 flags = vmcs_readl(GUEST_RFLAGS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001328 vcpu->arch.rmode.save_iopl
1329 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001330
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001331 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001332
1333 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001334 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335 update_exception_bitmap(vcpu);
1336
1337 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1338 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1339 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1340
1341 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001342 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001343 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1344 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001345 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1346
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001347 fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1348 fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1349 fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1350 fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001351
Eddie Dong8668a3c2007-10-10 14:26:45 +08001352 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001353 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001354}
1355
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001356#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357
1358static void enter_lmode(struct kvm_vcpu *vcpu)
1359{
1360 u32 guest_tr_ar;
1361
1362 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1363 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1364 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001365 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366 vmcs_write32(GUEST_TR_AR_BYTES,
1367 (guest_tr_ar & ~AR_TYPE_MASK)
1368 | AR_TYPE_BUSY_64_TSS);
1369 }
1370
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001371 vcpu->arch.shadow_efer |= EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372
Rusty Russell8b9cf982007-07-30 16:31:43 +10001373 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374 vmcs_write32(VM_ENTRY_CONTROLS,
1375 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001376 | VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377}
1378
1379static void exit_lmode(struct kvm_vcpu *vcpu)
1380{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001381 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382
1383 vmcs_write32(VM_ENTRY_CONTROLS,
1384 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001385 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386}
1387
1388#endif
1389
Sheng Yang2384d2b2008-01-17 15:14:33 +08001390static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1391{
1392 vpid_sync_vcpu_all(to_vmx(vcpu));
1393}
1394
Anthony Liguori25c4c272007-04-27 09:29:21 +03001395static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001396{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001397 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1398 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001399}
1400
Sheng Yang14394422008-04-28 12:24:45 +08001401static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1402{
1403 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1404 if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
1405 printk(KERN_ERR "EPT: Fail to load pdptrs!\n");
1406 return;
1407 }
1408 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1409 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1410 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1411 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1412 }
1413}
1414
1415static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1416
1417static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1418 unsigned long cr0,
1419 struct kvm_vcpu *vcpu)
1420{
1421 if (!(cr0 & X86_CR0_PG)) {
1422 /* From paging/starting to nonpaging */
1423 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
1424 vmcs_config.cpu_based_exec_ctrl |
1425 (CPU_BASED_CR3_LOAD_EXITING |
1426 CPU_BASED_CR3_STORE_EXITING));
1427 vcpu->arch.cr0 = cr0;
1428 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1429 *hw_cr0 |= X86_CR0_PE | X86_CR0_PG;
1430 *hw_cr0 &= ~X86_CR0_WP;
1431 } else if (!is_paging(vcpu)) {
1432 /* From nonpaging to paging */
1433 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
1434 vmcs_config.cpu_based_exec_ctrl &
1435 ~(CPU_BASED_CR3_LOAD_EXITING |
1436 CPU_BASED_CR3_STORE_EXITING));
1437 vcpu->arch.cr0 = cr0;
1438 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1439 if (!(vcpu->arch.cr0 & X86_CR0_WP))
1440 *hw_cr0 &= ~X86_CR0_WP;
1441 }
1442}
1443
1444static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1445 struct kvm_vcpu *vcpu)
1446{
1447 if (!is_paging(vcpu)) {
1448 *hw_cr4 &= ~X86_CR4_PAE;
1449 *hw_cr4 |= X86_CR4_PSE;
1450 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1451 *hw_cr4 &= ~X86_CR4_PAE;
1452}
1453
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1455{
Sheng Yang14394422008-04-28 12:24:45 +08001456 unsigned long hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) |
1457 KVM_VM_CR0_ALWAYS_ON;
1458
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001459 vmx_fpu_deactivate(vcpu);
1460
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001461 if (vcpu->arch.rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462 enter_pmode(vcpu);
1463
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001464 if (!vcpu->arch.rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001465 enter_rmode(vcpu);
1466
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001467#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001468 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001469 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001470 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001471 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472 exit_lmode(vcpu);
1473 }
1474#endif
1475
Sheng Yang14394422008-04-28 12:24:45 +08001476 if (vm_need_ept())
1477 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1478
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001480 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001481 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001482
Rusty Russell707d92fa2007-07-17 23:19:08 +10001483 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001484 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485}
1486
Sheng Yang14394422008-04-28 12:24:45 +08001487static u64 construct_eptp(unsigned long root_hpa)
1488{
1489 u64 eptp;
1490
1491 /* TODO write the value reading from MSR */
1492 eptp = VMX_EPT_DEFAULT_MT |
1493 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1494 eptp |= (root_hpa & PAGE_MASK);
1495
1496 return eptp;
1497}
1498
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1500{
Sheng Yang14394422008-04-28 12:24:45 +08001501 unsigned long guest_cr3;
1502 u64 eptp;
1503
1504 guest_cr3 = cr3;
1505 if (vm_need_ept()) {
1506 eptp = construct_eptp(cr3);
1507 vmcs_write64(EPT_POINTER, eptp);
1508 ept_sync_context(eptp);
1509 ept_load_pdptrs(vcpu);
1510 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
1511 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1512 }
1513
Sheng Yang2384d2b2008-01-17 15:14:33 +08001514 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001515 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001516 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001517 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001518}
1519
1520static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1521{
Sheng Yang14394422008-04-28 12:24:45 +08001522 unsigned long hw_cr4 = cr4 | (vcpu->arch.rmode.active ?
1523 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1524
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001525 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08001526 if (vm_need_ept())
1527 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1528
1529 vmcs_writel(CR4_READ_SHADOW, cr4);
1530 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001531}
1532
Avi Kivity6aa8b732006-12-10 02:21:36 -08001533static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1534{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001535 struct vcpu_vmx *vmx = to_vmx(vcpu);
1536 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001538 vcpu->arch.shadow_efer = efer;
Joerg Roedel9f62e192008-01-31 14:57:39 +01001539 if (!msr)
1540 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001541 if (efer & EFER_LMA) {
1542 vmcs_write32(VM_ENTRY_CONTROLS,
1543 vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001544 VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001545 msr->data = efer;
1546
1547 } else {
1548 vmcs_write32(VM_ENTRY_CONTROLS,
1549 vmcs_read32(VM_ENTRY_CONTROLS) &
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001550 ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001551
1552 msr->data = efer & ~EFER_LME;
1553 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001554 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001555}
1556
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1558{
1559 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1560
1561 return vmcs_readl(sf->base);
1562}
1563
1564static void vmx_get_segment(struct kvm_vcpu *vcpu,
1565 struct kvm_segment *var, int seg)
1566{
1567 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1568 u32 ar;
1569
1570 var->base = vmcs_readl(sf->base);
1571 var->limit = vmcs_read32(sf->limit);
1572 var->selector = vmcs_read16(sf->selector);
1573 ar = vmcs_read32(sf->ar_bytes);
1574 if (ar & AR_UNUSABLE_MASK)
1575 ar = 0;
1576 var->type = ar & 15;
1577 var->s = (ar >> 4) & 1;
1578 var->dpl = (ar >> 5) & 3;
1579 var->present = (ar >> 7) & 1;
1580 var->avl = (ar >> 12) & 1;
1581 var->l = (ar >> 13) & 1;
1582 var->db = (ar >> 14) & 1;
1583 var->g = (ar >> 15) & 1;
1584 var->unusable = (ar >> 16) & 1;
1585}
1586
Izik Eidus2e4d2652008-03-24 19:38:34 +02001587static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1588{
1589 struct kvm_segment kvm_seg;
1590
1591 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1592 return 0;
1593
1594 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1595 return 3;
1596
1597 vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
1598 return kvm_seg.selector & 3;
1599}
1600
Avi Kivity653e3102007-05-07 10:55:37 +03001601static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603 u32 ar;
1604
Avi Kivity653e3102007-05-07 10:55:37 +03001605 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001606 ar = 1 << 16;
1607 else {
1608 ar = var->type & 15;
1609 ar |= (var->s & 1) << 4;
1610 ar |= (var->dpl & 3) << 5;
1611 ar |= (var->present & 1) << 7;
1612 ar |= (var->avl & 1) << 12;
1613 ar |= (var->l & 1) << 13;
1614 ar |= (var->db & 1) << 14;
1615 ar |= (var->g & 1) << 15;
1616 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001617 if (ar == 0) /* a 0 value means unusable */
1618 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001619
1620 return ar;
1621}
1622
1623static void vmx_set_segment(struct kvm_vcpu *vcpu,
1624 struct kvm_segment *var, int seg)
1625{
1626 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1627 u32 ar;
1628
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001629 if (vcpu->arch.rmode.active && seg == VCPU_SREG_TR) {
1630 vcpu->arch.rmode.tr.selector = var->selector;
1631 vcpu->arch.rmode.tr.base = var->base;
1632 vcpu->arch.rmode.tr.limit = var->limit;
1633 vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001634 return;
1635 }
1636 vmcs_writel(sf->base, var->base);
1637 vmcs_write32(sf->limit, var->limit);
1638 vmcs_write16(sf->selector, var->selector);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001639 if (vcpu->arch.rmode.active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001640 /*
1641 * Hack real-mode segments into vm86 compatibility.
1642 */
1643 if (var->base == 0xffff0000 && var->selector == 0xf000)
1644 vmcs_writel(sf->base, 0xf0000);
1645 ar = 0xf3;
1646 } else
1647 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001648 vmcs_write32(sf->ar_bytes, ar);
1649}
1650
Avi Kivity6aa8b732006-12-10 02:21:36 -08001651static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1652{
1653 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1654
1655 *db = (ar >> 14) & 1;
1656 *l = (ar >> 13) & 1;
1657}
1658
1659static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1660{
1661 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1662 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1663}
1664
1665static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1666{
1667 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1668 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1669}
1670
1671static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1672{
1673 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1674 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1675}
1676
1677static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1678{
1679 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1680 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1681}
1682
Mike Dayd77c26f2007-10-08 09:02:08 -04001683static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001684{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001685 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001686 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001687 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02001688 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689
Izik Eidus195aefd2007-10-01 22:14:18 +02001690 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1691 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001692 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001693 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
1694 r = kvm_write_guest_page(kvm, fn++, &data, 0x66, sizeof(u16));
1695 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001696 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001697 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
1698 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001699 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001700 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1701 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001702 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001703 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001704 r = kvm_write_guest_page(kvm, fn, &data,
1705 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
1706 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02001707 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001708 goto out;
1709
1710 ret = 1;
1711out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001712 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001713}
1714
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001715static int init_rmode_identity_map(struct kvm *kvm)
1716{
1717 int i, r, ret;
1718 pfn_t identity_map_pfn;
1719 u32 tmp;
1720
1721 if (!vm_need_ept())
1722 return 1;
1723 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
1724 printk(KERN_ERR "EPT: identity-mapping pagetable "
1725 "haven't been allocated!\n");
1726 return 0;
1727 }
1728 if (likely(kvm->arch.ept_identity_pagetable_done))
1729 return 1;
1730 ret = 0;
1731 identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT;
1732 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
1733 if (r < 0)
1734 goto out;
1735 /* Set up identity-mapping pagetable for EPT in real mode */
1736 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
1737 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
1738 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
1739 r = kvm_write_guest_page(kvm, identity_map_pfn,
1740 &tmp, i * sizeof(tmp), sizeof(tmp));
1741 if (r < 0)
1742 goto out;
1743 }
1744 kvm->arch.ept_identity_pagetable_done = true;
1745 ret = 1;
1746out:
1747 return ret;
1748}
1749
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750static void seg_setup(int seg)
1751{
1752 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1753
1754 vmcs_write16(sf->selector, 0);
1755 vmcs_writel(sf->base, 0);
1756 vmcs_write32(sf->limit, 0xffff);
1757 vmcs_write32(sf->ar_bytes, 0x93);
1758}
1759
Sheng Yangf78e0e22007-10-29 09:40:42 +08001760static int alloc_apic_access_page(struct kvm *kvm)
1761{
1762 struct kvm_userspace_memory_region kvm_userspace_mem;
1763 int r = 0;
1764
Izik Eidus72dc67a2008-02-10 18:04:15 +02001765 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001766 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001767 goto out;
1768 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
1769 kvm_userspace_mem.flags = 0;
1770 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
1771 kvm_userspace_mem.memory_size = PAGE_SIZE;
1772 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
1773 if (r)
1774 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001775
1776 down_read(&current->mm->mmap_sem);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001777 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001778 up_read(&current->mm->mmap_sem);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001779out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001780 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001781 return r;
1782}
1783
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001784static int alloc_identity_pagetable(struct kvm *kvm)
1785{
1786 struct kvm_userspace_memory_region kvm_userspace_mem;
1787 int r = 0;
1788
1789 down_write(&kvm->slots_lock);
1790 if (kvm->arch.ept_identity_pagetable)
1791 goto out;
1792 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
1793 kvm_userspace_mem.flags = 0;
1794 kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1795 kvm_userspace_mem.memory_size = PAGE_SIZE;
1796 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
1797 if (r)
1798 goto out;
1799
1800 down_read(&current->mm->mmap_sem);
1801 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
1802 VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT);
1803 up_read(&current->mm->mmap_sem);
1804out:
1805 up_write(&kvm->slots_lock);
1806 return r;
1807}
1808
Sheng Yang2384d2b2008-01-17 15:14:33 +08001809static void allocate_vpid(struct vcpu_vmx *vmx)
1810{
1811 int vpid;
1812
1813 vmx->vpid = 0;
1814 if (!enable_vpid || !cpu_has_vmx_vpid())
1815 return;
1816 spin_lock(&vmx_vpid_lock);
1817 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
1818 if (vpid < VMX_NR_VPIDS) {
1819 vmx->vpid = vpid;
1820 __set_bit(vpid, vmx_vpid_bitmap);
1821 }
1822 spin_unlock(&vmx_vpid_lock);
1823}
1824
Harvey Harrison8b2cf732008-04-27 12:14:13 -07001825static void vmx_disable_intercept_for_msr(struct page *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08001826{
1827 void *va;
1828
1829 if (!cpu_has_vmx_msr_bitmap())
1830 return;
1831
1832 /*
1833 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
1834 * have the write-low and read-high bitmap offsets the wrong way round.
1835 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
1836 */
1837 va = kmap(msr_bitmap);
1838 if (msr <= 0x1fff) {
1839 __clear_bit(msr, va + 0x000); /* read-low */
1840 __clear_bit(msr, va + 0x800); /* write-low */
1841 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
1842 msr &= 0x1fff;
1843 __clear_bit(msr, va + 0x400); /* read-high */
1844 __clear_bit(msr, va + 0xc00); /* write-high */
1845 }
1846 kunmap(msr_bitmap);
1847}
1848
Avi Kivity6aa8b732006-12-10 02:21:36 -08001849/*
1850 * Sets up the vmcs for emulated real mode.
1851 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001852static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001853{
1854 u32 host_sysenter_cs;
1855 u32 junk;
1856 unsigned long a;
1857 struct descriptor_table dt;
1858 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03001859 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001860 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001861
Avi Kivity6aa8b732006-12-10 02:21:36 -08001862 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03001863 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
1864 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001865
Sheng Yang25c5f222008-03-28 13:18:56 +08001866 if (cpu_has_vmx_msr_bitmap())
1867 vmcs_write64(MSR_BITMAP, page_to_phys(vmx_msr_bitmap));
1868
Avi Kivity6aa8b732006-12-10 02:21:36 -08001869 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
1870
Avi Kivity6aa8b732006-12-10 02:21:36 -08001871 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001872 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
1873 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001874
1875 exec_control = vmcs_config.cpu_based_exec_ctrl;
1876 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
1877 exec_control &= ~CPU_BASED_TPR_SHADOW;
1878#ifdef CONFIG_X86_64
1879 exec_control |= CPU_BASED_CR8_STORE_EXITING |
1880 CPU_BASED_CR8_LOAD_EXITING;
1881#endif
1882 }
Sheng Yangd56f5462008-04-25 10:13:16 +08001883 if (!vm_need_ept())
1884 exec_control |= CPU_BASED_CR3_STORE_EXITING |
1885 CPU_BASED_CR3_LOAD_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001886 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001887
Sheng Yang83ff3b92007-11-21 14:33:25 +08001888 if (cpu_has_secondary_exec_ctrls()) {
1889 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
1890 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
1891 exec_control &=
1892 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001893 if (vmx->vpid == 0)
1894 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yangd56f5462008-04-25 10:13:16 +08001895 if (!vm_need_ept())
1896 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang83ff3b92007-11-21 14:33:25 +08001897 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
1898 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08001899
Avi Kivityc7addb92007-09-16 18:58:32 +02001900 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
1901 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001902 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
1903
1904 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
1905 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
1906 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1907
1908 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
1909 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1910 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1911 vmcs_write16(HOST_FS_SELECTOR, read_fs()); /* 22.2.4 */
1912 vmcs_write16(HOST_GS_SELECTOR, read_gs()); /* 22.2.4 */
1913 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001914#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001915 rdmsrl(MSR_FS_BASE, a);
1916 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
1917 rdmsrl(MSR_GS_BASE, a);
1918 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
1919#else
1920 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
1921 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
1922#endif
1923
1924 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
1925
1926 get_idt(&dt);
1927 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
1928
Mike Dayd77c26f2007-10-08 09:02:08 -04001929 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03001930 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03001931 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
1932 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
1933 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001934
1935 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
1936 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
1937 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
1938 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
1939 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
1940 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
1941
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942 for (i = 0; i < NR_VMX_MSR; ++i) {
1943 u32 index = vmx_msr_index[i];
1944 u32 data_low, data_high;
1945 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001946 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001947
1948 if (rdmsr_safe(index, &data_low, &data_high) < 0)
1949 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08001950 if (wrmsr_safe(index, data_low, data_high) < 0)
1951 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001953 vmx->host_msrs[j].index = index;
1954 vmx->host_msrs[j].reserved = 0;
1955 vmx->host_msrs[j].data = data;
1956 vmx->guest_msrs[j] = vmx->host_msrs[j];
1957 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001958 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001959
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001960 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961
1962 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001963 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
1964
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001965 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
1966 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
1967
Sheng Yangf78e0e22007-10-29 09:40:42 +08001968
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001969 return 0;
1970}
1971
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001972static int init_rmode(struct kvm *kvm)
1973{
1974 if (!init_rmode_tss(kvm))
1975 return 0;
1976 if (!init_rmode_identity_map(kvm))
1977 return 0;
1978 return 1;
1979}
1980
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001981static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
1982{
1983 struct vcpu_vmx *vmx = to_vmx(vcpu);
1984 u64 msr;
1985 int ret;
1986
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001987 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001988 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001989 ret = -ENOMEM;
1990 goto out;
1991 }
1992
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001993 vmx->vcpu.arch.rmode.active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001994
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001995 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001996 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001997 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
1998 if (vmx->vcpu.vcpu_id == 0)
1999 msr |= MSR_IA32_APICBASE_BSP;
2000 kvm_set_apic_base(&vmx->vcpu, msr);
2001
2002 fx_init(&vmx->vcpu);
2003
2004 /*
2005 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2006 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2007 */
2008 if (vmx->vcpu.vcpu_id == 0) {
2009 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2010 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2011 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002012 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2013 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002014 }
2015 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
2016 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
2017
2018 seg_setup(VCPU_SREG_DS);
2019 seg_setup(VCPU_SREG_ES);
2020 seg_setup(VCPU_SREG_FS);
2021 seg_setup(VCPU_SREG_GS);
2022 seg_setup(VCPU_SREG_SS);
2023
2024 vmcs_write16(GUEST_TR_SELECTOR, 0);
2025 vmcs_writel(GUEST_TR_BASE, 0);
2026 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2027 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2028
2029 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2030 vmcs_writel(GUEST_LDTR_BASE, 0);
2031 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2032 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2033
2034 vmcs_write32(GUEST_SYSENTER_CS, 0);
2035 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2036 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2037
2038 vmcs_writel(GUEST_RFLAGS, 0x02);
2039 if (vmx->vcpu.vcpu_id == 0)
2040 vmcs_writel(GUEST_RIP, 0xfff0);
2041 else
2042 vmcs_writel(GUEST_RIP, 0);
2043 vmcs_writel(GUEST_RSP, 0);
2044
2045 /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */
2046 vmcs_writel(GUEST_DR7, 0x400);
2047
2048 vmcs_writel(GUEST_GDTR_BASE, 0);
2049 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2050
2051 vmcs_writel(GUEST_IDTR_BASE, 0);
2052 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2053
2054 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2055 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2056 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2057
2058 guest_write_tsc(0);
2059
2060 /* Special registers */
2061 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2062
2063 setup_msrs(vmx);
2064
Avi Kivity6aa8b732006-12-10 02:21:36 -08002065 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2066
Sheng Yangf78e0e22007-10-29 09:40:42 +08002067 if (cpu_has_vmx_tpr_shadow()) {
2068 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2069 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2070 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002071 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002072 vmcs_write32(TPR_THRESHOLD, 0);
2073 }
2074
2075 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2076 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002077 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078
Sheng Yang2384d2b2008-01-17 15:14:33 +08002079 if (vmx->vpid != 0)
2080 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2081
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002082 vmx->vcpu.arch.cr0 = 0x60000010;
2083 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002084 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002085 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002086 vmx_fpu_activate(&vmx->vcpu);
2087 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088
Sheng Yang2384d2b2008-01-17 15:14:33 +08002089 vpid_sync_vcpu_all(vmx);
2090
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002091 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002092
Avi Kivity6aa8b732006-12-10 02:21:36 -08002093out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002094 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002095 return ret;
2096}
2097
Eddie Dong85f455f2007-07-06 12:20:49 +03002098static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
2099{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002100 struct vcpu_vmx *vmx = to_vmx(vcpu);
2101
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002102 KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler);
2103
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002104 if (vcpu->arch.rmode.active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002105 vmx->rmode.irq.pending = true;
2106 vmx->rmode.irq.vector = irq;
2107 vmx->rmode.irq.rip = vmcs_readl(GUEST_RIP);
Avi Kivity9c5623e2007-11-08 18:19:20 +02002108 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2109 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2110 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Avi Kivity9c8cba32007-11-22 11:42:59 +02002111 vmcs_writel(GUEST_RIP, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002112 return;
2113 }
2114 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2115 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
2116}
2117
Avi Kivity6aa8b732006-12-10 02:21:36 -08002118static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
2119{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002120 int word_index = __ffs(vcpu->arch.irq_summary);
2121 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122 int irq = word_index * BITS_PER_LONG + bit_index;
2123
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002124 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
2125 if (!vcpu->arch.irq_pending[word_index])
2126 clear_bit(word_index, &vcpu->arch.irq_summary);
Eddie Dong85f455f2007-07-06 12:20:49 +03002127 vmx_inject_irq(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002128}
2129
Dor Laorc1150d82007-01-05 16:36:24 -08002130
2131static void do_interrupt_requests(struct kvm_vcpu *vcpu,
2132 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002133{
Dor Laorc1150d82007-01-05 16:36:24 -08002134 u32 cpu_based_vm_exec_control;
2135
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002136 vcpu->arch.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08002137 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2138 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
2139
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002140 if (vcpu->arch.interrupt_window_open &&
2141 vcpu->arch.irq_summary &&
Dor Laorc1150d82007-01-05 16:36:24 -08002142 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143 /*
Dor Laorc1150d82007-01-05 16:36:24 -08002144 * If interrupts enabled, and not blocked by sti or mov ss. Good.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002145 */
2146 kvm_do_inject_irq(vcpu);
Dor Laorc1150d82007-01-05 16:36:24 -08002147
2148 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002149 if (!vcpu->arch.interrupt_window_open &&
2150 (vcpu->arch.irq_summary || kvm_run->request_interrupt_window))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002151 /*
2152 * Interrupts blocked. Wait for unblock.
2153 */
Dor Laorc1150d82007-01-05 16:36:24 -08002154 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2155 else
2156 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2157 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002158}
2159
Izik Eiduscbc94022007-10-25 00:29:55 +02002160static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2161{
2162 int ret;
2163 struct kvm_userspace_memory_region tss_mem = {
2164 .slot = 8,
2165 .guest_phys_addr = addr,
2166 .memory_size = PAGE_SIZE * 3,
2167 .flags = 0,
2168 };
2169
2170 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2171 if (ret)
2172 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002173 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002174 return 0;
2175}
2176
Avi Kivity6aa8b732006-12-10 02:21:36 -08002177static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
2178{
2179 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
2180
2181 set_debugreg(dbg->bp[0], 0);
2182 set_debugreg(dbg->bp[1], 1);
2183 set_debugreg(dbg->bp[2], 2);
2184 set_debugreg(dbg->bp[3], 3);
2185
2186 if (dbg->singlestep) {
2187 unsigned long flags;
2188
2189 flags = vmcs_readl(GUEST_RFLAGS);
2190 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
2191 vmcs_writel(GUEST_RFLAGS, flags);
2192 }
2193}
2194
2195static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2196 int vec, u32 err_code)
2197{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002198 if (!vcpu->arch.rmode.active)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002199 return 0;
2200
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002201 /*
2202 * Instruction with address size override prefix opcode 0x67
2203 * Cause the #SS fault with 0 error code in VM86 mode.
2204 */
2205 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02002206 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002207 return 1;
2208 return 0;
2209}
2210
2211static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2212{
Avi Kivity1155f762007-11-22 11:30:47 +02002213 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002214 u32 intr_info, error_code;
2215 unsigned long cr2, rip;
2216 u32 vect_info;
2217 enum emulation_result er;
2218
Avi Kivity1155f762007-11-22 11:30:47 +02002219 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002220 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2221
2222 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002223 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002224 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002225 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002226
Eddie Dong85f455f2007-07-06 12:20:49 +03002227 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002229 set_bit(irq, vcpu->arch.irq_pending);
2230 set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002231 }
2232
Avi Kivity1b6269d2007-10-09 12:12:19 +02002233 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
2234 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002235
2236 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002237 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002238 return 1;
2239 }
2240
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002241 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08002242 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002243 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002244 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002245 return 1;
2246 }
2247
Avi Kivity6aa8b732006-12-10 02:21:36 -08002248 error_code = 0;
2249 rip = vmcs_readl(GUEST_RIP);
Ryan Harper2e113842008-02-11 10:26:38 -06002250 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002251 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2252 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002253 /* EPT won't cause page fault directly */
2254 if (vm_need_ept())
2255 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002257 KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2,
2258 (u32)((u64)cr2 >> 32), handler);
Avi Kivity30677142007-10-28 18:48:59 +02002259 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260 }
2261
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002262 if (vcpu->arch.rmode.active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002263 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002264 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002265 if (vcpu->arch.halt_request) {
2266 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002267 return kvm_emulate_halt(vcpu);
2268 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002270 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271
Mike Dayd77c26f2007-10-08 09:02:08 -04002272 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) ==
2273 (INTR_TYPE_EXCEPTION | 1)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002274 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2275 return 0;
2276 }
2277 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2278 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
2279 kvm_run->ex.error_code = error_code;
2280 return 0;
2281}
2282
2283static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2284 struct kvm_run *kvm_run)
2285{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002286 ++vcpu->stat.irq_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002287 KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002288 return 1;
2289}
2290
Avi Kivity988ad742007-02-12 00:54:36 -08002291static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2292{
2293 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2294 return 0;
2295}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002296
Avi Kivity6aa8b732006-12-10 02:21:36 -08002297static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2298{
He, Qingbfdaab02007-09-12 14:18:28 +08002299 unsigned long exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02002300 int size, down, in, string, rep;
2301 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002302
Avi Kivity1165f5f2007-04-19 17:27:43 +03002303 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002304 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002305 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002306
2307 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002308 if (emulate_instruction(vcpu,
2309 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002310 return 0;
2311 return 1;
2312 }
2313
2314 size = (exit_qualification & 7) + 1;
2315 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002316 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002317 rep = (exit_qualification & 32) != 0;
2318 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002319
Laurent Vivier3090dd72007-08-05 10:43:32 +03002320 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321}
2322
Ingo Molnar102d8322007-02-19 14:37:47 +02002323static void
2324vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2325{
2326 /*
2327 * Patch in the VMCALL instruction:
2328 */
2329 hypercall[0] = 0x0f;
2330 hypercall[1] = 0x01;
2331 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002332}
2333
Avi Kivity6aa8b732006-12-10 02:21:36 -08002334static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2335{
He, Qingbfdaab02007-09-12 14:18:28 +08002336 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002337 int cr;
2338 int reg;
2339
He, Qingbfdaab02007-09-12 14:18:28 +08002340 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341 cr = exit_qualification & 15;
2342 reg = (exit_qualification >> 8) & 15;
2343 switch ((exit_qualification >> 4) & 3) {
2344 case 0: /* mov to cr */
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002345 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr, (u32)vcpu->arch.regs[reg],
2346 (u32)((u64)vcpu->arch.regs[reg] >> 32), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347 switch (cr) {
2348 case 0:
2349 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002350 kvm_set_cr0(vcpu, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351 skip_emulated_instruction(vcpu);
2352 return 1;
2353 case 3:
2354 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002355 kvm_set_cr3(vcpu, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002356 skip_emulated_instruction(vcpu);
2357 return 1;
2358 case 4:
2359 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002360 kvm_set_cr4(vcpu, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002361 skip_emulated_instruction(vcpu);
2362 return 1;
2363 case 8:
2364 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002365 kvm_set_cr8(vcpu, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366 skip_emulated_instruction(vcpu);
Avi Kivitye5314062007-12-06 16:32:45 +02002367 if (irqchip_in_kernel(vcpu->kvm))
2368 return 1;
Yang, Sheng253abde2007-08-16 13:01:00 +03002369 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2370 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371 };
2372 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002373 case 2: /* clts */
2374 vcpu_load_rsp_rip(vcpu);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002375 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002376 vcpu->arch.cr0 &= ~X86_CR0_TS;
2377 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002378 vmx_fpu_activate(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002379 KVMTRACE_0D(CLTS, vcpu, handler);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002380 skip_emulated_instruction(vcpu);
2381 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002382 case 1: /*mov from cr*/
2383 switch (cr) {
2384 case 3:
2385 vcpu_load_rsp_rip(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002386 vcpu->arch.regs[reg] = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387 vcpu_put_rsp_rip(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002388 KVMTRACE_3D(CR_READ, vcpu, (u32)cr,
2389 (u32)vcpu->arch.regs[reg],
2390 (u32)((u64)vcpu->arch.regs[reg] >> 32),
2391 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002392 skip_emulated_instruction(vcpu);
2393 return 1;
2394 case 8:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002395 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002396 vcpu->arch.regs[reg] = kvm_get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002397 vcpu_put_rsp_rip(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002398 KVMTRACE_2D(CR_READ, vcpu, (u32)cr,
2399 (u32)vcpu->arch.regs[reg], handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002400 skip_emulated_instruction(vcpu);
2401 return 1;
2402 }
2403 break;
2404 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002405 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002406
2407 skip_emulated_instruction(vcpu);
2408 return 1;
2409 default:
2410 break;
2411 }
2412 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002413 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002414 (int)(exit_qualification >> 4) & 3, cr);
2415 return 0;
2416}
2417
2418static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2419{
He, Qingbfdaab02007-09-12 14:18:28 +08002420 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421 unsigned long val;
2422 int dr, reg;
2423
2424 /*
2425 * FIXME: this code assumes the host is debugging the guest.
2426 * need to deal with guest debugging itself too.
2427 */
He, Qingbfdaab02007-09-12 14:18:28 +08002428 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429 dr = exit_qualification & 7;
2430 reg = (exit_qualification >> 8) & 15;
2431 vcpu_load_rsp_rip(vcpu);
2432 if (exit_qualification & 16) {
2433 /* mov from dr */
2434 switch (dr) {
2435 case 6:
2436 val = 0xffff0ff0;
2437 break;
2438 case 7:
2439 val = 0x400;
2440 break;
2441 default:
2442 val = 0;
2443 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002444 vcpu->arch.regs[reg] = val;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002445 KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002446 } else {
2447 /* mov to dr */
2448 }
2449 vcpu_put_rsp_rip(vcpu);
2450 skip_emulated_instruction(vcpu);
2451 return 1;
2452}
2453
2454static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2455{
Avi Kivity06465c52007-02-28 20:46:53 +02002456 kvm_emulate_cpuid(vcpu);
2457 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458}
2459
2460static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2461{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002462 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463 u64 data;
2464
2465 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002466 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002467 return 1;
2468 }
2469
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002470 KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32),
2471 handler);
2472
Avi Kivity6aa8b732006-12-10 02:21:36 -08002473 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002474 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
2475 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002476 skip_emulated_instruction(vcpu);
2477 return 1;
2478}
2479
2480static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2481{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002482 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
2483 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
2484 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002485
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002486 KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32),
2487 handler);
2488
Avi Kivity6aa8b732006-12-10 02:21:36 -08002489 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002490 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002491 return 1;
2492 }
2493
2494 skip_emulated_instruction(vcpu);
2495 return 1;
2496}
2497
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002498static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2499 struct kvm_run *kvm_run)
2500{
2501 return 1;
2502}
2503
Avi Kivity6aa8b732006-12-10 02:21:36 -08002504static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2505 struct kvm_run *kvm_run)
2506{
Eddie Dong85f455f2007-07-06 12:20:49 +03002507 u32 cpu_based_vm_exec_control;
2508
2509 /* clear pending irq */
2510 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2511 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2512 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002513
2514 KVMTRACE_0D(PEND_INTR, vcpu, handler);
2515
Dor Laorc1150d82007-01-05 16:36:24 -08002516 /*
2517 * If the user space waits to inject interrupts, exit as soon as
2518 * possible
2519 */
2520 if (kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002521 !vcpu->arch.irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002522 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002523 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002524 return 0;
2525 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002526 return 1;
2527}
2528
2529static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2530{
2531 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002532 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002533}
2534
Ingo Molnarc21415e2007-02-19 14:37:47 +02002535static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2536{
Dor Laor510043d2007-02-19 18:25:43 +02002537 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002538 kvm_emulate_hypercall(vcpu);
2539 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02002540}
2541
Eddie Donge5edaa02007-11-11 12:28:35 +02002542static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2543{
2544 skip_emulated_instruction(vcpu);
2545 /* TODO: Add support for VT-d/pass-through device */
2546 return 1;
2547}
2548
Sheng Yangf78e0e22007-10-29 09:40:42 +08002549static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2550{
2551 u64 exit_qualification;
2552 enum emulation_result er;
2553 unsigned long offset;
2554
2555 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2556 offset = exit_qualification & 0xffful;
2557
2558 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
2559
2560 if (er != EMULATE_DONE) {
2561 printk(KERN_ERR
2562 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
2563 offset);
2564 return -ENOTSUPP;
2565 }
2566 return 1;
2567}
2568
Izik Eidus37817f22008-03-24 23:14:53 +02002569static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2570{
2571 unsigned long exit_qualification;
2572 u16 tss_selector;
2573 int reason;
2574
2575 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
2576
2577 reason = (u32)exit_qualification >> 30;
2578 tss_selector = exit_qualification;
2579
2580 return kvm_task_switch(vcpu, tss_selector, reason);
2581}
2582
Sheng Yang14394422008-04-28 12:24:45 +08002583static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2584{
2585 u64 exit_qualification;
2586 enum emulation_result er;
2587 gpa_t gpa;
2588 unsigned long hva;
2589 int gla_validity;
2590 int r;
2591
2592 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2593
2594 if (exit_qualification & (1 << 6)) {
2595 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
2596 return -ENOTSUPP;
2597 }
2598
2599 gla_validity = (exit_qualification >> 7) & 0x3;
2600 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
2601 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
2602 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
2603 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
2604 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
2605 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
2606 (long unsigned int)exit_qualification);
2607 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2608 kvm_run->hw.hardware_exit_reason = 0;
2609 return -ENOTSUPP;
2610 }
2611
2612 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
2613 hva = gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT);
2614 if (!kvm_is_error_hva(hva)) {
2615 r = kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
2616 if (r < 0) {
2617 printk(KERN_ERR "EPT: Not enough memory!\n");
2618 return -ENOMEM;
2619 }
2620 return 1;
2621 } else {
2622 /* must be MMIO */
2623 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
2624
2625 if (er == EMULATE_FAIL) {
2626 printk(KERN_ERR
2627 "EPT: Fail to handle EPT violation vmexit!er is %d\n",
2628 er);
2629 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
2630 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
2631 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
2632 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
2633 (long unsigned int)exit_qualification);
2634 return -ENOTSUPP;
2635 } else if (er == EMULATE_DO_MMIO)
2636 return 0;
2637 }
2638 return 1;
2639}
2640
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641/*
2642 * The exit handlers return 1 if the exit was handled fully and guest execution
2643 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2644 * to be done to userspace and return 0.
2645 */
2646static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
2647 struct kvm_run *kvm_run) = {
2648 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
2649 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08002650 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002651 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652 [EXIT_REASON_CR_ACCESS] = handle_cr,
2653 [EXIT_REASON_DR_ACCESS] = handle_dr,
2654 [EXIT_REASON_CPUID] = handle_cpuid,
2655 [EXIT_REASON_MSR_READ] = handle_rdmsr,
2656 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
2657 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
2658 [EXIT_REASON_HLT] = handle_halt,
Ingo Molnarc21415e2007-02-19 14:37:47 +02002659 [EXIT_REASON_VMCALL] = handle_vmcall,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002660 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
2661 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02002662 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02002663 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Sheng Yang14394422008-04-28 12:24:45 +08002664 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002665};
2666
2667static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04002668 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002669
2670/*
2671 * The guest has exited. See if we can fix it or if we need userspace
2672 * assistance.
2673 */
2674static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2675{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002676 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity29bd8a72007-09-10 17:27:03 +03002677 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1155f762007-11-22 11:30:47 +02002678 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03002679
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002680 KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)vmcs_readl(GUEST_RIP),
2681 (u32)((u64)vmcs_readl(GUEST_RIP) >> 32), entryexit);
2682
Sheng Yang14394422008-04-28 12:24:45 +08002683 /* Access CR3 don't cause VMExit in paging mode, so we need
2684 * to sync with guest real CR3. */
2685 if (vm_need_ept() && is_paging(vcpu)) {
2686 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2687 ept_load_pdptrs(vcpu);
2688 }
2689
Avi Kivity29bd8a72007-09-10 17:27:03 +03002690 if (unlikely(vmx->fail)) {
2691 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2692 kvm_run->fail_entry.hardware_entry_failure_reason
2693 = vmcs_read32(VM_INSTRUCTION_ERROR);
2694 return 0;
2695 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002696
Mike Dayd77c26f2007-10-08 09:02:08 -04002697 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08002698 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
2699 exit_reason != EXIT_REASON_EPT_VIOLATION))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700 printk(KERN_WARNING "%s: unexpected, valid vectoring info and "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002701 "exit reason is 0x%x\n", __func__, exit_reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702 if (exit_reason < kvm_vmx_max_exit_handlers
2703 && kvm_vmx_exit_handlers[exit_reason])
2704 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
2705 else {
2706 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2707 kvm_run->hw.hardware_exit_reason = exit_reason;
2708 }
2709 return 0;
2710}
2711
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002712static void update_tpr_threshold(struct kvm_vcpu *vcpu)
2713{
2714 int max_irr, tpr;
2715
2716 if (!vm_need_tpr_shadow(vcpu->kvm))
2717 return;
2718
2719 if (!kvm_lapic_enabled(vcpu) ||
2720 ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
2721 vmcs_write32(TPR_THRESHOLD, 0);
2722 return;
2723 }
2724
2725 tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
2726 vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
2727}
2728
Eddie Dong85f455f2007-07-06 12:20:49 +03002729static void enable_irq_window(struct kvm_vcpu *vcpu)
2730{
2731 u32 cpu_based_vm_exec_control;
2732
2733 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2734 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2735 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2736}
2737
2738static void vmx_intr_assist(struct kvm_vcpu *vcpu)
2739{
Avi Kivity1155f762007-11-22 11:30:47 +02002740 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002741 u32 idtv_info_field, intr_info_field;
2742 int has_ext_irq, interrupt_window_open;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002743 int vector;
Eddie Dong85f455f2007-07-06 12:20:49 +03002744
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002745 update_tpr_threshold(vcpu);
2746
Eddie Dong85f455f2007-07-06 12:20:49 +03002747 has_ext_irq = kvm_cpu_has_interrupt(vcpu);
2748 intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
Avi Kivity1155f762007-11-22 11:30:47 +02002749 idtv_info_field = vmx->idt_vectoring_info;
Eddie Dong85f455f2007-07-06 12:20:49 +03002750 if (intr_info_field & INTR_INFO_VALID_MASK) {
2751 if (idtv_info_field & INTR_INFO_VALID_MASK) {
2752 /* TODO: fault when IDT_Vectoring */
Ryan Harper9584bf22007-12-13 10:21:10 -06002753 if (printk_ratelimit())
2754 printk(KERN_ERR "Fault when IDT_Vectoring\n");
Eddie Dong85f455f2007-07-06 12:20:49 +03002755 }
2756 if (has_ext_irq)
2757 enable_irq_window(vcpu);
2758 return;
2759 }
2760 if (unlikely(idtv_info_field & INTR_INFO_VALID_MASK)) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002761 if ((idtv_info_field & VECTORING_INFO_TYPE_MASK)
2762 == INTR_TYPE_EXT_INTR
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002763 && vcpu->arch.rmode.active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002764 u8 vect = idtv_info_field & VECTORING_INFO_VECTOR_MASK;
2765
2766 vmx_inject_irq(vcpu, vect);
2767 if (unlikely(has_ext_irq))
2768 enable_irq_window(vcpu);
2769 return;
2770 }
2771
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002772 KVMTRACE_1D(REDELIVER_EVT, vcpu, idtv_info_field, handler);
2773
Eddie Dong85f455f2007-07-06 12:20:49 +03002774 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, idtv_info_field);
2775 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2776 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
2777
Ryan Harper2e113842008-02-11 10:26:38 -06002778 if (unlikely(idtv_info_field & INTR_INFO_DELIVER_CODE_MASK))
Eddie Dong85f455f2007-07-06 12:20:49 +03002779 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2780 vmcs_read32(IDT_VECTORING_ERROR_CODE));
2781 if (unlikely(has_ext_irq))
2782 enable_irq_window(vcpu);
2783 return;
2784 }
2785 if (!has_ext_irq)
2786 return;
2787 interrupt_window_open =
2788 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2789 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002790 if (interrupt_window_open) {
2791 vector = kvm_cpu_get_interrupt(vcpu);
2792 vmx_inject_irq(vcpu, vector);
2793 kvm_timer_intr_post(vcpu, vector);
2794 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03002795 enable_irq_window(vcpu);
2796}
2797
Avi Kivity9c8cba32007-11-22 11:42:59 +02002798/*
2799 * Failure to inject an interrupt should give us the information
2800 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
2801 * when fetching the interrupt redirection bitmap in the real-mode
2802 * tss, this doesn't happen. So we do it ourselves.
2803 */
2804static void fixup_rmode_irq(struct vcpu_vmx *vmx)
2805{
2806 vmx->rmode.irq.pending = 0;
2807 if (vmcs_readl(GUEST_RIP) + 1 != vmx->rmode.irq.rip)
2808 return;
2809 vmcs_writel(GUEST_RIP, vmx->rmode.irq.rip);
2810 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
2811 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
2812 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
2813 return;
2814 }
2815 vmx->idt_vectoring_info =
2816 VECTORING_INFO_VALID_MASK
2817 | INTR_TYPE_EXT_INTR
2818 | vmx->rmode.irq.vector;
2819}
2820
Avi Kivity04d2cc72007-09-10 18:10:54 +03002821static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002822{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002823 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1b6269d2007-10-09 12:12:19 +02002824 u32 intr_info;
Avi Kivitye6adf282007-04-30 16:07:54 +03002825
2826 /*
2827 * Loading guest fpu may have cleared host cr0.ts
2828 */
2829 vmcs_writel(HOST_CR0, read_cr0());
2830
Mike Dayd77c26f2007-10-08 09:02:08 -04002831 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08002832 /* Store host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002833#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02002834 "push %%rdx; push %%rbp;"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835 "push %%rcx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836#else
Laurent Vivierff593e52007-10-25 14:18:55 +02002837 "push %%edx; push %%ebp;"
2838 "push %%ecx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839#endif
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002840 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002841 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02002842 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843 /* Load guest registers. Don't clobber flags. */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002844#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02002845 "mov %c[cr2](%0), %%rax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846 "mov %%rax, %%cr2 \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02002847 "mov %c[rax](%0), %%rax \n\t"
2848 "mov %c[rbx](%0), %%rbx \n\t"
2849 "mov %c[rdx](%0), %%rdx \n\t"
2850 "mov %c[rsi](%0), %%rsi \n\t"
2851 "mov %c[rdi](%0), %%rdi \n\t"
2852 "mov %c[rbp](%0), %%rbp \n\t"
2853 "mov %c[r8](%0), %%r8 \n\t"
2854 "mov %c[r9](%0), %%r9 \n\t"
2855 "mov %c[r10](%0), %%r10 \n\t"
2856 "mov %c[r11](%0), %%r11 \n\t"
2857 "mov %c[r12](%0), %%r12 \n\t"
2858 "mov %c[r13](%0), %%r13 \n\t"
2859 "mov %c[r14](%0), %%r14 \n\t"
2860 "mov %c[r15](%0), %%r15 \n\t"
2861 "mov %c[rcx](%0), %%rcx \n\t" /* kills %0 (rcx) */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862#else
Avi Kivitye08aa782007-11-15 18:06:18 +02002863 "mov %c[cr2](%0), %%eax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864 "mov %%eax, %%cr2 \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02002865 "mov %c[rax](%0), %%eax \n\t"
2866 "mov %c[rbx](%0), %%ebx \n\t"
2867 "mov %c[rdx](%0), %%edx \n\t"
2868 "mov %c[rsi](%0), %%esi \n\t"
2869 "mov %c[rdi](%0), %%edi \n\t"
2870 "mov %c[rbp](%0), %%ebp \n\t"
2871 "mov %c[rcx](%0), %%ecx \n\t" /* kills %0 (ecx) */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002872#endif
2873 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03002874 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002875 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03002876 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002877 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03002878 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08002879 /* Save guest registers, load host registers, keep flags */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002880#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02002881 "xchg %0, (%%rsp) \n\t"
2882 "mov %%rax, %c[rax](%0) \n\t"
2883 "mov %%rbx, %c[rbx](%0) \n\t"
2884 "pushq (%%rsp); popq %c[rcx](%0) \n\t"
2885 "mov %%rdx, %c[rdx](%0) \n\t"
2886 "mov %%rsi, %c[rsi](%0) \n\t"
2887 "mov %%rdi, %c[rdi](%0) \n\t"
2888 "mov %%rbp, %c[rbp](%0) \n\t"
2889 "mov %%r8, %c[r8](%0) \n\t"
2890 "mov %%r9, %c[r9](%0) \n\t"
2891 "mov %%r10, %c[r10](%0) \n\t"
2892 "mov %%r11, %c[r11](%0) \n\t"
2893 "mov %%r12, %c[r12](%0) \n\t"
2894 "mov %%r13, %c[r13](%0) \n\t"
2895 "mov %%r14, %c[r14](%0) \n\t"
2896 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002897 "mov %%cr2, %%rax \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02002898 "mov %%rax, %c[cr2](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899
Avi Kivitye08aa782007-11-15 18:06:18 +02002900 "pop %%rbp; pop %%rbp; pop %%rdx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002901#else
Avi Kivitye08aa782007-11-15 18:06:18 +02002902 "xchg %0, (%%esp) \n\t"
2903 "mov %%eax, %c[rax](%0) \n\t"
2904 "mov %%ebx, %c[rbx](%0) \n\t"
2905 "pushl (%%esp); popl %c[rcx](%0) \n\t"
2906 "mov %%edx, %c[rdx](%0) \n\t"
2907 "mov %%esi, %c[rsi](%0) \n\t"
2908 "mov %%edi, %c[rdi](%0) \n\t"
2909 "mov %%ebp, %c[rbp](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002910 "mov %%cr2, %%eax \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02002911 "mov %%eax, %c[cr2](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002912
Avi Kivitye08aa782007-11-15 18:06:18 +02002913 "pop %%ebp; pop %%ebp; pop %%edx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002914#endif
Avi Kivitye08aa782007-11-15 18:06:18 +02002915 "setbe %c[fail](%0) \n\t"
2916 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
2917 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
2918 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002919 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
2920 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
2921 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
2922 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
2923 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
2924 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
2925 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002926#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002927 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
2928 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
2929 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
2930 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
2931 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
2932 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
2933 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
2934 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002935#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002936 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02002937 : "cc", "memory"
2938#ifdef CONFIG_X86_64
2939 , "rbx", "rdi", "rsi"
2940 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Laurent Vivierff593e52007-10-25 14:18:55 +02002941#else
2942 , "ebx", "edi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02002943#endif
2944 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08002945
Avi Kivity1155f762007-11-22 11:30:47 +02002946 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02002947 if (vmx->rmode.irq.pending)
2948 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02002949
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002950 vcpu->arch.interrupt_window_open =
Mike Dayd77c26f2007-10-08 09:02:08 -04002951 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002952
Mike Dayd77c26f2007-10-08 09:02:08 -04002953 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03002954 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02002955
2956 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2957
2958 /* We need to handle NMIs before interrupts are enabled */
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002959 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) { /* nmi */
2960 KVMTRACE_0D(NMI, vcpu, handler);
Avi Kivity1b6269d2007-10-09 12:12:19 +02002961 asm("int $2");
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002962 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002963}
2964
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
2966{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002967 struct vcpu_vmx *vmx = to_vmx(vcpu);
2968
2969 if (vmx->vmcs) {
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002970 on_each_cpu(__vcpu_clear, vmx, 1);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002971 free_vmcs(vmx->vmcs);
2972 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002973 }
2974}
2975
2976static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
2977{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002978 struct vcpu_vmx *vmx = to_vmx(vcpu);
2979
Sheng Yang2384d2b2008-01-17 15:14:33 +08002980 spin_lock(&vmx_vpid_lock);
2981 if (vmx->vpid != 0)
2982 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
2983 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002984 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002985 kfree(vmx->host_msrs);
2986 kfree(vmx->guest_msrs);
2987 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10002988 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002989}
2990
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002991static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002992{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002993 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10002994 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03002995 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002996
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002997 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002998 return ERR_PTR(-ENOMEM);
2999
Sheng Yang2384d2b2008-01-17 15:14:33 +08003000 allocate_vpid(vmx);
Sheng Yang14394422008-04-28 12:24:45 +08003001 if (id == 0 && vm_need_ept()) {
3002 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
3003 VMX_EPT_WRITABLE_MASK |
3004 VMX_EPT_DEFAULT_MT << VMX_EPT_MT_EPTE_SHIFT);
3005 kvm_mmu_set_mask_ptes(0ull, VMX_EPT_FAKE_ACCESSED_MASK,
3006 VMX_EPT_FAKE_DIRTY_MASK, 0ull,
3007 VMX_EPT_EXECUTABLE_MASK);
3008 kvm_enable_tdp();
3009 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003010
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003011 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3012 if (err)
3013 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003014
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003015 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003016 if (!vmx->guest_msrs) {
3017 err = -ENOMEM;
3018 goto uninit_vcpu;
3019 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003020
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003021 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3022 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003023 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003024
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003025 vmx->vmcs = alloc_vmcs();
3026 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003027 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003028
3029 vmcs_clear(vmx->vmcs);
3030
Avi Kivity15ad7142007-07-11 18:17:21 +03003031 cpu = get_cpu();
3032 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003033 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003034 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003035 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003036 if (err)
3037 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003038 if (vm_need_virtualize_apic_accesses(kvm))
3039 if (alloc_apic_access_page(kvm) != 0)
3040 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003041
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003042 if (vm_need_ept())
3043 if (alloc_identity_pagetable(kvm) != 0)
3044 goto free_vmcs;
3045
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003046 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003047
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003048free_vmcs:
3049 free_vmcs(vmx->vmcs);
3050free_msrs:
3051 kfree(vmx->host_msrs);
3052free_guest_msrs:
3053 kfree(vmx->guest_msrs);
3054uninit_vcpu:
3055 kvm_vcpu_uninit(&vmx->vcpu);
3056free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003057 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003058 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003059}
3060
Yang, Sheng002c7f72007-07-31 14:23:01 +03003061static void __init vmx_check_processor_compat(void *rtn)
3062{
3063 struct vmcs_config vmcs_conf;
3064
3065 *(int *)rtn = 0;
3066 if (setup_vmcs_config(&vmcs_conf) < 0)
3067 *(int *)rtn = -EIO;
3068 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3069 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3070 smp_processor_id());
3071 *(int *)rtn = -EIO;
3072 }
3073}
3074
Sheng Yang67253af2008-04-25 10:20:22 +08003075static int get_ept_level(void)
3076{
3077 return VMX_EPT_DEFAULT_GAW + 1;
3078}
3079
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003080static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003081 .cpu_has_kvm_support = cpu_has_kvm_support,
3082 .disabled_by_bios = vmx_disabled_by_bios,
3083 .hardware_setup = hardware_setup,
3084 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003085 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003086 .hardware_enable = hardware_enable,
3087 .hardware_disable = hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003088 .cpu_has_accelerated_tpr = cpu_has_vmx_virtualize_apic_accesses,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089
3090 .vcpu_create = vmx_create_vcpu,
3091 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003092 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003093
Avi Kivity04d2cc72007-09-10 18:10:54 +03003094 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003095 .vcpu_load = vmx_vcpu_load,
3096 .vcpu_put = vmx_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08003097 .vcpu_decache = vmx_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003098
3099 .set_guest_debug = set_guest_debug,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003100 .guest_debug_pre = kvm_guest_debug_pre,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003101 .get_msr = vmx_get_msr,
3102 .set_msr = vmx_set_msr,
3103 .get_segment_base = vmx_get_segment_base,
3104 .get_segment = vmx_get_segment,
3105 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003106 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003107 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003108 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003109 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003110 .set_cr3 = vmx_set_cr3,
3111 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003112 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003113 .get_idt = vmx_get_idt,
3114 .set_idt = vmx_set_idt,
3115 .get_gdt = vmx_get_gdt,
3116 .set_gdt = vmx_set_gdt,
3117 .cache_regs = vcpu_load_rsp_rip,
3118 .decache_regs = vcpu_put_rsp_rip,
3119 .get_rflags = vmx_get_rflags,
3120 .set_rflags = vmx_set_rflags,
3121
3122 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003123
Avi Kivity6aa8b732006-12-10 02:21:36 -08003124 .run = vmx_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003125 .handle_exit = kvm_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003126 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02003127 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003128 .get_irq = vmx_get_irq,
3129 .set_irq = vmx_inject_irq,
Avi Kivity298101d2007-11-25 13:41:11 +02003130 .queue_exception = vmx_queue_exception,
3131 .exception_injected = vmx_exception_injected,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003132 .inject_pending_irq = vmx_intr_assist,
3133 .inject_pending_vectors = do_interrupt_requests,
Izik Eiduscbc94022007-10-25 00:29:55 +02003134
3135 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003136 .get_tdp_level = get_ept_level,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003137};
3138
3139static int __init vmx_init(void)
3140{
Sheng Yang25c5f222008-03-28 13:18:56 +08003141 void *va;
He, Qingfdef3ad2007-04-30 09:45:24 +03003142 int r;
3143
3144 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3145 if (!vmx_io_bitmap_a)
3146 return -ENOMEM;
3147
3148 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3149 if (!vmx_io_bitmap_b) {
3150 r = -ENOMEM;
3151 goto out;
3152 }
3153
Sheng Yang25c5f222008-03-28 13:18:56 +08003154 vmx_msr_bitmap = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3155 if (!vmx_msr_bitmap) {
3156 r = -ENOMEM;
3157 goto out1;
3158 }
3159
He, Qingfdef3ad2007-04-30 09:45:24 +03003160 /*
3161 * Allow direct access to the PC debug port (it is often used for I/O
3162 * delays, but the vmexits simply slow things down).
3163 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003164 va = kmap(vmx_io_bitmap_a);
3165 memset(va, 0xff, PAGE_SIZE);
3166 clear_bit(0x80, va);
Avi Kivitycd0536d2007-05-08 11:34:07 +03003167 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003168
Sheng Yang25c5f222008-03-28 13:18:56 +08003169 va = kmap(vmx_io_bitmap_b);
3170 memset(va, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03003171 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03003172
Sheng Yang25c5f222008-03-28 13:18:56 +08003173 va = kmap(vmx_msr_bitmap);
3174 memset(va, 0xff, PAGE_SIZE);
3175 kunmap(vmx_msr_bitmap);
3176
Sheng Yang2384d2b2008-01-17 15:14:33 +08003177 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
3178
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003179 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003180 if (r)
Sheng Yang25c5f222008-03-28 13:18:56 +08003181 goto out2;
3182
3183 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_FS_BASE);
3184 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_GS_BASE);
3185 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_CS);
3186 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_ESP);
3187 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_EIP);
He, Qingfdef3ad2007-04-30 09:45:24 +03003188
Sheng Yang14394422008-04-28 12:24:45 +08003189 if (cpu_has_vmx_ept())
3190 bypass_guest_pf = 0;
3191
Avi Kivityc7addb92007-09-16 18:58:32 +02003192 if (bypass_guest_pf)
3193 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
3194
Sheng Yang14394422008-04-28 12:24:45 +08003195 ept_sync_global();
3196
He, Qingfdef3ad2007-04-30 09:45:24 +03003197 return 0;
3198
Sheng Yang25c5f222008-03-28 13:18:56 +08003199out2:
3200 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03003201out1:
3202 __free_page(vmx_io_bitmap_b);
3203out:
3204 __free_page(vmx_io_bitmap_a);
3205 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206}
3207
3208static void __exit vmx_exit(void)
3209{
Sheng Yang25c5f222008-03-28 13:18:56 +08003210 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03003211 __free_page(vmx_io_bitmap_b);
3212 __free_page(vmx_io_bitmap_a);
3213
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003214 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003215}
3216
3217module_init(vmx_init)
3218module_exit(vmx_exit)