blob: f1e80a95b69d2a0dbf98021468dc7759d3cbbc08 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
18#include "kvm.h"
Laurent Viviere7d5d762007-07-30 13:41:19 +030019#include "x86_emulate.h"
Eddie Dong85f455f2007-07-06 12:20:49 +030020#include "irq.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080021#include "vmx.h"
Avi Kivitye4956062007-06-28 14:15:57 -040022#include "segment_descriptor.h"
23
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020025#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/mm.h>
27#include <linux/highmem.h>
Ingo Molnar07031e12007-01-10 23:15:38 -080028#include <linux/profile.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivitye4956062007-06-28 14:15:57 -040030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080032#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080033
Avi Kivity6aa8b732006-12-10 02:21:36 -080034MODULE_AUTHOR("Qumranet");
35MODULE_LICENSE("GPL");
36
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040037struct vmcs {
38 u32 revision_id;
39 u32 abort;
40 char data[0];
41};
42
43struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100044 struct kvm_vcpu vcpu;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040045 int launched;
46 struct kvm_msr_entry *guest_msrs;
47 struct kvm_msr_entry *host_msrs;
48 int nmsrs;
49 int save_nmsrs;
50 int msr_offset_efer;
51#ifdef CONFIG_X86_64
52 int msr_offset_kernel_gs_base;
53#endif
54 struct vmcs *vmcs;
55 struct {
56 int loaded;
57 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020058 int gs_ldt_reload_needed;
59 int fs_reload_needed;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040060 }host_state;
61
62};
63
64static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
65{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100066 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040067}
68
Avi Kivity75880a02007-06-20 11:20:04 +030069static int init_rmode_tss(struct kvm *kvm);
70
Avi Kivity6aa8b732006-12-10 02:21:36 -080071static DEFINE_PER_CPU(struct vmcs *, vmxarea);
72static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
73
He, Qingfdef3ad2007-04-30 09:45:24 +030074static struct page *vmx_io_bitmap_a;
75static struct page *vmx_io_bitmap_b;
76
Eddie Dong2cc51562007-05-21 07:28:09 +030077#define EFER_SAVE_RESTORE_BITS ((u64)EFER_SCE)
Avi Kivity6aa8b732006-12-10 02:21:36 -080078
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +030079static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -080080 int size;
81 int order;
82 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +030083 u32 pin_based_exec_ctrl;
84 u32 cpu_based_exec_ctrl;
85 u32 vmexit_ctrl;
86 u32 vmentry_ctrl;
87} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -080088
89#define VMX_SEGMENT_FIELD(seg) \
90 [VCPU_SREG_##seg] = { \
91 .selector = GUEST_##seg##_SELECTOR, \
92 .base = GUEST_##seg##_BASE, \
93 .limit = GUEST_##seg##_LIMIT, \
94 .ar_bytes = GUEST_##seg##_AR_BYTES, \
95 }
96
97static struct kvm_vmx_segment_field {
98 unsigned selector;
99 unsigned base;
100 unsigned limit;
101 unsigned ar_bytes;
102} kvm_vmx_segment_fields[] = {
103 VMX_SEGMENT_FIELD(CS),
104 VMX_SEGMENT_FIELD(DS),
105 VMX_SEGMENT_FIELD(ES),
106 VMX_SEGMENT_FIELD(FS),
107 VMX_SEGMENT_FIELD(GS),
108 VMX_SEGMENT_FIELD(SS),
109 VMX_SEGMENT_FIELD(TR),
110 VMX_SEGMENT_FIELD(LDTR),
111};
112
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300113/*
114 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
115 * away by decrementing the array size.
116 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800117static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800118#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
120#endif
121 MSR_EFER, MSR_K6_STAR,
122};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200123#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400125static void load_msrs(struct kvm_msr_entry *e, int n)
126{
127 int i;
128
129 for (i = 0; i < n; ++i)
130 wrmsrl(e[i].index, e[i].data);
131}
132
133static void save_msrs(struct kvm_msr_entry *e, int n)
134{
135 int i;
136
137 for (i = 0; i < n; ++i)
138 rdmsrl(e[i].index, e[i].data);
139}
140
141static inline u64 msr_efer_save_restore_bits(struct kvm_msr_entry msr)
Eddie Dong2cc51562007-05-21 07:28:09 +0300142{
143 return (u64)msr.data & EFER_SAVE_RESTORE_BITS;
144}
145
Rusty Russell8b9cf982007-07-30 16:31:43 +1000146static inline int msr_efer_need_save_restore(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300147{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400148 int efer_offset = vmx->msr_offset_efer;
149 return msr_efer_save_restore_bits(vmx->host_msrs[efer_offset]) !=
150 msr_efer_save_restore_bits(vmx->guest_msrs[efer_offset]);
Eddie Dong2cc51562007-05-21 07:28:09 +0300151}
152
Avi Kivity6aa8b732006-12-10 02:21:36 -0800153static inline int is_page_fault(u32 intr_info)
154{
155 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
156 INTR_INFO_VALID_MASK)) ==
157 (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
158}
159
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300160static inline int is_no_device(u32 intr_info)
161{
162 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
163 INTR_INFO_VALID_MASK)) ==
164 (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
165}
166
Avi Kivity6aa8b732006-12-10 02:21:36 -0800167static inline int is_external_interrupt(u32 intr_info)
168{
169 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
170 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
171}
172
Rusty Russell8b9cf982007-07-30 16:31:43 +1000173static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800174{
175 int i;
176
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400177 for (i = 0; i < vmx->nmsrs; ++i)
178 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300179 return i;
180 return -1;
181}
182
Rusty Russell8b9cf982007-07-30 16:31:43 +1000183static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300184{
185 int i;
186
Rusty Russell8b9cf982007-07-30 16:31:43 +1000187 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300188 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400189 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000190 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800191}
192
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193static void vmcs_clear(struct vmcs *vmcs)
194{
195 u64 phys_addr = __pa(vmcs);
196 u8 error;
197
198 asm volatile (ASM_VMX_VMCLEAR_RAX "; setna %0"
199 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
200 : "cc", "memory");
201 if (error)
202 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
203 vmcs, phys_addr);
204}
205
206static void __vcpu_clear(void *arg)
207{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000208 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800209 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800210
Rusty Russell8b9cf982007-07-30 16:31:43 +1000211 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400212 vmcs_clear(vmx->vmcs);
213 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800214 per_cpu(current_vmcs, cpu) = NULL;
Rusty Russell8b9cf982007-07-30 16:31:43 +1000215 rdtscll(vmx->vcpu.host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800216}
217
Rusty Russell8b9cf982007-07-30 16:31:43 +1000218static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800219{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000220 if (vmx->vcpu.cpu != raw_smp_processor_id() && vmx->vcpu.cpu != -1)
221 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear,
222 vmx, 0, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800223 else
Rusty Russell8b9cf982007-07-30 16:31:43 +1000224 __vcpu_clear(vmx);
225 vmx->launched = 0;
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800226}
227
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228static unsigned long vmcs_readl(unsigned long field)
229{
230 unsigned long value;
231
232 asm volatile (ASM_VMX_VMREAD_RDX_RAX
233 : "=a"(value) : "d"(field) : "cc");
234 return value;
235}
236
237static u16 vmcs_read16(unsigned long field)
238{
239 return vmcs_readl(field);
240}
241
242static u32 vmcs_read32(unsigned long field)
243{
244 return vmcs_readl(field);
245}
246
247static u64 vmcs_read64(unsigned long field)
248{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800249#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800250 return vmcs_readl(field);
251#else
252 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
253#endif
254}
255
Avi Kivitye52de1b2007-01-05 16:36:56 -0800256static noinline void vmwrite_error(unsigned long field, unsigned long value)
257{
258 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
259 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
260 dump_stack();
261}
262
Avi Kivity6aa8b732006-12-10 02:21:36 -0800263static void vmcs_writel(unsigned long field, unsigned long value)
264{
265 u8 error;
266
267 asm volatile (ASM_VMX_VMWRITE_RAX_RDX "; setna %0"
268 : "=q"(error) : "a"(value), "d"(field) : "cc" );
Avi Kivitye52de1b2007-01-05 16:36:56 -0800269 if (unlikely(error))
270 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271}
272
273static void vmcs_write16(unsigned long field, u16 value)
274{
275 vmcs_writel(field, value);
276}
277
278static void vmcs_write32(unsigned long field, u32 value)
279{
280 vmcs_writel(field, value);
281}
282
283static void vmcs_write64(unsigned long field, u64 value)
284{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800285#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286 vmcs_writel(field, value);
287#else
288 vmcs_writel(field, value);
289 asm volatile ("");
290 vmcs_writel(field+1, value >> 32);
291#endif
292}
293
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300294static void vmcs_clear_bits(unsigned long field, u32 mask)
295{
296 vmcs_writel(field, vmcs_readl(field) & ~mask);
297}
298
299static void vmcs_set_bits(unsigned long field, u32 mask)
300{
301 vmcs_writel(field, vmcs_readl(field) | mask);
302}
303
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300304static void update_exception_bitmap(struct kvm_vcpu *vcpu)
305{
306 u32 eb;
307
308 eb = 1u << PF_VECTOR;
309 if (!vcpu->fpu_active)
310 eb |= 1u << NM_VECTOR;
311 if (vcpu->guest_debug.enabled)
312 eb |= 1u << 1;
313 if (vcpu->rmode.active)
314 eb = ~0;
315 vmcs_write32(EXCEPTION_BITMAP, eb);
316}
317
Avi Kivity33ed6322007-05-02 16:54:03 +0300318static void reload_tss(void)
319{
320#ifndef CONFIG_X86_64
321
322 /*
323 * VT restores TR but not its size. Useless.
324 */
325 struct descriptor_table gdt;
326 struct segment_descriptor *descs;
327
328 get_gdt(&gdt);
329 descs = (void *)gdt.base;
330 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
331 load_TR_desc();
332#endif
333}
334
Rusty Russell8b9cf982007-07-30 16:31:43 +1000335static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300336{
337 u64 trans_efer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400338 int efer_offset = vmx->msr_offset_efer;
Eddie Dong2cc51562007-05-21 07:28:09 +0300339
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400340 trans_efer = vmx->host_msrs[efer_offset].data;
Eddie Dong2cc51562007-05-21 07:28:09 +0300341 trans_efer &= ~EFER_SAVE_RESTORE_BITS;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400342 trans_efer |= msr_efer_save_restore_bits(vmx->guest_msrs[efer_offset]);
Eddie Dong2cc51562007-05-21 07:28:09 +0300343 wrmsrl(MSR_EFER, trans_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000344 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300345}
346
Rusty Russell8b9cf982007-07-30 16:31:43 +1000347static void vmx_save_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300348{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400349 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300350 return;
351
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400352 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300353 /*
354 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
355 * allow segment selectors with cpl > 0 or ti == 1.
356 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400357 vmx->host_state.ldt_sel = read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200358 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400359 vmx->host_state.fs_sel = read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200360 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400361 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200362 vmx->host_state.fs_reload_needed = 0;
363 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300364 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200365 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300366 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400367 vmx->host_state.gs_sel = read_gs();
368 if (!(vmx->host_state.gs_sel & 7))
369 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300370 else {
371 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200372 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300373 }
374
375#ifdef CONFIG_X86_64
376 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
377 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
378#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400379 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
380 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300381#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300382
383#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000384 if (is_long_mode(&vmx->vcpu)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400385 save_msrs(vmx->host_msrs +
386 vmx->msr_offset_kernel_gs_base, 1);
Avi Kivity707c0872007-05-02 17:33:43 +0300387 }
388#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400389 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000390 if (msr_efer_need_save_restore(vmx))
391 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300392}
393
Rusty Russell8b9cf982007-07-30 16:31:43 +1000394static void vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300395{
Avi Kivity15ad7142007-07-11 18:17:21 +0300396 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300397
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400398 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300399 return;
400
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400401 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200402 if (vmx->host_state.fs_reload_needed)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400403 load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200404 if (vmx->host_state.gs_ldt_reload_needed) {
405 load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300406 /*
407 * If we have to reload gs, we must take care to
408 * preserve our gs base.
409 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300410 local_irq_save(flags);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400411 load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300412#ifdef CONFIG_X86_64
413 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
414#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300415 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300416 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200417 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400418 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
419 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000420 if (msr_efer_need_save_restore(vmx))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400421 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
Avi Kivity33ed6322007-05-02 16:54:03 +0300422}
423
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424/*
425 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
426 * vcpu mutex is already taken.
427 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300428static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400430 struct vcpu_vmx *vmx = to_vmx(vcpu);
431 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity77002702007-06-13 19:55:28 +0300432 u64 tsc_this, delta;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800434 if (vcpu->cpu != cpu)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000435 vcpu_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400437 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438 u8 error;
439
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400440 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441 asm volatile (ASM_VMX_VMPTRLD_RAX "; setna %0"
442 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
443 : "cc");
444 if (error)
445 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400446 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800447 }
448
449 if (vcpu->cpu != cpu) {
450 struct descriptor_table dt;
451 unsigned long sysenter_esp;
452
453 vcpu->cpu = cpu;
454 /*
455 * Linux uses per-cpu TSS and GDT, so set these when switching
456 * processors.
457 */
458 vmcs_writel(HOST_TR_BASE, read_tr_base()); /* 22.2.4 */
459 get_gdt(&dt);
460 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
461
462 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
463 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300464
465 /*
466 * Make sure the time stamp counter is monotonous.
467 */
468 rdtscll(tsc_this);
469 delta = vcpu->host_tsc - tsc_this;
470 vmcs_write64(TSC_OFFSET, vmcs_read64(TSC_OFFSET) + delta);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800471 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472}
473
474static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
475{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000476 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity7702fd12007-06-14 16:27:40 +0300477 kvm_put_guest_fpu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800478}
479
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300480static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
481{
482 if (vcpu->fpu_active)
483 return;
484 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000485 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
486 if (vcpu->cr0 & X86_CR0_TS)
487 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300488 update_exception_bitmap(vcpu);
489}
490
491static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
492{
493 if (!vcpu->fpu_active)
494 return;
495 vcpu->fpu_active = 0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000496 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300497 update_exception_bitmap(vcpu);
498}
499
Avi Kivity774c47f2007-02-12 00:54:47 -0800500static void vmx_vcpu_decache(struct kvm_vcpu *vcpu)
501{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000502 vcpu_clear(to_vmx(vcpu));
Avi Kivity774c47f2007-02-12 00:54:47 -0800503}
504
Avi Kivity6aa8b732006-12-10 02:21:36 -0800505static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
506{
507 return vmcs_readl(GUEST_RFLAGS);
508}
509
510static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
511{
512 vmcs_writel(GUEST_RFLAGS, rflags);
513}
514
515static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
516{
517 unsigned long rip;
518 u32 interruptibility;
519
520 rip = vmcs_readl(GUEST_RIP);
521 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
522 vmcs_writel(GUEST_RIP, rip);
523
524 /*
525 * We emulated an instruction, so temporary interrupt blocking
526 * should be removed, if set.
527 */
528 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
529 if (interruptibility & 3)
530 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
531 interruptibility & ~3);
Dor Laorc1150d82007-01-05 16:36:24 -0800532 vcpu->interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533}
534
535static void vmx_inject_gp(struct kvm_vcpu *vcpu, unsigned error_code)
536{
537 printk(KERN_DEBUG "inject_general_protection: rip 0x%lx\n",
538 vmcs_readl(GUEST_RIP));
539 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
540 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
541 GP_VECTOR |
542 INTR_TYPE_EXCEPTION |
543 INTR_INFO_DELIEVER_CODE_MASK |
544 INTR_INFO_VALID_MASK);
545}
546
547/*
Eddie Donga75beee2007-05-17 18:55:15 +0300548 * Swap MSR entry in host/guest MSR entry array.
549 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200550#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000551static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300552{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400553 struct kvm_msr_entry tmp;
554
555 tmp = vmx->guest_msrs[to];
556 vmx->guest_msrs[to] = vmx->guest_msrs[from];
557 vmx->guest_msrs[from] = tmp;
558 tmp = vmx->host_msrs[to];
559 vmx->host_msrs[to] = vmx->host_msrs[from];
560 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300561}
Gabriel C54e11fa2007-08-01 16:23:10 +0200562#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300563
564/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300565 * Set up the vmcs to automatically save and restore system
566 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
567 * mode, as fiddling with msrs is very expensive.
568 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000569static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300570{
Eddie Dong2cc51562007-05-21 07:28:09 +0300571 int save_nmsrs;
Avi Kivitye38aea32007-04-19 13:22:48 +0300572
Eddie Donga75beee2007-05-17 18:55:15 +0300573 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300574#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000575 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300576 int index;
577
Rusty Russell8b9cf982007-07-30 16:31:43 +1000578 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300579 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000580 move_msr_up(vmx, index, save_nmsrs++);
581 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300582 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000583 move_msr_up(vmx, index, save_nmsrs++);
584 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300585 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000586 move_msr_up(vmx, index, save_nmsrs++);
587 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300588 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000589 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300590 /*
591 * MSR_K6_STAR is only needed on long mode guests, and only
592 * if efer.sce is enabled.
593 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000594 index = __find_msr_index(vmx, MSR_K6_STAR);
595 if ((index >= 0) && (vmx->vcpu.shadow_efer & EFER_SCE))
596 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300597 }
Eddie Donga75beee2007-05-17 18:55:15 +0300598#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400599 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300600
Eddie Donga75beee2007-05-17 18:55:15 +0300601#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400602 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000603 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300604#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000605 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivitye38aea32007-04-19 13:22:48 +0300606}
607
608/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800609 * reads and returns guest's timestamp counter "register"
610 * guest_tsc = host_tsc + tsc_offset -- 21.3
611 */
612static u64 guest_read_tsc(void)
613{
614 u64 host_tsc, tsc_offset;
615
616 rdtscll(host_tsc);
617 tsc_offset = vmcs_read64(TSC_OFFSET);
618 return host_tsc + tsc_offset;
619}
620
621/*
622 * writes 'guest_tsc' into guest's timestamp counter "register"
623 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
624 */
625static void guest_write_tsc(u64 guest_tsc)
626{
627 u64 host_tsc;
628
629 rdtscll(host_tsc);
630 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
631}
632
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633/*
634 * Reads an msr value (of 'msr_index') into 'pdata'.
635 * Returns 0 on success, non-0 otherwise.
636 * Assumes vcpu_load() was already called.
637 */
638static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
639{
640 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400641 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800642
643 if (!pdata) {
644 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
645 return -EINVAL;
646 }
647
648 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800649#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650 case MSR_FS_BASE:
651 data = vmcs_readl(GUEST_FS_BASE);
652 break;
653 case MSR_GS_BASE:
654 data = vmcs_readl(GUEST_GS_BASE);
655 break;
656 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800657 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658#endif
659 case MSR_IA32_TIME_STAMP_COUNTER:
660 data = guest_read_tsc();
661 break;
662 case MSR_IA32_SYSENTER_CS:
663 data = vmcs_read32(GUEST_SYSENTER_CS);
664 break;
665 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200666 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800667 break;
668 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200669 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800670 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000672 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800673 if (msr) {
674 data = msr->data;
675 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800677 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800678 }
679
680 *pdata = data;
681 return 0;
682}
683
684/*
685 * Writes msr value into into the appropriate "register".
686 * Returns 0 on success, non-0 otherwise.
687 * Assumes vcpu_load() was already called.
688 */
689static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
690{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400691 struct vcpu_vmx *vmx = to_vmx(vcpu);
692 struct kvm_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +0300693 int ret = 0;
694
Avi Kivity6aa8b732006-12-10 02:21:36 -0800695 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800696#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800697 case MSR_EFER:
Eddie Dong2cc51562007-05-21 07:28:09 +0300698 ret = kvm_set_msr_common(vcpu, msr_index, data);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400699 if (vmx->host_state.loaded)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000700 load_transition_efer(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +0300701 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800702 case MSR_FS_BASE:
703 vmcs_writel(GUEST_FS_BASE, data);
704 break;
705 case MSR_GS_BASE:
706 vmcs_writel(GUEST_GS_BASE, data);
707 break;
708#endif
709 case MSR_IA32_SYSENTER_CS:
710 vmcs_write32(GUEST_SYSENTER_CS, data);
711 break;
712 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200713 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714 break;
715 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200716 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800717 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200718 case MSR_IA32_TIME_STAMP_COUNTER:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719 guest_write_tsc(data);
720 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000722 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800723 if (msr) {
724 msr->data = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400725 if (vmx->host_state.loaded)
726 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800727 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300729 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730 }
731
Eddie Dong2cc51562007-05-21 07:28:09 +0300732 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733}
734
735/*
736 * Sync the rsp and rip registers into the vcpu structure. This allows
737 * registers to be accessed by indexing vcpu->regs.
738 */
739static void vcpu_load_rsp_rip(struct kvm_vcpu *vcpu)
740{
741 vcpu->regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
742 vcpu->rip = vmcs_readl(GUEST_RIP);
743}
744
745/*
746 * Syncs rsp and rip back into the vmcs. Should be called after possible
747 * modification.
748 */
749static void vcpu_put_rsp_rip(struct kvm_vcpu *vcpu)
750{
751 vmcs_writel(GUEST_RSP, vcpu->regs[VCPU_REGS_RSP]);
752 vmcs_writel(GUEST_RIP, vcpu->rip);
753}
754
755static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
756{
757 unsigned long dr7 = 0x400;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758 int old_singlestep;
759
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760 old_singlestep = vcpu->guest_debug.singlestep;
761
762 vcpu->guest_debug.enabled = dbg->enabled;
763 if (vcpu->guest_debug.enabled) {
764 int i;
765
766 dr7 |= 0x200; /* exact */
767 for (i = 0; i < 4; ++i) {
768 if (!dbg->breakpoints[i].enabled)
769 continue;
770 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
771 dr7 |= 2 << (i*2); /* global enable */
772 dr7 |= 0 << (i*4+16); /* execution breakpoint */
773 }
774
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775 vcpu->guest_debug.singlestep = dbg->singlestep;
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300776 } else
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777 vcpu->guest_debug.singlestep = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778
779 if (old_singlestep && !vcpu->guest_debug.singlestep) {
780 unsigned long flags;
781
782 flags = vmcs_readl(GUEST_RFLAGS);
783 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
784 vmcs_writel(GUEST_RFLAGS, flags);
785 }
786
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300787 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788 vmcs_writel(GUEST_DR7, dr7);
789
790 return 0;
791}
792
793static __init int cpu_has_kvm_support(void)
794{
795 unsigned long ecx = cpuid_ecx(1);
796 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
797}
798
799static __init int vmx_disabled_by_bios(void)
800{
801 u64 msr;
802
803 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300804 return (msr & (MSR_IA32_FEATURE_CONTROL_LOCKED |
805 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
806 == MSR_IA32_FEATURE_CONTROL_LOCKED;
807 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800808}
809
Avi Kivity774c47f2007-02-12 00:54:47 -0800810static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811{
812 int cpu = raw_smp_processor_id();
813 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
814 u64 old;
815
816 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300817 if ((old & (MSR_IA32_FEATURE_CONTROL_LOCKED |
818 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
819 != (MSR_IA32_FEATURE_CONTROL_LOCKED |
820 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300822 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
823 MSR_IA32_FEATURE_CONTROL_LOCKED |
824 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +1000825 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800826 asm volatile (ASM_VMX_VMXON_RAX : : "a"(&phys_addr), "m"(phys_addr)
827 : "memory", "cc");
828}
829
830static void hardware_disable(void *garbage)
831{
832 asm volatile (ASM_VMX_VMXOFF : : : "cc");
833}
834
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300835static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
836 u32 msr, u32* result)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837{
838 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300839 u32 ctl = ctl_min | ctl_opt;
840
841 rdmsr(msr, vmx_msr_low, vmx_msr_high);
842
843 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
844 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
845
846 /* Ensure minimum (required) set of control bits are supported. */
847 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300848 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300849
850 *result = ctl;
851 return 0;
852}
853
Yang, Sheng002c7f72007-07-31 14:23:01 +0300854static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300855{
856 u32 vmx_msr_low, vmx_msr_high;
857 u32 min, opt;
858 u32 _pin_based_exec_control = 0;
859 u32 _cpu_based_exec_control = 0;
860 u32 _vmexit_control = 0;
861 u32 _vmentry_control = 0;
862
863 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
864 opt = 0;
865 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
866 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300867 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300868
869 min = CPU_BASED_HLT_EXITING |
870#ifdef CONFIG_X86_64
871 CPU_BASED_CR8_LOAD_EXITING |
872 CPU_BASED_CR8_STORE_EXITING |
873#endif
874 CPU_BASED_USE_IO_BITMAPS |
875 CPU_BASED_MOV_DR_EXITING |
876 CPU_BASED_USE_TSC_OFFSETING;
877 opt = 0;
878 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
879 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300880 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300881
882 min = 0;
883#ifdef CONFIG_X86_64
884 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
885#endif
886 opt = 0;
887 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
888 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300889 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300890
891 min = opt = 0;
892 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
893 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300894 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -0800896 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300897
898 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
899 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300900 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300901
902#ifdef CONFIG_X86_64
903 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
904 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +0300905 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300906#endif
907
908 /* Require Write-Back (WB) memory type for VMCS accesses. */
909 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300910 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300911
Yang, Sheng002c7f72007-07-31 14:23:01 +0300912 vmcs_conf->size = vmx_msr_high & 0x1fff;
913 vmcs_conf->order = get_order(vmcs_config.size);
914 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300915
Yang, Sheng002c7f72007-07-31 14:23:01 +0300916 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
917 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
918 vmcs_conf->vmexit_ctrl = _vmexit_control;
919 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300920
921 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -0800922}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923
924static struct vmcs *alloc_vmcs_cpu(int cpu)
925{
926 int node = cpu_to_node(cpu);
927 struct page *pages;
928 struct vmcs *vmcs;
929
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300930 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931 if (!pages)
932 return NULL;
933 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300934 memset(vmcs, 0, vmcs_config.size);
935 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 return vmcs;
937}
938
939static struct vmcs *alloc_vmcs(void)
940{
Ingo Molnard3b2c332007-01-05 16:36:23 -0800941 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942}
943
944static void free_vmcs(struct vmcs *vmcs)
945{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300946 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947}
948
Sam Ravnborg39959582007-06-01 00:47:13 -0700949static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950{
951 int cpu;
952
953 for_each_online_cpu(cpu)
954 free_vmcs(per_cpu(vmxarea, cpu));
955}
956
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957static __init int alloc_kvm_area(void)
958{
959 int cpu;
960
961 for_each_online_cpu(cpu) {
962 struct vmcs *vmcs;
963
964 vmcs = alloc_vmcs_cpu(cpu);
965 if (!vmcs) {
966 free_kvm_area();
967 return -ENOMEM;
968 }
969
970 per_cpu(vmxarea, cpu) = vmcs;
971 }
972 return 0;
973}
974
975static __init int hardware_setup(void)
976{
Yang, Sheng002c7f72007-07-31 14:23:01 +0300977 if (setup_vmcs_config(&vmcs_config) < 0)
978 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 return alloc_kvm_area();
980}
981
982static __exit void hardware_unsetup(void)
983{
984 free_kvm_area();
985}
986
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
988{
989 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
990
Avi Kivity6af11b92007-03-19 13:18:10 +0200991 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 vmcs_write16(sf->selector, save->selector);
993 vmcs_writel(sf->base, save->base);
994 vmcs_write32(sf->limit, save->limit);
995 vmcs_write32(sf->ar_bytes, save->ar);
996 } else {
997 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
998 << AR_DPL_SHIFT;
999 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1000 }
1001}
1002
1003static void enter_pmode(struct kvm_vcpu *vcpu)
1004{
1005 unsigned long flags;
1006
1007 vcpu->rmode.active = 0;
1008
1009 vmcs_writel(GUEST_TR_BASE, vcpu->rmode.tr.base);
1010 vmcs_write32(GUEST_TR_LIMIT, vcpu->rmode.tr.limit);
1011 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->rmode.tr.ar);
1012
1013 flags = vmcs_readl(GUEST_RFLAGS);
1014 flags &= ~(IOPL_MASK | X86_EFLAGS_VM);
1015 flags |= (vcpu->rmode.save_iopl << IOPL_SHIFT);
1016 vmcs_writel(GUEST_RFLAGS, flags);
1017
Rusty Russell66aee912007-07-17 23:34:16 +10001018 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1019 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020
1021 update_exception_bitmap(vcpu);
1022
1023 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->rmode.es);
1024 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->rmode.ds);
1025 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->rmode.gs);
1026 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->rmode.fs);
1027
1028 vmcs_write16(GUEST_SS_SELECTOR, 0);
1029 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1030
1031 vmcs_write16(GUEST_CS_SELECTOR,
1032 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1033 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1034}
1035
Izik Eidus33f5fa12007-08-19 22:24:58 +03001036static gva_t rmode_tss_base(struct kvm* kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037{
1038 gfn_t base_gfn = kvm->memslots[0].base_gfn + kvm->memslots[0].npages - 3;
1039 return base_gfn << PAGE_SHIFT;
1040}
1041
1042static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1043{
1044 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1045
1046 save->selector = vmcs_read16(sf->selector);
1047 save->base = vmcs_readl(sf->base);
1048 save->limit = vmcs_read32(sf->limit);
1049 save->ar = vmcs_read32(sf->ar_bytes);
1050 vmcs_write16(sf->selector, vmcs_readl(sf->base) >> 4);
1051 vmcs_write32(sf->limit, 0xffff);
1052 vmcs_write32(sf->ar_bytes, 0xf3);
1053}
1054
1055static void enter_rmode(struct kvm_vcpu *vcpu)
1056{
1057 unsigned long flags;
1058
1059 vcpu->rmode.active = 1;
1060
1061 vcpu->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
1062 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1063
1064 vcpu->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
1065 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1066
1067 vcpu->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
1068 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1069
1070 flags = vmcs_readl(GUEST_RFLAGS);
1071 vcpu->rmode.save_iopl = (flags & IOPL_MASK) >> IOPL_SHIFT;
1072
1073 flags |= IOPL_MASK | X86_EFLAGS_VM;
1074
1075 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001076 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077 update_exception_bitmap(vcpu);
1078
1079 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1080 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1081 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1082
1083 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001084 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001085 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1086 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1088
1089 fix_rmode_seg(VCPU_SREG_ES, &vcpu->rmode.es);
1090 fix_rmode_seg(VCPU_SREG_DS, &vcpu->rmode.ds);
1091 fix_rmode_seg(VCPU_SREG_GS, &vcpu->rmode.gs);
1092 fix_rmode_seg(VCPU_SREG_FS, &vcpu->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001093
1094 init_rmode_tss(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095}
1096
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001097#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098
1099static void enter_lmode(struct kvm_vcpu *vcpu)
1100{
1101 u32 guest_tr_ar;
1102
1103 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1104 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1105 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
1106 __FUNCTION__);
1107 vmcs_write32(GUEST_TR_AR_BYTES,
1108 (guest_tr_ar & ~AR_TYPE_MASK)
1109 | AR_TYPE_BUSY_64_TSS);
1110 }
1111
1112 vcpu->shadow_efer |= EFER_LMA;
1113
Rusty Russell8b9cf982007-07-30 16:31:43 +10001114 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115 vmcs_write32(VM_ENTRY_CONTROLS,
1116 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001117 | VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118}
1119
1120static void exit_lmode(struct kvm_vcpu *vcpu)
1121{
1122 vcpu->shadow_efer &= ~EFER_LMA;
1123
1124 vmcs_write32(VM_ENTRY_CONTROLS,
1125 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001126 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127}
1128
1129#endif
1130
Anthony Liguori25c4c272007-04-27 09:29:21 +03001131static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001132{
Avi Kivity399badf2007-01-05 16:36:38 -08001133 vcpu->cr4 &= KVM_GUEST_CR4_MASK;
1134 vcpu->cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
1135}
1136
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1138{
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001139 vmx_fpu_deactivate(vcpu);
1140
Rusty Russell707d92fa2007-07-17 23:19:08 +10001141 if (vcpu->rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142 enter_pmode(vcpu);
1143
Rusty Russell707d92fa2007-07-17 23:19:08 +10001144 if (!vcpu->rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145 enter_rmode(vcpu);
1146
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001147#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148 if (vcpu->shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001149 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001151 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152 exit_lmode(vcpu);
1153 }
1154#endif
1155
1156 vmcs_writel(CR0_READ_SHADOW, cr0);
1157 vmcs_writel(GUEST_CR0,
1158 (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON);
1159 vcpu->cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001160
Rusty Russell707d92fa2007-07-17 23:19:08 +10001161 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001162 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163}
1164
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1166{
1167 vmcs_writel(GUEST_CR3, cr3);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001168 if (vcpu->cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001169 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170}
1171
1172static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1173{
1174 vmcs_writel(CR4_READ_SHADOW, cr4);
1175 vmcs_writel(GUEST_CR4, cr4 | (vcpu->rmode.active ?
1176 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON));
1177 vcpu->cr4 = cr4;
1178}
1179
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001180#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001181
1182static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1183{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001184 struct vcpu_vmx *vmx = to_vmx(vcpu);
1185 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186
1187 vcpu->shadow_efer = efer;
1188 if (efer & EFER_LMA) {
1189 vmcs_write32(VM_ENTRY_CONTROLS,
1190 vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001191 VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001192 msr->data = efer;
1193
1194 } else {
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 msr->data = efer & ~EFER_LME;
1200 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001201 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001202}
1203
1204#endif
1205
1206static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1207{
1208 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1209
1210 return vmcs_readl(sf->base);
1211}
1212
1213static void vmx_get_segment(struct kvm_vcpu *vcpu,
1214 struct kvm_segment *var, int seg)
1215{
1216 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1217 u32 ar;
1218
1219 var->base = vmcs_readl(sf->base);
1220 var->limit = vmcs_read32(sf->limit);
1221 var->selector = vmcs_read16(sf->selector);
1222 ar = vmcs_read32(sf->ar_bytes);
1223 if (ar & AR_UNUSABLE_MASK)
1224 ar = 0;
1225 var->type = ar & 15;
1226 var->s = (ar >> 4) & 1;
1227 var->dpl = (ar >> 5) & 3;
1228 var->present = (ar >> 7) & 1;
1229 var->avl = (ar >> 12) & 1;
1230 var->l = (ar >> 13) & 1;
1231 var->db = (ar >> 14) & 1;
1232 var->g = (ar >> 15) & 1;
1233 var->unusable = (ar >> 16) & 1;
1234}
1235
Avi Kivity653e3102007-05-07 10:55:37 +03001236static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001237{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001238 u32 ar;
1239
Avi Kivity653e3102007-05-07 10:55:37 +03001240 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001241 ar = 1 << 16;
1242 else {
1243 ar = var->type & 15;
1244 ar |= (var->s & 1) << 4;
1245 ar |= (var->dpl & 3) << 5;
1246 ar |= (var->present & 1) << 7;
1247 ar |= (var->avl & 1) << 12;
1248 ar |= (var->l & 1) << 13;
1249 ar |= (var->db & 1) << 14;
1250 ar |= (var->g & 1) << 15;
1251 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001252 if (ar == 0) /* a 0 value means unusable */
1253 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001254
1255 return ar;
1256}
1257
1258static void vmx_set_segment(struct kvm_vcpu *vcpu,
1259 struct kvm_segment *var, int seg)
1260{
1261 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1262 u32 ar;
1263
1264 if (vcpu->rmode.active && seg == VCPU_SREG_TR) {
1265 vcpu->rmode.tr.selector = var->selector;
1266 vcpu->rmode.tr.base = var->base;
1267 vcpu->rmode.tr.limit = var->limit;
1268 vcpu->rmode.tr.ar = vmx_segment_access_rights(var);
1269 return;
1270 }
1271 vmcs_writel(sf->base, var->base);
1272 vmcs_write32(sf->limit, var->limit);
1273 vmcs_write16(sf->selector, var->selector);
1274 if (vcpu->rmode.active && var->s) {
1275 /*
1276 * Hack real-mode segments into vm86 compatibility.
1277 */
1278 if (var->base == 0xffff0000 && var->selector == 0xf000)
1279 vmcs_writel(sf->base, 0xf0000);
1280 ar = 0xf3;
1281 } else
1282 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283 vmcs_write32(sf->ar_bytes, ar);
1284}
1285
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1287{
1288 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1289
1290 *db = (ar >> 14) & 1;
1291 *l = (ar >> 13) & 1;
1292}
1293
1294static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1295{
1296 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1297 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1298}
1299
1300static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1301{
1302 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1303 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1304}
1305
1306static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1307{
1308 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1309 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1310}
1311
1312static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1313{
1314 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1315 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1316}
1317
1318static int init_rmode_tss(struct kvm* kvm)
1319{
1320 struct page *p1, *p2, *p3;
1321 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
1322 char *page;
1323
Avi Kivity954bbbc2007-03-30 14:02:32 +03001324 p1 = gfn_to_page(kvm, fn++);
1325 p2 = gfn_to_page(kvm, fn++);
1326 p3 = gfn_to_page(kvm, fn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327
1328 if (!p1 || !p2 || !p3) {
1329 kvm_printf(kvm,"%s: gfn_to_page failed\n", __FUNCTION__);
1330 return 0;
1331 }
1332
1333 page = kmap_atomic(p1, KM_USER0);
Shani Moideena3870c42007-06-11 09:31:33 +05301334 clear_page(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335 *(u16*)(page + 0x66) = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
1336 kunmap_atomic(page, KM_USER0);
1337
1338 page = kmap_atomic(p2, KM_USER0);
Shani Moideena3870c42007-06-11 09:31:33 +05301339 clear_page(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340 kunmap_atomic(page, KM_USER0);
1341
1342 page = kmap_atomic(p3, KM_USER0);
Shani Moideena3870c42007-06-11 09:31:33 +05301343 clear_page(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344 *(page + RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1) = ~0;
1345 kunmap_atomic(page, KM_USER0);
1346
1347 return 1;
1348}
1349
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350static void seg_setup(int seg)
1351{
1352 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1353
1354 vmcs_write16(sf->selector, 0);
1355 vmcs_writel(sf->base, 0);
1356 vmcs_write32(sf->limit, 0xffff);
1357 vmcs_write32(sf->ar_bytes, 0x93);
1358}
1359
1360/*
1361 * Sets up the vmcs for emulated real mode.
1362 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001363static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364{
1365 u32 host_sysenter_cs;
1366 u32 junk;
1367 unsigned long a;
1368 struct descriptor_table dt;
1369 int i;
1370 int ret = 0;
Avi Kivitycd2276a2007-05-14 20:41:13 +03001371 unsigned long kvm_vmx_return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372
Rusty Russell8b9cf982007-07-30 16:31:43 +10001373 if (!init_rmode_tss(vmx->vcpu.kvm)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374 ret = -ENOMEM;
1375 goto out;
1376 }
1377
Rusty Russell8b9cf982007-07-30 16:31:43 +10001378 vmx->vcpu.regs[VCPU_REGS_RDX] = get_rdx_init_val();
1379 vmx->vcpu.cr8 = 0;
1380 vmx->vcpu.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
1381 if (vmx->vcpu.vcpu_id == 0)
1382 vmx->vcpu.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383
Rusty Russell8b9cf982007-07-30 16:31:43 +10001384 fx_init(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385
1386 /*
1387 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
1388 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
1389 */
1390 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
1391 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
1392 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
1393 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1394
1395 seg_setup(VCPU_SREG_DS);
1396 seg_setup(VCPU_SREG_ES);
1397 seg_setup(VCPU_SREG_FS);
1398 seg_setup(VCPU_SREG_GS);
1399 seg_setup(VCPU_SREG_SS);
1400
1401 vmcs_write16(GUEST_TR_SELECTOR, 0);
1402 vmcs_writel(GUEST_TR_BASE, 0);
1403 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
1404 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1405
1406 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
1407 vmcs_writel(GUEST_LDTR_BASE, 0);
1408 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
1409 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
1410
1411 vmcs_write32(GUEST_SYSENTER_CS, 0);
1412 vmcs_writel(GUEST_SYSENTER_ESP, 0);
1413 vmcs_writel(GUEST_SYSENTER_EIP, 0);
1414
1415 vmcs_writel(GUEST_RFLAGS, 0x02);
1416 vmcs_writel(GUEST_RIP, 0xfff0);
1417 vmcs_writel(GUEST_RSP, 0);
1418
Avi Kivity6aa8b732006-12-10 02:21:36 -08001419 //todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0
1420 vmcs_writel(GUEST_DR7, 0x400);
1421
1422 vmcs_writel(GUEST_GDTR_BASE, 0);
1423 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
1424
1425 vmcs_writel(GUEST_IDTR_BASE, 0);
1426 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
1427
1428 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
1429 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
1430 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
1431
1432 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03001433 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
1434 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001435
1436 guest_write_tsc(0);
1437
1438 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
1439
1440 /* Special registers */
1441 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
1442
1443 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001444 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
1445 vmcs_config.pin_based_exec_ctrl);
1446 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
1447 vmcs_config.cpu_based_exec_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001448
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
1450 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
1451 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
1452
1453 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
1454 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
1455 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1456
1457 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
1458 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1459 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1460 vmcs_write16(HOST_FS_SELECTOR, read_fs()); /* 22.2.4 */
1461 vmcs_write16(HOST_GS_SELECTOR, read_gs()); /* 22.2.4 */
1462 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001463#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001464 rdmsrl(MSR_FS_BASE, a);
1465 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
1466 rdmsrl(MSR_GS_BASE, a);
1467 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
1468#else
1469 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
1470 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
1471#endif
1472
1473 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
1474
1475 get_idt(&dt);
1476 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
1477
Avi Kivitycd2276a2007-05-14 20:41:13 +03001478 asm ("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
1479 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03001480 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
1481 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
1482 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483
1484 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
1485 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
1486 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
1487 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
1488 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
1489 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
1490
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491 for (i = 0; i < NR_VMX_MSR; ++i) {
1492 u32 index = vmx_msr_index[i];
1493 u32 data_low, data_high;
1494 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001495 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496
1497 if (rdmsr_safe(index, &data_low, &data_high) < 0)
1498 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08001499 if (wrmsr_safe(index, data_low, data_high) < 0)
1500 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001502 vmx->host_msrs[j].index = index;
1503 vmx->host_msrs[j].reserved = 0;
1504 vmx->host_msrs[j].data = data;
1505 vmx->guest_msrs[j] = vmx->host_msrs[j];
1506 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508
Rusty Russell8b9cf982007-07-30 16:31:43 +10001509 setup_msrs(vmx);
Avi Kivitye38aea32007-04-19 13:22:48 +03001510
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001511 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512
1513 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001514 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
1515
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
1517
Michael Riepe3b99ab22006-12-13 00:34:15 -08001518#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519 vmcs_writel(VIRTUAL_APIC_PAGE_ADDR, 0);
1520 vmcs_writel(TPR_THRESHOLD, 0);
Michael Riepe3b99ab22006-12-13 00:34:15 -08001521#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522
Anthony Liguori25c4c272007-04-27 09:29:21 +03001523 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
1525
Rusty Russell8b9cf982007-07-30 16:31:43 +10001526 vmx->vcpu.cr0 = 0x60000010;
1527 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.cr0); // enter rmode
1528 vmx_set_cr4(&vmx->vcpu, 0);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001529#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001530 vmx_set_efer(&vmx->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001531#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +10001532 vmx_fpu_activate(&vmx->vcpu);
1533 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001534
1535 return 0;
1536
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537out:
1538 return ret;
1539}
1540
1541static void inject_rmode_irq(struct kvm_vcpu *vcpu, int irq)
1542{
1543 u16 ent[2];
1544 u16 cs;
1545 u16 ip;
1546 unsigned long flags;
1547 unsigned long ss_base = vmcs_readl(GUEST_SS_BASE);
1548 u16 sp = vmcs_readl(GUEST_RSP);
1549 u32 ss_limit = vmcs_read32(GUEST_SS_LIMIT);
1550
Eric Sesterhenn / Snakebyte39649942007-04-09 16:15:05 +02001551 if (sp > ss_limit || sp < 6 ) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001552 vcpu_printf(vcpu, "%s: #SS, rsp 0x%lx ss 0x%lx limit 0x%x\n",
1553 __FUNCTION__,
1554 vmcs_readl(GUEST_RSP),
1555 vmcs_readl(GUEST_SS_BASE),
1556 vmcs_read32(GUEST_SS_LIMIT));
1557 return;
1558 }
1559
Laurent Viviere7d5d762007-07-30 13:41:19 +03001560 if (emulator_read_std(irq * sizeof(ent), &ent, sizeof(ent), vcpu) !=
1561 X86EMUL_CONTINUE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001562 vcpu_printf(vcpu, "%s: read guest err\n", __FUNCTION__);
1563 return;
1564 }
1565
1566 flags = vmcs_readl(GUEST_RFLAGS);
1567 cs = vmcs_readl(GUEST_CS_BASE) >> 4;
1568 ip = vmcs_readl(GUEST_RIP);
1569
1570
Laurent Viviere7d5d762007-07-30 13:41:19 +03001571 if (emulator_write_emulated(ss_base + sp - 2, &flags, 2, vcpu) != X86EMUL_CONTINUE ||
1572 emulator_write_emulated(ss_base + sp - 4, &cs, 2, vcpu) != X86EMUL_CONTINUE ||
1573 emulator_write_emulated(ss_base + sp - 6, &ip, 2, vcpu) != X86EMUL_CONTINUE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001574 vcpu_printf(vcpu, "%s: write guest err\n", __FUNCTION__);
1575 return;
1576 }
1577
1578 vmcs_writel(GUEST_RFLAGS, flags &
1579 ~( X86_EFLAGS_IF | X86_EFLAGS_AC | X86_EFLAGS_TF));
1580 vmcs_write16(GUEST_CS_SELECTOR, ent[1]) ;
1581 vmcs_writel(GUEST_CS_BASE, ent[1] << 4);
1582 vmcs_writel(GUEST_RIP, ent[0]);
1583 vmcs_writel(GUEST_RSP, (vmcs_readl(GUEST_RSP) & ~0xffff) | (sp - 6));
1584}
1585
Eddie Dong85f455f2007-07-06 12:20:49 +03001586static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
1587{
1588 if (vcpu->rmode.active) {
1589 inject_rmode_irq(vcpu, irq);
1590 return;
1591 }
1592 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
1593 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1594}
1595
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
1597{
1598 int word_index = __ffs(vcpu->irq_summary);
1599 int bit_index = __ffs(vcpu->irq_pending[word_index]);
1600 int irq = word_index * BITS_PER_LONG + bit_index;
1601
1602 clear_bit(bit_index, &vcpu->irq_pending[word_index]);
1603 if (!vcpu->irq_pending[word_index])
1604 clear_bit(word_index, &vcpu->irq_summary);
Eddie Dong85f455f2007-07-06 12:20:49 +03001605 vmx_inject_irq(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001606}
1607
Dor Laorc1150d82007-01-05 16:36:24 -08001608
1609static void do_interrupt_requests(struct kvm_vcpu *vcpu,
1610 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611{
Dor Laorc1150d82007-01-05 16:36:24 -08001612 u32 cpu_based_vm_exec_control;
1613
1614 vcpu->interrupt_window_open =
1615 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
1616 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
1617
1618 if (vcpu->interrupt_window_open &&
1619 vcpu->irq_summary &&
1620 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621 /*
Dor Laorc1150d82007-01-05 16:36:24 -08001622 * If interrupts enabled, and not blocked by sti or mov ss. Good.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001623 */
1624 kvm_do_inject_irq(vcpu);
Dor Laorc1150d82007-01-05 16:36:24 -08001625
1626 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
1627 if (!vcpu->interrupt_window_open &&
1628 (vcpu->irq_summary || kvm_run->request_interrupt_window))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629 /*
1630 * Interrupts blocked. Wait for unblock.
1631 */
Dor Laorc1150d82007-01-05 16:36:24 -08001632 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
1633 else
1634 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
1635 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001636}
1637
1638static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
1639{
1640 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
1641
1642 set_debugreg(dbg->bp[0], 0);
1643 set_debugreg(dbg->bp[1], 1);
1644 set_debugreg(dbg->bp[2], 2);
1645 set_debugreg(dbg->bp[3], 3);
1646
1647 if (dbg->singlestep) {
1648 unsigned long flags;
1649
1650 flags = vmcs_readl(GUEST_RFLAGS);
1651 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1652 vmcs_writel(GUEST_RFLAGS, flags);
1653 }
1654}
1655
1656static int handle_rmode_exception(struct kvm_vcpu *vcpu,
1657 int vec, u32 err_code)
1658{
1659 if (!vcpu->rmode.active)
1660 return 0;
1661
Nitin A Kambleb3f37702007-05-17 15:50:34 +03001662 /*
1663 * Instruction with address size override prefix opcode 0x67
1664 * Cause the #SS fault with 0 error code in VM86 mode.
1665 */
1666 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667 if (emulate_instruction(vcpu, NULL, 0, 0) == EMULATE_DONE)
1668 return 1;
1669 return 0;
1670}
1671
1672static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1673{
1674 u32 intr_info, error_code;
1675 unsigned long cr2, rip;
1676 u32 vect_info;
1677 enum emulation_result er;
Avi Kivitye2dec932007-01-05 16:36:54 -08001678 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679
1680 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
1681 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
1682
1683 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
1684 !is_page_fault(intr_info)) {
1685 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
1686 "intr info 0x%x\n", __FUNCTION__, vect_info, intr_info);
1687 }
1688
Eddie Dong85f455f2007-07-06 12:20:49 +03001689 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
1691 set_bit(irq, vcpu->irq_pending);
1692 set_bit(irq / BITS_PER_LONG, &vcpu->irq_summary);
1693 }
1694
1695 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) { /* nmi */
1696 asm ("int $2");
1697 return 1;
1698 }
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001699
1700 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001701 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001702 return 1;
1703 }
1704
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705 error_code = 0;
1706 rip = vmcs_readl(GUEST_RIP);
1707 if (intr_info & INTR_INFO_DELIEVER_CODE_MASK)
1708 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
1709 if (is_page_fault(intr_info)) {
1710 cr2 = vmcs_readl(EXIT_QUALIFICATION);
1711
Shaohua Li11ec2802007-07-23 14:51:37 +08001712 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001713 r = kvm_mmu_page_fault(vcpu, cr2, error_code);
1714 if (r < 0) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001715 mutex_unlock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001716 return r;
1717 }
1718 if (!r) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001719 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720 return 1;
1721 }
1722
1723 er = emulate_instruction(vcpu, kvm_run, cr2, error_code);
Shaohua Li11ec2802007-07-23 14:51:37 +08001724 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725
1726 switch (er) {
1727 case EMULATE_DONE:
1728 return 1;
1729 case EMULATE_DO_MMIO:
Avi Kivity1165f5f2007-04-19 17:27:43 +03001730 ++vcpu->stat.mmio_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001731 return 0;
1732 case EMULATE_FAIL:
1733 vcpu_printf(vcpu, "%s: emulate fail\n", __FUNCTION__);
1734 break;
1735 default:
1736 BUG();
1737 }
1738 }
1739
1740 if (vcpu->rmode.active &&
1741 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03001742 error_code)) {
1743 if (vcpu->halt_request) {
1744 vcpu->halt_request = 0;
1745 return kvm_emulate_halt(vcpu);
1746 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001747 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03001748 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001749
1750 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) == (INTR_TYPE_EXCEPTION | 1)) {
1751 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1752 return 0;
1753 }
1754 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
1755 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
1756 kvm_run->ex.error_code = error_code;
1757 return 0;
1758}
1759
1760static int handle_external_interrupt(struct kvm_vcpu *vcpu,
1761 struct kvm_run *kvm_run)
1762{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001763 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001764 return 1;
1765}
1766
Avi Kivity988ad742007-02-12 00:54:36 -08001767static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1768{
1769 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1770 return 0;
1771}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001772
Avi Kivity6aa8b732006-12-10 02:21:36 -08001773static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1774{
1775 u64 exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02001776 int size, down, in, string, rep;
1777 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778
Avi Kivity1165f5f2007-04-19 17:27:43 +03001779 ++vcpu->stat.io_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001780 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02001781 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001782
1783 if (string) {
1784 if (emulate_instruction(vcpu, kvm_run, 0, 0) == EMULATE_DO_MMIO)
1785 return 0;
1786 return 1;
1787 }
1788
1789 size = (exit_qualification & 7) + 1;
1790 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001791 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001792 rep = (exit_qualification & 32) != 0;
1793 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03001794
Laurent Vivier3090dd72007-08-05 10:43:32 +03001795 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001796}
1797
Ingo Molnar102d8322007-02-19 14:37:47 +02001798static void
1799vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
1800{
1801 /*
1802 * Patch in the VMCALL instruction:
1803 */
1804 hypercall[0] = 0x0f;
1805 hypercall[1] = 0x01;
1806 hypercall[2] = 0xc1;
1807 hypercall[3] = 0xc3;
1808}
1809
Avi Kivity6aa8b732006-12-10 02:21:36 -08001810static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1811{
1812 u64 exit_qualification;
1813 int cr;
1814 int reg;
1815
1816 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
1817 cr = exit_qualification & 15;
1818 reg = (exit_qualification >> 8) & 15;
1819 switch ((exit_qualification >> 4) & 3) {
1820 case 0: /* mov to cr */
1821 switch (cr) {
1822 case 0:
1823 vcpu_load_rsp_rip(vcpu);
1824 set_cr0(vcpu, vcpu->regs[reg]);
1825 skip_emulated_instruction(vcpu);
1826 return 1;
1827 case 3:
1828 vcpu_load_rsp_rip(vcpu);
1829 set_cr3(vcpu, vcpu->regs[reg]);
1830 skip_emulated_instruction(vcpu);
1831 return 1;
1832 case 4:
1833 vcpu_load_rsp_rip(vcpu);
1834 set_cr4(vcpu, vcpu->regs[reg]);
1835 skip_emulated_instruction(vcpu);
1836 return 1;
1837 case 8:
1838 vcpu_load_rsp_rip(vcpu);
1839 set_cr8(vcpu, vcpu->regs[reg]);
1840 skip_emulated_instruction(vcpu);
Yang, Sheng253abde2007-08-16 13:01:00 +03001841 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
1842 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001843 };
1844 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03001845 case 2: /* clts */
1846 vcpu_load_rsp_rip(vcpu);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001847 vmx_fpu_deactivate(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001848 vcpu->cr0 &= ~X86_CR0_TS;
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001849 vmcs_writel(CR0_READ_SHADOW, vcpu->cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001850 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03001851 skip_emulated_instruction(vcpu);
1852 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001853 case 1: /*mov from cr*/
1854 switch (cr) {
1855 case 3:
1856 vcpu_load_rsp_rip(vcpu);
1857 vcpu->regs[reg] = vcpu->cr3;
1858 vcpu_put_rsp_rip(vcpu);
1859 skip_emulated_instruction(vcpu);
1860 return 1;
1861 case 8:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001862 vcpu_load_rsp_rip(vcpu);
1863 vcpu->regs[reg] = vcpu->cr8;
1864 vcpu_put_rsp_rip(vcpu);
1865 skip_emulated_instruction(vcpu);
1866 return 1;
1867 }
1868 break;
1869 case 3: /* lmsw */
1870 lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
1871
1872 skip_emulated_instruction(vcpu);
1873 return 1;
1874 default:
1875 break;
1876 }
1877 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10001878 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08001879 (int)(exit_qualification >> 4) & 3, cr);
1880 return 0;
1881}
1882
1883static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1884{
1885 u64 exit_qualification;
1886 unsigned long val;
1887 int dr, reg;
1888
1889 /*
1890 * FIXME: this code assumes the host is debugging the guest.
1891 * need to deal with guest debugging itself too.
1892 */
1893 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
1894 dr = exit_qualification & 7;
1895 reg = (exit_qualification >> 8) & 15;
1896 vcpu_load_rsp_rip(vcpu);
1897 if (exit_qualification & 16) {
1898 /* mov from dr */
1899 switch (dr) {
1900 case 6:
1901 val = 0xffff0ff0;
1902 break;
1903 case 7:
1904 val = 0x400;
1905 break;
1906 default:
1907 val = 0;
1908 }
1909 vcpu->regs[reg] = val;
1910 } else {
1911 /* mov to dr */
1912 }
1913 vcpu_put_rsp_rip(vcpu);
1914 skip_emulated_instruction(vcpu);
1915 return 1;
1916}
1917
1918static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1919{
Avi Kivity06465c52007-02-28 20:46:53 +02001920 kvm_emulate_cpuid(vcpu);
1921 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001922}
1923
1924static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1925{
1926 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
1927 u64 data;
1928
1929 if (vmx_get_msr(vcpu, ecx, &data)) {
1930 vmx_inject_gp(vcpu, 0);
1931 return 1;
1932 }
1933
1934 /* FIXME: handling of bits 32:63 of rax, rdx */
1935 vcpu->regs[VCPU_REGS_RAX] = data & -1u;
1936 vcpu->regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
1937 skip_emulated_instruction(vcpu);
1938 return 1;
1939}
1940
1941static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1942{
1943 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
1944 u64 data = (vcpu->regs[VCPU_REGS_RAX] & -1u)
1945 | ((u64)(vcpu->regs[VCPU_REGS_RDX] & -1u) << 32);
1946
1947 if (vmx_set_msr(vcpu, ecx, data) != 0) {
1948 vmx_inject_gp(vcpu, 0);
1949 return 1;
1950 }
1951
1952 skip_emulated_instruction(vcpu);
1953 return 1;
1954}
1955
Dor Laorc1150d82007-01-05 16:36:24 -08001956static void post_kvm_run_save(struct kvm_vcpu *vcpu,
1957 struct kvm_run *kvm_run)
1958{
1959 kvm_run->if_flag = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) != 0;
1960 kvm_run->cr8 = vcpu->cr8;
1961 kvm_run->apic_base = vcpu->apic_base;
1962 kvm_run->ready_for_interrupt_injection = (vcpu->interrupt_window_open &&
1963 vcpu->irq_summary == 0);
1964}
1965
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966static int handle_interrupt_window(struct kvm_vcpu *vcpu,
1967 struct kvm_run *kvm_run)
1968{
Eddie Dong85f455f2007-07-06 12:20:49 +03001969 u32 cpu_based_vm_exec_control;
1970
1971 /* clear pending irq */
1972 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
1973 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
1974 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Dor Laorc1150d82007-01-05 16:36:24 -08001975 /*
1976 * If the user space waits to inject interrupts, exit as soon as
1977 * possible
1978 */
1979 if (kvm_run->request_interrupt_window &&
Dor Laor022a9302007-01-05 16:37:00 -08001980 !vcpu->irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08001981 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001982 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08001983 return 0;
1984 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001985 return 1;
1986}
1987
1988static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1989{
1990 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03001991 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001992}
1993
Ingo Molnarc21415e2007-02-19 14:37:47 +02001994static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1995{
Dor Laor510043d2007-02-19 18:25:43 +02001996 skip_emulated_instruction(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001997 return kvm_hypercall(vcpu, kvm_run);
Ingo Molnarc21415e2007-02-19 14:37:47 +02001998}
1999
Avi Kivity6aa8b732006-12-10 02:21:36 -08002000/*
2001 * The exit handlers return 1 if the exit was handled fully and guest execution
2002 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2003 * to be done to userspace and return 0.
2004 */
2005static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
2006 struct kvm_run *kvm_run) = {
2007 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
2008 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08002009 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002010 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002011 [EXIT_REASON_CR_ACCESS] = handle_cr,
2012 [EXIT_REASON_DR_ACCESS] = handle_dr,
2013 [EXIT_REASON_CPUID] = handle_cpuid,
2014 [EXIT_REASON_MSR_READ] = handle_rdmsr,
2015 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
2016 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
2017 [EXIT_REASON_HLT] = handle_halt,
Ingo Molnarc21415e2007-02-19 14:37:47 +02002018 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002019};
2020
2021static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04002022 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002023
2024/*
2025 * The guest has exited. See if we can fix it or if we need userspace
2026 * assistance.
2027 */
2028static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2029{
2030 u32 vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2031 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
2032
2033 if ( (vectoring_info & VECTORING_INFO_VALID_MASK) &&
2034 exit_reason != EXIT_REASON_EXCEPTION_NMI )
2035 printk(KERN_WARNING "%s: unexpected, valid vectoring info and "
2036 "exit reason is 0x%x\n", __FUNCTION__, exit_reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002037 if (exit_reason < kvm_vmx_max_exit_handlers
2038 && kvm_vmx_exit_handlers[exit_reason])
2039 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
2040 else {
2041 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2042 kvm_run->hw.hardware_exit_reason = exit_reason;
2043 }
2044 return 0;
2045}
2046
Dor Laorc1150d82007-01-05 16:36:24 -08002047/*
2048 * Check if userspace requested an interrupt window, and that the
2049 * interrupt window is open.
2050 *
2051 * No need to exit to userspace if we already have an interrupt queued.
2052 */
2053static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2054 struct kvm_run *kvm_run)
2055{
2056 return (!vcpu->irq_summary &&
2057 kvm_run->request_interrupt_window &&
2058 vcpu->interrupt_window_open &&
2059 (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF));
2060}
2061
Avi Kivityd9e368d2007-06-07 19:18:30 +03002062static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
2063{
Avi Kivityd9e368d2007-06-07 19:18:30 +03002064}
2065
Eddie Dong85f455f2007-07-06 12:20:49 +03002066static void enable_irq_window(struct kvm_vcpu *vcpu)
2067{
2068 u32 cpu_based_vm_exec_control;
2069
2070 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2071 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2072 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2073}
2074
2075static void vmx_intr_assist(struct kvm_vcpu *vcpu)
2076{
2077 u32 idtv_info_field, intr_info_field;
2078 int has_ext_irq, interrupt_window_open;
2079
2080 has_ext_irq = kvm_cpu_has_interrupt(vcpu);
2081 intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
2082 idtv_info_field = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2083 if (intr_info_field & INTR_INFO_VALID_MASK) {
2084 if (idtv_info_field & INTR_INFO_VALID_MASK) {
2085 /* TODO: fault when IDT_Vectoring */
2086 printk(KERN_ERR "Fault when IDT_Vectoring\n");
2087 }
2088 if (has_ext_irq)
2089 enable_irq_window(vcpu);
2090 return;
2091 }
2092 if (unlikely(idtv_info_field & INTR_INFO_VALID_MASK)) {
2093 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, idtv_info_field);
2094 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2095 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
2096
2097 if (unlikely(idtv_info_field & INTR_INFO_DELIEVER_CODE_MASK))
2098 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2099 vmcs_read32(IDT_VECTORING_ERROR_CODE));
2100 if (unlikely(has_ext_irq))
2101 enable_irq_window(vcpu);
2102 return;
2103 }
2104 if (!has_ext_irq)
2105 return;
2106 interrupt_window_open =
2107 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2108 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
2109 if (interrupt_window_open)
2110 vmx_inject_irq(vcpu, kvm_cpu_get_interrupt(vcpu));
2111 else
2112 enable_irq_window(vcpu);
2113}
2114
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115static int vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2116{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002117 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002118 u8 fail;
Avi Kivitye2dec932007-01-05 16:36:54 -08002119 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120
Avi Kivitye6adf282007-04-30 16:07:54 +03002121preempted:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122 if (vcpu->guest_debug.enabled)
2123 kvm_guest_debug_pre(vcpu);
2124
Avi Kivitye6adf282007-04-30 16:07:54 +03002125again:
Shaohua Li9ae04482007-07-23 14:51:32 +08002126 r = kvm_mmu_reload(vcpu);
2127 if (unlikely(r))
2128 goto out;
2129
Avi Kivity15ad7142007-07-11 18:17:21 +03002130 preempt_disable();
2131
Rusty Russell8b9cf982007-07-30 16:31:43 +10002132 vmx_save_host_state(vmx);
Avi Kivitye6adf282007-04-30 16:07:54 +03002133 kvm_load_guest_fpu(vcpu);
2134
2135 /*
2136 * Loading guest fpu may have cleared host cr0.ts
2137 */
2138 vmcs_writel(HOST_CR0, read_cr0());
2139
Avi Kivityd9e368d2007-06-07 19:18:30 +03002140 local_irq_disable();
2141
Avi Kivity7e66f352007-08-15 15:23:34 +03002142 if (signal_pending(current)) {
2143 local_irq_enable();
2144 preempt_enable();
2145 r = -EINTR;
2146 kvm_run->exit_reason = KVM_EXIT_INTR;
2147 ++vcpu->stat.signal_exits;
2148 goto out;
2149 }
2150
Eddie Dong85f455f2007-07-06 12:20:49 +03002151 if (irqchip_in_kernel(vcpu->kvm))
2152 vmx_intr_assist(vcpu);
2153 else if (!vcpu->mmio_read_completed)
Avi Kivity7e66f352007-08-15 15:23:34 +03002154 do_interrupt_requests(vcpu, kvm_run);
2155
Avi Kivityd9e368d2007-06-07 19:18:30 +03002156 vcpu->guest_mode = 1;
2157 if (vcpu->requests)
2158 if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
2159 vmx_flush_tlb(vcpu);
2160
Avi Kivity6aa8b732006-12-10 02:21:36 -08002161 asm (
2162 /* Store host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002163#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002164 "push %%rax; push %%rbx; push %%rdx;"
2165 "push %%rsi; push %%rdi; push %%rbp;"
2166 "push %%r8; push %%r9; push %%r10; push %%r11;"
2167 "push %%r12; push %%r13; push %%r14; push %%r15;"
2168 "push %%rcx \n\t"
2169 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
2170#else
2171 "pusha; push %%ecx \n\t"
2172 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
2173#endif
2174 /* Check if vmlaunch of vmresume is needed */
2175 "cmp $0, %1 \n\t"
2176 /* Load guest registers. Don't clobber flags. */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002177#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002178 "mov %c[cr2](%3), %%rax \n\t"
2179 "mov %%rax, %%cr2 \n\t"
2180 "mov %c[rax](%3), %%rax \n\t"
2181 "mov %c[rbx](%3), %%rbx \n\t"
2182 "mov %c[rdx](%3), %%rdx \n\t"
2183 "mov %c[rsi](%3), %%rsi \n\t"
2184 "mov %c[rdi](%3), %%rdi \n\t"
2185 "mov %c[rbp](%3), %%rbp \n\t"
2186 "mov %c[r8](%3), %%r8 \n\t"
2187 "mov %c[r9](%3), %%r9 \n\t"
2188 "mov %c[r10](%3), %%r10 \n\t"
2189 "mov %c[r11](%3), %%r11 \n\t"
2190 "mov %c[r12](%3), %%r12 \n\t"
2191 "mov %c[r13](%3), %%r13 \n\t"
2192 "mov %c[r14](%3), %%r14 \n\t"
2193 "mov %c[r15](%3), %%r15 \n\t"
2194 "mov %c[rcx](%3), %%rcx \n\t" /* kills %3 (rcx) */
2195#else
2196 "mov %c[cr2](%3), %%eax \n\t"
2197 "mov %%eax, %%cr2 \n\t"
2198 "mov %c[rax](%3), %%eax \n\t"
2199 "mov %c[rbx](%3), %%ebx \n\t"
2200 "mov %c[rdx](%3), %%edx \n\t"
2201 "mov %c[rsi](%3), %%esi \n\t"
2202 "mov %c[rdi](%3), %%edi \n\t"
2203 "mov %c[rbp](%3), %%ebp \n\t"
2204 "mov %c[rcx](%3), %%ecx \n\t" /* kills %3 (ecx) */
2205#endif
2206 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03002207 "jne .Llaunched \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002208 ASM_VMX_VMLAUNCH "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03002209 "jmp .Lkvm_vmx_return \n\t"
2210 ".Llaunched: " ASM_VMX_VMRESUME "\n\t"
2211 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08002212 /* Save guest registers, load host registers, keep flags */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002213#ifdef CONFIG_X86_64
Ingo Molnar96958232007-02-12 00:54:33 -08002214 "xchg %3, (%%rsp) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002215 "mov %%rax, %c[rax](%3) \n\t"
2216 "mov %%rbx, %c[rbx](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002217 "pushq (%%rsp); popq %c[rcx](%3) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002218 "mov %%rdx, %c[rdx](%3) \n\t"
2219 "mov %%rsi, %c[rsi](%3) \n\t"
2220 "mov %%rdi, %c[rdi](%3) \n\t"
2221 "mov %%rbp, %c[rbp](%3) \n\t"
2222 "mov %%r8, %c[r8](%3) \n\t"
2223 "mov %%r9, %c[r9](%3) \n\t"
2224 "mov %%r10, %c[r10](%3) \n\t"
2225 "mov %%r11, %c[r11](%3) \n\t"
2226 "mov %%r12, %c[r12](%3) \n\t"
2227 "mov %%r13, %c[r13](%3) \n\t"
2228 "mov %%r14, %c[r14](%3) \n\t"
2229 "mov %%r15, %c[r15](%3) \n\t"
2230 "mov %%cr2, %%rax \n\t"
2231 "mov %%rax, %c[cr2](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002232 "mov (%%rsp), %3 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002233
2234 "pop %%rcx; pop %%r15; pop %%r14; pop %%r13; pop %%r12;"
2235 "pop %%r11; pop %%r10; pop %%r9; pop %%r8;"
2236 "pop %%rbp; pop %%rdi; pop %%rsi;"
2237 "pop %%rdx; pop %%rbx; pop %%rax \n\t"
2238#else
Ingo Molnar96958232007-02-12 00:54:33 -08002239 "xchg %3, (%%esp) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240 "mov %%eax, %c[rax](%3) \n\t"
2241 "mov %%ebx, %c[rbx](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002242 "pushl (%%esp); popl %c[rcx](%3) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243 "mov %%edx, %c[rdx](%3) \n\t"
2244 "mov %%esi, %c[rsi](%3) \n\t"
2245 "mov %%edi, %c[rdi](%3) \n\t"
2246 "mov %%ebp, %c[rbp](%3) \n\t"
2247 "mov %%cr2, %%eax \n\t"
2248 "mov %%eax, %c[cr2](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002249 "mov (%%esp), %3 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250
2251 "pop %%ecx; popa \n\t"
2252#endif
2253 "setbe %0 \n\t"
Herbert Xue0015482007-01-23 14:10:00 +11002254 : "=q" (fail)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002255 : "r"(vmx->launched), "d"((unsigned long)HOST_RSP),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256 "c"(vcpu),
2257 [rax]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RAX])),
2258 [rbx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBX])),
2259 [rcx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RCX])),
2260 [rdx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDX])),
2261 [rsi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RSI])),
2262 [rdi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDI])),
2263 [rbp]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002264#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265 [r8 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R8 ])),
2266 [r9 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R9 ])),
2267 [r10]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R10])),
2268 [r11]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R11])),
2269 [r12]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R12])),
2270 [r13]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R13])),
2271 [r14]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R14])),
2272 [r15]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R15])),
2273#endif
2274 [cr2]"i"(offsetof(struct kvm_vcpu, cr2))
2275 : "cc", "memory" );
2276
Avi Kivityd9e368d2007-06-07 19:18:30 +03002277 vcpu->guest_mode = 0;
2278 local_irq_enable();
2279
Avi Kivity1165f5f2007-04-19 17:27:43 +03002280 ++vcpu->stat.exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002281
Dor Laorc1150d82007-01-05 16:36:24 -08002282 vcpu->interrupt_window_open = (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002283
Avi Kivity6aa8b732006-12-10 02:21:36 -08002284 asm ("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03002285 vmx->launched = 1;
2286
2287 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002288
Avi Kivity05e0c8c2007-04-30 16:15:58 +03002289 if (unlikely(fail)) {
Avi Kivity8eb7d332007-03-04 14:17:08 +02002290 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2291 kvm_run->fail_entry.hardware_entry_failure_reason
2292 = vmcs_read32(VM_INSTRUCTION_ERROR);
Avi Kivitye2dec932007-01-05 16:36:54 -08002293 r = 0;
Avi Kivity05e0c8c2007-04-30 16:15:58 +03002294 goto out;
2295 }
2296 /*
2297 * Profile KVM exit RIPs:
2298 */
2299 if (unlikely(prof_on == KVM_PROFILING))
2300 profile_hit(KVM_PROFILING, (void *)vmcs_readl(GUEST_RIP));
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +01002301
Avi Kivity05e0c8c2007-04-30 16:15:58 +03002302 r = kvm_handle_exit(kvm_run, vcpu);
2303 if (r > 0) {
Avi Kivity05e0c8c2007-04-30 16:15:58 +03002304 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2305 r = -EINTR;
2306 kvm_run->exit_reason = KVM_EXIT_INTR;
2307 ++vcpu->stat.request_irq_exits;
2308 goto out;
2309 }
2310 if (!need_resched()) {
2311 ++vcpu->stat.light_exits;
2312 goto again;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002313 }
2314 }
Dor Laorc1150d82007-01-05 16:36:24 -08002315
Avi Kivitye6adf282007-04-30 16:07:54 +03002316out:
Avi Kivitye6adf282007-04-30 16:07:54 +03002317 if (r > 0) {
2318 kvm_resched(vcpu);
2319 goto preempted;
2320 }
2321
Dor Laorc1150d82007-01-05 16:36:24 -08002322 post_kvm_run_save(vcpu, kvm_run);
Avi Kivitye2dec932007-01-05 16:36:54 -08002323 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324}
2325
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326static void vmx_inject_page_fault(struct kvm_vcpu *vcpu,
2327 unsigned long addr,
2328 u32 err_code)
2329{
2330 u32 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2331
Avi Kivity1165f5f2007-04-19 17:27:43 +03002332 ++vcpu->stat.pf_guest;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333
2334 if (is_page_fault(vect_info)) {
2335 printk(KERN_DEBUG "inject_page_fault: "
2336 "double fault 0x%lx @ 0x%lx\n",
2337 addr, vmcs_readl(GUEST_RIP));
2338 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
2339 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2340 DF_VECTOR |
2341 INTR_TYPE_EXCEPTION |
2342 INTR_INFO_DELIEVER_CODE_MASK |
2343 INTR_INFO_VALID_MASK);
2344 return;
2345 }
2346 vcpu->cr2 = addr;
2347 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, err_code);
2348 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2349 PF_VECTOR |
2350 INTR_TYPE_EXCEPTION |
2351 INTR_INFO_DELIEVER_CODE_MASK |
2352 INTR_INFO_VALID_MASK);
2353
2354}
2355
2356static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
2357{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002358 struct vcpu_vmx *vmx = to_vmx(vcpu);
2359
2360 if (vmx->vmcs) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002361 on_each_cpu(__vcpu_clear, vmx, 0, 1);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002362 free_vmcs(vmx->vmcs);
2363 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364 }
2365}
2366
2367static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
2368{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002369 struct vcpu_vmx *vmx = to_vmx(vcpu);
2370
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002372 kfree(vmx->host_msrs);
2373 kfree(vmx->guest_msrs);
2374 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10002375 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376}
2377
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002378static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002380 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10002381 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03002382 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002384 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002385 return ERR_PTR(-ENOMEM);
2386
2387 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
2388 if (err)
2389 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002390
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002391 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002392 if (!vmx->guest_msrs) {
2393 err = -ENOMEM;
2394 goto uninit_vcpu;
2395 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08002396
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002397 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
2398 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002399 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002400
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002401 vmx->vmcs = alloc_vmcs();
2402 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002403 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002404
2405 vmcs_clear(vmx->vmcs);
2406
Avi Kivity15ad7142007-07-11 18:17:21 +03002407 cpu = get_cpu();
2408 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002409 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002410 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002411 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002412 if (err)
2413 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002414
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002415 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002416
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002417free_vmcs:
2418 free_vmcs(vmx->vmcs);
2419free_msrs:
2420 kfree(vmx->host_msrs);
2421free_guest_msrs:
2422 kfree(vmx->guest_msrs);
2423uninit_vcpu:
2424 kvm_vcpu_uninit(&vmx->vcpu);
2425free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10002426 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002427 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428}
2429
Yang, Sheng002c7f72007-07-31 14:23:01 +03002430static void __init vmx_check_processor_compat(void *rtn)
2431{
2432 struct vmcs_config vmcs_conf;
2433
2434 *(int *)rtn = 0;
2435 if (setup_vmcs_config(&vmcs_conf) < 0)
2436 *(int *)rtn = -EIO;
2437 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
2438 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
2439 smp_processor_id());
2440 *(int *)rtn = -EIO;
2441 }
2442}
2443
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444static struct kvm_arch_ops vmx_arch_ops = {
2445 .cpu_has_kvm_support = cpu_has_kvm_support,
2446 .disabled_by_bios = vmx_disabled_by_bios,
2447 .hardware_setup = hardware_setup,
2448 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03002449 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002450 .hardware_enable = hardware_enable,
2451 .hardware_disable = hardware_disable,
2452
2453 .vcpu_create = vmx_create_vcpu,
2454 .vcpu_free = vmx_free_vcpu,
2455
2456 .vcpu_load = vmx_vcpu_load,
2457 .vcpu_put = vmx_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08002458 .vcpu_decache = vmx_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002459
2460 .set_guest_debug = set_guest_debug,
2461 .get_msr = vmx_get_msr,
2462 .set_msr = vmx_set_msr,
2463 .get_segment_base = vmx_get_segment_base,
2464 .get_segment = vmx_get_segment,
2465 .set_segment = vmx_set_segment,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03002467 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469 .set_cr3 = vmx_set_cr3,
2470 .set_cr4 = vmx_set_cr4,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002471#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002472 .set_efer = vmx_set_efer,
2473#endif
2474 .get_idt = vmx_get_idt,
2475 .set_idt = vmx_set_idt,
2476 .get_gdt = vmx_get_gdt,
2477 .set_gdt = vmx_set_gdt,
2478 .cache_regs = vcpu_load_rsp_rip,
2479 .decache_regs = vcpu_put_rsp_rip,
2480 .get_rflags = vmx_get_rflags,
2481 .set_rflags = vmx_set_rflags,
2482
2483 .tlb_flush = vmx_flush_tlb,
2484 .inject_page_fault = vmx_inject_page_fault,
2485
2486 .inject_gp = vmx_inject_gp,
2487
2488 .run = vmx_vcpu_run,
2489 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02002490 .patch_hypercall = vmx_patch_hypercall,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002491};
2492
2493static int __init vmx_init(void)
2494{
He, Qingfdef3ad2007-04-30 09:45:24 +03002495 void *iova;
2496 int r;
2497
2498 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2499 if (!vmx_io_bitmap_a)
2500 return -ENOMEM;
2501
2502 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2503 if (!vmx_io_bitmap_b) {
2504 r = -ENOMEM;
2505 goto out;
2506 }
2507
2508 /*
2509 * Allow direct access to the PC debug port (it is often used for I/O
2510 * delays, but the vmexits simply slow things down).
2511 */
2512 iova = kmap(vmx_io_bitmap_a);
2513 memset(iova, 0xff, PAGE_SIZE);
2514 clear_bit(0x80, iova);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002515 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03002516
2517 iova = kmap(vmx_io_bitmap_b);
2518 memset(iova, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002519 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03002520
Rusty Russellc16f8622007-07-30 21:12:19 +10002521 r = kvm_init_arch(&vmx_arch_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03002522 if (r)
2523 goto out1;
2524
2525 return 0;
2526
2527out1:
2528 __free_page(vmx_io_bitmap_b);
2529out:
2530 __free_page(vmx_io_bitmap_a);
2531 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532}
2533
2534static void __exit vmx_exit(void)
2535{
He, Qingfdef3ad2007-04-30 09:45:24 +03002536 __free_page(vmx_io_bitmap_b);
2537 __free_page(vmx_io_bitmap_a);
2538
Avi Kivity6aa8b732006-12-10 02:21:36 -08002539 kvm_exit_arch();
2540}
2541
2542module_init(vmx_init)
2543module_exit(vmx_exit)