blob: 87ff351288254b2860486faa58c6e5b57cd05497 [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"
Zhang Xiantao34c16ee2007-10-20 15:34:38 +080019#include "x86.h"
Laurent Viviere7d5d762007-07-30 13:41:19 +030020#include "x86_emulate.h"
Eddie Dong85f455f2007-07-06 12:20:49 +030021#include "irq.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include "vmx.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023#include "segment_descriptor.h"
24
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Avi Kivitye4956062007-06-28 14:15:57 -040031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080033#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080034
Avi Kivity6aa8b732006-12-10 02:21:36 -080035MODULE_AUTHOR("Qumranet");
36MODULE_LICENSE("GPL");
37
Avi Kivityc7addb92007-09-16 18:58:32 +020038static int bypass_guest_pf = 1;
39module_param(bypass_guest_pf, bool, 0);
40
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040041struct vmcs {
42 u32 revision_id;
43 u32 abort;
44 char data[0];
45};
46
47struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100048 struct kvm_vcpu vcpu;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040049 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030050 u8 fail;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040051 struct kvm_msr_entry *guest_msrs;
52 struct kvm_msr_entry *host_msrs;
53 int nmsrs;
54 int save_nmsrs;
55 int msr_offset_efer;
56#ifdef CONFIG_X86_64
57 int msr_offset_kernel_gs_base;
58#endif
59 struct vmcs *vmcs;
60 struct {
61 int loaded;
62 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020063 int gs_ldt_reload_needed;
64 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030065 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040066 } host_state;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040067
68};
69
70static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
71{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100072 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040073}
74
Avi Kivity75880a02007-06-20 11:20:04 +030075static int init_rmode_tss(struct kvm *kvm);
76
Avi Kivity6aa8b732006-12-10 02:21:36 -080077static DEFINE_PER_CPU(struct vmcs *, vmxarea);
78static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
79
He, Qingfdef3ad2007-04-30 09:45:24 +030080static struct page *vmx_io_bitmap_a;
81static struct page *vmx_io_bitmap_b;
82
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +030083static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -080084 int size;
85 int order;
86 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +030087 u32 pin_based_exec_ctrl;
88 u32 cpu_based_exec_ctrl;
89 u32 vmexit_ctrl;
90 u32 vmentry_ctrl;
91} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -080092
93#define VMX_SEGMENT_FIELD(seg) \
94 [VCPU_SREG_##seg] = { \
95 .selector = GUEST_##seg##_SELECTOR, \
96 .base = GUEST_##seg##_BASE, \
97 .limit = GUEST_##seg##_LIMIT, \
98 .ar_bytes = GUEST_##seg##_AR_BYTES, \
99 }
100
101static struct kvm_vmx_segment_field {
102 unsigned selector;
103 unsigned base;
104 unsigned limit;
105 unsigned ar_bytes;
106} kvm_vmx_segment_fields[] = {
107 VMX_SEGMENT_FIELD(CS),
108 VMX_SEGMENT_FIELD(DS),
109 VMX_SEGMENT_FIELD(ES),
110 VMX_SEGMENT_FIELD(FS),
111 VMX_SEGMENT_FIELD(GS),
112 VMX_SEGMENT_FIELD(SS),
113 VMX_SEGMENT_FIELD(TR),
114 VMX_SEGMENT_FIELD(LDTR),
115};
116
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300117/*
118 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
119 * away by decrementing the array size.
120 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800122#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
124#endif
125 MSR_EFER, MSR_K6_STAR,
126};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200127#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800128
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400129static void load_msrs(struct kvm_msr_entry *e, int n)
130{
131 int i;
132
133 for (i = 0; i < n; ++i)
134 wrmsrl(e[i].index, e[i].data);
135}
136
137static void save_msrs(struct kvm_msr_entry *e, int n)
138{
139 int i;
140
141 for (i = 0; i < n; ++i)
142 rdmsrl(e[i].index, e[i].data);
143}
144
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145static inline int is_page_fault(u32 intr_info)
146{
147 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
148 INTR_INFO_VALID_MASK)) ==
149 (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
150}
151
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300152static inline int is_no_device(u32 intr_info)
153{
154 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
155 INTR_INFO_VALID_MASK)) ==
156 (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
157}
158
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500159static inline int is_invalid_opcode(u32 intr_info)
160{
161 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
162 INTR_INFO_VALID_MASK)) ==
163 (INTR_TYPE_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
164}
165
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166static inline int is_external_interrupt(u32 intr_info)
167{
168 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
169 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
170}
171
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800172static inline int cpu_has_vmx_tpr_shadow(void)
173{
174 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW);
175}
176
177static inline int vm_need_tpr_shadow(struct kvm *kvm)
178{
179 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)));
180}
181
Rusty Russell8b9cf982007-07-30 16:31:43 +1000182static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800183{
184 int i;
185
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400186 for (i = 0; i < vmx->nmsrs; ++i)
187 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300188 return i;
189 return -1;
190}
191
Rusty Russell8b9cf982007-07-30 16:31:43 +1000192static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300193{
194 int i;
195
Rusty Russell8b9cf982007-07-30 16:31:43 +1000196 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300197 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400198 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000199 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800200}
201
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202static void vmcs_clear(struct vmcs *vmcs)
203{
204 u64 phys_addr = __pa(vmcs);
205 u8 error;
206
207 asm volatile (ASM_VMX_VMCLEAR_RAX "; setna %0"
208 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
209 : "cc", "memory");
210 if (error)
211 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
212 vmcs, phys_addr);
213}
214
215static void __vcpu_clear(void *arg)
216{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000217 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800218 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800219
Rusty Russell8b9cf982007-07-30 16:31:43 +1000220 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400221 vmcs_clear(vmx->vmcs);
222 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223 per_cpu(current_vmcs, cpu) = NULL;
Rusty Russell8b9cf982007-07-30 16:31:43 +1000224 rdtscll(vmx->vcpu.host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225}
226
Rusty Russell8b9cf982007-07-30 16:31:43 +1000227static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800228{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200229 if (vmx->vcpu.cpu == -1)
230 return;
Avi Kivityf566e092007-09-30 11:02:53 +0200231 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 0, 1);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000232 vmx->launched = 0;
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800233}
234
Avi Kivity6aa8b732006-12-10 02:21:36 -0800235static unsigned long vmcs_readl(unsigned long field)
236{
237 unsigned long value;
238
239 asm volatile (ASM_VMX_VMREAD_RDX_RAX
240 : "=a"(value) : "d"(field) : "cc");
241 return value;
242}
243
244static u16 vmcs_read16(unsigned long field)
245{
246 return vmcs_readl(field);
247}
248
249static u32 vmcs_read32(unsigned long field)
250{
251 return vmcs_readl(field);
252}
253
254static u64 vmcs_read64(unsigned long field)
255{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800256#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800257 return vmcs_readl(field);
258#else
259 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
260#endif
261}
262
Avi Kivitye52de1b2007-01-05 16:36:56 -0800263static noinline void vmwrite_error(unsigned long field, unsigned long value)
264{
265 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
266 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
267 dump_stack();
268}
269
Avi Kivity6aa8b732006-12-10 02:21:36 -0800270static void vmcs_writel(unsigned long field, unsigned long value)
271{
272 u8 error;
273
274 asm volatile (ASM_VMX_VMWRITE_RAX_RDX "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400275 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800276 if (unlikely(error))
277 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278}
279
280static void vmcs_write16(unsigned long field, u16 value)
281{
282 vmcs_writel(field, value);
283}
284
285static void vmcs_write32(unsigned long field, u32 value)
286{
287 vmcs_writel(field, value);
288}
289
290static void vmcs_write64(unsigned long field, u64 value)
291{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800292#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293 vmcs_writel(field, value);
294#else
295 vmcs_writel(field, value);
296 asm volatile ("");
297 vmcs_writel(field+1, value >> 32);
298#endif
299}
300
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300301static void vmcs_clear_bits(unsigned long field, u32 mask)
302{
303 vmcs_writel(field, vmcs_readl(field) & ~mask);
304}
305
306static void vmcs_set_bits(unsigned long field, u32 mask)
307{
308 vmcs_writel(field, vmcs_readl(field) | mask);
309}
310
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300311static void update_exception_bitmap(struct kvm_vcpu *vcpu)
312{
313 u32 eb;
314
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500315 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300316 if (!vcpu->fpu_active)
317 eb |= 1u << NM_VECTOR;
318 if (vcpu->guest_debug.enabled)
319 eb |= 1u << 1;
320 if (vcpu->rmode.active)
321 eb = ~0;
322 vmcs_write32(EXCEPTION_BITMAP, eb);
323}
324
Avi Kivity33ed6322007-05-02 16:54:03 +0300325static void reload_tss(void)
326{
327#ifndef CONFIG_X86_64
328
329 /*
330 * VT restores TR but not its size. Useless.
331 */
332 struct descriptor_table gdt;
333 struct segment_descriptor *descs;
334
335 get_gdt(&gdt);
336 descs = (void *)gdt.base;
337 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
338 load_TR_desc();
339#endif
340}
341
Rusty Russell8b9cf982007-07-30 16:31:43 +1000342static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300343{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400344 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300345 u64 host_efer = vmx->host_msrs[efer_offset].data;
346 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
347 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300348
Avi Kivity51c6cf62007-08-29 03:48:05 +0300349 if (efer_offset < 0)
350 return;
351 /*
352 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
353 * outside long mode
354 */
355 ignore_bits = EFER_NX | EFER_SCE;
356#ifdef CONFIG_X86_64
357 ignore_bits |= EFER_LMA | EFER_LME;
358 /* SCE is meaningful only in long mode on Intel */
359 if (guest_efer & EFER_LMA)
360 ignore_bits &= ~(u64)EFER_SCE;
361#endif
362 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
363 return;
364
365 vmx->host_state.guest_efer_loaded = 1;
366 guest_efer &= ~ignore_bits;
367 guest_efer |= host_efer & ignore_bits;
368 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000369 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300370}
371
Avi Kivity51c6cf62007-08-29 03:48:05 +0300372static void reload_host_efer(struct vcpu_vmx *vmx)
373{
374 if (vmx->host_state.guest_efer_loaded) {
375 vmx->host_state.guest_efer_loaded = 0;
376 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
377 }
378}
379
Avi Kivity04d2cc72007-09-10 18:10:54 +0300380static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300381{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300382 struct vcpu_vmx *vmx = to_vmx(vcpu);
383
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400384 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300385 return;
386
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400387 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300388 /*
389 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
390 * allow segment selectors with cpl > 0 or ti == 1.
391 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400392 vmx->host_state.ldt_sel = read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200393 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400394 vmx->host_state.fs_sel = read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200395 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400396 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200397 vmx->host_state.fs_reload_needed = 0;
398 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300399 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200400 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300401 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400402 vmx->host_state.gs_sel = read_gs();
403 if (!(vmx->host_state.gs_sel & 7))
404 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300405 else {
406 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200407 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300408 }
409
410#ifdef CONFIG_X86_64
411 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
412 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
413#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400414 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
415 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300416#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300417
418#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400419 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400420 save_msrs(vmx->host_msrs +
421 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400422
Avi Kivity707c0872007-05-02 17:33:43 +0300423#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400424 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300425 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300426}
427
Rusty Russell8b9cf982007-07-30 16:31:43 +1000428static void vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300429{
Avi Kivity15ad7142007-07-11 18:17:21 +0300430 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300431
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400432 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300433 return;
434
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400435 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200436 if (vmx->host_state.fs_reload_needed)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400437 load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200438 if (vmx->host_state.gs_ldt_reload_needed) {
439 load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300440 /*
441 * If we have to reload gs, we must take care to
442 * preserve our gs base.
443 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300444 local_irq_save(flags);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400445 load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300446#ifdef CONFIG_X86_64
447 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
448#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300449 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300450 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200451 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400452 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
453 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300454 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300455}
456
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457/*
458 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
459 * vcpu mutex is already taken.
460 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300461static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400463 struct vcpu_vmx *vmx = to_vmx(vcpu);
464 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity77002702007-06-13 19:55:28 +0300465 u64 tsc_this, delta;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800466
Eddie Donga3d7f852007-09-03 16:15:12 +0300467 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000468 vcpu_clear(vmx);
Eddie Donga3d7f852007-09-03 16:15:12 +0300469 kvm_migrate_apic_timer(vcpu);
470 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800471
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400472 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473 u8 error;
474
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400475 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800476 asm volatile (ASM_VMX_VMPTRLD_RAX "; setna %0"
477 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
478 : "cc");
479 if (error)
480 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400481 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482 }
483
484 if (vcpu->cpu != cpu) {
485 struct descriptor_table dt;
486 unsigned long sysenter_esp;
487
488 vcpu->cpu = cpu;
489 /*
490 * Linux uses per-cpu TSS and GDT, so set these when switching
491 * processors.
492 */
493 vmcs_writel(HOST_TR_BASE, read_tr_base()); /* 22.2.4 */
494 get_gdt(&dt);
495 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
496
497 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
498 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300499
500 /*
501 * Make sure the time stamp counter is monotonous.
502 */
503 rdtscll(tsc_this);
504 delta = vcpu->host_tsc - tsc_this;
505 vmcs_write64(TSC_OFFSET, vmcs_read64(TSC_OFFSET) + delta);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800507}
508
509static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
510{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000511 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity7702fd12007-06-14 16:27:40 +0300512 kvm_put_guest_fpu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800513}
514
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300515static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
516{
517 if (vcpu->fpu_active)
518 return;
519 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000520 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
521 if (vcpu->cr0 & X86_CR0_TS)
522 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300523 update_exception_bitmap(vcpu);
524}
525
526static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
527{
528 if (!vcpu->fpu_active)
529 return;
530 vcpu->fpu_active = 0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000531 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300532 update_exception_bitmap(vcpu);
533}
534
Avi Kivity774c47f2007-02-12 00:54:47 -0800535static void vmx_vcpu_decache(struct kvm_vcpu *vcpu)
536{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000537 vcpu_clear(to_vmx(vcpu));
Avi Kivity774c47f2007-02-12 00:54:47 -0800538}
539
Avi Kivity6aa8b732006-12-10 02:21:36 -0800540static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
541{
542 return vmcs_readl(GUEST_RFLAGS);
543}
544
545static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
546{
Avi Kivity78f78268682007-10-16 19:06:15 +0200547 if (vcpu->rmode.active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100548 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800549 vmcs_writel(GUEST_RFLAGS, rflags);
550}
551
552static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
553{
554 unsigned long rip;
555 u32 interruptibility;
556
557 rip = vmcs_readl(GUEST_RIP);
558 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
559 vmcs_writel(GUEST_RIP, rip);
560
561 /*
562 * We emulated an instruction, so temporary interrupt blocking
563 * should be removed, if set.
564 */
565 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
566 if (interruptibility & 3)
567 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
568 interruptibility & ~3);
Dor Laorc1150d82007-01-05 16:36:24 -0800569 vcpu->interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570}
571
572static void vmx_inject_gp(struct kvm_vcpu *vcpu, unsigned error_code)
573{
574 printk(KERN_DEBUG "inject_general_protection: rip 0x%lx\n",
575 vmcs_readl(GUEST_RIP));
576 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
577 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
578 GP_VECTOR |
579 INTR_TYPE_EXCEPTION |
580 INTR_INFO_DELIEVER_CODE_MASK |
581 INTR_INFO_VALID_MASK);
582}
583
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500584static void vmx_inject_ud(struct kvm_vcpu *vcpu)
585{
586 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
587 UD_VECTOR |
588 INTR_TYPE_EXCEPTION |
589 INTR_INFO_VALID_MASK);
590}
591
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592/*
Eddie Donga75beee2007-05-17 18:55:15 +0300593 * Swap MSR entry in host/guest MSR entry array.
594 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200595#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000596static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300597{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400598 struct kvm_msr_entry tmp;
599
600 tmp = vmx->guest_msrs[to];
601 vmx->guest_msrs[to] = vmx->guest_msrs[from];
602 vmx->guest_msrs[from] = tmp;
603 tmp = vmx->host_msrs[to];
604 vmx->host_msrs[to] = vmx->host_msrs[from];
605 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300606}
Gabriel C54e11fa2007-08-01 16:23:10 +0200607#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300608
609/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300610 * Set up the vmcs to automatically save and restore system
611 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
612 * mode, as fiddling with msrs is very expensive.
613 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000614static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300615{
Eddie Dong2cc51562007-05-21 07:28:09 +0300616 int save_nmsrs;
Avi Kivitye38aea32007-04-19 13:22:48 +0300617
Eddie Donga75beee2007-05-17 18:55:15 +0300618 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300619#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000620 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300621 int index;
622
Rusty Russell8b9cf982007-07-30 16:31:43 +1000623 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300624 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000625 move_msr_up(vmx, index, save_nmsrs++);
626 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300627 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000628 move_msr_up(vmx, index, save_nmsrs++);
629 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300630 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000631 move_msr_up(vmx, index, save_nmsrs++);
632 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300633 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000634 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300635 /*
636 * MSR_K6_STAR is only needed on long mode guests, and only
637 * if efer.sce is enabled.
638 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000639 index = __find_msr_index(vmx, MSR_K6_STAR);
640 if ((index >= 0) && (vmx->vcpu.shadow_efer & EFER_SCE))
641 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300642 }
Eddie Donga75beee2007-05-17 18:55:15 +0300643#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400644 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300645
Eddie Donga75beee2007-05-17 18:55:15 +0300646#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400647 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000648 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300649#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000650 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivitye38aea32007-04-19 13:22:48 +0300651}
652
653/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800654 * reads and returns guest's timestamp counter "register"
655 * guest_tsc = host_tsc + tsc_offset -- 21.3
656 */
657static u64 guest_read_tsc(void)
658{
659 u64 host_tsc, tsc_offset;
660
661 rdtscll(host_tsc);
662 tsc_offset = vmcs_read64(TSC_OFFSET);
663 return host_tsc + tsc_offset;
664}
665
666/*
667 * writes 'guest_tsc' into guest's timestamp counter "register"
668 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
669 */
670static void guest_write_tsc(u64 guest_tsc)
671{
672 u64 host_tsc;
673
674 rdtscll(host_tsc);
675 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
676}
677
Avi Kivity6aa8b732006-12-10 02:21:36 -0800678/*
679 * Reads an msr value (of 'msr_index') into 'pdata'.
680 * Returns 0 on success, non-0 otherwise.
681 * Assumes vcpu_load() was already called.
682 */
683static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
684{
685 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400686 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800687
688 if (!pdata) {
689 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
690 return -EINVAL;
691 }
692
693 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800694#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800695 case MSR_FS_BASE:
696 data = vmcs_readl(GUEST_FS_BASE);
697 break;
698 case MSR_GS_BASE:
699 data = vmcs_readl(GUEST_GS_BASE);
700 break;
701 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800702 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703#endif
704 case MSR_IA32_TIME_STAMP_COUNTER:
705 data = guest_read_tsc();
706 break;
707 case MSR_IA32_SYSENTER_CS:
708 data = vmcs_read32(GUEST_SYSENTER_CS);
709 break;
710 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200711 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712 break;
713 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200714 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000717 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800718 if (msr) {
719 data = msr->data;
720 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800722 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723 }
724
725 *pdata = data;
726 return 0;
727}
728
729/*
730 * Writes msr value into into the appropriate "register".
731 * Returns 0 on success, non-0 otherwise.
732 * Assumes vcpu_load() was already called.
733 */
734static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
735{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400736 struct vcpu_vmx *vmx = to_vmx(vcpu);
737 struct kvm_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +0300738 int ret = 0;
739
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800741#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800742 case MSR_EFER:
Eddie Dong2cc51562007-05-21 07:28:09 +0300743 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300744 if (vmx->host_state.loaded) {
745 reload_host_efer(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000746 load_transition_efer(vmx);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300747 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300748 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749 case MSR_FS_BASE:
750 vmcs_writel(GUEST_FS_BASE, data);
751 break;
752 case MSR_GS_BASE:
753 vmcs_writel(GUEST_GS_BASE, data);
754 break;
755#endif
756 case MSR_IA32_SYSENTER_CS:
757 vmcs_write32(GUEST_SYSENTER_CS, data);
758 break;
759 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200760 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761 break;
762 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200763 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200765 case MSR_IA32_TIME_STAMP_COUNTER:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766 guest_write_tsc(data);
767 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000769 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800770 if (msr) {
771 msr->data = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400772 if (vmx->host_state.loaded)
773 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800774 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300776 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777 }
778
Eddie Dong2cc51562007-05-21 07:28:09 +0300779 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800780}
781
782/*
783 * Sync the rsp and rip registers into the vcpu structure. This allows
784 * registers to be accessed by indexing vcpu->regs.
785 */
786static void vcpu_load_rsp_rip(struct kvm_vcpu *vcpu)
787{
788 vcpu->regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
789 vcpu->rip = vmcs_readl(GUEST_RIP);
790}
791
792/*
793 * Syncs rsp and rip back into the vmcs. Should be called after possible
794 * modification.
795 */
796static void vcpu_put_rsp_rip(struct kvm_vcpu *vcpu)
797{
798 vmcs_writel(GUEST_RSP, vcpu->regs[VCPU_REGS_RSP]);
799 vmcs_writel(GUEST_RIP, vcpu->rip);
800}
801
802static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
803{
804 unsigned long dr7 = 0x400;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800805 int old_singlestep;
806
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807 old_singlestep = vcpu->guest_debug.singlestep;
808
809 vcpu->guest_debug.enabled = dbg->enabled;
810 if (vcpu->guest_debug.enabled) {
811 int i;
812
813 dr7 |= 0x200; /* exact */
814 for (i = 0; i < 4; ++i) {
815 if (!dbg->breakpoints[i].enabled)
816 continue;
817 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
818 dr7 |= 2 << (i*2); /* global enable */
819 dr7 |= 0 << (i*4+16); /* execution breakpoint */
820 }
821
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822 vcpu->guest_debug.singlestep = dbg->singlestep;
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300823 } else
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824 vcpu->guest_debug.singlestep = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825
826 if (old_singlestep && !vcpu->guest_debug.singlestep) {
827 unsigned long flags;
828
829 flags = vmcs_readl(GUEST_RFLAGS);
830 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
831 vmcs_writel(GUEST_RFLAGS, flags);
832 }
833
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300834 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835 vmcs_writel(GUEST_DR7, dr7);
836
837 return 0;
838}
839
Eddie Dong2a8067f2007-08-06 16:29:07 +0300840static int vmx_get_irq(struct kvm_vcpu *vcpu)
841{
842 u32 idtv_info_field;
843
844 idtv_info_field = vmcs_read32(IDT_VECTORING_INFO_FIELD);
845 if (idtv_info_field & INTR_INFO_VALID_MASK) {
846 if (is_external_interrupt(idtv_info_field))
847 return idtv_info_field & VECTORING_INFO_VECTOR_MASK;
848 else
Mike Dayd77c26f2007-10-08 09:02:08 -0400849 printk(KERN_DEBUG "pending exception: not handled yet\n");
Eddie Dong2a8067f2007-08-06 16:29:07 +0300850 }
851 return -1;
852}
853
Avi Kivity6aa8b732006-12-10 02:21:36 -0800854static __init int cpu_has_kvm_support(void)
855{
856 unsigned long ecx = cpuid_ecx(1);
857 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
858}
859
860static __init int vmx_disabled_by_bios(void)
861{
862 u64 msr;
863
864 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300865 return (msr & (MSR_IA32_FEATURE_CONTROL_LOCKED |
866 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
867 == MSR_IA32_FEATURE_CONTROL_LOCKED;
868 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869}
870
Avi Kivity774c47f2007-02-12 00:54:47 -0800871static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800872{
873 int cpu = raw_smp_processor_id();
874 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
875 u64 old;
876
877 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300878 if ((old & (MSR_IA32_FEATURE_CONTROL_LOCKED |
879 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
880 != (MSR_IA32_FEATURE_CONTROL_LOCKED |
881 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800882 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300883 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
884 MSR_IA32_FEATURE_CONTROL_LOCKED |
885 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +1000886 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887 asm volatile (ASM_VMX_VMXON_RAX : : "a"(&phys_addr), "m"(phys_addr)
888 : "memory", "cc");
889}
890
891static void hardware_disable(void *garbage)
892{
893 asm volatile (ASM_VMX_VMXOFF : : : "cc");
894}
895
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300896static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -0400897 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898{
899 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300900 u32 ctl = ctl_min | ctl_opt;
901
902 rdmsr(msr, vmx_msr_low, vmx_msr_high);
903
904 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
905 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
906
907 /* Ensure minimum (required) set of control bits are supported. */
908 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300909 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300910
911 *result = ctl;
912 return 0;
913}
914
Yang, Sheng002c7f72007-07-31 14:23:01 +0300915static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300916{
917 u32 vmx_msr_low, vmx_msr_high;
918 u32 min, opt;
919 u32 _pin_based_exec_control = 0;
920 u32 _cpu_based_exec_control = 0;
921 u32 _vmexit_control = 0;
922 u32 _vmentry_control = 0;
923
924 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
925 opt = 0;
926 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
927 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300928 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300929
930 min = CPU_BASED_HLT_EXITING |
931#ifdef CONFIG_X86_64
932 CPU_BASED_CR8_LOAD_EXITING |
933 CPU_BASED_CR8_STORE_EXITING |
934#endif
935 CPU_BASED_USE_IO_BITMAPS |
936 CPU_BASED_MOV_DR_EXITING |
937 CPU_BASED_USE_TSC_OFFSETING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800938#ifdef CONFIG_X86_64
939 opt = CPU_BASED_TPR_SHADOW;
940#else
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300941 opt = 0;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800942#endif
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300943 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
944 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300945 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800946#ifdef CONFIG_X86_64
947 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
948 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
949 ~CPU_BASED_CR8_STORE_EXITING;
950#endif
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300951
952 min = 0;
953#ifdef CONFIG_X86_64
954 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
955#endif
956 opt = 0;
957 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
958 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300959 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300960
961 min = opt = 0;
962 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
963 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300964 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -0800966 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300967
968 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
969 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300970 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300971
972#ifdef CONFIG_X86_64
973 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
974 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +0300975 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300976#endif
977
978 /* Require Write-Back (WB) memory type for VMCS accesses. */
979 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300980 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300981
Yang, Sheng002c7f72007-07-31 14:23:01 +0300982 vmcs_conf->size = vmx_msr_high & 0x1fff;
983 vmcs_conf->order = get_order(vmcs_config.size);
984 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300985
Yang, Sheng002c7f72007-07-31 14:23:01 +0300986 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
987 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
988 vmcs_conf->vmexit_ctrl = _vmexit_control;
989 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300990
991 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -0800992}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993
994static struct vmcs *alloc_vmcs_cpu(int cpu)
995{
996 int node = cpu_to_node(cpu);
997 struct page *pages;
998 struct vmcs *vmcs;
999
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001000 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001 if (!pages)
1002 return NULL;
1003 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001004 memset(vmcs, 0, vmcs_config.size);
1005 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006 return vmcs;
1007}
1008
1009static struct vmcs *alloc_vmcs(void)
1010{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001011 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012}
1013
1014static void free_vmcs(struct vmcs *vmcs)
1015{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001016 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017}
1018
Sam Ravnborg39959582007-06-01 00:47:13 -07001019static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020{
1021 int cpu;
1022
1023 for_each_online_cpu(cpu)
1024 free_vmcs(per_cpu(vmxarea, cpu));
1025}
1026
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027static __init int alloc_kvm_area(void)
1028{
1029 int cpu;
1030
1031 for_each_online_cpu(cpu) {
1032 struct vmcs *vmcs;
1033
1034 vmcs = alloc_vmcs_cpu(cpu);
1035 if (!vmcs) {
1036 free_kvm_area();
1037 return -ENOMEM;
1038 }
1039
1040 per_cpu(vmxarea, cpu) = vmcs;
1041 }
1042 return 0;
1043}
1044
1045static __init int hardware_setup(void)
1046{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001047 if (setup_vmcs_config(&vmcs_config) < 0)
1048 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001049 return alloc_kvm_area();
1050}
1051
1052static __exit void hardware_unsetup(void)
1053{
1054 free_kvm_area();
1055}
1056
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1058{
1059 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1060
Avi Kivity6af11b92007-03-19 13:18:10 +02001061 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001062 vmcs_write16(sf->selector, save->selector);
1063 vmcs_writel(sf->base, save->base);
1064 vmcs_write32(sf->limit, save->limit);
1065 vmcs_write32(sf->ar_bytes, save->ar);
1066 } else {
1067 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1068 << AR_DPL_SHIFT;
1069 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1070 }
1071}
1072
1073static void enter_pmode(struct kvm_vcpu *vcpu)
1074{
1075 unsigned long flags;
1076
1077 vcpu->rmode.active = 0;
1078
1079 vmcs_writel(GUEST_TR_BASE, vcpu->rmode.tr.base);
1080 vmcs_write32(GUEST_TR_LIMIT, vcpu->rmode.tr.limit);
1081 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->rmode.tr.ar);
1082
1083 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001084 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085 flags |= (vcpu->rmode.save_iopl << IOPL_SHIFT);
1086 vmcs_writel(GUEST_RFLAGS, flags);
1087
Rusty Russell66aee912007-07-17 23:34:16 +10001088 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1089 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090
1091 update_exception_bitmap(vcpu);
1092
1093 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->rmode.es);
1094 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->rmode.ds);
1095 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->rmode.gs);
1096 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->rmode.fs);
1097
1098 vmcs_write16(GUEST_SS_SELECTOR, 0);
1099 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1100
1101 vmcs_write16(GUEST_CS_SELECTOR,
1102 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1103 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1104}
1105
Mike Dayd77c26f2007-10-08 09:02:08 -04001106static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107{
1108 gfn_t base_gfn = kvm->memslots[0].base_gfn + kvm->memslots[0].npages - 3;
1109 return base_gfn << PAGE_SHIFT;
1110}
1111
1112static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1113{
1114 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1115
1116 save->selector = vmcs_read16(sf->selector);
1117 save->base = vmcs_readl(sf->base);
1118 save->limit = vmcs_read32(sf->limit);
1119 save->ar = vmcs_read32(sf->ar_bytes);
1120 vmcs_write16(sf->selector, vmcs_readl(sf->base) >> 4);
1121 vmcs_write32(sf->limit, 0xffff);
1122 vmcs_write32(sf->ar_bytes, 0xf3);
1123}
1124
1125static void enter_rmode(struct kvm_vcpu *vcpu)
1126{
1127 unsigned long flags;
1128
1129 vcpu->rmode.active = 1;
1130
1131 vcpu->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
1132 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1133
1134 vcpu->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
1135 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1136
1137 vcpu->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
1138 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1139
1140 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001141 vcpu->rmode.save_iopl = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001143 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144
1145 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001146 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147 update_exception_bitmap(vcpu);
1148
1149 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1150 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1151 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1152
1153 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001154 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001155 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1156 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001157 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1158
1159 fix_rmode_seg(VCPU_SREG_ES, &vcpu->rmode.es);
1160 fix_rmode_seg(VCPU_SREG_DS, &vcpu->rmode.ds);
1161 fix_rmode_seg(VCPU_SREG_GS, &vcpu->rmode.gs);
1162 fix_rmode_seg(VCPU_SREG_FS, &vcpu->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001163
Eddie Dong8668a3c2007-10-10 14:26:45 +08001164 kvm_mmu_reset_context(vcpu);
Avi Kivity75880a02007-06-20 11:20:04 +03001165 init_rmode_tss(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001166}
1167
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001168#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001169
1170static void enter_lmode(struct kvm_vcpu *vcpu)
1171{
1172 u32 guest_tr_ar;
1173
1174 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1175 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1176 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
1177 __FUNCTION__);
1178 vmcs_write32(GUEST_TR_AR_BYTES,
1179 (guest_tr_ar & ~AR_TYPE_MASK)
1180 | AR_TYPE_BUSY_64_TSS);
1181 }
1182
1183 vcpu->shadow_efer |= EFER_LMA;
1184
Rusty Russell8b9cf982007-07-30 16:31:43 +10001185 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186 vmcs_write32(VM_ENTRY_CONTROLS,
1187 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001188 | VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189}
1190
1191static void exit_lmode(struct kvm_vcpu *vcpu)
1192{
1193 vcpu->shadow_efer &= ~EFER_LMA;
1194
1195 vmcs_write32(VM_ENTRY_CONTROLS,
1196 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001197 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198}
1199
1200#endif
1201
Anthony Liguori25c4c272007-04-27 09:29:21 +03001202static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001203{
Avi Kivity399badf2007-01-05 16:36:38 -08001204 vcpu->cr4 &= KVM_GUEST_CR4_MASK;
1205 vcpu->cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
1206}
1207
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1209{
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001210 vmx_fpu_deactivate(vcpu);
1211
Rusty Russell707d92fa2007-07-17 23:19:08 +10001212 if (vcpu->rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001213 enter_pmode(vcpu);
1214
Rusty Russell707d92fa2007-07-17 23:19:08 +10001215 if (!vcpu->rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216 enter_rmode(vcpu);
1217
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001218#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219 if (vcpu->shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001220 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001221 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001222 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223 exit_lmode(vcpu);
1224 }
1225#endif
1226
1227 vmcs_writel(CR0_READ_SHADOW, cr0);
1228 vmcs_writel(GUEST_CR0,
1229 (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON);
1230 vcpu->cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001231
Rusty Russell707d92fa2007-07-17 23:19:08 +10001232 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001233 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001234}
1235
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1237{
1238 vmcs_writel(GUEST_CR3, cr3);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001239 if (vcpu->cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001240 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001241}
1242
1243static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1244{
1245 vmcs_writel(CR4_READ_SHADOW, cr4);
1246 vmcs_writel(GUEST_CR4, cr4 | (vcpu->rmode.active ?
1247 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON));
1248 vcpu->cr4 = cr4;
1249}
1250
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001251#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001252
1253static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1254{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001255 struct vcpu_vmx *vmx = to_vmx(vcpu);
1256 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001257
1258 vcpu->shadow_efer = efer;
1259 if (efer & EFER_LMA) {
1260 vmcs_write32(VM_ENTRY_CONTROLS,
1261 vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001262 VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001263 msr->data = efer;
1264
1265 } else {
1266 vmcs_write32(VM_ENTRY_CONTROLS,
1267 vmcs_read32(VM_ENTRY_CONTROLS) &
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001268 ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001269
1270 msr->data = efer & ~EFER_LME;
1271 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001272 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001273}
1274
1275#endif
1276
1277static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1278{
1279 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1280
1281 return vmcs_readl(sf->base);
1282}
1283
1284static void vmx_get_segment(struct kvm_vcpu *vcpu,
1285 struct kvm_segment *var, int seg)
1286{
1287 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1288 u32 ar;
1289
1290 var->base = vmcs_readl(sf->base);
1291 var->limit = vmcs_read32(sf->limit);
1292 var->selector = vmcs_read16(sf->selector);
1293 ar = vmcs_read32(sf->ar_bytes);
1294 if (ar & AR_UNUSABLE_MASK)
1295 ar = 0;
1296 var->type = ar & 15;
1297 var->s = (ar >> 4) & 1;
1298 var->dpl = (ar >> 5) & 3;
1299 var->present = (ar >> 7) & 1;
1300 var->avl = (ar >> 12) & 1;
1301 var->l = (ar >> 13) & 1;
1302 var->db = (ar >> 14) & 1;
1303 var->g = (ar >> 15) & 1;
1304 var->unusable = (ar >> 16) & 1;
1305}
1306
Avi Kivity653e3102007-05-07 10:55:37 +03001307static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001308{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309 u32 ar;
1310
Avi Kivity653e3102007-05-07 10:55:37 +03001311 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001312 ar = 1 << 16;
1313 else {
1314 ar = var->type & 15;
1315 ar |= (var->s & 1) << 4;
1316 ar |= (var->dpl & 3) << 5;
1317 ar |= (var->present & 1) << 7;
1318 ar |= (var->avl & 1) << 12;
1319 ar |= (var->l & 1) << 13;
1320 ar |= (var->db & 1) << 14;
1321 ar |= (var->g & 1) << 15;
1322 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001323 if (ar == 0) /* a 0 value means unusable */
1324 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001325
1326 return ar;
1327}
1328
1329static void vmx_set_segment(struct kvm_vcpu *vcpu,
1330 struct kvm_segment *var, int seg)
1331{
1332 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1333 u32 ar;
1334
1335 if (vcpu->rmode.active && seg == VCPU_SREG_TR) {
1336 vcpu->rmode.tr.selector = var->selector;
1337 vcpu->rmode.tr.base = var->base;
1338 vcpu->rmode.tr.limit = var->limit;
1339 vcpu->rmode.tr.ar = vmx_segment_access_rights(var);
1340 return;
1341 }
1342 vmcs_writel(sf->base, var->base);
1343 vmcs_write32(sf->limit, var->limit);
1344 vmcs_write16(sf->selector, var->selector);
1345 if (vcpu->rmode.active && var->s) {
1346 /*
1347 * Hack real-mode segments into vm86 compatibility.
1348 */
1349 if (var->base == 0xffff0000 && var->selector == 0xf000)
1350 vmcs_writel(sf->base, 0xf0000);
1351 ar = 0xf3;
1352 } else
1353 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001354 vmcs_write32(sf->ar_bytes, ar);
1355}
1356
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1358{
1359 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1360
1361 *db = (ar >> 14) & 1;
1362 *l = (ar >> 13) & 1;
1363}
1364
1365static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1366{
1367 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1368 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1369}
1370
1371static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1372{
1373 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1374 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1375}
1376
1377static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1378{
1379 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1380 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1381}
1382
1383static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1384{
1385 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1386 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1387}
1388
Mike Dayd77c26f2007-10-08 09:02:08 -04001389static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001392 u16 data = 0;
1393 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394
Izik Eidus195aefd2007-10-01 22:14:18 +02001395 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1396 if (r < 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397 return 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02001398 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
1399 r = kvm_write_guest_page(kvm, fn++, &data, 0x66, sizeof(u16));
1400 if (r < 0)
1401 return 0;
1402 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
1403 if (r < 0)
1404 return 0;
1405 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1406 if (r < 0)
1407 return 0;
1408 data = ~0;
1409 r = kvm_write_guest_page(kvm, fn, &data, RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
1410 sizeof(u8));
1411 if (r < 0)
1412 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413 return 1;
1414}
1415
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416static void seg_setup(int seg)
1417{
1418 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1419
1420 vmcs_write16(sf->selector, 0);
1421 vmcs_writel(sf->base, 0);
1422 vmcs_write32(sf->limit, 0xffff);
1423 vmcs_write32(sf->ar_bytes, 0x93);
1424}
1425
1426/*
1427 * Sets up the vmcs for emulated real mode.
1428 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001429static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001430{
1431 u32 host_sysenter_cs;
1432 u32 junk;
1433 unsigned long a;
1434 struct descriptor_table dt;
1435 int i;
1436 int ret = 0;
Avi Kivitycd2276a2007-05-14 20:41:13 +03001437 unsigned long kvm_vmx_return;
Eddie Dong7017fc32007-07-18 11:34:57 +03001438 u64 msr;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001439 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440
Rusty Russell8b9cf982007-07-30 16:31:43 +10001441 if (!init_rmode_tss(vmx->vcpu.kvm)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001442 ret = -ENOMEM;
1443 goto out;
1444 }
1445
He, Qingc5ec1532007-09-03 17:07:41 +03001446 vmx->vcpu.rmode.active = 0;
1447
Rusty Russell8b9cf982007-07-30 16:31:43 +10001448 vmx->vcpu.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Eddie Dong7017fc32007-07-18 11:34:57 +03001449 set_cr8(&vmx->vcpu, 0);
1450 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Rusty Russell8b9cf982007-07-30 16:31:43 +10001451 if (vmx->vcpu.vcpu_id == 0)
Eddie Dong7017fc32007-07-18 11:34:57 +03001452 msr |= MSR_IA32_APICBASE_BSP;
1453 kvm_set_apic_base(&vmx->vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454
Rusty Russell8b9cf982007-07-30 16:31:43 +10001455 fx_init(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001456
1457 /*
1458 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
1459 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
1460 */
He, Qingc5ec1532007-09-03 17:07:41 +03001461 if (vmx->vcpu.vcpu_id == 0) {
1462 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
1463 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
1464 } else {
1465 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.sipi_vector << 8);
1466 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.sipi_vector << 12);
1467 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
1469 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1470
1471 seg_setup(VCPU_SREG_DS);
1472 seg_setup(VCPU_SREG_ES);
1473 seg_setup(VCPU_SREG_FS);
1474 seg_setup(VCPU_SREG_GS);
1475 seg_setup(VCPU_SREG_SS);
1476
1477 vmcs_write16(GUEST_TR_SELECTOR, 0);
1478 vmcs_writel(GUEST_TR_BASE, 0);
1479 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
1480 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1481
1482 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
1483 vmcs_writel(GUEST_LDTR_BASE, 0);
1484 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
1485 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
1486
1487 vmcs_write32(GUEST_SYSENTER_CS, 0);
1488 vmcs_writel(GUEST_SYSENTER_ESP, 0);
1489 vmcs_writel(GUEST_SYSENTER_EIP, 0);
1490
1491 vmcs_writel(GUEST_RFLAGS, 0x02);
He, Qingc5ec1532007-09-03 17:07:41 +03001492 if (vmx->vcpu.vcpu_id == 0)
1493 vmcs_writel(GUEST_RIP, 0xfff0);
1494 else
1495 vmcs_writel(GUEST_RIP, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496 vmcs_writel(GUEST_RSP, 0);
1497
Mike Dayd77c26f2007-10-08 09:02:08 -04001498 /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499 vmcs_writel(GUEST_DR7, 0x400);
1500
1501 vmcs_writel(GUEST_GDTR_BASE, 0);
1502 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
1503
1504 vmcs_writel(GUEST_IDTR_BASE, 0);
1505 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
1506
1507 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
1508 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
1509 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
1510
1511 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03001512 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
1513 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514
1515 guest_write_tsc(0);
1516
1517 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
1518
1519 /* Special registers */
1520 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
1521
1522 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001523 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
1524 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001525
1526 exec_control = vmcs_config.cpu_based_exec_ctrl;
1527 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
1528 exec_control &= ~CPU_BASED_TPR_SHADOW;
1529#ifdef CONFIG_X86_64
1530 exec_control |= CPU_BASED_CR8_STORE_EXITING |
1531 CPU_BASED_CR8_LOAD_EXITING;
1532#endif
1533 }
1534 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001535
Avi Kivityc7addb92007-09-16 18:58:32 +02001536 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
1537 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001538 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
1539
1540 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
1541 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
1542 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1543
1544 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
1545 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1546 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1547 vmcs_write16(HOST_FS_SELECTOR, read_fs()); /* 22.2.4 */
1548 vmcs_write16(HOST_GS_SELECTOR, read_gs()); /* 22.2.4 */
1549 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001550#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001551 rdmsrl(MSR_FS_BASE, a);
1552 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
1553 rdmsrl(MSR_GS_BASE, a);
1554 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
1555#else
1556 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
1557 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
1558#endif
1559
1560 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
1561
1562 get_idt(&dt);
1563 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
1564
Mike Dayd77c26f2007-10-08 09:02:08 -04001565 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03001566 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03001567 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
1568 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
1569 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001570
1571 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
1572 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
1573 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
1574 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
1575 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
1576 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
1577
Avi Kivity6aa8b732006-12-10 02:21:36 -08001578 for (i = 0; i < NR_VMX_MSR; ++i) {
1579 u32 index = vmx_msr_index[i];
1580 u32 data_low, data_high;
1581 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001582 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001583
1584 if (rdmsr_safe(index, &data_low, &data_high) < 0)
1585 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08001586 if (wrmsr_safe(index, data_low, data_high) < 0)
1587 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001589 vmx->host_msrs[j].index = index;
1590 vmx->host_msrs[j].reserved = 0;
1591 vmx->host_msrs[j].data = data;
1592 vmx->guest_msrs[j] = vmx->host_msrs[j];
1593 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595
Rusty Russell8b9cf982007-07-30 16:31:43 +10001596 setup_msrs(vmx);
Avi Kivitye38aea32007-04-19 13:22:48 +03001597
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001598 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001599
1600 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001601 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
1602
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
1604
Michael Riepe3b99ab22006-12-13 00:34:15 -08001605#ifdef CONFIG_X86_64
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001606 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
1607 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
1608 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
1609 page_to_phys(vmx->vcpu.apic->regs_page));
1610 vmcs_write32(TPR_THRESHOLD, 0);
Michael Riepe3b99ab22006-12-13 00:34:15 -08001611#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612
Anthony Liguori25c4c272007-04-27 09:29:21 +03001613 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
1615
Rusty Russell8b9cf982007-07-30 16:31:43 +10001616 vmx->vcpu.cr0 = 0x60000010;
Mike Dayd77c26f2007-10-08 09:02:08 -04001617 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001618 vmx_set_cr4(&vmx->vcpu, 0);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001619#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001620 vmx_set_efer(&vmx->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +10001622 vmx_fpu_activate(&vmx->vcpu);
1623 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624
1625 return 0;
1626
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627out:
1628 return ret;
1629}
1630
Avi Kivity04d2cc72007-09-10 18:10:54 +03001631static void vmx_vcpu_reset(struct kvm_vcpu *vcpu)
1632{
1633 struct vcpu_vmx *vmx = to_vmx(vcpu);
1634
1635 vmx_vcpu_setup(vmx);
1636}
1637
Avi Kivity6aa8b732006-12-10 02:21:36 -08001638static void inject_rmode_irq(struct kvm_vcpu *vcpu, int irq)
1639{
1640 u16 ent[2];
1641 u16 cs;
1642 u16 ip;
1643 unsigned long flags;
1644 unsigned long ss_base = vmcs_readl(GUEST_SS_BASE);
1645 u16 sp = vmcs_readl(GUEST_RSP);
1646 u32 ss_limit = vmcs_read32(GUEST_SS_LIMIT);
1647
Mike Dayd77c26f2007-10-08 09:02:08 -04001648 if (sp > ss_limit || sp < 6) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649 vcpu_printf(vcpu, "%s: #SS, rsp 0x%lx ss 0x%lx limit 0x%x\n",
1650 __FUNCTION__,
1651 vmcs_readl(GUEST_RSP),
1652 vmcs_readl(GUEST_SS_BASE),
1653 vmcs_read32(GUEST_SS_LIMIT));
1654 return;
1655 }
1656
Laurent Viviere7d5d762007-07-30 13:41:19 +03001657 if (emulator_read_std(irq * sizeof(ent), &ent, sizeof(ent), vcpu) !=
1658 X86EMUL_CONTINUE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659 vcpu_printf(vcpu, "%s: read guest err\n", __FUNCTION__);
1660 return;
1661 }
1662
1663 flags = vmcs_readl(GUEST_RFLAGS);
1664 cs = vmcs_readl(GUEST_CS_BASE) >> 4;
1665 ip = vmcs_readl(GUEST_RIP);
1666
1667
Mike Dayd77c26f2007-10-08 09:02:08 -04001668 if (emulator_write_emulated(
1669 ss_base + sp - 2, &flags, 2, vcpu) != X86EMUL_CONTINUE ||
1670 emulator_write_emulated(
1671 ss_base + sp - 4, &cs, 2, vcpu) != X86EMUL_CONTINUE ||
1672 emulator_write_emulated(
1673 ss_base + sp - 6, &ip, 2, vcpu) != X86EMUL_CONTINUE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674 vcpu_printf(vcpu, "%s: write guest err\n", __FUNCTION__);
1675 return;
1676 }
1677
1678 vmcs_writel(GUEST_RFLAGS, flags &
Mike Dayd77c26f2007-10-08 09:02:08 -04001679 ~(X86_EFLAGS_IF | X86_EFLAGS_AC | X86_EFLAGS_TF));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680 vmcs_write16(GUEST_CS_SELECTOR, ent[1]) ;
1681 vmcs_writel(GUEST_CS_BASE, ent[1] << 4);
1682 vmcs_writel(GUEST_RIP, ent[0]);
1683 vmcs_writel(GUEST_RSP, (vmcs_readl(GUEST_RSP) & ~0xffff) | (sp - 6));
1684}
1685
Eddie Dong85f455f2007-07-06 12:20:49 +03001686static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
1687{
1688 if (vcpu->rmode.active) {
1689 inject_rmode_irq(vcpu, irq);
1690 return;
1691 }
1692 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
1693 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1694}
1695
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
1697{
1698 int word_index = __ffs(vcpu->irq_summary);
1699 int bit_index = __ffs(vcpu->irq_pending[word_index]);
1700 int irq = word_index * BITS_PER_LONG + bit_index;
1701
1702 clear_bit(bit_index, &vcpu->irq_pending[word_index]);
1703 if (!vcpu->irq_pending[word_index])
1704 clear_bit(word_index, &vcpu->irq_summary);
Eddie Dong85f455f2007-07-06 12:20:49 +03001705 vmx_inject_irq(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706}
1707
Dor Laorc1150d82007-01-05 16:36:24 -08001708
1709static void do_interrupt_requests(struct kvm_vcpu *vcpu,
1710 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711{
Dor Laorc1150d82007-01-05 16:36:24 -08001712 u32 cpu_based_vm_exec_control;
1713
1714 vcpu->interrupt_window_open =
1715 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
1716 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
1717
1718 if (vcpu->interrupt_window_open &&
1719 vcpu->irq_summary &&
1720 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001721 /*
Dor Laorc1150d82007-01-05 16:36:24 -08001722 * If interrupts enabled, and not blocked by sti or mov ss. Good.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001723 */
1724 kvm_do_inject_irq(vcpu);
Dor Laorc1150d82007-01-05 16:36:24 -08001725
1726 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
1727 if (!vcpu->interrupt_window_open &&
1728 (vcpu->irq_summary || kvm_run->request_interrupt_window))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729 /*
1730 * Interrupts blocked. Wait for unblock.
1731 */
Dor Laorc1150d82007-01-05 16:36:24 -08001732 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
1733 else
1734 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
1735 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001736}
1737
1738static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
1739{
1740 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
1741
1742 set_debugreg(dbg->bp[0], 0);
1743 set_debugreg(dbg->bp[1], 1);
1744 set_debugreg(dbg->bp[2], 2);
1745 set_debugreg(dbg->bp[3], 3);
1746
1747 if (dbg->singlestep) {
1748 unsigned long flags;
1749
1750 flags = vmcs_readl(GUEST_RFLAGS);
1751 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1752 vmcs_writel(GUEST_RFLAGS, flags);
1753 }
1754}
1755
1756static int handle_rmode_exception(struct kvm_vcpu *vcpu,
1757 int vec, u32 err_code)
1758{
1759 if (!vcpu->rmode.active)
1760 return 0;
1761
Nitin A Kambleb3f37702007-05-17 15:50:34 +03001762 /*
1763 * Instruction with address size override prefix opcode 0x67
1764 * Cause the #SS fault with 0 error code in VM86 mode.
1765 */
1766 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02001767 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001768 return 1;
1769 return 0;
1770}
1771
1772static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1773{
1774 u32 intr_info, error_code;
1775 unsigned long cr2, rip;
1776 u32 vect_info;
1777 enum emulation_result er;
Avi Kivitye2dec932007-01-05 16:36:54 -08001778 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001779
1780 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
1781 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
1782
1783 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04001784 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001785 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
1786 "intr info 0x%x\n", __FUNCTION__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001787
Eddie Dong85f455f2007-07-06 12:20:49 +03001788 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001789 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
1790 set_bit(irq, vcpu->irq_pending);
1791 set_bit(irq / BITS_PER_LONG, &vcpu->irq_summary);
1792 }
1793
Avi Kivity1b6269d2007-10-09 12:12:19 +02001794 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
1795 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001796
1797 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001798 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001799 return 1;
1800 }
1801
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001802 if (is_invalid_opcode(intr_info)) {
Laurent Vivier34273182007-09-18 11:27:37 +02001803 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001804 if (er != EMULATE_DONE)
1805 vmx_inject_ud(vcpu);
1806
1807 return 1;
1808 }
1809
Avi Kivity6aa8b732006-12-10 02:21:36 -08001810 error_code = 0;
1811 rip = vmcs_readl(GUEST_RIP);
1812 if (intr_info & INTR_INFO_DELIEVER_CODE_MASK)
1813 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
1814 if (is_page_fault(intr_info)) {
1815 cr2 = vmcs_readl(EXIT_QUALIFICATION);
1816
Shaohua Li11ec2802007-07-23 14:51:37 +08001817 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001818 r = kvm_mmu_page_fault(vcpu, cr2, error_code);
1819 if (r < 0) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001820 mutex_unlock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001821 return r;
1822 }
1823 if (!r) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001824 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001825 return 1;
1826 }
1827
Laurent Vivier34273182007-09-18 11:27:37 +02001828 er = emulate_instruction(vcpu, kvm_run, cr2, error_code, 0);
Shaohua Li11ec2802007-07-23 14:51:37 +08001829 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001830
1831 switch (er) {
1832 case EMULATE_DONE:
1833 return 1;
1834 case EMULATE_DO_MMIO:
Avi Kivity1165f5f2007-04-19 17:27:43 +03001835 ++vcpu->stat.mmio_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836 return 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001837 case EMULATE_FAIL:
Avi Kivity054b1362007-09-12 13:21:09 +03001838 kvm_report_emulation_failure(vcpu, "pagetable");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001839 break;
1840 default:
1841 BUG();
1842 }
1843 }
1844
1845 if (vcpu->rmode.active &&
1846 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03001847 error_code)) {
1848 if (vcpu->halt_request) {
1849 vcpu->halt_request = 0;
1850 return kvm_emulate_halt(vcpu);
1851 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001852 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03001853 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001854
Mike Dayd77c26f2007-10-08 09:02:08 -04001855 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) ==
1856 (INTR_TYPE_EXCEPTION | 1)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001857 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1858 return 0;
1859 }
1860 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
1861 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
1862 kvm_run->ex.error_code = error_code;
1863 return 0;
1864}
1865
1866static int handle_external_interrupt(struct kvm_vcpu *vcpu,
1867 struct kvm_run *kvm_run)
1868{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001869 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870 return 1;
1871}
1872
Avi Kivity988ad742007-02-12 00:54:36 -08001873static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1874{
1875 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1876 return 0;
1877}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001878
Avi Kivity6aa8b732006-12-10 02:21:36 -08001879static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1880{
He, Qingbfdaab02007-09-12 14:18:28 +08001881 unsigned long exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02001882 int size, down, in, string, rep;
1883 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884
Avi Kivity1165f5f2007-04-19 17:27:43 +03001885 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08001886 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02001887 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001888
1889 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02001890 if (emulate_instruction(vcpu,
1891 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03001892 return 0;
1893 return 1;
1894 }
1895
1896 size = (exit_qualification & 7) + 1;
1897 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001898 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001899 rep = (exit_qualification & 32) != 0;
1900 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03001901
Laurent Vivier3090dd72007-08-05 10:43:32 +03001902 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903}
1904
Ingo Molnar102d8322007-02-19 14:37:47 +02001905static void
1906vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
1907{
1908 /*
1909 * Patch in the VMCALL instruction:
1910 */
1911 hypercall[0] = 0x0f;
1912 hypercall[1] = 0x01;
1913 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02001914}
1915
Avi Kivity6aa8b732006-12-10 02:21:36 -08001916static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1917{
He, Qingbfdaab02007-09-12 14:18:28 +08001918 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001919 int cr;
1920 int reg;
1921
He, Qingbfdaab02007-09-12 14:18:28 +08001922 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001923 cr = exit_qualification & 15;
1924 reg = (exit_qualification >> 8) & 15;
1925 switch ((exit_qualification >> 4) & 3) {
1926 case 0: /* mov to cr */
1927 switch (cr) {
1928 case 0:
1929 vcpu_load_rsp_rip(vcpu);
1930 set_cr0(vcpu, vcpu->regs[reg]);
1931 skip_emulated_instruction(vcpu);
1932 return 1;
1933 case 3:
1934 vcpu_load_rsp_rip(vcpu);
1935 set_cr3(vcpu, vcpu->regs[reg]);
1936 skip_emulated_instruction(vcpu);
1937 return 1;
1938 case 4:
1939 vcpu_load_rsp_rip(vcpu);
1940 set_cr4(vcpu, vcpu->regs[reg]);
1941 skip_emulated_instruction(vcpu);
1942 return 1;
1943 case 8:
1944 vcpu_load_rsp_rip(vcpu);
1945 set_cr8(vcpu, vcpu->regs[reg]);
1946 skip_emulated_instruction(vcpu);
Yang, Sheng253abde2007-08-16 13:01:00 +03001947 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
1948 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001949 };
1950 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03001951 case 2: /* clts */
1952 vcpu_load_rsp_rip(vcpu);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001953 vmx_fpu_deactivate(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001954 vcpu->cr0 &= ~X86_CR0_TS;
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001955 vmcs_writel(CR0_READ_SHADOW, vcpu->cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001956 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03001957 skip_emulated_instruction(vcpu);
1958 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001959 case 1: /*mov from cr*/
1960 switch (cr) {
1961 case 3:
1962 vcpu_load_rsp_rip(vcpu);
1963 vcpu->regs[reg] = vcpu->cr3;
1964 vcpu_put_rsp_rip(vcpu);
1965 skip_emulated_instruction(vcpu);
1966 return 1;
1967 case 8:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001968 vcpu_load_rsp_rip(vcpu);
Eddie Dong7017fc32007-07-18 11:34:57 +03001969 vcpu->regs[reg] = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001970 vcpu_put_rsp_rip(vcpu);
1971 skip_emulated_instruction(vcpu);
1972 return 1;
1973 }
1974 break;
1975 case 3: /* lmsw */
1976 lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
1977
1978 skip_emulated_instruction(vcpu);
1979 return 1;
1980 default:
1981 break;
1982 }
1983 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10001984 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08001985 (int)(exit_qualification >> 4) & 3, cr);
1986 return 0;
1987}
1988
1989static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1990{
He, Qingbfdaab02007-09-12 14:18:28 +08001991 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001992 unsigned long val;
1993 int dr, reg;
1994
1995 /*
1996 * FIXME: this code assumes the host is debugging the guest.
1997 * need to deal with guest debugging itself too.
1998 */
He, Qingbfdaab02007-09-12 14:18:28 +08001999 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002000 dr = exit_qualification & 7;
2001 reg = (exit_qualification >> 8) & 15;
2002 vcpu_load_rsp_rip(vcpu);
2003 if (exit_qualification & 16) {
2004 /* mov from dr */
2005 switch (dr) {
2006 case 6:
2007 val = 0xffff0ff0;
2008 break;
2009 case 7:
2010 val = 0x400;
2011 break;
2012 default:
2013 val = 0;
2014 }
2015 vcpu->regs[reg] = val;
2016 } else {
2017 /* mov to dr */
2018 }
2019 vcpu_put_rsp_rip(vcpu);
2020 skip_emulated_instruction(vcpu);
2021 return 1;
2022}
2023
2024static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2025{
Avi Kivity06465c52007-02-28 20:46:53 +02002026 kvm_emulate_cpuid(vcpu);
2027 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002028}
2029
2030static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2031{
2032 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
2033 u64 data;
2034
2035 if (vmx_get_msr(vcpu, ecx, &data)) {
2036 vmx_inject_gp(vcpu, 0);
2037 return 1;
2038 }
2039
2040 /* FIXME: handling of bits 32:63 of rax, rdx */
2041 vcpu->regs[VCPU_REGS_RAX] = data & -1u;
2042 vcpu->regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
2043 skip_emulated_instruction(vcpu);
2044 return 1;
2045}
2046
2047static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2048{
2049 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
2050 u64 data = (vcpu->regs[VCPU_REGS_RAX] & -1u)
2051 | ((u64)(vcpu->regs[VCPU_REGS_RDX] & -1u) << 32);
2052
2053 if (vmx_set_msr(vcpu, ecx, data) != 0) {
2054 vmx_inject_gp(vcpu, 0);
2055 return 1;
2056 }
2057
2058 skip_emulated_instruction(vcpu);
2059 return 1;
2060}
2061
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002062static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2063 struct kvm_run *kvm_run)
2064{
2065 return 1;
2066}
2067
Avi Kivity6aa8b732006-12-10 02:21:36 -08002068static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2069 struct kvm_run *kvm_run)
2070{
Eddie Dong85f455f2007-07-06 12:20:49 +03002071 u32 cpu_based_vm_exec_control;
2072
2073 /* clear pending irq */
2074 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2075 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2076 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Dor Laorc1150d82007-01-05 16:36:24 -08002077 /*
2078 * If the user space waits to inject interrupts, exit as soon as
2079 * possible
2080 */
2081 if (kvm_run->request_interrupt_window &&
Dor Laor022a9302007-01-05 16:37:00 -08002082 !vcpu->irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002083 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002084 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002085 return 0;
2086 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087 return 1;
2088}
2089
2090static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2091{
2092 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002093 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094}
2095
Ingo Molnarc21415e2007-02-19 14:37:47 +02002096static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2097{
Dor Laor510043d2007-02-19 18:25:43 +02002098 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002099 kvm_emulate_hypercall(vcpu);
2100 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02002101}
2102
Avi Kivity6aa8b732006-12-10 02:21:36 -08002103/*
2104 * The exit handlers return 1 if the exit was handled fully and guest execution
2105 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2106 * to be done to userspace and return 0.
2107 */
2108static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
2109 struct kvm_run *kvm_run) = {
2110 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
2111 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08002112 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002113 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114 [EXIT_REASON_CR_ACCESS] = handle_cr,
2115 [EXIT_REASON_DR_ACCESS] = handle_dr,
2116 [EXIT_REASON_CPUID] = handle_cpuid,
2117 [EXIT_REASON_MSR_READ] = handle_rdmsr,
2118 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
2119 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
2120 [EXIT_REASON_HLT] = handle_halt,
Ingo Molnarc21415e2007-02-19 14:37:47 +02002121 [EXIT_REASON_VMCALL] = handle_vmcall,
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002122 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123};
2124
2125static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04002126 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002127
2128/*
2129 * The guest has exited. See if we can fix it or if we need userspace
2130 * assistance.
2131 */
2132static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2133{
2134 u32 vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2135 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity29bd8a72007-09-10 17:27:03 +03002136 struct vcpu_vmx *vmx = to_vmx(vcpu);
2137
2138 if (unlikely(vmx->fail)) {
2139 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2140 kvm_run->fail_entry.hardware_entry_failure_reason
2141 = vmcs_read32(VM_INSTRUCTION_ERROR);
2142 return 0;
2143 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002144
Mike Dayd77c26f2007-10-08 09:02:08 -04002145 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
2146 exit_reason != EXIT_REASON_EXCEPTION_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147 printk(KERN_WARNING "%s: unexpected, valid vectoring info and "
2148 "exit reason is 0x%x\n", __FUNCTION__, exit_reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002149 if (exit_reason < kvm_vmx_max_exit_handlers
2150 && kvm_vmx_exit_handlers[exit_reason])
2151 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
2152 else {
2153 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2154 kvm_run->hw.hardware_exit_reason = exit_reason;
2155 }
2156 return 0;
2157}
2158
Avi Kivityd9e368d2007-06-07 19:18:30 +03002159static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
2160{
Avi Kivityd9e368d2007-06-07 19:18:30 +03002161}
2162
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002163static void update_tpr_threshold(struct kvm_vcpu *vcpu)
2164{
2165 int max_irr, tpr;
2166
2167 if (!vm_need_tpr_shadow(vcpu->kvm))
2168 return;
2169
2170 if (!kvm_lapic_enabled(vcpu) ||
2171 ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
2172 vmcs_write32(TPR_THRESHOLD, 0);
2173 return;
2174 }
2175
2176 tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
2177 vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
2178}
2179
Eddie Dong85f455f2007-07-06 12:20:49 +03002180static void enable_irq_window(struct kvm_vcpu *vcpu)
2181{
2182 u32 cpu_based_vm_exec_control;
2183
2184 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2185 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2186 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2187}
2188
2189static void vmx_intr_assist(struct kvm_vcpu *vcpu)
2190{
2191 u32 idtv_info_field, intr_info_field;
2192 int has_ext_irq, interrupt_window_open;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002193 int vector;
Eddie Dong85f455f2007-07-06 12:20:49 +03002194
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002195 update_tpr_threshold(vcpu);
2196
Eddie Dong85f455f2007-07-06 12:20:49 +03002197 has_ext_irq = kvm_cpu_has_interrupt(vcpu);
2198 intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
2199 idtv_info_field = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2200 if (intr_info_field & INTR_INFO_VALID_MASK) {
2201 if (idtv_info_field & INTR_INFO_VALID_MASK) {
2202 /* TODO: fault when IDT_Vectoring */
2203 printk(KERN_ERR "Fault when IDT_Vectoring\n");
2204 }
2205 if (has_ext_irq)
2206 enable_irq_window(vcpu);
2207 return;
2208 }
2209 if (unlikely(idtv_info_field & INTR_INFO_VALID_MASK)) {
2210 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, idtv_info_field);
2211 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2212 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
2213
2214 if (unlikely(idtv_info_field & INTR_INFO_DELIEVER_CODE_MASK))
2215 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2216 vmcs_read32(IDT_VECTORING_ERROR_CODE));
2217 if (unlikely(has_ext_irq))
2218 enable_irq_window(vcpu);
2219 return;
2220 }
2221 if (!has_ext_irq)
2222 return;
2223 interrupt_window_open =
2224 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2225 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002226 if (interrupt_window_open) {
2227 vector = kvm_cpu_get_interrupt(vcpu);
2228 vmx_inject_irq(vcpu, vector);
2229 kvm_timer_intr_post(vcpu, vector);
2230 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03002231 enable_irq_window(vcpu);
2232}
2233
Avi Kivity04d2cc72007-09-10 18:10:54 +03002234static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002235{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002236 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1b6269d2007-10-09 12:12:19 +02002237 u32 intr_info;
Avi Kivitye6adf282007-04-30 16:07:54 +03002238
2239 /*
2240 * Loading guest fpu may have cleared host cr0.ts
2241 */
2242 vmcs_writel(HOST_CR0, read_cr0());
2243
Mike Dayd77c26f2007-10-08 09:02:08 -04002244 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08002245 /* Store host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002246#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247 "push %%rax; push %%rbx; push %%rdx;"
2248 "push %%rsi; push %%rdi; push %%rbp;"
2249 "push %%r8; push %%r9; push %%r10; push %%r11;"
2250 "push %%r12; push %%r13; push %%r14; push %%r15;"
2251 "push %%rcx \n\t"
2252 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
2253#else
2254 "pusha; push %%ecx \n\t"
2255 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
2256#endif
2257 /* Check if vmlaunch of vmresume is needed */
2258 "cmp $0, %1 \n\t"
2259 /* Load guest registers. Don't clobber flags. */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002260#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261 "mov %c[cr2](%3), %%rax \n\t"
2262 "mov %%rax, %%cr2 \n\t"
2263 "mov %c[rax](%3), %%rax \n\t"
2264 "mov %c[rbx](%3), %%rbx \n\t"
2265 "mov %c[rdx](%3), %%rdx \n\t"
2266 "mov %c[rsi](%3), %%rsi \n\t"
2267 "mov %c[rdi](%3), %%rdi \n\t"
2268 "mov %c[rbp](%3), %%rbp \n\t"
2269 "mov %c[r8](%3), %%r8 \n\t"
2270 "mov %c[r9](%3), %%r9 \n\t"
2271 "mov %c[r10](%3), %%r10 \n\t"
2272 "mov %c[r11](%3), %%r11 \n\t"
2273 "mov %c[r12](%3), %%r12 \n\t"
2274 "mov %c[r13](%3), %%r13 \n\t"
2275 "mov %c[r14](%3), %%r14 \n\t"
2276 "mov %c[r15](%3), %%r15 \n\t"
2277 "mov %c[rcx](%3), %%rcx \n\t" /* kills %3 (rcx) */
2278#else
2279 "mov %c[cr2](%3), %%eax \n\t"
2280 "mov %%eax, %%cr2 \n\t"
2281 "mov %c[rax](%3), %%eax \n\t"
2282 "mov %c[rbx](%3), %%ebx \n\t"
2283 "mov %c[rdx](%3), %%edx \n\t"
2284 "mov %c[rsi](%3), %%esi \n\t"
2285 "mov %c[rdi](%3), %%edi \n\t"
2286 "mov %c[rbp](%3), %%ebp \n\t"
2287 "mov %c[rcx](%3), %%ecx \n\t" /* kills %3 (ecx) */
2288#endif
2289 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03002290 "jne .Llaunched \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002291 ASM_VMX_VMLAUNCH "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03002292 "jmp .Lkvm_vmx_return \n\t"
2293 ".Llaunched: " ASM_VMX_VMRESUME "\n\t"
2294 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08002295 /* Save guest registers, load host registers, keep flags */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002296#ifdef CONFIG_X86_64
Ingo Molnar96958232007-02-12 00:54:33 -08002297 "xchg %3, (%%rsp) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298 "mov %%rax, %c[rax](%3) \n\t"
2299 "mov %%rbx, %c[rbx](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002300 "pushq (%%rsp); popq %c[rcx](%3) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002301 "mov %%rdx, %c[rdx](%3) \n\t"
2302 "mov %%rsi, %c[rsi](%3) \n\t"
2303 "mov %%rdi, %c[rdi](%3) \n\t"
2304 "mov %%rbp, %c[rbp](%3) \n\t"
2305 "mov %%r8, %c[r8](%3) \n\t"
2306 "mov %%r9, %c[r9](%3) \n\t"
2307 "mov %%r10, %c[r10](%3) \n\t"
2308 "mov %%r11, %c[r11](%3) \n\t"
2309 "mov %%r12, %c[r12](%3) \n\t"
2310 "mov %%r13, %c[r13](%3) \n\t"
2311 "mov %%r14, %c[r14](%3) \n\t"
2312 "mov %%r15, %c[r15](%3) \n\t"
2313 "mov %%cr2, %%rax \n\t"
2314 "mov %%rax, %c[cr2](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002315 "mov (%%rsp), %3 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316
2317 "pop %%rcx; pop %%r15; pop %%r14; pop %%r13; pop %%r12;"
2318 "pop %%r11; pop %%r10; pop %%r9; pop %%r8;"
2319 "pop %%rbp; pop %%rdi; pop %%rsi;"
2320 "pop %%rdx; pop %%rbx; pop %%rax \n\t"
2321#else
Ingo Molnar96958232007-02-12 00:54:33 -08002322 "xchg %3, (%%esp) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002323 "mov %%eax, %c[rax](%3) \n\t"
2324 "mov %%ebx, %c[rbx](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002325 "pushl (%%esp); popl %c[rcx](%3) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326 "mov %%edx, %c[rdx](%3) \n\t"
2327 "mov %%esi, %c[rsi](%3) \n\t"
2328 "mov %%edi, %c[rdi](%3) \n\t"
2329 "mov %%ebp, %c[rbp](%3) \n\t"
2330 "mov %%cr2, %%eax \n\t"
2331 "mov %%eax, %c[cr2](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002332 "mov (%%esp), %3 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333
2334 "pop %%ecx; popa \n\t"
2335#endif
2336 "setbe %0 \n\t"
Avi Kivity29bd8a72007-09-10 17:27:03 +03002337 : "=q" (vmx->fail)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002338 : "r"(vmx->launched), "d"((unsigned long)HOST_RSP),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339 "c"(vcpu),
2340 [rax]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RAX])),
2341 [rbx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBX])),
2342 [rcx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RCX])),
2343 [rdx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDX])),
2344 [rsi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RSI])),
2345 [rdi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDI])),
2346 [rbp]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002347#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -04002348 [r8]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R8])),
2349 [r9]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R9])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350 [r10]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R10])),
2351 [r11]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R11])),
2352 [r12]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R12])),
2353 [r13]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R13])),
2354 [r14]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R14])),
2355 [r15]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R15])),
2356#endif
2357 [cr2]"i"(offsetof(struct kvm_vcpu, cr2))
Mike Dayd77c26f2007-10-08 09:02:08 -04002358 : "cc", "memory");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359
Mike Dayd77c26f2007-10-08 09:02:08 -04002360 vcpu->interrupt_window_open =
2361 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362
Mike Dayd77c26f2007-10-08 09:02:08 -04002363 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03002364 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02002365
2366 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2367
2368 /* We need to handle NMIs before interrupts are enabled */
2369 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
2370 asm("int $2");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371}
2372
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373static void vmx_inject_page_fault(struct kvm_vcpu *vcpu,
2374 unsigned long addr,
2375 u32 err_code)
2376{
2377 u32 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2378
Avi Kivity1165f5f2007-04-19 17:27:43 +03002379 ++vcpu->stat.pf_guest;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380
2381 if (is_page_fault(vect_info)) {
2382 printk(KERN_DEBUG "inject_page_fault: "
2383 "double fault 0x%lx @ 0x%lx\n",
2384 addr, vmcs_readl(GUEST_RIP));
2385 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
2386 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2387 DF_VECTOR |
2388 INTR_TYPE_EXCEPTION |
2389 INTR_INFO_DELIEVER_CODE_MASK |
2390 INTR_INFO_VALID_MASK);
2391 return;
2392 }
2393 vcpu->cr2 = addr;
2394 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, err_code);
2395 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2396 PF_VECTOR |
2397 INTR_TYPE_EXCEPTION |
2398 INTR_INFO_DELIEVER_CODE_MASK |
2399 INTR_INFO_VALID_MASK);
2400
2401}
2402
2403static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
2404{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002405 struct vcpu_vmx *vmx = to_vmx(vcpu);
2406
2407 if (vmx->vmcs) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002408 on_each_cpu(__vcpu_clear, vmx, 0, 1);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002409 free_vmcs(vmx->vmcs);
2410 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002411 }
2412}
2413
2414static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
2415{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002416 struct vcpu_vmx *vmx = to_vmx(vcpu);
2417
Avi Kivity6aa8b732006-12-10 02:21:36 -08002418 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002419 kfree(vmx->host_msrs);
2420 kfree(vmx->guest_msrs);
2421 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10002422 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002423}
2424
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002425static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002427 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10002428 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03002429 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002430
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002431 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002432 return ERR_PTR(-ENOMEM);
2433
2434 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
2435 if (err)
2436 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002437
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002438 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002439 if (!vmx->guest_msrs) {
2440 err = -ENOMEM;
2441 goto uninit_vcpu;
2442 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08002443
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002444 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
2445 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002446 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002447
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002448 vmx->vmcs = alloc_vmcs();
2449 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002450 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002451
2452 vmcs_clear(vmx->vmcs);
2453
Avi Kivity15ad7142007-07-11 18:17:21 +03002454 cpu = get_cpu();
2455 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002456 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002457 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002458 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002459 if (err)
2460 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002461
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002462 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002463
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002464free_vmcs:
2465 free_vmcs(vmx->vmcs);
2466free_msrs:
2467 kfree(vmx->host_msrs);
2468free_guest_msrs:
2469 kfree(vmx->guest_msrs);
2470uninit_vcpu:
2471 kvm_vcpu_uninit(&vmx->vcpu);
2472free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10002473 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002474 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002475}
2476
Yang, Sheng002c7f72007-07-31 14:23:01 +03002477static void __init vmx_check_processor_compat(void *rtn)
2478{
2479 struct vmcs_config vmcs_conf;
2480
2481 *(int *)rtn = 0;
2482 if (setup_vmcs_config(&vmcs_conf) < 0)
2483 *(int *)rtn = -EIO;
2484 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
2485 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
2486 smp_processor_id());
2487 *(int *)rtn = -EIO;
2488 }
2489}
2490
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002491static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492 .cpu_has_kvm_support = cpu_has_kvm_support,
2493 .disabled_by_bios = vmx_disabled_by_bios,
2494 .hardware_setup = hardware_setup,
2495 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03002496 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002497 .hardware_enable = hardware_enable,
2498 .hardware_disable = hardware_disable,
2499
2500 .vcpu_create = vmx_create_vcpu,
2501 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002502 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002503
Avi Kivity04d2cc72007-09-10 18:10:54 +03002504 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002505 .vcpu_load = vmx_vcpu_load,
2506 .vcpu_put = vmx_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08002507 .vcpu_decache = vmx_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002508
2509 .set_guest_debug = set_guest_debug,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002510 .guest_debug_pre = kvm_guest_debug_pre,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511 .get_msr = vmx_get_msr,
2512 .set_msr = vmx_set_msr,
2513 .get_segment_base = vmx_get_segment_base,
2514 .get_segment = vmx_get_segment,
2515 .set_segment = vmx_set_segment,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002516 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03002517 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002518 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519 .set_cr3 = vmx_set_cr3,
2520 .set_cr4 = vmx_set_cr4,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002521#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002522 .set_efer = vmx_set_efer,
2523#endif
2524 .get_idt = vmx_get_idt,
2525 .set_idt = vmx_set_idt,
2526 .get_gdt = vmx_get_gdt,
2527 .set_gdt = vmx_set_gdt,
2528 .cache_regs = vcpu_load_rsp_rip,
2529 .decache_regs = vcpu_put_rsp_rip,
2530 .get_rflags = vmx_get_rflags,
2531 .set_rflags = vmx_set_rflags,
2532
2533 .tlb_flush = vmx_flush_tlb,
2534 .inject_page_fault = vmx_inject_page_fault,
2535
2536 .inject_gp = vmx_inject_gp,
2537
2538 .run = vmx_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002539 .handle_exit = kvm_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002540 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02002541 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03002542 .get_irq = vmx_get_irq,
2543 .set_irq = vmx_inject_irq,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002544 .inject_pending_irq = vmx_intr_assist,
2545 .inject_pending_vectors = do_interrupt_requests,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546};
2547
2548static int __init vmx_init(void)
2549{
He, Qingfdef3ad2007-04-30 09:45:24 +03002550 void *iova;
2551 int r;
2552
2553 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2554 if (!vmx_io_bitmap_a)
2555 return -ENOMEM;
2556
2557 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2558 if (!vmx_io_bitmap_b) {
2559 r = -ENOMEM;
2560 goto out;
2561 }
2562
2563 /*
2564 * Allow direct access to the PC debug port (it is often used for I/O
2565 * delays, but the vmexits simply slow things down).
2566 */
2567 iova = kmap(vmx_io_bitmap_a);
2568 memset(iova, 0xff, PAGE_SIZE);
2569 clear_bit(0x80, iova);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002570 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03002571
2572 iova = kmap(vmx_io_bitmap_b);
2573 memset(iova, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002574 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03002575
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002576 r = kvm_init_x86(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03002577 if (r)
2578 goto out1;
2579
Avi Kivityc7addb92007-09-16 18:58:32 +02002580 if (bypass_guest_pf)
2581 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
2582
He, Qingfdef3ad2007-04-30 09:45:24 +03002583 return 0;
2584
2585out1:
2586 __free_page(vmx_io_bitmap_b);
2587out:
2588 __free_page(vmx_io_bitmap_a);
2589 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590}
2591
2592static void __exit vmx_exit(void)
2593{
He, Qingfdef3ad2007-04-30 09:45:24 +03002594 __free_page(vmx_io_bitmap_b);
2595 __free_page(vmx_io_bitmap_a);
2596
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002597 kvm_exit_x86();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002598}
2599
2600module_init(vmx_init)
2601module_exit(vmx_exit)