blob: 3312047664a436547dd6894701bd9f28b5f48242 [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"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040020
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020023#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/mm.h>
25#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040026#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020027#include <linux/moduleparam.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030028#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030029#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080032#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020033#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020034#include <asm/virtext.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080035
Avi Kivity4ecac3f2008-05-13 13:23:38 +030036#define __ex(x) __kvm_handle_fault_on_reboot(x)
37
Avi Kivity6aa8b732006-12-10 02:21:36 -080038MODULE_AUTHOR("Qumranet");
39MODULE_LICENSE("GPL");
40
Avi Kivityc7addb92007-09-16 18:58:32 +020041static int bypass_guest_pf = 1;
42module_param(bypass_guest_pf, bool, 0);
43
Sheng Yang2384d2b2008-01-17 15:14:33 +080044static int enable_vpid = 1;
45module_param(enable_vpid, bool, 0);
46
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020047static int flexpriority_enabled = 1;
48module_param(flexpriority_enabled, bool, 0);
49
Sheng Yang14394422008-04-28 12:24:45 +080050static int enable_ept = 1;
Sheng Yangd56f5462008-04-25 10:13:16 +080051module_param(enable_ept, bool, 0);
52
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030053static int emulate_invalid_guest_state = 0;
54module_param(emulate_invalid_guest_state, bool, 0);
55
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040056struct vmcs {
57 u32 revision_id;
58 u32 abort;
59 char data[0];
60};
61
62struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100063 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +030064 struct list_head local_vcpus_link;
Avi Kivity313dbd42008-07-17 18:04:30 +030065 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040066 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030067 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020068 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040069 struct kvm_msr_entry *guest_msrs;
70 struct kvm_msr_entry *host_msrs;
71 int nmsrs;
72 int save_nmsrs;
73 int msr_offset_efer;
74#ifdef CONFIG_X86_64
75 int msr_offset_kernel_gs_base;
76#endif
77 struct vmcs *vmcs;
78 struct {
79 int loaded;
80 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020081 int gs_ldt_reload_needed;
82 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030083 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040084 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +020085 struct {
86 struct {
87 bool pending;
88 u8 vector;
89 unsigned rip;
90 } irq;
91 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +080092 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030093 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +020094
95 /* Support for vnmi-less CPUs */
96 int soft_vnmi_blocked;
97 ktime_t entry_time;
98 s64 vnmi_blocked_time;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040099};
100
101static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
102{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000103 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400104}
105
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800106static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800107static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300108
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109static DEFINE_PER_CPU(struct vmcs *, vmxarea);
110static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300111static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112
He, Qingfdef3ad2007-04-30 09:45:24 +0300113static struct page *vmx_io_bitmap_a;
114static struct page *vmx_io_bitmap_b;
Sheng Yang25c5f222008-03-28 13:18:56 +0800115static struct page *vmx_msr_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300116
Sheng Yang2384d2b2008-01-17 15:14:33 +0800117static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
118static DEFINE_SPINLOCK(vmx_vpid_lock);
119
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300120static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121 int size;
122 int order;
123 u32 revision_id;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300124 u32 pin_based_exec_ctrl;
125 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800126 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300127 u32 vmexit_ctrl;
128 u32 vmentry_ctrl;
129} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130
Hannes Ederefff9e52008-11-28 17:02:06 +0100131static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800132 u32 ept;
133 u32 vpid;
134} vmx_capability;
135
Avi Kivity6aa8b732006-12-10 02:21:36 -0800136#define VMX_SEGMENT_FIELD(seg) \
137 [VCPU_SREG_##seg] = { \
138 .selector = GUEST_##seg##_SELECTOR, \
139 .base = GUEST_##seg##_BASE, \
140 .limit = GUEST_##seg##_LIMIT, \
141 .ar_bytes = GUEST_##seg##_AR_BYTES, \
142 }
143
144static struct kvm_vmx_segment_field {
145 unsigned selector;
146 unsigned base;
147 unsigned limit;
148 unsigned ar_bytes;
149} kvm_vmx_segment_fields[] = {
150 VMX_SEGMENT_FIELD(CS),
151 VMX_SEGMENT_FIELD(DS),
152 VMX_SEGMENT_FIELD(ES),
153 VMX_SEGMENT_FIELD(FS),
154 VMX_SEGMENT_FIELD(GS),
155 VMX_SEGMENT_FIELD(SS),
156 VMX_SEGMENT_FIELD(TR),
157 VMX_SEGMENT_FIELD(LDTR),
158};
159
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300160/*
161 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
162 * away by decrementing the array size.
163 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800165#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
167#endif
168 MSR_EFER, MSR_K6_STAR,
169};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200170#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400172static void load_msrs(struct kvm_msr_entry *e, int n)
173{
174 int i;
175
176 for (i = 0; i < n; ++i)
177 wrmsrl(e[i].index, e[i].data);
178}
179
180static void save_msrs(struct kvm_msr_entry *e, int n)
181{
182 int i;
183
184 for (i = 0; i < n; ++i)
185 rdmsrl(e[i].index, e[i].data);
186}
187
Avi Kivity6aa8b732006-12-10 02:21:36 -0800188static inline int is_page_fault(u32 intr_info)
189{
190 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
191 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100192 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193}
194
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300195static inline int is_no_device(u32 intr_info)
196{
197 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
198 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100199 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300200}
201
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500202static inline int is_invalid_opcode(u32 intr_info)
203{
204 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
205 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100206 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500207}
208
Avi Kivity6aa8b732006-12-10 02:21:36 -0800209static inline int is_external_interrupt(u32 intr_info)
210{
211 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
212 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
213}
214
Sheng Yang25c5f222008-03-28 13:18:56 +0800215static inline int cpu_has_vmx_msr_bitmap(void)
216{
217 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS);
218}
219
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800220static inline int cpu_has_vmx_tpr_shadow(void)
221{
222 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW);
223}
224
225static inline int vm_need_tpr_shadow(struct kvm *kvm)
226{
227 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)));
228}
229
Sheng Yangf78e0e22007-10-29 09:40:42 +0800230static inline int cpu_has_secondary_exec_ctrls(void)
231{
232 return (vmcs_config.cpu_based_exec_ctrl &
233 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS);
234}
235
Avi Kivity774ead32007-12-26 13:57:04 +0200236static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800237{
Avi Kivity4c9fc8e2008-03-24 18:15:14 +0200238 return flexpriority_enabled
239 && (vmcs_config.cpu_based_2nd_exec_ctrl &
240 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800241}
242
Sheng Yangd56f5462008-04-25 10:13:16 +0800243static inline int cpu_has_vmx_invept_individual_addr(void)
244{
245 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT));
246}
247
248static inline int cpu_has_vmx_invept_context(void)
249{
250 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT));
251}
252
253static inline int cpu_has_vmx_invept_global(void)
254{
255 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT));
256}
257
258static inline int cpu_has_vmx_ept(void)
259{
260 return (vmcs_config.cpu_based_2nd_exec_ctrl &
261 SECONDARY_EXEC_ENABLE_EPT);
262}
263
264static inline int vm_need_ept(void)
265{
266 return (cpu_has_vmx_ept() && enable_ept);
267}
268
Sheng Yangf78e0e22007-10-29 09:40:42 +0800269static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
270{
271 return ((cpu_has_vmx_virtualize_apic_accesses()) &&
272 (irqchip_in_kernel(kvm)));
273}
274
Sheng Yang2384d2b2008-01-17 15:14:33 +0800275static inline int cpu_has_vmx_vpid(void)
276{
277 return (vmcs_config.cpu_based_2nd_exec_ctrl &
278 SECONDARY_EXEC_ENABLE_VPID);
279}
280
Sheng Yangf08864b2008-05-15 18:23:25 +0800281static inline int cpu_has_virtual_nmis(void)
282{
283 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
284}
285
Rusty Russell8b9cf982007-07-30 16:31:43 +1000286static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800287{
288 int i;
289
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400290 for (i = 0; i < vmx->nmsrs; ++i)
291 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300292 return i;
293 return -1;
294}
295
Sheng Yang2384d2b2008-01-17 15:14:33 +0800296static inline void __invvpid(int ext, u16 vpid, gva_t gva)
297{
298 struct {
299 u64 vpid : 16;
300 u64 rsvd : 48;
301 u64 gva;
302 } operand = { vpid, 0, gva };
303
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300304 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800305 /* CF==1 or ZF==1 --> rc = -1 */
306 "; ja 1f ; ud2 ; 1:"
307 : : "a"(&operand), "c"(ext) : "cc", "memory");
308}
309
Sheng Yang14394422008-04-28 12:24:45 +0800310static inline void __invept(int ext, u64 eptp, gpa_t gpa)
311{
312 struct {
313 u64 eptp, gpa;
314 } operand = {eptp, gpa};
315
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300316 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800317 /* CF==1 or ZF==1 --> rc = -1 */
318 "; ja 1f ; ud2 ; 1:\n"
319 : : "a" (&operand), "c" (ext) : "cc", "memory");
320}
321
Rusty Russell8b9cf982007-07-30 16:31:43 +1000322static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300323{
324 int i;
325
Rusty Russell8b9cf982007-07-30 16:31:43 +1000326 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300327 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400328 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000329 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800330}
331
Avi Kivity6aa8b732006-12-10 02:21:36 -0800332static void vmcs_clear(struct vmcs *vmcs)
333{
334 u64 phys_addr = __pa(vmcs);
335 u8 error;
336
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300337 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
339 : "cc", "memory");
340 if (error)
341 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
342 vmcs, phys_addr);
343}
344
345static void __vcpu_clear(void *arg)
346{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000347 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800348 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800349
Rusty Russell8b9cf982007-07-30 16:31:43 +1000350 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400351 vmcs_clear(vmx->vmcs);
352 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800353 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800354 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300355 list_del(&vmx->local_vcpus_link);
356 vmx->vcpu.cpu = -1;
357 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800358}
359
Rusty Russell8b9cf982007-07-30 16:31:43 +1000360static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800361{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200362 if (vmx->vcpu.cpu == -1)
363 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200364 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800365}
366
Sheng Yang2384d2b2008-01-17 15:14:33 +0800367static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
368{
369 if (vmx->vpid == 0)
370 return;
371
372 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
373}
374
Sheng Yang14394422008-04-28 12:24:45 +0800375static inline void ept_sync_global(void)
376{
377 if (cpu_has_vmx_invept_global())
378 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
379}
380
381static inline void ept_sync_context(u64 eptp)
382{
383 if (vm_need_ept()) {
384 if (cpu_has_vmx_invept_context())
385 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
386 else
387 ept_sync_global();
388 }
389}
390
391static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
392{
393 if (vm_need_ept()) {
394 if (cpu_has_vmx_invept_individual_addr())
395 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
396 eptp, gpa);
397 else
398 ept_sync_context(eptp);
399 }
400}
401
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402static unsigned long vmcs_readl(unsigned long field)
403{
404 unsigned long value;
405
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300406 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800407 : "=a"(value) : "d"(field) : "cc");
408 return value;
409}
410
411static u16 vmcs_read16(unsigned long field)
412{
413 return vmcs_readl(field);
414}
415
416static u32 vmcs_read32(unsigned long field)
417{
418 return vmcs_readl(field);
419}
420
421static u64 vmcs_read64(unsigned long field)
422{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800423#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424 return vmcs_readl(field);
425#else
426 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
427#endif
428}
429
Avi Kivitye52de1b2007-01-05 16:36:56 -0800430static noinline void vmwrite_error(unsigned long field, unsigned long value)
431{
432 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
433 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
434 dump_stack();
435}
436
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437static void vmcs_writel(unsigned long field, unsigned long value)
438{
439 u8 error;
440
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300441 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400442 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800443 if (unlikely(error))
444 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445}
446
447static void vmcs_write16(unsigned long field, u16 value)
448{
449 vmcs_writel(field, value);
450}
451
452static void vmcs_write32(unsigned long field, u32 value)
453{
454 vmcs_writel(field, value);
455}
456
457static void vmcs_write64(unsigned long field, u64 value)
458{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300460#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461 asm volatile ("");
462 vmcs_writel(field+1, value >> 32);
463#endif
464}
465
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300466static void vmcs_clear_bits(unsigned long field, u32 mask)
467{
468 vmcs_writel(field, vmcs_readl(field) & ~mask);
469}
470
471static void vmcs_set_bits(unsigned long field, u32 mask)
472{
473 vmcs_writel(field, vmcs_readl(field) | mask);
474}
475
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300476static void update_exception_bitmap(struct kvm_vcpu *vcpu)
477{
478 u32 eb;
479
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500480 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300481 if (!vcpu->fpu_active)
482 eb |= 1u << NM_VECTOR;
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100483 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
484 if (vcpu->guest_debug &
485 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
486 eb |= 1u << DB_VECTOR;
487 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
488 eb |= 1u << BP_VECTOR;
489 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800490 if (vcpu->arch.rmode.active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300491 eb = ~0;
Sheng Yang14394422008-04-28 12:24:45 +0800492 if (vm_need_ept())
493 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300494 vmcs_write32(EXCEPTION_BITMAP, eb);
495}
496
Avi Kivity33ed6322007-05-02 16:54:03 +0300497static void reload_tss(void)
498{
Avi Kivity33ed6322007-05-02 16:54:03 +0300499 /*
500 * VT restores TR but not its size. Useless.
501 */
502 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200503 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300504
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300505 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300506 descs = (void *)gdt.base;
507 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
508 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300509}
510
Rusty Russell8b9cf982007-07-30 16:31:43 +1000511static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300512{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400513 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300514 u64 host_efer = vmx->host_msrs[efer_offset].data;
515 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
516 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300517
Avi Kivity51c6cf62007-08-29 03:48:05 +0300518 if (efer_offset < 0)
519 return;
520 /*
521 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
522 * outside long mode
523 */
524 ignore_bits = EFER_NX | EFER_SCE;
525#ifdef CONFIG_X86_64
526 ignore_bits |= EFER_LMA | EFER_LME;
527 /* SCE is meaningful only in long mode on Intel */
528 if (guest_efer & EFER_LMA)
529 ignore_bits &= ~(u64)EFER_SCE;
530#endif
531 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
532 return;
533
534 vmx->host_state.guest_efer_loaded = 1;
535 guest_efer &= ~ignore_bits;
536 guest_efer |= host_efer & ignore_bits;
537 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000538 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300539}
540
Avi Kivity51c6cf62007-08-29 03:48:05 +0300541static void reload_host_efer(struct vcpu_vmx *vmx)
542{
543 if (vmx->host_state.guest_efer_loaded) {
544 vmx->host_state.guest_efer_loaded = 0;
545 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
546 }
547}
548
Avi Kivity04d2cc72007-09-10 18:10:54 +0300549static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300550{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300551 struct vcpu_vmx *vmx = to_vmx(vcpu);
552
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400553 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300554 return;
555
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400556 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300557 /*
558 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
559 * allow segment selectors with cpl > 0 or ti == 1.
560 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300561 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200562 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300563 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200564 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400565 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200566 vmx->host_state.fs_reload_needed = 0;
567 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300568 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200569 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300570 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300571 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400572 if (!(vmx->host_state.gs_sel & 7))
573 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300574 else {
575 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200576 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300577 }
578
579#ifdef CONFIG_X86_64
580 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
581 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
582#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400583 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
584 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300585#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300586
587#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400588 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400589 save_msrs(vmx->host_msrs +
590 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400591
Avi Kivity707c0872007-05-02 17:33:43 +0300592#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400593 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300594 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300595}
596
Avi Kivitya9b21b62008-06-24 11:48:49 +0300597static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300598{
Avi Kivity15ad7142007-07-11 18:17:21 +0300599 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300600
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400601 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300602 return;
603
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200604 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400605 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200606 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300607 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200608 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300609 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300610 /*
611 * If we have to reload gs, we must take care to
612 * preserve our gs base.
613 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300614 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300615 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300616#ifdef CONFIG_X86_64
617 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
618#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300619 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300620 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200621 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400622 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
623 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300624 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300625}
626
Avi Kivitya9b21b62008-06-24 11:48:49 +0300627static void vmx_load_host_state(struct vcpu_vmx *vmx)
628{
629 preempt_disable();
630 __vmx_load_host_state(vmx);
631 preempt_enable();
632}
633
Avi Kivity6aa8b732006-12-10 02:21:36 -0800634/*
635 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
636 * vcpu mutex is already taken.
637 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300638static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400640 struct vcpu_vmx *vmx = to_vmx(vcpu);
641 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200642 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800643
Eddie Donga3d7f852007-09-03 16:15:12 +0300644 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000645 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300646 kvm_migrate_timers(vcpu);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800647 vpid_sync_vcpu_all(vmx);
Avi Kivity543e4242008-05-13 16:22:47 +0300648 local_irq_disable();
649 list_add(&vmx->local_vcpus_link,
650 &per_cpu(vcpus_on_cpu, cpu));
651 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300652 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400654 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800655 u8 error;
656
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400657 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300658 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800659 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
660 : "cc");
661 if (error)
662 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400663 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664 }
665
666 if (vcpu->cpu != cpu) {
667 struct descriptor_table dt;
668 unsigned long sysenter_esp;
669
670 vcpu->cpu = cpu;
671 /*
672 * Linux uses per-cpu TSS and GDT, so set these when switching
673 * processors.
674 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300675 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
676 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
678
679 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
680 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300681
682 /*
683 * Make sure the time stamp counter is monotonous.
684 */
685 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200686 if (tsc_this < vcpu->arch.host_tsc) {
687 delta = vcpu->arch.host_tsc - tsc_this;
688 new_offset = vmcs_read64(TSC_OFFSET) + delta;
689 vmcs_write64(TSC_OFFSET, new_offset);
690 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800692}
693
694static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
695{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300696 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800697}
698
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300699static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
700{
701 if (vcpu->fpu_active)
702 return;
703 vcpu->fpu_active = 1;
Rusty Russell707d92f2007-07-17 23:19:08 +1000704 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800705 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92f2007-07-17 23:19:08 +1000706 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300707 update_exception_bitmap(vcpu);
708}
709
710static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
711{
712 if (!vcpu->fpu_active)
713 return;
714 vcpu->fpu_active = 0;
Rusty Russell707d92f2007-07-17 23:19:08 +1000715 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300716 update_exception_bitmap(vcpu);
717}
718
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
720{
721 return vmcs_readl(GUEST_RFLAGS);
722}
723
724static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
725{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800726 if (vcpu->arch.rmode.active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100727 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728 vmcs_writel(GUEST_RFLAGS, rflags);
729}
730
731static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
732{
733 unsigned long rip;
734 u32 interruptibility;
735
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300736 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300738 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739
740 /*
741 * We emulated an instruction, so temporary interrupt blocking
742 * should be removed, if set.
743 */
744 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
745 if (interruptibility & 3)
746 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
747 interruptibility & ~3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800748 vcpu->arch.interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749}
750
Avi Kivity298101d2007-11-25 13:41:11 +0200751static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
752 bool has_error_code, u32 error_code)
753{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200754 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100755 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200756
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100757 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200758 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100759 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
760 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200761
762 if (vcpu->arch.rmode.active) {
763 vmx->rmode.irq.pending = true;
764 vmx->rmode.irq.vector = nr;
765 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100766 if (nr == BP_VECTOR || nr == OF_VECTOR)
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200767 vmx->rmode.irq.rip++;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100768 intr_info |= INTR_TYPE_SOFT_INTR;
769 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200770 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
771 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
772 return;
773 }
774
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100775 if (nr == BP_VECTOR || nr == OF_VECTOR) {
776 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
777 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
778 } else
779 intr_info |= INTR_TYPE_HARD_EXCEPTION;
780
781 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200782}
783
784static bool vmx_exception_injected(struct kvm_vcpu *vcpu)
785{
Avi Kivity35920a32008-07-03 14:50:12 +0300786 return false;
Avi Kivity298101d2007-11-25 13:41:11 +0200787}
788
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789/*
Eddie Donga75beee2007-05-17 18:55:15 +0300790 * Swap MSR entry in host/guest MSR entry array.
791 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200792#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000793static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300794{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400795 struct kvm_msr_entry tmp;
796
797 tmp = vmx->guest_msrs[to];
798 vmx->guest_msrs[to] = vmx->guest_msrs[from];
799 vmx->guest_msrs[from] = tmp;
800 tmp = vmx->host_msrs[to];
801 vmx->host_msrs[to] = vmx->host_msrs[from];
802 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300803}
Gabriel C54e11fa2007-08-01 16:23:10 +0200804#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300805
806/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300807 * Set up the vmcs to automatically save and restore system
808 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
809 * mode, as fiddling with msrs is very expensive.
810 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000811static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300812{
Eddie Dong2cc51562007-05-21 07:28:09 +0300813 int save_nmsrs;
Avi Kivitye38aea32007-04-19 13:22:48 +0300814
Avi Kivity33f9c502008-02-27 16:06:57 +0200815 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300816 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300817#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000818 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300819 int index;
820
Rusty Russell8b9cf982007-07-30 16:31:43 +1000821 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300822 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000823 move_msr_up(vmx, index, save_nmsrs++);
824 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300825 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000826 move_msr_up(vmx, index, save_nmsrs++);
827 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300828 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000829 move_msr_up(vmx, index, save_nmsrs++);
830 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300831 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000832 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300833 /*
834 * MSR_K6_STAR is only needed on long mode guests, and only
835 * if efer.sce is enabled.
836 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000837 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800838 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000839 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300840 }
Eddie Donga75beee2007-05-17 18:55:15 +0300841#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400842 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300843
Eddie Donga75beee2007-05-17 18:55:15 +0300844#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400845 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000846 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300847#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000848 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivitye38aea32007-04-19 13:22:48 +0300849}
850
851/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852 * reads and returns guest's timestamp counter "register"
853 * guest_tsc = host_tsc + tsc_offset -- 21.3
854 */
855static u64 guest_read_tsc(void)
856{
857 u64 host_tsc, tsc_offset;
858
859 rdtscll(host_tsc);
860 tsc_offset = vmcs_read64(TSC_OFFSET);
861 return host_tsc + tsc_offset;
862}
863
864/*
865 * writes 'guest_tsc' into guest's timestamp counter "register"
866 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
867 */
Marcelo Tosatti53f658b2008-12-11 20:45:05 +0100868static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
871}
872
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873/*
874 * Reads an msr value (of 'msr_index') into 'pdata'.
875 * Returns 0 on success, non-0 otherwise.
876 * Assumes vcpu_load() was already called.
877 */
878static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
879{
880 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400881 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800882
883 if (!pdata) {
884 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
885 return -EINVAL;
886 }
887
888 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800889#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 case MSR_FS_BASE:
891 data = vmcs_readl(GUEST_FS_BASE);
892 break;
893 case MSR_GS_BASE:
894 data = vmcs_readl(GUEST_GS_BASE);
895 break;
896 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800897 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898#endif
899 case MSR_IA32_TIME_STAMP_COUNTER:
900 data = guest_read_tsc();
901 break;
902 case MSR_IA32_SYSENTER_CS:
903 data = vmcs_read32(GUEST_SYSENTER_CS);
904 break;
905 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200906 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907 break;
908 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200909 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911 default:
Avi Kivity516a1a72009-02-15 02:32:07 +0200912 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +1000913 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800914 if (msr) {
915 data = msr->data;
916 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800918 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 }
920
921 *pdata = data;
922 return 0;
923}
924
925/*
926 * Writes msr value into into the appropriate "register".
927 * Returns 0 on success, non-0 otherwise.
928 * Assumes vcpu_load() was already called.
929 */
930static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
931{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400932 struct vcpu_vmx *vmx = to_vmx(vcpu);
933 struct kvm_msr_entry *msr;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +0100934 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +0300935 int ret = 0;
936
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800938#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800939 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300940 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +0300941 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +0300942 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 case MSR_FS_BASE:
944 vmcs_writel(GUEST_FS_BASE, data);
945 break;
946 case MSR_GS_BASE:
947 vmcs_writel(GUEST_GS_BASE, data);
948 break;
949#endif
950 case MSR_IA32_SYSENTER_CS:
951 vmcs_write32(GUEST_SYSENTER_CS, data);
952 break;
953 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200954 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955 break;
956 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200957 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200959 case MSR_IA32_TIME_STAMP_COUNTER:
Marcelo Tosatti53f658b2008-12-11 20:45:05 +0100960 rdtscll(host_tsc);
961 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 break;
Chris Lalancetteefa67e02008-06-20 09:51:30 +0200963 case MSR_P6_PERFCTR0:
964 case MSR_P6_PERFCTR1:
965 case MSR_P6_EVNTSEL0:
966 case MSR_P6_EVNTSEL1:
967 /*
968 * Just discard all writes to the performance counters; this
969 * should keep both older linux and windows 64-bit guests
970 * happy
971 */
972 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", msr_index, data);
973
974 break;
Sheng Yang468d4722008-10-09 16:01:55 +0800975 case MSR_IA32_CR_PAT:
976 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
977 vmcs_write64(GUEST_IA32_PAT, data);
978 vcpu->arch.pat = data;
979 break;
980 }
981 /* Otherwise falls through to kvm_set_msr_common */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982 default:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300983 vmx_load_host_state(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000984 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800985 if (msr) {
986 msr->data = data;
987 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300989 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990 }
991
Eddie Dong2cc51562007-05-21 07:28:09 +0300992 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993}
994
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300995static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300997 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
998 switch (reg) {
999 case VCPU_REGS_RSP:
1000 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1001 break;
1002 case VCPU_REGS_RIP:
1003 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1004 break;
1005 default:
1006 break;
1007 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008}
1009
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001010static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001012 int old_debug = vcpu->guest_debug;
1013 unsigned long flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001015 vcpu->guest_debug = dbg->control;
1016 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
1017 vcpu->guest_debug = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001019 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1020 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1021 else
1022 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1023
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001024 flags = vmcs_readl(GUEST_RFLAGS);
1025 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
1026 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1027 else if (old_debug & KVM_GUESTDBG_SINGLESTEP)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001029 vmcs_writel(GUEST_RFLAGS, flags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001031 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032
1033 return 0;
1034}
1035
Eddie Dong2a8067f2007-08-06 16:29:07 +03001036static int vmx_get_irq(struct kvm_vcpu *vcpu)
1037{
Avi Kivityf7d92382008-07-03 16:14:28 +03001038 if (!vcpu->arch.interrupt.pending)
1039 return -1;
1040 return vcpu->arch.interrupt.nr;
Eddie Dong2a8067f2007-08-06 16:29:07 +03001041}
1042
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043static __init int cpu_has_kvm_support(void)
1044{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001045 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046}
1047
1048static __init int vmx_disabled_by_bios(void)
1049{
1050 u64 msr;
1051
1052 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001053 return (msr & (FEATURE_CONTROL_LOCKED |
1054 FEATURE_CONTROL_VMXON_ENABLED))
1055 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001056 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057}
1058
Avi Kivity774c47f2007-02-12 00:54:47 -08001059static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060{
1061 int cpu = raw_smp_processor_id();
1062 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1063 u64 old;
1064
Avi Kivity543e4242008-05-13 16:22:47 +03001065 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001067 if ((old & (FEATURE_CONTROL_LOCKED |
1068 FEATURE_CONTROL_VMXON_ENABLED))
1069 != (FEATURE_CONTROL_LOCKED |
1070 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001072 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001073 FEATURE_CONTROL_LOCKED |
1074 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001075 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001076 asm volatile (ASM_VMX_VMXON_RAX
1077 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078 : "memory", "cc");
1079}
1080
Avi Kivity543e4242008-05-13 16:22:47 +03001081static void vmclear_local_vcpus(void)
1082{
1083 int cpu = raw_smp_processor_id();
1084 struct vcpu_vmx *vmx, *n;
1085
1086 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1087 local_vcpus_link)
1088 __vcpu_clear(vmx);
1089}
1090
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001091
1092/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1093 * tricks.
1094 */
1095static void kvm_cpu_vmxoff(void)
1096{
1097 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1098 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1099}
1100
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101static void hardware_disable(void *garbage)
1102{
Avi Kivity543e4242008-05-13 16:22:47 +03001103 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001104 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105}
1106
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001107static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001108 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109{
1110 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001111 u32 ctl = ctl_min | ctl_opt;
1112
1113 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1114
1115 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1116 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1117
1118 /* Ensure minimum (required) set of control bits are supported. */
1119 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001120 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001121
1122 *result = ctl;
1123 return 0;
1124}
1125
Yang, Sheng002c7f72007-07-31 14:23:01 +03001126static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001127{
1128 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001129 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001130 u32 _pin_based_exec_control = 0;
1131 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001132 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001133 u32 _vmexit_control = 0;
1134 u32 _vmentry_control = 0;
1135
1136 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001137 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001138 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1139 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001140 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001141
1142 min = CPU_BASED_HLT_EXITING |
1143#ifdef CONFIG_X86_64
1144 CPU_BASED_CR8_LOAD_EXITING |
1145 CPU_BASED_CR8_STORE_EXITING |
1146#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001147 CPU_BASED_CR3_LOAD_EXITING |
1148 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001149 CPU_BASED_USE_IO_BITMAPS |
1150 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001151 CPU_BASED_USE_TSC_OFFSETING |
1152 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001153 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001154 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001155 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001156 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1157 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001158 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001159#ifdef CONFIG_X86_64
1160 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1161 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1162 ~CPU_BASED_CR8_STORE_EXITING;
1163#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001164 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001165 min2 = 0;
1166 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001167 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001168 SECONDARY_EXEC_ENABLE_VPID |
1169 SECONDARY_EXEC_ENABLE_EPT;
1170 if (adjust_vmx_controls(min2, opt2,
1171 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001172 &_cpu_based_2nd_exec_control) < 0)
1173 return -EIO;
1174 }
1175#ifndef CONFIG_X86_64
1176 if (!(_cpu_based_2nd_exec_control &
1177 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1178 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1179#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001180 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001181 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1182 enabled */
Sheng Yangd56f5462008-04-25 10:13:16 +08001183 min &= ~(CPU_BASED_CR3_LOAD_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001184 CPU_BASED_CR3_STORE_EXITING |
1185 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001186 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1187 &_cpu_based_exec_control) < 0)
1188 return -EIO;
1189 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1190 vmx_capability.ept, vmx_capability.vpid);
1191 }
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001192
1193 min = 0;
1194#ifdef CONFIG_X86_64
1195 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1196#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001197 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001198 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1199 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001200 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001201
Sheng Yang468d4722008-10-09 16:01:55 +08001202 min = 0;
1203 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001204 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1205 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001206 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001208 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001209
1210 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1211 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001212 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001213
1214#ifdef CONFIG_X86_64
1215 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1216 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001217 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001218#endif
1219
1220 /* Require Write-Back (WB) memory type for VMCS accesses. */
1221 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001222 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001223
Yang, Sheng002c7f72007-07-31 14:23:01 +03001224 vmcs_conf->size = vmx_msr_high & 0x1fff;
1225 vmcs_conf->order = get_order(vmcs_config.size);
1226 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001227
Yang, Sheng002c7f72007-07-31 14:23:01 +03001228 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1229 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001230 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001231 vmcs_conf->vmexit_ctrl = _vmexit_control;
1232 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001233
1234 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001235}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236
1237static struct vmcs *alloc_vmcs_cpu(int cpu)
1238{
1239 int node = cpu_to_node(cpu);
1240 struct page *pages;
1241 struct vmcs *vmcs;
1242
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001243 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244 if (!pages)
1245 return NULL;
1246 vmcs = page_address(pages);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001247 memset(vmcs, 0, vmcs_config.size);
1248 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249 return vmcs;
1250}
1251
1252static struct vmcs *alloc_vmcs(void)
1253{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001254 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255}
1256
1257static void free_vmcs(struct vmcs *vmcs)
1258{
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001259 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260}
1261
Sam Ravnborg39959582007-06-01 00:47:13 -07001262static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001263{
1264 int cpu;
1265
1266 for_each_online_cpu(cpu)
1267 free_vmcs(per_cpu(vmxarea, cpu));
1268}
1269
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270static __init int alloc_kvm_area(void)
1271{
1272 int cpu;
1273
1274 for_each_online_cpu(cpu) {
1275 struct vmcs *vmcs;
1276
1277 vmcs = alloc_vmcs_cpu(cpu);
1278 if (!vmcs) {
1279 free_kvm_area();
1280 return -ENOMEM;
1281 }
1282
1283 per_cpu(vmxarea, cpu) = vmcs;
1284 }
1285 return 0;
1286}
1287
1288static __init int hardware_setup(void)
1289{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001290 if (setup_vmcs_config(&vmcs_config) < 0)
1291 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001292
1293 if (boot_cpu_has(X86_FEATURE_NX))
1294 kvm_enable_efer_bits(EFER_NX);
1295
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296 return alloc_kvm_area();
1297}
1298
1299static __exit void hardware_unsetup(void)
1300{
1301 free_kvm_area();
1302}
1303
Avi Kivity6aa8b732006-12-10 02:21:36 -08001304static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1305{
1306 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1307
Avi Kivity6af11b92007-03-19 13:18:10 +02001308 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309 vmcs_write16(sf->selector, save->selector);
1310 vmcs_writel(sf->base, save->base);
1311 vmcs_write32(sf->limit, save->limit);
1312 vmcs_write32(sf->ar_bytes, save->ar);
1313 } else {
1314 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1315 << AR_DPL_SHIFT;
1316 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1317 }
1318}
1319
1320static void enter_pmode(struct kvm_vcpu *vcpu)
1321{
1322 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001323 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001325 vmx->emulation_required = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001326 vcpu->arch.rmode.active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001328 vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base);
1329 vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit);
1330 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331
1332 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001333 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001334 flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335 vmcs_writel(GUEST_RFLAGS, flags);
1336
Rusty Russell66aee912007-07-17 23:34:16 +10001337 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1338 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339
1340 update_exception_bitmap(vcpu);
1341
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001342 if (emulate_invalid_guest_state)
1343 return;
1344
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001345 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1346 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1347 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1348 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001349
1350 vmcs_write16(GUEST_SS_SELECTOR, 0);
1351 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1352
1353 vmcs_write16(GUEST_CS_SELECTOR,
1354 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1355 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1356}
1357
Mike Dayd77c26f2007-10-08 09:02:08 -04001358static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001360 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001361 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1362 kvm->memslots[0].npages - 3;
1363 return base_gfn << PAGE_SHIFT;
1364 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001365 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366}
1367
1368static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1369{
1370 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1371
1372 save->selector = vmcs_read16(sf->selector);
1373 save->base = vmcs_readl(sf->base);
1374 save->limit = vmcs_read32(sf->limit);
1375 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001376 vmcs_write16(sf->selector, save->base >> 4);
1377 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378 vmcs_write32(sf->limit, 0xffff);
1379 vmcs_write32(sf->ar_bytes, 0xf3);
1380}
1381
1382static void enter_rmode(struct kvm_vcpu *vcpu)
1383{
1384 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001385 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001387 vmx->emulation_required = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001388 vcpu->arch.rmode.active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001390 vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1392
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001393 vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1395
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001396 vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1398
1399 flags = vmcs_readl(GUEST_RFLAGS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001400 vcpu->arch.rmode.save_iopl
1401 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001403 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001404
1405 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001406 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407 update_exception_bitmap(vcpu);
1408
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001409 if (emulate_invalid_guest_state)
1410 goto continue_rmode;
1411
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1413 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1414 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1415
1416 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001417 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001418 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1419 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1421
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001422 fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1423 fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1424 fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1425 fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001426
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001427continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001428 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001429 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001430}
1431
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001432#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001433
1434static void enter_lmode(struct kvm_vcpu *vcpu)
1435{
1436 u32 guest_tr_ar;
1437
1438 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1439 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1440 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001441 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001442 vmcs_write32(GUEST_TR_AR_BYTES,
1443 (guest_tr_ar & ~AR_TYPE_MASK)
1444 | AR_TYPE_BUSY_64_TSS);
1445 }
1446
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001447 vcpu->arch.shadow_efer |= EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001448
Rusty Russell8b9cf982007-07-30 16:31:43 +10001449 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450 vmcs_write32(VM_ENTRY_CONTROLS,
1451 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001452 | VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453}
1454
1455static void exit_lmode(struct kvm_vcpu *vcpu)
1456{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001457 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458
1459 vmcs_write32(VM_ENTRY_CONTROLS,
1460 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001461 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462}
1463
1464#endif
1465
Sheng Yang2384d2b2008-01-17 15:14:33 +08001466static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1467{
1468 vpid_sync_vcpu_all(to_vmx(vcpu));
Sheng Yang4e1096d2008-07-06 19:16:51 +08001469 if (vm_need_ept())
1470 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001471}
1472
Anthony Liguori25c4c272007-04-27 09:29:21 +03001473static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001474{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001475 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1476 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001477}
1478
Sheng Yang14394422008-04-28 12:24:45 +08001479static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1480{
1481 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1482 if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
1483 printk(KERN_ERR "EPT: Fail to load pdptrs!\n");
1484 return;
1485 }
1486 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1487 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1488 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1489 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1490 }
1491}
1492
1493static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1494
1495static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1496 unsigned long cr0,
1497 struct kvm_vcpu *vcpu)
1498{
1499 if (!(cr0 & X86_CR0_PG)) {
1500 /* From paging/starting to nonpaging */
1501 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001502 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001503 (CPU_BASED_CR3_LOAD_EXITING |
1504 CPU_BASED_CR3_STORE_EXITING));
1505 vcpu->arch.cr0 = cr0;
1506 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1507 *hw_cr0 |= X86_CR0_PE | X86_CR0_PG;
1508 *hw_cr0 &= ~X86_CR0_WP;
1509 } else if (!is_paging(vcpu)) {
1510 /* From nonpaging to paging */
1511 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001512 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001513 ~(CPU_BASED_CR3_LOAD_EXITING |
1514 CPU_BASED_CR3_STORE_EXITING));
1515 vcpu->arch.cr0 = cr0;
1516 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1517 if (!(vcpu->arch.cr0 & X86_CR0_WP))
1518 *hw_cr0 &= ~X86_CR0_WP;
1519 }
1520}
1521
1522static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1523 struct kvm_vcpu *vcpu)
1524{
1525 if (!is_paging(vcpu)) {
1526 *hw_cr4 &= ~X86_CR4_PAE;
1527 *hw_cr4 |= X86_CR4_PSE;
1528 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1529 *hw_cr4 &= ~X86_CR4_PAE;
1530}
1531
Avi Kivity6aa8b732006-12-10 02:21:36 -08001532static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1533{
Sheng Yang14394422008-04-28 12:24:45 +08001534 unsigned long hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) |
1535 KVM_VM_CR0_ALWAYS_ON;
1536
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001537 vmx_fpu_deactivate(vcpu);
1538
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001539 if (vcpu->arch.rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001540 enter_pmode(vcpu);
1541
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001542 if (!vcpu->arch.rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001543 enter_rmode(vcpu);
1544
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001545#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001546 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001547 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001548 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10001549 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001550 exit_lmode(vcpu);
1551 }
1552#endif
1553
Sheng Yang14394422008-04-28 12:24:45 +08001554 if (vm_need_ept())
1555 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1556
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001558 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001559 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001560
Rusty Russell707d92f2007-07-17 23:19:08 +10001561 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001562 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001563}
1564
Sheng Yang14394422008-04-28 12:24:45 +08001565static u64 construct_eptp(unsigned long root_hpa)
1566{
1567 u64 eptp;
1568
1569 /* TODO write the value reading from MSR */
1570 eptp = VMX_EPT_DEFAULT_MT |
1571 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1572 eptp |= (root_hpa & PAGE_MASK);
1573
1574 return eptp;
1575}
1576
Avi Kivity6aa8b732006-12-10 02:21:36 -08001577static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1578{
Sheng Yang14394422008-04-28 12:24:45 +08001579 unsigned long guest_cr3;
1580 u64 eptp;
1581
1582 guest_cr3 = cr3;
1583 if (vm_need_ept()) {
1584 eptp = construct_eptp(cr3);
1585 vmcs_write64(EPT_POINTER, eptp);
1586 ept_sync_context(eptp);
1587 ept_load_pdptrs(vcpu);
1588 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
1589 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1590 }
1591
Sheng Yang2384d2b2008-01-17 15:14:33 +08001592 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001593 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001594 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001595 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596}
1597
1598static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1599{
Sheng Yang14394422008-04-28 12:24:45 +08001600 unsigned long hw_cr4 = cr4 | (vcpu->arch.rmode.active ?
1601 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1602
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001603 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08001604 if (vm_need_ept())
1605 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1606
1607 vmcs_writel(CR4_READ_SHADOW, cr4);
1608 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609}
1610
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1612{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001613 struct vcpu_vmx *vmx = to_vmx(vcpu);
1614 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001616 vcpu->arch.shadow_efer = efer;
Joerg Roedel9f62e192008-01-31 14:57:39 +01001617 if (!msr)
1618 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619 if (efer & EFER_LMA) {
1620 vmcs_write32(VM_ENTRY_CONTROLS,
1621 vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001622 VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001623 msr->data = efer;
1624
1625 } else {
1626 vmcs_write32(VM_ENTRY_CONTROLS,
1627 vmcs_read32(VM_ENTRY_CONTROLS) &
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001628 ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629
1630 msr->data = efer & ~EFER_LME;
1631 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001632 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633}
1634
Avi Kivity6aa8b732006-12-10 02:21:36 -08001635static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1636{
1637 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1638
1639 return vmcs_readl(sf->base);
1640}
1641
1642static void vmx_get_segment(struct kvm_vcpu *vcpu,
1643 struct kvm_segment *var, int seg)
1644{
1645 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1646 u32 ar;
1647
1648 var->base = vmcs_readl(sf->base);
1649 var->limit = vmcs_read32(sf->limit);
1650 var->selector = vmcs_read16(sf->selector);
1651 ar = vmcs_read32(sf->ar_bytes);
1652 if (ar & AR_UNUSABLE_MASK)
1653 ar = 0;
1654 var->type = ar & 15;
1655 var->s = (ar >> 4) & 1;
1656 var->dpl = (ar >> 5) & 3;
1657 var->present = (ar >> 7) & 1;
1658 var->avl = (ar >> 12) & 1;
1659 var->l = (ar >> 13) & 1;
1660 var->db = (ar >> 14) & 1;
1661 var->g = (ar >> 15) & 1;
1662 var->unusable = (ar >> 16) & 1;
1663}
1664
Izik Eidus2e4d2652008-03-24 19:38:34 +02001665static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1666{
1667 struct kvm_segment kvm_seg;
1668
1669 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1670 return 0;
1671
1672 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1673 return 3;
1674
1675 vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
1676 return kvm_seg.selector & 3;
1677}
1678
Avi Kivity653e3102007-05-07 10:55:37 +03001679static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001681 u32 ar;
1682
Avi Kivity653e3102007-05-07 10:55:37 +03001683 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001684 ar = 1 << 16;
1685 else {
1686 ar = var->type & 15;
1687 ar |= (var->s & 1) << 4;
1688 ar |= (var->dpl & 3) << 5;
1689 ar |= (var->present & 1) << 7;
1690 ar |= (var->avl & 1) << 12;
1691 ar |= (var->l & 1) << 13;
1692 ar |= (var->db & 1) << 14;
1693 ar |= (var->g & 1) << 15;
1694 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001695 if (ar == 0) /* a 0 value means unusable */
1696 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001697
1698 return ar;
1699}
1700
1701static void vmx_set_segment(struct kvm_vcpu *vcpu,
1702 struct kvm_segment *var, int seg)
1703{
1704 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1705 u32 ar;
1706
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001707 if (vcpu->arch.rmode.active && seg == VCPU_SREG_TR) {
1708 vcpu->arch.rmode.tr.selector = var->selector;
1709 vcpu->arch.rmode.tr.base = var->base;
1710 vcpu->arch.rmode.tr.limit = var->limit;
1711 vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001712 return;
1713 }
1714 vmcs_writel(sf->base, var->base);
1715 vmcs_write32(sf->limit, var->limit);
1716 vmcs_write16(sf->selector, var->selector);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001717 if (vcpu->arch.rmode.active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001718 /*
1719 * Hack real-mode segments into vm86 compatibility.
1720 */
1721 if (var->base == 0xffff0000 && var->selector == 0xf000)
1722 vmcs_writel(sf->base, 0xf0000);
1723 ar = 0xf3;
1724 } else
1725 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726 vmcs_write32(sf->ar_bytes, ar);
1727}
1728
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1730{
1731 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1732
1733 *db = (ar >> 14) & 1;
1734 *l = (ar >> 13) & 1;
1735}
1736
1737static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1738{
1739 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1740 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1741}
1742
1743static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1744{
1745 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1746 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1747}
1748
1749static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1750{
1751 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1752 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1753}
1754
1755static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1756{
1757 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1758 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1759}
1760
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001761static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1762{
1763 struct kvm_segment var;
1764 u32 ar;
1765
1766 vmx_get_segment(vcpu, &var, seg);
1767 ar = vmx_segment_access_rights(&var);
1768
1769 if (var.base != (var.selector << 4))
1770 return false;
1771 if (var.limit != 0xffff)
1772 return false;
1773 if (ar != 0xf3)
1774 return false;
1775
1776 return true;
1777}
1778
1779static bool code_segment_valid(struct kvm_vcpu *vcpu)
1780{
1781 struct kvm_segment cs;
1782 unsigned int cs_rpl;
1783
1784 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1785 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1786
1787 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1788 return false;
1789 if (!cs.s)
1790 return false;
1791 if (!(~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK))) {
1792 if (cs.dpl > cs_rpl)
1793 return false;
1794 } else if (cs.type & AR_TYPE_CODE_MASK) {
1795 if (cs.dpl != cs_rpl)
1796 return false;
1797 }
1798 if (!cs.present)
1799 return false;
1800
1801 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1802 return true;
1803}
1804
1805static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1806{
1807 struct kvm_segment ss;
1808 unsigned int ss_rpl;
1809
1810 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1811 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1812
1813 if ((ss.type != 3) || (ss.type != 7))
1814 return false;
1815 if (!ss.s)
1816 return false;
1817 if (ss.dpl != ss_rpl) /* DPL != RPL */
1818 return false;
1819 if (!ss.present)
1820 return false;
1821
1822 return true;
1823}
1824
1825static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1826{
1827 struct kvm_segment var;
1828 unsigned int rpl;
1829
1830 vmx_get_segment(vcpu, &var, seg);
1831 rpl = var.selector & SELECTOR_RPL_MASK;
1832
1833 if (!var.s)
1834 return false;
1835 if (!var.present)
1836 return false;
1837 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
1838 if (var.dpl < rpl) /* DPL < RPL */
1839 return false;
1840 }
1841
1842 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1843 * rights flags
1844 */
1845 return true;
1846}
1847
1848static bool tr_valid(struct kvm_vcpu *vcpu)
1849{
1850 struct kvm_segment tr;
1851
1852 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
1853
1854 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1855 return false;
1856 if ((tr.type != 3) || (tr.type != 11)) /* TODO: Check if guest is in IA32e mode */
1857 return false;
1858 if (!tr.present)
1859 return false;
1860
1861 return true;
1862}
1863
1864static bool ldtr_valid(struct kvm_vcpu *vcpu)
1865{
1866 struct kvm_segment ldtr;
1867
1868 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
1869
1870 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1871 return false;
1872 if (ldtr.type != 2)
1873 return false;
1874 if (!ldtr.present)
1875 return false;
1876
1877 return true;
1878}
1879
1880static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
1881{
1882 struct kvm_segment cs, ss;
1883
1884 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1885 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1886
1887 return ((cs.selector & SELECTOR_RPL_MASK) ==
1888 (ss.selector & SELECTOR_RPL_MASK));
1889}
1890
1891/*
1892 * Check if guest state is valid. Returns true if valid, false if
1893 * not.
1894 * We assume that registers are always usable
1895 */
1896static bool guest_state_valid(struct kvm_vcpu *vcpu)
1897{
1898 /* real mode guest state checks */
1899 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
1900 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
1901 return false;
1902 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
1903 return false;
1904 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
1905 return false;
1906 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
1907 return false;
1908 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
1909 return false;
1910 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
1911 return false;
1912 } else {
1913 /* protected mode guest state checks */
1914 if (!cs_ss_rpl_check(vcpu))
1915 return false;
1916 if (!code_segment_valid(vcpu))
1917 return false;
1918 if (!stack_segment_valid(vcpu))
1919 return false;
1920 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
1921 return false;
1922 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
1923 return false;
1924 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
1925 return false;
1926 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
1927 return false;
1928 if (!tr_valid(vcpu))
1929 return false;
1930 if (!ldtr_valid(vcpu))
1931 return false;
1932 }
1933 /* TODO:
1934 * - Add checks on RIP
1935 * - Add checks on RFLAGS
1936 */
1937
1938 return true;
1939}
1940
Mike Dayd77c26f2007-10-08 09:02:08 -04001941static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001944 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001945 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02001946 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001947
Izik Eidus195aefd2007-10-01 22:14:18 +02001948 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1949 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001950 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001951 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08001952 r = kvm_write_guest_page(kvm, fn++, &data,
1953 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02001954 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001955 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001956 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
1957 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001958 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001959 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1960 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001961 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001962 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001963 r = kvm_write_guest_page(kvm, fn, &data,
1964 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
1965 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02001966 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001967 goto out;
1968
1969 ret = 1;
1970out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001971 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001972}
1973
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001974static int init_rmode_identity_map(struct kvm *kvm)
1975{
1976 int i, r, ret;
1977 pfn_t identity_map_pfn;
1978 u32 tmp;
1979
1980 if (!vm_need_ept())
1981 return 1;
1982 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
1983 printk(KERN_ERR "EPT: identity-mapping pagetable "
1984 "haven't been allocated!\n");
1985 return 0;
1986 }
1987 if (likely(kvm->arch.ept_identity_pagetable_done))
1988 return 1;
1989 ret = 0;
1990 identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT;
1991 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
1992 if (r < 0)
1993 goto out;
1994 /* Set up identity-mapping pagetable for EPT in real mode */
1995 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
1996 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
1997 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
1998 r = kvm_write_guest_page(kvm, identity_map_pfn,
1999 &tmp, i * sizeof(tmp), sizeof(tmp));
2000 if (r < 0)
2001 goto out;
2002 }
2003 kvm->arch.ept_identity_pagetable_done = true;
2004 ret = 1;
2005out:
2006 return ret;
2007}
2008
Avi Kivity6aa8b732006-12-10 02:21:36 -08002009static void seg_setup(int seg)
2010{
2011 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2012
2013 vmcs_write16(sf->selector, 0);
2014 vmcs_writel(sf->base, 0);
2015 vmcs_write32(sf->limit, 0xffff);
Avi Kivitya16b20d2008-08-20 15:48:27 +03002016 vmcs_write32(sf->ar_bytes, 0xf3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002017}
2018
Sheng Yangf78e0e22007-10-29 09:40:42 +08002019static int alloc_apic_access_page(struct kvm *kvm)
2020{
2021 struct kvm_userspace_memory_region kvm_userspace_mem;
2022 int r = 0;
2023
Izik Eidus72dc67a2008-02-10 18:04:15 +02002024 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002025 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002026 goto out;
2027 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2028 kvm_userspace_mem.flags = 0;
2029 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2030 kvm_userspace_mem.memory_size = PAGE_SIZE;
2031 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2032 if (r)
2033 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002034
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002035 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002036out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002037 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002038 return r;
2039}
2040
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002041static int alloc_identity_pagetable(struct kvm *kvm)
2042{
2043 struct kvm_userspace_memory_region kvm_userspace_mem;
2044 int r = 0;
2045
2046 down_write(&kvm->slots_lock);
2047 if (kvm->arch.ept_identity_pagetable)
2048 goto out;
2049 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2050 kvm_userspace_mem.flags = 0;
2051 kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
2052 kvm_userspace_mem.memory_size = PAGE_SIZE;
2053 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2054 if (r)
2055 goto out;
2056
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002057 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
2058 VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002059out:
2060 up_write(&kvm->slots_lock);
2061 return r;
2062}
2063
Sheng Yang2384d2b2008-01-17 15:14:33 +08002064static void allocate_vpid(struct vcpu_vmx *vmx)
2065{
2066 int vpid;
2067
2068 vmx->vpid = 0;
2069 if (!enable_vpid || !cpu_has_vmx_vpid())
2070 return;
2071 spin_lock(&vmx_vpid_lock);
2072 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2073 if (vpid < VMX_NR_VPIDS) {
2074 vmx->vpid = vpid;
2075 __set_bit(vpid, vmx_vpid_bitmap);
2076 }
2077 spin_unlock(&vmx_vpid_lock);
2078}
2079
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002080static void vmx_disable_intercept_for_msr(struct page *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002081{
2082 void *va;
2083
2084 if (!cpu_has_vmx_msr_bitmap())
2085 return;
2086
2087 /*
2088 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2089 * have the write-low and read-high bitmap offsets the wrong way round.
2090 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2091 */
2092 va = kmap(msr_bitmap);
2093 if (msr <= 0x1fff) {
2094 __clear_bit(msr, va + 0x000); /* read-low */
2095 __clear_bit(msr, va + 0x800); /* write-low */
2096 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2097 msr &= 0x1fff;
2098 __clear_bit(msr, va + 0x400); /* read-high */
2099 __clear_bit(msr, va + 0xc00); /* write-high */
2100 }
2101 kunmap(msr_bitmap);
2102}
2103
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104/*
2105 * Sets up the vmcs for emulated real mode.
2106 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002107static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002108{
Sheng Yang468d4722008-10-09 16:01:55 +08002109 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110 u32 junk;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002111 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112 unsigned long a;
2113 struct descriptor_table dt;
2114 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002115 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002116 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117
Avi Kivity6aa8b732006-12-10 02:21:36 -08002118 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03002119 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
2120 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002121
Sheng Yang25c5f222008-03-28 13:18:56 +08002122 if (cpu_has_vmx_msr_bitmap())
2123 vmcs_write64(MSR_BITMAP, page_to_phys(vmx_msr_bitmap));
2124
Avi Kivity6aa8b732006-12-10 02:21:36 -08002125 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2126
Avi Kivity6aa8b732006-12-10 02:21:36 -08002127 /* Control */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002128 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2129 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002130
2131 exec_control = vmcs_config.cpu_based_exec_ctrl;
2132 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2133 exec_control &= ~CPU_BASED_TPR_SHADOW;
2134#ifdef CONFIG_X86_64
2135 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2136 CPU_BASED_CR8_LOAD_EXITING;
2137#endif
2138 }
Sheng Yangd56f5462008-04-25 10:13:16 +08002139 if (!vm_need_ept())
2140 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002141 CPU_BASED_CR3_LOAD_EXITING |
2142 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002143 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002144
Sheng Yang83ff3b92007-11-21 14:33:25 +08002145 if (cpu_has_secondary_exec_ctrls()) {
2146 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2147 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2148 exec_control &=
2149 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002150 if (vmx->vpid == 0)
2151 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yangd56f5462008-04-25 10:13:16 +08002152 if (!vm_need_ept())
2153 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002154 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2155 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002156
Avi Kivityc7addb92007-09-16 18:58:32 +02002157 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2158 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2160
2161 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2162 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2163 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2164
2165 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2166 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2167 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002168 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2169 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002170 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002171#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172 rdmsrl(MSR_FS_BASE, a);
2173 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2174 rdmsrl(MSR_GS_BASE, a);
2175 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2176#else
2177 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2178 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2179#endif
2180
2181 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2182
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002183 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2185
Mike Dayd77c26f2007-10-08 09:02:08 -04002186 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002187 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002188 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2189 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2190 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191
2192 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2193 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2194 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2195 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2196 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2197 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2198
Sheng Yang468d4722008-10-09 16:01:55 +08002199 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2200 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2201 host_pat = msr_low | ((u64) msr_high << 32);
2202 vmcs_write64(HOST_IA32_PAT, host_pat);
2203 }
2204 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2205 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2206 host_pat = msr_low | ((u64) msr_high << 32);
2207 /* Write the default value follow host pat */
2208 vmcs_write64(GUEST_IA32_PAT, host_pat);
2209 /* Keep arch.pat sync with GUEST_IA32_PAT */
2210 vmx->vcpu.arch.pat = host_pat;
2211 }
2212
Avi Kivity6aa8b732006-12-10 02:21:36 -08002213 for (i = 0; i < NR_VMX_MSR; ++i) {
2214 u32 index = vmx_msr_index[i];
2215 u32 data_low, data_high;
2216 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002217 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002218
2219 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2220 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002221 if (wrmsr_safe(index, data_low, data_high) < 0)
2222 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002224 vmx->host_msrs[j].index = index;
2225 vmx->host_msrs[j].reserved = 0;
2226 vmx->host_msrs[j].data = data;
2227 vmx->guest_msrs[j] = vmx->host_msrs[j];
2228 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002229 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002231 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232
2233 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002234 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2235
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002236 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2237 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2238
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002239 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2240 rdtscll(tsc_this);
2241 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2242 tsc_base = tsc_this;
2243
2244 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002245
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002246 return 0;
2247}
2248
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002249static int init_rmode(struct kvm *kvm)
2250{
2251 if (!init_rmode_tss(kvm))
2252 return 0;
2253 if (!init_rmode_identity_map(kvm))
2254 return 0;
2255 return 1;
2256}
2257
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002258static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2259{
2260 struct vcpu_vmx *vmx = to_vmx(vcpu);
2261 u64 msr;
2262 int ret;
2263
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002264 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002265 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002266 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002267 ret = -ENOMEM;
2268 goto out;
2269 }
2270
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002271 vmx->vcpu.arch.rmode.active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002272
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002273 vmx->soft_vnmi_blocked = 0;
2274
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002275 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002276 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002277 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
2278 if (vmx->vcpu.vcpu_id == 0)
2279 msr |= MSR_IA32_APICBASE_BSP;
2280 kvm_set_apic_base(&vmx->vcpu, msr);
2281
2282 fx_init(&vmx->vcpu);
2283
Avi Kivity5706be02008-08-20 15:07:31 +03002284 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002285 /*
2286 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2287 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2288 */
2289 if (vmx->vcpu.vcpu_id == 0) {
2290 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2291 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2292 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002293 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2294 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002295 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002296
2297 seg_setup(VCPU_SREG_DS);
2298 seg_setup(VCPU_SREG_ES);
2299 seg_setup(VCPU_SREG_FS);
2300 seg_setup(VCPU_SREG_GS);
2301 seg_setup(VCPU_SREG_SS);
2302
2303 vmcs_write16(GUEST_TR_SELECTOR, 0);
2304 vmcs_writel(GUEST_TR_BASE, 0);
2305 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2306 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2307
2308 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2309 vmcs_writel(GUEST_LDTR_BASE, 0);
2310 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2311 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2312
2313 vmcs_write32(GUEST_SYSENTER_CS, 0);
2314 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2315 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2316
2317 vmcs_writel(GUEST_RFLAGS, 0x02);
2318 if (vmx->vcpu.vcpu_id == 0)
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002319 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002320 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002321 kvm_rip_write(vcpu, 0);
2322 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002323
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002324 vmcs_writel(GUEST_DR7, 0x400);
2325
2326 vmcs_writel(GUEST_GDTR_BASE, 0);
2327 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2328
2329 vmcs_writel(GUEST_IDTR_BASE, 0);
2330 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2331
2332 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2333 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2334 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2335
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002336 /* Special registers */
2337 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2338
2339 setup_msrs(vmx);
2340
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2342
Sheng Yangf78e0e22007-10-29 09:40:42 +08002343 if (cpu_has_vmx_tpr_shadow()) {
2344 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2345 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2346 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002347 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002348 vmcs_write32(TPR_THRESHOLD, 0);
2349 }
2350
2351 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2352 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002353 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002354
Sheng Yang2384d2b2008-01-17 15:14:33 +08002355 if (vmx->vpid != 0)
2356 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2357
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002358 vmx->vcpu.arch.cr0 = 0x60000010;
2359 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002360 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002361 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002362 vmx_fpu_activate(&vmx->vcpu);
2363 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364
Sheng Yang2384d2b2008-01-17 15:14:33 +08002365 vpid_sync_vcpu_all(vmx);
2366
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002367 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002369 /* HACK: Don't enable emulation on guest boot/reset */
2370 vmx->emulation_required = 0;
2371
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002373 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374 return ret;
2375}
2376
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002377static void enable_irq_window(struct kvm_vcpu *vcpu)
2378{
2379 u32 cpu_based_vm_exec_control;
2380
2381 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2382 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2383 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2384}
2385
2386static void enable_nmi_window(struct kvm_vcpu *vcpu)
2387{
2388 u32 cpu_based_vm_exec_control;
2389
2390 if (!cpu_has_virtual_nmis()) {
2391 enable_irq_window(vcpu);
2392 return;
2393 }
2394
2395 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2396 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2397 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2398}
2399
Eddie Dong85f455f2007-07-06 12:20:49 +03002400static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
2401{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002402 struct vcpu_vmx *vmx = to_vmx(vcpu);
2403
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002404 KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler);
2405
Avi Kivityfa89a812008-09-01 15:57:51 +03002406 ++vcpu->stat.irq_injections;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002407 if (vcpu->arch.rmode.active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002408 vmx->rmode.irq.pending = true;
2409 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002410 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Avi Kivity9c5623e2007-11-08 18:19:20 +02002411 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2412 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2413 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002414 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002415 return;
2416 }
2417 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2418 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
2419}
2420
Sheng Yangf08864b2008-05-15 18:23:25 +08002421static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2422{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002423 struct vcpu_vmx *vmx = to_vmx(vcpu);
2424
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002425 if (!cpu_has_virtual_nmis()) {
2426 /*
2427 * Tracking the NMI-blocked state in software is built upon
2428 * finding the next open IRQ window. This, in turn, depends on
2429 * well-behaving guests: They have to keep IRQs disabled at
2430 * least as long as the NMI handler runs. Otherwise we may
2431 * cause NMI nesting, maybe breaking the guest. But as this is
2432 * highly unlikely, we can live with the residual risk.
2433 */
2434 vmx->soft_vnmi_blocked = 1;
2435 vmx->vnmi_blocked_time = 0;
2436 }
2437
Jan Kiszka487b3912008-09-26 09:30:56 +02002438 ++vcpu->stat.nmi_injections;
Jan Kiszka66a5a342008-09-26 09:30:51 +02002439 if (vcpu->arch.rmode.active) {
2440 vmx->rmode.irq.pending = true;
2441 vmx->rmode.irq.vector = NMI_VECTOR;
2442 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2443 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2444 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2445 INTR_INFO_VALID_MASK);
2446 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2447 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2448 return;
2449 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002450 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2451 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002452}
2453
Jan Kiszka33f089c2008-09-26 09:30:49 +02002454static void vmx_update_window_states(struct kvm_vcpu *vcpu)
2455{
2456 u32 guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2457
2458 vcpu->arch.nmi_window_open =
2459 !(guest_intr & (GUEST_INTR_STATE_STI |
2460 GUEST_INTR_STATE_MOV_SS |
2461 GUEST_INTR_STATE_NMI));
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002462 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
2463 vcpu->arch.nmi_window_open = 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002464
2465 vcpu->arch.interrupt_window_open =
2466 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2467 !(guest_intr & (GUEST_INTR_STATE_STI |
2468 GUEST_INTR_STATE_MOV_SS)));
2469}
2470
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
2472{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002473 int word_index = __ffs(vcpu->arch.irq_summary);
2474 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002475 int irq = word_index * BITS_PER_LONG + bit_index;
2476
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002477 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
2478 if (!vcpu->arch.irq_pending[word_index])
2479 clear_bit(word_index, &vcpu->arch.irq_summary);
Avi Kivityecfc79c2008-08-14 11:13:16 +03002480 kvm_queue_interrupt(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002481}
2482
Jan Kiszkaf460ee42008-09-26 09:30:50 +02002483static void do_interrupt_requests(struct kvm_vcpu *vcpu,
2484 struct kvm_run *kvm_run)
2485{
2486 vmx_update_window_states(vcpu);
2487
Jan Kiszka55934c02008-12-15 13:52:10 +01002488 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
2489 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
2490 GUEST_INTR_STATE_STI |
2491 GUEST_INTR_STATE_MOV_SS);
2492
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002493 if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
Jan Kiszka264ff012008-11-24 12:26:19 +01002494 if (vcpu->arch.interrupt.pending) {
2495 enable_nmi_window(vcpu);
2496 } else if (vcpu->arch.nmi_window_open) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002497 vcpu->arch.nmi_pending = false;
2498 vcpu->arch.nmi_injected = true;
2499 } else {
2500 enable_nmi_window(vcpu);
Jan Kiszka487b3912008-09-26 09:30:56 +02002501 return;
2502 }
Jan Kiszka487b3912008-09-26 09:30:56 +02002503 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002504 if (vcpu->arch.nmi_injected) {
2505 vmx_inject_nmi(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01002506 if (vcpu->arch.nmi_pending)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002507 enable_nmi_window(vcpu);
2508 else if (vcpu->arch.irq_summary
2509 || kvm_run->request_interrupt_window)
2510 enable_irq_window(vcpu);
2511 return;
2512 }
Jan Kiszka487b3912008-09-26 09:30:56 +02002513
Jan Kiszkaf460ee42008-09-26 09:30:50 +02002514 if (vcpu->arch.interrupt_window_open) {
2515 if (vcpu->arch.irq_summary && !vcpu->arch.interrupt.pending)
2516 kvm_do_inject_irq(vcpu);
2517
2518 if (vcpu->arch.interrupt.pending)
2519 vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
2520 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002521 if (!vcpu->arch.interrupt_window_open &&
2522 (vcpu->arch.irq_summary || kvm_run->request_interrupt_window))
Jan Kiszkaf460ee42008-09-26 09:30:50 +02002523 enable_irq_window(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002524}
2525
Izik Eiduscbc94022007-10-25 00:29:55 +02002526static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2527{
2528 int ret;
2529 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002530 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002531 .guest_phys_addr = addr,
2532 .memory_size = PAGE_SIZE * 3,
2533 .flags = 0,
2534 };
2535
2536 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2537 if (ret)
2538 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002539 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002540 return 0;
2541}
2542
Avi Kivity6aa8b732006-12-10 02:21:36 -08002543static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2544 int vec, u32 err_code)
2545{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002546 /*
2547 * Instruction with address size override prefix opcode 0x67
2548 * Cause the #SS fault with 0 error code in VM86 mode.
2549 */
2550 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02002551 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002552 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002553 /*
2554 * Forward all other exceptions that are valid in real mode.
2555 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2556 * the required debugging infrastructure rework.
2557 */
2558 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002559 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002560 if (vcpu->guest_debug &
2561 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2562 return 0;
2563 kvm_queue_exception(vcpu, vec);
2564 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002565 case BP_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002566 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2567 return 0;
2568 /* fall through */
2569 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002570 case OF_VECTOR:
2571 case BR_VECTOR:
2572 case UD_VECTOR:
2573 case DF_VECTOR:
2574 case SS_VECTOR:
2575 case GP_VECTOR:
2576 case MF_VECTOR:
2577 kvm_queue_exception(vcpu, vec);
2578 return 1;
2579 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002580 return 0;
2581}
2582
2583static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2584{
Avi Kivity1155f762007-11-22 11:30:47 +02002585 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002586 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002587 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002588 u32 vect_info;
2589 enum emulation_result er;
2590
Avi Kivity1155f762007-11-22 11:30:47 +02002591 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002592 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2593
2594 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002595 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002596 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002597 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002598
Eddie Dong85f455f2007-07-06 12:20:49 +03002599 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002600 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002601 set_bit(irq, vcpu->arch.irq_pending);
2602 set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002603 }
2604
Jan Kiszkae4a41882008-09-26 09:30:46 +02002605 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002606 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002607
2608 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002609 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002610 return 1;
2611 }
2612
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002613 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08002614 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002615 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002616 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002617 return 1;
2618 }
2619
Avi Kivity6aa8b732006-12-10 02:21:36 -08002620 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002621 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002622 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2624 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002625 /* EPT won't cause page fault directly */
2626 if (vm_need_ept())
2627 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002629 KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2,
2630 (u32)((u64)cr2 >> 32), handler);
Avi Kivityf7d92382008-07-03 16:14:28 +03002631 if (vcpu->arch.interrupt.pending || vcpu->arch.exception.pending)
Avi Kivity577bdc42008-07-19 08:57:05 +03002632 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002633 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634 }
2635
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002636 if (vcpu->arch.rmode.active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002638 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002639 if (vcpu->arch.halt_request) {
2640 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002641 return kvm_emulate_halt(vcpu);
2642 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002643 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002644 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002645
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002646 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002647 switch (ex_no) {
2648 case DB_VECTOR:
2649 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2650 if (!(vcpu->guest_debug &
2651 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2652 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2653 kvm_queue_exception(vcpu, DB_VECTOR);
2654 return 1;
2655 }
2656 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2657 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2658 /* fall through */
2659 case BP_VECTOR:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002661 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2662 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002663 break;
2664 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002665 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2666 kvm_run->ex.exception = ex_no;
2667 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002668 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002669 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002670 return 0;
2671}
2672
2673static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2674 struct kvm_run *kvm_run)
2675{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002676 ++vcpu->stat.irq_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002677 KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678 return 1;
2679}
2680
Avi Kivity988ad742007-02-12 00:54:36 -08002681static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2682{
2683 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2684 return 0;
2685}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002686
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2688{
He, Qingbfdaab02007-09-12 14:18:28 +08002689 unsigned long exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02002690 int size, down, in, string, rep;
2691 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692
Avi Kivity1165f5f2007-04-19 17:27:43 +03002693 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002694 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002695 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002696
2697 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002698 if (emulate_instruction(vcpu,
2699 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002700 return 0;
2701 return 1;
2702 }
2703
2704 size = (exit_qualification & 7) + 1;
2705 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002706 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002707 rep = (exit_qualification & 32) != 0;
2708 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002709
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002710 skip_emulated_instruction(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002711 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712}
2713
Ingo Molnar102d8322007-02-19 14:37:47 +02002714static void
2715vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2716{
2717 /*
2718 * Patch in the VMCALL instruction:
2719 */
2720 hypercall[0] = 0x0f;
2721 hypercall[1] = 0x01;
2722 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002723}
2724
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2726{
He, Qingbfdaab02007-09-12 14:18:28 +08002727 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728 int cr;
2729 int reg;
2730
He, Qingbfdaab02007-09-12 14:18:28 +08002731 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732 cr = exit_qualification & 15;
2733 reg = (exit_qualification >> 8) & 15;
2734 switch ((exit_qualification >> 4) & 3) {
2735 case 0: /* mov to cr */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002736 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr,
2737 (u32)kvm_register_read(vcpu, reg),
2738 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
2739 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002740 switch (cr) {
2741 case 0:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002742 kvm_set_cr0(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002743 skip_emulated_instruction(vcpu);
2744 return 1;
2745 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002746 kvm_set_cr3(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002747 skip_emulated_instruction(vcpu);
2748 return 1;
2749 case 4:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002750 kvm_set_cr4(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751 skip_emulated_instruction(vcpu);
2752 return 1;
2753 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002754 kvm_set_cr8(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755 skip_emulated_instruction(vcpu);
Avi Kivitye5314062007-12-06 16:32:45 +02002756 if (irqchip_in_kernel(vcpu->kvm))
2757 return 1;
Yang, Sheng253abde2007-08-16 13:01:00 +03002758 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2759 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760 };
2761 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002762 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002763 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002764 vcpu->arch.cr0 &= ~X86_CR0_TS;
2765 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002766 vmx_fpu_activate(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002767 KVMTRACE_0D(CLTS, vcpu, handler);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002768 skip_emulated_instruction(vcpu);
2769 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 case 1: /*mov from cr*/
2771 switch (cr) {
2772 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002773 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002774 KVMTRACE_3D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002775 (u32)kvm_register_read(vcpu, reg),
2776 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002777 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002778 skip_emulated_instruction(vcpu);
2779 return 1;
2780 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002781 kvm_register_write(vcpu, reg, kvm_get_cr8(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002782 KVMTRACE_2D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002783 (u32)kvm_register_read(vcpu, reg), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002784 skip_emulated_instruction(vcpu);
2785 return 1;
2786 }
2787 break;
2788 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002789 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790
2791 skip_emulated_instruction(vcpu);
2792 return 1;
2793 default:
2794 break;
2795 }
2796 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002797 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002798 (int)(exit_qualification >> 4) & 3, cr);
2799 return 0;
2800}
2801
2802static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2803{
He, Qingbfdaab02007-09-12 14:18:28 +08002804 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002805 unsigned long val;
2806 int dr, reg;
2807
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002808 dr = vmcs_readl(GUEST_DR7);
2809 if (dr & DR7_GD) {
2810 /*
2811 * As the vm-exit takes precedence over the debug trap, we
2812 * need to emulate the latter, either for the host or the
2813 * guest debugging itself.
2814 */
2815 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
2816 kvm_run->debug.arch.dr6 = vcpu->arch.dr6;
2817 kvm_run->debug.arch.dr7 = dr;
2818 kvm_run->debug.arch.pc =
2819 vmcs_readl(GUEST_CS_BASE) +
2820 vmcs_readl(GUEST_RIP);
2821 kvm_run->debug.arch.exception = DB_VECTOR;
2822 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2823 return 0;
2824 } else {
2825 vcpu->arch.dr7 &= ~DR7_GD;
2826 vcpu->arch.dr6 |= DR6_BD;
2827 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2828 kvm_queue_exception(vcpu, DB_VECTOR);
2829 return 1;
2830 }
2831 }
2832
He, Qingbfdaab02007-09-12 14:18:28 +08002833 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002834 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
2835 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
2836 if (exit_qualification & TYPE_MOV_FROM_DR) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002837 switch (dr) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002838 case 0 ... 3:
2839 val = vcpu->arch.db[dr];
2840 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002841 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002842 val = vcpu->arch.dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843 break;
2844 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002845 val = vcpu->arch.dr7;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846 break;
2847 default:
2848 val = 0;
2849 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002850 kvm_register_write(vcpu, reg, val);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002851 KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002852 } else {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002853 val = vcpu->arch.regs[reg];
2854 switch (dr) {
2855 case 0 ... 3:
2856 vcpu->arch.db[dr] = val;
2857 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
2858 vcpu->arch.eff_db[dr] = val;
2859 break;
2860 case 4 ... 5:
2861 if (vcpu->arch.cr4 & X86_CR4_DE)
2862 kvm_queue_exception(vcpu, UD_VECTOR);
2863 break;
2864 case 6:
2865 if (val & 0xffffffff00000000ULL) {
2866 kvm_queue_exception(vcpu, GP_VECTOR);
2867 break;
2868 }
2869 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
2870 break;
2871 case 7:
2872 if (val & 0xffffffff00000000ULL) {
2873 kvm_queue_exception(vcpu, GP_VECTOR);
2874 break;
2875 }
2876 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
2877 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
2878 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2879 vcpu->arch.switch_db_regs =
2880 (val & DR7_BP_EN_MASK);
2881 }
2882 break;
2883 }
2884 KVMTRACE_2D(DR_WRITE, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886 skip_emulated_instruction(vcpu);
2887 return 1;
2888}
2889
2890static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2891{
Avi Kivity06465c52007-02-28 20:46:53 +02002892 kvm_emulate_cpuid(vcpu);
2893 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002894}
2895
2896static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2897{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002898 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899 u64 data;
2900
2901 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002902 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002903 return 1;
2904 }
2905
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002906 KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32),
2907 handler);
2908
Avi Kivity6aa8b732006-12-10 02:21:36 -08002909 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002910 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
2911 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002912 skip_emulated_instruction(vcpu);
2913 return 1;
2914}
2915
2916static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2917{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002918 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
2919 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
2920 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002921
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002922 KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32),
2923 handler);
2924
Avi Kivity6aa8b732006-12-10 02:21:36 -08002925 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002926 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002927 return 1;
2928 }
2929
2930 skip_emulated_instruction(vcpu);
2931 return 1;
2932}
2933
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002934static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2935 struct kvm_run *kvm_run)
2936{
2937 return 1;
2938}
2939
Avi Kivity6aa8b732006-12-10 02:21:36 -08002940static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2941 struct kvm_run *kvm_run)
2942{
Eddie Dong85f455f2007-07-06 12:20:49 +03002943 u32 cpu_based_vm_exec_control;
2944
2945 /* clear pending irq */
2946 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2947 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2948 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002949
2950 KVMTRACE_0D(PEND_INTR, vcpu, handler);
Jan Kiszkaa26bf122008-09-26 09:30:45 +02002951 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002952
Dor Laorc1150d82007-01-05 16:36:24 -08002953 /*
2954 * If the user space waits to inject interrupts, exit as soon as
2955 * possible
2956 */
2957 if (kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002958 !vcpu->arch.irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002959 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08002960 return 0;
2961 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002962 return 1;
2963}
2964
2965static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2966{
2967 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002968 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002969}
2970
Ingo Molnarc21415e2007-02-19 14:37:47 +02002971static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2972{
Dor Laor510043d2007-02-19 18:25:43 +02002973 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002974 kvm_emulate_hypercall(vcpu);
2975 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02002976}
2977
Marcelo Tosattia7052892008-09-23 13:18:35 -03002978static int handle_invlpg(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2979{
2980 u64 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2981
2982 kvm_mmu_invlpg(vcpu, exit_qualification);
2983 skip_emulated_instruction(vcpu);
2984 return 1;
2985}
2986
Eddie Donge5edaa02007-11-11 12:28:35 +02002987static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2988{
2989 skip_emulated_instruction(vcpu);
2990 /* TODO: Add support for VT-d/pass-through device */
2991 return 1;
2992}
2993
Sheng Yangf78e0e22007-10-29 09:40:42 +08002994static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2995{
2996 u64 exit_qualification;
2997 enum emulation_result er;
2998 unsigned long offset;
2999
3000 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
3001 offset = exit_qualification & 0xffful;
3002
3003 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3004
3005 if (er != EMULATE_DONE) {
3006 printk(KERN_ERR
3007 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3008 offset);
3009 return -ENOTSUPP;
3010 }
3011 return 1;
3012}
3013
Izik Eidus37817f22008-03-24 23:14:53 +02003014static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3015{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003016 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003017 unsigned long exit_qualification;
3018 u16 tss_selector;
3019 int reason;
3020
3021 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3022
3023 reason = (u32)exit_qualification >> 30;
Jan Kiszka60637aa2008-09-26 09:30:47 +02003024 if (reason == TASK_SWITCH_GATE && vmx->vcpu.arch.nmi_injected &&
3025 (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
3026 (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK)
3027 == INTR_TYPE_NMI_INTR) {
3028 vcpu->arch.nmi_injected = false;
3029 if (cpu_has_virtual_nmis())
3030 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3031 GUEST_INTR_STATE_NMI);
3032 }
Izik Eidus37817f22008-03-24 23:14:53 +02003033 tss_selector = exit_qualification;
3034
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003035 if (!kvm_task_switch(vcpu, tss_selector, reason))
3036 return 0;
3037
3038 /* clear all local breakpoint enable flags */
3039 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3040
3041 /*
3042 * TODO: What about debug traps on tss switch?
3043 * Are we supposed to inject them and update dr6?
3044 */
3045
3046 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003047}
3048
Sheng Yang14394422008-04-28 12:24:45 +08003049static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3050{
3051 u64 exit_qualification;
3052 enum emulation_result er;
3053 gpa_t gpa;
3054 unsigned long hva;
3055 int gla_validity;
3056 int r;
3057
3058 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
3059
3060 if (exit_qualification & (1 << 6)) {
3061 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
3062 return -ENOTSUPP;
3063 }
3064
3065 gla_validity = (exit_qualification >> 7) & 0x3;
3066 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3067 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3068 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3069 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
3070 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
3071 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3072 (long unsigned int)exit_qualification);
3073 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3074 kvm_run->hw.hardware_exit_reason = 0;
3075 return -ENOTSUPP;
3076 }
3077
3078 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3079 hva = gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT);
3080 if (!kvm_is_error_hva(hva)) {
3081 r = kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
3082 if (r < 0) {
3083 printk(KERN_ERR "EPT: Not enough memory!\n");
3084 return -ENOMEM;
3085 }
3086 return 1;
3087 } else {
3088 /* must be MMIO */
3089 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3090
3091 if (er == EMULATE_FAIL) {
3092 printk(KERN_ERR
3093 "EPT: Fail to handle EPT violation vmexit!er is %d\n",
3094 er);
3095 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3096 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
3097 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
3098 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3099 (long unsigned int)exit_qualification);
3100 return -ENOTSUPP;
3101 } else if (er == EMULATE_DO_MMIO)
3102 return 0;
3103 }
3104 return 1;
3105}
3106
Sheng Yangf08864b2008-05-15 18:23:25 +08003107static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3108{
3109 u32 cpu_based_vm_exec_control;
3110
3111 /* clear pending NMI */
3112 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3113 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3114 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3115 ++vcpu->stat.nmi_window_exits;
3116
3117 return 1;
3118}
3119
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003120static void handle_invalid_guest_state(struct kvm_vcpu *vcpu,
3121 struct kvm_run *kvm_run)
3122{
3123 struct vcpu_vmx *vmx = to_vmx(vcpu);
3124 int err;
3125
3126 preempt_enable();
3127 local_irq_enable();
3128
3129 while (!guest_state_valid(vcpu)) {
3130 err = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3131
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003132 if (err == EMULATE_DO_MMIO)
3133 break;
3134
3135 if (err != EMULATE_DONE) {
3136 kvm_report_emulation_failure(vcpu, "emulation failure");
3137 return;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003138 }
3139
3140 if (signal_pending(current))
3141 break;
3142 if (need_resched())
3143 schedule();
3144 }
3145
3146 local_irq_disable();
3147 preempt_disable();
3148
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003149 /* Guest state should be valid now except if we need to
3150 * emulate an MMIO */
3151 if (guest_state_valid(vcpu))
3152 vmx->emulation_required = 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003153}
3154
Avi Kivity6aa8b732006-12-10 02:21:36 -08003155/*
3156 * The exit handlers return 1 if the exit was handled fully and guest execution
3157 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3158 * to be done to userspace and return 0.
3159 */
3160static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
3161 struct kvm_run *kvm_run) = {
3162 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3163 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003164 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003165 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003166 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003167 [EXIT_REASON_CR_ACCESS] = handle_cr,
3168 [EXIT_REASON_DR_ACCESS] = handle_dr,
3169 [EXIT_REASON_CPUID] = handle_cpuid,
3170 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3171 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3172 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3173 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003174 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003175 [EXIT_REASON_VMCALL] = handle_vmcall,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003176 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3177 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003178 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003179 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Sheng Yang14394422008-04-28 12:24:45 +08003180 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003181};
3182
3183static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003184 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003185
3186/*
3187 * The guest has exited. See if we can fix it or if we need userspace
3188 * assistance.
3189 */
3190static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
3191{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003192 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity29bd8a72007-09-10 17:27:03 +03003193 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1155f762007-11-22 11:30:47 +02003194 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003195
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003196 KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)kvm_rip_read(vcpu),
3197 (u32)((u64)kvm_rip_read(vcpu) >> 32), entryexit);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003198
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003199 /* If we need to emulate an MMIO from handle_invalid_guest_state
3200 * we just return 0 */
3201 if (vmx->emulation_required && emulate_invalid_guest_state)
3202 return 0;
3203
Sheng Yang14394422008-04-28 12:24:45 +08003204 /* Access CR3 don't cause VMExit in paging mode, so we need
3205 * to sync with guest real CR3. */
3206 if (vm_need_ept() && is_paging(vcpu)) {
3207 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3208 ept_load_pdptrs(vcpu);
3209 }
3210
Avi Kivity29bd8a72007-09-10 17:27:03 +03003211 if (unlikely(vmx->fail)) {
3212 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3213 kvm_run->fail_entry.hardware_entry_failure_reason
3214 = vmcs_read32(VM_INSTRUCTION_ERROR);
3215 return 0;
3216 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217
Mike Dayd77c26f2007-10-08 09:02:08 -04003218 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003219 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003220 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3221 exit_reason != EXIT_REASON_TASK_SWITCH))
3222 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3223 "(0x%x) and exit reason is 0x%x\n",
3224 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003225
3226 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
3227 if (vcpu->arch.interrupt_window_open) {
3228 vmx->soft_vnmi_blocked = 0;
3229 vcpu->arch.nmi_window_open = 1;
3230 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003231 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003232 /*
3233 * This CPU don't support us in finding the end of an
3234 * NMI-blocked window if the guest runs with IRQs
3235 * disabled. So we pull the trigger after 1 s of
3236 * futile waiting, but inform the user about this.
3237 */
3238 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3239 "state on VCPU %d after 1 s timeout\n",
3240 __func__, vcpu->vcpu_id);
3241 vmx->soft_vnmi_blocked = 0;
3242 vmx->vcpu.arch.nmi_window_open = 1;
3243 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003244 }
3245
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246 if (exit_reason < kvm_vmx_max_exit_handlers
3247 && kvm_vmx_exit_handlers[exit_reason])
3248 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
3249 else {
3250 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3251 kvm_run->hw.hardware_exit_reason = exit_reason;
3252 }
3253 return 0;
3254}
3255
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003256static void update_tpr_threshold(struct kvm_vcpu *vcpu)
3257{
3258 int max_irr, tpr;
3259
3260 if (!vm_need_tpr_shadow(vcpu->kvm))
3261 return;
3262
3263 if (!kvm_lapic_enabled(vcpu) ||
3264 ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
3265 vmcs_write32(TPR_THRESHOLD, 0);
3266 return;
3267 }
3268
3269 tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
3270 vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
3271}
3272
Avi Kivitycf393f72008-07-01 16:20:21 +03003273static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3274{
3275 u32 exit_intr_info;
Avi Kivity668f6122008-07-02 09:28:55 +03003276 u32 idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003277 bool unblock_nmi;
3278 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003279 int type;
3280 bool idtv_info_valid;
Avi Kivity35920a32008-07-03 14:50:12 +03003281 u32 error;
Avi Kivitycf393f72008-07-01 16:20:21 +03003282
3283 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
3284 if (cpu_has_virtual_nmis()) {
3285 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3286 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3287 /*
3288 * SDM 3: 25.7.1.2
3289 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3290 * a guest IRET fault.
3291 */
3292 if (unblock_nmi && vector != DF_VECTOR)
3293 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3294 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003295 } else if (unlikely(vmx->soft_vnmi_blocked))
3296 vmx->vnmi_blocked_time +=
3297 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003298
3299 idt_vectoring_info = vmx->idt_vectoring_info;
3300 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3301 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3302 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
3303 if (vmx->vcpu.arch.nmi_injected) {
3304 /*
3305 * SDM 3: 25.7.1.2
3306 * Clear bit "block by NMI" before VM entry if a NMI delivery
3307 * faulted.
3308 */
3309 if (idtv_info_valid && type == INTR_TYPE_NMI_INTR)
3310 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3311 GUEST_INTR_STATE_NMI);
3312 else
3313 vmx->vcpu.arch.nmi_injected = false;
3314 }
Avi Kivity35920a32008-07-03 14:50:12 +03003315 kvm_clear_exception_queue(&vmx->vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003316 if (idtv_info_valid && (type == INTR_TYPE_HARD_EXCEPTION ||
3317 type == INTR_TYPE_SOFT_EXCEPTION)) {
Avi Kivity35920a32008-07-03 14:50:12 +03003318 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
3319 error = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3320 kvm_queue_exception_e(&vmx->vcpu, vector, error);
3321 } else
3322 kvm_queue_exception(&vmx->vcpu, vector);
3323 vmx->idt_vectoring_info = 0;
3324 }
Avi Kivityf7d92382008-07-03 16:14:28 +03003325 kvm_clear_interrupt_queue(&vmx->vcpu);
3326 if (idtv_info_valid && type == INTR_TYPE_EXT_INTR) {
3327 kvm_queue_interrupt(&vmx->vcpu, vector);
3328 vmx->idt_vectoring_info = 0;
3329 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003330}
3331
Eddie Dong85f455f2007-07-06 12:20:49 +03003332static void vmx_intr_assist(struct kvm_vcpu *vcpu)
3333{
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003334 update_tpr_threshold(vcpu);
3335
Jan Kiszka33f089c2008-09-26 09:30:49 +02003336 vmx_update_window_states(vcpu);
3337
Jan Kiszka55934c02008-12-15 13:52:10 +01003338 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3339 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3340 GUEST_INTR_STATE_STI |
3341 GUEST_INTR_STATE_MOV_SS);
3342
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003343 if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
3344 if (vcpu->arch.interrupt.pending) {
3345 enable_nmi_window(vcpu);
3346 } else if (vcpu->arch.nmi_window_open) {
3347 vcpu->arch.nmi_pending = false;
3348 vcpu->arch.nmi_injected = true;
3349 } else {
3350 enable_nmi_window(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003351 return;
3352 }
Sheng Yangf08864b2008-05-15 18:23:25 +08003353 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003354 if (vcpu->arch.nmi_injected) {
3355 vmx_inject_nmi(vcpu);
3356 if (vcpu->arch.nmi_pending)
3357 enable_nmi_window(vcpu);
3358 else if (kvm_cpu_has_interrupt(vcpu))
3359 enable_irq_window(vcpu);
3360 return;
3361 }
Avi Kivityf7d92382008-07-03 16:14:28 +03003362 if (!vcpu->arch.interrupt.pending && kvm_cpu_has_interrupt(vcpu)) {
Jan Kiszka33f089c2008-09-26 09:30:49 +02003363 if (vcpu->arch.interrupt_window_open)
Avi Kivityf7d92382008-07-03 16:14:28 +03003364 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu));
3365 else
3366 enable_irq_window(vcpu);
3367 }
3368 if (vcpu->arch.interrupt.pending) {
3369 vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
Avi Kivitydf203ec2008-11-23 18:08:57 +02003370 if (kvm_cpu_has_interrupt(vcpu))
3371 enable_irq_window(vcpu);
Avi Kivityf7d92382008-07-03 16:14:28 +03003372 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003373}
3374
Avi Kivity9c8cba32007-11-22 11:42:59 +02003375/*
3376 * Failure to inject an interrupt should give us the information
3377 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3378 * when fetching the interrupt redirection bitmap in the real-mode
3379 * tss, this doesn't happen. So we do it ourselves.
3380 */
3381static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3382{
3383 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003384 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003385 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003386 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003387 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3388 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3389 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3390 return;
3391 }
3392 vmx->idt_vectoring_info =
3393 VECTORING_INFO_VALID_MASK
3394 | INTR_TYPE_EXT_INTR
3395 | vmx->rmode.irq.vector;
3396}
3397
Avi Kivityc8019492008-07-14 14:44:59 +03003398#ifdef CONFIG_X86_64
3399#define R "r"
3400#define Q "q"
3401#else
3402#define R "e"
3403#define Q "l"
3404#endif
3405
Avi Kivity04d2cc72007-09-10 18:10:54 +03003406static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003407{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003408 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1b6269d2007-10-09 12:12:19 +02003409 u32 intr_info;
Avi Kivitye6adf282007-04-30 16:07:54 +03003410
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003411 /* Record the guest's net vcpu time for enforced NMI injections. */
3412 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3413 vmx->entry_time = ktime_get();
3414
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003415 /* Handle invalid guest state instead of entering VMX */
3416 if (vmx->emulation_required && emulate_invalid_guest_state) {
3417 handle_invalid_guest_state(vcpu, kvm_run);
3418 return;
3419 }
3420
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003421 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3422 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3423 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3424 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3425
Avi Kivitye6adf282007-04-30 16:07:54 +03003426 /*
3427 * Loading guest fpu may have cleared host cr0.ts
3428 */
3429 vmcs_writel(HOST_CR0, read_cr0());
3430
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003431 set_debugreg(vcpu->arch.dr6, 6);
3432
Mike Dayd77c26f2007-10-08 09:02:08 -04003433 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003434 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003435 "push %%"R"dx; push %%"R"bp;"
3436 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003437 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3438 "je 1f \n\t"
3439 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003440 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003441 "1: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003442 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003443 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003444 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003445 "mov %c[cr2](%0), %%"R"ax \n\t"
3446 "mov %%"R"ax, %%cr2 \n\t"
3447 "mov %c[rax](%0), %%"R"ax \n\t"
3448 "mov %c[rbx](%0), %%"R"bx \n\t"
3449 "mov %c[rdx](%0), %%"R"dx \n\t"
3450 "mov %c[rsi](%0), %%"R"si \n\t"
3451 "mov %c[rdi](%0), %%"R"di \n\t"
3452 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003453#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003454 "mov %c[r8](%0), %%r8 \n\t"
3455 "mov %c[r9](%0), %%r9 \n\t"
3456 "mov %c[r10](%0), %%r10 \n\t"
3457 "mov %c[r11](%0), %%r11 \n\t"
3458 "mov %c[r12](%0), %%r12 \n\t"
3459 "mov %c[r13](%0), %%r13 \n\t"
3460 "mov %c[r14](%0), %%r14 \n\t"
3461 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003462#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003463 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3464
Avi Kivity6aa8b732006-12-10 02:21:36 -08003465 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003466 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003467 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003468 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003469 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003470 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003471 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003472 "xchg %0, (%%"R"sp) \n\t"
3473 "mov %%"R"ax, %c[rax](%0) \n\t"
3474 "mov %%"R"bx, %c[rbx](%0) \n\t"
3475 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3476 "mov %%"R"dx, %c[rdx](%0) \n\t"
3477 "mov %%"R"si, %c[rsi](%0) \n\t"
3478 "mov %%"R"di, %c[rdi](%0) \n\t"
3479 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003480#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003481 "mov %%r8, %c[r8](%0) \n\t"
3482 "mov %%r9, %c[r9](%0) \n\t"
3483 "mov %%r10, %c[r10](%0) \n\t"
3484 "mov %%r11, %c[r11](%0) \n\t"
3485 "mov %%r12, %c[r12](%0) \n\t"
3486 "mov %%r13, %c[r13](%0) \n\t"
3487 "mov %%r14, %c[r14](%0) \n\t"
3488 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003489#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003490 "mov %%cr2, %%"R"ax \n\t"
3491 "mov %%"R"ax, %c[cr2](%0) \n\t"
3492
3493 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003494 "setbe %c[fail](%0) \n\t"
3495 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3496 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3497 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03003498 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003499 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3500 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3501 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3502 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3503 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3504 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3505 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003506#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003507 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3508 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3509 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3510 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3511 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3512 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3513 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3514 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003515#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003516 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003517 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003518 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003519#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003520 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3521#endif
3522 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003523
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003524 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
3525 vcpu->arch.regs_dirty = 0;
3526
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003527 get_debugreg(vcpu->arch.dr6, 6);
3528
Avi Kivity1155f762007-11-22 11:30:47 +02003529 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003530 if (vmx->rmode.irq.pending)
3531 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003532
Jan Kiszka33f089c2008-09-26 09:30:49 +02003533 vmx_update_window_states(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003534
Mike Dayd77c26f2007-10-08 09:02:08 -04003535 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003536 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003537
3538 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
3539
3540 /* We need to handle NMIs before interrupts are enabled */
Jan Kiszkae4a41882008-09-26 09:30:46 +02003541 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Sheng Yangf08864b2008-05-15 18:23:25 +08003542 (intr_info & INTR_INFO_VALID_MASK)) {
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003543 KVMTRACE_0D(NMI, vcpu, handler);
Avi Kivity1b6269d2007-10-09 12:12:19 +02003544 asm("int $2");
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003545 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003546
3547 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003548}
3549
Avi Kivityc8019492008-07-14 14:44:59 +03003550#undef R
3551#undef Q
3552
Avi Kivity6aa8b732006-12-10 02:21:36 -08003553static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3554{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003555 struct vcpu_vmx *vmx = to_vmx(vcpu);
3556
3557 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003558 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003559 free_vmcs(vmx->vmcs);
3560 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003561 }
3562}
3563
3564static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3565{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003566 struct vcpu_vmx *vmx = to_vmx(vcpu);
3567
Sheng Yang2384d2b2008-01-17 15:14:33 +08003568 spin_lock(&vmx_vpid_lock);
3569 if (vmx->vpid != 0)
3570 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3571 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003572 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003573 kfree(vmx->host_msrs);
3574 kfree(vmx->guest_msrs);
3575 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003576 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003577}
3578
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003579static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003580{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003581 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003582 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003583 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003584
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003585 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003586 return ERR_PTR(-ENOMEM);
3587
Sheng Yang2384d2b2008-01-17 15:14:33 +08003588 allocate_vpid(vmx);
3589
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003590 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3591 if (err)
3592 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003593
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003594 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003595 if (!vmx->guest_msrs) {
3596 err = -ENOMEM;
3597 goto uninit_vcpu;
3598 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003599
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003600 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3601 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003602 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003603
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003604 vmx->vmcs = alloc_vmcs();
3605 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003606 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003607
3608 vmcs_clear(vmx->vmcs);
3609
Avi Kivity15ad7142007-07-11 18:17:21 +03003610 cpu = get_cpu();
3611 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003612 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003613 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003614 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003615 if (err)
3616 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003617 if (vm_need_virtualize_apic_accesses(kvm))
3618 if (alloc_apic_access_page(kvm) != 0)
3619 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003620
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003621 if (vm_need_ept())
3622 if (alloc_identity_pagetable(kvm) != 0)
3623 goto free_vmcs;
3624
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003625 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003626
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003627free_vmcs:
3628 free_vmcs(vmx->vmcs);
3629free_msrs:
3630 kfree(vmx->host_msrs);
3631free_guest_msrs:
3632 kfree(vmx->guest_msrs);
3633uninit_vcpu:
3634 kvm_vcpu_uninit(&vmx->vcpu);
3635free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003636 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003637 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003638}
3639
Yang, Sheng002c7f72007-07-31 14:23:01 +03003640static void __init vmx_check_processor_compat(void *rtn)
3641{
3642 struct vmcs_config vmcs_conf;
3643
3644 *(int *)rtn = 0;
3645 if (setup_vmcs_config(&vmcs_conf) < 0)
3646 *(int *)rtn = -EIO;
3647 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3648 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3649 smp_processor_id());
3650 *(int *)rtn = -EIO;
3651 }
3652}
3653
Sheng Yang67253af2008-04-25 10:20:22 +08003654static int get_ept_level(void)
3655{
3656 return VMX_EPT_DEFAULT_GAW + 1;
3657}
3658
Sheng Yang64d4d522008-10-09 16:01:57 +08003659static int vmx_get_mt_mask_shift(void)
3660{
3661 return VMX_EPT_MT_EPTE_SHIFT;
3662}
3663
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003664static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003665 .cpu_has_kvm_support = cpu_has_kvm_support,
3666 .disabled_by_bios = vmx_disabled_by_bios,
3667 .hardware_setup = hardware_setup,
3668 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003669 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003670 .hardware_enable = hardware_enable,
3671 .hardware_disable = hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003672 .cpu_has_accelerated_tpr = cpu_has_vmx_virtualize_apic_accesses,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003673
3674 .vcpu_create = vmx_create_vcpu,
3675 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003676 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003677
Avi Kivity04d2cc72007-09-10 18:10:54 +03003678 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003679 .vcpu_load = vmx_vcpu_load,
3680 .vcpu_put = vmx_vcpu_put,
3681
3682 .set_guest_debug = set_guest_debug,
3683 .get_msr = vmx_get_msr,
3684 .set_msr = vmx_set_msr,
3685 .get_segment_base = vmx_get_segment_base,
3686 .get_segment = vmx_get_segment,
3687 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003688 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003689 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003690 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003691 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003692 .set_cr3 = vmx_set_cr3,
3693 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003694 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003695 .get_idt = vmx_get_idt,
3696 .set_idt = vmx_set_idt,
3697 .get_gdt = vmx_get_gdt,
3698 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003699 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003700 .get_rflags = vmx_get_rflags,
3701 .set_rflags = vmx_set_rflags,
3702
3703 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003704
Avi Kivity6aa8b732006-12-10 02:21:36 -08003705 .run = vmx_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003706 .handle_exit = kvm_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003707 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02003708 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003709 .get_irq = vmx_get_irq,
3710 .set_irq = vmx_inject_irq,
Avi Kivity298101d2007-11-25 13:41:11 +02003711 .queue_exception = vmx_queue_exception,
3712 .exception_injected = vmx_exception_injected,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003713 .inject_pending_irq = vmx_intr_assist,
3714 .inject_pending_vectors = do_interrupt_requests,
Izik Eiduscbc94022007-10-25 00:29:55 +02003715
3716 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003717 .get_tdp_level = get_ept_level,
Sheng Yang64d4d522008-10-09 16:01:57 +08003718 .get_mt_mask_shift = vmx_get_mt_mask_shift,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003719};
3720
3721static int __init vmx_init(void)
3722{
Sheng Yang25c5f222008-03-28 13:18:56 +08003723 void *va;
He, Qingfdef3ad2007-04-30 09:45:24 +03003724 int r;
3725
3726 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3727 if (!vmx_io_bitmap_a)
3728 return -ENOMEM;
3729
3730 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3731 if (!vmx_io_bitmap_b) {
3732 r = -ENOMEM;
3733 goto out;
3734 }
3735
Sheng Yang25c5f222008-03-28 13:18:56 +08003736 vmx_msr_bitmap = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3737 if (!vmx_msr_bitmap) {
3738 r = -ENOMEM;
3739 goto out1;
3740 }
3741
He, Qingfdef3ad2007-04-30 09:45:24 +03003742 /*
3743 * Allow direct access to the PC debug port (it is often used for I/O
3744 * delays, but the vmexits simply slow things down).
3745 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003746 va = kmap(vmx_io_bitmap_a);
3747 memset(va, 0xff, PAGE_SIZE);
3748 clear_bit(0x80, va);
Avi Kivitycd0536d2007-05-08 11:34:07 +03003749 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003750
Sheng Yang25c5f222008-03-28 13:18:56 +08003751 va = kmap(vmx_io_bitmap_b);
3752 memset(va, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03003753 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03003754
Sheng Yang25c5f222008-03-28 13:18:56 +08003755 va = kmap(vmx_msr_bitmap);
3756 memset(va, 0xff, PAGE_SIZE);
3757 kunmap(vmx_msr_bitmap);
3758
Sheng Yang2384d2b2008-01-17 15:14:33 +08003759 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
3760
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003761 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003762 if (r)
Sheng Yang25c5f222008-03-28 13:18:56 +08003763 goto out2;
3764
3765 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_FS_BASE);
3766 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_GS_BASE);
3767 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_CS);
3768 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_ESP);
3769 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_EIP);
He, Qingfdef3ad2007-04-30 09:45:24 +03003770
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003771 if (vm_need_ept()) {
Sheng Yang14394422008-04-28 12:24:45 +08003772 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003773 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08003774 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08003775 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang64d4d522008-10-09 16:01:57 +08003776 VMX_EPT_EXECUTABLE_MASK,
3777 VMX_EPT_DEFAULT_MT << VMX_EPT_MT_EPTE_SHIFT);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003778 kvm_enable_tdp();
3779 } else
3780 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08003781
Avi Kivityc7addb92007-09-16 18:58:32 +02003782 if (bypass_guest_pf)
3783 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
3784
Sheng Yang14394422008-04-28 12:24:45 +08003785 ept_sync_global();
3786
He, Qingfdef3ad2007-04-30 09:45:24 +03003787 return 0;
3788
Sheng Yang25c5f222008-03-28 13:18:56 +08003789out2:
3790 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03003791out1:
3792 __free_page(vmx_io_bitmap_b);
3793out:
3794 __free_page(vmx_io_bitmap_a);
3795 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003796}
3797
3798static void __exit vmx_exit(void)
3799{
Sheng Yang25c5f222008-03-28 13:18:56 +08003800 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03003801 __free_page(vmx_io_bitmap_b);
3802 __free_page(vmx_io_bitmap_a);
3803
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003804 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003805}
3806
3807module_init(vmx_init)
3808module_exit(vmx_exit)