blob: 9f77ddbeb025a8482c6b192fec81854242db4550 [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
18#include "kvm.h"
Laurent Viviere7d5d762007-07-30 13:41:19 +030019#include "x86_emulate.h"
Eddie Dong85f455f2007-07-06 12:20:49 +030020#include "irq.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080021#include "vmx.h"
Avi Kivitye4956062007-06-28 14:15:57 -040022#include "segment_descriptor.h"
23
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020025#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/mm.h>
27#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040028#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020029#include <linux/moduleparam.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>
Avi Kivity6aa8b732006-12-10 02:21:36 -080033
Avi Kivity6aa8b732006-12-10 02:21:36 -080034MODULE_AUTHOR("Qumranet");
35MODULE_LICENSE("GPL");
36
Avi Kivityc7addb92007-09-16 18:58:32 +020037static int bypass_guest_pf = 1;
38module_param(bypass_guest_pf, bool, 0);
39
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040040struct vmcs {
41 u32 revision_id;
42 u32 abort;
43 char data[0];
44};
45
46struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100047 struct kvm_vcpu vcpu;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040048 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030049 u8 fail;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040050 struct kvm_msr_entry *guest_msrs;
51 struct kvm_msr_entry *host_msrs;
52 int nmsrs;
53 int save_nmsrs;
54 int msr_offset_efer;
55#ifdef CONFIG_X86_64
56 int msr_offset_kernel_gs_base;
57#endif
58 struct vmcs *vmcs;
59 struct {
60 int loaded;
61 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020062 int gs_ldt_reload_needed;
63 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030064 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040065 } host_state;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040066
67};
68
69static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
70{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100071 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040072}
73
Avi Kivity75880a02007-06-20 11:20:04 +030074static int init_rmode_tss(struct kvm *kvm);
75
Avi Kivity6aa8b732006-12-10 02:21:36 -080076static DEFINE_PER_CPU(struct vmcs *, vmxarea);
77static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
78
He, Qingfdef3ad2007-04-30 09:45:24 +030079static struct page *vmx_io_bitmap_a;
80static struct page *vmx_io_bitmap_b;
81
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +030082static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -080083 int size;
84 int order;
85 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +030086 u32 pin_based_exec_ctrl;
87 u32 cpu_based_exec_ctrl;
88 u32 vmexit_ctrl;
89 u32 vmentry_ctrl;
90} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -080091
92#define VMX_SEGMENT_FIELD(seg) \
93 [VCPU_SREG_##seg] = { \
94 .selector = GUEST_##seg##_SELECTOR, \
95 .base = GUEST_##seg##_BASE, \
96 .limit = GUEST_##seg##_LIMIT, \
97 .ar_bytes = GUEST_##seg##_AR_BYTES, \
98 }
99
100static struct kvm_vmx_segment_field {
101 unsigned selector;
102 unsigned base;
103 unsigned limit;
104 unsigned ar_bytes;
105} kvm_vmx_segment_fields[] = {
106 VMX_SEGMENT_FIELD(CS),
107 VMX_SEGMENT_FIELD(DS),
108 VMX_SEGMENT_FIELD(ES),
109 VMX_SEGMENT_FIELD(FS),
110 VMX_SEGMENT_FIELD(GS),
111 VMX_SEGMENT_FIELD(SS),
112 VMX_SEGMENT_FIELD(TR),
113 VMX_SEGMENT_FIELD(LDTR),
114};
115
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300116/*
117 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
118 * away by decrementing the array size.
119 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800121#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
123#endif
124 MSR_EFER, MSR_K6_STAR,
125};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200126#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800127
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400128static void load_msrs(struct kvm_msr_entry *e, int n)
129{
130 int i;
131
132 for (i = 0; i < n; ++i)
133 wrmsrl(e[i].index, e[i].data);
134}
135
136static void save_msrs(struct kvm_msr_entry *e, int n)
137{
138 int i;
139
140 for (i = 0; i < n; ++i)
141 rdmsrl(e[i].index, e[i].data);
142}
143
Avi Kivity6aa8b732006-12-10 02:21:36 -0800144static inline int is_page_fault(u32 intr_info)
145{
146 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
147 INTR_INFO_VALID_MASK)) ==
148 (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
149}
150
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300151static inline int is_no_device(u32 intr_info)
152{
153 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
154 INTR_INFO_VALID_MASK)) ==
155 (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
156}
157
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500158static inline int is_invalid_opcode(u32 intr_info)
159{
160 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
161 INTR_INFO_VALID_MASK)) ==
162 (INTR_TYPE_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
163}
164
Avi Kivity6aa8b732006-12-10 02:21:36 -0800165static inline int is_external_interrupt(u32 intr_info)
166{
167 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
168 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
169}
170
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800171static inline int cpu_has_vmx_tpr_shadow(void)
172{
173 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW);
174}
175
176static inline int vm_need_tpr_shadow(struct kvm *kvm)
177{
178 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)));
179}
180
Rusty Russell8b9cf982007-07-30 16:31:43 +1000181static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800182{
183 int i;
184
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400185 for (i = 0; i < vmx->nmsrs; ++i)
186 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300187 return i;
188 return -1;
189}
190
Rusty Russell8b9cf982007-07-30 16:31:43 +1000191static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300192{
193 int i;
194
Rusty Russell8b9cf982007-07-30 16:31:43 +1000195 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300196 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400197 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000198 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800199}
200
Avi Kivity6aa8b732006-12-10 02:21:36 -0800201static void vmcs_clear(struct vmcs *vmcs)
202{
203 u64 phys_addr = __pa(vmcs);
204 u8 error;
205
206 asm volatile (ASM_VMX_VMCLEAR_RAX "; setna %0"
207 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
208 : "cc", "memory");
209 if (error)
210 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
211 vmcs, phys_addr);
212}
213
214static void __vcpu_clear(void *arg)
215{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000216 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800217 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218
Rusty Russell8b9cf982007-07-30 16:31:43 +1000219 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400220 vmcs_clear(vmx->vmcs);
221 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222 per_cpu(current_vmcs, cpu) = NULL;
Rusty Russell8b9cf982007-07-30 16:31:43 +1000223 rdtscll(vmx->vcpu.host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224}
225
Rusty Russell8b9cf982007-07-30 16:31:43 +1000226static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800227{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200228 if (vmx->vcpu.cpu == -1)
229 return;
Avi Kivityf566e092007-09-30 11:02:53 +0200230 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 0, 1);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000231 vmx->launched = 0;
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800232}
233
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234static unsigned long vmcs_readl(unsigned long field)
235{
236 unsigned long value;
237
238 asm volatile (ASM_VMX_VMREAD_RDX_RAX
239 : "=a"(value) : "d"(field) : "cc");
240 return value;
241}
242
243static u16 vmcs_read16(unsigned long field)
244{
245 return vmcs_readl(field);
246}
247
248static u32 vmcs_read32(unsigned long field)
249{
250 return vmcs_readl(field);
251}
252
253static u64 vmcs_read64(unsigned long field)
254{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800255#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800256 return vmcs_readl(field);
257#else
258 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
259#endif
260}
261
Avi Kivitye52de1b2007-01-05 16:36:56 -0800262static noinline void vmwrite_error(unsigned long field, unsigned long value)
263{
264 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
265 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
266 dump_stack();
267}
268
Avi Kivity6aa8b732006-12-10 02:21:36 -0800269static void vmcs_writel(unsigned long field, unsigned long value)
270{
271 u8 error;
272
273 asm volatile (ASM_VMX_VMWRITE_RAX_RDX "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400274 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800275 if (unlikely(error))
276 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800277}
278
279static void vmcs_write16(unsigned long field, u16 value)
280{
281 vmcs_writel(field, value);
282}
283
284static void vmcs_write32(unsigned long field, u32 value)
285{
286 vmcs_writel(field, value);
287}
288
289static void vmcs_write64(unsigned long field, u64 value)
290{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800291#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800292 vmcs_writel(field, value);
293#else
294 vmcs_writel(field, value);
295 asm volatile ("");
296 vmcs_writel(field+1, value >> 32);
297#endif
298}
299
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300300static void vmcs_clear_bits(unsigned long field, u32 mask)
301{
302 vmcs_writel(field, vmcs_readl(field) & ~mask);
303}
304
305static void vmcs_set_bits(unsigned long field, u32 mask)
306{
307 vmcs_writel(field, vmcs_readl(field) | mask);
308}
309
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300310static void update_exception_bitmap(struct kvm_vcpu *vcpu)
311{
312 u32 eb;
313
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500314 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300315 if (!vcpu->fpu_active)
316 eb |= 1u << NM_VECTOR;
317 if (vcpu->guest_debug.enabled)
318 eb |= 1u << 1;
319 if (vcpu->rmode.active)
320 eb = ~0;
321 vmcs_write32(EXCEPTION_BITMAP, eb);
322}
323
Avi Kivity33ed6322007-05-02 16:54:03 +0300324static void reload_tss(void)
325{
326#ifndef CONFIG_X86_64
327
328 /*
329 * VT restores TR but not its size. Useless.
330 */
331 struct descriptor_table gdt;
332 struct segment_descriptor *descs;
333
334 get_gdt(&gdt);
335 descs = (void *)gdt.base;
336 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
337 load_TR_desc();
338#endif
339}
340
Rusty Russell8b9cf982007-07-30 16:31:43 +1000341static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300342{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400343 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300344 u64 host_efer = vmx->host_msrs[efer_offset].data;
345 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
346 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300347
Avi Kivity51c6cf62007-08-29 03:48:05 +0300348 if (efer_offset < 0)
349 return;
350 /*
351 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
352 * outside long mode
353 */
354 ignore_bits = EFER_NX | EFER_SCE;
355#ifdef CONFIG_X86_64
356 ignore_bits |= EFER_LMA | EFER_LME;
357 /* SCE is meaningful only in long mode on Intel */
358 if (guest_efer & EFER_LMA)
359 ignore_bits &= ~(u64)EFER_SCE;
360#endif
361 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
362 return;
363
364 vmx->host_state.guest_efer_loaded = 1;
365 guest_efer &= ~ignore_bits;
366 guest_efer |= host_efer & ignore_bits;
367 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000368 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300369}
370
Avi Kivity51c6cf62007-08-29 03:48:05 +0300371static void reload_host_efer(struct vcpu_vmx *vmx)
372{
373 if (vmx->host_state.guest_efer_loaded) {
374 vmx->host_state.guest_efer_loaded = 0;
375 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
376 }
377}
378
Avi Kivity04d2cc72007-09-10 18:10:54 +0300379static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300380{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300381 struct vcpu_vmx *vmx = to_vmx(vcpu);
382
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400383 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300384 return;
385
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400386 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300387 /*
388 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
389 * allow segment selectors with cpl > 0 or ti == 1.
390 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400391 vmx->host_state.ldt_sel = read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200392 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400393 vmx->host_state.fs_sel = read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200394 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400395 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200396 vmx->host_state.fs_reload_needed = 0;
397 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300398 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200399 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300400 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400401 vmx->host_state.gs_sel = read_gs();
402 if (!(vmx->host_state.gs_sel & 7))
403 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300404 else {
405 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200406 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300407 }
408
409#ifdef CONFIG_X86_64
410 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
411 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
412#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400413 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
414 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300415#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300416
417#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400418 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400419 save_msrs(vmx->host_msrs +
420 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400421
Avi Kivity707c0872007-05-02 17:33:43 +0300422#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400423 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300424 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300425}
426
Rusty Russell8b9cf982007-07-30 16:31:43 +1000427static void vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300428{
Avi Kivity15ad7142007-07-11 18:17:21 +0300429 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300430
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400431 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300432 return;
433
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400434 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200435 if (vmx->host_state.fs_reload_needed)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400436 load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200437 if (vmx->host_state.gs_ldt_reload_needed) {
438 load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300439 /*
440 * If we have to reload gs, we must take care to
441 * preserve our gs base.
442 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300443 local_irq_save(flags);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400444 load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300445#ifdef CONFIG_X86_64
446 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
447#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300448 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300449 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200450 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400451 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
452 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300453 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300454}
455
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456/*
457 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
458 * vcpu mutex is already taken.
459 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300460static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400462 struct vcpu_vmx *vmx = to_vmx(vcpu);
463 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity77002702007-06-13 19:55:28 +0300464 u64 tsc_this, delta;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800465
Eddie Donga3d7f852007-09-03 16:15:12 +0300466 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000467 vcpu_clear(vmx);
Eddie Donga3d7f852007-09-03 16:15:12 +0300468 kvm_migrate_apic_timer(vcpu);
469 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400471 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472 u8 error;
473
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400474 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475 asm volatile (ASM_VMX_VMPTRLD_RAX "; setna %0"
476 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
477 : "cc");
478 if (error)
479 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400480 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481 }
482
483 if (vcpu->cpu != cpu) {
484 struct descriptor_table dt;
485 unsigned long sysenter_esp;
486
487 vcpu->cpu = cpu;
488 /*
489 * Linux uses per-cpu TSS and GDT, so set these when switching
490 * processors.
491 */
492 vmcs_writel(HOST_TR_BASE, read_tr_base()); /* 22.2.4 */
493 get_gdt(&dt);
494 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
495
496 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
497 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300498
499 /*
500 * Make sure the time stamp counter is monotonous.
501 */
502 rdtscll(tsc_this);
503 delta = vcpu->host_tsc - tsc_this;
504 vmcs_write64(TSC_OFFSET, vmcs_read64(TSC_OFFSET) + delta);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800505 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506}
507
508static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
509{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000510 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity7702fd12007-06-14 16:27:40 +0300511 kvm_put_guest_fpu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800512}
513
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300514static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
515{
516 if (vcpu->fpu_active)
517 return;
518 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000519 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
520 if (vcpu->cr0 & X86_CR0_TS)
521 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300522 update_exception_bitmap(vcpu);
523}
524
525static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
526{
527 if (!vcpu->fpu_active)
528 return;
529 vcpu->fpu_active = 0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000530 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300531 update_exception_bitmap(vcpu);
532}
533
Avi Kivity774c47f2007-02-12 00:54:47 -0800534static void vmx_vcpu_decache(struct kvm_vcpu *vcpu)
535{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000536 vcpu_clear(to_vmx(vcpu));
Avi Kivity774c47f2007-02-12 00:54:47 -0800537}
538
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
540{
541 return vmcs_readl(GUEST_RFLAGS);
542}
543
544static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
545{
Avi Kivity78f78268682007-10-16 19:06:15 +0200546 if (vcpu->rmode.active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100547 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800548 vmcs_writel(GUEST_RFLAGS, rflags);
549}
550
551static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
552{
553 unsigned long rip;
554 u32 interruptibility;
555
556 rip = vmcs_readl(GUEST_RIP);
557 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
558 vmcs_writel(GUEST_RIP, rip);
559
560 /*
561 * We emulated an instruction, so temporary interrupt blocking
562 * should be removed, if set.
563 */
564 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
565 if (interruptibility & 3)
566 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
567 interruptibility & ~3);
Dor Laorc1150d82007-01-05 16:36:24 -0800568 vcpu->interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800569}
570
571static void vmx_inject_gp(struct kvm_vcpu *vcpu, unsigned error_code)
572{
573 printk(KERN_DEBUG "inject_general_protection: rip 0x%lx\n",
574 vmcs_readl(GUEST_RIP));
575 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
576 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
577 GP_VECTOR |
578 INTR_TYPE_EXCEPTION |
579 INTR_INFO_DELIEVER_CODE_MASK |
580 INTR_INFO_VALID_MASK);
581}
582
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500583static void vmx_inject_ud(struct kvm_vcpu *vcpu)
584{
585 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
586 UD_VECTOR |
587 INTR_TYPE_EXCEPTION |
588 INTR_INFO_VALID_MASK);
589}
590
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591/*
Eddie Donga75beee2007-05-17 18:55:15 +0300592 * Swap MSR entry in host/guest MSR entry array.
593 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200594#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000595static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300596{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400597 struct kvm_msr_entry tmp;
598
599 tmp = vmx->guest_msrs[to];
600 vmx->guest_msrs[to] = vmx->guest_msrs[from];
601 vmx->guest_msrs[from] = tmp;
602 tmp = vmx->host_msrs[to];
603 vmx->host_msrs[to] = vmx->host_msrs[from];
604 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300605}
Gabriel C54e11fa2007-08-01 16:23:10 +0200606#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300607
608/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300609 * Set up the vmcs to automatically save and restore system
610 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
611 * mode, as fiddling with msrs is very expensive.
612 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000613static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300614{
Eddie Dong2cc51562007-05-21 07:28:09 +0300615 int save_nmsrs;
Avi Kivitye38aea32007-04-19 13:22:48 +0300616
Eddie Donga75beee2007-05-17 18:55:15 +0300617 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300618#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000619 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300620 int index;
621
Rusty Russell8b9cf982007-07-30 16:31:43 +1000622 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300623 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000624 move_msr_up(vmx, index, save_nmsrs++);
625 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300626 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000627 move_msr_up(vmx, index, save_nmsrs++);
628 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300629 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000630 move_msr_up(vmx, index, save_nmsrs++);
631 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300632 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000633 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300634 /*
635 * MSR_K6_STAR is only needed on long mode guests, and only
636 * if efer.sce is enabled.
637 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000638 index = __find_msr_index(vmx, MSR_K6_STAR);
639 if ((index >= 0) && (vmx->vcpu.shadow_efer & EFER_SCE))
640 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300641 }
Eddie Donga75beee2007-05-17 18:55:15 +0300642#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400643 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300644
Eddie Donga75beee2007-05-17 18:55:15 +0300645#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400646 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000647 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300648#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000649 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivitye38aea32007-04-19 13:22:48 +0300650}
651
652/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653 * reads and returns guest's timestamp counter "register"
654 * guest_tsc = host_tsc + tsc_offset -- 21.3
655 */
656static u64 guest_read_tsc(void)
657{
658 u64 host_tsc, tsc_offset;
659
660 rdtscll(host_tsc);
661 tsc_offset = vmcs_read64(TSC_OFFSET);
662 return host_tsc + tsc_offset;
663}
664
665/*
666 * writes 'guest_tsc' into guest's timestamp counter "register"
667 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
668 */
669static void guest_write_tsc(u64 guest_tsc)
670{
671 u64 host_tsc;
672
673 rdtscll(host_tsc);
674 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
675}
676
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677/*
678 * Reads an msr value (of 'msr_index') into 'pdata'.
679 * Returns 0 on success, non-0 otherwise.
680 * Assumes vcpu_load() was already called.
681 */
682static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
683{
684 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400685 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800686
687 if (!pdata) {
688 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
689 return -EINVAL;
690 }
691
692 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800693#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800694 case MSR_FS_BASE:
695 data = vmcs_readl(GUEST_FS_BASE);
696 break;
697 case MSR_GS_BASE:
698 data = vmcs_readl(GUEST_GS_BASE);
699 break;
700 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800701 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800702#endif
703 case MSR_IA32_TIME_STAMP_COUNTER:
704 data = guest_read_tsc();
705 break;
706 case MSR_IA32_SYSENTER_CS:
707 data = vmcs_read32(GUEST_SYSENTER_CS);
708 break;
709 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200710 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711 break;
712 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200713 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000716 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800717 if (msr) {
718 data = msr->data;
719 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800720 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800721 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800722 }
723
724 *pdata = data;
725 return 0;
726}
727
728/*
729 * Writes msr value into into the appropriate "register".
730 * Returns 0 on success, non-0 otherwise.
731 * Assumes vcpu_load() was already called.
732 */
733static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
734{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400735 struct vcpu_vmx *vmx = to_vmx(vcpu);
736 struct kvm_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +0300737 int ret = 0;
738
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800740#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800741 case MSR_EFER:
Eddie Dong2cc51562007-05-21 07:28:09 +0300742 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300743 if (vmx->host_state.loaded) {
744 reload_host_efer(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000745 load_transition_efer(vmx);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300746 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300747 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800748 case MSR_FS_BASE:
749 vmcs_writel(GUEST_FS_BASE, data);
750 break;
751 case MSR_GS_BASE:
752 vmcs_writel(GUEST_GS_BASE, data);
753 break;
754#endif
755 case MSR_IA32_SYSENTER_CS:
756 vmcs_write32(GUEST_SYSENTER_CS, data);
757 break;
758 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200759 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760 break;
761 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200762 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800763 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200764 case MSR_IA32_TIME_STAMP_COUNTER:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800765 guest_write_tsc(data);
766 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000768 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800769 if (msr) {
770 msr->data = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400771 if (vmx->host_state.loaded)
772 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800773 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300775 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776 }
777
Eddie Dong2cc51562007-05-21 07:28:09 +0300778 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779}
780
781/*
782 * Sync the rsp and rip registers into the vcpu structure. This allows
783 * registers to be accessed by indexing vcpu->regs.
784 */
785static void vcpu_load_rsp_rip(struct kvm_vcpu *vcpu)
786{
787 vcpu->regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
788 vcpu->rip = vmcs_readl(GUEST_RIP);
789}
790
791/*
792 * Syncs rsp and rip back into the vmcs. Should be called after possible
793 * modification.
794 */
795static void vcpu_put_rsp_rip(struct kvm_vcpu *vcpu)
796{
797 vmcs_writel(GUEST_RSP, vcpu->regs[VCPU_REGS_RSP]);
798 vmcs_writel(GUEST_RIP, vcpu->rip);
799}
800
801static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
802{
803 unsigned long dr7 = 0x400;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804 int old_singlestep;
805
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806 old_singlestep = vcpu->guest_debug.singlestep;
807
808 vcpu->guest_debug.enabled = dbg->enabled;
809 if (vcpu->guest_debug.enabled) {
810 int i;
811
812 dr7 |= 0x200; /* exact */
813 for (i = 0; i < 4; ++i) {
814 if (!dbg->breakpoints[i].enabled)
815 continue;
816 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
817 dr7 |= 2 << (i*2); /* global enable */
818 dr7 |= 0 << (i*4+16); /* execution breakpoint */
819 }
820
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821 vcpu->guest_debug.singlestep = dbg->singlestep;
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300822 } else
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823 vcpu->guest_debug.singlestep = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824
825 if (old_singlestep && !vcpu->guest_debug.singlestep) {
826 unsigned long flags;
827
828 flags = vmcs_readl(GUEST_RFLAGS);
829 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
830 vmcs_writel(GUEST_RFLAGS, flags);
831 }
832
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300833 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834 vmcs_writel(GUEST_DR7, dr7);
835
836 return 0;
837}
838
Eddie Dong2a8067f2007-08-06 16:29:07 +0300839static int vmx_get_irq(struct kvm_vcpu *vcpu)
840{
841 u32 idtv_info_field;
842
843 idtv_info_field = vmcs_read32(IDT_VECTORING_INFO_FIELD);
844 if (idtv_info_field & INTR_INFO_VALID_MASK) {
845 if (is_external_interrupt(idtv_info_field))
846 return idtv_info_field & VECTORING_INFO_VECTOR_MASK;
847 else
Mike Dayd77c26f2007-10-08 09:02:08 -0400848 printk(KERN_DEBUG "pending exception: not handled yet\n");
Eddie Dong2a8067f2007-08-06 16:29:07 +0300849 }
850 return -1;
851}
852
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853static __init int cpu_has_kvm_support(void)
854{
855 unsigned long ecx = cpuid_ecx(1);
856 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
857}
858
859static __init int vmx_disabled_by_bios(void)
860{
861 u64 msr;
862
863 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300864 return (msr & (MSR_IA32_FEATURE_CONTROL_LOCKED |
865 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
866 == MSR_IA32_FEATURE_CONTROL_LOCKED;
867 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868}
869
Avi Kivity774c47f2007-02-12 00:54:47 -0800870static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871{
872 int cpu = raw_smp_processor_id();
873 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
874 u64 old;
875
876 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300877 if ((old & (MSR_IA32_FEATURE_CONTROL_LOCKED |
878 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
879 != (MSR_IA32_FEATURE_CONTROL_LOCKED |
880 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300882 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
883 MSR_IA32_FEATURE_CONTROL_LOCKED |
884 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +1000885 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886 asm volatile (ASM_VMX_VMXON_RAX : : "a"(&phys_addr), "m"(phys_addr)
887 : "memory", "cc");
888}
889
890static void hardware_disable(void *garbage)
891{
892 asm volatile (ASM_VMX_VMXOFF : : : "cc");
893}
894
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300895static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -0400896 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897{
898 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300899 u32 ctl = ctl_min | ctl_opt;
900
901 rdmsr(msr, vmx_msr_low, vmx_msr_high);
902
903 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
904 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
905
906 /* Ensure minimum (required) set of control bits are supported. */
907 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300908 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300909
910 *result = ctl;
911 return 0;
912}
913
Yang, Sheng002c7f72007-07-31 14:23:01 +0300914static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300915{
916 u32 vmx_msr_low, vmx_msr_high;
917 u32 min, opt;
918 u32 _pin_based_exec_control = 0;
919 u32 _cpu_based_exec_control = 0;
920 u32 _vmexit_control = 0;
921 u32 _vmentry_control = 0;
922
923 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
924 opt = 0;
925 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
926 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300927 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300928
929 min = CPU_BASED_HLT_EXITING |
930#ifdef CONFIG_X86_64
931 CPU_BASED_CR8_LOAD_EXITING |
932 CPU_BASED_CR8_STORE_EXITING |
933#endif
934 CPU_BASED_USE_IO_BITMAPS |
935 CPU_BASED_MOV_DR_EXITING |
936 CPU_BASED_USE_TSC_OFFSETING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800937#ifdef CONFIG_X86_64
938 opt = CPU_BASED_TPR_SHADOW;
939#else
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300940 opt = 0;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800941#endif
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300942 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
943 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300944 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800945#ifdef CONFIG_X86_64
946 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
947 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
948 ~CPU_BASED_CR8_STORE_EXITING;
949#endif
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300950
951 min = 0;
952#ifdef CONFIG_X86_64
953 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
954#endif
955 opt = 0;
956 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
957 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300958 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300959
960 min = opt = 0;
961 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
962 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300963 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -0800965 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300966
967 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
968 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300969 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300970
971#ifdef CONFIG_X86_64
972 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
973 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +0300974 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300975#endif
976
977 /* Require Write-Back (WB) memory type for VMCS accesses. */
978 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300979 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300980
Yang, Sheng002c7f72007-07-31 14:23:01 +0300981 vmcs_conf->size = vmx_msr_high & 0x1fff;
982 vmcs_conf->order = get_order(vmcs_config.size);
983 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300984
Yang, Sheng002c7f72007-07-31 14:23:01 +0300985 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
986 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
987 vmcs_conf->vmexit_ctrl = _vmexit_control;
988 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300989
990 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -0800991}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992
993static struct vmcs *alloc_vmcs_cpu(int cpu)
994{
995 int node = cpu_to_node(cpu);
996 struct page *pages;
997 struct vmcs *vmcs;
998
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300999 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000 if (!pages)
1001 return NULL;
1002 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001003 memset(vmcs, 0, vmcs_config.size);
1004 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005 return vmcs;
1006}
1007
1008static struct vmcs *alloc_vmcs(void)
1009{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001010 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011}
1012
1013static void free_vmcs(struct vmcs *vmcs)
1014{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001015 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016}
1017
Sam Ravnborg39959582007-06-01 00:47:13 -07001018static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019{
1020 int cpu;
1021
1022 for_each_online_cpu(cpu)
1023 free_vmcs(per_cpu(vmxarea, cpu));
1024}
1025
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026static __init int alloc_kvm_area(void)
1027{
1028 int cpu;
1029
1030 for_each_online_cpu(cpu) {
1031 struct vmcs *vmcs;
1032
1033 vmcs = alloc_vmcs_cpu(cpu);
1034 if (!vmcs) {
1035 free_kvm_area();
1036 return -ENOMEM;
1037 }
1038
1039 per_cpu(vmxarea, cpu) = vmcs;
1040 }
1041 return 0;
1042}
1043
1044static __init int hardware_setup(void)
1045{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001046 if (setup_vmcs_config(&vmcs_config) < 0)
1047 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048 return alloc_kvm_area();
1049}
1050
1051static __exit void hardware_unsetup(void)
1052{
1053 free_kvm_area();
1054}
1055
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1057{
1058 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1059
Avi Kivity6af11b92007-03-19 13:18:10 +02001060 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061 vmcs_write16(sf->selector, save->selector);
1062 vmcs_writel(sf->base, save->base);
1063 vmcs_write32(sf->limit, save->limit);
1064 vmcs_write32(sf->ar_bytes, save->ar);
1065 } else {
1066 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1067 << AR_DPL_SHIFT;
1068 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1069 }
1070}
1071
1072static void enter_pmode(struct kvm_vcpu *vcpu)
1073{
1074 unsigned long flags;
1075
1076 vcpu->rmode.active = 0;
1077
1078 vmcs_writel(GUEST_TR_BASE, vcpu->rmode.tr.base);
1079 vmcs_write32(GUEST_TR_LIMIT, vcpu->rmode.tr.limit);
1080 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->rmode.tr.ar);
1081
1082 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001083 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084 flags |= (vcpu->rmode.save_iopl << IOPL_SHIFT);
1085 vmcs_writel(GUEST_RFLAGS, flags);
1086
Rusty Russell66aee912007-07-17 23:34:16 +10001087 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1088 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089
1090 update_exception_bitmap(vcpu);
1091
1092 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->rmode.es);
1093 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->rmode.ds);
1094 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->rmode.gs);
1095 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->rmode.fs);
1096
1097 vmcs_write16(GUEST_SS_SELECTOR, 0);
1098 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1099
1100 vmcs_write16(GUEST_CS_SELECTOR,
1101 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1102 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1103}
1104
Mike Dayd77c26f2007-10-08 09:02:08 -04001105static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106{
1107 gfn_t base_gfn = kvm->memslots[0].base_gfn + kvm->memslots[0].npages - 3;
1108 return base_gfn << PAGE_SHIFT;
1109}
1110
1111static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1112{
1113 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1114
1115 save->selector = vmcs_read16(sf->selector);
1116 save->base = vmcs_readl(sf->base);
1117 save->limit = vmcs_read32(sf->limit);
1118 save->ar = vmcs_read32(sf->ar_bytes);
1119 vmcs_write16(sf->selector, vmcs_readl(sf->base) >> 4);
1120 vmcs_write32(sf->limit, 0xffff);
1121 vmcs_write32(sf->ar_bytes, 0xf3);
1122}
1123
1124static void enter_rmode(struct kvm_vcpu *vcpu)
1125{
1126 unsigned long flags;
1127
1128 vcpu->rmode.active = 1;
1129
1130 vcpu->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
1131 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1132
1133 vcpu->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
1134 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1135
1136 vcpu->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
1137 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1138
1139 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001140 vcpu->rmode.save_iopl = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001142 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143
1144 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001145 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001146 update_exception_bitmap(vcpu);
1147
1148 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1149 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1150 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1151
1152 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001153 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001154 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1155 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1157
1158 fix_rmode_seg(VCPU_SREG_ES, &vcpu->rmode.es);
1159 fix_rmode_seg(VCPU_SREG_DS, &vcpu->rmode.ds);
1160 fix_rmode_seg(VCPU_SREG_GS, &vcpu->rmode.gs);
1161 fix_rmode_seg(VCPU_SREG_FS, &vcpu->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001162
Eddie Dong8668a3c2007-10-10 14:26:45 +08001163 kvm_mmu_reset_context(vcpu);
Avi Kivity75880a02007-06-20 11:20:04 +03001164 init_rmode_tss(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165}
1166
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001167#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168
1169static void enter_lmode(struct kvm_vcpu *vcpu)
1170{
1171 u32 guest_tr_ar;
1172
1173 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1174 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1175 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
1176 __FUNCTION__);
1177 vmcs_write32(GUEST_TR_AR_BYTES,
1178 (guest_tr_ar & ~AR_TYPE_MASK)
1179 | AR_TYPE_BUSY_64_TSS);
1180 }
1181
1182 vcpu->shadow_efer |= EFER_LMA;
1183
Rusty Russell8b9cf982007-07-30 16:31:43 +10001184 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185 vmcs_write32(VM_ENTRY_CONTROLS,
1186 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001187 | VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188}
1189
1190static void exit_lmode(struct kvm_vcpu *vcpu)
1191{
1192 vcpu->shadow_efer &= ~EFER_LMA;
1193
1194 vmcs_write32(VM_ENTRY_CONTROLS,
1195 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001196 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001197}
1198
1199#endif
1200
Anthony Liguori25c4c272007-04-27 09:29:21 +03001201static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001202{
Avi Kivity399badf2007-01-05 16:36:38 -08001203 vcpu->cr4 &= KVM_GUEST_CR4_MASK;
1204 vcpu->cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
1205}
1206
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1208{
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001209 vmx_fpu_deactivate(vcpu);
1210
Rusty Russell707d92fa2007-07-17 23:19:08 +10001211 if (vcpu->rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212 enter_pmode(vcpu);
1213
Rusty Russell707d92fa2007-07-17 23:19:08 +10001214 if (!vcpu->rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001215 enter_rmode(vcpu);
1216
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001217#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001218 if (vcpu->shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001219 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001220 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001221 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222 exit_lmode(vcpu);
1223 }
1224#endif
1225
1226 vmcs_writel(CR0_READ_SHADOW, cr0);
1227 vmcs_writel(GUEST_CR0,
1228 (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON);
1229 vcpu->cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001230
Rusty Russell707d92fa2007-07-17 23:19:08 +10001231 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001232 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001233}
1234
Avi Kivity6aa8b732006-12-10 02:21:36 -08001235static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1236{
1237 vmcs_writel(GUEST_CR3, cr3);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001238 if (vcpu->cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001239 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001240}
1241
1242static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1243{
1244 vmcs_writel(CR4_READ_SHADOW, cr4);
1245 vmcs_writel(GUEST_CR4, cr4 | (vcpu->rmode.active ?
1246 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON));
1247 vcpu->cr4 = cr4;
1248}
1249
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001250#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251
1252static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1253{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001254 struct vcpu_vmx *vmx = to_vmx(vcpu);
1255 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001256
1257 vcpu->shadow_efer = efer;
1258 if (efer & EFER_LMA) {
1259 vmcs_write32(VM_ENTRY_CONTROLS,
1260 vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001261 VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262 msr->data = efer;
1263
1264 } else {
1265 vmcs_write32(VM_ENTRY_CONTROLS,
1266 vmcs_read32(VM_ENTRY_CONTROLS) &
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001267 ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268
1269 msr->data = efer & ~EFER_LME;
1270 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001271 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272}
1273
1274#endif
1275
1276static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1277{
1278 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1279
1280 return vmcs_readl(sf->base);
1281}
1282
1283static void vmx_get_segment(struct kvm_vcpu *vcpu,
1284 struct kvm_segment *var, int seg)
1285{
1286 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1287 u32 ar;
1288
1289 var->base = vmcs_readl(sf->base);
1290 var->limit = vmcs_read32(sf->limit);
1291 var->selector = vmcs_read16(sf->selector);
1292 ar = vmcs_read32(sf->ar_bytes);
1293 if (ar & AR_UNUSABLE_MASK)
1294 ar = 0;
1295 var->type = ar & 15;
1296 var->s = (ar >> 4) & 1;
1297 var->dpl = (ar >> 5) & 3;
1298 var->present = (ar >> 7) & 1;
1299 var->avl = (ar >> 12) & 1;
1300 var->l = (ar >> 13) & 1;
1301 var->db = (ar >> 14) & 1;
1302 var->g = (ar >> 15) & 1;
1303 var->unusable = (ar >> 16) & 1;
1304}
1305
Avi Kivity653e3102007-05-07 10:55:37 +03001306static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001308 u32 ar;
1309
Avi Kivity653e3102007-05-07 10:55:37 +03001310 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001311 ar = 1 << 16;
1312 else {
1313 ar = var->type & 15;
1314 ar |= (var->s & 1) << 4;
1315 ar |= (var->dpl & 3) << 5;
1316 ar |= (var->present & 1) << 7;
1317 ar |= (var->avl & 1) << 12;
1318 ar |= (var->l & 1) << 13;
1319 ar |= (var->db & 1) << 14;
1320 ar |= (var->g & 1) << 15;
1321 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001322 if (ar == 0) /* a 0 value means unusable */
1323 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001324
1325 return ar;
1326}
1327
1328static void vmx_set_segment(struct kvm_vcpu *vcpu,
1329 struct kvm_segment *var, int seg)
1330{
1331 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1332 u32 ar;
1333
1334 if (vcpu->rmode.active && seg == VCPU_SREG_TR) {
1335 vcpu->rmode.tr.selector = var->selector;
1336 vcpu->rmode.tr.base = var->base;
1337 vcpu->rmode.tr.limit = var->limit;
1338 vcpu->rmode.tr.ar = vmx_segment_access_rights(var);
1339 return;
1340 }
1341 vmcs_writel(sf->base, var->base);
1342 vmcs_write32(sf->limit, var->limit);
1343 vmcs_write16(sf->selector, var->selector);
1344 if (vcpu->rmode.active && var->s) {
1345 /*
1346 * Hack real-mode segments into vm86 compatibility.
1347 */
1348 if (var->base == 0xffff0000 && var->selector == 0xf000)
1349 vmcs_writel(sf->base, 0xf0000);
1350 ar = 0xf3;
1351 } else
1352 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353 vmcs_write32(sf->ar_bytes, ar);
1354}
1355
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1357{
1358 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1359
1360 *db = (ar >> 14) & 1;
1361 *l = (ar >> 13) & 1;
1362}
1363
1364static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1365{
1366 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1367 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1368}
1369
1370static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1371{
1372 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1373 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1374}
1375
1376static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1377{
1378 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1379 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1380}
1381
1382static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1383{
1384 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1385 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1386}
1387
Mike Dayd77c26f2007-10-08 09:02:08 -04001388static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001391 u16 data = 0;
1392 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393
Izik Eidus195aefd2007-10-01 22:14:18 +02001394 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1395 if (r < 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396 return 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02001397 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
1398 r = kvm_write_guest_page(kvm, fn++, &data, 0x66, sizeof(u16));
1399 if (r < 0)
1400 return 0;
1401 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
1402 if (r < 0)
1403 return 0;
1404 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1405 if (r < 0)
1406 return 0;
1407 data = ~0;
1408 r = kvm_write_guest_page(kvm, fn, &data, RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
1409 sizeof(u8));
1410 if (r < 0)
1411 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412 return 1;
1413}
1414
Avi Kivity6aa8b732006-12-10 02:21:36 -08001415static void seg_setup(int seg)
1416{
1417 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1418
1419 vmcs_write16(sf->selector, 0);
1420 vmcs_writel(sf->base, 0);
1421 vmcs_write32(sf->limit, 0xffff);
1422 vmcs_write32(sf->ar_bytes, 0x93);
1423}
1424
1425/*
1426 * Sets up the vmcs for emulated real mode.
1427 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001428static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429{
1430 u32 host_sysenter_cs;
1431 u32 junk;
1432 unsigned long a;
1433 struct descriptor_table dt;
1434 int i;
1435 int ret = 0;
Avi Kivitycd2276a2007-05-14 20:41:13 +03001436 unsigned long kvm_vmx_return;
Eddie Dong7017fc32007-07-18 11:34:57 +03001437 u64 msr;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001438 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439
Rusty Russell8b9cf982007-07-30 16:31:43 +10001440 if (!init_rmode_tss(vmx->vcpu.kvm)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441 ret = -ENOMEM;
1442 goto out;
1443 }
1444
He, Qingc5ec1532007-09-03 17:07:41 +03001445 vmx->vcpu.rmode.active = 0;
1446
Rusty Russell8b9cf982007-07-30 16:31:43 +10001447 vmx->vcpu.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Eddie Dong7017fc32007-07-18 11:34:57 +03001448 set_cr8(&vmx->vcpu, 0);
1449 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Rusty Russell8b9cf982007-07-30 16:31:43 +10001450 if (vmx->vcpu.vcpu_id == 0)
Eddie Dong7017fc32007-07-18 11:34:57 +03001451 msr |= MSR_IA32_APICBASE_BSP;
1452 kvm_set_apic_base(&vmx->vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453
Rusty Russell8b9cf982007-07-30 16:31:43 +10001454 fx_init(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455
1456 /*
1457 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
1458 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
1459 */
He, Qingc5ec1532007-09-03 17:07:41 +03001460 if (vmx->vcpu.vcpu_id == 0) {
1461 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
1462 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
1463 } else {
1464 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.sipi_vector << 8);
1465 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.sipi_vector << 12);
1466 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001467 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
1468 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1469
1470 seg_setup(VCPU_SREG_DS);
1471 seg_setup(VCPU_SREG_ES);
1472 seg_setup(VCPU_SREG_FS);
1473 seg_setup(VCPU_SREG_GS);
1474 seg_setup(VCPU_SREG_SS);
1475
1476 vmcs_write16(GUEST_TR_SELECTOR, 0);
1477 vmcs_writel(GUEST_TR_BASE, 0);
1478 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
1479 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1480
1481 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
1482 vmcs_writel(GUEST_LDTR_BASE, 0);
1483 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
1484 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
1485
1486 vmcs_write32(GUEST_SYSENTER_CS, 0);
1487 vmcs_writel(GUEST_SYSENTER_ESP, 0);
1488 vmcs_writel(GUEST_SYSENTER_EIP, 0);
1489
1490 vmcs_writel(GUEST_RFLAGS, 0x02);
He, Qingc5ec1532007-09-03 17:07:41 +03001491 if (vmx->vcpu.vcpu_id == 0)
1492 vmcs_writel(GUEST_RIP, 0xfff0);
1493 else
1494 vmcs_writel(GUEST_RIP, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495 vmcs_writel(GUEST_RSP, 0);
1496
Mike Dayd77c26f2007-10-08 09:02:08 -04001497 /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498 vmcs_writel(GUEST_DR7, 0x400);
1499
1500 vmcs_writel(GUEST_GDTR_BASE, 0);
1501 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
1502
1503 vmcs_writel(GUEST_IDTR_BASE, 0);
1504 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
1505
1506 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
1507 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
1508 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
1509
1510 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03001511 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
1512 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001513
1514 guest_write_tsc(0);
1515
1516 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
1517
1518 /* Special registers */
1519 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
1520
1521 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001522 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
1523 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001524
1525 exec_control = vmcs_config.cpu_based_exec_ctrl;
1526 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
1527 exec_control &= ~CPU_BASED_TPR_SHADOW;
1528#ifdef CONFIG_X86_64
1529 exec_control |= CPU_BASED_CR8_STORE_EXITING |
1530 CPU_BASED_CR8_LOAD_EXITING;
1531#endif
1532 }
1533 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001534
Avi Kivityc7addb92007-09-16 18:58:32 +02001535 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
1536 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
1538
1539 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
1540 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
1541 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1542
1543 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
1544 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1545 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1546 vmcs_write16(HOST_FS_SELECTOR, read_fs()); /* 22.2.4 */
1547 vmcs_write16(HOST_GS_SELECTOR, read_gs()); /* 22.2.4 */
1548 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001549#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001550 rdmsrl(MSR_FS_BASE, a);
1551 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
1552 rdmsrl(MSR_GS_BASE, a);
1553 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
1554#else
1555 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
1556 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
1557#endif
1558
1559 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
1560
1561 get_idt(&dt);
1562 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
1563
Mike Dayd77c26f2007-10-08 09:02:08 -04001564 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03001565 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03001566 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
1567 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
1568 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001569
1570 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
1571 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
1572 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
1573 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
1574 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
1575 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
1576
Avi Kivity6aa8b732006-12-10 02:21:36 -08001577 for (i = 0; i < NR_VMX_MSR; ++i) {
1578 u32 index = vmx_msr_index[i];
1579 u32 data_low, data_high;
1580 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001581 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001582
1583 if (rdmsr_safe(index, &data_low, &data_high) < 0)
1584 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08001585 if (wrmsr_safe(index, data_low, data_high) < 0)
1586 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001587 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001588 vmx->host_msrs[j].index = index;
1589 vmx->host_msrs[j].reserved = 0;
1590 vmx->host_msrs[j].data = data;
1591 vmx->guest_msrs[j] = vmx->host_msrs[j];
1592 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594
Rusty Russell8b9cf982007-07-30 16:31:43 +10001595 setup_msrs(vmx);
Avi Kivitye38aea32007-04-19 13:22:48 +03001596
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001597 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001598
1599 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001600 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
1601
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
1603
Michael Riepe3b99ab22006-12-13 00:34:15 -08001604#ifdef CONFIG_X86_64
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001605 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
1606 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
1607 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
1608 page_to_phys(vmx->vcpu.apic->regs_page));
1609 vmcs_write32(TPR_THRESHOLD, 0);
Michael Riepe3b99ab22006-12-13 00:34:15 -08001610#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611
Anthony Liguori25c4c272007-04-27 09:29:21 +03001612 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
1614
Rusty Russell8b9cf982007-07-30 16:31:43 +10001615 vmx->vcpu.cr0 = 0x60000010;
Mike Dayd77c26f2007-10-08 09:02:08 -04001616 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001617 vmx_set_cr4(&vmx->vcpu, 0);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001618#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001619 vmx_set_efer(&vmx->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001620#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +10001621 vmx_fpu_activate(&vmx->vcpu);
1622 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001623
1624 return 0;
1625
Avi Kivity6aa8b732006-12-10 02:21:36 -08001626out:
1627 return ret;
1628}
1629
Avi Kivity04d2cc72007-09-10 18:10:54 +03001630static void vmx_vcpu_reset(struct kvm_vcpu *vcpu)
1631{
1632 struct vcpu_vmx *vmx = to_vmx(vcpu);
1633
1634 vmx_vcpu_setup(vmx);
1635}
1636
Avi Kivity6aa8b732006-12-10 02:21:36 -08001637static void inject_rmode_irq(struct kvm_vcpu *vcpu, int irq)
1638{
1639 u16 ent[2];
1640 u16 cs;
1641 u16 ip;
1642 unsigned long flags;
1643 unsigned long ss_base = vmcs_readl(GUEST_SS_BASE);
1644 u16 sp = vmcs_readl(GUEST_RSP);
1645 u32 ss_limit = vmcs_read32(GUEST_SS_LIMIT);
1646
Mike Dayd77c26f2007-10-08 09:02:08 -04001647 if (sp > ss_limit || sp < 6) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001648 vcpu_printf(vcpu, "%s: #SS, rsp 0x%lx ss 0x%lx limit 0x%x\n",
1649 __FUNCTION__,
1650 vmcs_readl(GUEST_RSP),
1651 vmcs_readl(GUEST_SS_BASE),
1652 vmcs_read32(GUEST_SS_LIMIT));
1653 return;
1654 }
1655
Laurent Viviere7d5d762007-07-30 13:41:19 +03001656 if (emulator_read_std(irq * sizeof(ent), &ent, sizeof(ent), vcpu) !=
1657 X86EMUL_CONTINUE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658 vcpu_printf(vcpu, "%s: read guest err\n", __FUNCTION__);
1659 return;
1660 }
1661
1662 flags = vmcs_readl(GUEST_RFLAGS);
1663 cs = vmcs_readl(GUEST_CS_BASE) >> 4;
1664 ip = vmcs_readl(GUEST_RIP);
1665
1666
Mike Dayd77c26f2007-10-08 09:02:08 -04001667 if (emulator_write_emulated(
1668 ss_base + sp - 2, &flags, 2, vcpu) != X86EMUL_CONTINUE ||
1669 emulator_write_emulated(
1670 ss_base + sp - 4, &cs, 2, vcpu) != X86EMUL_CONTINUE ||
1671 emulator_write_emulated(
1672 ss_base + sp - 6, &ip, 2, vcpu) != X86EMUL_CONTINUE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001673 vcpu_printf(vcpu, "%s: write guest err\n", __FUNCTION__);
1674 return;
1675 }
1676
1677 vmcs_writel(GUEST_RFLAGS, flags &
Mike Dayd77c26f2007-10-08 09:02:08 -04001678 ~(X86_EFLAGS_IF | X86_EFLAGS_AC | X86_EFLAGS_TF));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679 vmcs_write16(GUEST_CS_SELECTOR, ent[1]) ;
1680 vmcs_writel(GUEST_CS_BASE, ent[1] << 4);
1681 vmcs_writel(GUEST_RIP, ent[0]);
1682 vmcs_writel(GUEST_RSP, (vmcs_readl(GUEST_RSP) & ~0xffff) | (sp - 6));
1683}
1684
Eddie Dong85f455f2007-07-06 12:20:49 +03001685static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
1686{
1687 if (vcpu->rmode.active) {
1688 inject_rmode_irq(vcpu, irq);
1689 return;
1690 }
1691 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
1692 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1693}
1694
Avi Kivity6aa8b732006-12-10 02:21:36 -08001695static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
1696{
1697 int word_index = __ffs(vcpu->irq_summary);
1698 int bit_index = __ffs(vcpu->irq_pending[word_index]);
1699 int irq = word_index * BITS_PER_LONG + bit_index;
1700
1701 clear_bit(bit_index, &vcpu->irq_pending[word_index]);
1702 if (!vcpu->irq_pending[word_index])
1703 clear_bit(word_index, &vcpu->irq_summary);
Eddie Dong85f455f2007-07-06 12:20:49 +03001704 vmx_inject_irq(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705}
1706
Dor Laorc1150d82007-01-05 16:36:24 -08001707
1708static void do_interrupt_requests(struct kvm_vcpu *vcpu,
1709 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001710{
Dor Laorc1150d82007-01-05 16:36:24 -08001711 u32 cpu_based_vm_exec_control;
1712
1713 vcpu->interrupt_window_open =
1714 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
1715 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
1716
1717 if (vcpu->interrupt_window_open &&
1718 vcpu->irq_summary &&
1719 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720 /*
Dor Laorc1150d82007-01-05 16:36:24 -08001721 * If interrupts enabled, and not blocked by sti or mov ss. Good.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001722 */
1723 kvm_do_inject_irq(vcpu);
Dor Laorc1150d82007-01-05 16:36:24 -08001724
1725 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
1726 if (!vcpu->interrupt_window_open &&
1727 (vcpu->irq_summary || kvm_run->request_interrupt_window))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001728 /*
1729 * Interrupts blocked. Wait for unblock.
1730 */
Dor Laorc1150d82007-01-05 16:36:24 -08001731 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
1732 else
1733 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
1734 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001735}
1736
1737static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
1738{
1739 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
1740
1741 set_debugreg(dbg->bp[0], 0);
1742 set_debugreg(dbg->bp[1], 1);
1743 set_debugreg(dbg->bp[2], 2);
1744 set_debugreg(dbg->bp[3], 3);
1745
1746 if (dbg->singlestep) {
1747 unsigned long flags;
1748
1749 flags = vmcs_readl(GUEST_RFLAGS);
1750 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1751 vmcs_writel(GUEST_RFLAGS, flags);
1752 }
1753}
1754
1755static int handle_rmode_exception(struct kvm_vcpu *vcpu,
1756 int vec, u32 err_code)
1757{
1758 if (!vcpu->rmode.active)
1759 return 0;
1760
Nitin A Kambleb3f37702007-05-17 15:50:34 +03001761 /*
1762 * Instruction with address size override prefix opcode 0x67
1763 * Cause the #SS fault with 0 error code in VM86 mode.
1764 */
1765 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02001766 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001767 return 1;
1768 return 0;
1769}
1770
1771static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1772{
1773 u32 intr_info, error_code;
1774 unsigned long cr2, rip;
1775 u32 vect_info;
1776 enum emulation_result er;
Avi Kivitye2dec932007-01-05 16:36:54 -08001777 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778
1779 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
1780 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
1781
1782 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04001783 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001784 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
1785 "intr info 0x%x\n", __FUNCTION__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001786
Eddie Dong85f455f2007-07-06 12:20:49 +03001787 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001788 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
1789 set_bit(irq, vcpu->irq_pending);
1790 set_bit(irq / BITS_PER_LONG, &vcpu->irq_summary);
1791 }
1792
Avi Kivity1b6269d2007-10-09 12:12:19 +02001793 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
1794 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001795
1796 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001797 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001798 return 1;
1799 }
1800
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001801 if (is_invalid_opcode(intr_info)) {
Laurent Vivier34273182007-09-18 11:27:37 +02001802 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001803 if (er != EMULATE_DONE)
1804 vmx_inject_ud(vcpu);
1805
1806 return 1;
1807 }
1808
Avi Kivity6aa8b732006-12-10 02:21:36 -08001809 error_code = 0;
1810 rip = vmcs_readl(GUEST_RIP);
1811 if (intr_info & INTR_INFO_DELIEVER_CODE_MASK)
1812 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
1813 if (is_page_fault(intr_info)) {
1814 cr2 = vmcs_readl(EXIT_QUALIFICATION);
1815
Shaohua Li11ec2802007-07-23 14:51:37 +08001816 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001817 r = kvm_mmu_page_fault(vcpu, cr2, error_code);
1818 if (r < 0) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001819 mutex_unlock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001820 return r;
1821 }
1822 if (!r) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001823 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001824 return 1;
1825 }
1826
Laurent Vivier34273182007-09-18 11:27:37 +02001827 er = emulate_instruction(vcpu, kvm_run, cr2, error_code, 0);
Shaohua Li11ec2802007-07-23 14:51:37 +08001828 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001829
1830 switch (er) {
1831 case EMULATE_DONE:
1832 return 1;
1833 case EMULATE_DO_MMIO:
Avi Kivity1165f5f2007-04-19 17:27:43 +03001834 ++vcpu->stat.mmio_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835 return 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001836 case EMULATE_FAIL:
Avi Kivity054b1362007-09-12 13:21:09 +03001837 kvm_report_emulation_failure(vcpu, "pagetable");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001838 break;
1839 default:
1840 BUG();
1841 }
1842 }
1843
1844 if (vcpu->rmode.active &&
1845 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03001846 error_code)) {
1847 if (vcpu->halt_request) {
1848 vcpu->halt_request = 0;
1849 return kvm_emulate_halt(vcpu);
1850 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001851 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03001852 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001853
Mike Dayd77c26f2007-10-08 09:02:08 -04001854 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) ==
1855 (INTR_TYPE_EXCEPTION | 1)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001856 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1857 return 0;
1858 }
1859 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
1860 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
1861 kvm_run->ex.error_code = error_code;
1862 return 0;
1863}
1864
1865static int handle_external_interrupt(struct kvm_vcpu *vcpu,
1866 struct kvm_run *kvm_run)
1867{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001868 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001869 return 1;
1870}
1871
Avi Kivity988ad742007-02-12 00:54:36 -08001872static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1873{
1874 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1875 return 0;
1876}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001877
Avi Kivity6aa8b732006-12-10 02:21:36 -08001878static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1879{
He, Qingbfdaab02007-09-12 14:18:28 +08001880 unsigned long exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02001881 int size, down, in, string, rep;
1882 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001883
Avi Kivity1165f5f2007-04-19 17:27:43 +03001884 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08001885 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02001886 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001887
1888 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02001889 if (emulate_instruction(vcpu,
1890 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03001891 return 0;
1892 return 1;
1893 }
1894
1895 size = (exit_qualification & 7) + 1;
1896 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001897 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001898 rep = (exit_qualification & 32) != 0;
1899 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03001900
Laurent Vivier3090dd72007-08-05 10:43:32 +03001901 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001902}
1903
Ingo Molnar102d8322007-02-19 14:37:47 +02001904static void
1905vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
1906{
1907 /*
1908 * Patch in the VMCALL instruction:
1909 */
1910 hypercall[0] = 0x0f;
1911 hypercall[1] = 0x01;
1912 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02001913}
1914
Avi Kivity6aa8b732006-12-10 02:21:36 -08001915static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1916{
He, Qingbfdaab02007-09-12 14:18:28 +08001917 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001918 int cr;
1919 int reg;
1920
He, Qingbfdaab02007-09-12 14:18:28 +08001921 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001922 cr = exit_qualification & 15;
1923 reg = (exit_qualification >> 8) & 15;
1924 switch ((exit_qualification >> 4) & 3) {
1925 case 0: /* mov to cr */
1926 switch (cr) {
1927 case 0:
1928 vcpu_load_rsp_rip(vcpu);
1929 set_cr0(vcpu, vcpu->regs[reg]);
1930 skip_emulated_instruction(vcpu);
1931 return 1;
1932 case 3:
1933 vcpu_load_rsp_rip(vcpu);
1934 set_cr3(vcpu, vcpu->regs[reg]);
1935 skip_emulated_instruction(vcpu);
1936 return 1;
1937 case 4:
1938 vcpu_load_rsp_rip(vcpu);
1939 set_cr4(vcpu, vcpu->regs[reg]);
1940 skip_emulated_instruction(vcpu);
1941 return 1;
1942 case 8:
1943 vcpu_load_rsp_rip(vcpu);
1944 set_cr8(vcpu, vcpu->regs[reg]);
1945 skip_emulated_instruction(vcpu);
Yang, Sheng253abde2007-08-16 13:01:00 +03001946 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
1947 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948 };
1949 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03001950 case 2: /* clts */
1951 vcpu_load_rsp_rip(vcpu);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001952 vmx_fpu_deactivate(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001953 vcpu->cr0 &= ~X86_CR0_TS;
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001954 vmcs_writel(CR0_READ_SHADOW, vcpu->cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001955 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03001956 skip_emulated_instruction(vcpu);
1957 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001958 case 1: /*mov from cr*/
1959 switch (cr) {
1960 case 3:
1961 vcpu_load_rsp_rip(vcpu);
1962 vcpu->regs[reg] = vcpu->cr3;
1963 vcpu_put_rsp_rip(vcpu);
1964 skip_emulated_instruction(vcpu);
1965 return 1;
1966 case 8:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001967 vcpu_load_rsp_rip(vcpu);
Eddie Dong7017fc32007-07-18 11:34:57 +03001968 vcpu->regs[reg] = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001969 vcpu_put_rsp_rip(vcpu);
1970 skip_emulated_instruction(vcpu);
1971 return 1;
1972 }
1973 break;
1974 case 3: /* lmsw */
1975 lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
1976
1977 skip_emulated_instruction(vcpu);
1978 return 1;
1979 default:
1980 break;
1981 }
1982 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10001983 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08001984 (int)(exit_qualification >> 4) & 3, cr);
1985 return 0;
1986}
1987
1988static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1989{
He, Qingbfdaab02007-09-12 14:18:28 +08001990 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001991 unsigned long val;
1992 int dr, reg;
1993
1994 /*
1995 * FIXME: this code assumes the host is debugging the guest.
1996 * need to deal with guest debugging itself too.
1997 */
He, Qingbfdaab02007-09-12 14:18:28 +08001998 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001999 dr = exit_qualification & 7;
2000 reg = (exit_qualification >> 8) & 15;
2001 vcpu_load_rsp_rip(vcpu);
2002 if (exit_qualification & 16) {
2003 /* mov from dr */
2004 switch (dr) {
2005 case 6:
2006 val = 0xffff0ff0;
2007 break;
2008 case 7:
2009 val = 0x400;
2010 break;
2011 default:
2012 val = 0;
2013 }
2014 vcpu->regs[reg] = val;
2015 } else {
2016 /* mov to dr */
2017 }
2018 vcpu_put_rsp_rip(vcpu);
2019 skip_emulated_instruction(vcpu);
2020 return 1;
2021}
2022
2023static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2024{
Avi Kivity06465c52007-02-28 20:46:53 +02002025 kvm_emulate_cpuid(vcpu);
2026 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002027}
2028
2029static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2030{
2031 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
2032 u64 data;
2033
2034 if (vmx_get_msr(vcpu, ecx, &data)) {
2035 vmx_inject_gp(vcpu, 0);
2036 return 1;
2037 }
2038
2039 /* FIXME: handling of bits 32:63 of rax, rdx */
2040 vcpu->regs[VCPU_REGS_RAX] = data & -1u;
2041 vcpu->regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
2042 skip_emulated_instruction(vcpu);
2043 return 1;
2044}
2045
2046static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2047{
2048 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
2049 u64 data = (vcpu->regs[VCPU_REGS_RAX] & -1u)
2050 | ((u64)(vcpu->regs[VCPU_REGS_RDX] & -1u) << 32);
2051
2052 if (vmx_set_msr(vcpu, ecx, data) != 0) {
2053 vmx_inject_gp(vcpu, 0);
2054 return 1;
2055 }
2056
2057 skip_emulated_instruction(vcpu);
2058 return 1;
2059}
2060
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002061static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2062 struct kvm_run *kvm_run)
2063{
2064 return 1;
2065}
2066
Avi Kivity6aa8b732006-12-10 02:21:36 -08002067static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2068 struct kvm_run *kvm_run)
2069{
Eddie Dong85f455f2007-07-06 12:20:49 +03002070 u32 cpu_based_vm_exec_control;
2071
2072 /* clear pending irq */
2073 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2074 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2075 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Dor Laorc1150d82007-01-05 16:36:24 -08002076 /*
2077 * If the user space waits to inject interrupts, exit as soon as
2078 * possible
2079 */
2080 if (kvm_run->request_interrupt_window &&
Dor Laor022a9302007-01-05 16:37:00 -08002081 !vcpu->irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002082 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002083 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002084 return 0;
2085 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002086 return 1;
2087}
2088
2089static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2090{
2091 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002092 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002093}
2094
Ingo Molnarc21415e2007-02-19 14:37:47 +02002095static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2096{
Dor Laor510043d2007-02-19 18:25:43 +02002097 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002098 kvm_emulate_hypercall(vcpu);
2099 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02002100}
2101
Avi Kivity6aa8b732006-12-10 02:21:36 -08002102/*
2103 * The exit handlers return 1 if the exit was handled fully and guest execution
2104 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2105 * to be done to userspace and return 0.
2106 */
2107static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
2108 struct kvm_run *kvm_run) = {
2109 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
2110 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08002111 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002113 [EXIT_REASON_CR_ACCESS] = handle_cr,
2114 [EXIT_REASON_DR_ACCESS] = handle_dr,
2115 [EXIT_REASON_CPUID] = handle_cpuid,
2116 [EXIT_REASON_MSR_READ] = handle_rdmsr,
2117 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
2118 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
2119 [EXIT_REASON_HLT] = handle_halt,
Ingo Molnarc21415e2007-02-19 14:37:47 +02002120 [EXIT_REASON_VMCALL] = handle_vmcall,
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002121 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122};
2123
2124static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04002125 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002126
2127/*
2128 * The guest has exited. See if we can fix it or if we need userspace
2129 * assistance.
2130 */
2131static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2132{
2133 u32 vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2134 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity29bd8a72007-09-10 17:27:03 +03002135 struct vcpu_vmx *vmx = to_vmx(vcpu);
2136
2137 if (unlikely(vmx->fail)) {
2138 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2139 kvm_run->fail_entry.hardware_entry_failure_reason
2140 = vmcs_read32(VM_INSTRUCTION_ERROR);
2141 return 0;
2142 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143
Mike Dayd77c26f2007-10-08 09:02:08 -04002144 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
2145 exit_reason != EXIT_REASON_EXCEPTION_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146 printk(KERN_WARNING "%s: unexpected, valid vectoring info and "
2147 "exit reason is 0x%x\n", __FUNCTION__, exit_reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148 if (exit_reason < kvm_vmx_max_exit_handlers
2149 && kvm_vmx_exit_handlers[exit_reason])
2150 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
2151 else {
2152 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2153 kvm_run->hw.hardware_exit_reason = exit_reason;
2154 }
2155 return 0;
2156}
2157
Avi Kivityd9e368d2007-06-07 19:18:30 +03002158static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
2159{
Avi Kivityd9e368d2007-06-07 19:18:30 +03002160}
2161
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002162static void update_tpr_threshold(struct kvm_vcpu *vcpu)
2163{
2164 int max_irr, tpr;
2165
2166 if (!vm_need_tpr_shadow(vcpu->kvm))
2167 return;
2168
2169 if (!kvm_lapic_enabled(vcpu) ||
2170 ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
2171 vmcs_write32(TPR_THRESHOLD, 0);
2172 return;
2173 }
2174
2175 tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
2176 vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
2177}
2178
Eddie Dong85f455f2007-07-06 12:20:49 +03002179static void enable_irq_window(struct kvm_vcpu *vcpu)
2180{
2181 u32 cpu_based_vm_exec_control;
2182
2183 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2184 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2185 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2186}
2187
2188static void vmx_intr_assist(struct kvm_vcpu *vcpu)
2189{
2190 u32 idtv_info_field, intr_info_field;
2191 int has_ext_irq, interrupt_window_open;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002192 int vector;
Eddie Dong85f455f2007-07-06 12:20:49 +03002193
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002194 update_tpr_threshold(vcpu);
2195
Eddie Dong85f455f2007-07-06 12:20:49 +03002196 has_ext_irq = kvm_cpu_has_interrupt(vcpu);
2197 intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
2198 idtv_info_field = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2199 if (intr_info_field & INTR_INFO_VALID_MASK) {
2200 if (idtv_info_field & INTR_INFO_VALID_MASK) {
2201 /* TODO: fault when IDT_Vectoring */
2202 printk(KERN_ERR "Fault when IDT_Vectoring\n");
2203 }
2204 if (has_ext_irq)
2205 enable_irq_window(vcpu);
2206 return;
2207 }
2208 if (unlikely(idtv_info_field & INTR_INFO_VALID_MASK)) {
2209 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, idtv_info_field);
2210 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2211 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
2212
2213 if (unlikely(idtv_info_field & INTR_INFO_DELIEVER_CODE_MASK))
2214 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2215 vmcs_read32(IDT_VECTORING_ERROR_CODE));
2216 if (unlikely(has_ext_irq))
2217 enable_irq_window(vcpu);
2218 return;
2219 }
2220 if (!has_ext_irq)
2221 return;
2222 interrupt_window_open =
2223 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2224 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002225 if (interrupt_window_open) {
2226 vector = kvm_cpu_get_interrupt(vcpu);
2227 vmx_inject_irq(vcpu, vector);
2228 kvm_timer_intr_post(vcpu, vector);
2229 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03002230 enable_irq_window(vcpu);
2231}
2232
Avi Kivity04d2cc72007-09-10 18:10:54 +03002233static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002234{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002235 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1b6269d2007-10-09 12:12:19 +02002236 u32 intr_info;
Avi Kivitye6adf282007-04-30 16:07:54 +03002237
2238 /*
2239 * Loading guest fpu may have cleared host cr0.ts
2240 */
2241 vmcs_writel(HOST_CR0, read_cr0());
2242
Mike Dayd77c26f2007-10-08 09:02:08 -04002243 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244 /* Store host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002245#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246 "push %%rax; push %%rbx; push %%rdx;"
2247 "push %%rsi; push %%rdi; push %%rbp;"
2248 "push %%r8; push %%r9; push %%r10; push %%r11;"
2249 "push %%r12; push %%r13; push %%r14; push %%r15;"
2250 "push %%rcx \n\t"
2251 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
2252#else
2253 "pusha; push %%ecx \n\t"
2254 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
2255#endif
2256 /* Check if vmlaunch of vmresume is needed */
2257 "cmp $0, %1 \n\t"
2258 /* Load guest registers. Don't clobber flags. */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002259#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260 "mov %c[cr2](%3), %%rax \n\t"
2261 "mov %%rax, %%cr2 \n\t"
2262 "mov %c[rax](%3), %%rax \n\t"
2263 "mov %c[rbx](%3), %%rbx \n\t"
2264 "mov %c[rdx](%3), %%rdx \n\t"
2265 "mov %c[rsi](%3), %%rsi \n\t"
2266 "mov %c[rdi](%3), %%rdi \n\t"
2267 "mov %c[rbp](%3), %%rbp \n\t"
2268 "mov %c[r8](%3), %%r8 \n\t"
2269 "mov %c[r9](%3), %%r9 \n\t"
2270 "mov %c[r10](%3), %%r10 \n\t"
2271 "mov %c[r11](%3), %%r11 \n\t"
2272 "mov %c[r12](%3), %%r12 \n\t"
2273 "mov %c[r13](%3), %%r13 \n\t"
2274 "mov %c[r14](%3), %%r14 \n\t"
2275 "mov %c[r15](%3), %%r15 \n\t"
2276 "mov %c[rcx](%3), %%rcx \n\t" /* kills %3 (rcx) */
2277#else
2278 "mov %c[cr2](%3), %%eax \n\t"
2279 "mov %%eax, %%cr2 \n\t"
2280 "mov %c[rax](%3), %%eax \n\t"
2281 "mov %c[rbx](%3), %%ebx \n\t"
2282 "mov %c[rdx](%3), %%edx \n\t"
2283 "mov %c[rsi](%3), %%esi \n\t"
2284 "mov %c[rdi](%3), %%edi \n\t"
2285 "mov %c[rbp](%3), %%ebp \n\t"
2286 "mov %c[rcx](%3), %%ecx \n\t" /* kills %3 (ecx) */
2287#endif
2288 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03002289 "jne .Llaunched \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002290 ASM_VMX_VMLAUNCH "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03002291 "jmp .Lkvm_vmx_return \n\t"
2292 ".Llaunched: " ASM_VMX_VMRESUME "\n\t"
2293 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294 /* Save guest registers, load host registers, keep flags */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002295#ifdef CONFIG_X86_64
Ingo Molnar96958232007-02-12 00:54:33 -08002296 "xchg %3, (%%rsp) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002297 "mov %%rax, %c[rax](%3) \n\t"
2298 "mov %%rbx, %c[rbx](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002299 "pushq (%%rsp); popq %c[rcx](%3) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002300 "mov %%rdx, %c[rdx](%3) \n\t"
2301 "mov %%rsi, %c[rsi](%3) \n\t"
2302 "mov %%rdi, %c[rdi](%3) \n\t"
2303 "mov %%rbp, %c[rbp](%3) \n\t"
2304 "mov %%r8, %c[r8](%3) \n\t"
2305 "mov %%r9, %c[r9](%3) \n\t"
2306 "mov %%r10, %c[r10](%3) \n\t"
2307 "mov %%r11, %c[r11](%3) \n\t"
2308 "mov %%r12, %c[r12](%3) \n\t"
2309 "mov %%r13, %c[r13](%3) \n\t"
2310 "mov %%r14, %c[r14](%3) \n\t"
2311 "mov %%r15, %c[r15](%3) \n\t"
2312 "mov %%cr2, %%rax \n\t"
2313 "mov %%rax, %c[cr2](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002314 "mov (%%rsp), %3 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002315
2316 "pop %%rcx; pop %%r15; pop %%r14; pop %%r13; pop %%r12;"
2317 "pop %%r11; pop %%r10; pop %%r9; pop %%r8;"
2318 "pop %%rbp; pop %%rdi; pop %%rsi;"
2319 "pop %%rdx; pop %%rbx; pop %%rax \n\t"
2320#else
Ingo Molnar96958232007-02-12 00:54:33 -08002321 "xchg %3, (%%esp) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322 "mov %%eax, %c[rax](%3) \n\t"
2323 "mov %%ebx, %c[rbx](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002324 "pushl (%%esp); popl %c[rcx](%3) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325 "mov %%edx, %c[rdx](%3) \n\t"
2326 "mov %%esi, %c[rsi](%3) \n\t"
2327 "mov %%edi, %c[rdi](%3) \n\t"
2328 "mov %%ebp, %c[rbp](%3) \n\t"
2329 "mov %%cr2, %%eax \n\t"
2330 "mov %%eax, %c[cr2](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002331 "mov (%%esp), %3 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002332
2333 "pop %%ecx; popa \n\t"
2334#endif
2335 "setbe %0 \n\t"
Avi Kivity29bd8a72007-09-10 17:27:03 +03002336 : "=q" (vmx->fail)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002337 : "r"(vmx->launched), "d"((unsigned long)HOST_RSP),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002338 "c"(vcpu),
2339 [rax]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RAX])),
2340 [rbx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBX])),
2341 [rcx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RCX])),
2342 [rdx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDX])),
2343 [rsi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RSI])),
2344 [rdi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDI])),
2345 [rbp]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002346#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -04002347 [r8]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R8])),
2348 [r9]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R9])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349 [r10]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R10])),
2350 [r11]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R11])),
2351 [r12]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R12])),
2352 [r13]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R13])),
2353 [r14]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R14])),
2354 [r15]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R15])),
2355#endif
2356 [cr2]"i"(offsetof(struct kvm_vcpu, cr2))
Mike Dayd77c26f2007-10-08 09:02:08 -04002357 : "cc", "memory");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002358
Mike Dayd77c26f2007-10-08 09:02:08 -04002359 vcpu->interrupt_window_open =
2360 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002361
Mike Dayd77c26f2007-10-08 09:02:08 -04002362 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03002363 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02002364
2365 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2366
2367 /* We need to handle NMIs before interrupts are enabled */
2368 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
2369 asm("int $2");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370}
2371
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372static void vmx_inject_page_fault(struct kvm_vcpu *vcpu,
2373 unsigned long addr,
2374 u32 err_code)
2375{
2376 u32 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2377
Avi Kivity1165f5f2007-04-19 17:27:43 +03002378 ++vcpu->stat.pf_guest;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379
2380 if (is_page_fault(vect_info)) {
2381 printk(KERN_DEBUG "inject_page_fault: "
2382 "double fault 0x%lx @ 0x%lx\n",
2383 addr, vmcs_readl(GUEST_RIP));
2384 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
2385 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2386 DF_VECTOR |
2387 INTR_TYPE_EXCEPTION |
2388 INTR_INFO_DELIEVER_CODE_MASK |
2389 INTR_INFO_VALID_MASK);
2390 return;
2391 }
2392 vcpu->cr2 = addr;
2393 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, err_code);
2394 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2395 PF_VECTOR |
2396 INTR_TYPE_EXCEPTION |
2397 INTR_INFO_DELIEVER_CODE_MASK |
2398 INTR_INFO_VALID_MASK);
2399
2400}
2401
2402static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
2403{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002404 struct vcpu_vmx *vmx = to_vmx(vcpu);
2405
2406 if (vmx->vmcs) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002407 on_each_cpu(__vcpu_clear, vmx, 0, 1);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002408 free_vmcs(vmx->vmcs);
2409 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002410 }
2411}
2412
2413static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
2414{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002415 struct vcpu_vmx *vmx = to_vmx(vcpu);
2416
Avi Kivity6aa8b732006-12-10 02:21:36 -08002417 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002418 kfree(vmx->host_msrs);
2419 kfree(vmx->guest_msrs);
2420 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10002421 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002422}
2423
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002424static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002426 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10002427 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03002428 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002430 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002431 return ERR_PTR(-ENOMEM);
2432
2433 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
2434 if (err)
2435 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002436
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002437 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002438 if (!vmx->guest_msrs) {
2439 err = -ENOMEM;
2440 goto uninit_vcpu;
2441 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08002442
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002443 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
2444 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002445 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002446
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002447 vmx->vmcs = alloc_vmcs();
2448 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002449 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002450
2451 vmcs_clear(vmx->vmcs);
2452
Avi Kivity15ad7142007-07-11 18:17:21 +03002453 cpu = get_cpu();
2454 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002455 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002456 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002457 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002458 if (err)
2459 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002460
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002461 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002462
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002463free_vmcs:
2464 free_vmcs(vmx->vmcs);
2465free_msrs:
2466 kfree(vmx->host_msrs);
2467free_guest_msrs:
2468 kfree(vmx->guest_msrs);
2469uninit_vcpu:
2470 kvm_vcpu_uninit(&vmx->vcpu);
2471free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10002472 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002473 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002474}
2475
Yang, Sheng002c7f72007-07-31 14:23:01 +03002476static void __init vmx_check_processor_compat(void *rtn)
2477{
2478 struct vmcs_config vmcs_conf;
2479
2480 *(int *)rtn = 0;
2481 if (setup_vmcs_config(&vmcs_conf) < 0)
2482 *(int *)rtn = -EIO;
2483 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
2484 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
2485 smp_processor_id());
2486 *(int *)rtn = -EIO;
2487 }
2488}
2489
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002490static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002491 .cpu_has_kvm_support = cpu_has_kvm_support,
2492 .disabled_by_bios = vmx_disabled_by_bios,
2493 .hardware_setup = hardware_setup,
2494 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03002495 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496 .hardware_enable = hardware_enable,
2497 .hardware_disable = hardware_disable,
2498
2499 .vcpu_create = vmx_create_vcpu,
2500 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002501 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002502
Avi Kivity04d2cc72007-09-10 18:10:54 +03002503 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002504 .vcpu_load = vmx_vcpu_load,
2505 .vcpu_put = vmx_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08002506 .vcpu_decache = vmx_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002507
2508 .set_guest_debug = set_guest_debug,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002509 .guest_debug_pre = kvm_guest_debug_pre,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002510 .get_msr = vmx_get_msr,
2511 .set_msr = vmx_set_msr,
2512 .get_segment_base = vmx_get_segment_base,
2513 .get_segment = vmx_get_segment,
2514 .set_segment = vmx_set_segment,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03002516 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002518 .set_cr3 = vmx_set_cr3,
2519 .set_cr4 = vmx_set_cr4,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002520#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521 .set_efer = vmx_set_efer,
2522#endif
2523 .get_idt = vmx_get_idt,
2524 .set_idt = vmx_set_idt,
2525 .get_gdt = vmx_get_gdt,
2526 .set_gdt = vmx_set_gdt,
2527 .cache_regs = vcpu_load_rsp_rip,
2528 .decache_regs = vcpu_put_rsp_rip,
2529 .get_rflags = vmx_get_rflags,
2530 .set_rflags = vmx_set_rflags,
2531
2532 .tlb_flush = vmx_flush_tlb,
2533 .inject_page_fault = vmx_inject_page_fault,
2534
2535 .inject_gp = vmx_inject_gp,
2536
2537 .run = vmx_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002538 .handle_exit = kvm_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002539 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02002540 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03002541 .get_irq = vmx_get_irq,
2542 .set_irq = vmx_inject_irq,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002543 .inject_pending_irq = vmx_intr_assist,
2544 .inject_pending_vectors = do_interrupt_requests,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002545};
2546
2547static int __init vmx_init(void)
2548{
He, Qingfdef3ad2007-04-30 09:45:24 +03002549 void *iova;
2550 int r;
2551
2552 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2553 if (!vmx_io_bitmap_a)
2554 return -ENOMEM;
2555
2556 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2557 if (!vmx_io_bitmap_b) {
2558 r = -ENOMEM;
2559 goto out;
2560 }
2561
2562 /*
2563 * Allow direct access to the PC debug port (it is often used for I/O
2564 * delays, but the vmexits simply slow things down).
2565 */
2566 iova = kmap(vmx_io_bitmap_a);
2567 memset(iova, 0xff, PAGE_SIZE);
2568 clear_bit(0x80, iova);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002569 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03002570
2571 iova = kmap(vmx_io_bitmap_b);
2572 memset(iova, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002573 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03002574
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002575 r = kvm_init_x86(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03002576 if (r)
2577 goto out1;
2578
Avi Kivityc7addb92007-09-16 18:58:32 +02002579 if (bypass_guest_pf)
2580 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
2581
He, Qingfdef3ad2007-04-30 09:45:24 +03002582 return 0;
2583
2584out1:
2585 __free_page(vmx_io_bitmap_b);
2586out:
2587 __free_page(vmx_io_bitmap_a);
2588 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002589}
2590
2591static void __exit vmx_exit(void)
2592{
He, Qingfdef3ad2007-04-30 09:45:24 +03002593 __free_page(vmx_io_bitmap_b);
2594 __free_page(vmx_io_bitmap_a);
2595
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002596 kvm_exit_x86();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002597}
2598
2599module_init(vmx_init)
2600module_exit(vmx_exit)