blob: 27a3318fa6c29281b4d89badf7a44ebe35785465 [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, Sheng1c3d14f2007-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, Sheng1c3d14f2007-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{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000228 if (vmx->vcpu.cpu != raw_smp_processor_id() && vmx->vcpu.cpu != -1)
229 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear,
230 vmx, 0, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800231 else
Rusty Russell8b9cf982007-07-30 16:31:43 +1000232 __vcpu_clear(vmx);
233 vmx->launched = 0;
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800234}
235
Avi Kivity6aa8b732006-12-10 02:21:36 -0800236static unsigned long vmcs_readl(unsigned long field)
237{
238 unsigned long value;
239
240 asm volatile (ASM_VMX_VMREAD_RDX_RAX
241 : "=a"(value) : "d"(field) : "cc");
242 return value;
243}
244
245static u16 vmcs_read16(unsigned long field)
246{
247 return vmcs_readl(field);
248}
249
250static u32 vmcs_read32(unsigned long field)
251{
252 return vmcs_readl(field);
253}
254
255static u64 vmcs_read64(unsigned long field)
256{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800257#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800258 return vmcs_readl(field);
259#else
260 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
261#endif
262}
263
Avi Kivitye52de1b2007-01-05 16:36:56 -0800264static noinline void vmwrite_error(unsigned long field, unsigned long value)
265{
266 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
267 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
268 dump_stack();
269}
270
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271static void vmcs_writel(unsigned long field, unsigned long value)
272{
273 u8 error;
274
275 asm volatile (ASM_VMX_VMWRITE_RAX_RDX "; setna %0"
276 : "=q"(error) : "a"(value), "d"(field) : "cc" );
Avi Kivitye52de1b2007-01-05 16:36:56 -0800277 if (unlikely(error))
278 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800279}
280
281static void vmcs_write16(unsigned long field, u16 value)
282{
283 vmcs_writel(field, value);
284}
285
286static void vmcs_write32(unsigned long field, u32 value)
287{
288 vmcs_writel(field, value);
289}
290
291static void vmcs_write64(unsigned long field, u64 value)
292{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800293#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294 vmcs_writel(field, value);
295#else
296 vmcs_writel(field, value);
297 asm volatile ("");
298 vmcs_writel(field+1, value >> 32);
299#endif
300}
301
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300302static void vmcs_clear_bits(unsigned long field, u32 mask)
303{
304 vmcs_writel(field, vmcs_readl(field) & ~mask);
305}
306
307static void vmcs_set_bits(unsigned long field, u32 mask)
308{
309 vmcs_writel(field, vmcs_readl(field) | mask);
310}
311
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300312static void update_exception_bitmap(struct kvm_vcpu *vcpu)
313{
314 u32 eb;
315
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500316 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300317 if (!vcpu->fpu_active)
318 eb |= 1u << NM_VECTOR;
319 if (vcpu->guest_debug.enabled)
320 eb |= 1u << 1;
321 if (vcpu->rmode.active)
322 eb = ~0;
323 vmcs_write32(EXCEPTION_BITMAP, eb);
324}
325
Avi Kivity33ed6322007-05-02 16:54:03 +0300326static void reload_tss(void)
327{
328#ifndef CONFIG_X86_64
329
330 /*
331 * VT restores TR but not its size. Useless.
332 */
333 struct descriptor_table gdt;
334 struct segment_descriptor *descs;
335
336 get_gdt(&gdt);
337 descs = (void *)gdt.base;
338 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
339 load_TR_desc();
340#endif
341}
342
Rusty Russell8b9cf982007-07-30 16:31:43 +1000343static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300344{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400345 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300346 u64 host_efer = vmx->host_msrs[efer_offset].data;
347 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
348 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300349
Avi Kivity51c6cf62007-08-29 03:48:05 +0300350 if (efer_offset < 0)
351 return;
352 /*
353 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
354 * outside long mode
355 */
356 ignore_bits = EFER_NX | EFER_SCE;
357#ifdef CONFIG_X86_64
358 ignore_bits |= EFER_LMA | EFER_LME;
359 /* SCE is meaningful only in long mode on Intel */
360 if (guest_efer & EFER_LMA)
361 ignore_bits &= ~(u64)EFER_SCE;
362#endif
363 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
364 return;
365
366 vmx->host_state.guest_efer_loaded = 1;
367 guest_efer &= ~ignore_bits;
368 guest_efer |= host_efer & ignore_bits;
369 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000370 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300371}
372
Avi Kivity51c6cf62007-08-29 03:48:05 +0300373static void reload_host_efer(struct vcpu_vmx *vmx)
374{
375 if (vmx->host_state.guest_efer_loaded) {
376 vmx->host_state.guest_efer_loaded = 0;
377 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
378 }
379}
380
Avi Kivity04d2cc72007-09-10 18:10:54 +0300381static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300382{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300383 struct vcpu_vmx *vmx = to_vmx(vcpu);
384
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400385 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300386 return;
387
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400388 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300389 /*
390 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
391 * allow segment selectors with cpl > 0 or ti == 1.
392 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400393 vmx->host_state.ldt_sel = read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200394 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400395 vmx->host_state.fs_sel = read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200396 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400397 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200398 vmx->host_state.fs_reload_needed = 0;
399 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300400 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200401 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300402 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400403 vmx->host_state.gs_sel = read_gs();
404 if (!(vmx->host_state.gs_sel & 7))
405 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300406 else {
407 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200408 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300409 }
410
411#ifdef CONFIG_X86_64
412 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
413 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
414#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400415 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
416 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300417#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300418
419#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000420 if (is_long_mode(&vmx->vcpu)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400421 save_msrs(vmx->host_msrs +
422 vmx->msr_offset_kernel_gs_base, 1);
Avi Kivity707c0872007-05-02 17:33:43 +0300423 }
424#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400425 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300426 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300427}
428
Rusty Russell8b9cf982007-07-30 16:31:43 +1000429static void vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300430{
Avi Kivity15ad7142007-07-11 18:17:21 +0300431 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300432
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400433 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300434 return;
435
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400436 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200437 if (vmx->host_state.fs_reload_needed)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400438 load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200439 if (vmx->host_state.gs_ldt_reload_needed) {
440 load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300441 /*
442 * If we have to reload gs, we must take care to
443 * preserve our gs base.
444 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300445 local_irq_save(flags);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400446 load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300447#ifdef CONFIG_X86_64
448 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
449#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300450 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300451 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200452 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400453 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
454 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300455 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300456}
457
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458/*
459 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
460 * vcpu mutex is already taken.
461 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300462static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800463{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400464 struct vcpu_vmx *vmx = to_vmx(vcpu);
465 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity77002702007-06-13 19:55:28 +0300466 u64 tsc_this, delta;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467
Eddie Donga3d7f852007-09-03 16:15:12 +0300468 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000469 vcpu_clear(vmx);
Eddie Donga3d7f852007-09-03 16:15:12 +0300470 kvm_migrate_apic_timer(vcpu);
471 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400473 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800474 u8 error;
475
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400476 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800477 asm volatile (ASM_VMX_VMPTRLD_RAX "; setna %0"
478 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
479 : "cc");
480 if (error)
481 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400482 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483 }
484
485 if (vcpu->cpu != cpu) {
486 struct descriptor_table dt;
487 unsigned long sysenter_esp;
488
489 vcpu->cpu = cpu;
490 /*
491 * Linux uses per-cpu TSS and GDT, so set these when switching
492 * processors.
493 */
494 vmcs_writel(HOST_TR_BASE, read_tr_base()); /* 22.2.4 */
495 get_gdt(&dt);
496 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
497
498 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
499 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300500
501 /*
502 * Make sure the time stamp counter is monotonous.
503 */
504 rdtscll(tsc_this);
505 delta = vcpu->host_tsc - tsc_this;
506 vmcs_write64(TSC_OFFSET, vmcs_read64(TSC_OFFSET) + delta);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800507 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800508}
509
510static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
511{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000512 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity7702fd12007-06-14 16:27:40 +0300513 kvm_put_guest_fpu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800514}
515
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300516static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
517{
518 if (vcpu->fpu_active)
519 return;
520 vcpu->fpu_active = 1;
Rusty Russell707d92f2007-07-17 23:19:08 +1000521 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
522 if (vcpu->cr0 & X86_CR0_TS)
523 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300524 update_exception_bitmap(vcpu);
525}
526
527static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
528{
529 if (!vcpu->fpu_active)
530 return;
531 vcpu->fpu_active = 0;
Rusty Russell707d92f2007-07-17 23:19:08 +1000532 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300533 update_exception_bitmap(vcpu);
534}
535
Avi Kivity774c47f2007-02-12 00:54:47 -0800536static void vmx_vcpu_decache(struct kvm_vcpu *vcpu)
537{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000538 vcpu_clear(to_vmx(vcpu));
Avi Kivity774c47f2007-02-12 00:54:47 -0800539}
540
Avi Kivity6aa8b732006-12-10 02:21:36 -0800541static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
542{
543 return vmcs_readl(GUEST_RFLAGS);
544}
545
546static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
547{
Avi Kivity78f78268682007-10-16 19:06:15 +0200548 if (vcpu->rmode.active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100549 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800550 vmcs_writel(GUEST_RFLAGS, rflags);
551}
552
553static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
554{
555 unsigned long rip;
556 u32 interruptibility;
557
558 rip = vmcs_readl(GUEST_RIP);
559 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
560 vmcs_writel(GUEST_RIP, rip);
561
562 /*
563 * We emulated an instruction, so temporary interrupt blocking
564 * should be removed, if set.
565 */
566 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
567 if (interruptibility & 3)
568 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
569 interruptibility & ~3);
Dor Laorc1150d82007-01-05 16:36:24 -0800570 vcpu->interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571}
572
573static void vmx_inject_gp(struct kvm_vcpu *vcpu, unsigned error_code)
574{
575 printk(KERN_DEBUG "inject_general_protection: rip 0x%lx\n",
576 vmcs_readl(GUEST_RIP));
577 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
578 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
579 GP_VECTOR |
580 INTR_TYPE_EXCEPTION |
581 INTR_INFO_DELIEVER_CODE_MASK |
582 INTR_INFO_VALID_MASK);
583}
584
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500585static void vmx_inject_ud(struct kvm_vcpu *vcpu)
586{
587 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
588 UD_VECTOR |
589 INTR_TYPE_EXCEPTION |
590 INTR_INFO_VALID_MASK);
591}
592
Avi Kivity6aa8b732006-12-10 02:21:36 -0800593/*
Eddie Donga75beee2007-05-17 18:55:15 +0300594 * Swap MSR entry in host/guest MSR entry array.
595 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200596#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000597static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300598{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400599 struct kvm_msr_entry tmp;
600
601 tmp = vmx->guest_msrs[to];
602 vmx->guest_msrs[to] = vmx->guest_msrs[from];
603 vmx->guest_msrs[from] = tmp;
604 tmp = vmx->host_msrs[to];
605 vmx->host_msrs[to] = vmx->host_msrs[from];
606 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300607}
Gabriel C54e11fa2007-08-01 16:23:10 +0200608#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300609
610/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300611 * Set up the vmcs to automatically save and restore system
612 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
613 * mode, as fiddling with msrs is very expensive.
614 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000615static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300616{
Eddie Dong2cc51562007-05-21 07:28:09 +0300617 int save_nmsrs;
Avi Kivitye38aea32007-04-19 13:22:48 +0300618
Eddie Donga75beee2007-05-17 18:55:15 +0300619 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300620#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000621 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300622 int index;
623
Rusty Russell8b9cf982007-07-30 16:31:43 +1000624 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300625 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000626 move_msr_up(vmx, index, save_nmsrs++);
627 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300628 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000629 move_msr_up(vmx, index, save_nmsrs++);
630 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300631 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000632 move_msr_up(vmx, index, save_nmsrs++);
633 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300634 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000635 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300636 /*
637 * MSR_K6_STAR is only needed on long mode guests, and only
638 * if efer.sce is enabled.
639 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000640 index = __find_msr_index(vmx, MSR_K6_STAR);
641 if ((index >= 0) && (vmx->vcpu.shadow_efer & EFER_SCE))
642 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300643 }
Eddie Donga75beee2007-05-17 18:55:15 +0300644#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400645 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300646
Eddie Donga75beee2007-05-17 18:55:15 +0300647#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400648 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000649 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300650#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000651 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivitye38aea32007-04-19 13:22:48 +0300652}
653
654/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800655 * reads and returns guest's timestamp counter "register"
656 * guest_tsc = host_tsc + tsc_offset -- 21.3
657 */
658static u64 guest_read_tsc(void)
659{
660 u64 host_tsc, tsc_offset;
661
662 rdtscll(host_tsc);
663 tsc_offset = vmcs_read64(TSC_OFFSET);
664 return host_tsc + tsc_offset;
665}
666
667/*
668 * writes 'guest_tsc' into guest's timestamp counter "register"
669 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
670 */
671static void guest_write_tsc(u64 guest_tsc)
672{
673 u64 host_tsc;
674
675 rdtscll(host_tsc);
676 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
677}
678
Avi Kivity6aa8b732006-12-10 02:21:36 -0800679/*
680 * Reads an msr value (of 'msr_index') into 'pdata'.
681 * Returns 0 on success, non-0 otherwise.
682 * Assumes vcpu_load() was already called.
683 */
684static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
685{
686 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400687 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688
689 if (!pdata) {
690 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
691 return -EINVAL;
692 }
693
694 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800695#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800696 case MSR_FS_BASE:
697 data = vmcs_readl(GUEST_FS_BASE);
698 break;
699 case MSR_GS_BASE:
700 data = vmcs_readl(GUEST_GS_BASE);
701 break;
702 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800703 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704#endif
705 case MSR_IA32_TIME_STAMP_COUNTER:
706 data = guest_read_tsc();
707 break;
708 case MSR_IA32_SYSENTER_CS:
709 data = vmcs_read32(GUEST_SYSENTER_CS);
710 break;
711 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200712 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713 break;
714 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200715 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800717 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000718 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800719 if (msr) {
720 data = msr->data;
721 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800722 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800723 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800724 }
725
726 *pdata = data;
727 return 0;
728}
729
730/*
731 * Writes msr value into into the appropriate "register".
732 * Returns 0 on success, non-0 otherwise.
733 * Assumes vcpu_load() was already called.
734 */
735static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
736{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400737 struct vcpu_vmx *vmx = to_vmx(vcpu);
738 struct kvm_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +0300739 int ret = 0;
740
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800742#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800743 case MSR_EFER:
Eddie Dong2cc51562007-05-21 07:28:09 +0300744 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300745 if (vmx->host_state.loaded) {
746 reload_host_efer(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000747 load_transition_efer(vmx);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300748 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300749 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800750 case MSR_FS_BASE:
751 vmcs_writel(GUEST_FS_BASE, data);
752 break;
753 case MSR_GS_BASE:
754 vmcs_writel(GUEST_GS_BASE, data);
755 break;
756#endif
757 case MSR_IA32_SYSENTER_CS:
758 vmcs_write32(GUEST_SYSENTER_CS, data);
759 break;
760 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200761 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762 break;
763 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200764 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800765 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200766 case MSR_IA32_TIME_STAMP_COUNTER:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767 guest_write_tsc(data);
768 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000770 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800771 if (msr) {
772 msr->data = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400773 if (vmx->host_state.loaded)
774 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800775 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300777 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778 }
779
Eddie Dong2cc51562007-05-21 07:28:09 +0300780 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781}
782
783/*
784 * Sync the rsp and rip registers into the vcpu structure. This allows
785 * registers to be accessed by indexing vcpu->regs.
786 */
787static void vcpu_load_rsp_rip(struct kvm_vcpu *vcpu)
788{
789 vcpu->regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
790 vcpu->rip = vmcs_readl(GUEST_RIP);
791}
792
793/*
794 * Syncs rsp and rip back into the vmcs. Should be called after possible
795 * modification.
796 */
797static void vcpu_put_rsp_rip(struct kvm_vcpu *vcpu)
798{
799 vmcs_writel(GUEST_RSP, vcpu->regs[VCPU_REGS_RSP]);
800 vmcs_writel(GUEST_RIP, vcpu->rip);
801}
802
803static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
804{
805 unsigned long dr7 = 0x400;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806 int old_singlestep;
807
Avi Kivity6aa8b732006-12-10 02:21:36 -0800808 old_singlestep = vcpu->guest_debug.singlestep;
809
810 vcpu->guest_debug.enabled = dbg->enabled;
811 if (vcpu->guest_debug.enabled) {
812 int i;
813
814 dr7 |= 0x200; /* exact */
815 for (i = 0; i < 4; ++i) {
816 if (!dbg->breakpoints[i].enabled)
817 continue;
818 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
819 dr7 |= 2 << (i*2); /* global enable */
820 dr7 |= 0 << (i*4+16); /* execution breakpoint */
821 }
822
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823 vcpu->guest_debug.singlestep = dbg->singlestep;
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300824 } else
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825 vcpu->guest_debug.singlestep = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800826
827 if (old_singlestep && !vcpu->guest_debug.singlestep) {
828 unsigned long flags;
829
830 flags = vmcs_readl(GUEST_RFLAGS);
831 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
832 vmcs_writel(GUEST_RFLAGS, flags);
833 }
834
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300835 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836 vmcs_writel(GUEST_DR7, dr7);
837
838 return 0;
839}
840
Eddie Dong2a8067f2007-08-06 16:29:07 +0300841static int vmx_get_irq(struct kvm_vcpu *vcpu)
842{
843 u32 idtv_info_field;
844
845 idtv_info_field = vmcs_read32(IDT_VECTORING_INFO_FIELD);
846 if (idtv_info_field & INTR_INFO_VALID_MASK) {
847 if (is_external_interrupt(idtv_info_field))
848 return idtv_info_field & VECTORING_INFO_VECTOR_MASK;
849 else
850 printk("pending exception: not handled yet\n");
851 }
852 return -1;
853}
854
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855static __init int cpu_has_kvm_support(void)
856{
857 unsigned long ecx = cpuid_ecx(1);
858 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
859}
860
861static __init int vmx_disabled_by_bios(void)
862{
863 u64 msr;
864
865 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300866 return (msr & (MSR_IA32_FEATURE_CONTROL_LOCKED |
867 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
868 == MSR_IA32_FEATURE_CONTROL_LOCKED;
869 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870}
871
Avi Kivity774c47f2007-02-12 00:54:47 -0800872static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873{
874 int cpu = raw_smp_processor_id();
875 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
876 u64 old;
877
878 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300879 if ((old & (MSR_IA32_FEATURE_CONTROL_LOCKED |
880 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
881 != (MSR_IA32_FEATURE_CONTROL_LOCKED |
882 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300884 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
885 MSR_IA32_FEATURE_CONTROL_LOCKED |
886 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +1000887 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888 asm volatile (ASM_VMX_VMXON_RAX : : "a"(&phys_addr), "m"(phys_addr)
889 : "memory", "cc");
890}
891
892static void hardware_disable(void *garbage)
893{
894 asm volatile (ASM_VMX_VMXOFF : : : "cc");
895}
896
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300897static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
898 u32 msr, u32* result)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899{
900 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300901 u32 ctl = ctl_min | ctl_opt;
902
903 rdmsr(msr, vmx_msr_low, vmx_msr_high);
904
905 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
906 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
907
908 /* Ensure minimum (required) set of control bits are supported. */
909 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300910 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300911
912 *result = ctl;
913 return 0;
914}
915
Yang, Sheng002c7f72007-07-31 14:23:01 +0300916static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300917{
918 u32 vmx_msr_low, vmx_msr_high;
919 u32 min, opt;
920 u32 _pin_based_exec_control = 0;
921 u32 _cpu_based_exec_control = 0;
922 u32 _vmexit_control = 0;
923 u32 _vmentry_control = 0;
924
925 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
926 opt = 0;
927 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
928 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300929 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300930
931 min = CPU_BASED_HLT_EXITING |
932#ifdef CONFIG_X86_64
933 CPU_BASED_CR8_LOAD_EXITING |
934 CPU_BASED_CR8_STORE_EXITING |
935#endif
936 CPU_BASED_USE_IO_BITMAPS |
937 CPU_BASED_MOV_DR_EXITING |
938 CPU_BASED_USE_TSC_OFFSETING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800939#ifdef CONFIG_X86_64
940 opt = CPU_BASED_TPR_SHADOW;
941#else
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300942 opt = 0;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800943#endif
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300944 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
945 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300946 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800947#ifdef CONFIG_X86_64
948 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
949 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
950 ~CPU_BASED_CR8_STORE_EXITING;
951#endif
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300952
953 min = 0;
954#ifdef CONFIG_X86_64
955 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
956#endif
957 opt = 0;
958 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
959 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300960 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300961
962 min = opt = 0;
963 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
964 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300965 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -0800967 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300968
969 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
970 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300971 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300972
973#ifdef CONFIG_X86_64
974 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
975 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +0300976 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300977#endif
978
979 /* Require Write-Back (WB) memory type for VMCS accesses. */
980 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300981 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300982
Yang, Sheng002c7f72007-07-31 14:23:01 +0300983 vmcs_conf->size = vmx_msr_high & 0x1fff;
984 vmcs_conf->order = get_order(vmcs_config.size);
985 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300986
Yang, Sheng002c7f72007-07-31 14:23:01 +0300987 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
988 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
989 vmcs_conf->vmexit_ctrl = _vmexit_control;
990 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300991
992 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -0800993}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994
995static struct vmcs *alloc_vmcs_cpu(int cpu)
996{
997 int node = cpu_to_node(cpu);
998 struct page *pages;
999 struct vmcs *vmcs;
1000
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001001 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002 if (!pages)
1003 return NULL;
1004 vmcs = page_address(pages);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001005 memset(vmcs, 0, vmcs_config.size);
1006 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007 return vmcs;
1008}
1009
1010static struct vmcs *alloc_vmcs(void)
1011{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001012 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013}
1014
1015static void free_vmcs(struct vmcs *vmcs)
1016{
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001017 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018}
1019
Sam Ravnborg39959582007-06-01 00:47:13 -07001020static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021{
1022 int cpu;
1023
1024 for_each_online_cpu(cpu)
1025 free_vmcs(per_cpu(vmxarea, cpu));
1026}
1027
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028static __init int alloc_kvm_area(void)
1029{
1030 int cpu;
1031
1032 for_each_online_cpu(cpu) {
1033 struct vmcs *vmcs;
1034
1035 vmcs = alloc_vmcs_cpu(cpu);
1036 if (!vmcs) {
1037 free_kvm_area();
1038 return -ENOMEM;
1039 }
1040
1041 per_cpu(vmxarea, cpu) = vmcs;
1042 }
1043 return 0;
1044}
1045
1046static __init int hardware_setup(void)
1047{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001048 if (setup_vmcs_config(&vmcs_config) < 0)
1049 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050 return alloc_kvm_area();
1051}
1052
1053static __exit void hardware_unsetup(void)
1054{
1055 free_kvm_area();
1056}
1057
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1059{
1060 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1061
Avi Kivity6af11b92007-03-19 13:18:10 +02001062 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063 vmcs_write16(sf->selector, save->selector);
1064 vmcs_writel(sf->base, save->base);
1065 vmcs_write32(sf->limit, save->limit);
1066 vmcs_write32(sf->ar_bytes, save->ar);
1067 } else {
1068 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1069 << AR_DPL_SHIFT;
1070 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1071 }
1072}
1073
1074static void enter_pmode(struct kvm_vcpu *vcpu)
1075{
1076 unsigned long flags;
1077
1078 vcpu->rmode.active = 0;
1079
1080 vmcs_writel(GUEST_TR_BASE, vcpu->rmode.tr.base);
1081 vmcs_write32(GUEST_TR_LIMIT, vcpu->rmode.tr.limit);
1082 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->rmode.tr.ar);
1083
1084 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001085 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086 flags |= (vcpu->rmode.save_iopl << IOPL_SHIFT);
1087 vmcs_writel(GUEST_RFLAGS, flags);
1088
Rusty Russell66aee912007-07-17 23:34:16 +10001089 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1090 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091
1092 update_exception_bitmap(vcpu);
1093
1094 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->rmode.es);
1095 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->rmode.ds);
1096 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->rmode.gs);
1097 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->rmode.fs);
1098
1099 vmcs_write16(GUEST_SS_SELECTOR, 0);
1100 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1101
1102 vmcs_write16(GUEST_CS_SELECTOR,
1103 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1104 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1105}
1106
Izik Eidus33f5fa12007-08-19 22:24:58 +03001107static gva_t rmode_tss_base(struct kvm* kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108{
1109 gfn_t base_gfn = kvm->memslots[0].base_gfn + kvm->memslots[0].npages - 3;
1110 return base_gfn << PAGE_SHIFT;
1111}
1112
1113static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1114{
1115 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1116
1117 save->selector = vmcs_read16(sf->selector);
1118 save->base = vmcs_readl(sf->base);
1119 save->limit = vmcs_read32(sf->limit);
1120 save->ar = vmcs_read32(sf->ar_bytes);
1121 vmcs_write16(sf->selector, vmcs_readl(sf->base) >> 4);
1122 vmcs_write32(sf->limit, 0xffff);
1123 vmcs_write32(sf->ar_bytes, 0xf3);
1124}
1125
1126static void enter_rmode(struct kvm_vcpu *vcpu)
1127{
1128 unsigned long flags;
1129
1130 vcpu->rmode.active = 1;
1131
1132 vcpu->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
1133 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1134
1135 vcpu->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
1136 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1137
1138 vcpu->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
1139 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1140
1141 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001142 vcpu->rmode.save_iopl = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001144 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145
1146 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001147 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148 update_exception_bitmap(vcpu);
1149
1150 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1151 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1152 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1153
1154 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001155 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001156 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1157 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1159
1160 fix_rmode_seg(VCPU_SREG_ES, &vcpu->rmode.es);
1161 fix_rmode_seg(VCPU_SREG_DS, &vcpu->rmode.ds);
1162 fix_rmode_seg(VCPU_SREG_GS, &vcpu->rmode.gs);
1163 fix_rmode_seg(VCPU_SREG_FS, &vcpu->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001164
Eddie Dong8668a3c2007-10-10 14:26:45 +08001165 kvm_mmu_reset_context(vcpu);
Avi Kivity75880a02007-06-20 11:20:04 +03001166 init_rmode_tss(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167}
1168
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001169#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170
1171static void enter_lmode(struct kvm_vcpu *vcpu)
1172{
1173 u32 guest_tr_ar;
1174
1175 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1176 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1177 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
1178 __FUNCTION__);
1179 vmcs_write32(GUEST_TR_AR_BYTES,
1180 (guest_tr_ar & ~AR_TYPE_MASK)
1181 | AR_TYPE_BUSY_64_TSS);
1182 }
1183
1184 vcpu->shadow_efer |= EFER_LMA;
1185
Rusty Russell8b9cf982007-07-30 16:31:43 +10001186 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001187 vmcs_write32(VM_ENTRY_CONTROLS,
1188 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001189 | VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001190}
1191
1192static void exit_lmode(struct kvm_vcpu *vcpu)
1193{
1194 vcpu->shadow_efer &= ~EFER_LMA;
1195
1196 vmcs_write32(VM_ENTRY_CONTROLS,
1197 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001198 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001199}
1200
1201#endif
1202
Anthony Liguori25c4c272007-04-27 09:29:21 +03001203static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001204{
Avi Kivity399badf2007-01-05 16:36:38 -08001205 vcpu->cr4 &= KVM_GUEST_CR4_MASK;
1206 vcpu->cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
1207}
1208
Avi Kivity6aa8b732006-12-10 02:21:36 -08001209static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1210{
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001211 vmx_fpu_deactivate(vcpu);
1212
Rusty Russell707d92f2007-07-17 23:19:08 +10001213 if (vcpu->rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214 enter_pmode(vcpu);
1215
Rusty Russell707d92f2007-07-17 23:19:08 +10001216 if (!vcpu->rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001217 enter_rmode(vcpu);
1218
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001219#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001220 if (vcpu->shadow_efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001221 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10001223 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001224 exit_lmode(vcpu);
1225 }
1226#endif
1227
1228 vmcs_writel(CR0_READ_SHADOW, cr0);
1229 vmcs_writel(GUEST_CR0,
1230 (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON);
1231 vcpu->cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001232
Rusty Russell707d92f2007-07-17 23:19:08 +10001233 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001234 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001235}
1236
Avi Kivity6aa8b732006-12-10 02:21:36 -08001237static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1238{
1239 vmcs_writel(GUEST_CR3, cr3);
Rusty Russell707d92f2007-07-17 23:19:08 +10001240 if (vcpu->cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001241 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001242}
1243
1244static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1245{
1246 vmcs_writel(CR4_READ_SHADOW, cr4);
1247 vmcs_writel(GUEST_CR4, cr4 | (vcpu->rmode.active ?
1248 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON));
1249 vcpu->cr4 = cr4;
1250}
1251
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001252#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001253
1254static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1255{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001256 struct vcpu_vmx *vmx = to_vmx(vcpu);
1257 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001258
1259 vcpu->shadow_efer = efer;
1260 if (efer & EFER_LMA) {
1261 vmcs_write32(VM_ENTRY_CONTROLS,
1262 vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001263 VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264 msr->data = efer;
1265
1266 } else {
1267 vmcs_write32(VM_ENTRY_CONTROLS,
1268 vmcs_read32(VM_ENTRY_CONTROLS) &
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001269 ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270
1271 msr->data = efer & ~EFER_LME;
1272 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001273 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001274}
1275
1276#endif
1277
1278static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1279{
1280 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1281
1282 return vmcs_readl(sf->base);
1283}
1284
1285static void vmx_get_segment(struct kvm_vcpu *vcpu,
1286 struct kvm_segment *var, int seg)
1287{
1288 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1289 u32 ar;
1290
1291 var->base = vmcs_readl(sf->base);
1292 var->limit = vmcs_read32(sf->limit);
1293 var->selector = vmcs_read16(sf->selector);
1294 ar = vmcs_read32(sf->ar_bytes);
1295 if (ar & AR_UNUSABLE_MASK)
1296 ar = 0;
1297 var->type = ar & 15;
1298 var->s = (ar >> 4) & 1;
1299 var->dpl = (ar >> 5) & 3;
1300 var->present = (ar >> 7) & 1;
1301 var->avl = (ar >> 12) & 1;
1302 var->l = (ar >> 13) & 1;
1303 var->db = (ar >> 14) & 1;
1304 var->g = (ar >> 15) & 1;
1305 var->unusable = (ar >> 16) & 1;
1306}
1307
Avi Kivity653e3102007-05-07 10:55:37 +03001308static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310 u32 ar;
1311
Avi Kivity653e3102007-05-07 10:55:37 +03001312 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313 ar = 1 << 16;
1314 else {
1315 ar = var->type & 15;
1316 ar |= (var->s & 1) << 4;
1317 ar |= (var->dpl & 3) << 5;
1318 ar |= (var->present & 1) << 7;
1319 ar |= (var->avl & 1) << 12;
1320 ar |= (var->l & 1) << 13;
1321 ar |= (var->db & 1) << 14;
1322 ar |= (var->g & 1) << 15;
1323 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001324 if (ar == 0) /* a 0 value means unusable */
1325 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001326
1327 return ar;
1328}
1329
1330static void vmx_set_segment(struct kvm_vcpu *vcpu,
1331 struct kvm_segment *var, int seg)
1332{
1333 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1334 u32 ar;
1335
1336 if (vcpu->rmode.active && seg == VCPU_SREG_TR) {
1337 vcpu->rmode.tr.selector = var->selector;
1338 vcpu->rmode.tr.base = var->base;
1339 vcpu->rmode.tr.limit = var->limit;
1340 vcpu->rmode.tr.ar = vmx_segment_access_rights(var);
1341 return;
1342 }
1343 vmcs_writel(sf->base, var->base);
1344 vmcs_write32(sf->limit, var->limit);
1345 vmcs_write16(sf->selector, var->selector);
1346 if (vcpu->rmode.active && var->s) {
1347 /*
1348 * Hack real-mode segments into vm86 compatibility.
1349 */
1350 if (var->base == 0xffff0000 && var->selector == 0xf000)
1351 vmcs_writel(sf->base, 0xf0000);
1352 ar = 0xf3;
1353 } else
1354 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355 vmcs_write32(sf->ar_bytes, ar);
1356}
1357
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1359{
1360 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1361
1362 *db = (ar >> 14) & 1;
1363 *l = (ar >> 13) & 1;
1364}
1365
1366static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1367{
1368 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1369 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1370}
1371
1372static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1373{
1374 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1375 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1376}
1377
1378static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1379{
1380 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1381 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1382}
1383
1384static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1385{
1386 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1387 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1388}
1389
1390static int init_rmode_tss(struct kvm* kvm)
1391{
1392 struct page *p1, *p2, *p3;
1393 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
1394 char *page;
1395
Avi Kivity954bbbc22007-03-30 14:02:32 +03001396 p1 = gfn_to_page(kvm, fn++);
1397 p2 = gfn_to_page(kvm, fn++);
1398 p3 = gfn_to_page(kvm, fn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399
1400 if (!p1 || !p2 || !p3) {
1401 kvm_printf(kvm,"%s: gfn_to_page failed\n", __FUNCTION__);
1402 return 0;
1403 }
1404
1405 page = kmap_atomic(p1, KM_USER0);
Shani Moideena3870c42007-06-11 09:31:33 +05301406 clear_page(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407 *(u16*)(page + 0x66) = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
1408 kunmap_atomic(page, KM_USER0);
1409
1410 page = kmap_atomic(p2, KM_USER0);
Shani Moideena3870c42007-06-11 09:31:33 +05301411 clear_page(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412 kunmap_atomic(page, KM_USER0);
1413
1414 page = kmap_atomic(p3, KM_USER0);
Shani Moideena3870c42007-06-11 09:31:33 +05301415 clear_page(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416 *(page + RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1) = ~0;
1417 kunmap_atomic(page, KM_USER0);
1418
1419 return 1;
1420}
1421
Avi Kivity6aa8b732006-12-10 02:21:36 -08001422static void seg_setup(int seg)
1423{
1424 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1425
1426 vmcs_write16(sf->selector, 0);
1427 vmcs_writel(sf->base, 0);
1428 vmcs_write32(sf->limit, 0xffff);
1429 vmcs_write32(sf->ar_bytes, 0x93);
1430}
1431
1432/*
1433 * Sets up the vmcs for emulated real mode.
1434 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001435static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436{
1437 u32 host_sysenter_cs;
1438 u32 junk;
1439 unsigned long a;
1440 struct descriptor_table dt;
1441 int i;
1442 int ret = 0;
Avi Kivitycd2276a2007-05-14 20:41:13 +03001443 unsigned long kvm_vmx_return;
Eddie Dong7017fc32007-07-18 11:34:57 +03001444 u64 msr;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001445 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001446
Rusty Russell8b9cf982007-07-30 16:31:43 +10001447 if (!init_rmode_tss(vmx->vcpu.kvm)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001448 ret = -ENOMEM;
1449 goto out;
1450 }
1451
He, Qingc5ec1532007-09-03 17:07:41 +03001452 vmx->vcpu.rmode.active = 0;
1453
Rusty Russell8b9cf982007-07-30 16:31:43 +10001454 vmx->vcpu.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Eddie Dong7017fc32007-07-18 11:34:57 +03001455 set_cr8(&vmx->vcpu, 0);
1456 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Rusty Russell8b9cf982007-07-30 16:31:43 +10001457 if (vmx->vcpu.vcpu_id == 0)
Eddie Dong7017fc32007-07-18 11:34:57 +03001458 msr |= MSR_IA32_APICBASE_BSP;
1459 kvm_set_apic_base(&vmx->vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001460
Rusty Russell8b9cf982007-07-30 16:31:43 +10001461 fx_init(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462
1463 /*
1464 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
1465 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
1466 */
He, Qingc5ec1532007-09-03 17:07:41 +03001467 if (vmx->vcpu.vcpu_id == 0) {
1468 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
1469 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
1470 } else {
1471 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.sipi_vector << 8);
1472 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.sipi_vector << 12);
1473 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001474 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
1475 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1476
1477 seg_setup(VCPU_SREG_DS);
1478 seg_setup(VCPU_SREG_ES);
1479 seg_setup(VCPU_SREG_FS);
1480 seg_setup(VCPU_SREG_GS);
1481 seg_setup(VCPU_SREG_SS);
1482
1483 vmcs_write16(GUEST_TR_SELECTOR, 0);
1484 vmcs_writel(GUEST_TR_BASE, 0);
1485 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
1486 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1487
1488 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
1489 vmcs_writel(GUEST_LDTR_BASE, 0);
1490 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
1491 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
1492
1493 vmcs_write32(GUEST_SYSENTER_CS, 0);
1494 vmcs_writel(GUEST_SYSENTER_ESP, 0);
1495 vmcs_writel(GUEST_SYSENTER_EIP, 0);
1496
1497 vmcs_writel(GUEST_RFLAGS, 0x02);
He, Qingc5ec1532007-09-03 17:07:41 +03001498 if (vmx->vcpu.vcpu_id == 0)
1499 vmcs_writel(GUEST_RIP, 0xfff0);
1500 else
1501 vmcs_writel(GUEST_RIP, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502 vmcs_writel(GUEST_RSP, 0);
1503
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504 //todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0
1505 vmcs_writel(GUEST_DR7, 0x400);
1506
1507 vmcs_writel(GUEST_GDTR_BASE, 0);
1508 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
1509
1510 vmcs_writel(GUEST_IDTR_BASE, 0);
1511 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
1512
1513 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
1514 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
1515 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
1516
1517 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03001518 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
1519 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001520
1521 guest_write_tsc(0);
1522
1523 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
1524
1525 /* Special registers */
1526 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
1527
1528 /* Control */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001529 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
1530 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001531
1532 exec_control = vmcs_config.cpu_based_exec_ctrl;
1533 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
1534 exec_control &= ~CPU_BASED_TPR_SHADOW;
1535#ifdef CONFIG_X86_64
1536 exec_control |= CPU_BASED_CR8_STORE_EXITING |
1537 CPU_BASED_CR8_LOAD_EXITING;
1538#endif
1539 }
1540 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001541
Avi Kivityc7addb92007-09-16 18:58:32 +02001542 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
1543 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001544 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
1545
1546 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
1547 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
1548 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1549
1550 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
1551 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1552 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1553 vmcs_write16(HOST_FS_SELECTOR, read_fs()); /* 22.2.4 */
1554 vmcs_write16(HOST_GS_SELECTOR, read_gs()); /* 22.2.4 */
1555 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001556#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557 rdmsrl(MSR_FS_BASE, a);
1558 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
1559 rdmsrl(MSR_GS_BASE, a);
1560 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
1561#else
1562 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
1563 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
1564#endif
1565
1566 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
1567
1568 get_idt(&dt);
1569 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
1570
Avi Kivitycd2276a2007-05-14 20:41:13 +03001571 asm ("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
1572 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03001573 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
1574 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
1575 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001576
1577 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
1578 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
1579 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
1580 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
1581 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
1582 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
1583
Avi Kivity6aa8b732006-12-10 02:21:36 -08001584 for (i = 0; i < NR_VMX_MSR; ++i) {
1585 u32 index = vmx_msr_index[i];
1586 u32 data_low, data_high;
1587 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001588 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001589
1590 if (rdmsr_safe(index, &data_low, &data_high) < 0)
1591 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08001592 if (wrmsr_safe(index, data_low, data_high) < 0)
1593 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001595 vmx->host_msrs[j].index = index;
1596 vmx->host_msrs[j].reserved = 0;
1597 vmx->host_msrs[j].data = data;
1598 vmx->guest_msrs[j] = vmx->host_msrs[j];
1599 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001600 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001601
Rusty Russell8b9cf982007-07-30 16:31:43 +10001602 setup_msrs(vmx);
Avi Kivitye38aea32007-04-19 13:22:48 +03001603
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001604 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001605
1606 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001607 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
1608
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
1610
Michael Riepe3b99ab22006-12-13 00:34:15 -08001611#ifdef CONFIG_X86_64
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001612 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
1613 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
1614 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
1615 page_to_phys(vmx->vcpu.apic->regs_page));
1616 vmcs_write32(TPR_THRESHOLD, 0);
Michael Riepe3b99ab22006-12-13 00:34:15 -08001617#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618
Anthony Liguori25c4c272007-04-27 09:29:21 +03001619 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001620 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
1621
Rusty Russell8b9cf982007-07-30 16:31:43 +10001622 vmx->vcpu.cr0 = 0x60000010;
1623 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.cr0); // enter rmode
1624 vmx_set_cr4(&vmx->vcpu, 0);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001625#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001626 vmx_set_efer(&vmx->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +10001628 vmx_fpu_activate(&vmx->vcpu);
1629 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001630
1631 return 0;
1632
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633out:
1634 return ret;
1635}
1636
Avi Kivity04d2cc72007-09-10 18:10:54 +03001637static void vmx_vcpu_reset(struct kvm_vcpu *vcpu)
1638{
1639 struct vcpu_vmx *vmx = to_vmx(vcpu);
1640
1641 vmx_vcpu_setup(vmx);
1642}
1643
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644static void inject_rmode_irq(struct kvm_vcpu *vcpu, int irq)
1645{
1646 u16 ent[2];
1647 u16 cs;
1648 u16 ip;
1649 unsigned long flags;
1650 unsigned long ss_base = vmcs_readl(GUEST_SS_BASE);
1651 u16 sp = vmcs_readl(GUEST_RSP);
1652 u32 ss_limit = vmcs_read32(GUEST_SS_LIMIT);
1653
Eric Sesterhenn / Snakebyte39649942007-04-09 16:15:05 +02001654 if (sp > ss_limit || sp < 6 ) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655 vcpu_printf(vcpu, "%s: #SS, rsp 0x%lx ss 0x%lx limit 0x%x\n",
1656 __FUNCTION__,
1657 vmcs_readl(GUEST_RSP),
1658 vmcs_readl(GUEST_SS_BASE),
1659 vmcs_read32(GUEST_SS_LIMIT));
1660 return;
1661 }
1662
Laurent Viviere7d5d762007-07-30 13:41:19 +03001663 if (emulator_read_std(irq * sizeof(ent), &ent, sizeof(ent), vcpu) !=
1664 X86EMUL_CONTINUE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665 vcpu_printf(vcpu, "%s: read guest err\n", __FUNCTION__);
1666 return;
1667 }
1668
1669 flags = vmcs_readl(GUEST_RFLAGS);
1670 cs = vmcs_readl(GUEST_CS_BASE) >> 4;
1671 ip = vmcs_readl(GUEST_RIP);
1672
1673
Laurent Viviere7d5d762007-07-30 13:41:19 +03001674 if (emulator_write_emulated(ss_base + sp - 2, &flags, 2, vcpu) != X86EMUL_CONTINUE ||
1675 emulator_write_emulated(ss_base + sp - 4, &cs, 2, vcpu) != X86EMUL_CONTINUE ||
1676 emulator_write_emulated(ss_base + sp - 6, &ip, 2, vcpu) != X86EMUL_CONTINUE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001677 vcpu_printf(vcpu, "%s: write guest err\n", __FUNCTION__);
1678 return;
1679 }
1680
1681 vmcs_writel(GUEST_RFLAGS, flags &
1682 ~( X86_EFLAGS_IF | X86_EFLAGS_AC | X86_EFLAGS_TF));
1683 vmcs_write16(GUEST_CS_SELECTOR, ent[1]) ;
1684 vmcs_writel(GUEST_CS_BASE, ent[1] << 4);
1685 vmcs_writel(GUEST_RIP, ent[0]);
1686 vmcs_writel(GUEST_RSP, (vmcs_readl(GUEST_RSP) & ~0xffff) | (sp - 6));
1687}
1688
Eddie Dong85f455f2007-07-06 12:20:49 +03001689static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
1690{
1691 if (vcpu->rmode.active) {
1692 inject_rmode_irq(vcpu, irq);
1693 return;
1694 }
1695 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
1696 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1697}
1698
Avi Kivity6aa8b732006-12-10 02:21:36 -08001699static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
1700{
1701 int word_index = __ffs(vcpu->irq_summary);
1702 int bit_index = __ffs(vcpu->irq_pending[word_index]);
1703 int irq = word_index * BITS_PER_LONG + bit_index;
1704
1705 clear_bit(bit_index, &vcpu->irq_pending[word_index]);
1706 if (!vcpu->irq_pending[word_index])
1707 clear_bit(word_index, &vcpu->irq_summary);
Eddie Dong85f455f2007-07-06 12:20:49 +03001708 vmx_inject_irq(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709}
1710
Dor Laorc1150d82007-01-05 16:36:24 -08001711
1712static void do_interrupt_requests(struct kvm_vcpu *vcpu,
1713 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714{
Dor Laorc1150d82007-01-05 16:36:24 -08001715 u32 cpu_based_vm_exec_control;
1716
1717 vcpu->interrupt_window_open =
1718 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
1719 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
1720
1721 if (vcpu->interrupt_window_open &&
1722 vcpu->irq_summary &&
1723 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001724 /*
Dor Laorc1150d82007-01-05 16:36:24 -08001725 * If interrupts enabled, and not blocked by sti or mov ss. Good.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726 */
1727 kvm_do_inject_irq(vcpu);
Dor Laorc1150d82007-01-05 16:36:24 -08001728
1729 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
1730 if (!vcpu->interrupt_window_open &&
1731 (vcpu->irq_summary || kvm_run->request_interrupt_window))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001732 /*
1733 * Interrupts blocked. Wait for unblock.
1734 */
Dor Laorc1150d82007-01-05 16:36:24 -08001735 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
1736 else
1737 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
1738 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001739}
1740
1741static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
1742{
1743 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
1744
1745 set_debugreg(dbg->bp[0], 0);
1746 set_debugreg(dbg->bp[1], 1);
1747 set_debugreg(dbg->bp[2], 2);
1748 set_debugreg(dbg->bp[3], 3);
1749
1750 if (dbg->singlestep) {
1751 unsigned long flags;
1752
1753 flags = vmcs_readl(GUEST_RFLAGS);
1754 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1755 vmcs_writel(GUEST_RFLAGS, flags);
1756 }
1757}
1758
1759static int handle_rmode_exception(struct kvm_vcpu *vcpu,
1760 int vec, u32 err_code)
1761{
1762 if (!vcpu->rmode.active)
1763 return 0;
1764
Nitin A Kambleb3f37702007-05-17 15:50:34 +03001765 /*
1766 * Instruction with address size override prefix opcode 0x67
1767 * Cause the #SS fault with 0 error code in VM86 mode.
1768 */
1769 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02001770 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001771 return 1;
1772 return 0;
1773}
1774
1775static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1776{
1777 u32 intr_info, error_code;
1778 unsigned long cr2, rip;
1779 u32 vect_info;
1780 enum emulation_result er;
Avi Kivitye2dec932007-01-05 16:36:54 -08001781 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001782
1783 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
1784 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
1785
1786 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
1787 !is_page_fault(intr_info)) {
1788 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
1789 "intr info 0x%x\n", __FUNCTION__, vect_info, intr_info);
1790 }
1791
Eddie Dong85f455f2007-07-06 12:20:49 +03001792 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001793 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
1794 set_bit(irq, vcpu->irq_pending);
1795 set_bit(irq / BITS_PER_LONG, &vcpu->irq_summary);
1796 }
1797
Avi Kivity1b6269d2007-10-09 12:12:19 +02001798 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
1799 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001800
1801 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001802 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001803 return 1;
1804 }
1805
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001806 if (is_invalid_opcode(intr_info)) {
Laurent Vivier34273182007-09-18 11:27:37 +02001807 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001808 if (er != EMULATE_DONE)
1809 vmx_inject_ud(vcpu);
1810
1811 return 1;
1812 }
1813
Avi Kivity6aa8b732006-12-10 02:21:36 -08001814 error_code = 0;
1815 rip = vmcs_readl(GUEST_RIP);
1816 if (intr_info & INTR_INFO_DELIEVER_CODE_MASK)
1817 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
1818 if (is_page_fault(intr_info)) {
1819 cr2 = vmcs_readl(EXIT_QUALIFICATION);
1820
Shaohua Li11ec2802007-07-23 14:51:37 +08001821 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001822 r = kvm_mmu_page_fault(vcpu, cr2, error_code);
1823 if (r < 0) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001824 mutex_unlock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001825 return r;
1826 }
1827 if (!r) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001828 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001829 return 1;
1830 }
1831
Laurent Vivier34273182007-09-18 11:27:37 +02001832 er = emulate_instruction(vcpu, kvm_run, cr2, error_code, 0);
Shaohua Li11ec2802007-07-23 14:51:37 +08001833 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001834
1835 switch (er) {
1836 case EMULATE_DONE:
1837 return 1;
1838 case EMULATE_DO_MMIO:
Avi Kivity1165f5f2007-04-19 17:27:43 +03001839 ++vcpu->stat.mmio_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001840 return 0;
1841 case EMULATE_FAIL:
Avi Kivity054b1362007-09-12 13:21:09 +03001842 kvm_report_emulation_failure(vcpu, "pagetable");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001843 break;
1844 default:
1845 BUG();
1846 }
1847 }
1848
1849 if (vcpu->rmode.active &&
1850 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03001851 error_code)) {
1852 if (vcpu->halt_request) {
1853 vcpu->halt_request = 0;
1854 return kvm_emulate_halt(vcpu);
1855 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001856 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03001857 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858
1859 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) == (INTR_TYPE_EXCEPTION | 1)) {
1860 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1861 return 0;
1862 }
1863 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
1864 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
1865 kvm_run->ex.error_code = error_code;
1866 return 0;
1867}
1868
1869static int handle_external_interrupt(struct kvm_vcpu *vcpu,
1870 struct kvm_run *kvm_run)
1871{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001872 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001873 return 1;
1874}
1875
Avi Kivity988ad742007-02-12 00:54:36 -08001876static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1877{
1878 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1879 return 0;
1880}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001881
Avi Kivity6aa8b732006-12-10 02:21:36 -08001882static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1883{
He, Qingbfdaab02007-09-12 14:18:28 +08001884 unsigned long exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02001885 int size, down, in, string, rep;
1886 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001887
Avi Kivity1165f5f2007-04-19 17:27:43 +03001888 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08001889 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02001890 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001891
1892 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02001893 if (emulate_instruction(vcpu,
1894 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03001895 return 0;
1896 return 1;
1897 }
1898
1899 size = (exit_qualification & 7) + 1;
1900 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001901 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001902 rep = (exit_qualification & 32) != 0;
1903 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03001904
Laurent Vivier3090dd72007-08-05 10:43:32 +03001905 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001906}
1907
Ingo Molnar102d8322007-02-19 14:37:47 +02001908static void
1909vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
1910{
1911 /*
1912 * Patch in the VMCALL instruction:
1913 */
1914 hypercall[0] = 0x0f;
1915 hypercall[1] = 0x01;
1916 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02001917}
1918
Avi Kivity6aa8b732006-12-10 02:21:36 -08001919static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1920{
He, Qingbfdaab02007-09-12 14:18:28 +08001921 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001922 int cr;
1923 int reg;
1924
He, Qingbfdaab02007-09-12 14:18:28 +08001925 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001926 cr = exit_qualification & 15;
1927 reg = (exit_qualification >> 8) & 15;
1928 switch ((exit_qualification >> 4) & 3) {
1929 case 0: /* mov to cr */
1930 switch (cr) {
1931 case 0:
1932 vcpu_load_rsp_rip(vcpu);
1933 set_cr0(vcpu, vcpu->regs[reg]);
1934 skip_emulated_instruction(vcpu);
1935 return 1;
1936 case 3:
1937 vcpu_load_rsp_rip(vcpu);
1938 set_cr3(vcpu, vcpu->regs[reg]);
1939 skip_emulated_instruction(vcpu);
1940 return 1;
1941 case 4:
1942 vcpu_load_rsp_rip(vcpu);
1943 set_cr4(vcpu, vcpu->regs[reg]);
1944 skip_emulated_instruction(vcpu);
1945 return 1;
1946 case 8:
1947 vcpu_load_rsp_rip(vcpu);
1948 set_cr8(vcpu, vcpu->regs[reg]);
1949 skip_emulated_instruction(vcpu);
Yang, Sheng253abde2007-08-16 13:01:00 +03001950 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
1951 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952 };
1953 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03001954 case 2: /* clts */
1955 vcpu_load_rsp_rip(vcpu);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001956 vmx_fpu_deactivate(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10001957 vcpu->cr0 &= ~X86_CR0_TS;
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001958 vmcs_writel(CR0_READ_SHADOW, vcpu->cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001959 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03001960 skip_emulated_instruction(vcpu);
1961 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001962 case 1: /*mov from cr*/
1963 switch (cr) {
1964 case 3:
1965 vcpu_load_rsp_rip(vcpu);
1966 vcpu->regs[reg] = vcpu->cr3;
1967 vcpu_put_rsp_rip(vcpu);
1968 skip_emulated_instruction(vcpu);
1969 return 1;
1970 case 8:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001971 vcpu_load_rsp_rip(vcpu);
Eddie Dong7017fc32007-07-18 11:34:57 +03001972 vcpu->regs[reg] = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973 vcpu_put_rsp_rip(vcpu);
1974 skip_emulated_instruction(vcpu);
1975 return 1;
1976 }
1977 break;
1978 case 3: /* lmsw */
1979 lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
1980
1981 skip_emulated_instruction(vcpu);
1982 return 1;
1983 default:
1984 break;
1985 }
1986 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10001987 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08001988 (int)(exit_qualification >> 4) & 3, cr);
1989 return 0;
1990}
1991
1992static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1993{
He, Qingbfdaab02007-09-12 14:18:28 +08001994 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001995 unsigned long val;
1996 int dr, reg;
1997
1998 /*
1999 * FIXME: this code assumes the host is debugging the guest.
2000 * need to deal with guest debugging itself too.
2001 */
He, Qingbfdaab02007-09-12 14:18:28 +08002002 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002003 dr = exit_qualification & 7;
2004 reg = (exit_qualification >> 8) & 15;
2005 vcpu_load_rsp_rip(vcpu);
2006 if (exit_qualification & 16) {
2007 /* mov from dr */
2008 switch (dr) {
2009 case 6:
2010 val = 0xffff0ff0;
2011 break;
2012 case 7:
2013 val = 0x400;
2014 break;
2015 default:
2016 val = 0;
2017 }
2018 vcpu->regs[reg] = val;
2019 } else {
2020 /* mov to dr */
2021 }
2022 vcpu_put_rsp_rip(vcpu);
2023 skip_emulated_instruction(vcpu);
2024 return 1;
2025}
2026
2027static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2028{
Avi Kivity06465c52007-02-28 20:46:53 +02002029 kvm_emulate_cpuid(vcpu);
2030 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002031}
2032
2033static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2034{
2035 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
2036 u64 data;
2037
2038 if (vmx_get_msr(vcpu, ecx, &data)) {
2039 vmx_inject_gp(vcpu, 0);
2040 return 1;
2041 }
2042
2043 /* FIXME: handling of bits 32:63 of rax, rdx */
2044 vcpu->regs[VCPU_REGS_RAX] = data & -1u;
2045 vcpu->regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
2046 skip_emulated_instruction(vcpu);
2047 return 1;
2048}
2049
2050static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2051{
2052 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
2053 u64 data = (vcpu->regs[VCPU_REGS_RAX] & -1u)
2054 | ((u64)(vcpu->regs[VCPU_REGS_RDX] & -1u) << 32);
2055
2056 if (vmx_set_msr(vcpu, ecx, data) != 0) {
2057 vmx_inject_gp(vcpu, 0);
2058 return 1;
2059 }
2060
2061 skip_emulated_instruction(vcpu);
2062 return 1;
2063}
2064
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002065static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2066 struct kvm_run *kvm_run)
2067{
2068 return 1;
2069}
2070
Avi Kivity6aa8b732006-12-10 02:21:36 -08002071static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2072 struct kvm_run *kvm_run)
2073{
Eddie Dong85f455f2007-07-06 12:20:49 +03002074 u32 cpu_based_vm_exec_control;
2075
2076 /* clear pending irq */
2077 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2078 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2079 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Dor Laorc1150d82007-01-05 16:36:24 -08002080 /*
2081 * If the user space waits to inject interrupts, exit as soon as
2082 * possible
2083 */
2084 if (kvm_run->request_interrupt_window &&
Dor Laor022a9302007-01-05 16:37:00 -08002085 !vcpu->irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002086 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002087 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002088 return 0;
2089 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090 return 1;
2091}
2092
2093static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2094{
2095 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002096 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002097}
2098
Ingo Molnarc21415e2007-02-19 14:37:47 +02002099static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2100{
Dor Laor510043d2007-02-19 18:25:43 +02002101 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002102 kvm_emulate_hypercall(vcpu);
2103 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02002104}
2105
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106/*
2107 * The exit handlers return 1 if the exit was handled fully and guest execution
2108 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2109 * to be done to userspace and return 0.
2110 */
2111static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
2112 struct kvm_run *kvm_run) = {
2113 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
2114 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08002115 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002116 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117 [EXIT_REASON_CR_ACCESS] = handle_cr,
2118 [EXIT_REASON_DR_ACCESS] = handle_dr,
2119 [EXIT_REASON_CPUID] = handle_cpuid,
2120 [EXIT_REASON_MSR_READ] = handle_rdmsr,
2121 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
2122 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
2123 [EXIT_REASON_HLT] = handle_halt,
Ingo Molnarc21415e2007-02-19 14:37:47 +02002124 [EXIT_REASON_VMCALL] = handle_vmcall,
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002125 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold
Avi Kivity6aa8b732006-12-10 02:21:36 -08002126};
2127
2128static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04002129 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002130
2131/*
2132 * The guest has exited. See if we can fix it or if we need userspace
2133 * assistance.
2134 */
2135static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2136{
2137 u32 vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2138 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity29bd8a72007-09-10 17:27:03 +03002139 struct vcpu_vmx *vmx = to_vmx(vcpu);
2140
2141 if (unlikely(vmx->fail)) {
2142 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2143 kvm_run->fail_entry.hardware_entry_failure_reason
2144 = vmcs_read32(VM_INSTRUCTION_ERROR);
2145 return 0;
2146 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147
2148 if ( (vectoring_info & VECTORING_INFO_VALID_MASK) &&
2149 exit_reason != EXIT_REASON_EXCEPTION_NMI )
2150 printk(KERN_WARNING "%s: unexpected, valid vectoring info and "
2151 "exit reason is 0x%x\n", __FUNCTION__, exit_reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002152 if (exit_reason < kvm_vmx_max_exit_handlers
2153 && kvm_vmx_exit_handlers[exit_reason])
2154 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
2155 else {
2156 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2157 kvm_run->hw.hardware_exit_reason = exit_reason;
2158 }
2159 return 0;
2160}
2161
Avi Kivityd9e368d2007-06-07 19:18:30 +03002162static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
2163{
Avi Kivityd9e368d2007-06-07 19:18:30 +03002164}
2165
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002166static void update_tpr_threshold(struct kvm_vcpu *vcpu)
2167{
2168 int max_irr, tpr;
2169
2170 if (!vm_need_tpr_shadow(vcpu->kvm))
2171 return;
2172
2173 if (!kvm_lapic_enabled(vcpu) ||
2174 ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
2175 vmcs_write32(TPR_THRESHOLD, 0);
2176 return;
2177 }
2178
2179 tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
2180 vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
2181}
2182
Eddie Dong85f455f2007-07-06 12:20:49 +03002183static void enable_irq_window(struct kvm_vcpu *vcpu)
2184{
2185 u32 cpu_based_vm_exec_control;
2186
2187 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2188 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2189 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2190}
2191
2192static void vmx_intr_assist(struct kvm_vcpu *vcpu)
2193{
2194 u32 idtv_info_field, intr_info_field;
2195 int has_ext_irq, interrupt_window_open;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002196 int vector;
Eddie Dong85f455f2007-07-06 12:20:49 +03002197
Eddie Dong1b9778d2007-09-03 16:56:58 +03002198 kvm_inject_pending_timer_irqs(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002199 update_tpr_threshold(vcpu);
2200
Eddie Dong85f455f2007-07-06 12:20:49 +03002201 has_ext_irq = kvm_cpu_has_interrupt(vcpu);
2202 intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
2203 idtv_info_field = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2204 if (intr_info_field & INTR_INFO_VALID_MASK) {
2205 if (idtv_info_field & INTR_INFO_VALID_MASK) {
2206 /* TODO: fault when IDT_Vectoring */
2207 printk(KERN_ERR "Fault when IDT_Vectoring\n");
2208 }
2209 if (has_ext_irq)
2210 enable_irq_window(vcpu);
2211 return;
2212 }
2213 if (unlikely(idtv_info_field & INTR_INFO_VALID_MASK)) {
2214 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, idtv_info_field);
2215 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2216 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
2217
2218 if (unlikely(idtv_info_field & INTR_INFO_DELIEVER_CODE_MASK))
2219 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2220 vmcs_read32(IDT_VECTORING_ERROR_CODE));
2221 if (unlikely(has_ext_irq))
2222 enable_irq_window(vcpu);
2223 return;
2224 }
2225 if (!has_ext_irq)
2226 return;
2227 interrupt_window_open =
2228 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2229 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002230 if (interrupt_window_open) {
2231 vector = kvm_cpu_get_interrupt(vcpu);
2232 vmx_inject_irq(vcpu, vector);
2233 kvm_timer_intr_post(vcpu, vector);
2234 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03002235 enable_irq_window(vcpu);
2236}
2237
Avi Kivity04d2cc72007-09-10 18:10:54 +03002238static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002239{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002240 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1b6269d2007-10-09 12:12:19 +02002241 u32 intr_info;
Avi Kivitye6adf282007-04-30 16:07:54 +03002242
2243 /*
2244 * Loading guest fpu may have cleared host cr0.ts
2245 */
2246 vmcs_writel(HOST_CR0, read_cr0());
2247
Avi Kivity6aa8b732006-12-10 02:21:36 -08002248 asm (
2249 /* Store host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002250#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002251 "push %%rax; push %%rbx; push %%rdx;"
2252 "push %%rsi; push %%rdi; push %%rbp;"
2253 "push %%r8; push %%r9; push %%r10; push %%r11;"
2254 "push %%r12; push %%r13; push %%r14; push %%r15;"
2255 "push %%rcx \n\t"
2256 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
2257#else
2258 "pusha; push %%ecx \n\t"
2259 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
2260#endif
2261 /* Check if vmlaunch of vmresume is needed */
2262 "cmp $0, %1 \n\t"
2263 /* Load guest registers. Don't clobber flags. */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002264#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265 "mov %c[cr2](%3), %%rax \n\t"
2266 "mov %%rax, %%cr2 \n\t"
2267 "mov %c[rax](%3), %%rax \n\t"
2268 "mov %c[rbx](%3), %%rbx \n\t"
2269 "mov %c[rdx](%3), %%rdx \n\t"
2270 "mov %c[rsi](%3), %%rsi \n\t"
2271 "mov %c[rdi](%3), %%rdi \n\t"
2272 "mov %c[rbp](%3), %%rbp \n\t"
2273 "mov %c[r8](%3), %%r8 \n\t"
2274 "mov %c[r9](%3), %%r9 \n\t"
2275 "mov %c[r10](%3), %%r10 \n\t"
2276 "mov %c[r11](%3), %%r11 \n\t"
2277 "mov %c[r12](%3), %%r12 \n\t"
2278 "mov %c[r13](%3), %%r13 \n\t"
2279 "mov %c[r14](%3), %%r14 \n\t"
2280 "mov %c[r15](%3), %%r15 \n\t"
2281 "mov %c[rcx](%3), %%rcx \n\t" /* kills %3 (rcx) */
2282#else
2283 "mov %c[cr2](%3), %%eax \n\t"
2284 "mov %%eax, %%cr2 \n\t"
2285 "mov %c[rax](%3), %%eax \n\t"
2286 "mov %c[rbx](%3), %%ebx \n\t"
2287 "mov %c[rdx](%3), %%edx \n\t"
2288 "mov %c[rsi](%3), %%esi \n\t"
2289 "mov %c[rdi](%3), %%edi \n\t"
2290 "mov %c[rbp](%3), %%ebp \n\t"
2291 "mov %c[rcx](%3), %%ecx \n\t" /* kills %3 (ecx) */
2292#endif
2293 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03002294 "jne .Llaunched \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002295 ASM_VMX_VMLAUNCH "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03002296 "jmp .Lkvm_vmx_return \n\t"
2297 ".Llaunched: " ASM_VMX_VMRESUME "\n\t"
2298 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299 /* Save guest registers, load host registers, keep flags */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002300#ifdef CONFIG_X86_64
Ingo Molnar96958232007-02-12 00:54:33 -08002301 "xchg %3, (%%rsp) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002302 "mov %%rax, %c[rax](%3) \n\t"
2303 "mov %%rbx, %c[rbx](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002304 "pushq (%%rsp); popq %c[rcx](%3) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305 "mov %%rdx, %c[rdx](%3) \n\t"
2306 "mov %%rsi, %c[rsi](%3) \n\t"
2307 "mov %%rdi, %c[rdi](%3) \n\t"
2308 "mov %%rbp, %c[rbp](%3) \n\t"
2309 "mov %%r8, %c[r8](%3) \n\t"
2310 "mov %%r9, %c[r9](%3) \n\t"
2311 "mov %%r10, %c[r10](%3) \n\t"
2312 "mov %%r11, %c[r11](%3) \n\t"
2313 "mov %%r12, %c[r12](%3) \n\t"
2314 "mov %%r13, %c[r13](%3) \n\t"
2315 "mov %%r14, %c[r14](%3) \n\t"
2316 "mov %%r15, %c[r15](%3) \n\t"
2317 "mov %%cr2, %%rax \n\t"
2318 "mov %%rax, %c[cr2](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002319 "mov (%%rsp), %3 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002320
2321 "pop %%rcx; pop %%r15; pop %%r14; pop %%r13; pop %%r12;"
2322 "pop %%r11; pop %%r10; pop %%r9; pop %%r8;"
2323 "pop %%rbp; pop %%rdi; pop %%rsi;"
2324 "pop %%rdx; pop %%rbx; pop %%rax \n\t"
2325#else
Ingo Molnar96958232007-02-12 00:54:33 -08002326 "xchg %3, (%%esp) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327 "mov %%eax, %c[rax](%3) \n\t"
2328 "mov %%ebx, %c[rbx](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002329 "pushl (%%esp); popl %c[rcx](%3) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330 "mov %%edx, %c[rdx](%3) \n\t"
2331 "mov %%esi, %c[rsi](%3) \n\t"
2332 "mov %%edi, %c[rdi](%3) \n\t"
2333 "mov %%ebp, %c[rbp](%3) \n\t"
2334 "mov %%cr2, %%eax \n\t"
2335 "mov %%eax, %c[cr2](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002336 "mov (%%esp), %3 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002337
2338 "pop %%ecx; popa \n\t"
2339#endif
2340 "setbe %0 \n\t"
Avi Kivity29bd8a72007-09-10 17:27:03 +03002341 : "=q" (vmx->fail)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002342 : "r"(vmx->launched), "d"((unsigned long)HOST_RSP),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343 "c"(vcpu),
2344 [rax]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RAX])),
2345 [rbx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBX])),
2346 [rcx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RCX])),
2347 [rdx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDX])),
2348 [rsi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RSI])),
2349 [rdi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDI])),
2350 [rbp]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002351#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002352 [r8 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R8 ])),
2353 [r9 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R9 ])),
2354 [r10]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R10])),
2355 [r11]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R11])),
2356 [r12]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R12])),
2357 [r13]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R13])),
2358 [r14]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R14])),
2359 [r15]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R15])),
2360#endif
2361 [cr2]"i"(offsetof(struct kvm_vcpu, cr2))
2362 : "cc", "memory" );
2363
Dor Laorc1150d82007-01-05 16:36:24 -08002364 vcpu->interrupt_window_open = (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002365
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366 asm ("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03002367 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02002368
2369 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2370
2371 /* We need to handle NMIs before interrupts are enabled */
2372 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
2373 asm("int $2");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374}
2375
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376static void vmx_inject_page_fault(struct kvm_vcpu *vcpu,
2377 unsigned long addr,
2378 u32 err_code)
2379{
2380 u32 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2381
Avi Kivity1165f5f2007-04-19 17:27:43 +03002382 ++vcpu->stat.pf_guest;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383
2384 if (is_page_fault(vect_info)) {
2385 printk(KERN_DEBUG "inject_page_fault: "
2386 "double fault 0x%lx @ 0x%lx\n",
2387 addr, vmcs_readl(GUEST_RIP));
2388 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
2389 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2390 DF_VECTOR |
2391 INTR_TYPE_EXCEPTION |
2392 INTR_INFO_DELIEVER_CODE_MASK |
2393 INTR_INFO_VALID_MASK);
2394 return;
2395 }
2396 vcpu->cr2 = addr;
2397 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, err_code);
2398 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2399 PF_VECTOR |
2400 INTR_TYPE_EXCEPTION |
2401 INTR_INFO_DELIEVER_CODE_MASK |
2402 INTR_INFO_VALID_MASK);
2403
2404}
2405
2406static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
2407{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002408 struct vcpu_vmx *vmx = to_vmx(vcpu);
2409
2410 if (vmx->vmcs) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002411 on_each_cpu(__vcpu_clear, vmx, 0, 1);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002412 free_vmcs(vmx->vmcs);
2413 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002414 }
2415}
2416
2417static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
2418{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002419 struct vcpu_vmx *vmx = to_vmx(vcpu);
2420
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002422 kfree(vmx->host_msrs);
2423 kfree(vmx->guest_msrs);
2424 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10002425 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426}
2427
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002428static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002430 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10002431 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03002432 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002434 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002435 return ERR_PTR(-ENOMEM);
2436
2437 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
2438 if (err)
2439 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002440
Eddie Dong97222cc2007-09-12 10:58:04 +03002441 if (irqchip_in_kernel(kvm)) {
2442 err = kvm_create_lapic(&vmx->vcpu);
2443 if (err < 0)
2444 goto free_vcpu;
2445 }
2446
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002447 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002448 if (!vmx->guest_msrs) {
2449 err = -ENOMEM;
2450 goto uninit_vcpu;
2451 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08002452
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002453 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
2454 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002455 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002456
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002457 vmx->vmcs = alloc_vmcs();
2458 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002459 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002460
2461 vmcs_clear(vmx->vmcs);
2462
Avi Kivity15ad7142007-07-11 18:17:21 +03002463 cpu = get_cpu();
2464 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002465 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002466 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002467 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002468 if (err)
2469 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002470
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002471 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002472
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002473free_vmcs:
2474 free_vmcs(vmx->vmcs);
2475free_msrs:
2476 kfree(vmx->host_msrs);
2477free_guest_msrs:
2478 kfree(vmx->guest_msrs);
2479uninit_vcpu:
2480 kvm_vcpu_uninit(&vmx->vcpu);
2481free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10002482 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002483 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002484}
2485
Yang, Sheng002c7f72007-07-31 14:23:01 +03002486static void __init vmx_check_processor_compat(void *rtn)
2487{
2488 struct vmcs_config vmcs_conf;
2489
2490 *(int *)rtn = 0;
2491 if (setup_vmcs_config(&vmcs_conf) < 0)
2492 *(int *)rtn = -EIO;
2493 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
2494 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
2495 smp_processor_id());
2496 *(int *)rtn = -EIO;
2497 }
2498}
2499
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002500static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501 .cpu_has_kvm_support = cpu_has_kvm_support,
2502 .disabled_by_bios = vmx_disabled_by_bios,
2503 .hardware_setup = hardware_setup,
2504 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03002505 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002506 .hardware_enable = hardware_enable,
2507 .hardware_disable = hardware_disable,
2508
2509 .vcpu_create = vmx_create_vcpu,
2510 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002511 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002512
Avi Kivity04d2cc72007-09-10 18:10:54 +03002513 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002514 .vcpu_load = vmx_vcpu_load,
2515 .vcpu_put = vmx_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08002516 .vcpu_decache = vmx_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517
2518 .set_guest_debug = set_guest_debug,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002519 .guest_debug_pre = kvm_guest_debug_pre,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002520 .get_msr = vmx_get_msr,
2521 .set_msr = vmx_set_msr,
2522 .get_segment_base = vmx_get_segment_base,
2523 .get_segment = vmx_get_segment,
2524 .set_segment = vmx_set_segment,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002525 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03002526 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002527 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528 .set_cr3 = vmx_set_cr3,
2529 .set_cr4 = vmx_set_cr4,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002530#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002531 .set_efer = vmx_set_efer,
2532#endif
2533 .get_idt = vmx_get_idt,
2534 .set_idt = vmx_set_idt,
2535 .get_gdt = vmx_get_gdt,
2536 .set_gdt = vmx_set_gdt,
2537 .cache_regs = vcpu_load_rsp_rip,
2538 .decache_regs = vcpu_put_rsp_rip,
2539 .get_rflags = vmx_get_rflags,
2540 .set_rflags = vmx_set_rflags,
2541
2542 .tlb_flush = vmx_flush_tlb,
2543 .inject_page_fault = vmx_inject_page_fault,
2544
2545 .inject_gp = vmx_inject_gp,
2546
2547 .run = vmx_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002548 .handle_exit = kvm_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002549 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02002550 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03002551 .get_irq = vmx_get_irq,
2552 .set_irq = vmx_inject_irq,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002553 .inject_pending_irq = vmx_intr_assist,
2554 .inject_pending_vectors = do_interrupt_requests,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555};
2556
2557static int __init vmx_init(void)
2558{
He, Qingfdef3ad2007-04-30 09:45:24 +03002559 void *iova;
2560 int r;
2561
2562 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2563 if (!vmx_io_bitmap_a)
2564 return -ENOMEM;
2565
2566 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2567 if (!vmx_io_bitmap_b) {
2568 r = -ENOMEM;
2569 goto out;
2570 }
2571
2572 /*
2573 * Allow direct access to the PC debug port (it is often used for I/O
2574 * delays, but the vmexits simply slow things down).
2575 */
2576 iova = kmap(vmx_io_bitmap_a);
2577 memset(iova, 0xff, PAGE_SIZE);
2578 clear_bit(0x80, iova);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002579 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03002580
2581 iova = kmap(vmx_io_bitmap_b);
2582 memset(iova, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002583 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03002584
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002585 r = kvm_init_x86(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03002586 if (r)
2587 goto out1;
2588
Avi Kivityc7addb92007-09-16 18:58:32 +02002589 if (bypass_guest_pf)
2590 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
2591
He, Qingfdef3ad2007-04-30 09:45:24 +03002592 return 0;
2593
2594out1:
2595 __free_page(vmx_io_bitmap_b);
2596out:
2597 __free_page(vmx_io_bitmap_a);
2598 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002599}
2600
2601static void __exit vmx_exit(void)
2602{
He, Qingfdef3ad2007-04-30 09:45:24 +03002603 __free_page(vmx_io_bitmap_b);
2604 __free_page(vmx_io_bitmap_a);
2605
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002606 kvm_exit_x86();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002607}
2608
2609module_init(vmx_init)
2610module_exit(vmx_exit)