blob: f130c01422cf746cad8d2b372f6221775eef58b9 [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 Kivitye4956062007-06-28 14:15:57 -040029
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080031#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032
Avi Kivity6aa8b732006-12-10 02:21:36 -080033MODULE_AUTHOR("Qumranet");
34MODULE_LICENSE("GPL");
35
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040036struct vmcs {
37 u32 revision_id;
38 u32 abort;
39 char data[0];
40};
41
42struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100043 struct kvm_vcpu vcpu;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040044 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030045 u8 fail;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040046 struct kvm_msr_entry *guest_msrs;
47 struct kvm_msr_entry *host_msrs;
48 int nmsrs;
49 int save_nmsrs;
50 int msr_offset_efer;
51#ifdef CONFIG_X86_64
52 int msr_offset_kernel_gs_base;
53#endif
54 struct vmcs *vmcs;
55 struct {
56 int loaded;
57 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020058 int gs_ldt_reload_needed;
59 int fs_reload_needed;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040060 }host_state;
61
62};
63
64static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
65{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100066 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040067}
68
Avi Kivity75880a02007-06-20 11:20:04 +030069static int init_rmode_tss(struct kvm *kvm);
70
Avi Kivity6aa8b732006-12-10 02:21:36 -080071static DEFINE_PER_CPU(struct vmcs *, vmxarea);
72static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
73
He, Qingfdef3ad2007-04-30 09:45:24 +030074static struct page *vmx_io_bitmap_a;
75static struct page *vmx_io_bitmap_b;
76
Eddie Dong2cc51562007-05-21 07:28:09 +030077#define EFER_SAVE_RESTORE_BITS ((u64)EFER_SCE)
Avi Kivity6aa8b732006-12-10 02:21:36 -080078
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +030079static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -080080 int size;
81 int order;
82 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +030083 u32 pin_based_exec_ctrl;
84 u32 cpu_based_exec_ctrl;
85 u32 vmexit_ctrl;
86 u32 vmentry_ctrl;
87} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -080088
89#define VMX_SEGMENT_FIELD(seg) \
90 [VCPU_SREG_##seg] = { \
91 .selector = GUEST_##seg##_SELECTOR, \
92 .base = GUEST_##seg##_BASE, \
93 .limit = GUEST_##seg##_LIMIT, \
94 .ar_bytes = GUEST_##seg##_AR_BYTES, \
95 }
96
97static struct kvm_vmx_segment_field {
98 unsigned selector;
99 unsigned base;
100 unsigned limit;
101 unsigned ar_bytes;
102} kvm_vmx_segment_fields[] = {
103 VMX_SEGMENT_FIELD(CS),
104 VMX_SEGMENT_FIELD(DS),
105 VMX_SEGMENT_FIELD(ES),
106 VMX_SEGMENT_FIELD(FS),
107 VMX_SEGMENT_FIELD(GS),
108 VMX_SEGMENT_FIELD(SS),
109 VMX_SEGMENT_FIELD(TR),
110 VMX_SEGMENT_FIELD(LDTR),
111};
112
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300113/*
114 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
115 * away by decrementing the array size.
116 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800117static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800118#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
120#endif
121 MSR_EFER, MSR_K6_STAR,
122};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200123#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400125static void load_msrs(struct kvm_msr_entry *e, int n)
126{
127 int i;
128
129 for (i = 0; i < n; ++i)
130 wrmsrl(e[i].index, e[i].data);
131}
132
133static void save_msrs(struct kvm_msr_entry *e, int n)
134{
135 int i;
136
137 for (i = 0; i < n; ++i)
138 rdmsrl(e[i].index, e[i].data);
139}
140
141static inline u64 msr_efer_save_restore_bits(struct kvm_msr_entry msr)
Eddie Dong2cc51562007-05-21 07:28:09 +0300142{
143 return (u64)msr.data & EFER_SAVE_RESTORE_BITS;
144}
145
Rusty Russell8b9cf982007-07-30 16:31:43 +1000146static inline int msr_efer_need_save_restore(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300147{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400148 int efer_offset = vmx->msr_offset_efer;
149 return msr_efer_save_restore_bits(vmx->host_msrs[efer_offset]) !=
150 msr_efer_save_restore_bits(vmx->guest_msrs[efer_offset]);
Eddie Dong2cc51562007-05-21 07:28:09 +0300151}
152
Avi Kivity6aa8b732006-12-10 02:21:36 -0800153static inline int is_page_fault(u32 intr_info)
154{
155 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
156 INTR_INFO_VALID_MASK)) ==
157 (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
158}
159
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300160static inline int is_no_device(u32 intr_info)
161{
162 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
163 INTR_INFO_VALID_MASK)) ==
164 (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
165}
166
Avi Kivity6aa8b732006-12-10 02:21:36 -0800167static inline int is_external_interrupt(u32 intr_info)
168{
169 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
170 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
171}
172
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800173static inline int cpu_has_vmx_tpr_shadow(void)
174{
175 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW);
176}
177
178static inline int vm_need_tpr_shadow(struct kvm *kvm)
179{
180 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)));
181}
182
Rusty Russell8b9cf982007-07-30 16:31:43 +1000183static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800184{
185 int i;
186
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400187 for (i = 0; i < vmx->nmsrs; ++i)
188 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300189 return i;
190 return -1;
191}
192
Rusty Russell8b9cf982007-07-30 16:31:43 +1000193static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300194{
195 int i;
196
Rusty Russell8b9cf982007-07-30 16:31:43 +1000197 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300198 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400199 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000200 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800201}
202
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203static void vmcs_clear(struct vmcs *vmcs)
204{
205 u64 phys_addr = __pa(vmcs);
206 u8 error;
207
208 asm volatile (ASM_VMX_VMCLEAR_RAX "; setna %0"
209 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
210 : "cc", "memory");
211 if (error)
212 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
213 vmcs, phys_addr);
214}
215
216static void __vcpu_clear(void *arg)
217{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000218 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800219 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220
Rusty Russell8b9cf982007-07-30 16:31:43 +1000221 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400222 vmcs_clear(vmx->vmcs);
223 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224 per_cpu(current_vmcs, cpu) = NULL;
Rusty Russell8b9cf982007-07-30 16:31:43 +1000225 rdtscll(vmx->vcpu.host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226}
227
Rusty Russell8b9cf982007-07-30 16:31:43 +1000228static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800229{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000230 if (vmx->vcpu.cpu != raw_smp_processor_id() && vmx->vcpu.cpu != -1)
231 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear,
232 vmx, 0, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800233 else
Rusty Russell8b9cf982007-07-30 16:31:43 +1000234 __vcpu_clear(vmx);
235 vmx->launched = 0;
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800236}
237
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238static unsigned long vmcs_readl(unsigned long field)
239{
240 unsigned long value;
241
242 asm volatile (ASM_VMX_VMREAD_RDX_RAX
243 : "=a"(value) : "d"(field) : "cc");
244 return value;
245}
246
247static u16 vmcs_read16(unsigned long field)
248{
249 return vmcs_readl(field);
250}
251
252static u32 vmcs_read32(unsigned long field)
253{
254 return vmcs_readl(field);
255}
256
257static u64 vmcs_read64(unsigned long field)
258{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800259#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800260 return vmcs_readl(field);
261#else
262 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
263#endif
264}
265
Avi Kivitye52de1b2007-01-05 16:36:56 -0800266static noinline void vmwrite_error(unsigned long field, unsigned long value)
267{
268 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
269 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
270 dump_stack();
271}
272
Avi Kivity6aa8b732006-12-10 02:21:36 -0800273static void vmcs_writel(unsigned long field, unsigned long value)
274{
275 u8 error;
276
277 asm volatile (ASM_VMX_VMWRITE_RAX_RDX "; setna %0"
278 : "=q"(error) : "a"(value), "d"(field) : "cc" );
Avi Kivitye52de1b2007-01-05 16:36:56 -0800279 if (unlikely(error))
280 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800281}
282
283static void vmcs_write16(unsigned long field, u16 value)
284{
285 vmcs_writel(field, value);
286}
287
288static void vmcs_write32(unsigned long field, u32 value)
289{
290 vmcs_writel(field, value);
291}
292
293static void vmcs_write64(unsigned long field, u64 value)
294{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800295#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296 vmcs_writel(field, value);
297#else
298 vmcs_writel(field, value);
299 asm volatile ("");
300 vmcs_writel(field+1, value >> 32);
301#endif
302}
303
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300304static void vmcs_clear_bits(unsigned long field, u32 mask)
305{
306 vmcs_writel(field, vmcs_readl(field) & ~mask);
307}
308
309static void vmcs_set_bits(unsigned long field, u32 mask)
310{
311 vmcs_writel(field, vmcs_readl(field) | mask);
312}
313
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300314static void update_exception_bitmap(struct kvm_vcpu *vcpu)
315{
316 u32 eb;
317
318 eb = 1u << PF_VECTOR;
319 if (!vcpu->fpu_active)
320 eb |= 1u << NM_VECTOR;
321 if (vcpu->guest_debug.enabled)
322 eb |= 1u << 1;
323 if (vcpu->rmode.active)
324 eb = ~0;
325 vmcs_write32(EXCEPTION_BITMAP, eb);
326}
327
Avi Kivity33ed6322007-05-02 16:54:03 +0300328static void reload_tss(void)
329{
330#ifndef CONFIG_X86_64
331
332 /*
333 * VT restores TR but not its size. Useless.
334 */
335 struct descriptor_table gdt;
336 struct segment_descriptor *descs;
337
338 get_gdt(&gdt);
339 descs = (void *)gdt.base;
340 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
341 load_TR_desc();
342#endif
343}
344
Rusty Russell8b9cf982007-07-30 16:31:43 +1000345static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300346{
347 u64 trans_efer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400348 int efer_offset = vmx->msr_offset_efer;
Eddie Dong2cc51562007-05-21 07:28:09 +0300349
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400350 trans_efer = vmx->host_msrs[efer_offset].data;
Eddie Dong2cc51562007-05-21 07:28:09 +0300351 trans_efer &= ~EFER_SAVE_RESTORE_BITS;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400352 trans_efer |= msr_efer_save_restore_bits(vmx->guest_msrs[efer_offset]);
Eddie Dong2cc51562007-05-21 07:28:09 +0300353 wrmsrl(MSR_EFER, trans_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000354 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300355}
356
Avi Kivity04d2cc72007-09-10 18:10:54 +0300357static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300358{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300359 struct vcpu_vmx *vmx = to_vmx(vcpu);
360
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400361 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300362 return;
363
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400364 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300365 /*
366 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
367 * allow segment selectors with cpl > 0 or ti == 1.
368 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400369 vmx->host_state.ldt_sel = read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200370 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400371 vmx->host_state.fs_sel = read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200372 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400373 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200374 vmx->host_state.fs_reload_needed = 0;
375 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300376 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200377 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300378 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400379 vmx->host_state.gs_sel = read_gs();
380 if (!(vmx->host_state.gs_sel & 7))
381 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300382 else {
383 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200384 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300385 }
386
387#ifdef CONFIG_X86_64
388 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
389 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
390#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400391 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
392 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300393#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300394
395#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000396 if (is_long_mode(&vmx->vcpu)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400397 save_msrs(vmx->host_msrs +
398 vmx->msr_offset_kernel_gs_base, 1);
Avi Kivity707c0872007-05-02 17:33:43 +0300399 }
400#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400401 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000402 if (msr_efer_need_save_restore(vmx))
403 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300404}
405
Rusty Russell8b9cf982007-07-30 16:31:43 +1000406static void vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300407{
Avi Kivity15ad7142007-07-11 18:17:21 +0300408 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300409
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400410 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300411 return;
412
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400413 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200414 if (vmx->host_state.fs_reload_needed)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400415 load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200416 if (vmx->host_state.gs_ldt_reload_needed) {
417 load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300418 /*
419 * If we have to reload gs, we must take care to
420 * preserve our gs base.
421 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300422 local_irq_save(flags);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400423 load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300424#ifdef CONFIG_X86_64
425 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
426#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300427 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300428 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200429 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400430 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
431 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000432 if (msr_efer_need_save_restore(vmx))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400433 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
Avi Kivity33ed6322007-05-02 16:54:03 +0300434}
435
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436/*
437 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
438 * vcpu mutex is already taken.
439 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300440static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400442 struct vcpu_vmx *vmx = to_vmx(vcpu);
443 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity77002702007-06-13 19:55:28 +0300444 u64 tsc_this, delta;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445
Eddie Donga3d7f852007-09-03 16:15:12 +0300446 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000447 vcpu_clear(vmx);
Eddie Donga3d7f852007-09-03 16:15:12 +0300448 kvm_migrate_apic_timer(vcpu);
449 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400451 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452 u8 error;
453
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400454 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455 asm volatile (ASM_VMX_VMPTRLD_RAX "; setna %0"
456 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
457 : "cc");
458 if (error)
459 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400460 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461 }
462
463 if (vcpu->cpu != cpu) {
464 struct descriptor_table dt;
465 unsigned long sysenter_esp;
466
467 vcpu->cpu = cpu;
468 /*
469 * Linux uses per-cpu TSS and GDT, so set these when switching
470 * processors.
471 */
472 vmcs_writel(HOST_TR_BASE, read_tr_base()); /* 22.2.4 */
473 get_gdt(&dt);
474 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
475
476 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
477 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300478
479 /*
480 * Make sure the time stamp counter is monotonous.
481 */
482 rdtscll(tsc_this);
483 delta = vcpu->host_tsc - tsc_this;
484 vmcs_write64(TSC_OFFSET, vmcs_read64(TSC_OFFSET) + delta);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800485 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800486}
487
488static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
489{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000490 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity7702fd12007-06-14 16:27:40 +0300491 kvm_put_guest_fpu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800492}
493
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300494static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
495{
496 if (vcpu->fpu_active)
497 return;
498 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000499 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
500 if (vcpu->cr0 & X86_CR0_TS)
501 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300502 update_exception_bitmap(vcpu);
503}
504
505static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
506{
507 if (!vcpu->fpu_active)
508 return;
509 vcpu->fpu_active = 0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000510 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300511 update_exception_bitmap(vcpu);
512}
513
Avi Kivity774c47f2007-02-12 00:54:47 -0800514static void vmx_vcpu_decache(struct kvm_vcpu *vcpu)
515{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000516 vcpu_clear(to_vmx(vcpu));
Avi Kivity774c47f2007-02-12 00:54:47 -0800517}
518
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
520{
521 return vmcs_readl(GUEST_RFLAGS);
522}
523
524static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
525{
526 vmcs_writel(GUEST_RFLAGS, rflags);
527}
528
529static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
530{
531 unsigned long rip;
532 u32 interruptibility;
533
534 rip = vmcs_readl(GUEST_RIP);
535 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
536 vmcs_writel(GUEST_RIP, rip);
537
538 /*
539 * We emulated an instruction, so temporary interrupt blocking
540 * should be removed, if set.
541 */
542 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
543 if (interruptibility & 3)
544 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
545 interruptibility & ~3);
Dor Laorc1150d82007-01-05 16:36:24 -0800546 vcpu->interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800547}
548
549static void vmx_inject_gp(struct kvm_vcpu *vcpu, unsigned error_code)
550{
551 printk(KERN_DEBUG "inject_general_protection: rip 0x%lx\n",
552 vmcs_readl(GUEST_RIP));
553 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
554 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
555 GP_VECTOR |
556 INTR_TYPE_EXCEPTION |
557 INTR_INFO_DELIEVER_CODE_MASK |
558 INTR_INFO_VALID_MASK);
559}
560
561/*
Eddie Donga75beee2007-05-17 18:55:15 +0300562 * Swap MSR entry in host/guest MSR entry array.
563 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200564#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000565static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300566{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400567 struct kvm_msr_entry tmp;
568
569 tmp = vmx->guest_msrs[to];
570 vmx->guest_msrs[to] = vmx->guest_msrs[from];
571 vmx->guest_msrs[from] = tmp;
572 tmp = vmx->host_msrs[to];
573 vmx->host_msrs[to] = vmx->host_msrs[from];
574 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300575}
Gabriel C54e11fa2007-08-01 16:23:10 +0200576#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300577
578/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300579 * Set up the vmcs to automatically save and restore system
580 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
581 * mode, as fiddling with msrs is very expensive.
582 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000583static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300584{
Eddie Dong2cc51562007-05-21 07:28:09 +0300585 int save_nmsrs;
Avi Kivitye38aea32007-04-19 13:22:48 +0300586
Eddie Donga75beee2007-05-17 18:55:15 +0300587 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300588#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000589 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300590 int index;
591
Rusty Russell8b9cf982007-07-30 16:31:43 +1000592 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300593 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000594 move_msr_up(vmx, index, save_nmsrs++);
595 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300596 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000597 move_msr_up(vmx, index, save_nmsrs++);
598 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300599 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000600 move_msr_up(vmx, index, save_nmsrs++);
601 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300602 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000603 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300604 /*
605 * MSR_K6_STAR is only needed on long mode guests, and only
606 * if efer.sce is enabled.
607 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000608 index = __find_msr_index(vmx, MSR_K6_STAR);
609 if ((index >= 0) && (vmx->vcpu.shadow_efer & EFER_SCE))
610 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300611 }
Eddie Donga75beee2007-05-17 18:55:15 +0300612#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400613 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300614
Eddie Donga75beee2007-05-17 18:55:15 +0300615#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400616 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000617 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300618#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000619 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivitye38aea32007-04-19 13:22:48 +0300620}
621
622/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623 * reads and returns guest's timestamp counter "register"
624 * guest_tsc = host_tsc + tsc_offset -- 21.3
625 */
626static u64 guest_read_tsc(void)
627{
628 u64 host_tsc, tsc_offset;
629
630 rdtscll(host_tsc);
631 tsc_offset = vmcs_read64(TSC_OFFSET);
632 return host_tsc + tsc_offset;
633}
634
635/*
636 * writes 'guest_tsc' into guest's timestamp counter "register"
637 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
638 */
639static void guest_write_tsc(u64 guest_tsc)
640{
641 u64 host_tsc;
642
643 rdtscll(host_tsc);
644 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
645}
646
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647/*
648 * Reads an msr value (of 'msr_index') into 'pdata'.
649 * Returns 0 on success, non-0 otherwise.
650 * Assumes vcpu_load() was already called.
651 */
652static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
653{
654 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400655 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656
657 if (!pdata) {
658 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
659 return -EINVAL;
660 }
661
662 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800663#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664 case MSR_FS_BASE:
665 data = vmcs_readl(GUEST_FS_BASE);
666 break;
667 case MSR_GS_BASE:
668 data = vmcs_readl(GUEST_GS_BASE);
669 break;
670 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800671 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672#endif
673 case MSR_IA32_TIME_STAMP_COUNTER:
674 data = guest_read_tsc();
675 break;
676 case MSR_IA32_SYSENTER_CS:
677 data = vmcs_read32(GUEST_SYSENTER_CS);
678 break;
679 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200680 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800681 break;
682 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200683 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000686 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800687 if (msr) {
688 data = msr->data;
689 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800690 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800691 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800692 }
693
694 *pdata = data;
695 return 0;
696}
697
698/*
699 * Writes msr value into into the appropriate "register".
700 * Returns 0 on success, non-0 otherwise.
701 * Assumes vcpu_load() was already called.
702 */
703static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
704{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400705 struct vcpu_vmx *vmx = to_vmx(vcpu);
706 struct kvm_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +0300707 int ret = 0;
708
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800710#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800711 case MSR_EFER:
Eddie Dong2cc51562007-05-21 07:28:09 +0300712 ret = kvm_set_msr_common(vcpu, msr_index, data);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400713 if (vmx->host_state.loaded)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000714 load_transition_efer(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +0300715 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716 case MSR_FS_BASE:
717 vmcs_writel(GUEST_FS_BASE, data);
718 break;
719 case MSR_GS_BASE:
720 vmcs_writel(GUEST_GS_BASE, data);
721 break;
722#endif
723 case MSR_IA32_SYSENTER_CS:
724 vmcs_write32(GUEST_SYSENTER_CS, data);
725 break;
726 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200727 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728 break;
729 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200730 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800731 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200732 case MSR_IA32_TIME_STAMP_COUNTER:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733 guest_write_tsc(data);
734 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000736 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800737 if (msr) {
738 msr->data = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400739 if (vmx->host_state.loaded)
740 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800741 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800742 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300743 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744 }
745
Eddie Dong2cc51562007-05-21 07:28:09 +0300746 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747}
748
749/*
750 * Sync the rsp and rip registers into the vcpu structure. This allows
751 * registers to be accessed by indexing vcpu->regs.
752 */
753static void vcpu_load_rsp_rip(struct kvm_vcpu *vcpu)
754{
755 vcpu->regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
756 vcpu->rip = vmcs_readl(GUEST_RIP);
757}
758
759/*
760 * Syncs rsp and rip back into the vmcs. Should be called after possible
761 * modification.
762 */
763static void vcpu_put_rsp_rip(struct kvm_vcpu *vcpu)
764{
765 vmcs_writel(GUEST_RSP, vcpu->regs[VCPU_REGS_RSP]);
766 vmcs_writel(GUEST_RIP, vcpu->rip);
767}
768
769static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
770{
771 unsigned long dr7 = 0x400;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772 int old_singlestep;
773
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774 old_singlestep = vcpu->guest_debug.singlestep;
775
776 vcpu->guest_debug.enabled = dbg->enabled;
777 if (vcpu->guest_debug.enabled) {
778 int i;
779
780 dr7 |= 0x200; /* exact */
781 for (i = 0; i < 4; ++i) {
782 if (!dbg->breakpoints[i].enabled)
783 continue;
784 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
785 dr7 |= 2 << (i*2); /* global enable */
786 dr7 |= 0 << (i*4+16); /* execution breakpoint */
787 }
788
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789 vcpu->guest_debug.singlestep = dbg->singlestep;
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300790 } else
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791 vcpu->guest_debug.singlestep = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792
793 if (old_singlestep && !vcpu->guest_debug.singlestep) {
794 unsigned long flags;
795
796 flags = vmcs_readl(GUEST_RFLAGS);
797 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
798 vmcs_writel(GUEST_RFLAGS, flags);
799 }
800
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300801 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802 vmcs_writel(GUEST_DR7, dr7);
803
804 return 0;
805}
806
Eddie Dong2a8067f2007-08-06 16:29:07 +0300807static int vmx_get_irq(struct kvm_vcpu *vcpu)
808{
809 u32 idtv_info_field;
810
811 idtv_info_field = vmcs_read32(IDT_VECTORING_INFO_FIELD);
812 if (idtv_info_field & INTR_INFO_VALID_MASK) {
813 if (is_external_interrupt(idtv_info_field))
814 return idtv_info_field & VECTORING_INFO_VECTOR_MASK;
815 else
816 printk("pending exception: not handled yet\n");
817 }
818 return -1;
819}
820
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821static __init int cpu_has_kvm_support(void)
822{
823 unsigned long ecx = cpuid_ecx(1);
824 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
825}
826
827static __init int vmx_disabled_by_bios(void)
828{
829 u64 msr;
830
831 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300832 return (msr & (MSR_IA32_FEATURE_CONTROL_LOCKED |
833 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
834 == MSR_IA32_FEATURE_CONTROL_LOCKED;
835 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836}
837
Avi Kivity774c47f2007-02-12 00:54:47 -0800838static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839{
840 int cpu = raw_smp_processor_id();
841 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
842 u64 old;
843
844 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300845 if ((old & (MSR_IA32_FEATURE_CONTROL_LOCKED |
846 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
847 != (MSR_IA32_FEATURE_CONTROL_LOCKED |
848 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300850 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
851 MSR_IA32_FEATURE_CONTROL_LOCKED |
852 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +1000853 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800854 asm volatile (ASM_VMX_VMXON_RAX : : "a"(&phys_addr), "m"(phys_addr)
855 : "memory", "cc");
856}
857
858static void hardware_disable(void *garbage)
859{
860 asm volatile (ASM_VMX_VMXOFF : : : "cc");
861}
862
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300863static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
864 u32 msr, u32* result)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865{
866 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300867 u32 ctl = ctl_min | ctl_opt;
868
869 rdmsr(msr, vmx_msr_low, vmx_msr_high);
870
871 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
872 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
873
874 /* Ensure minimum (required) set of control bits are supported. */
875 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300876 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300877
878 *result = ctl;
879 return 0;
880}
881
Yang, Sheng002c7f72007-07-31 14:23:01 +0300882static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300883{
884 u32 vmx_msr_low, vmx_msr_high;
885 u32 min, opt;
886 u32 _pin_based_exec_control = 0;
887 u32 _cpu_based_exec_control = 0;
888 u32 _vmexit_control = 0;
889 u32 _vmentry_control = 0;
890
891 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
892 opt = 0;
893 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
894 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300895 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300896
897 min = CPU_BASED_HLT_EXITING |
898#ifdef CONFIG_X86_64
899 CPU_BASED_CR8_LOAD_EXITING |
900 CPU_BASED_CR8_STORE_EXITING |
901#endif
902 CPU_BASED_USE_IO_BITMAPS |
903 CPU_BASED_MOV_DR_EXITING |
904 CPU_BASED_USE_TSC_OFFSETING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800905#ifdef CONFIG_X86_64
906 opt = CPU_BASED_TPR_SHADOW;
907#else
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300908 opt = 0;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800909#endif
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300910 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
911 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300912 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800913#ifdef CONFIG_X86_64
914 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
915 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
916 ~CPU_BASED_CR8_STORE_EXITING;
917#endif
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300918
919 min = 0;
920#ifdef CONFIG_X86_64
921 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
922#endif
923 opt = 0;
924 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
925 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300926 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300927
928 min = opt = 0;
929 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
930 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300931 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -0800933 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300934
935 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
936 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300937 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300938
939#ifdef CONFIG_X86_64
940 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
941 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +0300942 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300943#endif
944
945 /* Require Write-Back (WB) memory type for VMCS accesses. */
946 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300947 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300948
Yang, Sheng002c7f72007-07-31 14:23:01 +0300949 vmcs_conf->size = vmx_msr_high & 0x1fff;
950 vmcs_conf->order = get_order(vmcs_config.size);
951 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300952
Yang, Sheng002c7f72007-07-31 14:23:01 +0300953 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
954 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
955 vmcs_conf->vmexit_ctrl = _vmexit_control;
956 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300957
958 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -0800959}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960
961static struct vmcs *alloc_vmcs_cpu(int cpu)
962{
963 int node = cpu_to_node(cpu);
964 struct page *pages;
965 struct vmcs *vmcs;
966
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300967 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 if (!pages)
969 return NULL;
970 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300971 memset(vmcs, 0, vmcs_config.size);
972 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973 return vmcs;
974}
975
976static struct vmcs *alloc_vmcs(void)
977{
Ingo Molnard3b2c332007-01-05 16:36:23 -0800978 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979}
980
981static void free_vmcs(struct vmcs *vmcs)
982{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300983 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984}
985
Sam Ravnborg39959582007-06-01 00:47:13 -0700986static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987{
988 int cpu;
989
990 for_each_online_cpu(cpu)
991 free_vmcs(per_cpu(vmxarea, cpu));
992}
993
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994static __init int alloc_kvm_area(void)
995{
996 int cpu;
997
998 for_each_online_cpu(cpu) {
999 struct vmcs *vmcs;
1000
1001 vmcs = alloc_vmcs_cpu(cpu);
1002 if (!vmcs) {
1003 free_kvm_area();
1004 return -ENOMEM;
1005 }
1006
1007 per_cpu(vmxarea, cpu) = vmcs;
1008 }
1009 return 0;
1010}
1011
1012static __init int hardware_setup(void)
1013{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001014 if (setup_vmcs_config(&vmcs_config) < 0)
1015 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016 return alloc_kvm_area();
1017}
1018
1019static __exit void hardware_unsetup(void)
1020{
1021 free_kvm_area();
1022}
1023
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1025{
1026 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1027
Avi Kivity6af11b92007-03-19 13:18:10 +02001028 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029 vmcs_write16(sf->selector, save->selector);
1030 vmcs_writel(sf->base, save->base);
1031 vmcs_write32(sf->limit, save->limit);
1032 vmcs_write32(sf->ar_bytes, save->ar);
1033 } else {
1034 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1035 << AR_DPL_SHIFT;
1036 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1037 }
1038}
1039
1040static void enter_pmode(struct kvm_vcpu *vcpu)
1041{
1042 unsigned long flags;
1043
1044 vcpu->rmode.active = 0;
1045
1046 vmcs_writel(GUEST_TR_BASE, vcpu->rmode.tr.base);
1047 vmcs_write32(GUEST_TR_LIMIT, vcpu->rmode.tr.limit);
1048 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->rmode.tr.ar);
1049
1050 flags = vmcs_readl(GUEST_RFLAGS);
1051 flags &= ~(IOPL_MASK | X86_EFLAGS_VM);
1052 flags |= (vcpu->rmode.save_iopl << IOPL_SHIFT);
1053 vmcs_writel(GUEST_RFLAGS, flags);
1054
Rusty Russell66aee912007-07-17 23:34:16 +10001055 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1056 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057
1058 update_exception_bitmap(vcpu);
1059
1060 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->rmode.es);
1061 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->rmode.ds);
1062 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->rmode.gs);
1063 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->rmode.fs);
1064
1065 vmcs_write16(GUEST_SS_SELECTOR, 0);
1066 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1067
1068 vmcs_write16(GUEST_CS_SELECTOR,
1069 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1070 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1071}
1072
Izik Eidus33f5fa12007-08-19 22:24:58 +03001073static gva_t rmode_tss_base(struct kvm* kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074{
1075 gfn_t base_gfn = kvm->memslots[0].base_gfn + kvm->memslots[0].npages - 3;
1076 return base_gfn << PAGE_SHIFT;
1077}
1078
1079static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1080{
1081 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1082
1083 save->selector = vmcs_read16(sf->selector);
1084 save->base = vmcs_readl(sf->base);
1085 save->limit = vmcs_read32(sf->limit);
1086 save->ar = vmcs_read32(sf->ar_bytes);
1087 vmcs_write16(sf->selector, vmcs_readl(sf->base) >> 4);
1088 vmcs_write32(sf->limit, 0xffff);
1089 vmcs_write32(sf->ar_bytes, 0xf3);
1090}
1091
1092static void enter_rmode(struct kvm_vcpu *vcpu)
1093{
1094 unsigned long flags;
1095
1096 vcpu->rmode.active = 1;
1097
1098 vcpu->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
1099 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1100
1101 vcpu->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
1102 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1103
1104 vcpu->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
1105 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1106
1107 flags = vmcs_readl(GUEST_RFLAGS);
1108 vcpu->rmode.save_iopl = (flags & IOPL_MASK) >> IOPL_SHIFT;
1109
1110 flags |= IOPL_MASK | X86_EFLAGS_VM;
1111
1112 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001113 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114 update_exception_bitmap(vcpu);
1115
1116 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1117 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1118 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1119
1120 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001121 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001122 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1123 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001124 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1125
1126 fix_rmode_seg(VCPU_SREG_ES, &vcpu->rmode.es);
1127 fix_rmode_seg(VCPU_SREG_DS, &vcpu->rmode.ds);
1128 fix_rmode_seg(VCPU_SREG_GS, &vcpu->rmode.gs);
1129 fix_rmode_seg(VCPU_SREG_FS, &vcpu->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001130
Eddie Dong8668a3c2007-10-10 14:26:45 +08001131 kvm_mmu_reset_context(vcpu);
Avi Kivity75880a02007-06-20 11:20:04 +03001132 init_rmode_tss(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133}
1134
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001135#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136
1137static void enter_lmode(struct kvm_vcpu *vcpu)
1138{
1139 u32 guest_tr_ar;
1140
1141 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1142 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1143 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
1144 __FUNCTION__);
1145 vmcs_write32(GUEST_TR_AR_BYTES,
1146 (guest_tr_ar & ~AR_TYPE_MASK)
1147 | AR_TYPE_BUSY_64_TSS);
1148 }
1149
1150 vcpu->shadow_efer |= EFER_LMA;
1151
Rusty Russell8b9cf982007-07-30 16:31:43 +10001152 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153 vmcs_write32(VM_ENTRY_CONTROLS,
1154 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001155 | VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156}
1157
1158static void exit_lmode(struct kvm_vcpu *vcpu)
1159{
1160 vcpu->shadow_efer &= ~EFER_LMA;
1161
1162 vmcs_write32(VM_ENTRY_CONTROLS,
1163 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001164 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165}
1166
1167#endif
1168
Anthony Liguori25c4c272007-04-27 09:29:21 +03001169static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001170{
Avi Kivity399badf2007-01-05 16:36:38 -08001171 vcpu->cr4 &= KVM_GUEST_CR4_MASK;
1172 vcpu->cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
1173}
1174
Avi Kivity6aa8b732006-12-10 02:21:36 -08001175static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1176{
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001177 vmx_fpu_deactivate(vcpu);
1178
Rusty Russell707d92fa2007-07-17 23:19:08 +10001179 if (vcpu->rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180 enter_pmode(vcpu);
1181
Rusty Russell707d92fa2007-07-17 23:19:08 +10001182 if (!vcpu->rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001183 enter_rmode(vcpu);
1184
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001185#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186 if (vcpu->shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001187 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001189 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001190 exit_lmode(vcpu);
1191 }
1192#endif
1193
1194 vmcs_writel(CR0_READ_SHADOW, cr0);
1195 vmcs_writel(GUEST_CR0,
1196 (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON);
1197 vcpu->cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001198
Rusty Russell707d92fa2007-07-17 23:19:08 +10001199 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001200 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001201}
1202
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1204{
1205 vmcs_writel(GUEST_CR3, cr3);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001206 if (vcpu->cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001207 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208}
1209
1210static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1211{
1212 vmcs_writel(CR4_READ_SHADOW, cr4);
1213 vmcs_writel(GUEST_CR4, cr4 | (vcpu->rmode.active ?
1214 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON));
1215 vcpu->cr4 = cr4;
1216}
1217
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001218#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219
1220static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1221{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001222 struct vcpu_vmx *vmx = to_vmx(vcpu);
1223 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001224
1225 vcpu->shadow_efer = efer;
1226 if (efer & EFER_LMA) {
1227 vmcs_write32(VM_ENTRY_CONTROLS,
1228 vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001229 VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001230 msr->data = efer;
1231
1232 } else {
1233 vmcs_write32(VM_ENTRY_CONTROLS,
1234 vmcs_read32(VM_ENTRY_CONTROLS) &
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001235 ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236
1237 msr->data = efer & ~EFER_LME;
1238 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001239 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001240}
1241
1242#endif
1243
1244static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1245{
1246 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1247
1248 return vmcs_readl(sf->base);
1249}
1250
1251static void vmx_get_segment(struct kvm_vcpu *vcpu,
1252 struct kvm_segment *var, int seg)
1253{
1254 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1255 u32 ar;
1256
1257 var->base = vmcs_readl(sf->base);
1258 var->limit = vmcs_read32(sf->limit);
1259 var->selector = vmcs_read16(sf->selector);
1260 ar = vmcs_read32(sf->ar_bytes);
1261 if (ar & AR_UNUSABLE_MASK)
1262 ar = 0;
1263 var->type = ar & 15;
1264 var->s = (ar >> 4) & 1;
1265 var->dpl = (ar >> 5) & 3;
1266 var->present = (ar >> 7) & 1;
1267 var->avl = (ar >> 12) & 1;
1268 var->l = (ar >> 13) & 1;
1269 var->db = (ar >> 14) & 1;
1270 var->g = (ar >> 15) & 1;
1271 var->unusable = (ar >> 16) & 1;
1272}
1273
Avi Kivity653e3102007-05-07 10:55:37 +03001274static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001275{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276 u32 ar;
1277
Avi Kivity653e3102007-05-07 10:55:37 +03001278 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001279 ar = 1 << 16;
1280 else {
1281 ar = var->type & 15;
1282 ar |= (var->s & 1) << 4;
1283 ar |= (var->dpl & 3) << 5;
1284 ar |= (var->present & 1) << 7;
1285 ar |= (var->avl & 1) << 12;
1286 ar |= (var->l & 1) << 13;
1287 ar |= (var->db & 1) << 14;
1288 ar |= (var->g & 1) << 15;
1289 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001290 if (ar == 0) /* a 0 value means unusable */
1291 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001292
1293 return ar;
1294}
1295
1296static void vmx_set_segment(struct kvm_vcpu *vcpu,
1297 struct kvm_segment *var, int seg)
1298{
1299 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1300 u32 ar;
1301
1302 if (vcpu->rmode.active && seg == VCPU_SREG_TR) {
1303 vcpu->rmode.tr.selector = var->selector;
1304 vcpu->rmode.tr.base = var->base;
1305 vcpu->rmode.tr.limit = var->limit;
1306 vcpu->rmode.tr.ar = vmx_segment_access_rights(var);
1307 return;
1308 }
1309 vmcs_writel(sf->base, var->base);
1310 vmcs_write32(sf->limit, var->limit);
1311 vmcs_write16(sf->selector, var->selector);
1312 if (vcpu->rmode.active && var->s) {
1313 /*
1314 * Hack real-mode segments into vm86 compatibility.
1315 */
1316 if (var->base == 0xffff0000 && var->selector == 0xf000)
1317 vmcs_writel(sf->base, 0xf0000);
1318 ar = 0xf3;
1319 } else
1320 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001321 vmcs_write32(sf->ar_bytes, ar);
1322}
1323
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1325{
1326 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1327
1328 *db = (ar >> 14) & 1;
1329 *l = (ar >> 13) & 1;
1330}
1331
1332static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1333{
1334 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1335 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1336}
1337
1338static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1339{
1340 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1341 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1342}
1343
1344static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1345{
1346 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1347 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1348}
1349
1350static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1351{
1352 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1353 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1354}
1355
1356static int init_rmode_tss(struct kvm* kvm)
1357{
1358 struct page *p1, *p2, *p3;
1359 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
1360 char *page;
1361
Avi Kivity954bbbc2007-03-30 14:02:32 +03001362 p1 = gfn_to_page(kvm, fn++);
1363 p2 = gfn_to_page(kvm, fn++);
1364 p3 = gfn_to_page(kvm, fn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001365
1366 if (!p1 || !p2 || !p3) {
1367 kvm_printf(kvm,"%s: gfn_to_page failed\n", __FUNCTION__);
1368 return 0;
1369 }
1370
1371 page = kmap_atomic(p1, KM_USER0);
Shani Moideena3870c42007-06-11 09:31:33 +05301372 clear_page(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373 *(u16*)(page + 0x66) = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
1374 kunmap_atomic(page, KM_USER0);
1375
1376 page = kmap_atomic(p2, KM_USER0);
Shani Moideena3870c42007-06-11 09:31:33 +05301377 clear_page(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378 kunmap_atomic(page, KM_USER0);
1379
1380 page = kmap_atomic(p3, KM_USER0);
Shani Moideena3870c42007-06-11 09:31:33 +05301381 clear_page(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382 *(page + RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1) = ~0;
1383 kunmap_atomic(page, KM_USER0);
1384
1385 return 1;
1386}
1387
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388static void seg_setup(int seg)
1389{
1390 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1391
1392 vmcs_write16(sf->selector, 0);
1393 vmcs_writel(sf->base, 0);
1394 vmcs_write32(sf->limit, 0xffff);
1395 vmcs_write32(sf->ar_bytes, 0x93);
1396}
1397
1398/*
1399 * Sets up the vmcs for emulated real mode.
1400 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001401static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402{
1403 u32 host_sysenter_cs;
1404 u32 junk;
1405 unsigned long a;
1406 struct descriptor_table dt;
1407 int i;
1408 int ret = 0;
Avi Kivitycd2276a2007-05-14 20:41:13 +03001409 unsigned long kvm_vmx_return;
Eddie Dong7017fc32007-07-18 11:34:57 +03001410 u64 msr;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001411 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412
Rusty Russell8b9cf982007-07-30 16:31:43 +10001413 if (!init_rmode_tss(vmx->vcpu.kvm)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001414 ret = -ENOMEM;
1415 goto out;
1416 }
1417
He, Qingc5ec1532007-09-03 17:07:41 +03001418 vmx->vcpu.rmode.active = 0;
1419
Rusty Russell8b9cf982007-07-30 16:31:43 +10001420 vmx->vcpu.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Eddie Dong7017fc32007-07-18 11:34:57 +03001421 set_cr8(&vmx->vcpu, 0);
1422 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Rusty Russell8b9cf982007-07-30 16:31:43 +10001423 if (vmx->vcpu.vcpu_id == 0)
Eddie Dong7017fc32007-07-18 11:34:57 +03001424 msr |= MSR_IA32_APICBASE_BSP;
1425 kvm_set_apic_base(&vmx->vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426
Rusty Russell8b9cf982007-07-30 16:31:43 +10001427 fx_init(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428
1429 /*
1430 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
1431 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
1432 */
He, Qingc5ec1532007-09-03 17:07:41 +03001433 if (vmx->vcpu.vcpu_id == 0) {
1434 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
1435 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
1436 } else {
1437 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.sipi_vector << 8);
1438 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.sipi_vector << 12);
1439 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
1441 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1442
1443 seg_setup(VCPU_SREG_DS);
1444 seg_setup(VCPU_SREG_ES);
1445 seg_setup(VCPU_SREG_FS);
1446 seg_setup(VCPU_SREG_GS);
1447 seg_setup(VCPU_SREG_SS);
1448
1449 vmcs_write16(GUEST_TR_SELECTOR, 0);
1450 vmcs_writel(GUEST_TR_BASE, 0);
1451 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
1452 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1453
1454 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
1455 vmcs_writel(GUEST_LDTR_BASE, 0);
1456 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
1457 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
1458
1459 vmcs_write32(GUEST_SYSENTER_CS, 0);
1460 vmcs_writel(GUEST_SYSENTER_ESP, 0);
1461 vmcs_writel(GUEST_SYSENTER_EIP, 0);
1462
1463 vmcs_writel(GUEST_RFLAGS, 0x02);
He, Qingc5ec1532007-09-03 17:07:41 +03001464 if (vmx->vcpu.vcpu_id == 0)
1465 vmcs_writel(GUEST_RIP, 0xfff0);
1466 else
1467 vmcs_writel(GUEST_RIP, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468 vmcs_writel(GUEST_RSP, 0);
1469
Avi Kivity6aa8b732006-12-10 02:21:36 -08001470 //todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0
1471 vmcs_writel(GUEST_DR7, 0x400);
1472
1473 vmcs_writel(GUEST_GDTR_BASE, 0);
1474 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
1475
1476 vmcs_writel(GUEST_IDTR_BASE, 0);
1477 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
1478
1479 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
1480 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
1481 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
1482
1483 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03001484 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
1485 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486
1487 guest_write_tsc(0);
1488
1489 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
1490
1491 /* Special registers */
1492 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
1493
1494 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001495 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
1496 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001497
1498 exec_control = vmcs_config.cpu_based_exec_ctrl;
1499 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
1500 exec_control &= ~CPU_BASED_TPR_SHADOW;
1501#ifdef CONFIG_X86_64
1502 exec_control |= CPU_BASED_CR8_STORE_EXITING |
1503 CPU_BASED_CR8_LOAD_EXITING;
1504#endif
1505 }
1506 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
1509 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
1510 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
1511
1512 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
1513 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
1514 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1515
1516 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
1517 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1518 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1519 vmcs_write16(HOST_FS_SELECTOR, read_fs()); /* 22.2.4 */
1520 vmcs_write16(HOST_GS_SELECTOR, read_gs()); /* 22.2.4 */
1521 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001522#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001523 rdmsrl(MSR_FS_BASE, a);
1524 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
1525 rdmsrl(MSR_GS_BASE, a);
1526 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
1527#else
1528 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
1529 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
1530#endif
1531
1532 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
1533
1534 get_idt(&dt);
1535 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
1536
Avi Kivitycd2276a2007-05-14 20:41:13 +03001537 asm ("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
1538 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03001539 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
1540 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
1541 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001542
1543 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
1544 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
1545 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
1546 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
1547 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
1548 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
1549
Avi Kivity6aa8b732006-12-10 02:21:36 -08001550 for (i = 0; i < NR_VMX_MSR; ++i) {
1551 u32 index = vmx_msr_index[i];
1552 u32 data_low, data_high;
1553 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001554 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001555
1556 if (rdmsr_safe(index, &data_low, &data_high) < 0)
1557 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08001558 if (wrmsr_safe(index, data_low, data_high) < 0)
1559 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001560 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001561 vmx->host_msrs[j].index = index;
1562 vmx->host_msrs[j].reserved = 0;
1563 vmx->host_msrs[j].data = data;
1564 vmx->guest_msrs[j] = vmx->host_msrs[j];
1565 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001566 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567
Rusty Russell8b9cf982007-07-30 16:31:43 +10001568 setup_msrs(vmx);
Avi Kivitye38aea32007-04-19 13:22:48 +03001569
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001570 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571
1572 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001573 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
1574
Avi Kivity6aa8b732006-12-10 02:21:36 -08001575 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
1576
Michael Riepe3b99ab22006-12-13 00:34:15 -08001577#ifdef CONFIG_X86_64
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001578 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
1579 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
1580 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
1581 page_to_phys(vmx->vcpu.apic->regs_page));
1582 vmcs_write32(TPR_THRESHOLD, 0);
Michael Riepe3b99ab22006-12-13 00:34:15 -08001583#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001584
Anthony Liguori25c4c272007-04-27 09:29:21 +03001585 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
1587
Rusty Russell8b9cf982007-07-30 16:31:43 +10001588 vmx->vcpu.cr0 = 0x60000010;
1589 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.cr0); // enter rmode
1590 vmx_set_cr4(&vmx->vcpu, 0);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001591#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001592 vmx_set_efer(&vmx->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +10001594 vmx_fpu_activate(&vmx->vcpu);
1595 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596
1597 return 0;
1598
Avi Kivity6aa8b732006-12-10 02:21:36 -08001599out:
1600 return ret;
1601}
1602
Avi Kivity04d2cc72007-09-10 18:10:54 +03001603static void vmx_vcpu_reset(struct kvm_vcpu *vcpu)
1604{
1605 struct vcpu_vmx *vmx = to_vmx(vcpu);
1606
1607 vmx_vcpu_setup(vmx);
1608}
1609
Avi Kivity6aa8b732006-12-10 02:21:36 -08001610static void inject_rmode_irq(struct kvm_vcpu *vcpu, int irq)
1611{
1612 u16 ent[2];
1613 u16 cs;
1614 u16 ip;
1615 unsigned long flags;
1616 unsigned long ss_base = vmcs_readl(GUEST_SS_BASE);
1617 u16 sp = vmcs_readl(GUEST_RSP);
1618 u32 ss_limit = vmcs_read32(GUEST_SS_LIMIT);
1619
Eric Sesterhenn / Snakebyte39649942007-04-09 16:15:05 +02001620 if (sp > ss_limit || sp < 6 ) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621 vcpu_printf(vcpu, "%s: #SS, rsp 0x%lx ss 0x%lx limit 0x%x\n",
1622 __FUNCTION__,
1623 vmcs_readl(GUEST_RSP),
1624 vmcs_readl(GUEST_SS_BASE),
1625 vmcs_read32(GUEST_SS_LIMIT));
1626 return;
1627 }
1628
Laurent Viviere7d5d762007-07-30 13:41:19 +03001629 if (emulator_read_std(irq * sizeof(ent), &ent, sizeof(ent), vcpu) !=
1630 X86EMUL_CONTINUE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631 vcpu_printf(vcpu, "%s: read guest err\n", __FUNCTION__);
1632 return;
1633 }
1634
1635 flags = vmcs_readl(GUEST_RFLAGS);
1636 cs = vmcs_readl(GUEST_CS_BASE) >> 4;
1637 ip = vmcs_readl(GUEST_RIP);
1638
1639
Laurent Viviere7d5d762007-07-30 13:41:19 +03001640 if (emulator_write_emulated(ss_base + sp - 2, &flags, 2, vcpu) != X86EMUL_CONTINUE ||
1641 emulator_write_emulated(ss_base + sp - 4, &cs, 2, vcpu) != X86EMUL_CONTINUE ||
1642 emulator_write_emulated(ss_base + sp - 6, &ip, 2, vcpu) != X86EMUL_CONTINUE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001643 vcpu_printf(vcpu, "%s: write guest err\n", __FUNCTION__);
1644 return;
1645 }
1646
1647 vmcs_writel(GUEST_RFLAGS, flags &
1648 ~( X86_EFLAGS_IF | X86_EFLAGS_AC | X86_EFLAGS_TF));
1649 vmcs_write16(GUEST_CS_SELECTOR, ent[1]) ;
1650 vmcs_writel(GUEST_CS_BASE, ent[1] << 4);
1651 vmcs_writel(GUEST_RIP, ent[0]);
1652 vmcs_writel(GUEST_RSP, (vmcs_readl(GUEST_RSP) & ~0xffff) | (sp - 6));
1653}
1654
Eddie Dong85f455f2007-07-06 12:20:49 +03001655static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
1656{
1657 if (vcpu->rmode.active) {
1658 inject_rmode_irq(vcpu, irq);
1659 return;
1660 }
1661 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
1662 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1663}
1664
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
1666{
1667 int word_index = __ffs(vcpu->irq_summary);
1668 int bit_index = __ffs(vcpu->irq_pending[word_index]);
1669 int irq = word_index * BITS_PER_LONG + bit_index;
1670
1671 clear_bit(bit_index, &vcpu->irq_pending[word_index]);
1672 if (!vcpu->irq_pending[word_index])
1673 clear_bit(word_index, &vcpu->irq_summary);
Eddie Dong85f455f2007-07-06 12:20:49 +03001674 vmx_inject_irq(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001675}
1676
Dor Laorc1150d82007-01-05 16:36:24 -08001677
1678static void do_interrupt_requests(struct kvm_vcpu *vcpu,
1679 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680{
Dor Laorc1150d82007-01-05 16:36:24 -08001681 u32 cpu_based_vm_exec_control;
1682
1683 vcpu->interrupt_window_open =
1684 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
1685 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
1686
1687 if (vcpu->interrupt_window_open &&
1688 vcpu->irq_summary &&
1689 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690 /*
Dor Laorc1150d82007-01-05 16:36:24 -08001691 * If interrupts enabled, and not blocked by sti or mov ss. Good.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692 */
1693 kvm_do_inject_irq(vcpu);
Dor Laorc1150d82007-01-05 16:36:24 -08001694
1695 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
1696 if (!vcpu->interrupt_window_open &&
1697 (vcpu->irq_summary || kvm_run->request_interrupt_window))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001698 /*
1699 * Interrupts blocked. Wait for unblock.
1700 */
Dor Laorc1150d82007-01-05 16:36:24 -08001701 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
1702 else
1703 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
1704 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705}
1706
1707static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
1708{
1709 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
1710
1711 set_debugreg(dbg->bp[0], 0);
1712 set_debugreg(dbg->bp[1], 1);
1713 set_debugreg(dbg->bp[2], 2);
1714 set_debugreg(dbg->bp[3], 3);
1715
1716 if (dbg->singlestep) {
1717 unsigned long flags;
1718
1719 flags = vmcs_readl(GUEST_RFLAGS);
1720 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1721 vmcs_writel(GUEST_RFLAGS, flags);
1722 }
1723}
1724
1725static int handle_rmode_exception(struct kvm_vcpu *vcpu,
1726 int vec, u32 err_code)
1727{
1728 if (!vcpu->rmode.active)
1729 return 0;
1730
Nitin A Kambleb3f37702007-05-17 15:50:34 +03001731 /*
1732 * Instruction with address size override prefix opcode 0x67
1733 * Cause the #SS fault with 0 error code in VM86 mode.
1734 */
1735 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001736 if (emulate_instruction(vcpu, NULL, 0, 0) == EMULATE_DONE)
1737 return 1;
1738 return 0;
1739}
1740
1741static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1742{
1743 u32 intr_info, error_code;
1744 unsigned long cr2, rip;
1745 u32 vect_info;
1746 enum emulation_result er;
Avi Kivitye2dec932007-01-05 16:36:54 -08001747 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001748
1749 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
1750 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
1751
1752 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
1753 !is_page_fault(intr_info)) {
1754 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
1755 "intr info 0x%x\n", __FUNCTION__, vect_info, intr_info);
1756 }
1757
Eddie Dong85f455f2007-07-06 12:20:49 +03001758 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001759 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
1760 set_bit(irq, vcpu->irq_pending);
1761 set_bit(irq / BITS_PER_LONG, &vcpu->irq_summary);
1762 }
1763
Avi Kivity1b6269d2007-10-09 12:12:19 +02001764 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
1765 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001766
1767 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001768 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001769 return 1;
1770 }
1771
Avi Kivity6aa8b732006-12-10 02:21:36 -08001772 error_code = 0;
1773 rip = vmcs_readl(GUEST_RIP);
1774 if (intr_info & INTR_INFO_DELIEVER_CODE_MASK)
1775 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
1776 if (is_page_fault(intr_info)) {
1777 cr2 = vmcs_readl(EXIT_QUALIFICATION);
1778
Shaohua Li11ec2802007-07-23 14:51:37 +08001779 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001780 r = kvm_mmu_page_fault(vcpu, cr2, error_code);
1781 if (r < 0) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001782 mutex_unlock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001783 return r;
1784 }
1785 if (!r) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001786 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001787 return 1;
1788 }
1789
1790 er = emulate_instruction(vcpu, kvm_run, cr2, error_code);
Shaohua Li11ec2802007-07-23 14:51:37 +08001791 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001792
1793 switch (er) {
1794 case EMULATE_DONE:
1795 return 1;
1796 case EMULATE_DO_MMIO:
Avi Kivity1165f5f2007-04-19 17:27:43 +03001797 ++vcpu->stat.mmio_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001798 return 0;
1799 case EMULATE_FAIL:
Avi Kivity054b1362007-09-12 13:21:09 +03001800 kvm_report_emulation_failure(vcpu, "pagetable");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001801 break;
1802 default:
1803 BUG();
1804 }
1805 }
1806
1807 if (vcpu->rmode.active &&
1808 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03001809 error_code)) {
1810 if (vcpu->halt_request) {
1811 vcpu->halt_request = 0;
1812 return kvm_emulate_halt(vcpu);
1813 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001814 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03001815 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001816
1817 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) == (INTR_TYPE_EXCEPTION | 1)) {
1818 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1819 return 0;
1820 }
1821 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
1822 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
1823 kvm_run->ex.error_code = error_code;
1824 return 0;
1825}
1826
1827static int handle_external_interrupt(struct kvm_vcpu *vcpu,
1828 struct kvm_run *kvm_run)
1829{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001830 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831 return 1;
1832}
1833
Avi Kivity988ad742007-02-12 00:54:36 -08001834static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1835{
1836 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1837 return 0;
1838}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001839
Avi Kivity6aa8b732006-12-10 02:21:36 -08001840static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1841{
He, Qingbfdaab02007-09-12 14:18:28 +08001842 unsigned long exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02001843 int size, down, in, string, rep;
1844 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845
Avi Kivity1165f5f2007-04-19 17:27:43 +03001846 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08001847 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02001848 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001849
1850 if (string) {
1851 if (emulate_instruction(vcpu, kvm_run, 0, 0) == EMULATE_DO_MMIO)
1852 return 0;
1853 return 1;
1854 }
1855
1856 size = (exit_qualification & 7) + 1;
1857 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001858 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001859 rep = (exit_qualification & 32) != 0;
1860 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03001861
Laurent Vivier3090dd72007-08-05 10:43:32 +03001862 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863}
1864
Ingo Molnar102d8322007-02-19 14:37:47 +02001865static void
1866vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
1867{
1868 /*
1869 * Patch in the VMCALL instruction:
1870 */
1871 hypercall[0] = 0x0f;
1872 hypercall[1] = 0x01;
1873 hypercall[2] = 0xc1;
1874 hypercall[3] = 0xc3;
1875}
1876
Avi Kivity6aa8b732006-12-10 02:21:36 -08001877static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1878{
He, Qingbfdaab02007-09-12 14:18:28 +08001879 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001880 int cr;
1881 int reg;
1882
He, Qingbfdaab02007-09-12 14:18:28 +08001883 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884 cr = exit_qualification & 15;
1885 reg = (exit_qualification >> 8) & 15;
1886 switch ((exit_qualification >> 4) & 3) {
1887 case 0: /* mov to cr */
1888 switch (cr) {
1889 case 0:
1890 vcpu_load_rsp_rip(vcpu);
1891 set_cr0(vcpu, vcpu->regs[reg]);
1892 skip_emulated_instruction(vcpu);
1893 return 1;
1894 case 3:
1895 vcpu_load_rsp_rip(vcpu);
1896 set_cr3(vcpu, vcpu->regs[reg]);
1897 skip_emulated_instruction(vcpu);
1898 return 1;
1899 case 4:
1900 vcpu_load_rsp_rip(vcpu);
1901 set_cr4(vcpu, vcpu->regs[reg]);
1902 skip_emulated_instruction(vcpu);
1903 return 1;
1904 case 8:
1905 vcpu_load_rsp_rip(vcpu);
1906 set_cr8(vcpu, vcpu->regs[reg]);
1907 skip_emulated_instruction(vcpu);
Yang, Sheng253abde2007-08-16 13:01:00 +03001908 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
1909 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001910 };
1911 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03001912 case 2: /* clts */
1913 vcpu_load_rsp_rip(vcpu);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001914 vmx_fpu_deactivate(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001915 vcpu->cr0 &= ~X86_CR0_TS;
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001916 vmcs_writel(CR0_READ_SHADOW, vcpu->cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001917 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03001918 skip_emulated_instruction(vcpu);
1919 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001920 case 1: /*mov from cr*/
1921 switch (cr) {
1922 case 3:
1923 vcpu_load_rsp_rip(vcpu);
1924 vcpu->regs[reg] = vcpu->cr3;
1925 vcpu_put_rsp_rip(vcpu);
1926 skip_emulated_instruction(vcpu);
1927 return 1;
1928 case 8:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001929 vcpu_load_rsp_rip(vcpu);
Eddie Dong7017fc32007-07-18 11:34:57 +03001930 vcpu->regs[reg] = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931 vcpu_put_rsp_rip(vcpu);
1932 skip_emulated_instruction(vcpu);
1933 return 1;
1934 }
1935 break;
1936 case 3: /* lmsw */
1937 lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
1938
1939 skip_emulated_instruction(vcpu);
1940 return 1;
1941 default:
1942 break;
1943 }
1944 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10001945 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08001946 (int)(exit_qualification >> 4) & 3, cr);
1947 return 0;
1948}
1949
1950static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1951{
He, Qingbfdaab02007-09-12 14:18:28 +08001952 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001953 unsigned long val;
1954 int dr, reg;
1955
1956 /*
1957 * FIXME: this code assumes the host is debugging the guest.
1958 * need to deal with guest debugging itself too.
1959 */
He, Qingbfdaab02007-09-12 14:18:28 +08001960 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961 dr = exit_qualification & 7;
1962 reg = (exit_qualification >> 8) & 15;
1963 vcpu_load_rsp_rip(vcpu);
1964 if (exit_qualification & 16) {
1965 /* mov from dr */
1966 switch (dr) {
1967 case 6:
1968 val = 0xffff0ff0;
1969 break;
1970 case 7:
1971 val = 0x400;
1972 break;
1973 default:
1974 val = 0;
1975 }
1976 vcpu->regs[reg] = val;
1977 } else {
1978 /* mov to dr */
1979 }
1980 vcpu_put_rsp_rip(vcpu);
1981 skip_emulated_instruction(vcpu);
1982 return 1;
1983}
1984
1985static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1986{
Avi Kivity06465c52007-02-28 20:46:53 +02001987 kvm_emulate_cpuid(vcpu);
1988 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001989}
1990
1991static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1992{
1993 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
1994 u64 data;
1995
1996 if (vmx_get_msr(vcpu, ecx, &data)) {
1997 vmx_inject_gp(vcpu, 0);
1998 return 1;
1999 }
2000
2001 /* FIXME: handling of bits 32:63 of rax, rdx */
2002 vcpu->regs[VCPU_REGS_RAX] = data & -1u;
2003 vcpu->regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
2004 skip_emulated_instruction(vcpu);
2005 return 1;
2006}
2007
2008static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2009{
2010 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
2011 u64 data = (vcpu->regs[VCPU_REGS_RAX] & -1u)
2012 | ((u64)(vcpu->regs[VCPU_REGS_RDX] & -1u) << 32);
2013
2014 if (vmx_set_msr(vcpu, ecx, data) != 0) {
2015 vmx_inject_gp(vcpu, 0);
2016 return 1;
2017 }
2018
2019 skip_emulated_instruction(vcpu);
2020 return 1;
2021}
2022
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002023static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2024 struct kvm_run *kvm_run)
2025{
2026 return 1;
2027}
2028
Avi Kivity6aa8b732006-12-10 02:21:36 -08002029static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2030 struct kvm_run *kvm_run)
2031{
Eddie Dong85f455f2007-07-06 12:20:49 +03002032 u32 cpu_based_vm_exec_control;
2033
2034 /* clear pending irq */
2035 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2036 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2037 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Dor Laorc1150d82007-01-05 16:36:24 -08002038 /*
2039 * If the user space waits to inject interrupts, exit as soon as
2040 * possible
2041 */
2042 if (kvm_run->request_interrupt_window &&
Dor Laor022a9302007-01-05 16:37:00 -08002043 !vcpu->irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002044 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002045 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002046 return 0;
2047 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002048 return 1;
2049}
2050
2051static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2052{
2053 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002054 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002055}
2056
Ingo Molnarc21415e2007-02-19 14:37:47 +02002057static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2058{
Dor Laor510043d2007-02-19 18:25:43 +02002059 skip_emulated_instruction(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02002060 return kvm_hypercall(vcpu, kvm_run);
Ingo Molnarc21415e2007-02-19 14:37:47 +02002061}
2062
Avi Kivity6aa8b732006-12-10 02:21:36 -08002063/*
2064 * The exit handlers return 1 if the exit was handled fully and guest execution
2065 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2066 * to be done to userspace and return 0.
2067 */
2068static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
2069 struct kvm_run *kvm_run) = {
2070 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
2071 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08002072 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002073 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002074 [EXIT_REASON_CR_ACCESS] = handle_cr,
2075 [EXIT_REASON_DR_ACCESS] = handle_dr,
2076 [EXIT_REASON_CPUID] = handle_cpuid,
2077 [EXIT_REASON_MSR_READ] = handle_rdmsr,
2078 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
2079 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
2080 [EXIT_REASON_HLT] = handle_halt,
Ingo Molnarc21415e2007-02-19 14:37:47 +02002081 [EXIT_REASON_VMCALL] = handle_vmcall,
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002082 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold
Avi Kivity6aa8b732006-12-10 02:21:36 -08002083};
2084
2085static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04002086 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087
2088/*
2089 * The guest has exited. See if we can fix it or if we need userspace
2090 * assistance.
2091 */
2092static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2093{
2094 u32 vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2095 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity29bd8a72007-09-10 17:27:03 +03002096 struct vcpu_vmx *vmx = to_vmx(vcpu);
2097
2098 if (unlikely(vmx->fail)) {
2099 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2100 kvm_run->fail_entry.hardware_entry_failure_reason
2101 = vmcs_read32(VM_INSTRUCTION_ERROR);
2102 return 0;
2103 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104
2105 if ( (vectoring_info & VECTORING_INFO_VALID_MASK) &&
2106 exit_reason != EXIT_REASON_EXCEPTION_NMI )
2107 printk(KERN_WARNING "%s: unexpected, valid vectoring info and "
2108 "exit reason is 0x%x\n", __FUNCTION__, exit_reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109 if (exit_reason < kvm_vmx_max_exit_handlers
2110 && kvm_vmx_exit_handlers[exit_reason])
2111 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
2112 else {
2113 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2114 kvm_run->hw.hardware_exit_reason = exit_reason;
2115 }
2116 return 0;
2117}
2118
Avi Kivityd9e368d2007-06-07 19:18:30 +03002119static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
2120{
Avi Kivityd9e368d2007-06-07 19:18:30 +03002121}
2122
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002123static void update_tpr_threshold(struct kvm_vcpu *vcpu)
2124{
2125 int max_irr, tpr;
2126
2127 if (!vm_need_tpr_shadow(vcpu->kvm))
2128 return;
2129
2130 if (!kvm_lapic_enabled(vcpu) ||
2131 ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
2132 vmcs_write32(TPR_THRESHOLD, 0);
2133 return;
2134 }
2135
2136 tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
2137 vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
2138}
2139
Eddie Dong85f455f2007-07-06 12:20:49 +03002140static void enable_irq_window(struct kvm_vcpu *vcpu)
2141{
2142 u32 cpu_based_vm_exec_control;
2143
2144 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2145 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2146 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2147}
2148
2149static void vmx_intr_assist(struct kvm_vcpu *vcpu)
2150{
2151 u32 idtv_info_field, intr_info_field;
2152 int has_ext_irq, interrupt_window_open;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002153 int vector;
Eddie Dong85f455f2007-07-06 12:20:49 +03002154
Eddie Dong1b9778d2007-09-03 16:56:58 +03002155 kvm_inject_pending_timer_irqs(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002156 update_tpr_threshold(vcpu);
2157
Eddie Dong85f455f2007-07-06 12:20:49 +03002158 has_ext_irq = kvm_cpu_has_interrupt(vcpu);
2159 intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
2160 idtv_info_field = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2161 if (intr_info_field & INTR_INFO_VALID_MASK) {
2162 if (idtv_info_field & INTR_INFO_VALID_MASK) {
2163 /* TODO: fault when IDT_Vectoring */
2164 printk(KERN_ERR "Fault when IDT_Vectoring\n");
2165 }
2166 if (has_ext_irq)
2167 enable_irq_window(vcpu);
2168 return;
2169 }
2170 if (unlikely(idtv_info_field & INTR_INFO_VALID_MASK)) {
2171 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, idtv_info_field);
2172 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2173 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
2174
2175 if (unlikely(idtv_info_field & INTR_INFO_DELIEVER_CODE_MASK))
2176 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2177 vmcs_read32(IDT_VECTORING_ERROR_CODE));
2178 if (unlikely(has_ext_irq))
2179 enable_irq_window(vcpu);
2180 return;
2181 }
2182 if (!has_ext_irq)
2183 return;
2184 interrupt_window_open =
2185 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2186 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002187 if (interrupt_window_open) {
2188 vector = kvm_cpu_get_interrupt(vcpu);
2189 vmx_inject_irq(vcpu, vector);
2190 kvm_timer_intr_post(vcpu, vector);
2191 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03002192 enable_irq_window(vcpu);
2193}
2194
Avi Kivity04d2cc72007-09-10 18:10:54 +03002195static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002196{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002197 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1b6269d2007-10-09 12:12:19 +02002198 u32 intr_info;
Avi Kivitye6adf282007-04-30 16:07:54 +03002199
2200 /*
2201 * Loading guest fpu may have cleared host cr0.ts
2202 */
2203 vmcs_writel(HOST_CR0, read_cr0());
2204
Avi Kivity6aa8b732006-12-10 02:21:36 -08002205 asm (
2206 /* Store host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002207#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002208 "push %%rax; push %%rbx; push %%rdx;"
2209 "push %%rsi; push %%rdi; push %%rbp;"
2210 "push %%r8; push %%r9; push %%r10; push %%r11;"
2211 "push %%r12; push %%r13; push %%r14; push %%r15;"
2212 "push %%rcx \n\t"
2213 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
2214#else
2215 "pusha; push %%ecx \n\t"
2216 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
2217#endif
2218 /* Check if vmlaunch of vmresume is needed */
2219 "cmp $0, %1 \n\t"
2220 /* Load guest registers. Don't clobber flags. */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002221#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002222 "mov %c[cr2](%3), %%rax \n\t"
2223 "mov %%rax, %%cr2 \n\t"
2224 "mov %c[rax](%3), %%rax \n\t"
2225 "mov %c[rbx](%3), %%rbx \n\t"
2226 "mov %c[rdx](%3), %%rdx \n\t"
2227 "mov %c[rsi](%3), %%rsi \n\t"
2228 "mov %c[rdi](%3), %%rdi \n\t"
2229 "mov %c[rbp](%3), %%rbp \n\t"
2230 "mov %c[r8](%3), %%r8 \n\t"
2231 "mov %c[r9](%3), %%r9 \n\t"
2232 "mov %c[r10](%3), %%r10 \n\t"
2233 "mov %c[r11](%3), %%r11 \n\t"
2234 "mov %c[r12](%3), %%r12 \n\t"
2235 "mov %c[r13](%3), %%r13 \n\t"
2236 "mov %c[r14](%3), %%r14 \n\t"
2237 "mov %c[r15](%3), %%r15 \n\t"
2238 "mov %c[rcx](%3), %%rcx \n\t" /* kills %3 (rcx) */
2239#else
2240 "mov %c[cr2](%3), %%eax \n\t"
2241 "mov %%eax, %%cr2 \n\t"
2242 "mov %c[rax](%3), %%eax \n\t"
2243 "mov %c[rbx](%3), %%ebx \n\t"
2244 "mov %c[rdx](%3), %%edx \n\t"
2245 "mov %c[rsi](%3), %%esi \n\t"
2246 "mov %c[rdi](%3), %%edi \n\t"
2247 "mov %c[rbp](%3), %%ebp \n\t"
2248 "mov %c[rcx](%3), %%ecx \n\t" /* kills %3 (ecx) */
2249#endif
2250 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03002251 "jne .Llaunched \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252 ASM_VMX_VMLAUNCH "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03002253 "jmp .Lkvm_vmx_return \n\t"
2254 ".Llaunched: " ASM_VMX_VMRESUME "\n\t"
2255 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256 /* Save guest registers, load host registers, keep flags */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002257#ifdef CONFIG_X86_64
Ingo Molnar96958232007-02-12 00:54:33 -08002258 "xchg %3, (%%rsp) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002259 "mov %%rax, %c[rax](%3) \n\t"
2260 "mov %%rbx, %c[rbx](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002261 "pushq (%%rsp); popq %c[rcx](%3) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262 "mov %%rdx, %c[rdx](%3) \n\t"
2263 "mov %%rsi, %c[rsi](%3) \n\t"
2264 "mov %%rdi, %c[rdi](%3) \n\t"
2265 "mov %%rbp, %c[rbp](%3) \n\t"
2266 "mov %%r8, %c[r8](%3) \n\t"
2267 "mov %%r9, %c[r9](%3) \n\t"
2268 "mov %%r10, %c[r10](%3) \n\t"
2269 "mov %%r11, %c[r11](%3) \n\t"
2270 "mov %%r12, %c[r12](%3) \n\t"
2271 "mov %%r13, %c[r13](%3) \n\t"
2272 "mov %%r14, %c[r14](%3) \n\t"
2273 "mov %%r15, %c[r15](%3) \n\t"
2274 "mov %%cr2, %%rax \n\t"
2275 "mov %%rax, %c[cr2](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002276 "mov (%%rsp), %3 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002277
2278 "pop %%rcx; pop %%r15; pop %%r14; pop %%r13; pop %%r12;"
2279 "pop %%r11; pop %%r10; pop %%r9; pop %%r8;"
2280 "pop %%rbp; pop %%rdi; pop %%rsi;"
2281 "pop %%rdx; pop %%rbx; pop %%rax \n\t"
2282#else
Ingo Molnar96958232007-02-12 00:54:33 -08002283 "xchg %3, (%%esp) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002284 "mov %%eax, %c[rax](%3) \n\t"
2285 "mov %%ebx, %c[rbx](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002286 "pushl (%%esp); popl %c[rcx](%3) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002287 "mov %%edx, %c[rdx](%3) \n\t"
2288 "mov %%esi, %c[rsi](%3) \n\t"
2289 "mov %%edi, %c[rdi](%3) \n\t"
2290 "mov %%ebp, %c[rbp](%3) \n\t"
2291 "mov %%cr2, %%eax \n\t"
2292 "mov %%eax, %c[cr2](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002293 "mov (%%esp), %3 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294
2295 "pop %%ecx; popa \n\t"
2296#endif
2297 "setbe %0 \n\t"
Avi Kivity29bd8a72007-09-10 17:27:03 +03002298 : "=q" (vmx->fail)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002299 : "r"(vmx->launched), "d"((unsigned long)HOST_RSP),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002300 "c"(vcpu),
2301 [rax]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RAX])),
2302 [rbx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBX])),
2303 [rcx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RCX])),
2304 [rdx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDX])),
2305 [rsi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RSI])),
2306 [rdi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDI])),
2307 [rbp]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002308#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309 [r8 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R8 ])),
2310 [r9 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R9 ])),
2311 [r10]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R10])),
2312 [r11]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R11])),
2313 [r12]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R12])),
2314 [r13]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R13])),
2315 [r14]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R14])),
2316 [r15]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R15])),
2317#endif
2318 [cr2]"i"(offsetof(struct kvm_vcpu, cr2))
2319 : "cc", "memory" );
2320
Dor Laorc1150d82007-01-05 16:36:24 -08002321 vcpu->interrupt_window_open = (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322
Avi Kivity6aa8b732006-12-10 02:21:36 -08002323 asm ("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03002324 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02002325
2326 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2327
2328 /* We need to handle NMIs before interrupts are enabled */
2329 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
2330 asm("int $2");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331}
2332
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333static void vmx_inject_page_fault(struct kvm_vcpu *vcpu,
2334 unsigned long addr,
2335 u32 err_code)
2336{
2337 u32 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2338
Avi Kivity1165f5f2007-04-19 17:27:43 +03002339 ++vcpu->stat.pf_guest;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002340
2341 if (is_page_fault(vect_info)) {
2342 printk(KERN_DEBUG "inject_page_fault: "
2343 "double fault 0x%lx @ 0x%lx\n",
2344 addr, vmcs_readl(GUEST_RIP));
2345 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
2346 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2347 DF_VECTOR |
2348 INTR_TYPE_EXCEPTION |
2349 INTR_INFO_DELIEVER_CODE_MASK |
2350 INTR_INFO_VALID_MASK);
2351 return;
2352 }
2353 vcpu->cr2 = addr;
2354 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, err_code);
2355 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2356 PF_VECTOR |
2357 INTR_TYPE_EXCEPTION |
2358 INTR_INFO_DELIEVER_CODE_MASK |
2359 INTR_INFO_VALID_MASK);
2360
2361}
2362
2363static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
2364{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002365 struct vcpu_vmx *vmx = to_vmx(vcpu);
2366
2367 if (vmx->vmcs) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002368 on_each_cpu(__vcpu_clear, vmx, 0, 1);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002369 free_vmcs(vmx->vmcs);
2370 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371 }
2372}
2373
2374static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
2375{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002376 struct vcpu_vmx *vmx = to_vmx(vcpu);
2377
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002379 kfree(vmx->host_msrs);
2380 kfree(vmx->guest_msrs);
2381 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10002382 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383}
2384
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002385static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002387 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10002388 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03002389 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002391 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002392 return ERR_PTR(-ENOMEM);
2393
2394 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
2395 if (err)
2396 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002397
Eddie Dong97222cc2007-09-12 10:58:04 +03002398 if (irqchip_in_kernel(kvm)) {
2399 err = kvm_create_lapic(&vmx->vcpu);
2400 if (err < 0)
2401 goto free_vcpu;
2402 }
2403
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002404 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002405 if (!vmx->guest_msrs) {
2406 err = -ENOMEM;
2407 goto uninit_vcpu;
2408 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08002409
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002410 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
2411 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002412 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002413
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002414 vmx->vmcs = alloc_vmcs();
2415 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002416 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002417
2418 vmcs_clear(vmx->vmcs);
2419
Avi Kivity15ad7142007-07-11 18:17:21 +03002420 cpu = get_cpu();
2421 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002422 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002423 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002424 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002425 if (err)
2426 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002427
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002428 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002429
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002430free_vmcs:
2431 free_vmcs(vmx->vmcs);
2432free_msrs:
2433 kfree(vmx->host_msrs);
2434free_guest_msrs:
2435 kfree(vmx->guest_msrs);
2436uninit_vcpu:
2437 kvm_vcpu_uninit(&vmx->vcpu);
2438free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10002439 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002440 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002441}
2442
Yang, Sheng002c7f72007-07-31 14:23:01 +03002443static void __init vmx_check_processor_compat(void *rtn)
2444{
2445 struct vmcs_config vmcs_conf;
2446
2447 *(int *)rtn = 0;
2448 if (setup_vmcs_config(&vmcs_conf) < 0)
2449 *(int *)rtn = -EIO;
2450 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
2451 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
2452 smp_processor_id());
2453 *(int *)rtn = -EIO;
2454 }
2455}
2456
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002457static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458 .cpu_has_kvm_support = cpu_has_kvm_support,
2459 .disabled_by_bios = vmx_disabled_by_bios,
2460 .hardware_setup = hardware_setup,
2461 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03002462 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463 .hardware_enable = hardware_enable,
2464 .hardware_disable = hardware_disable,
2465
2466 .vcpu_create = vmx_create_vcpu,
2467 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002468 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469
Avi Kivity04d2cc72007-09-10 18:10:54 +03002470 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471 .vcpu_load = vmx_vcpu_load,
2472 .vcpu_put = vmx_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08002473 .vcpu_decache = vmx_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002474
2475 .set_guest_debug = set_guest_debug,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002476 .guest_debug_pre = kvm_guest_debug_pre,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002477 .get_msr = vmx_get_msr,
2478 .set_msr = vmx_set_msr,
2479 .get_segment_base = vmx_get_segment_base,
2480 .get_segment = vmx_get_segment,
2481 .set_segment = vmx_set_segment,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002482 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03002483 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002484 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002485 .set_cr3 = vmx_set_cr3,
2486 .set_cr4 = vmx_set_cr4,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002487#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002488 .set_efer = vmx_set_efer,
2489#endif
2490 .get_idt = vmx_get_idt,
2491 .set_idt = vmx_set_idt,
2492 .get_gdt = vmx_get_gdt,
2493 .set_gdt = vmx_set_gdt,
2494 .cache_regs = vcpu_load_rsp_rip,
2495 .decache_regs = vcpu_put_rsp_rip,
2496 .get_rflags = vmx_get_rflags,
2497 .set_rflags = vmx_set_rflags,
2498
2499 .tlb_flush = vmx_flush_tlb,
2500 .inject_page_fault = vmx_inject_page_fault,
2501
2502 .inject_gp = vmx_inject_gp,
2503
2504 .run = vmx_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002505 .handle_exit = kvm_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002506 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02002507 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03002508 .get_irq = vmx_get_irq,
2509 .set_irq = vmx_inject_irq,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002510 .inject_pending_irq = vmx_intr_assist,
2511 .inject_pending_vectors = do_interrupt_requests,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002512};
2513
2514static int __init vmx_init(void)
2515{
He, Qingfdef3ad2007-04-30 09:45:24 +03002516 void *iova;
2517 int r;
2518
2519 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2520 if (!vmx_io_bitmap_a)
2521 return -ENOMEM;
2522
2523 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2524 if (!vmx_io_bitmap_b) {
2525 r = -ENOMEM;
2526 goto out;
2527 }
2528
2529 /*
2530 * Allow direct access to the PC debug port (it is often used for I/O
2531 * delays, but the vmexits simply slow things down).
2532 */
2533 iova = kmap(vmx_io_bitmap_a);
2534 memset(iova, 0xff, PAGE_SIZE);
2535 clear_bit(0x80, iova);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002536 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03002537
2538 iova = kmap(vmx_io_bitmap_b);
2539 memset(iova, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002540 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03002541
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002542 r = kvm_init_x86(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03002543 if (r)
2544 goto out1;
2545
2546 return 0;
2547
2548out1:
2549 __free_page(vmx_io_bitmap_b);
2550out:
2551 __free_page(vmx_io_bitmap_a);
2552 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002553}
2554
2555static void __exit vmx_exit(void)
2556{
He, Qingfdef3ad2007-04-30 09:45:24 +03002557 __free_page(vmx_io_bitmap_b);
2558 __free_page(vmx_io_bitmap_a);
2559
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002560 kvm_exit_x86();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002561}
2562
2563module_init(vmx_init)
2564module_exit(vmx_exit)