blob: cc2844203c246afda087b18f05eabae0f4a48481 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
18#include "kvm.h"
Laurent Viviere7d5d762007-07-30 13:41:19 +030019#include "x86_emulate.h"
Eddie Dong85f455f2007-07-06 12:20:49 +030020#include "irq.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080021#include "vmx.h"
Avi Kivitye4956062007-06-28 14:15:57 -040022#include "segment_descriptor.h"
23
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020025#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/mm.h>
27#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040028#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020029#include <linux/moduleparam.h>
Avi Kivitye4956062007-06-28 14:15:57 -040030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080032#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080033
Avi Kivity6aa8b732006-12-10 02:21:36 -080034MODULE_AUTHOR("Qumranet");
35MODULE_LICENSE("GPL");
36
Avi Kivityc7addb92007-09-16 18:58:32 +020037static int bypass_guest_pf = 1;
38module_param(bypass_guest_pf, bool, 0);
39
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040040struct vmcs {
41 u32 revision_id;
42 u32 abort;
43 char data[0];
44};
45
46struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100047 struct kvm_vcpu vcpu;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040048 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030049 u8 fail;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040050 struct kvm_msr_entry *guest_msrs;
51 struct kvm_msr_entry *host_msrs;
52 int nmsrs;
53 int save_nmsrs;
54 int msr_offset_efer;
55#ifdef CONFIG_X86_64
56 int msr_offset_kernel_gs_base;
57#endif
58 struct vmcs *vmcs;
59 struct {
60 int loaded;
61 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020062 int gs_ldt_reload_needed;
63 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030064 int guest_efer_loaded;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040065 }host_state;
66
67};
68
69static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
70{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100071 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040072}
73
Avi Kivity75880a02007-06-20 11:20:04 +030074static int init_rmode_tss(struct kvm *kvm);
75
Avi Kivity6aa8b732006-12-10 02:21:36 -080076static DEFINE_PER_CPU(struct vmcs *, vmxarea);
77static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
78
He, Qingfdef3ad2007-04-30 09:45:24 +030079static struct page *vmx_io_bitmap_a;
80static struct page *vmx_io_bitmap_b;
81
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +030082static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -080083 int size;
84 int order;
85 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +030086 u32 pin_based_exec_ctrl;
87 u32 cpu_based_exec_ctrl;
88 u32 vmexit_ctrl;
89 u32 vmentry_ctrl;
90} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -080091
92#define VMX_SEGMENT_FIELD(seg) \
93 [VCPU_SREG_##seg] = { \
94 .selector = GUEST_##seg##_SELECTOR, \
95 .base = GUEST_##seg##_BASE, \
96 .limit = GUEST_##seg##_LIMIT, \
97 .ar_bytes = GUEST_##seg##_AR_BYTES, \
98 }
99
100static struct kvm_vmx_segment_field {
101 unsigned selector;
102 unsigned base;
103 unsigned limit;
104 unsigned ar_bytes;
105} kvm_vmx_segment_fields[] = {
106 VMX_SEGMENT_FIELD(CS),
107 VMX_SEGMENT_FIELD(DS),
108 VMX_SEGMENT_FIELD(ES),
109 VMX_SEGMENT_FIELD(FS),
110 VMX_SEGMENT_FIELD(GS),
111 VMX_SEGMENT_FIELD(SS),
112 VMX_SEGMENT_FIELD(TR),
113 VMX_SEGMENT_FIELD(LDTR),
114};
115
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300116/*
117 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
118 * away by decrementing the array size.
119 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800121#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
123#endif
124 MSR_EFER, MSR_K6_STAR,
125};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200126#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800127
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400128static void load_msrs(struct kvm_msr_entry *e, int n)
129{
130 int i;
131
132 for (i = 0; i < n; ++i)
133 wrmsrl(e[i].index, e[i].data);
134}
135
136static void save_msrs(struct kvm_msr_entry *e, int n)
137{
138 int i;
139
140 for (i = 0; i < n; ++i)
141 rdmsrl(e[i].index, e[i].data);
142}
143
Avi Kivity6aa8b732006-12-10 02:21:36 -0800144static inline int is_page_fault(u32 intr_info)
145{
146 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
147 INTR_INFO_VALID_MASK)) ==
148 (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
149}
150
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300151static inline int is_no_device(u32 intr_info)
152{
153 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
154 INTR_INFO_VALID_MASK)) ==
155 (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
156}
157
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500158static inline int is_invalid_opcode(u32 intr_info)
159{
160 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
161 INTR_INFO_VALID_MASK)) ==
162 (INTR_TYPE_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
163}
164
Avi Kivity6aa8b732006-12-10 02:21:36 -0800165static inline int is_external_interrupt(u32 intr_info)
166{
167 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
168 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
169}
170
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800171static inline int cpu_has_vmx_tpr_shadow(void)
172{
173 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW);
174}
175
176static inline int vm_need_tpr_shadow(struct kvm *kvm)
177{
178 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)));
179}
180
Rusty Russell8b9cf982007-07-30 16:31:43 +1000181static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800182{
183 int i;
184
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400185 for (i = 0; i < vmx->nmsrs; ++i)
186 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300187 return i;
188 return -1;
189}
190
Rusty Russell8b9cf982007-07-30 16:31:43 +1000191static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300192{
193 int i;
194
Rusty Russell8b9cf982007-07-30 16:31:43 +1000195 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300196 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400197 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000198 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800199}
200
Avi Kivity6aa8b732006-12-10 02:21:36 -0800201static void vmcs_clear(struct vmcs *vmcs)
202{
203 u64 phys_addr = __pa(vmcs);
204 u8 error;
205
206 asm volatile (ASM_VMX_VMCLEAR_RAX "; setna %0"
207 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
208 : "cc", "memory");
209 if (error)
210 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
211 vmcs, phys_addr);
212}
213
214static void __vcpu_clear(void *arg)
215{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000216 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800217 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218
Rusty Russell8b9cf982007-07-30 16:31:43 +1000219 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400220 vmcs_clear(vmx->vmcs);
221 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222 per_cpu(current_vmcs, cpu) = NULL;
Rusty Russell8b9cf982007-07-30 16:31:43 +1000223 rdtscll(vmx->vcpu.host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224}
225
Rusty Russell8b9cf982007-07-30 16:31:43 +1000226static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800227{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200228 if (vmx->vcpu.cpu == -1)
229 return;
230 if (vmx->vcpu.cpu != raw_smp_processor_id())
Rusty Russell8b9cf982007-07-30 16:31:43 +1000231 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
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500318 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300319 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{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400347 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300348 u64 host_efer = vmx->host_msrs[efer_offset].data;
349 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
350 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300351
Avi Kivity51c6cf62007-08-29 03:48:05 +0300352 if (efer_offset < 0)
353 return;
354 /*
355 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
356 * outside long mode
357 */
358 ignore_bits = EFER_NX | EFER_SCE;
359#ifdef CONFIG_X86_64
360 ignore_bits |= EFER_LMA | EFER_LME;
361 /* SCE is meaningful only in long mode on Intel */
362 if (guest_efer & EFER_LMA)
363 ignore_bits &= ~(u64)EFER_SCE;
364#endif
365 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
366 return;
367
368 vmx->host_state.guest_efer_loaded = 1;
369 guest_efer &= ~ignore_bits;
370 guest_efer |= host_efer & ignore_bits;
371 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000372 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300373}
374
Avi Kivity51c6cf62007-08-29 03:48:05 +0300375static void reload_host_efer(struct vcpu_vmx *vmx)
376{
377 if (vmx->host_state.guest_efer_loaded) {
378 vmx->host_state.guest_efer_loaded = 0;
379 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
380 }
381}
382
Avi Kivity04d2cc72007-09-10 18:10:54 +0300383static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300384{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300385 struct vcpu_vmx *vmx = to_vmx(vcpu);
386
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400387 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300388 return;
389
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400390 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300391 /*
392 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
393 * allow segment selectors with cpl > 0 or ti == 1.
394 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400395 vmx->host_state.ldt_sel = read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200396 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400397 vmx->host_state.fs_sel = read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200398 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400399 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200400 vmx->host_state.fs_reload_needed = 0;
401 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300402 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200403 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300404 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400405 vmx->host_state.gs_sel = read_gs();
406 if (!(vmx->host_state.gs_sel & 7))
407 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300408 else {
409 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200410 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300411 }
412
413#ifdef CONFIG_X86_64
414 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
415 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
416#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400417 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
418 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300419#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300420
421#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000422 if (is_long_mode(&vmx->vcpu)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400423 save_msrs(vmx->host_msrs +
424 vmx->msr_offset_kernel_gs_base, 1);
Avi Kivity707c0872007-05-02 17:33:43 +0300425 }
426#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400427 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300428 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300429}
430
Rusty Russell8b9cf982007-07-30 16:31:43 +1000431static void vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300432{
Avi Kivity15ad7142007-07-11 18:17:21 +0300433 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300434
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400435 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300436 return;
437
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400438 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200439 if (vmx->host_state.fs_reload_needed)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400440 load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200441 if (vmx->host_state.gs_ldt_reload_needed) {
442 load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300443 /*
444 * If we have to reload gs, we must take care to
445 * preserve our gs base.
446 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300447 local_irq_save(flags);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400448 load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300449#ifdef CONFIG_X86_64
450 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
451#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300452 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300453 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200454 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400455 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
456 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300457 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300458}
459
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460/*
461 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
462 * vcpu mutex is already taken.
463 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300464static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800465{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400466 struct vcpu_vmx *vmx = to_vmx(vcpu);
467 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity77002702007-06-13 19:55:28 +0300468 u64 tsc_this, delta;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469
Eddie Donga3d7f852007-09-03 16:15:12 +0300470 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000471 vcpu_clear(vmx);
Eddie Donga3d7f852007-09-03 16:15:12 +0300472 kvm_migrate_apic_timer(vcpu);
473 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800474
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400475 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800476 u8 error;
477
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400478 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 asm volatile (ASM_VMX_VMPTRLD_RAX "; setna %0"
480 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
481 : "cc");
482 if (error)
483 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400484 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800485 }
486
487 if (vcpu->cpu != cpu) {
488 struct descriptor_table dt;
489 unsigned long sysenter_esp;
490
491 vcpu->cpu = cpu;
492 /*
493 * Linux uses per-cpu TSS and GDT, so set these when switching
494 * processors.
495 */
496 vmcs_writel(HOST_TR_BASE, read_tr_base()); /* 22.2.4 */
497 get_gdt(&dt);
498 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
499
500 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
501 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300502
503 /*
504 * Make sure the time stamp counter is monotonous.
505 */
506 rdtscll(tsc_this);
507 delta = vcpu->host_tsc - tsc_this;
508 vmcs_write64(TSC_OFFSET, vmcs_read64(TSC_OFFSET) + delta);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800509 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800510}
511
512static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
513{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000514 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity7702fd12007-06-14 16:27:40 +0300515 kvm_put_guest_fpu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800516}
517
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300518static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
519{
520 if (vcpu->fpu_active)
521 return;
522 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000523 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
524 if (vcpu->cr0 & X86_CR0_TS)
525 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300526 update_exception_bitmap(vcpu);
527}
528
529static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
530{
531 if (!vcpu->fpu_active)
532 return;
533 vcpu->fpu_active = 0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000534 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300535 update_exception_bitmap(vcpu);
536}
537
Avi Kivity774c47f2007-02-12 00:54:47 -0800538static void vmx_vcpu_decache(struct kvm_vcpu *vcpu)
539{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000540 vcpu_clear(to_vmx(vcpu));
Avi Kivity774c47f2007-02-12 00:54:47 -0800541}
542
Avi Kivity6aa8b732006-12-10 02:21:36 -0800543static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
544{
545 return vmcs_readl(GUEST_RFLAGS);
546}
547
548static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
549{
Avi Kivity78f78268682007-10-16 19:06:15 +0200550 if (vcpu->rmode.active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100551 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800552 vmcs_writel(GUEST_RFLAGS, rflags);
553}
554
555static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
556{
557 unsigned long rip;
558 u32 interruptibility;
559
560 rip = vmcs_readl(GUEST_RIP);
561 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
562 vmcs_writel(GUEST_RIP, rip);
563
564 /*
565 * We emulated an instruction, so temporary interrupt blocking
566 * should be removed, if set.
567 */
568 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
569 if (interruptibility & 3)
570 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
571 interruptibility & ~3);
Dor Laorc1150d82007-01-05 16:36:24 -0800572 vcpu->interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573}
574
575static void vmx_inject_gp(struct kvm_vcpu *vcpu, unsigned error_code)
576{
577 printk(KERN_DEBUG "inject_general_protection: rip 0x%lx\n",
578 vmcs_readl(GUEST_RIP));
579 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
580 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
581 GP_VECTOR |
582 INTR_TYPE_EXCEPTION |
583 INTR_INFO_DELIEVER_CODE_MASK |
584 INTR_INFO_VALID_MASK);
585}
586
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500587static void vmx_inject_ud(struct kvm_vcpu *vcpu)
588{
589 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
590 UD_VECTOR |
591 INTR_TYPE_EXCEPTION |
592 INTR_INFO_VALID_MASK);
593}
594
Avi Kivity6aa8b732006-12-10 02:21:36 -0800595/*
Eddie Donga75beee2007-05-17 18:55:15 +0300596 * Swap MSR entry in host/guest MSR entry array.
597 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200598#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000599static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300600{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400601 struct kvm_msr_entry tmp;
602
603 tmp = vmx->guest_msrs[to];
604 vmx->guest_msrs[to] = vmx->guest_msrs[from];
605 vmx->guest_msrs[from] = tmp;
606 tmp = vmx->host_msrs[to];
607 vmx->host_msrs[to] = vmx->host_msrs[from];
608 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300609}
Gabriel C54e11fa2007-08-01 16:23:10 +0200610#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300611
612/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300613 * Set up the vmcs to automatically save and restore system
614 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
615 * mode, as fiddling with msrs is very expensive.
616 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000617static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300618{
Eddie Dong2cc51562007-05-21 07:28:09 +0300619 int save_nmsrs;
Avi Kivitye38aea32007-04-19 13:22:48 +0300620
Eddie Donga75beee2007-05-17 18:55:15 +0300621 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300622#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000623 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300624 int index;
625
Rusty Russell8b9cf982007-07-30 16:31:43 +1000626 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300627 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000628 move_msr_up(vmx, index, save_nmsrs++);
629 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300630 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000631 move_msr_up(vmx, index, save_nmsrs++);
632 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300633 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000634 move_msr_up(vmx, index, save_nmsrs++);
635 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300636 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000637 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300638 /*
639 * MSR_K6_STAR is only needed on long mode guests, and only
640 * if efer.sce is enabled.
641 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000642 index = __find_msr_index(vmx, MSR_K6_STAR);
643 if ((index >= 0) && (vmx->vcpu.shadow_efer & EFER_SCE))
644 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300645 }
Eddie Donga75beee2007-05-17 18:55:15 +0300646#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400647 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300648
Eddie Donga75beee2007-05-17 18:55:15 +0300649#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400650 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000651 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300652#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000653 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivitye38aea32007-04-19 13:22:48 +0300654}
655
656/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657 * reads and returns guest's timestamp counter "register"
658 * guest_tsc = host_tsc + tsc_offset -- 21.3
659 */
660static u64 guest_read_tsc(void)
661{
662 u64 host_tsc, tsc_offset;
663
664 rdtscll(host_tsc);
665 tsc_offset = vmcs_read64(TSC_OFFSET);
666 return host_tsc + tsc_offset;
667}
668
669/*
670 * writes 'guest_tsc' into guest's timestamp counter "register"
671 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
672 */
673static void guest_write_tsc(u64 guest_tsc)
674{
675 u64 host_tsc;
676
677 rdtscll(host_tsc);
678 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
679}
680
Avi Kivity6aa8b732006-12-10 02:21:36 -0800681/*
682 * Reads an msr value (of 'msr_index') into 'pdata'.
683 * Returns 0 on success, non-0 otherwise.
684 * Assumes vcpu_load() was already called.
685 */
686static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
687{
688 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400689 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800690
691 if (!pdata) {
692 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
693 return -EINVAL;
694 }
695
696 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800697#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800698 case MSR_FS_BASE:
699 data = vmcs_readl(GUEST_FS_BASE);
700 break;
701 case MSR_GS_BASE:
702 data = vmcs_readl(GUEST_GS_BASE);
703 break;
704 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800705 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706#endif
707 case MSR_IA32_TIME_STAMP_COUNTER:
708 data = guest_read_tsc();
709 break;
710 case MSR_IA32_SYSENTER_CS:
711 data = vmcs_read32(GUEST_SYSENTER_CS);
712 break;
713 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200714 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715 break;
716 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200717 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800718 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000720 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800721 if (msr) {
722 data = msr->data;
723 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800724 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800725 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800726 }
727
728 *pdata = data;
729 return 0;
730}
731
732/*
733 * Writes msr value into into the appropriate "register".
734 * Returns 0 on success, non-0 otherwise.
735 * Assumes vcpu_load() was already called.
736 */
737static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
738{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400739 struct vcpu_vmx *vmx = to_vmx(vcpu);
740 struct kvm_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +0300741 int ret = 0;
742
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800744#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800745 case MSR_EFER:
Eddie Dong2cc51562007-05-21 07:28:09 +0300746 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300747 if (vmx->host_state.loaded) {
748 reload_host_efer(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000749 load_transition_efer(vmx);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300750 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300751 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752 case MSR_FS_BASE:
753 vmcs_writel(GUEST_FS_BASE, data);
754 break;
755 case MSR_GS_BASE:
756 vmcs_writel(GUEST_GS_BASE, data);
757 break;
758#endif
759 case MSR_IA32_SYSENTER_CS:
760 vmcs_write32(GUEST_SYSENTER_CS, data);
761 break;
762 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200763 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764 break;
765 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200766 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200768 case MSR_IA32_TIME_STAMP_COUNTER:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769 guest_write_tsc(data);
770 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800771 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000772 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800773 if (msr) {
774 msr->data = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400775 if (vmx->host_state.loaded)
776 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800777 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300779 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800780 }
781
Eddie Dong2cc51562007-05-21 07:28:09 +0300782 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783}
784
785/*
786 * Sync the rsp and rip registers into the vcpu structure. This allows
787 * registers to be accessed by indexing vcpu->regs.
788 */
789static void vcpu_load_rsp_rip(struct kvm_vcpu *vcpu)
790{
791 vcpu->regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
792 vcpu->rip = vmcs_readl(GUEST_RIP);
793}
794
795/*
796 * Syncs rsp and rip back into the vmcs. Should be called after possible
797 * modification.
798 */
799static void vcpu_put_rsp_rip(struct kvm_vcpu *vcpu)
800{
801 vmcs_writel(GUEST_RSP, vcpu->regs[VCPU_REGS_RSP]);
802 vmcs_writel(GUEST_RIP, vcpu->rip);
803}
804
805static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
806{
807 unsigned long dr7 = 0x400;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800808 int old_singlestep;
809
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810 old_singlestep = vcpu->guest_debug.singlestep;
811
812 vcpu->guest_debug.enabled = dbg->enabled;
813 if (vcpu->guest_debug.enabled) {
814 int i;
815
816 dr7 |= 0x200; /* exact */
817 for (i = 0; i < 4; ++i) {
818 if (!dbg->breakpoints[i].enabled)
819 continue;
820 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
821 dr7 |= 2 << (i*2); /* global enable */
822 dr7 |= 0 << (i*4+16); /* execution breakpoint */
823 }
824
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825 vcpu->guest_debug.singlestep = dbg->singlestep;
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300826 } else
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827 vcpu->guest_debug.singlestep = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828
829 if (old_singlestep && !vcpu->guest_debug.singlestep) {
830 unsigned long flags;
831
832 flags = vmcs_readl(GUEST_RFLAGS);
833 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
834 vmcs_writel(GUEST_RFLAGS, flags);
835 }
836
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300837 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838 vmcs_writel(GUEST_DR7, dr7);
839
840 return 0;
841}
842
Eddie Dong2a8067f2007-08-06 16:29:07 +0300843static int vmx_get_irq(struct kvm_vcpu *vcpu)
844{
845 u32 idtv_info_field;
846
847 idtv_info_field = vmcs_read32(IDT_VECTORING_INFO_FIELD);
848 if (idtv_info_field & INTR_INFO_VALID_MASK) {
849 if (is_external_interrupt(idtv_info_field))
850 return idtv_info_field & VECTORING_INFO_VECTOR_MASK;
851 else
852 printk("pending exception: not handled yet\n");
853 }
854 return -1;
855}
856
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857static __init int cpu_has_kvm_support(void)
858{
859 unsigned long ecx = cpuid_ecx(1);
860 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
861}
862
863static __init int vmx_disabled_by_bios(void)
864{
865 u64 msr;
866
867 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300868 return (msr & (MSR_IA32_FEATURE_CONTROL_LOCKED |
869 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
870 == MSR_IA32_FEATURE_CONTROL_LOCKED;
871 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800872}
873
Avi Kivity774c47f2007-02-12 00:54:47 -0800874static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875{
876 int cpu = raw_smp_processor_id();
877 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
878 u64 old;
879
880 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300881 if ((old & (MSR_IA32_FEATURE_CONTROL_LOCKED |
882 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
883 != (MSR_IA32_FEATURE_CONTROL_LOCKED |
884 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300886 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
887 MSR_IA32_FEATURE_CONTROL_LOCKED |
888 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +1000889 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 asm volatile (ASM_VMX_VMXON_RAX : : "a"(&phys_addr), "m"(phys_addr)
891 : "memory", "cc");
892}
893
894static void hardware_disable(void *garbage)
895{
896 asm volatile (ASM_VMX_VMXOFF : : : "cc");
897}
898
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300899static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
900 u32 msr, u32* result)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901{
902 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300903 u32 ctl = ctl_min | ctl_opt;
904
905 rdmsr(msr, vmx_msr_low, vmx_msr_high);
906
907 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
908 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
909
910 /* Ensure minimum (required) set of control bits are supported. */
911 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300912 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300913
914 *result = ctl;
915 return 0;
916}
917
Yang, Sheng002c7f72007-07-31 14:23:01 +0300918static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300919{
920 u32 vmx_msr_low, vmx_msr_high;
921 u32 min, opt;
922 u32 _pin_based_exec_control = 0;
923 u32 _cpu_based_exec_control = 0;
924 u32 _vmexit_control = 0;
925 u32 _vmentry_control = 0;
926
927 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
928 opt = 0;
929 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
930 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300931 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300932
933 min = CPU_BASED_HLT_EXITING |
934#ifdef CONFIG_X86_64
935 CPU_BASED_CR8_LOAD_EXITING |
936 CPU_BASED_CR8_STORE_EXITING |
937#endif
938 CPU_BASED_USE_IO_BITMAPS |
939 CPU_BASED_MOV_DR_EXITING |
940 CPU_BASED_USE_TSC_OFFSETING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800941#ifdef CONFIG_X86_64
942 opt = CPU_BASED_TPR_SHADOW;
943#else
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300944 opt = 0;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800945#endif
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300946 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
947 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300948 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800949#ifdef CONFIG_X86_64
950 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
951 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
952 ~CPU_BASED_CR8_STORE_EXITING;
953#endif
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300954
955 min = 0;
956#ifdef CONFIG_X86_64
957 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
958#endif
959 opt = 0;
960 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
961 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300962 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300963
964 min = opt = 0;
965 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
966 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300967 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -0800969 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300970
971 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
972 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300973 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300974
975#ifdef CONFIG_X86_64
976 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
977 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +0300978 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300979#endif
980
981 /* Require Write-Back (WB) memory type for VMCS accesses. */
982 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300983 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300984
Yang, Sheng002c7f72007-07-31 14:23:01 +0300985 vmcs_conf->size = vmx_msr_high & 0x1fff;
986 vmcs_conf->order = get_order(vmcs_config.size);
987 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300988
Yang, Sheng002c7f72007-07-31 14:23:01 +0300989 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
990 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
991 vmcs_conf->vmexit_ctrl = _vmexit_control;
992 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300993
994 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -0800995}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996
997static struct vmcs *alloc_vmcs_cpu(int cpu)
998{
999 int node = cpu_to_node(cpu);
1000 struct page *pages;
1001 struct vmcs *vmcs;
1002
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001003 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004 if (!pages)
1005 return NULL;
1006 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001007 memset(vmcs, 0, vmcs_config.size);
1008 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009 return vmcs;
1010}
1011
1012static struct vmcs *alloc_vmcs(void)
1013{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001014 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015}
1016
1017static void free_vmcs(struct vmcs *vmcs)
1018{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001019 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020}
1021
Sam Ravnborg39959582007-06-01 00:47:13 -07001022static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023{
1024 int cpu;
1025
1026 for_each_online_cpu(cpu)
1027 free_vmcs(per_cpu(vmxarea, cpu));
1028}
1029
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030static __init int alloc_kvm_area(void)
1031{
1032 int cpu;
1033
1034 for_each_online_cpu(cpu) {
1035 struct vmcs *vmcs;
1036
1037 vmcs = alloc_vmcs_cpu(cpu);
1038 if (!vmcs) {
1039 free_kvm_area();
1040 return -ENOMEM;
1041 }
1042
1043 per_cpu(vmxarea, cpu) = vmcs;
1044 }
1045 return 0;
1046}
1047
1048static __init int hardware_setup(void)
1049{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001050 if (setup_vmcs_config(&vmcs_config) < 0)
1051 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001052 return alloc_kvm_area();
1053}
1054
1055static __exit void hardware_unsetup(void)
1056{
1057 free_kvm_area();
1058}
1059
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1061{
1062 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1063
Avi Kivity6af11b92007-03-19 13:18:10 +02001064 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065 vmcs_write16(sf->selector, save->selector);
1066 vmcs_writel(sf->base, save->base);
1067 vmcs_write32(sf->limit, save->limit);
1068 vmcs_write32(sf->ar_bytes, save->ar);
1069 } else {
1070 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1071 << AR_DPL_SHIFT;
1072 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1073 }
1074}
1075
1076static void enter_pmode(struct kvm_vcpu *vcpu)
1077{
1078 unsigned long flags;
1079
1080 vcpu->rmode.active = 0;
1081
1082 vmcs_writel(GUEST_TR_BASE, vcpu->rmode.tr.base);
1083 vmcs_write32(GUEST_TR_LIMIT, vcpu->rmode.tr.limit);
1084 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->rmode.tr.ar);
1085
1086 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001087 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088 flags |= (vcpu->rmode.save_iopl << IOPL_SHIFT);
1089 vmcs_writel(GUEST_RFLAGS, flags);
1090
Rusty Russell66aee912007-07-17 23:34:16 +10001091 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1092 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093
1094 update_exception_bitmap(vcpu);
1095
1096 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->rmode.es);
1097 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->rmode.ds);
1098 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->rmode.gs);
1099 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->rmode.fs);
1100
1101 vmcs_write16(GUEST_SS_SELECTOR, 0);
1102 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1103
1104 vmcs_write16(GUEST_CS_SELECTOR,
1105 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1106 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1107}
1108
Izik Eidus33f5fa12007-08-19 22:24:58 +03001109static gva_t rmode_tss_base(struct kvm* kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110{
1111 gfn_t base_gfn = kvm->memslots[0].base_gfn + kvm->memslots[0].npages - 3;
1112 return base_gfn << PAGE_SHIFT;
1113}
1114
1115static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1116{
1117 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1118
1119 save->selector = vmcs_read16(sf->selector);
1120 save->base = vmcs_readl(sf->base);
1121 save->limit = vmcs_read32(sf->limit);
1122 save->ar = vmcs_read32(sf->ar_bytes);
1123 vmcs_write16(sf->selector, vmcs_readl(sf->base) >> 4);
1124 vmcs_write32(sf->limit, 0xffff);
1125 vmcs_write32(sf->ar_bytes, 0xf3);
1126}
1127
1128static void enter_rmode(struct kvm_vcpu *vcpu)
1129{
1130 unsigned long flags;
1131
1132 vcpu->rmode.active = 1;
1133
1134 vcpu->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
1135 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1136
1137 vcpu->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
1138 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1139
1140 vcpu->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
1141 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1142
1143 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001144 vcpu->rmode.save_iopl = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001146 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147
1148 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001149 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150 update_exception_bitmap(vcpu);
1151
1152 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1153 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1154 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1155
1156 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001157 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001158 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1159 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1161
1162 fix_rmode_seg(VCPU_SREG_ES, &vcpu->rmode.es);
1163 fix_rmode_seg(VCPU_SREG_DS, &vcpu->rmode.ds);
1164 fix_rmode_seg(VCPU_SREG_GS, &vcpu->rmode.gs);
1165 fix_rmode_seg(VCPU_SREG_FS, &vcpu->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001166
Eddie Dong8668a3c2007-10-10 14:26:45 +08001167 kvm_mmu_reset_context(vcpu);
Avi Kivity75880a02007-06-20 11:20:04 +03001168 init_rmode_tss(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001169}
1170
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001171#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001172
1173static void enter_lmode(struct kvm_vcpu *vcpu)
1174{
1175 u32 guest_tr_ar;
1176
1177 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1178 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1179 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
1180 __FUNCTION__);
1181 vmcs_write32(GUEST_TR_AR_BYTES,
1182 (guest_tr_ar & ~AR_TYPE_MASK)
1183 | AR_TYPE_BUSY_64_TSS);
1184 }
1185
1186 vcpu->shadow_efer |= EFER_LMA;
1187
Rusty Russell8b9cf982007-07-30 16:31:43 +10001188 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189 vmcs_write32(VM_ENTRY_CONTROLS,
1190 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001191 | VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001192}
1193
1194static void exit_lmode(struct kvm_vcpu *vcpu)
1195{
1196 vcpu->shadow_efer &= ~EFER_LMA;
1197
1198 vmcs_write32(VM_ENTRY_CONTROLS,
1199 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001200 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001201}
1202
1203#endif
1204
Anthony Liguori25c4c272007-04-27 09:29:21 +03001205static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001206{
Avi Kivity399badf2007-01-05 16:36:38 -08001207 vcpu->cr4 &= KVM_GUEST_CR4_MASK;
1208 vcpu->cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
1209}
1210
Avi Kivity6aa8b732006-12-10 02:21:36 -08001211static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1212{
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001213 vmx_fpu_deactivate(vcpu);
1214
Rusty Russell707d92fa2007-07-17 23:19:08 +10001215 if (vcpu->rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216 enter_pmode(vcpu);
1217
Rusty Russell707d92fa2007-07-17 23:19:08 +10001218 if (!vcpu->rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219 enter_rmode(vcpu);
1220
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001221#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222 if (vcpu->shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001223 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001224 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001225 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226 exit_lmode(vcpu);
1227 }
1228#endif
1229
1230 vmcs_writel(CR0_READ_SHADOW, cr0);
1231 vmcs_writel(GUEST_CR0,
1232 (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON);
1233 vcpu->cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001234
Rusty Russell707d92fa2007-07-17 23:19:08 +10001235 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001236 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001237}
1238
Avi Kivity6aa8b732006-12-10 02:21:36 -08001239static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1240{
1241 vmcs_writel(GUEST_CR3, cr3);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001242 if (vcpu->cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001243 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244}
1245
1246static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1247{
1248 vmcs_writel(CR4_READ_SHADOW, cr4);
1249 vmcs_writel(GUEST_CR4, cr4 | (vcpu->rmode.active ?
1250 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON));
1251 vcpu->cr4 = cr4;
1252}
1253
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001254#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255
1256static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1257{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001258 struct vcpu_vmx *vmx = to_vmx(vcpu);
1259 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260
1261 vcpu->shadow_efer = efer;
1262 if (efer & EFER_LMA) {
1263 vmcs_write32(VM_ENTRY_CONTROLS,
1264 vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001265 VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266 msr->data = efer;
1267
1268 } else {
1269 vmcs_write32(VM_ENTRY_CONTROLS,
1270 vmcs_read32(VM_ENTRY_CONTROLS) &
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001271 ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272
1273 msr->data = efer & ~EFER_LME;
1274 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001275 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276}
1277
1278#endif
1279
1280static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1281{
1282 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1283
1284 return vmcs_readl(sf->base);
1285}
1286
1287static void vmx_get_segment(struct kvm_vcpu *vcpu,
1288 struct kvm_segment *var, int seg)
1289{
1290 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1291 u32 ar;
1292
1293 var->base = vmcs_readl(sf->base);
1294 var->limit = vmcs_read32(sf->limit);
1295 var->selector = vmcs_read16(sf->selector);
1296 ar = vmcs_read32(sf->ar_bytes);
1297 if (ar & AR_UNUSABLE_MASK)
1298 ar = 0;
1299 var->type = ar & 15;
1300 var->s = (ar >> 4) & 1;
1301 var->dpl = (ar >> 5) & 3;
1302 var->present = (ar >> 7) & 1;
1303 var->avl = (ar >> 12) & 1;
1304 var->l = (ar >> 13) & 1;
1305 var->db = (ar >> 14) & 1;
1306 var->g = (ar >> 15) & 1;
1307 var->unusable = (ar >> 16) & 1;
1308}
1309
Avi Kivity653e3102007-05-07 10:55:37 +03001310static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001311{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001312 u32 ar;
1313
Avi Kivity653e3102007-05-07 10:55:37 +03001314 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001315 ar = 1 << 16;
1316 else {
1317 ar = var->type & 15;
1318 ar |= (var->s & 1) << 4;
1319 ar |= (var->dpl & 3) << 5;
1320 ar |= (var->present & 1) << 7;
1321 ar |= (var->avl & 1) << 12;
1322 ar |= (var->l & 1) << 13;
1323 ar |= (var->db & 1) << 14;
1324 ar |= (var->g & 1) << 15;
1325 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001326 if (ar == 0) /* a 0 value means unusable */
1327 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001328
1329 return ar;
1330}
1331
1332static void vmx_set_segment(struct kvm_vcpu *vcpu,
1333 struct kvm_segment *var, int seg)
1334{
1335 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1336 u32 ar;
1337
1338 if (vcpu->rmode.active && seg == VCPU_SREG_TR) {
1339 vcpu->rmode.tr.selector = var->selector;
1340 vcpu->rmode.tr.base = var->base;
1341 vcpu->rmode.tr.limit = var->limit;
1342 vcpu->rmode.tr.ar = vmx_segment_access_rights(var);
1343 return;
1344 }
1345 vmcs_writel(sf->base, var->base);
1346 vmcs_write32(sf->limit, var->limit);
1347 vmcs_write16(sf->selector, var->selector);
1348 if (vcpu->rmode.active && var->s) {
1349 /*
1350 * Hack real-mode segments into vm86 compatibility.
1351 */
1352 if (var->base == 0xffff0000 && var->selector == 0xf000)
1353 vmcs_writel(sf->base, 0xf0000);
1354 ar = 0xf3;
1355 } else
1356 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357 vmcs_write32(sf->ar_bytes, ar);
1358}
1359
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1361{
1362 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1363
1364 *db = (ar >> 14) & 1;
1365 *l = (ar >> 13) & 1;
1366}
1367
1368static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1369{
1370 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1371 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1372}
1373
1374static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1375{
1376 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1377 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1378}
1379
1380static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1381{
1382 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1383 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1384}
1385
1386static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1387{
1388 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1389 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1390}
1391
1392static int init_rmode_tss(struct kvm* kvm)
1393{
1394 struct page *p1, *p2, *p3;
1395 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
1396 char *page;
1397
Avi Kivity954bbbc2007-03-30 14:02:32 +03001398 p1 = gfn_to_page(kvm, fn++);
1399 p2 = gfn_to_page(kvm, fn++);
1400 p3 = gfn_to_page(kvm, fn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401
1402 if (!p1 || !p2 || !p3) {
1403 kvm_printf(kvm,"%s: gfn_to_page failed\n", __FUNCTION__);
1404 return 0;
1405 }
1406
1407 page = kmap_atomic(p1, KM_USER0);
Shani Moideena3870c42007-06-11 09:31:33 +05301408 clear_page(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409 *(u16*)(page + 0x66) = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
1410 kunmap_atomic(page, KM_USER0);
1411
1412 page = kmap_atomic(p2, KM_USER0);
Shani Moideena3870c42007-06-11 09:31:33 +05301413 clear_page(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001414 kunmap_atomic(page, KM_USER0);
1415
1416 page = kmap_atomic(p3, KM_USER0);
Shani Moideena3870c42007-06-11 09:31:33 +05301417 clear_page(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418 *(page + RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1) = ~0;
1419 kunmap_atomic(page, KM_USER0);
1420
1421 return 1;
1422}
1423
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424static void seg_setup(int seg)
1425{
1426 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1427
1428 vmcs_write16(sf->selector, 0);
1429 vmcs_writel(sf->base, 0);
1430 vmcs_write32(sf->limit, 0xffff);
1431 vmcs_write32(sf->ar_bytes, 0x93);
1432}
1433
1434/*
1435 * Sets up the vmcs for emulated real mode.
1436 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001437static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001438{
1439 u32 host_sysenter_cs;
1440 u32 junk;
1441 unsigned long a;
1442 struct descriptor_table dt;
1443 int i;
1444 int ret = 0;
Avi Kivitycd2276a2007-05-14 20:41:13 +03001445 unsigned long kvm_vmx_return;
Eddie Dong7017fc32007-07-18 11:34:57 +03001446 u64 msr;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001447 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001448
Rusty Russell8b9cf982007-07-30 16:31:43 +10001449 if (!init_rmode_tss(vmx->vcpu.kvm)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450 ret = -ENOMEM;
1451 goto out;
1452 }
1453
He, Qingc5ec1532007-09-03 17:07:41 +03001454 vmx->vcpu.rmode.active = 0;
1455
Rusty Russell8b9cf982007-07-30 16:31:43 +10001456 vmx->vcpu.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Eddie Dong7017fc32007-07-18 11:34:57 +03001457 set_cr8(&vmx->vcpu, 0);
1458 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Rusty Russell8b9cf982007-07-30 16:31:43 +10001459 if (vmx->vcpu.vcpu_id == 0)
Eddie Dong7017fc32007-07-18 11:34:57 +03001460 msr |= MSR_IA32_APICBASE_BSP;
1461 kvm_set_apic_base(&vmx->vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462
Rusty Russell8b9cf982007-07-30 16:31:43 +10001463 fx_init(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001464
1465 /*
1466 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
1467 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
1468 */
He, Qingc5ec1532007-09-03 17:07:41 +03001469 if (vmx->vcpu.vcpu_id == 0) {
1470 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
1471 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
1472 } else {
1473 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.sipi_vector << 8);
1474 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.sipi_vector << 12);
1475 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
1477 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1478
1479 seg_setup(VCPU_SREG_DS);
1480 seg_setup(VCPU_SREG_ES);
1481 seg_setup(VCPU_SREG_FS);
1482 seg_setup(VCPU_SREG_GS);
1483 seg_setup(VCPU_SREG_SS);
1484
1485 vmcs_write16(GUEST_TR_SELECTOR, 0);
1486 vmcs_writel(GUEST_TR_BASE, 0);
1487 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
1488 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1489
1490 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
1491 vmcs_writel(GUEST_LDTR_BASE, 0);
1492 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
1493 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
1494
1495 vmcs_write32(GUEST_SYSENTER_CS, 0);
1496 vmcs_writel(GUEST_SYSENTER_ESP, 0);
1497 vmcs_writel(GUEST_SYSENTER_EIP, 0);
1498
1499 vmcs_writel(GUEST_RFLAGS, 0x02);
He, Qingc5ec1532007-09-03 17:07:41 +03001500 if (vmx->vcpu.vcpu_id == 0)
1501 vmcs_writel(GUEST_RIP, 0xfff0);
1502 else
1503 vmcs_writel(GUEST_RIP, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504 vmcs_writel(GUEST_RSP, 0);
1505
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506 //todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0
1507 vmcs_writel(GUEST_DR7, 0x400);
1508
1509 vmcs_writel(GUEST_GDTR_BASE, 0);
1510 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
1511
1512 vmcs_writel(GUEST_IDTR_BASE, 0);
1513 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
1514
1515 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
1516 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
1517 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
1518
1519 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03001520 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
1521 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522
1523 guest_write_tsc(0);
1524
1525 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
1526
1527 /* Special registers */
1528 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
1529
1530 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001531 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
1532 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001533
1534 exec_control = vmcs_config.cpu_based_exec_ctrl;
1535 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
1536 exec_control &= ~CPU_BASED_TPR_SHADOW;
1537#ifdef CONFIG_X86_64
1538 exec_control |= CPU_BASED_CR8_STORE_EXITING |
1539 CPU_BASED_CR8_LOAD_EXITING;
1540#endif
1541 }
1542 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001543
Avi Kivityc7addb92007-09-16 18:58:32 +02001544 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
1545 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001546 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
1547
1548 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
1549 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
1550 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1551
1552 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
1553 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1554 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1555 vmcs_write16(HOST_FS_SELECTOR, read_fs()); /* 22.2.4 */
1556 vmcs_write16(HOST_GS_SELECTOR, read_gs()); /* 22.2.4 */
1557 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001558#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001559 rdmsrl(MSR_FS_BASE, a);
1560 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
1561 rdmsrl(MSR_GS_BASE, a);
1562 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
1563#else
1564 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
1565 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
1566#endif
1567
1568 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
1569
1570 get_idt(&dt);
1571 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
1572
Avi Kivitycd2276a2007-05-14 20:41:13 +03001573 asm ("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
1574 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03001575 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
1576 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
1577 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001578
1579 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
1580 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
1581 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
1582 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
1583 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
1584 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
1585
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586 for (i = 0; i < NR_VMX_MSR; ++i) {
1587 u32 index = vmx_msr_index[i];
1588 u32 data_low, data_high;
1589 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001590 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001591
1592 if (rdmsr_safe(index, &data_low, &data_high) < 0)
1593 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08001594 if (wrmsr_safe(index, data_low, data_high) < 0)
1595 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001597 vmx->host_msrs[j].index = index;
1598 vmx->host_msrs[j].reserved = 0;
1599 vmx->host_msrs[j].data = data;
1600 vmx->guest_msrs[j] = vmx->host_msrs[j];
1601 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603
Rusty Russell8b9cf982007-07-30 16:31:43 +10001604 setup_msrs(vmx);
Avi Kivitye38aea32007-04-19 13:22:48 +03001605
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001606 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607
1608 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001609 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
1610
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
1612
Michael Riepe3b99ab22006-12-13 00:34:15 -08001613#ifdef CONFIG_X86_64
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001614 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
1615 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
1616 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
1617 page_to_phys(vmx->vcpu.apic->regs_page));
1618 vmcs_write32(TPR_THRESHOLD, 0);
Michael Riepe3b99ab22006-12-13 00:34:15 -08001619#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001620
Anthony Liguori25c4c272007-04-27 09:29:21 +03001621 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
1623
Rusty Russell8b9cf982007-07-30 16:31:43 +10001624 vmx->vcpu.cr0 = 0x60000010;
1625 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.cr0); // enter rmode
1626 vmx_set_cr4(&vmx->vcpu, 0);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001627#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001628 vmx_set_efer(&vmx->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +10001630 vmx_fpu_activate(&vmx->vcpu);
1631 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632
1633 return 0;
1634
Avi Kivity6aa8b732006-12-10 02:21:36 -08001635out:
1636 return ret;
1637}
1638
Avi Kivity04d2cc72007-09-10 18:10:54 +03001639static void vmx_vcpu_reset(struct kvm_vcpu *vcpu)
1640{
1641 struct vcpu_vmx *vmx = to_vmx(vcpu);
1642
1643 vmx_vcpu_setup(vmx);
1644}
1645
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646static void inject_rmode_irq(struct kvm_vcpu *vcpu, int irq)
1647{
1648 u16 ent[2];
1649 u16 cs;
1650 u16 ip;
1651 unsigned long flags;
1652 unsigned long ss_base = vmcs_readl(GUEST_SS_BASE);
1653 u16 sp = vmcs_readl(GUEST_RSP);
1654 u32 ss_limit = vmcs_read32(GUEST_SS_LIMIT);
1655
Eric Sesterhenn / Snakebyte39649942007-04-09 16:15:05 +02001656 if (sp > ss_limit || sp < 6 ) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657 vcpu_printf(vcpu, "%s: #SS, rsp 0x%lx ss 0x%lx limit 0x%x\n",
1658 __FUNCTION__,
1659 vmcs_readl(GUEST_RSP),
1660 vmcs_readl(GUEST_SS_BASE),
1661 vmcs_read32(GUEST_SS_LIMIT));
1662 return;
1663 }
1664
Laurent Viviere7d5d762007-07-30 13:41:19 +03001665 if (emulator_read_std(irq * sizeof(ent), &ent, sizeof(ent), vcpu) !=
1666 X86EMUL_CONTINUE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667 vcpu_printf(vcpu, "%s: read guest err\n", __FUNCTION__);
1668 return;
1669 }
1670
1671 flags = vmcs_readl(GUEST_RFLAGS);
1672 cs = vmcs_readl(GUEST_CS_BASE) >> 4;
1673 ip = vmcs_readl(GUEST_RIP);
1674
1675
Laurent Viviere7d5d762007-07-30 13:41:19 +03001676 if (emulator_write_emulated(ss_base + sp - 2, &flags, 2, vcpu) != X86EMUL_CONTINUE ||
1677 emulator_write_emulated(ss_base + sp - 4, &cs, 2, vcpu) != X86EMUL_CONTINUE ||
1678 emulator_write_emulated(ss_base + sp - 6, &ip, 2, vcpu) != X86EMUL_CONTINUE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679 vcpu_printf(vcpu, "%s: write guest err\n", __FUNCTION__);
1680 return;
1681 }
1682
1683 vmcs_writel(GUEST_RFLAGS, flags &
1684 ~( X86_EFLAGS_IF | X86_EFLAGS_AC | X86_EFLAGS_TF));
1685 vmcs_write16(GUEST_CS_SELECTOR, ent[1]) ;
1686 vmcs_writel(GUEST_CS_BASE, ent[1] << 4);
1687 vmcs_writel(GUEST_RIP, ent[0]);
1688 vmcs_writel(GUEST_RSP, (vmcs_readl(GUEST_RSP) & ~0xffff) | (sp - 6));
1689}
1690
Eddie Dong85f455f2007-07-06 12:20:49 +03001691static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
1692{
1693 if (vcpu->rmode.active) {
1694 inject_rmode_irq(vcpu, irq);
1695 return;
1696 }
1697 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
1698 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1699}
1700
Avi Kivity6aa8b732006-12-10 02:21:36 -08001701static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
1702{
1703 int word_index = __ffs(vcpu->irq_summary);
1704 int bit_index = __ffs(vcpu->irq_pending[word_index]);
1705 int irq = word_index * BITS_PER_LONG + bit_index;
1706
1707 clear_bit(bit_index, &vcpu->irq_pending[word_index]);
1708 if (!vcpu->irq_pending[word_index])
1709 clear_bit(word_index, &vcpu->irq_summary);
Eddie Dong85f455f2007-07-06 12:20:49 +03001710 vmx_inject_irq(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711}
1712
Dor Laorc1150d82007-01-05 16:36:24 -08001713
1714static void do_interrupt_requests(struct kvm_vcpu *vcpu,
1715 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001716{
Dor Laorc1150d82007-01-05 16:36:24 -08001717 u32 cpu_based_vm_exec_control;
1718
1719 vcpu->interrupt_window_open =
1720 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
1721 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
1722
1723 if (vcpu->interrupt_window_open &&
1724 vcpu->irq_summary &&
1725 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726 /*
Dor Laorc1150d82007-01-05 16:36:24 -08001727 * If interrupts enabled, and not blocked by sti or mov ss. Good.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001728 */
1729 kvm_do_inject_irq(vcpu);
Dor Laorc1150d82007-01-05 16:36:24 -08001730
1731 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
1732 if (!vcpu->interrupt_window_open &&
1733 (vcpu->irq_summary || kvm_run->request_interrupt_window))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001734 /*
1735 * Interrupts blocked. Wait for unblock.
1736 */
Dor Laorc1150d82007-01-05 16:36:24 -08001737 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
1738 else
1739 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
1740 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001741}
1742
1743static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
1744{
1745 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
1746
1747 set_debugreg(dbg->bp[0], 0);
1748 set_debugreg(dbg->bp[1], 1);
1749 set_debugreg(dbg->bp[2], 2);
1750 set_debugreg(dbg->bp[3], 3);
1751
1752 if (dbg->singlestep) {
1753 unsigned long flags;
1754
1755 flags = vmcs_readl(GUEST_RFLAGS);
1756 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1757 vmcs_writel(GUEST_RFLAGS, flags);
1758 }
1759}
1760
1761static int handle_rmode_exception(struct kvm_vcpu *vcpu,
1762 int vec, u32 err_code)
1763{
1764 if (!vcpu->rmode.active)
1765 return 0;
1766
Nitin A Kambleb3f37702007-05-17 15:50:34 +03001767 /*
1768 * Instruction with address size override prefix opcode 0x67
1769 * Cause the #SS fault with 0 error code in VM86 mode.
1770 */
1771 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02001772 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001773 return 1;
1774 return 0;
1775}
1776
1777static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1778{
1779 u32 intr_info, error_code;
1780 unsigned long cr2, rip;
1781 u32 vect_info;
1782 enum emulation_result er;
Avi Kivitye2dec932007-01-05 16:36:54 -08001783 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001784
1785 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
1786 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
1787
1788 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
1789 !is_page_fault(intr_info)) {
1790 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
1791 "intr info 0x%x\n", __FUNCTION__, vect_info, intr_info);
1792 }
1793
Eddie Dong85f455f2007-07-06 12:20:49 +03001794 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001795 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
1796 set_bit(irq, vcpu->irq_pending);
1797 set_bit(irq / BITS_PER_LONG, &vcpu->irq_summary);
1798 }
1799
Avi Kivity1b6269d2007-10-09 12:12:19 +02001800 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
1801 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001802
1803 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001804 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001805 return 1;
1806 }
1807
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001808 if (is_invalid_opcode(intr_info)) {
Laurent Vivier34273182007-09-18 11:27:37 +02001809 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001810 if (er != EMULATE_DONE)
1811 vmx_inject_ud(vcpu);
1812
1813 return 1;
1814 }
1815
Avi Kivity6aa8b732006-12-10 02:21:36 -08001816 error_code = 0;
1817 rip = vmcs_readl(GUEST_RIP);
1818 if (intr_info & INTR_INFO_DELIEVER_CODE_MASK)
1819 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
1820 if (is_page_fault(intr_info)) {
1821 cr2 = vmcs_readl(EXIT_QUALIFICATION);
1822
Shaohua Li11ec2802007-07-23 14:51:37 +08001823 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001824 r = kvm_mmu_page_fault(vcpu, cr2, error_code);
1825 if (r < 0) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001826 mutex_unlock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001827 return r;
1828 }
1829 if (!r) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001830 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831 return 1;
1832 }
1833
Laurent Vivier34273182007-09-18 11:27:37 +02001834 er = emulate_instruction(vcpu, kvm_run, cr2, error_code, 0);
Shaohua Li11ec2802007-07-23 14:51:37 +08001835 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836
1837 switch (er) {
1838 case EMULATE_DONE:
1839 return 1;
1840 case EMULATE_DO_MMIO:
Avi Kivity1165f5f2007-04-19 17:27:43 +03001841 ++vcpu->stat.mmio_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001842 return 0;
1843 case EMULATE_FAIL:
Avi Kivity054b1362007-09-12 13:21:09 +03001844 kvm_report_emulation_failure(vcpu, "pagetable");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845 break;
1846 default:
1847 BUG();
1848 }
1849 }
1850
1851 if (vcpu->rmode.active &&
1852 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03001853 error_code)) {
1854 if (vcpu->halt_request) {
1855 vcpu->halt_request = 0;
1856 return kvm_emulate_halt(vcpu);
1857 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03001859 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001860
1861 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) == (INTR_TYPE_EXCEPTION | 1)) {
1862 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1863 return 0;
1864 }
1865 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
1866 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
1867 kvm_run->ex.error_code = error_code;
1868 return 0;
1869}
1870
1871static int handle_external_interrupt(struct kvm_vcpu *vcpu,
1872 struct kvm_run *kvm_run)
1873{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001874 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001875 return 1;
1876}
1877
Avi Kivity988ad742007-02-12 00:54:36 -08001878static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1879{
1880 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1881 return 0;
1882}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001883
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1885{
He, Qingbfdaab02007-09-12 14:18:28 +08001886 unsigned long exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02001887 int size, down, in, string, rep;
1888 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889
Avi Kivity1165f5f2007-04-19 17:27:43 +03001890 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08001891 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02001892 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001893
1894 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02001895 if (emulate_instruction(vcpu,
1896 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03001897 return 0;
1898 return 1;
1899 }
1900
1901 size = (exit_qualification & 7) + 1;
1902 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001903 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001904 rep = (exit_qualification & 32) != 0;
1905 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03001906
Laurent Vivier3090dd72007-08-05 10:43:32 +03001907 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001908}
1909
Ingo Molnar102d8322007-02-19 14:37:47 +02001910static void
1911vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
1912{
1913 /*
1914 * Patch in the VMCALL instruction:
1915 */
1916 hypercall[0] = 0x0f;
1917 hypercall[1] = 0x01;
1918 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02001919}
1920
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1922{
He, Qingbfdaab02007-09-12 14:18:28 +08001923 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001924 int cr;
1925 int reg;
1926
He, Qingbfdaab02007-09-12 14:18:28 +08001927 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001928 cr = exit_qualification & 15;
1929 reg = (exit_qualification >> 8) & 15;
1930 switch ((exit_qualification >> 4) & 3) {
1931 case 0: /* mov to cr */
1932 switch (cr) {
1933 case 0:
1934 vcpu_load_rsp_rip(vcpu);
1935 set_cr0(vcpu, vcpu->regs[reg]);
1936 skip_emulated_instruction(vcpu);
1937 return 1;
1938 case 3:
1939 vcpu_load_rsp_rip(vcpu);
1940 set_cr3(vcpu, vcpu->regs[reg]);
1941 skip_emulated_instruction(vcpu);
1942 return 1;
1943 case 4:
1944 vcpu_load_rsp_rip(vcpu);
1945 set_cr4(vcpu, vcpu->regs[reg]);
1946 skip_emulated_instruction(vcpu);
1947 return 1;
1948 case 8:
1949 vcpu_load_rsp_rip(vcpu);
1950 set_cr8(vcpu, vcpu->regs[reg]);
1951 skip_emulated_instruction(vcpu);
Yang, Sheng253abde2007-08-16 13:01:00 +03001952 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
1953 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954 };
1955 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03001956 case 2: /* clts */
1957 vcpu_load_rsp_rip(vcpu);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001958 vmx_fpu_deactivate(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001959 vcpu->cr0 &= ~X86_CR0_TS;
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001960 vmcs_writel(CR0_READ_SHADOW, vcpu->cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001961 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03001962 skip_emulated_instruction(vcpu);
1963 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001964 case 1: /*mov from cr*/
1965 switch (cr) {
1966 case 3:
1967 vcpu_load_rsp_rip(vcpu);
1968 vcpu->regs[reg] = vcpu->cr3;
1969 vcpu_put_rsp_rip(vcpu);
1970 skip_emulated_instruction(vcpu);
1971 return 1;
1972 case 8:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973 vcpu_load_rsp_rip(vcpu);
Eddie Dong7017fc32007-07-18 11:34:57 +03001974 vcpu->regs[reg] = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001975 vcpu_put_rsp_rip(vcpu);
1976 skip_emulated_instruction(vcpu);
1977 return 1;
1978 }
1979 break;
1980 case 3: /* lmsw */
1981 lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
1982
1983 skip_emulated_instruction(vcpu);
1984 return 1;
1985 default:
1986 break;
1987 }
1988 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10001989 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08001990 (int)(exit_qualification >> 4) & 3, cr);
1991 return 0;
1992}
1993
1994static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1995{
He, Qingbfdaab02007-09-12 14:18:28 +08001996 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001997 unsigned long val;
1998 int dr, reg;
1999
2000 /*
2001 * FIXME: this code assumes the host is debugging the guest.
2002 * need to deal with guest debugging itself too.
2003 */
He, Qingbfdaab02007-09-12 14:18:28 +08002004 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002005 dr = exit_qualification & 7;
2006 reg = (exit_qualification >> 8) & 15;
2007 vcpu_load_rsp_rip(vcpu);
2008 if (exit_qualification & 16) {
2009 /* mov from dr */
2010 switch (dr) {
2011 case 6:
2012 val = 0xffff0ff0;
2013 break;
2014 case 7:
2015 val = 0x400;
2016 break;
2017 default:
2018 val = 0;
2019 }
2020 vcpu->regs[reg] = val;
2021 } else {
2022 /* mov to dr */
2023 }
2024 vcpu_put_rsp_rip(vcpu);
2025 skip_emulated_instruction(vcpu);
2026 return 1;
2027}
2028
2029static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2030{
Avi Kivity06465c52007-02-28 20:46:53 +02002031 kvm_emulate_cpuid(vcpu);
2032 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002033}
2034
2035static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2036{
2037 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
2038 u64 data;
2039
2040 if (vmx_get_msr(vcpu, ecx, &data)) {
2041 vmx_inject_gp(vcpu, 0);
2042 return 1;
2043 }
2044
2045 /* FIXME: handling of bits 32:63 of rax, rdx */
2046 vcpu->regs[VCPU_REGS_RAX] = data & -1u;
2047 vcpu->regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
2048 skip_emulated_instruction(vcpu);
2049 return 1;
2050}
2051
2052static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2053{
2054 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
2055 u64 data = (vcpu->regs[VCPU_REGS_RAX] & -1u)
2056 | ((u64)(vcpu->regs[VCPU_REGS_RDX] & -1u) << 32);
2057
2058 if (vmx_set_msr(vcpu, ecx, data) != 0) {
2059 vmx_inject_gp(vcpu, 0);
2060 return 1;
2061 }
2062
2063 skip_emulated_instruction(vcpu);
2064 return 1;
2065}
2066
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002067static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2068 struct kvm_run *kvm_run)
2069{
2070 return 1;
2071}
2072
Avi Kivity6aa8b732006-12-10 02:21:36 -08002073static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2074 struct kvm_run *kvm_run)
2075{
Eddie Dong85f455f2007-07-06 12:20:49 +03002076 u32 cpu_based_vm_exec_control;
2077
2078 /* clear pending irq */
2079 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2080 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2081 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Dor Laorc1150d82007-01-05 16:36:24 -08002082 /*
2083 * If the user space waits to inject interrupts, exit as soon as
2084 * possible
2085 */
2086 if (kvm_run->request_interrupt_window &&
Dor Laor022a9302007-01-05 16:37:00 -08002087 !vcpu->irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002088 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002089 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002090 return 0;
2091 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002092 return 1;
2093}
2094
2095static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2096{
2097 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002098 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099}
2100
Ingo Molnarc21415e2007-02-19 14:37:47 +02002101static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2102{
Dor Laor510043d2007-02-19 18:25:43 +02002103 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002104 kvm_emulate_hypercall(vcpu);
2105 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02002106}
2107
Avi Kivity6aa8b732006-12-10 02:21:36 -08002108/*
2109 * The exit handlers return 1 if the exit was handled fully and guest execution
2110 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2111 * to be done to userspace and return 0.
2112 */
2113static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
2114 struct kvm_run *kvm_run) = {
2115 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
2116 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08002117 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002118 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119 [EXIT_REASON_CR_ACCESS] = handle_cr,
2120 [EXIT_REASON_DR_ACCESS] = handle_dr,
2121 [EXIT_REASON_CPUID] = handle_cpuid,
2122 [EXIT_REASON_MSR_READ] = handle_rdmsr,
2123 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
2124 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
2125 [EXIT_REASON_HLT] = handle_halt,
Ingo Molnarc21415e2007-02-19 14:37:47 +02002126 [EXIT_REASON_VMCALL] = handle_vmcall,
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002127 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold
Avi Kivity6aa8b732006-12-10 02:21:36 -08002128};
2129
2130static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04002131 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002132
2133/*
2134 * The guest has exited. See if we can fix it or if we need userspace
2135 * assistance.
2136 */
2137static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2138{
2139 u32 vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2140 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity29bd8a72007-09-10 17:27:03 +03002141 struct vcpu_vmx *vmx = to_vmx(vcpu);
2142
2143 if (unlikely(vmx->fail)) {
2144 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2145 kvm_run->fail_entry.hardware_entry_failure_reason
2146 = vmcs_read32(VM_INSTRUCTION_ERROR);
2147 return 0;
2148 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002149
2150 if ( (vectoring_info & VECTORING_INFO_VALID_MASK) &&
2151 exit_reason != EXIT_REASON_EXCEPTION_NMI )
2152 printk(KERN_WARNING "%s: unexpected, valid vectoring info and "
2153 "exit reason is 0x%x\n", __FUNCTION__, exit_reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002154 if (exit_reason < kvm_vmx_max_exit_handlers
2155 && kvm_vmx_exit_handlers[exit_reason])
2156 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
2157 else {
2158 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2159 kvm_run->hw.hardware_exit_reason = exit_reason;
2160 }
2161 return 0;
2162}
2163
Avi Kivityd9e368d2007-06-07 19:18:30 +03002164static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
2165{
Avi Kivityd9e368d2007-06-07 19:18:30 +03002166}
2167
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002168static void update_tpr_threshold(struct kvm_vcpu *vcpu)
2169{
2170 int max_irr, tpr;
2171
2172 if (!vm_need_tpr_shadow(vcpu->kvm))
2173 return;
2174
2175 if (!kvm_lapic_enabled(vcpu) ||
2176 ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
2177 vmcs_write32(TPR_THRESHOLD, 0);
2178 return;
2179 }
2180
2181 tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
2182 vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
2183}
2184
Eddie Dong85f455f2007-07-06 12:20:49 +03002185static void enable_irq_window(struct kvm_vcpu *vcpu)
2186{
2187 u32 cpu_based_vm_exec_control;
2188
2189 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2190 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2191 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2192}
2193
2194static void vmx_intr_assist(struct kvm_vcpu *vcpu)
2195{
2196 u32 idtv_info_field, intr_info_field;
2197 int has_ext_irq, interrupt_window_open;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002198 int vector;
Eddie Dong85f455f2007-07-06 12:20:49 +03002199
Eddie Dong1b9778d2007-09-03 16:56:58 +03002200 kvm_inject_pending_timer_irqs(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002201 update_tpr_threshold(vcpu);
2202
Eddie Dong85f455f2007-07-06 12:20:49 +03002203 has_ext_irq = kvm_cpu_has_interrupt(vcpu);
2204 intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
2205 idtv_info_field = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2206 if (intr_info_field & INTR_INFO_VALID_MASK) {
2207 if (idtv_info_field & INTR_INFO_VALID_MASK) {
2208 /* TODO: fault when IDT_Vectoring */
2209 printk(KERN_ERR "Fault when IDT_Vectoring\n");
2210 }
2211 if (has_ext_irq)
2212 enable_irq_window(vcpu);
2213 return;
2214 }
2215 if (unlikely(idtv_info_field & INTR_INFO_VALID_MASK)) {
2216 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, idtv_info_field);
2217 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2218 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
2219
2220 if (unlikely(idtv_info_field & INTR_INFO_DELIEVER_CODE_MASK))
2221 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2222 vmcs_read32(IDT_VECTORING_ERROR_CODE));
2223 if (unlikely(has_ext_irq))
2224 enable_irq_window(vcpu);
2225 return;
2226 }
2227 if (!has_ext_irq)
2228 return;
2229 interrupt_window_open =
2230 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2231 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002232 if (interrupt_window_open) {
2233 vector = kvm_cpu_get_interrupt(vcpu);
2234 vmx_inject_irq(vcpu, vector);
2235 kvm_timer_intr_post(vcpu, vector);
2236 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03002237 enable_irq_window(vcpu);
2238}
2239
Avi Kivity04d2cc72007-09-10 18:10:54 +03002240static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002242 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1b6269d2007-10-09 12:12:19 +02002243 u32 intr_info;
Avi Kivitye6adf282007-04-30 16:07:54 +03002244
2245 /*
2246 * Loading guest fpu may have cleared host cr0.ts
2247 */
2248 vmcs_writel(HOST_CR0, read_cr0());
2249
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250 asm (
2251 /* Store host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002252#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253 "push %%rax; push %%rbx; push %%rdx;"
2254 "push %%rsi; push %%rdi; push %%rbp;"
2255 "push %%r8; push %%r9; push %%r10; push %%r11;"
2256 "push %%r12; push %%r13; push %%r14; push %%r15;"
2257 "push %%rcx \n\t"
2258 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
2259#else
2260 "pusha; push %%ecx \n\t"
2261 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
2262#endif
2263 /* Check if vmlaunch of vmresume is needed */
2264 "cmp $0, %1 \n\t"
2265 /* Load guest registers. Don't clobber flags. */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002266#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267 "mov %c[cr2](%3), %%rax \n\t"
2268 "mov %%rax, %%cr2 \n\t"
2269 "mov %c[rax](%3), %%rax \n\t"
2270 "mov %c[rbx](%3), %%rbx \n\t"
2271 "mov %c[rdx](%3), %%rdx \n\t"
2272 "mov %c[rsi](%3), %%rsi \n\t"
2273 "mov %c[rdi](%3), %%rdi \n\t"
2274 "mov %c[rbp](%3), %%rbp \n\t"
2275 "mov %c[r8](%3), %%r8 \n\t"
2276 "mov %c[r9](%3), %%r9 \n\t"
2277 "mov %c[r10](%3), %%r10 \n\t"
2278 "mov %c[r11](%3), %%r11 \n\t"
2279 "mov %c[r12](%3), %%r12 \n\t"
2280 "mov %c[r13](%3), %%r13 \n\t"
2281 "mov %c[r14](%3), %%r14 \n\t"
2282 "mov %c[r15](%3), %%r15 \n\t"
2283 "mov %c[rcx](%3), %%rcx \n\t" /* kills %3 (rcx) */
2284#else
2285 "mov %c[cr2](%3), %%eax \n\t"
2286 "mov %%eax, %%cr2 \n\t"
2287 "mov %c[rax](%3), %%eax \n\t"
2288 "mov %c[rbx](%3), %%ebx \n\t"
2289 "mov %c[rdx](%3), %%edx \n\t"
2290 "mov %c[rsi](%3), %%esi \n\t"
2291 "mov %c[rdi](%3), %%edi \n\t"
2292 "mov %c[rbp](%3), %%ebp \n\t"
2293 "mov %c[rcx](%3), %%ecx \n\t" /* kills %3 (ecx) */
2294#endif
2295 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03002296 "jne .Llaunched \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002297 ASM_VMX_VMLAUNCH "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03002298 "jmp .Lkvm_vmx_return \n\t"
2299 ".Llaunched: " ASM_VMX_VMRESUME "\n\t"
2300 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08002301 /* Save guest registers, load host registers, keep flags */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002302#ifdef CONFIG_X86_64
Ingo Molnar96958232007-02-12 00:54:33 -08002303 "xchg %3, (%%rsp) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304 "mov %%rax, %c[rax](%3) \n\t"
2305 "mov %%rbx, %c[rbx](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002306 "pushq (%%rsp); popq %c[rcx](%3) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002307 "mov %%rdx, %c[rdx](%3) \n\t"
2308 "mov %%rsi, %c[rsi](%3) \n\t"
2309 "mov %%rdi, %c[rdi](%3) \n\t"
2310 "mov %%rbp, %c[rbp](%3) \n\t"
2311 "mov %%r8, %c[r8](%3) \n\t"
2312 "mov %%r9, %c[r9](%3) \n\t"
2313 "mov %%r10, %c[r10](%3) \n\t"
2314 "mov %%r11, %c[r11](%3) \n\t"
2315 "mov %%r12, %c[r12](%3) \n\t"
2316 "mov %%r13, %c[r13](%3) \n\t"
2317 "mov %%r14, %c[r14](%3) \n\t"
2318 "mov %%r15, %c[r15](%3) \n\t"
2319 "mov %%cr2, %%rax \n\t"
2320 "mov %%rax, %c[cr2](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002321 "mov (%%rsp), %3 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322
2323 "pop %%rcx; pop %%r15; pop %%r14; pop %%r13; pop %%r12;"
2324 "pop %%r11; pop %%r10; pop %%r9; pop %%r8;"
2325 "pop %%rbp; pop %%rdi; pop %%rsi;"
2326 "pop %%rdx; pop %%rbx; pop %%rax \n\t"
2327#else
Ingo Molnar96958232007-02-12 00:54:33 -08002328 "xchg %3, (%%esp) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002329 "mov %%eax, %c[rax](%3) \n\t"
2330 "mov %%ebx, %c[rbx](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002331 "pushl (%%esp); popl %c[rcx](%3) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002332 "mov %%edx, %c[rdx](%3) \n\t"
2333 "mov %%esi, %c[rsi](%3) \n\t"
2334 "mov %%edi, %c[rdi](%3) \n\t"
2335 "mov %%ebp, %c[rbp](%3) \n\t"
2336 "mov %%cr2, %%eax \n\t"
2337 "mov %%eax, %c[cr2](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002338 "mov (%%esp), %3 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339
2340 "pop %%ecx; popa \n\t"
2341#endif
2342 "setbe %0 \n\t"
Avi Kivity29bd8a72007-09-10 17:27:03 +03002343 : "=q" (vmx->fail)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002344 : "r"(vmx->launched), "d"((unsigned long)HOST_RSP),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002345 "c"(vcpu),
2346 [rax]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RAX])),
2347 [rbx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBX])),
2348 [rcx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RCX])),
2349 [rdx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDX])),
2350 [rsi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RSI])),
2351 [rdi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDI])),
2352 [rbp]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002353#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002354 [r8 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R8 ])),
2355 [r9 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R9 ])),
2356 [r10]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R10])),
2357 [r11]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R11])),
2358 [r12]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R12])),
2359 [r13]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R13])),
2360 [r14]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R14])),
2361 [r15]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R15])),
2362#endif
2363 [cr2]"i"(offsetof(struct kvm_vcpu, cr2))
2364 : "cc", "memory" );
2365
Dor Laorc1150d82007-01-05 16:36:24 -08002366 vcpu->interrupt_window_open = (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368 asm ("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03002369 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02002370
2371 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2372
2373 /* We need to handle NMIs before interrupts are enabled */
2374 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
2375 asm("int $2");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376}
2377
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378static void vmx_inject_page_fault(struct kvm_vcpu *vcpu,
2379 unsigned long addr,
2380 u32 err_code)
2381{
2382 u32 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2383
Avi Kivity1165f5f2007-04-19 17:27:43 +03002384 ++vcpu->stat.pf_guest;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385
2386 if (is_page_fault(vect_info)) {
2387 printk(KERN_DEBUG "inject_page_fault: "
2388 "double fault 0x%lx @ 0x%lx\n",
2389 addr, vmcs_readl(GUEST_RIP));
2390 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
2391 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2392 DF_VECTOR |
2393 INTR_TYPE_EXCEPTION |
2394 INTR_INFO_DELIEVER_CODE_MASK |
2395 INTR_INFO_VALID_MASK);
2396 return;
2397 }
2398 vcpu->cr2 = addr;
2399 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, err_code);
2400 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2401 PF_VECTOR |
2402 INTR_TYPE_EXCEPTION |
2403 INTR_INFO_DELIEVER_CODE_MASK |
2404 INTR_INFO_VALID_MASK);
2405
2406}
2407
2408static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
2409{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002410 struct vcpu_vmx *vmx = to_vmx(vcpu);
2411
2412 if (vmx->vmcs) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002413 on_each_cpu(__vcpu_clear, vmx, 0, 1);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002414 free_vmcs(vmx->vmcs);
2415 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002416 }
2417}
2418
2419static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
2420{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002421 struct vcpu_vmx *vmx = to_vmx(vcpu);
2422
Avi Kivity6aa8b732006-12-10 02:21:36 -08002423 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002424 kfree(vmx->host_msrs);
2425 kfree(vmx->guest_msrs);
2426 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10002427 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428}
2429
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002430static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002432 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10002433 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03002434 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002436 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002437 return ERR_PTR(-ENOMEM);
2438
2439 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
2440 if (err)
2441 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002442
Eddie Dong97222cc2007-09-12 10:58:04 +03002443 if (irqchip_in_kernel(kvm)) {
2444 err = kvm_create_lapic(&vmx->vcpu);
2445 if (err < 0)
2446 goto free_vcpu;
2447 }
2448
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002449 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002450 if (!vmx->guest_msrs) {
2451 err = -ENOMEM;
2452 goto uninit_vcpu;
2453 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08002454
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002455 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
2456 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002457 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002458
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002459 vmx->vmcs = alloc_vmcs();
2460 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002461 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002462
2463 vmcs_clear(vmx->vmcs);
2464
Avi Kivity15ad7142007-07-11 18:17:21 +03002465 cpu = get_cpu();
2466 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002467 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002468 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002469 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002470 if (err)
2471 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002472
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002473 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002474
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002475free_vmcs:
2476 free_vmcs(vmx->vmcs);
2477free_msrs:
2478 kfree(vmx->host_msrs);
2479free_guest_msrs:
2480 kfree(vmx->guest_msrs);
2481uninit_vcpu:
2482 kvm_vcpu_uninit(&vmx->vcpu);
2483free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10002484 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002485 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002486}
2487
Yang, Sheng002c7f72007-07-31 14:23:01 +03002488static void __init vmx_check_processor_compat(void *rtn)
2489{
2490 struct vmcs_config vmcs_conf;
2491
2492 *(int *)rtn = 0;
2493 if (setup_vmcs_config(&vmcs_conf) < 0)
2494 *(int *)rtn = -EIO;
2495 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
2496 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
2497 smp_processor_id());
2498 *(int *)rtn = -EIO;
2499 }
2500}
2501
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002502static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002503 .cpu_has_kvm_support = cpu_has_kvm_support,
2504 .disabled_by_bios = vmx_disabled_by_bios,
2505 .hardware_setup = hardware_setup,
2506 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03002507 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002508 .hardware_enable = hardware_enable,
2509 .hardware_disable = hardware_disable,
2510
2511 .vcpu_create = vmx_create_vcpu,
2512 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002513 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002514
Avi Kivity04d2cc72007-09-10 18:10:54 +03002515 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002516 .vcpu_load = vmx_vcpu_load,
2517 .vcpu_put = vmx_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08002518 .vcpu_decache = vmx_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519
2520 .set_guest_debug = set_guest_debug,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002521 .guest_debug_pre = kvm_guest_debug_pre,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002522 .get_msr = vmx_get_msr,
2523 .set_msr = vmx_set_msr,
2524 .get_segment_base = vmx_get_segment_base,
2525 .get_segment = vmx_get_segment,
2526 .set_segment = vmx_set_segment,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002527 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03002528 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002529 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002530 .set_cr3 = vmx_set_cr3,
2531 .set_cr4 = vmx_set_cr4,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002532#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002533 .set_efer = vmx_set_efer,
2534#endif
2535 .get_idt = vmx_get_idt,
2536 .set_idt = vmx_set_idt,
2537 .get_gdt = vmx_get_gdt,
2538 .set_gdt = vmx_set_gdt,
2539 .cache_regs = vcpu_load_rsp_rip,
2540 .decache_regs = vcpu_put_rsp_rip,
2541 .get_rflags = vmx_get_rflags,
2542 .set_rflags = vmx_set_rflags,
2543
2544 .tlb_flush = vmx_flush_tlb,
2545 .inject_page_fault = vmx_inject_page_fault,
2546
2547 .inject_gp = vmx_inject_gp,
2548
2549 .run = vmx_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002550 .handle_exit = kvm_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002551 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02002552 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03002553 .get_irq = vmx_get_irq,
2554 .set_irq = vmx_inject_irq,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002555 .inject_pending_irq = vmx_intr_assist,
2556 .inject_pending_vectors = do_interrupt_requests,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557};
2558
2559static int __init vmx_init(void)
2560{
He, Qingfdef3ad2007-04-30 09:45:24 +03002561 void *iova;
2562 int r;
2563
2564 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2565 if (!vmx_io_bitmap_a)
2566 return -ENOMEM;
2567
2568 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2569 if (!vmx_io_bitmap_b) {
2570 r = -ENOMEM;
2571 goto out;
2572 }
2573
2574 /*
2575 * Allow direct access to the PC debug port (it is often used for I/O
2576 * delays, but the vmexits simply slow things down).
2577 */
2578 iova = kmap(vmx_io_bitmap_a);
2579 memset(iova, 0xff, PAGE_SIZE);
2580 clear_bit(0x80, iova);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002581 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03002582
2583 iova = kmap(vmx_io_bitmap_b);
2584 memset(iova, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002585 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03002586
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002587 r = kvm_init_x86(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03002588 if (r)
2589 goto out1;
2590
Avi Kivityc7addb92007-09-16 18:58:32 +02002591 if (bypass_guest_pf)
2592 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
2593
He, Qingfdef3ad2007-04-30 09:45:24 +03002594 return 0;
2595
2596out1:
2597 __free_page(vmx_io_bitmap_b);
2598out:
2599 __free_page(vmx_io_bitmap_a);
2600 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002601}
2602
2603static void __exit vmx_exit(void)
2604{
He, Qingfdef3ad2007-04-30 09:45:24 +03002605 __free_page(vmx_io_bitmap_b);
2606 __free_page(vmx_io_bitmap_a);
2607
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002608 kvm_exit_x86();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002609}
2610
2611module_init(vmx_init)
2612module_exit(vmx_exit)