blob: 02efbe75f317d68b8f24a6bd7945eaaeffcbfe89 [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 Kivity6aa8b732006-12-10 02:21:36 -080033MODULE_AUTHOR("Qumranet");
34MODULE_LICENSE("GPL");
35
Avi Kivityc7addb92007-09-16 18:58:32 +020036static int bypass_guest_pf = 1;
37module_param(bypass_guest_pf, bool, 0);
38
Sheng Yang2384d2b2008-01-17 15:14:33 +080039static int enable_vpid = 1;
40module_param(enable_vpid, bool, 0);
41
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020042static int flexpriority_enabled = 1;
43module_param(flexpriority_enabled, bool, 0);
44
Sheng Yang14394422008-04-28 12:24:45 +080045static int enable_ept = 1;
Sheng Yangd56f5462008-04-25 10:13:16 +080046module_param(enable_ept, bool, 0);
47
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040048struct vmcs {
49 u32 revision_id;
50 u32 abort;
51 char data[0];
52};
53
54struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100055 struct kvm_vcpu vcpu;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040056 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030057 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020058 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040059 struct kvm_msr_entry *guest_msrs;
60 struct kvm_msr_entry *host_msrs;
61 int nmsrs;
62 int save_nmsrs;
63 int msr_offset_efer;
64#ifdef CONFIG_X86_64
65 int msr_offset_kernel_gs_base;
66#endif
67 struct vmcs *vmcs;
68 struct {
69 int loaded;
70 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020071 int gs_ldt_reload_needed;
72 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030073 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040074 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +020075 struct {
76 struct {
77 bool pending;
78 u8 vector;
79 unsigned rip;
80 } irq;
81 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +080082 int vpid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040083};
84
85static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
86{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100087 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040088}
89
Sheng Yangb7ebfb02008-04-25 21:44:52 +080090static int init_rmode(struct kvm *kvm);
Avi Kivity75880a02007-06-20 11:20:04 +030091
Avi Kivity6aa8b732006-12-10 02:21:36 -080092static DEFINE_PER_CPU(struct vmcs *, vmxarea);
93static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
94
He, Qingfdef3ad2007-04-30 09:45:24 +030095static struct page *vmx_io_bitmap_a;
96static struct page *vmx_io_bitmap_b;
Sheng Yang25c5f222008-03-28 13:18:56 +080097static struct page *vmx_msr_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +030098
Sheng Yang2384d2b2008-01-17 15:14:33 +080099static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
100static DEFINE_SPINLOCK(vmx_vpid_lock);
101
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300102static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103 int size;
104 int order;
105 u32 revision_id;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300106 u32 pin_based_exec_ctrl;
107 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800108 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300109 u32 vmexit_ctrl;
110 u32 vmentry_ctrl;
111} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112
Sheng Yangd56f5462008-04-25 10:13:16 +0800113struct vmx_capability {
114 u32 ept;
115 u32 vpid;
116} vmx_capability;
117
Avi Kivity6aa8b732006-12-10 02:21:36 -0800118#define VMX_SEGMENT_FIELD(seg) \
119 [VCPU_SREG_##seg] = { \
120 .selector = GUEST_##seg##_SELECTOR, \
121 .base = GUEST_##seg##_BASE, \
122 .limit = GUEST_##seg##_LIMIT, \
123 .ar_bytes = GUEST_##seg##_AR_BYTES, \
124 }
125
126static struct kvm_vmx_segment_field {
127 unsigned selector;
128 unsigned base;
129 unsigned limit;
130 unsigned ar_bytes;
131} kvm_vmx_segment_fields[] = {
132 VMX_SEGMENT_FIELD(CS),
133 VMX_SEGMENT_FIELD(DS),
134 VMX_SEGMENT_FIELD(ES),
135 VMX_SEGMENT_FIELD(FS),
136 VMX_SEGMENT_FIELD(GS),
137 VMX_SEGMENT_FIELD(SS),
138 VMX_SEGMENT_FIELD(TR),
139 VMX_SEGMENT_FIELD(LDTR),
140};
141
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300142/*
143 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
144 * away by decrementing the array size.
145 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800147#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800148 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
149#endif
150 MSR_EFER, MSR_K6_STAR,
151};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200152#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800153
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400154static void load_msrs(struct kvm_msr_entry *e, int n)
155{
156 int i;
157
158 for (i = 0; i < n; ++i)
159 wrmsrl(e[i].index, e[i].data);
160}
161
162static void save_msrs(struct kvm_msr_entry *e, int n)
163{
164 int i;
165
166 for (i = 0; i < n; ++i)
167 rdmsrl(e[i].index, e[i].data);
168}
169
Avi Kivity6aa8b732006-12-10 02:21:36 -0800170static inline int is_page_fault(u32 intr_info)
171{
172 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
173 INTR_INFO_VALID_MASK)) ==
174 (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
175}
176
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300177static inline int is_no_device(u32 intr_info)
178{
179 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
180 INTR_INFO_VALID_MASK)) ==
181 (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
182}
183
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500184static inline int is_invalid_opcode(u32 intr_info)
185{
186 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
187 INTR_INFO_VALID_MASK)) ==
188 (INTR_TYPE_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
189}
190
Avi Kivity6aa8b732006-12-10 02:21:36 -0800191static inline int is_external_interrupt(u32 intr_info)
192{
193 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
194 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
195}
196
Sheng Yang25c5f222008-03-28 13:18:56 +0800197static inline int cpu_has_vmx_msr_bitmap(void)
198{
199 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS);
200}
201
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800202static inline int cpu_has_vmx_tpr_shadow(void)
203{
204 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW);
205}
206
207static inline int vm_need_tpr_shadow(struct kvm *kvm)
208{
209 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)));
210}
211
Sheng Yangf78e0e22007-10-29 09:40:42 +0800212static inline int cpu_has_secondary_exec_ctrls(void)
213{
214 return (vmcs_config.cpu_based_exec_ctrl &
215 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS);
216}
217
Avi Kivity774ead32007-12-26 13:57:04 +0200218static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800219{
Avi Kivity4c9fc8e2008-03-24 18:15:14 +0200220 return flexpriority_enabled
221 && (vmcs_config.cpu_based_2nd_exec_ctrl &
222 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800223}
224
Sheng Yangd56f5462008-04-25 10:13:16 +0800225static inline int cpu_has_vmx_invept_individual_addr(void)
226{
227 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT));
228}
229
230static inline int cpu_has_vmx_invept_context(void)
231{
232 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT));
233}
234
235static inline int cpu_has_vmx_invept_global(void)
236{
237 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT));
238}
239
240static inline int cpu_has_vmx_ept(void)
241{
242 return (vmcs_config.cpu_based_2nd_exec_ctrl &
243 SECONDARY_EXEC_ENABLE_EPT);
244}
245
246static inline int vm_need_ept(void)
247{
248 return (cpu_has_vmx_ept() && enable_ept);
249}
250
Sheng Yangf78e0e22007-10-29 09:40:42 +0800251static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
252{
253 return ((cpu_has_vmx_virtualize_apic_accesses()) &&
254 (irqchip_in_kernel(kvm)));
255}
256
Sheng Yang2384d2b2008-01-17 15:14:33 +0800257static inline int cpu_has_vmx_vpid(void)
258{
259 return (vmcs_config.cpu_based_2nd_exec_ctrl &
260 SECONDARY_EXEC_ENABLE_VPID);
261}
262
Rusty Russell8b9cf982007-07-30 16:31:43 +1000263static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800264{
265 int i;
266
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400267 for (i = 0; i < vmx->nmsrs; ++i)
268 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300269 return i;
270 return -1;
271}
272
Sheng Yang2384d2b2008-01-17 15:14:33 +0800273static inline void __invvpid(int ext, u16 vpid, gva_t gva)
274{
275 struct {
276 u64 vpid : 16;
277 u64 rsvd : 48;
278 u64 gva;
279 } operand = { vpid, 0, gva };
280
281 asm volatile (ASM_VMX_INVVPID
282 /* CF==1 or ZF==1 --> rc = -1 */
283 "; ja 1f ; ud2 ; 1:"
284 : : "a"(&operand), "c"(ext) : "cc", "memory");
285}
286
Sheng Yang14394422008-04-28 12:24:45 +0800287static inline void __invept(int ext, u64 eptp, gpa_t gpa)
288{
289 struct {
290 u64 eptp, gpa;
291 } operand = {eptp, gpa};
292
293 asm volatile (ASM_VMX_INVEPT
294 /* CF==1 or ZF==1 --> rc = -1 */
295 "; ja 1f ; ud2 ; 1:\n"
296 : : "a" (&operand), "c" (ext) : "cc", "memory");
297}
298
Rusty Russell8b9cf982007-07-30 16:31:43 +1000299static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300300{
301 int i;
302
Rusty Russell8b9cf982007-07-30 16:31:43 +1000303 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300304 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400305 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000306 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800307}
308
Avi Kivity6aa8b732006-12-10 02:21:36 -0800309static void vmcs_clear(struct vmcs *vmcs)
310{
311 u64 phys_addr = __pa(vmcs);
312 u8 error;
313
314 asm volatile (ASM_VMX_VMCLEAR_RAX "; setna %0"
315 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
316 : "cc", "memory");
317 if (error)
318 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
319 vmcs, phys_addr);
320}
321
322static void __vcpu_clear(void *arg)
323{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000324 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800325 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800326
Rusty Russell8b9cf982007-07-30 16:31:43 +1000327 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400328 vmcs_clear(vmx->vmcs);
329 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800330 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800331 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800332}
333
Rusty Russell8b9cf982007-07-30 16:31:43 +1000334static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800335{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200336 if (vmx->vcpu.cpu == -1)
337 return;
Avi Kivityf566e092007-09-30 11:02:53 +0200338 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 0, 1);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000339 vmx->launched = 0;
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800340}
341
Sheng Yang2384d2b2008-01-17 15:14:33 +0800342static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
343{
344 if (vmx->vpid == 0)
345 return;
346
347 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
348}
349
Sheng Yang14394422008-04-28 12:24:45 +0800350static inline void ept_sync_global(void)
351{
352 if (cpu_has_vmx_invept_global())
353 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
354}
355
356static inline void ept_sync_context(u64 eptp)
357{
358 if (vm_need_ept()) {
359 if (cpu_has_vmx_invept_context())
360 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
361 else
362 ept_sync_global();
363 }
364}
365
366static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
367{
368 if (vm_need_ept()) {
369 if (cpu_has_vmx_invept_individual_addr())
370 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
371 eptp, gpa);
372 else
373 ept_sync_context(eptp);
374 }
375}
376
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377static unsigned long vmcs_readl(unsigned long field)
378{
379 unsigned long value;
380
381 asm volatile (ASM_VMX_VMREAD_RDX_RAX
382 : "=a"(value) : "d"(field) : "cc");
383 return value;
384}
385
386static u16 vmcs_read16(unsigned long field)
387{
388 return vmcs_readl(field);
389}
390
391static u32 vmcs_read32(unsigned long field)
392{
393 return vmcs_readl(field);
394}
395
396static u64 vmcs_read64(unsigned long field)
397{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800398#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800399 return vmcs_readl(field);
400#else
401 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
402#endif
403}
404
Avi Kivitye52de1b2007-01-05 16:36:56 -0800405static noinline void vmwrite_error(unsigned long field, unsigned long value)
406{
407 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
408 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
409 dump_stack();
410}
411
Avi Kivity6aa8b732006-12-10 02:21:36 -0800412static void vmcs_writel(unsigned long field, unsigned long value)
413{
414 u8 error;
415
416 asm volatile (ASM_VMX_VMWRITE_RAX_RDX "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400417 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800418 if (unlikely(error))
419 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420}
421
422static void vmcs_write16(unsigned long field, u16 value)
423{
424 vmcs_writel(field, value);
425}
426
427static void vmcs_write32(unsigned long field, u32 value)
428{
429 vmcs_writel(field, value);
430}
431
432static void vmcs_write64(unsigned long field, u64 value)
433{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800434#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435 vmcs_writel(field, value);
436#else
437 vmcs_writel(field, value);
438 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
Rusty Russell8b9cf982007-07-30 16:31:43 +1000569static 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 Kivity6aa8b732006-12-10 02:21:36 -0800599/*
600 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
601 * vcpu mutex is already taken.
602 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300603static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800604{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400605 struct vcpu_vmx *vmx = to_vmx(vcpu);
606 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200607 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800608
Eddie Donga3d7f852007-09-03 16:15:12 +0300609 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000610 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300611 kvm_migrate_timers(vcpu);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800612 vpid_sync_vcpu_all(vmx);
Eddie Donga3d7f852007-09-03 16:15:12 +0300613 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800614
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400615 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800616 u8 error;
617
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400618 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800619 asm volatile (ASM_VMX_VMPTRLD_RAX "; setna %0"
620 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
621 : "cc");
622 if (error)
623 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400624 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625 }
626
627 if (vcpu->cpu != cpu) {
628 struct descriptor_table dt;
629 unsigned long sysenter_esp;
630
631 vcpu->cpu = cpu;
632 /*
633 * Linux uses per-cpu TSS and GDT, so set these when switching
634 * processors.
635 */
636 vmcs_writel(HOST_TR_BASE, read_tr_base()); /* 22.2.4 */
637 get_gdt(&dt);
638 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
639
640 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
641 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300642
643 /*
644 * Make sure the time stamp counter is monotonous.
645 */
646 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200647 if (tsc_this < vcpu->arch.host_tsc) {
648 delta = vcpu->arch.host_tsc - tsc_this;
649 new_offset = vmcs_read64(TSC_OFFSET) + delta;
650 vmcs_write64(TSC_OFFSET, new_offset);
651 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800652 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653}
654
655static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
656{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000657 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658}
659
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300660static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
661{
662 if (vcpu->fpu_active)
663 return;
664 vcpu->fpu_active = 1;
Rusty Russell707d92f2007-07-17 23:19:08 +1000665 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800666 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92f2007-07-17 23:19:08 +1000667 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300668 update_exception_bitmap(vcpu);
669}
670
671static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
672{
673 if (!vcpu->fpu_active)
674 return;
675 vcpu->fpu_active = 0;
Rusty Russell707d92f2007-07-17 23:19:08 +1000676 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300677 update_exception_bitmap(vcpu);
678}
679
Avi Kivity774c47f2007-02-12 00:54:47 -0800680static void vmx_vcpu_decache(struct kvm_vcpu *vcpu)
681{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000682 vcpu_clear(to_vmx(vcpu));
Avi Kivity774c47f2007-02-12 00:54:47 -0800683}
684
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
686{
687 return vmcs_readl(GUEST_RFLAGS);
688}
689
690static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
691{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800692 if (vcpu->arch.rmode.active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100693 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800694 vmcs_writel(GUEST_RFLAGS, rflags);
695}
696
697static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
698{
699 unsigned long rip;
700 u32 interruptibility;
701
702 rip = vmcs_readl(GUEST_RIP);
703 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
704 vmcs_writel(GUEST_RIP, rip);
705
706 /*
707 * We emulated an instruction, so temporary interrupt blocking
708 * should be removed, if set.
709 */
710 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
711 if (interruptibility & 3)
712 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
713 interruptibility & ~3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800714 vcpu->arch.interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715}
716
Avi Kivity298101d2007-11-25 13:41:11 +0200717static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
718 bool has_error_code, u32 error_code)
719{
720 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
721 nr | INTR_TYPE_EXCEPTION
Ryan Harper2e113842008-02-11 10:26:38 -0600722 | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0)
Avi Kivity298101d2007-11-25 13:41:11 +0200723 | INTR_INFO_VALID_MASK);
724 if (has_error_code)
725 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
726}
727
728static bool vmx_exception_injected(struct kvm_vcpu *vcpu)
729{
730 struct vcpu_vmx *vmx = to_vmx(vcpu);
731
732 return !(vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
733}
734
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735/*
Eddie Donga75beee2007-05-17 18:55:15 +0300736 * Swap MSR entry in host/guest MSR entry array.
737 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200738#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000739static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300740{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400741 struct kvm_msr_entry tmp;
742
743 tmp = vmx->guest_msrs[to];
744 vmx->guest_msrs[to] = vmx->guest_msrs[from];
745 vmx->guest_msrs[from] = tmp;
746 tmp = vmx->host_msrs[to];
747 vmx->host_msrs[to] = vmx->host_msrs[from];
748 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300749}
Gabriel C54e11fa2007-08-01 16:23:10 +0200750#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300751
752/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300753 * Set up the vmcs to automatically save and restore system
754 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
755 * mode, as fiddling with msrs is very expensive.
756 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000757static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300758{
Eddie Dong2cc51562007-05-21 07:28:09 +0300759 int save_nmsrs;
Avi Kivitye38aea32007-04-19 13:22:48 +0300760
Avi Kivity33f9c502008-02-27 16:06:57 +0200761 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300762 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300763#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000764 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300765 int index;
766
Rusty Russell8b9cf982007-07-30 16:31:43 +1000767 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300768 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000769 move_msr_up(vmx, index, save_nmsrs++);
770 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300771 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000772 move_msr_up(vmx, index, save_nmsrs++);
773 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300774 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000775 move_msr_up(vmx, index, save_nmsrs++);
776 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300777 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000778 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300779 /*
780 * MSR_K6_STAR is only needed on long mode guests, and only
781 * if efer.sce is enabled.
782 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000783 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800784 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000785 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300786 }
Eddie Donga75beee2007-05-17 18:55:15 +0300787#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400788 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300789
Eddie Donga75beee2007-05-17 18:55:15 +0300790#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400791 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000792 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300793#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000794 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivitye38aea32007-04-19 13:22:48 +0300795}
796
797/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798 * reads and returns guest's timestamp counter "register"
799 * guest_tsc = host_tsc + tsc_offset -- 21.3
800 */
801static u64 guest_read_tsc(void)
802{
803 u64 host_tsc, tsc_offset;
804
805 rdtscll(host_tsc);
806 tsc_offset = vmcs_read64(TSC_OFFSET);
807 return host_tsc + tsc_offset;
808}
809
810/*
811 * writes 'guest_tsc' into guest's timestamp counter "register"
812 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
813 */
814static void guest_write_tsc(u64 guest_tsc)
815{
816 u64 host_tsc;
817
818 rdtscll(host_tsc);
819 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
820}
821
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822/*
823 * Reads an msr value (of 'msr_index') into 'pdata'.
824 * Returns 0 on success, non-0 otherwise.
825 * Assumes vcpu_load() was already called.
826 */
827static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
828{
829 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400830 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800831
832 if (!pdata) {
833 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
834 return -EINVAL;
835 }
836
837 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800838#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839 case MSR_FS_BASE:
840 data = vmcs_readl(GUEST_FS_BASE);
841 break;
842 case MSR_GS_BASE:
843 data = vmcs_readl(GUEST_GS_BASE);
844 break;
845 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800846 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847#endif
848 case MSR_IA32_TIME_STAMP_COUNTER:
849 data = guest_read_tsc();
850 break;
851 case MSR_IA32_SYSENTER_CS:
852 data = vmcs_read32(GUEST_SYSENTER_CS);
853 break;
854 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200855 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800856 break;
857 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200858 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000861 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800862 if (msr) {
863 data = msr->data;
864 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800866 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867 }
868
869 *pdata = data;
870 return 0;
871}
872
873/*
874 * Writes msr value into into the appropriate "register".
875 * Returns 0 on success, non-0 otherwise.
876 * Assumes vcpu_load() was already called.
877 */
878static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
879{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400880 struct vcpu_vmx *vmx = to_vmx(vcpu);
881 struct kvm_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +0300882 int ret = 0;
883
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800885#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800886 case MSR_EFER:
Eddie Dong2cc51562007-05-21 07:28:09 +0300887 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300888 if (vmx->host_state.loaded) {
889 reload_host_efer(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000890 load_transition_efer(vmx);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300891 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300892 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893 case MSR_FS_BASE:
894 vmcs_writel(GUEST_FS_BASE, data);
895 break;
896 case MSR_GS_BASE:
897 vmcs_writel(GUEST_GS_BASE, data);
898 break;
899#endif
900 case MSR_IA32_SYSENTER_CS:
901 vmcs_write32(GUEST_SYSENTER_CS, data);
902 break;
903 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200904 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 break;
906 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200907 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200909 case MSR_IA32_TIME_STAMP_COUNTER:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 guest_write_tsc(data);
911 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000913 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800914 if (msr) {
915 msr->data = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400916 if (vmx->host_state.loaded)
917 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800918 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300920 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921 }
922
Eddie Dong2cc51562007-05-21 07:28:09 +0300923 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924}
925
926/*
927 * Sync the rsp and rip registers into the vcpu structure. This allows
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800928 * registers to be accessed by indexing vcpu->arch.regs.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929 */
930static void vcpu_load_rsp_rip(struct kvm_vcpu *vcpu)
931{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800932 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
933 vcpu->arch.rip = vmcs_readl(GUEST_RIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934}
935
936/*
937 * Syncs rsp and rip back into the vmcs. Should be called after possible
938 * modification.
939 */
940static void vcpu_put_rsp_rip(struct kvm_vcpu *vcpu)
941{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800942 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
943 vmcs_writel(GUEST_RIP, vcpu->arch.rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944}
945
946static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
947{
948 unsigned long dr7 = 0x400;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949 int old_singlestep;
950
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951 old_singlestep = vcpu->guest_debug.singlestep;
952
953 vcpu->guest_debug.enabled = dbg->enabled;
954 if (vcpu->guest_debug.enabled) {
955 int i;
956
957 dr7 |= 0x200; /* exact */
958 for (i = 0; i < 4; ++i) {
959 if (!dbg->breakpoints[i].enabled)
960 continue;
961 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
962 dr7 |= 2 << (i*2); /* global enable */
963 dr7 |= 0 << (i*4+16); /* execution breakpoint */
964 }
965
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966 vcpu->guest_debug.singlestep = dbg->singlestep;
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300967 } else
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 vcpu->guest_debug.singlestep = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969
970 if (old_singlestep && !vcpu->guest_debug.singlestep) {
971 unsigned long flags;
972
973 flags = vmcs_readl(GUEST_RFLAGS);
974 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
975 vmcs_writel(GUEST_RFLAGS, flags);
976 }
977
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300978 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 vmcs_writel(GUEST_DR7, dr7);
980
981 return 0;
982}
983
Eddie Dong2a8067f2007-08-06 16:29:07 +0300984static int vmx_get_irq(struct kvm_vcpu *vcpu)
985{
Avi Kivity1155f762007-11-22 11:30:47 +0200986 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +0300987 u32 idtv_info_field;
988
Avi Kivity1155f762007-11-22 11:30:47 +0200989 idtv_info_field = vmx->idt_vectoring_info;
Eddie Dong2a8067f2007-08-06 16:29:07 +0300990 if (idtv_info_field & INTR_INFO_VALID_MASK) {
991 if (is_external_interrupt(idtv_info_field))
992 return idtv_info_field & VECTORING_INFO_VECTOR_MASK;
993 else
Mike Dayd77c26f2007-10-08 09:02:08 -0400994 printk(KERN_DEBUG "pending exception: not handled yet\n");
Eddie Dong2a8067f2007-08-06 16:29:07 +0300995 }
996 return -1;
997}
998
Avi Kivity6aa8b732006-12-10 02:21:36 -0800999static __init int cpu_has_kvm_support(void)
1000{
1001 unsigned long ecx = cpuid_ecx(1);
1002 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
1003}
1004
1005static __init int vmx_disabled_by_bios(void)
1006{
1007 u64 msr;
1008
1009 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001010 return (msr & (MSR_IA32_FEATURE_CONTROL_LOCKED |
1011 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
1012 == MSR_IA32_FEATURE_CONTROL_LOCKED;
1013 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014}
1015
Avi Kivity774c47f2007-02-12 00:54:47 -08001016static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017{
1018 int cpu = raw_smp_processor_id();
1019 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1020 u64 old;
1021
1022 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001023 if ((old & (MSR_IA32_FEATURE_CONTROL_LOCKED |
1024 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
1025 != (MSR_IA32_FEATURE_CONTROL_LOCKED |
1026 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001028 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
1029 MSR_IA32_FEATURE_CONTROL_LOCKED |
1030 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001031 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032 asm volatile (ASM_VMX_VMXON_RAX : : "a"(&phys_addr), "m"(phys_addr)
1033 : "memory", "cc");
1034}
1035
1036static void hardware_disable(void *garbage)
1037{
1038 asm volatile (ASM_VMX_VMXOFF : : : "cc");
Eli Collinse693d712008-06-01 20:24:40 -07001039 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040}
1041
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001042static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001043 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044{
1045 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001046 u32 ctl = ctl_min | ctl_opt;
1047
1048 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1049
1050 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1051 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1052
1053 /* Ensure minimum (required) set of control bits are supported. */
1054 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001055 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001056
1057 *result = ctl;
1058 return 0;
1059}
1060
Yang, Sheng002c7f72007-07-31 14:23:01 +03001061static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001062{
1063 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001064 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001065 u32 _pin_based_exec_control = 0;
1066 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001067 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001068 u32 _vmexit_control = 0;
1069 u32 _vmentry_control = 0;
1070
1071 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
1072 opt = 0;
1073 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1074 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001075 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001076
1077 min = CPU_BASED_HLT_EXITING |
1078#ifdef CONFIG_X86_64
1079 CPU_BASED_CR8_LOAD_EXITING |
1080 CPU_BASED_CR8_STORE_EXITING |
1081#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001082 CPU_BASED_CR3_LOAD_EXITING |
1083 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001084 CPU_BASED_USE_IO_BITMAPS |
1085 CPU_BASED_MOV_DR_EXITING |
1086 CPU_BASED_USE_TSC_OFFSETING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001087 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001088 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001089 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001090 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1091 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001092 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001093#ifdef CONFIG_X86_64
1094 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1095 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1096 ~CPU_BASED_CR8_STORE_EXITING;
1097#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001098 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001099 min2 = 0;
1100 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001101 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001102 SECONDARY_EXEC_ENABLE_VPID |
1103 SECONDARY_EXEC_ENABLE_EPT;
1104 if (adjust_vmx_controls(min2, opt2,
1105 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001106 &_cpu_based_2nd_exec_control) < 0)
1107 return -EIO;
1108 }
1109#ifndef CONFIG_X86_64
1110 if (!(_cpu_based_2nd_exec_control &
1111 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1112 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1113#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001114 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
1115 /* CR3 accesses don't need to cause VM Exits when EPT enabled */
1116 min &= ~(CPU_BASED_CR3_LOAD_EXITING |
1117 CPU_BASED_CR3_STORE_EXITING);
1118 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1119 &_cpu_based_exec_control) < 0)
1120 return -EIO;
1121 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1122 vmx_capability.ept, vmx_capability.vpid);
1123 }
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001124
1125 min = 0;
1126#ifdef CONFIG_X86_64
1127 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1128#endif
1129 opt = 0;
1130 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1131 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001132 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001133
1134 min = opt = 0;
1135 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1136 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001137 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001139 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001140
1141 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1142 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001143 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001144
1145#ifdef CONFIG_X86_64
1146 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1147 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001148 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001149#endif
1150
1151 /* Require Write-Back (WB) memory type for VMCS accesses. */
1152 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001153 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001154
Yang, Sheng002c7f72007-07-31 14:23:01 +03001155 vmcs_conf->size = vmx_msr_high & 0x1fff;
1156 vmcs_conf->order = get_order(vmcs_config.size);
1157 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001158
Yang, Sheng002c7f72007-07-31 14:23:01 +03001159 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1160 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001161 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001162 vmcs_conf->vmexit_ctrl = _vmexit_control;
1163 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001164
1165 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001166}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167
1168static struct vmcs *alloc_vmcs_cpu(int cpu)
1169{
1170 int node = cpu_to_node(cpu);
1171 struct page *pages;
1172 struct vmcs *vmcs;
1173
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001174 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001175 if (!pages)
1176 return NULL;
1177 vmcs = page_address(pages);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001178 memset(vmcs, 0, vmcs_config.size);
1179 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180 return vmcs;
1181}
1182
1183static struct vmcs *alloc_vmcs(void)
1184{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001185 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186}
1187
1188static void free_vmcs(struct vmcs *vmcs)
1189{
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001190 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191}
1192
Sam Ravnborg39959582007-06-01 00:47:13 -07001193static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001194{
1195 int cpu;
1196
1197 for_each_online_cpu(cpu)
1198 free_vmcs(per_cpu(vmxarea, cpu));
1199}
1200
Avi Kivity6aa8b732006-12-10 02:21:36 -08001201static __init int alloc_kvm_area(void)
1202{
1203 int cpu;
1204
1205 for_each_online_cpu(cpu) {
1206 struct vmcs *vmcs;
1207
1208 vmcs = alloc_vmcs_cpu(cpu);
1209 if (!vmcs) {
1210 free_kvm_area();
1211 return -ENOMEM;
1212 }
1213
1214 per_cpu(vmxarea, cpu) = vmcs;
1215 }
1216 return 0;
1217}
1218
1219static __init int hardware_setup(void)
1220{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001221 if (setup_vmcs_config(&vmcs_config) < 0)
1222 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001223
1224 if (boot_cpu_has(X86_FEATURE_NX))
1225 kvm_enable_efer_bits(EFER_NX);
1226
Avi Kivity6aa8b732006-12-10 02:21:36 -08001227 return alloc_kvm_area();
1228}
1229
1230static __exit void hardware_unsetup(void)
1231{
1232 free_kvm_area();
1233}
1234
Avi Kivity6aa8b732006-12-10 02:21:36 -08001235static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1236{
1237 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1238
Avi Kivity6af11b92007-03-19 13:18:10 +02001239 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001240 vmcs_write16(sf->selector, save->selector);
1241 vmcs_writel(sf->base, save->base);
1242 vmcs_write32(sf->limit, save->limit);
1243 vmcs_write32(sf->ar_bytes, save->ar);
1244 } else {
1245 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1246 << AR_DPL_SHIFT;
1247 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1248 }
1249}
1250
1251static void enter_pmode(struct kvm_vcpu *vcpu)
1252{
1253 unsigned long flags;
1254
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001255 vcpu->arch.rmode.active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001256
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001257 vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base);
1258 vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit);
1259 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260
1261 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001262 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001263 flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264 vmcs_writel(GUEST_RFLAGS, flags);
1265
Rusty Russell66aee912007-07-17 23:34:16 +10001266 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1267 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268
1269 update_exception_bitmap(vcpu);
1270
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001271 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1272 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1273 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1274 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001275
1276 vmcs_write16(GUEST_SS_SELECTOR, 0);
1277 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1278
1279 vmcs_write16(GUEST_CS_SELECTOR,
1280 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1281 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1282}
1283
Mike Dayd77c26f2007-10-08 09:02:08 -04001284static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001285{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001286 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001287 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1288 kvm->memslots[0].npages - 3;
1289 return base_gfn << PAGE_SHIFT;
1290 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001291 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292}
1293
1294static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1295{
1296 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1297
1298 save->selector = vmcs_read16(sf->selector);
1299 save->base = vmcs_readl(sf->base);
1300 save->limit = vmcs_read32(sf->limit);
1301 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001302 vmcs_write16(sf->selector, save->base >> 4);
1303 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001304 vmcs_write32(sf->limit, 0xffff);
1305 vmcs_write32(sf->ar_bytes, 0xf3);
1306}
1307
1308static void enter_rmode(struct kvm_vcpu *vcpu)
1309{
1310 unsigned long flags;
1311
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001312 vcpu->arch.rmode.active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001314 vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001315 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1316
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001317 vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1319
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001320 vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001321 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1322
1323 flags = vmcs_readl(GUEST_RFLAGS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001324 vcpu->arch.rmode.save_iopl
1325 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001327 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001328
1329 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001330 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331 update_exception_bitmap(vcpu);
1332
1333 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1334 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1335 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1336
1337 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001338 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001339 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1340 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001341 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1342
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001343 fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1344 fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1345 fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1346 fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001347
Eddie Dong8668a3c2007-10-10 14:26:45 +08001348 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001349 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350}
1351
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001352#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353
1354static void enter_lmode(struct kvm_vcpu *vcpu)
1355{
1356 u32 guest_tr_ar;
1357
1358 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1359 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1360 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001361 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362 vmcs_write32(GUEST_TR_AR_BYTES,
1363 (guest_tr_ar & ~AR_TYPE_MASK)
1364 | AR_TYPE_BUSY_64_TSS);
1365 }
1366
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001367 vcpu->arch.shadow_efer |= EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368
Rusty Russell8b9cf982007-07-30 16:31:43 +10001369 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370 vmcs_write32(VM_ENTRY_CONTROLS,
1371 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001372 | VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373}
1374
1375static void exit_lmode(struct kvm_vcpu *vcpu)
1376{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001377 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378
1379 vmcs_write32(VM_ENTRY_CONTROLS,
1380 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001381 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382}
1383
1384#endif
1385
Sheng Yang2384d2b2008-01-17 15:14:33 +08001386static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1387{
1388 vpid_sync_vcpu_all(to_vmx(vcpu));
1389}
1390
Anthony Liguori25c4c272007-04-27 09:29:21 +03001391static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001392{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001393 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1394 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001395}
1396
Sheng Yang14394422008-04-28 12:24:45 +08001397static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1398{
1399 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1400 if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
1401 printk(KERN_ERR "EPT: Fail to load pdptrs!\n");
1402 return;
1403 }
1404 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1405 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1406 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1407 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1408 }
1409}
1410
1411static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1412
1413static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1414 unsigned long cr0,
1415 struct kvm_vcpu *vcpu)
1416{
1417 if (!(cr0 & X86_CR0_PG)) {
1418 /* From paging/starting to nonpaging */
1419 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
1420 vmcs_config.cpu_based_exec_ctrl |
1421 (CPU_BASED_CR3_LOAD_EXITING |
1422 CPU_BASED_CR3_STORE_EXITING));
1423 vcpu->arch.cr0 = cr0;
1424 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1425 *hw_cr0 |= X86_CR0_PE | X86_CR0_PG;
1426 *hw_cr0 &= ~X86_CR0_WP;
1427 } else if (!is_paging(vcpu)) {
1428 /* From nonpaging to paging */
1429 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
1430 vmcs_config.cpu_based_exec_ctrl &
1431 ~(CPU_BASED_CR3_LOAD_EXITING |
1432 CPU_BASED_CR3_STORE_EXITING));
1433 vcpu->arch.cr0 = cr0;
1434 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1435 if (!(vcpu->arch.cr0 & X86_CR0_WP))
1436 *hw_cr0 &= ~X86_CR0_WP;
1437 }
1438}
1439
1440static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1441 struct kvm_vcpu *vcpu)
1442{
1443 if (!is_paging(vcpu)) {
1444 *hw_cr4 &= ~X86_CR4_PAE;
1445 *hw_cr4 |= X86_CR4_PSE;
1446 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1447 *hw_cr4 &= ~X86_CR4_PAE;
1448}
1449
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1451{
Sheng Yang14394422008-04-28 12:24:45 +08001452 unsigned long hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) |
1453 KVM_VM_CR0_ALWAYS_ON;
1454
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001455 vmx_fpu_deactivate(vcpu);
1456
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001457 if (vcpu->arch.rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458 enter_pmode(vcpu);
1459
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001460 if (!vcpu->arch.rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001461 enter_rmode(vcpu);
1462
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001463#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001464 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001465 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001466 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10001467 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468 exit_lmode(vcpu);
1469 }
1470#endif
1471
Sheng Yang14394422008-04-28 12:24:45 +08001472 if (vm_need_ept())
1473 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1474
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001476 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001477 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001478
Rusty Russell707d92f2007-07-17 23:19:08 +10001479 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001480 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481}
1482
Sheng Yang14394422008-04-28 12:24:45 +08001483static u64 construct_eptp(unsigned long root_hpa)
1484{
1485 u64 eptp;
1486
1487 /* TODO write the value reading from MSR */
1488 eptp = VMX_EPT_DEFAULT_MT |
1489 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1490 eptp |= (root_hpa & PAGE_MASK);
1491
1492 return eptp;
1493}
1494
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1496{
Sheng Yang14394422008-04-28 12:24:45 +08001497 unsigned long guest_cr3;
1498 u64 eptp;
1499
1500 guest_cr3 = cr3;
1501 if (vm_need_ept()) {
1502 eptp = construct_eptp(cr3);
1503 vmcs_write64(EPT_POINTER, eptp);
1504 ept_sync_context(eptp);
1505 ept_load_pdptrs(vcpu);
1506 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
1507 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1508 }
1509
Sheng Yang2384d2b2008-01-17 15:14:33 +08001510 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001511 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001512 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001513 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514}
1515
1516static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1517{
Sheng Yang14394422008-04-28 12:24:45 +08001518 unsigned long hw_cr4 = cr4 | (vcpu->arch.rmode.active ?
1519 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1520
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001521 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08001522 if (vm_need_ept())
1523 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1524
1525 vmcs_writel(CR4_READ_SHADOW, cr4);
1526 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527}
1528
Avi Kivity6aa8b732006-12-10 02:21:36 -08001529static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1530{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001531 struct vcpu_vmx *vmx = to_vmx(vcpu);
1532 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001533
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001534 vcpu->arch.shadow_efer = efer;
Joerg Roedel9f62e192008-01-31 14:57:39 +01001535 if (!msr)
1536 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537 if (efer & EFER_LMA) {
1538 vmcs_write32(VM_ENTRY_CONTROLS,
1539 vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001540 VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001541 msr->data = efer;
1542
1543 } else {
1544 vmcs_write32(VM_ENTRY_CONTROLS,
1545 vmcs_read32(VM_ENTRY_CONTROLS) &
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001546 ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001547
1548 msr->data = efer & ~EFER_LME;
1549 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001550 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001551}
1552
Avi Kivity6aa8b732006-12-10 02:21:36 -08001553static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1554{
1555 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1556
1557 return vmcs_readl(sf->base);
1558}
1559
1560static void vmx_get_segment(struct kvm_vcpu *vcpu,
1561 struct kvm_segment *var, int seg)
1562{
1563 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1564 u32 ar;
1565
1566 var->base = vmcs_readl(sf->base);
1567 var->limit = vmcs_read32(sf->limit);
1568 var->selector = vmcs_read16(sf->selector);
1569 ar = vmcs_read32(sf->ar_bytes);
1570 if (ar & AR_UNUSABLE_MASK)
1571 ar = 0;
1572 var->type = ar & 15;
1573 var->s = (ar >> 4) & 1;
1574 var->dpl = (ar >> 5) & 3;
1575 var->present = (ar >> 7) & 1;
1576 var->avl = (ar >> 12) & 1;
1577 var->l = (ar >> 13) & 1;
1578 var->db = (ar >> 14) & 1;
1579 var->g = (ar >> 15) & 1;
1580 var->unusable = (ar >> 16) & 1;
1581}
1582
Izik Eidus2e4d2652008-03-24 19:38:34 +02001583static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1584{
1585 struct kvm_segment kvm_seg;
1586
1587 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1588 return 0;
1589
1590 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1591 return 3;
1592
1593 vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
1594 return kvm_seg.selector & 3;
1595}
1596
Avi Kivity653e3102007-05-07 10:55:37 +03001597static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001598{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001599 u32 ar;
1600
Avi Kivity653e3102007-05-07 10:55:37 +03001601 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602 ar = 1 << 16;
1603 else {
1604 ar = var->type & 15;
1605 ar |= (var->s & 1) << 4;
1606 ar |= (var->dpl & 3) << 5;
1607 ar |= (var->present & 1) << 7;
1608 ar |= (var->avl & 1) << 12;
1609 ar |= (var->l & 1) << 13;
1610 ar |= (var->db & 1) << 14;
1611 ar |= (var->g & 1) << 15;
1612 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001613 if (ar == 0) /* a 0 value means unusable */
1614 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001615
1616 return ar;
1617}
1618
1619static void vmx_set_segment(struct kvm_vcpu *vcpu,
1620 struct kvm_segment *var, int seg)
1621{
1622 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1623 u32 ar;
1624
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001625 if (vcpu->arch.rmode.active && seg == VCPU_SREG_TR) {
1626 vcpu->arch.rmode.tr.selector = var->selector;
1627 vcpu->arch.rmode.tr.base = var->base;
1628 vcpu->arch.rmode.tr.limit = var->limit;
1629 vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001630 return;
1631 }
1632 vmcs_writel(sf->base, var->base);
1633 vmcs_write32(sf->limit, var->limit);
1634 vmcs_write16(sf->selector, var->selector);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001635 if (vcpu->arch.rmode.active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001636 /*
1637 * Hack real-mode segments into vm86 compatibility.
1638 */
1639 if (var->base == 0xffff0000 && var->selector == 0xf000)
1640 vmcs_writel(sf->base, 0xf0000);
1641 ar = 0xf3;
1642 } else
1643 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644 vmcs_write32(sf->ar_bytes, ar);
1645}
1646
Avi Kivity6aa8b732006-12-10 02:21:36 -08001647static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1648{
1649 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1650
1651 *db = (ar >> 14) & 1;
1652 *l = (ar >> 13) & 1;
1653}
1654
1655static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1656{
1657 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1658 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1659}
1660
1661static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1662{
1663 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1664 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1665}
1666
1667static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1668{
1669 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1670 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1671}
1672
1673static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1674{
1675 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1676 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1677}
1678
Mike Dayd77c26f2007-10-08 09:02:08 -04001679static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001681 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001682 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001683 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02001684 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001685
Izik Eidus195aefd2007-10-01 22:14:18 +02001686 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1687 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001688 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001689 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
1690 r = kvm_write_guest_page(kvm, fn++, &data, 0x66, sizeof(u16));
1691 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001692 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001693 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
1694 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001695 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001696 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1697 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001698 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001699 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001700 r = kvm_write_guest_page(kvm, fn, &data,
1701 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
1702 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02001703 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001704 goto out;
1705
1706 ret = 1;
1707out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001708 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709}
1710
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001711static int init_rmode_identity_map(struct kvm *kvm)
1712{
1713 int i, r, ret;
1714 pfn_t identity_map_pfn;
1715 u32 tmp;
1716
1717 if (!vm_need_ept())
1718 return 1;
1719 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
1720 printk(KERN_ERR "EPT: identity-mapping pagetable "
1721 "haven't been allocated!\n");
1722 return 0;
1723 }
1724 if (likely(kvm->arch.ept_identity_pagetable_done))
1725 return 1;
1726 ret = 0;
1727 identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT;
1728 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
1729 if (r < 0)
1730 goto out;
1731 /* Set up identity-mapping pagetable for EPT in real mode */
1732 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
1733 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
1734 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
1735 r = kvm_write_guest_page(kvm, identity_map_pfn,
1736 &tmp, i * sizeof(tmp), sizeof(tmp));
1737 if (r < 0)
1738 goto out;
1739 }
1740 kvm->arch.ept_identity_pagetable_done = true;
1741 ret = 1;
1742out:
1743 return ret;
1744}
1745
Avi Kivity6aa8b732006-12-10 02:21:36 -08001746static void seg_setup(int seg)
1747{
1748 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1749
1750 vmcs_write16(sf->selector, 0);
1751 vmcs_writel(sf->base, 0);
1752 vmcs_write32(sf->limit, 0xffff);
1753 vmcs_write32(sf->ar_bytes, 0x93);
1754}
1755
Sheng Yangf78e0e22007-10-29 09:40:42 +08001756static int alloc_apic_access_page(struct kvm *kvm)
1757{
1758 struct kvm_userspace_memory_region kvm_userspace_mem;
1759 int r = 0;
1760
Izik Eidus72dc67a2008-02-10 18:04:15 +02001761 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001762 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001763 goto out;
1764 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
1765 kvm_userspace_mem.flags = 0;
1766 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
1767 kvm_userspace_mem.memory_size = PAGE_SIZE;
1768 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
1769 if (r)
1770 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001771
1772 down_read(&current->mm->mmap_sem);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001773 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001774 up_read(&current->mm->mmap_sem);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001775out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001776 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001777 return r;
1778}
1779
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001780static int alloc_identity_pagetable(struct kvm *kvm)
1781{
1782 struct kvm_userspace_memory_region kvm_userspace_mem;
1783 int r = 0;
1784
1785 down_write(&kvm->slots_lock);
1786 if (kvm->arch.ept_identity_pagetable)
1787 goto out;
1788 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
1789 kvm_userspace_mem.flags = 0;
1790 kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1791 kvm_userspace_mem.memory_size = PAGE_SIZE;
1792 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
1793 if (r)
1794 goto out;
1795
1796 down_read(&current->mm->mmap_sem);
1797 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
1798 VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT);
1799 up_read(&current->mm->mmap_sem);
1800out:
1801 up_write(&kvm->slots_lock);
1802 return r;
1803}
1804
Sheng Yang2384d2b2008-01-17 15:14:33 +08001805static void allocate_vpid(struct vcpu_vmx *vmx)
1806{
1807 int vpid;
1808
1809 vmx->vpid = 0;
1810 if (!enable_vpid || !cpu_has_vmx_vpid())
1811 return;
1812 spin_lock(&vmx_vpid_lock);
1813 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
1814 if (vpid < VMX_NR_VPIDS) {
1815 vmx->vpid = vpid;
1816 __set_bit(vpid, vmx_vpid_bitmap);
1817 }
1818 spin_unlock(&vmx_vpid_lock);
1819}
1820
Sheng Yang25c5f222008-03-28 13:18:56 +08001821void vmx_disable_intercept_for_msr(struct page *msr_bitmap, u32 msr)
1822{
1823 void *va;
1824
1825 if (!cpu_has_vmx_msr_bitmap())
1826 return;
1827
1828 /*
1829 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
1830 * have the write-low and read-high bitmap offsets the wrong way round.
1831 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
1832 */
1833 va = kmap(msr_bitmap);
1834 if (msr <= 0x1fff) {
1835 __clear_bit(msr, va + 0x000); /* read-low */
1836 __clear_bit(msr, va + 0x800); /* write-low */
1837 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
1838 msr &= 0x1fff;
1839 __clear_bit(msr, va + 0x400); /* read-high */
1840 __clear_bit(msr, va + 0xc00); /* write-high */
1841 }
1842 kunmap(msr_bitmap);
1843}
1844
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845/*
1846 * Sets up the vmcs for emulated real mode.
1847 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001848static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001849{
1850 u32 host_sysenter_cs;
1851 u32 junk;
1852 unsigned long a;
1853 struct descriptor_table dt;
1854 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03001855 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001856 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001857
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03001859 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
1860 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001861
Sheng Yang25c5f222008-03-28 13:18:56 +08001862 if (cpu_has_vmx_msr_bitmap())
1863 vmcs_write64(MSR_BITMAP, page_to_phys(vmx_msr_bitmap));
1864
Avi Kivity6aa8b732006-12-10 02:21:36 -08001865 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
1866
Avi Kivity6aa8b732006-12-10 02:21:36 -08001867 /* Control */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001868 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
1869 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001870
1871 exec_control = vmcs_config.cpu_based_exec_ctrl;
1872 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
1873 exec_control &= ~CPU_BASED_TPR_SHADOW;
1874#ifdef CONFIG_X86_64
1875 exec_control |= CPU_BASED_CR8_STORE_EXITING |
1876 CPU_BASED_CR8_LOAD_EXITING;
1877#endif
1878 }
Sheng Yangd56f5462008-04-25 10:13:16 +08001879 if (!vm_need_ept())
1880 exec_control |= CPU_BASED_CR3_STORE_EXITING |
1881 CPU_BASED_CR3_LOAD_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001882 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001883
Sheng Yang83ff3b92007-11-21 14:33:25 +08001884 if (cpu_has_secondary_exec_ctrls()) {
1885 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
1886 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
1887 exec_control &=
1888 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001889 if (vmx->vpid == 0)
1890 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yangd56f5462008-04-25 10:13:16 +08001891 if (!vm_need_ept())
1892 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang83ff3b92007-11-21 14:33:25 +08001893 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
1894 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08001895
Avi Kivityc7addb92007-09-16 18:58:32 +02001896 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
1897 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001898 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
1899
1900 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
1901 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
1902 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1903
1904 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
1905 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1906 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1907 vmcs_write16(HOST_FS_SELECTOR, read_fs()); /* 22.2.4 */
1908 vmcs_write16(HOST_GS_SELECTOR, read_gs()); /* 22.2.4 */
1909 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001910#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001911 rdmsrl(MSR_FS_BASE, a);
1912 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
1913 rdmsrl(MSR_GS_BASE, a);
1914 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
1915#else
1916 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
1917 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
1918#endif
1919
1920 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
1921
1922 get_idt(&dt);
1923 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
1924
Mike Dayd77c26f2007-10-08 09:02:08 -04001925 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03001926 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03001927 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
1928 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
1929 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001930
1931 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
1932 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
1933 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
1934 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
1935 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
1936 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
1937
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938 for (i = 0; i < NR_VMX_MSR; ++i) {
1939 u32 index = vmx_msr_index[i];
1940 u32 data_low, data_high;
1941 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001942 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943
1944 if (rdmsr_safe(index, &data_low, &data_high) < 0)
1945 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08001946 if (wrmsr_safe(index, data_low, data_high) < 0)
1947 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001949 vmx->host_msrs[j].index = index;
1950 vmx->host_msrs[j].reserved = 0;
1951 vmx->host_msrs[j].data = data;
1952 vmx->guest_msrs[j] = vmx->host_msrs[j];
1953 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001955
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001956 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001957
1958 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001959 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
1960
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001961 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
1962 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
1963
Sheng Yangf78e0e22007-10-29 09:40:42 +08001964
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001965 return 0;
1966}
1967
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001968static int init_rmode(struct kvm *kvm)
1969{
1970 if (!init_rmode_tss(kvm))
1971 return 0;
1972 if (!init_rmode_identity_map(kvm))
1973 return 0;
1974 return 1;
1975}
1976
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001977static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
1978{
1979 struct vcpu_vmx *vmx = to_vmx(vcpu);
1980 u64 msr;
1981 int ret;
1982
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001983 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001984 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001985 ret = -ENOMEM;
1986 goto out;
1987 }
1988
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001989 vmx->vcpu.arch.rmode.active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001990
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001991 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001992 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001993 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
1994 if (vmx->vcpu.vcpu_id == 0)
1995 msr |= MSR_IA32_APICBASE_BSP;
1996 kvm_set_apic_base(&vmx->vcpu, msr);
1997
1998 fx_init(&vmx->vcpu);
1999
2000 /*
2001 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2002 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2003 */
2004 if (vmx->vcpu.vcpu_id == 0) {
2005 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2006 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2007 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002008 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2009 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002010 }
2011 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
2012 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
2013
2014 seg_setup(VCPU_SREG_DS);
2015 seg_setup(VCPU_SREG_ES);
2016 seg_setup(VCPU_SREG_FS);
2017 seg_setup(VCPU_SREG_GS);
2018 seg_setup(VCPU_SREG_SS);
2019
2020 vmcs_write16(GUEST_TR_SELECTOR, 0);
2021 vmcs_writel(GUEST_TR_BASE, 0);
2022 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2023 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2024
2025 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2026 vmcs_writel(GUEST_LDTR_BASE, 0);
2027 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2028 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2029
2030 vmcs_write32(GUEST_SYSENTER_CS, 0);
2031 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2032 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2033
2034 vmcs_writel(GUEST_RFLAGS, 0x02);
2035 if (vmx->vcpu.vcpu_id == 0)
2036 vmcs_writel(GUEST_RIP, 0xfff0);
2037 else
2038 vmcs_writel(GUEST_RIP, 0);
2039 vmcs_writel(GUEST_RSP, 0);
2040
2041 /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */
2042 vmcs_writel(GUEST_DR7, 0x400);
2043
2044 vmcs_writel(GUEST_GDTR_BASE, 0);
2045 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2046
2047 vmcs_writel(GUEST_IDTR_BASE, 0);
2048 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2049
2050 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2051 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2052 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2053
2054 guest_write_tsc(0);
2055
2056 /* Special registers */
2057 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2058
2059 setup_msrs(vmx);
2060
Avi Kivity6aa8b732006-12-10 02:21:36 -08002061 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2062
Sheng Yangf78e0e22007-10-29 09:40:42 +08002063 if (cpu_has_vmx_tpr_shadow()) {
2064 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2065 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2066 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002067 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002068 vmcs_write32(TPR_THRESHOLD, 0);
2069 }
2070
2071 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2072 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002073 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002074
Sheng Yang2384d2b2008-01-17 15:14:33 +08002075 if (vmx->vpid != 0)
2076 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2077
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002078 vmx->vcpu.arch.cr0 = 0x60000010;
2079 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002080 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002081 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002082 vmx_fpu_activate(&vmx->vcpu);
2083 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002084
Sheng Yang2384d2b2008-01-17 15:14:33 +08002085 vpid_sync_vcpu_all(vmx);
2086
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002087 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088
Avi Kivity6aa8b732006-12-10 02:21:36 -08002089out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002090 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002091 return ret;
2092}
2093
Eddie Dong85f455f2007-07-06 12:20:49 +03002094static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
2095{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002096 struct vcpu_vmx *vmx = to_vmx(vcpu);
2097
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002098 KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler);
2099
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002100 if (vcpu->arch.rmode.active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002101 vmx->rmode.irq.pending = true;
2102 vmx->rmode.irq.vector = irq;
2103 vmx->rmode.irq.rip = vmcs_readl(GUEST_RIP);
Avi Kivity9c5623e2007-11-08 18:19:20 +02002104 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2105 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2106 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Avi Kivity9c8cba32007-11-22 11:42:59 +02002107 vmcs_writel(GUEST_RIP, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002108 return;
2109 }
2110 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2111 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
2112}
2113
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
2115{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002116 int word_index = __ffs(vcpu->arch.irq_summary);
2117 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002118 int irq = word_index * BITS_PER_LONG + bit_index;
2119
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002120 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
2121 if (!vcpu->arch.irq_pending[word_index])
2122 clear_bit(word_index, &vcpu->arch.irq_summary);
Eddie Dong85f455f2007-07-06 12:20:49 +03002123 vmx_inject_irq(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002124}
2125
Dor Laorc1150d82007-01-05 16:36:24 -08002126
2127static void do_interrupt_requests(struct kvm_vcpu *vcpu,
2128 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002129{
Dor Laorc1150d82007-01-05 16:36:24 -08002130 u32 cpu_based_vm_exec_control;
2131
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002132 vcpu->arch.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08002133 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2134 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
2135
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002136 if (vcpu->arch.interrupt_window_open &&
2137 vcpu->arch.irq_summary &&
Dor Laorc1150d82007-01-05 16:36:24 -08002138 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002139 /*
Dor Laorc1150d82007-01-05 16:36:24 -08002140 * If interrupts enabled, and not blocked by sti or mov ss. Good.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002141 */
2142 kvm_do_inject_irq(vcpu);
Dor Laorc1150d82007-01-05 16:36:24 -08002143
2144 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002145 if (!vcpu->arch.interrupt_window_open &&
2146 (vcpu->arch.irq_summary || kvm_run->request_interrupt_window))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147 /*
2148 * Interrupts blocked. Wait for unblock.
2149 */
Dor Laorc1150d82007-01-05 16:36:24 -08002150 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2151 else
2152 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2153 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002154}
2155
Izik Eiduscbc94022007-10-25 00:29:55 +02002156static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2157{
2158 int ret;
2159 struct kvm_userspace_memory_region tss_mem = {
2160 .slot = 8,
2161 .guest_phys_addr = addr,
2162 .memory_size = PAGE_SIZE * 3,
2163 .flags = 0,
2164 };
2165
2166 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2167 if (ret)
2168 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002169 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002170 return 0;
2171}
2172
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
2174{
2175 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
2176
2177 set_debugreg(dbg->bp[0], 0);
2178 set_debugreg(dbg->bp[1], 1);
2179 set_debugreg(dbg->bp[2], 2);
2180 set_debugreg(dbg->bp[3], 3);
2181
2182 if (dbg->singlestep) {
2183 unsigned long flags;
2184
2185 flags = vmcs_readl(GUEST_RFLAGS);
2186 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
2187 vmcs_writel(GUEST_RFLAGS, flags);
2188 }
2189}
2190
2191static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2192 int vec, u32 err_code)
2193{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002194 if (!vcpu->arch.rmode.active)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002195 return 0;
2196
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002197 /*
2198 * Instruction with address size override prefix opcode 0x67
2199 * Cause the #SS fault with 0 error code in VM86 mode.
2200 */
2201 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02002202 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203 return 1;
2204 return 0;
2205}
2206
2207static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2208{
Avi Kivity1155f762007-11-22 11:30:47 +02002209 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002210 u32 intr_info, error_code;
2211 unsigned long cr2, rip;
2212 u32 vect_info;
2213 enum emulation_result er;
2214
Avi Kivity1155f762007-11-22 11:30:47 +02002215 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002216 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2217
2218 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002219 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002220 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002221 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002222
Eddie Dong85f455f2007-07-06 12:20:49 +03002223 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002224 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002225 set_bit(irq, vcpu->arch.irq_pending);
2226 set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002227 }
2228
Avi Kivity1b6269d2007-10-09 12:12:19 +02002229 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
2230 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002231
2232 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002233 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002234 return 1;
2235 }
2236
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002237 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08002238 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002239 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002240 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002241 return 1;
2242 }
2243
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244 error_code = 0;
2245 rip = vmcs_readl(GUEST_RIP);
Ryan Harper2e113842008-02-11 10:26:38 -06002246 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2248 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002249 /* EPT won't cause page fault directly */
2250 if (vm_need_ept())
2251 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002253 KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2,
2254 (u32)((u64)cr2 >> 32), handler);
Avi Kivity30677142007-10-28 18:48:59 +02002255 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256 }
2257
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002258 if (vcpu->arch.rmode.active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002259 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002260 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002261 if (vcpu->arch.halt_request) {
2262 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002263 return kvm_emulate_halt(vcpu);
2264 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002266 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267
Mike Dayd77c26f2007-10-08 09:02:08 -04002268 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) ==
2269 (INTR_TYPE_EXCEPTION | 1)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2271 return 0;
2272 }
2273 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2274 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
2275 kvm_run->ex.error_code = error_code;
2276 return 0;
2277}
2278
2279static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2280 struct kvm_run *kvm_run)
2281{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002282 ++vcpu->stat.irq_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002283 KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002284 return 1;
2285}
2286
Avi Kivity988ad742007-02-12 00:54:36 -08002287static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2288{
2289 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2290 return 0;
2291}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002292
Avi Kivity6aa8b732006-12-10 02:21:36 -08002293static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2294{
He, Qingbfdaab02007-09-12 14:18:28 +08002295 unsigned long exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02002296 int size, down, in, string, rep;
2297 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298
Avi Kivity1165f5f2007-04-19 17:27:43 +03002299 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002300 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002301 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002302
2303 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002304 if (emulate_instruction(vcpu,
2305 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002306 return 0;
2307 return 1;
2308 }
2309
2310 size = (exit_qualification & 7) + 1;
2311 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002312 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002313 rep = (exit_qualification & 32) != 0;
2314 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002315
Laurent Vivier3090dd72007-08-05 10:43:32 +03002316 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002317}
2318
Ingo Molnar102d8322007-02-19 14:37:47 +02002319static void
2320vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2321{
2322 /*
2323 * Patch in the VMCALL instruction:
2324 */
2325 hypercall[0] = 0x0f;
2326 hypercall[1] = 0x01;
2327 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002328}
2329
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2331{
He, Qingbfdaab02007-09-12 14:18:28 +08002332 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333 int cr;
2334 int reg;
2335
He, Qingbfdaab02007-09-12 14:18:28 +08002336 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002337 cr = exit_qualification & 15;
2338 reg = (exit_qualification >> 8) & 15;
2339 switch ((exit_qualification >> 4) & 3) {
2340 case 0: /* mov to cr */
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002341 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr, (u32)vcpu->arch.regs[reg],
2342 (u32)((u64)vcpu->arch.regs[reg] >> 32), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343 switch (cr) {
2344 case 0:
2345 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002346 kvm_set_cr0(vcpu, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347 skip_emulated_instruction(vcpu);
2348 return 1;
2349 case 3:
2350 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002351 kvm_set_cr3(vcpu, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002352 skip_emulated_instruction(vcpu);
2353 return 1;
2354 case 4:
2355 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002356 kvm_set_cr4(vcpu, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002357 skip_emulated_instruction(vcpu);
2358 return 1;
2359 case 8:
2360 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002361 kvm_set_cr8(vcpu, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362 skip_emulated_instruction(vcpu);
Avi Kivitye5314062007-12-06 16:32:45 +02002363 if (irqchip_in_kernel(vcpu->kvm))
2364 return 1;
Yang, Sheng253abde2007-08-16 13:01:00 +03002365 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2366 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367 };
2368 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002369 case 2: /* clts */
2370 vcpu_load_rsp_rip(vcpu);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002371 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002372 vcpu->arch.cr0 &= ~X86_CR0_TS;
2373 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002374 vmx_fpu_activate(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002375 KVMTRACE_0D(CLTS, vcpu, handler);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002376 skip_emulated_instruction(vcpu);
2377 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378 case 1: /*mov from cr*/
2379 switch (cr) {
2380 case 3:
2381 vcpu_load_rsp_rip(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002382 vcpu->arch.regs[reg] = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383 vcpu_put_rsp_rip(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002384 KVMTRACE_3D(CR_READ, vcpu, (u32)cr,
2385 (u32)vcpu->arch.regs[reg],
2386 (u32)((u64)vcpu->arch.regs[reg] >> 32),
2387 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002388 skip_emulated_instruction(vcpu);
2389 return 1;
2390 case 8:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002392 vcpu->arch.regs[reg] = kvm_get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393 vcpu_put_rsp_rip(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002394 KVMTRACE_2D(CR_READ, vcpu, (u32)cr,
2395 (u32)vcpu->arch.regs[reg], handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396 skip_emulated_instruction(vcpu);
2397 return 1;
2398 }
2399 break;
2400 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002401 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402
2403 skip_emulated_instruction(vcpu);
2404 return 1;
2405 default:
2406 break;
2407 }
2408 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002409 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002410 (int)(exit_qualification >> 4) & 3, cr);
2411 return 0;
2412}
2413
2414static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2415{
He, Qingbfdaab02007-09-12 14:18:28 +08002416 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002417 unsigned long val;
2418 int dr, reg;
2419
2420 /*
2421 * FIXME: this code assumes the host is debugging the guest.
2422 * need to deal with guest debugging itself too.
2423 */
He, Qingbfdaab02007-09-12 14:18:28 +08002424 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425 dr = exit_qualification & 7;
2426 reg = (exit_qualification >> 8) & 15;
2427 vcpu_load_rsp_rip(vcpu);
2428 if (exit_qualification & 16) {
2429 /* mov from dr */
2430 switch (dr) {
2431 case 6:
2432 val = 0xffff0ff0;
2433 break;
2434 case 7:
2435 val = 0x400;
2436 break;
2437 default:
2438 val = 0;
2439 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002440 vcpu->arch.regs[reg] = val;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002441 KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442 } else {
2443 /* mov to dr */
2444 }
2445 vcpu_put_rsp_rip(vcpu);
2446 skip_emulated_instruction(vcpu);
2447 return 1;
2448}
2449
2450static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2451{
Avi Kivity06465c52007-02-28 20:46:53 +02002452 kvm_emulate_cpuid(vcpu);
2453 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454}
2455
2456static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2457{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002458 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002459 u64 data;
2460
2461 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002462 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463 return 1;
2464 }
2465
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002466 KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32),
2467 handler);
2468
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002470 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
2471 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002472 skip_emulated_instruction(vcpu);
2473 return 1;
2474}
2475
2476static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2477{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002478 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
2479 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
2480 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002481
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002482 KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32),
2483 handler);
2484
Avi Kivity6aa8b732006-12-10 02:21:36 -08002485 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002486 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002487 return 1;
2488 }
2489
2490 skip_emulated_instruction(vcpu);
2491 return 1;
2492}
2493
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002494static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2495 struct kvm_run *kvm_run)
2496{
2497 return 1;
2498}
2499
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2501 struct kvm_run *kvm_run)
2502{
Eddie Dong85f455f2007-07-06 12:20:49 +03002503 u32 cpu_based_vm_exec_control;
2504
2505 /* clear pending irq */
2506 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2507 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2508 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002509
2510 KVMTRACE_0D(PEND_INTR, vcpu, handler);
2511
Dor Laorc1150d82007-01-05 16:36:24 -08002512 /*
2513 * If the user space waits to inject interrupts, exit as soon as
2514 * possible
2515 */
2516 if (kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002517 !vcpu->arch.irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002518 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002519 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002520 return 0;
2521 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002522 return 1;
2523}
2524
2525static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2526{
2527 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002528 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002529}
2530
Ingo Molnarc21415e2007-02-19 14:37:47 +02002531static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2532{
Dor Laor510043d2007-02-19 18:25:43 +02002533 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002534 kvm_emulate_hypercall(vcpu);
2535 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02002536}
2537
Eddie Donge5edaa02007-11-11 12:28:35 +02002538static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2539{
2540 skip_emulated_instruction(vcpu);
2541 /* TODO: Add support for VT-d/pass-through device */
2542 return 1;
2543}
2544
Sheng Yangf78e0e22007-10-29 09:40:42 +08002545static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2546{
2547 u64 exit_qualification;
2548 enum emulation_result er;
2549 unsigned long offset;
2550
2551 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2552 offset = exit_qualification & 0xffful;
2553
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002554 KVMTRACE_1D(APIC_ACCESS, vcpu, (u32)offset, handler);
2555
Sheng Yangf78e0e22007-10-29 09:40:42 +08002556 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
2557
2558 if (er != EMULATE_DONE) {
2559 printk(KERN_ERR
2560 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
2561 offset);
2562 return -ENOTSUPP;
2563 }
2564 return 1;
2565}
2566
Izik Eidus37817f22008-03-24 23:14:53 +02002567static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2568{
2569 unsigned long exit_qualification;
2570 u16 tss_selector;
2571 int reason;
2572
2573 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
2574
2575 reason = (u32)exit_qualification >> 30;
2576 tss_selector = exit_qualification;
2577
2578 return kvm_task_switch(vcpu, tss_selector, reason);
2579}
2580
Sheng Yang14394422008-04-28 12:24:45 +08002581static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2582{
2583 u64 exit_qualification;
2584 enum emulation_result er;
2585 gpa_t gpa;
2586 unsigned long hva;
2587 int gla_validity;
2588 int r;
2589
2590 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2591
2592 if (exit_qualification & (1 << 6)) {
2593 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
2594 return -ENOTSUPP;
2595 }
2596
2597 gla_validity = (exit_qualification >> 7) & 0x3;
2598 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
2599 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
2600 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
2601 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
2602 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
2603 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
2604 (long unsigned int)exit_qualification);
2605 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2606 kvm_run->hw.hardware_exit_reason = 0;
2607 return -ENOTSUPP;
2608 }
2609
2610 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
2611 hva = gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT);
2612 if (!kvm_is_error_hva(hva)) {
2613 r = kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
2614 if (r < 0) {
2615 printk(KERN_ERR "EPT: Not enough memory!\n");
2616 return -ENOMEM;
2617 }
2618 return 1;
2619 } else {
2620 /* must be MMIO */
2621 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
2622
2623 if (er == EMULATE_FAIL) {
2624 printk(KERN_ERR
2625 "EPT: Fail to handle EPT violation vmexit!er is %d\n",
2626 er);
2627 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
2628 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
2629 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
2630 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
2631 (long unsigned int)exit_qualification);
2632 return -ENOTSUPP;
2633 } else if (er == EMULATE_DO_MMIO)
2634 return 0;
2635 }
2636 return 1;
2637}
2638
Avi Kivity6aa8b732006-12-10 02:21:36 -08002639/*
2640 * The exit handlers return 1 if the exit was handled fully and guest execution
2641 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2642 * to be done to userspace and return 0.
2643 */
2644static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
2645 struct kvm_run *kvm_run) = {
2646 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
2647 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08002648 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002649 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650 [EXIT_REASON_CR_ACCESS] = handle_cr,
2651 [EXIT_REASON_DR_ACCESS] = handle_dr,
2652 [EXIT_REASON_CPUID] = handle_cpuid,
2653 [EXIT_REASON_MSR_READ] = handle_rdmsr,
2654 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
2655 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
2656 [EXIT_REASON_HLT] = handle_halt,
Ingo Molnarc21415e2007-02-19 14:37:47 +02002657 [EXIT_REASON_VMCALL] = handle_vmcall,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002658 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
2659 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02002660 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02002661 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Sheng Yang14394422008-04-28 12:24:45 +08002662 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663};
2664
2665static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04002666 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002667
2668/*
2669 * The guest has exited. See if we can fix it or if we need userspace
2670 * assistance.
2671 */
2672static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2673{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity29bd8a72007-09-10 17:27:03 +03002675 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1155f762007-11-22 11:30:47 +02002676 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03002677
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002678 KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)vmcs_readl(GUEST_RIP),
2679 (u32)((u64)vmcs_readl(GUEST_RIP) >> 32), entryexit);
2680
Sheng Yang14394422008-04-28 12:24:45 +08002681 /* Access CR3 don't cause VMExit in paging mode, so we need
2682 * to sync with guest real CR3. */
2683 if (vm_need_ept() && is_paging(vcpu)) {
2684 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2685 ept_load_pdptrs(vcpu);
2686 }
2687
Avi Kivity29bd8a72007-09-10 17:27:03 +03002688 if (unlikely(vmx->fail)) {
2689 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2690 kvm_run->fail_entry.hardware_entry_failure_reason
2691 = vmcs_read32(VM_INSTRUCTION_ERROR);
2692 return 0;
2693 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002694
Mike Dayd77c26f2007-10-08 09:02:08 -04002695 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08002696 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
2697 exit_reason != EXIT_REASON_EPT_VIOLATION))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698 printk(KERN_WARNING "%s: unexpected, valid vectoring info and "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002699 "exit reason is 0x%x\n", __func__, exit_reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700 if (exit_reason < kvm_vmx_max_exit_handlers
2701 && kvm_vmx_exit_handlers[exit_reason])
2702 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
2703 else {
2704 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2705 kvm_run->hw.hardware_exit_reason = exit_reason;
2706 }
2707 return 0;
2708}
2709
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002710static void update_tpr_threshold(struct kvm_vcpu *vcpu)
2711{
2712 int max_irr, tpr;
2713
2714 if (!vm_need_tpr_shadow(vcpu->kvm))
2715 return;
2716
2717 if (!kvm_lapic_enabled(vcpu) ||
2718 ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
2719 vmcs_write32(TPR_THRESHOLD, 0);
2720 return;
2721 }
2722
2723 tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
2724 vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
2725}
2726
Eddie Dong85f455f2007-07-06 12:20:49 +03002727static void enable_irq_window(struct kvm_vcpu *vcpu)
2728{
2729 u32 cpu_based_vm_exec_control;
2730
2731 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2732 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2733 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2734}
2735
2736static void vmx_intr_assist(struct kvm_vcpu *vcpu)
2737{
Avi Kivity1155f762007-11-22 11:30:47 +02002738 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002739 u32 idtv_info_field, intr_info_field;
2740 int has_ext_irq, interrupt_window_open;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002741 int vector;
Eddie Dong85f455f2007-07-06 12:20:49 +03002742
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002743 update_tpr_threshold(vcpu);
2744
Eddie Dong85f455f2007-07-06 12:20:49 +03002745 has_ext_irq = kvm_cpu_has_interrupt(vcpu);
2746 intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
Avi Kivity1155f762007-11-22 11:30:47 +02002747 idtv_info_field = vmx->idt_vectoring_info;
Eddie Dong85f455f2007-07-06 12:20:49 +03002748 if (intr_info_field & INTR_INFO_VALID_MASK) {
2749 if (idtv_info_field & INTR_INFO_VALID_MASK) {
2750 /* TODO: fault when IDT_Vectoring */
Ryan Harper9584bf22007-12-13 10:21:10 -06002751 if (printk_ratelimit())
2752 printk(KERN_ERR "Fault when IDT_Vectoring\n");
Eddie Dong85f455f2007-07-06 12:20:49 +03002753 }
2754 if (has_ext_irq)
2755 enable_irq_window(vcpu);
2756 return;
2757 }
2758 if (unlikely(idtv_info_field & INTR_INFO_VALID_MASK)) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002759 if ((idtv_info_field & VECTORING_INFO_TYPE_MASK)
2760 == INTR_TYPE_EXT_INTR
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002761 && vcpu->arch.rmode.active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002762 u8 vect = idtv_info_field & VECTORING_INFO_VECTOR_MASK;
2763
2764 vmx_inject_irq(vcpu, vect);
2765 if (unlikely(has_ext_irq))
2766 enable_irq_window(vcpu);
2767 return;
2768 }
2769
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002770 KVMTRACE_1D(REDELIVER_EVT, vcpu, idtv_info_field, handler);
2771
Eddie Dong85f455f2007-07-06 12:20:49 +03002772 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, idtv_info_field);
2773 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2774 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
2775
Ryan Harper2e113842008-02-11 10:26:38 -06002776 if (unlikely(idtv_info_field & INTR_INFO_DELIVER_CODE_MASK))
Eddie Dong85f455f2007-07-06 12:20:49 +03002777 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2778 vmcs_read32(IDT_VECTORING_ERROR_CODE));
2779 if (unlikely(has_ext_irq))
2780 enable_irq_window(vcpu);
2781 return;
2782 }
2783 if (!has_ext_irq)
2784 return;
2785 interrupt_window_open =
2786 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2787 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002788 if (interrupt_window_open) {
2789 vector = kvm_cpu_get_interrupt(vcpu);
2790 vmx_inject_irq(vcpu, vector);
2791 kvm_timer_intr_post(vcpu, vector);
2792 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03002793 enable_irq_window(vcpu);
2794}
2795
Avi Kivity9c8cba32007-11-22 11:42:59 +02002796/*
2797 * Failure to inject an interrupt should give us the information
2798 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
2799 * when fetching the interrupt redirection bitmap in the real-mode
2800 * tss, this doesn't happen. So we do it ourselves.
2801 */
2802static void fixup_rmode_irq(struct vcpu_vmx *vmx)
2803{
2804 vmx->rmode.irq.pending = 0;
2805 if (vmcs_readl(GUEST_RIP) + 1 != vmx->rmode.irq.rip)
2806 return;
2807 vmcs_writel(GUEST_RIP, vmx->rmode.irq.rip);
2808 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
2809 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
2810 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
2811 return;
2812 }
2813 vmx->idt_vectoring_info =
2814 VECTORING_INFO_VALID_MASK
2815 | INTR_TYPE_EXT_INTR
2816 | vmx->rmode.irq.vector;
2817}
2818
Avi Kivity04d2cc72007-09-10 18:10:54 +03002819static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002820{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002821 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1b6269d2007-10-09 12:12:19 +02002822 u32 intr_info;
Avi Kivitye6adf282007-04-30 16:07:54 +03002823
2824 /*
2825 * Loading guest fpu may have cleared host cr0.ts
2826 */
2827 vmcs_writel(HOST_CR0, read_cr0());
2828
Mike Dayd77c26f2007-10-08 09:02:08 -04002829 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08002830 /* Store host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002831#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02002832 "push %%rdx; push %%rbp;"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833 "push %%rcx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002834#else
Laurent Vivierff593e52007-10-25 14:18:55 +02002835 "push %%edx; push %%ebp;"
2836 "push %%ecx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002837#endif
Laurent Vivierc2036302007-10-25 14:18:52 +02002838 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02002840 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002841 /* Load guest registers. Don't clobber flags. */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002842#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02002843 "mov %c[cr2](%0), %%rax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844 "mov %%rax, %%cr2 \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02002845 "mov %c[rax](%0), %%rax \n\t"
2846 "mov %c[rbx](%0), %%rbx \n\t"
2847 "mov %c[rdx](%0), %%rdx \n\t"
2848 "mov %c[rsi](%0), %%rsi \n\t"
2849 "mov %c[rdi](%0), %%rdi \n\t"
2850 "mov %c[rbp](%0), %%rbp \n\t"
2851 "mov %c[r8](%0), %%r8 \n\t"
2852 "mov %c[r9](%0), %%r9 \n\t"
2853 "mov %c[r10](%0), %%r10 \n\t"
2854 "mov %c[r11](%0), %%r11 \n\t"
2855 "mov %c[r12](%0), %%r12 \n\t"
2856 "mov %c[r13](%0), %%r13 \n\t"
2857 "mov %c[r14](%0), %%r14 \n\t"
2858 "mov %c[r15](%0), %%r15 \n\t"
2859 "mov %c[rcx](%0), %%rcx \n\t" /* kills %0 (rcx) */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860#else
Avi Kivitye08aa782007-11-15 18:06:18 +02002861 "mov %c[cr2](%0), %%eax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862 "mov %%eax, %%cr2 \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02002863 "mov %c[rax](%0), %%eax \n\t"
2864 "mov %c[rbx](%0), %%ebx \n\t"
2865 "mov %c[rdx](%0), %%edx \n\t"
2866 "mov %c[rsi](%0), %%esi \n\t"
2867 "mov %c[rdi](%0), %%edi \n\t"
2868 "mov %c[rbp](%0), %%ebp \n\t"
2869 "mov %c[rcx](%0), %%ecx \n\t" /* kills %0 (ecx) */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002870#endif
2871 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03002872 "jne .Llaunched \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002873 ASM_VMX_VMLAUNCH "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03002874 "jmp .Lkvm_vmx_return \n\t"
2875 ".Llaunched: " ASM_VMX_VMRESUME "\n\t"
2876 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877 /* Save guest registers, load host registers, keep flags */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002878#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02002879 "xchg %0, (%%rsp) \n\t"
2880 "mov %%rax, %c[rax](%0) \n\t"
2881 "mov %%rbx, %c[rbx](%0) \n\t"
2882 "pushq (%%rsp); popq %c[rcx](%0) \n\t"
2883 "mov %%rdx, %c[rdx](%0) \n\t"
2884 "mov %%rsi, %c[rsi](%0) \n\t"
2885 "mov %%rdi, %c[rdi](%0) \n\t"
2886 "mov %%rbp, %c[rbp](%0) \n\t"
2887 "mov %%r8, %c[r8](%0) \n\t"
2888 "mov %%r9, %c[r9](%0) \n\t"
2889 "mov %%r10, %c[r10](%0) \n\t"
2890 "mov %%r11, %c[r11](%0) \n\t"
2891 "mov %%r12, %c[r12](%0) \n\t"
2892 "mov %%r13, %c[r13](%0) \n\t"
2893 "mov %%r14, %c[r14](%0) \n\t"
2894 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002895 "mov %%cr2, %%rax \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02002896 "mov %%rax, %c[cr2](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002897
Avi Kivitye08aa782007-11-15 18:06:18 +02002898 "pop %%rbp; pop %%rbp; pop %%rdx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899#else
Avi Kivitye08aa782007-11-15 18:06:18 +02002900 "xchg %0, (%%esp) \n\t"
2901 "mov %%eax, %c[rax](%0) \n\t"
2902 "mov %%ebx, %c[rbx](%0) \n\t"
2903 "pushl (%%esp); popl %c[rcx](%0) \n\t"
2904 "mov %%edx, %c[rdx](%0) \n\t"
2905 "mov %%esi, %c[rsi](%0) \n\t"
2906 "mov %%edi, %c[rdi](%0) \n\t"
2907 "mov %%ebp, %c[rbp](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908 "mov %%cr2, %%eax \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02002909 "mov %%eax, %c[cr2](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002910
Avi Kivitye08aa782007-11-15 18:06:18 +02002911 "pop %%ebp; pop %%ebp; pop %%edx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002912#endif
Avi Kivitye08aa782007-11-15 18:06:18 +02002913 "setbe %c[fail](%0) \n\t"
2914 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
2915 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
2916 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002917 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
2918 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
2919 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
2920 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
2921 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
2922 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
2923 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002924#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002925 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
2926 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
2927 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
2928 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
2929 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
2930 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
2931 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
2932 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002934 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02002935 : "cc", "memory"
2936#ifdef CONFIG_X86_64
2937 , "rbx", "rdi", "rsi"
2938 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Laurent Vivierff593e52007-10-25 14:18:55 +02002939#else
2940 , "ebx", "edi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02002941#endif
2942 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08002943
Avi Kivity1155f762007-11-22 11:30:47 +02002944 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02002945 if (vmx->rmode.irq.pending)
2946 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02002947
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002948 vcpu->arch.interrupt_window_open =
Mike Dayd77c26f2007-10-08 09:02:08 -04002949 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002950
Mike Dayd77c26f2007-10-08 09:02:08 -04002951 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03002952 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02002953
2954 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2955
2956 /* We need to handle NMIs before interrupts are enabled */
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002957 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) { /* nmi */
2958 KVMTRACE_0D(NMI, vcpu, handler);
Avi Kivity1b6269d2007-10-09 12:12:19 +02002959 asm("int $2");
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002960 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002961}
2962
Avi Kivity6aa8b732006-12-10 02:21:36 -08002963static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
2964{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002965 struct vcpu_vmx *vmx = to_vmx(vcpu);
2966
2967 if (vmx->vmcs) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002968 on_each_cpu(__vcpu_clear, vmx, 0, 1);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002969 free_vmcs(vmx->vmcs);
2970 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002971 }
2972}
2973
2974static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
2975{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002976 struct vcpu_vmx *vmx = to_vmx(vcpu);
2977
Sheng Yang2384d2b2008-01-17 15:14:33 +08002978 spin_lock(&vmx_vpid_lock);
2979 if (vmx->vpid != 0)
2980 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
2981 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002982 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002983 kfree(vmx->host_msrs);
2984 kfree(vmx->guest_msrs);
2985 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10002986 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002987}
2988
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002989static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002990{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002991 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10002992 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03002993 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002994
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002995 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002996 return ERR_PTR(-ENOMEM);
2997
Sheng Yang2384d2b2008-01-17 15:14:33 +08002998 allocate_vpid(vmx);
Sheng Yang14394422008-04-28 12:24:45 +08002999 if (id == 0 && vm_need_ept()) {
3000 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
3001 VMX_EPT_WRITABLE_MASK |
3002 VMX_EPT_DEFAULT_MT << VMX_EPT_MT_EPTE_SHIFT);
3003 kvm_mmu_set_mask_ptes(0ull, VMX_EPT_FAKE_ACCESSED_MASK,
3004 VMX_EPT_FAKE_DIRTY_MASK, 0ull,
3005 VMX_EPT_EXECUTABLE_MASK);
3006 kvm_enable_tdp();
3007 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003008
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003009 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3010 if (err)
3011 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003012
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003013 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003014 if (!vmx->guest_msrs) {
3015 err = -ENOMEM;
3016 goto uninit_vcpu;
3017 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003018
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003019 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3020 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003021 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003022
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003023 vmx->vmcs = alloc_vmcs();
3024 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003025 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003026
3027 vmcs_clear(vmx->vmcs);
3028
Avi Kivity15ad7142007-07-11 18:17:21 +03003029 cpu = get_cpu();
3030 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003031 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003032 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003033 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003034 if (err)
3035 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003036 if (vm_need_virtualize_apic_accesses(kvm))
3037 if (alloc_apic_access_page(kvm) != 0)
3038 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003039
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003040 if (vm_need_ept())
3041 if (alloc_identity_pagetable(kvm) != 0)
3042 goto free_vmcs;
3043
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003044 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003045
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003046free_vmcs:
3047 free_vmcs(vmx->vmcs);
3048free_msrs:
3049 kfree(vmx->host_msrs);
3050free_guest_msrs:
3051 kfree(vmx->guest_msrs);
3052uninit_vcpu:
3053 kvm_vcpu_uninit(&vmx->vcpu);
3054free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003055 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003056 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003057}
3058
Yang, Sheng002c7f72007-07-31 14:23:01 +03003059static void __init vmx_check_processor_compat(void *rtn)
3060{
3061 struct vmcs_config vmcs_conf;
3062
3063 *(int *)rtn = 0;
3064 if (setup_vmcs_config(&vmcs_conf) < 0)
3065 *(int *)rtn = -EIO;
3066 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3067 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3068 smp_processor_id());
3069 *(int *)rtn = -EIO;
3070 }
3071}
3072
Sheng Yang67253af2008-04-25 10:20:22 +08003073static int get_ept_level(void)
3074{
3075 return VMX_EPT_DEFAULT_GAW + 1;
3076}
3077
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003078static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003079 .cpu_has_kvm_support = cpu_has_kvm_support,
3080 .disabled_by_bios = vmx_disabled_by_bios,
3081 .hardware_setup = hardware_setup,
3082 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003083 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003084 .hardware_enable = hardware_enable,
3085 .hardware_disable = hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003086 .cpu_has_accelerated_tpr = cpu_has_vmx_virtualize_apic_accesses,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003087
3088 .vcpu_create = vmx_create_vcpu,
3089 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003090 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003091
Avi Kivity04d2cc72007-09-10 18:10:54 +03003092 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003093 .vcpu_load = vmx_vcpu_load,
3094 .vcpu_put = vmx_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08003095 .vcpu_decache = vmx_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003096
3097 .set_guest_debug = set_guest_debug,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003098 .guest_debug_pre = kvm_guest_debug_pre,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003099 .get_msr = vmx_get_msr,
3100 .set_msr = vmx_set_msr,
3101 .get_segment_base = vmx_get_segment_base,
3102 .get_segment = vmx_get_segment,
3103 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003104 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003105 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003106 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003107 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003108 .set_cr3 = vmx_set_cr3,
3109 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003110 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003111 .get_idt = vmx_get_idt,
3112 .set_idt = vmx_set_idt,
3113 .get_gdt = vmx_get_gdt,
3114 .set_gdt = vmx_set_gdt,
3115 .cache_regs = vcpu_load_rsp_rip,
3116 .decache_regs = vcpu_put_rsp_rip,
3117 .get_rflags = vmx_get_rflags,
3118 .set_rflags = vmx_set_rflags,
3119
3120 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003121
Avi Kivity6aa8b732006-12-10 02:21:36 -08003122 .run = vmx_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003123 .handle_exit = kvm_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003124 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02003125 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003126 .get_irq = vmx_get_irq,
3127 .set_irq = vmx_inject_irq,
Avi Kivity298101d2007-11-25 13:41:11 +02003128 .queue_exception = vmx_queue_exception,
3129 .exception_injected = vmx_exception_injected,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003130 .inject_pending_irq = vmx_intr_assist,
3131 .inject_pending_vectors = do_interrupt_requests,
Izik Eiduscbc94022007-10-25 00:29:55 +02003132
3133 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003134 .get_tdp_level = get_ept_level,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003135};
3136
3137static int __init vmx_init(void)
3138{
Sheng Yang25c5f222008-03-28 13:18:56 +08003139 void *va;
He, Qingfdef3ad2007-04-30 09:45:24 +03003140 int r;
3141
3142 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3143 if (!vmx_io_bitmap_a)
3144 return -ENOMEM;
3145
3146 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3147 if (!vmx_io_bitmap_b) {
3148 r = -ENOMEM;
3149 goto out;
3150 }
3151
Sheng Yang25c5f222008-03-28 13:18:56 +08003152 vmx_msr_bitmap = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3153 if (!vmx_msr_bitmap) {
3154 r = -ENOMEM;
3155 goto out1;
3156 }
3157
He, Qingfdef3ad2007-04-30 09:45:24 +03003158 /*
3159 * Allow direct access to the PC debug port (it is often used for I/O
3160 * delays, but the vmexits simply slow things down).
3161 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003162 va = kmap(vmx_io_bitmap_a);
3163 memset(va, 0xff, PAGE_SIZE);
3164 clear_bit(0x80, va);
Avi Kivitycd0536d2007-05-08 11:34:07 +03003165 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003166
Sheng Yang25c5f222008-03-28 13:18:56 +08003167 va = kmap(vmx_io_bitmap_b);
3168 memset(va, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03003169 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03003170
Sheng Yang25c5f222008-03-28 13:18:56 +08003171 va = kmap(vmx_msr_bitmap);
3172 memset(va, 0xff, PAGE_SIZE);
3173 kunmap(vmx_msr_bitmap);
3174
Sheng Yang2384d2b2008-01-17 15:14:33 +08003175 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
3176
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003177 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003178 if (r)
Sheng Yang25c5f222008-03-28 13:18:56 +08003179 goto out2;
3180
3181 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_FS_BASE);
3182 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_GS_BASE);
3183 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_CS);
3184 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_ESP);
3185 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_EIP);
He, Qingfdef3ad2007-04-30 09:45:24 +03003186
Sheng Yang14394422008-04-28 12:24:45 +08003187 if (cpu_has_vmx_ept())
3188 bypass_guest_pf = 0;
3189
Avi Kivityc7addb92007-09-16 18:58:32 +02003190 if (bypass_guest_pf)
3191 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
3192
Sheng Yang14394422008-04-28 12:24:45 +08003193 ept_sync_global();
3194
He, Qingfdef3ad2007-04-30 09:45:24 +03003195 return 0;
3196
Sheng Yang25c5f222008-03-28 13:18:56 +08003197out2:
3198 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03003199out1:
3200 __free_page(vmx_io_bitmap_b);
3201out:
3202 __free_page(vmx_io_bitmap_a);
3203 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204}
3205
3206static void __exit vmx_exit(void)
3207{
Sheng Yang25c5f222008-03-28 13:18:56 +08003208 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03003209 __free_page(vmx_io_bitmap_b);
3210 __free_page(vmx_io_bitmap_a);
3211
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003212 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213}
3214
3215module_init(vmx_init)
3216module_exit(vmx_exit)