blob: 6c371ea210469b9e3f505973eb1b5b65256a3f25 [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, Sheng1c3d14f2007-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, Sheng1c3d14f2007-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 Russell707d92f2007-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 Russell707d92f2007-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
Eddie Dong2a8067f2007-08-06 16:29:07 +0300793static int vmx_get_irq(struct kvm_vcpu *vcpu)
794{
795 u32 idtv_info_field;
796
797 idtv_info_field = vmcs_read32(IDT_VECTORING_INFO_FIELD);
798 if (idtv_info_field & INTR_INFO_VALID_MASK) {
799 if (is_external_interrupt(idtv_info_field))
800 return idtv_info_field & VECTORING_INFO_VECTOR_MASK;
801 else
802 printk("pending exception: not handled yet\n");
803 }
804 return -1;
805}
806
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807static __init int cpu_has_kvm_support(void)
808{
809 unsigned long ecx = cpuid_ecx(1);
810 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
811}
812
813static __init int vmx_disabled_by_bios(void)
814{
815 u64 msr;
816
817 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300818 return (msr & (MSR_IA32_FEATURE_CONTROL_LOCKED |
819 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
820 == MSR_IA32_FEATURE_CONTROL_LOCKED;
821 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822}
823
Avi Kivity774c47f2007-02-12 00:54:47 -0800824static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825{
826 int cpu = raw_smp_processor_id();
827 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
828 u64 old;
829
830 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300831 if ((old & (MSR_IA32_FEATURE_CONTROL_LOCKED |
832 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
833 != (MSR_IA32_FEATURE_CONTROL_LOCKED |
834 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300836 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
837 MSR_IA32_FEATURE_CONTROL_LOCKED |
838 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +1000839 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800840 asm volatile (ASM_VMX_VMXON_RAX : : "a"(&phys_addr), "m"(phys_addr)
841 : "memory", "cc");
842}
843
844static void hardware_disable(void *garbage)
845{
846 asm volatile (ASM_VMX_VMXOFF : : : "cc");
847}
848
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300849static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
850 u32 msr, u32* result)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800851{
852 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300853 u32 ctl = ctl_min | ctl_opt;
854
855 rdmsr(msr, vmx_msr_low, vmx_msr_high);
856
857 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
858 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
859
860 /* Ensure minimum (required) set of control bits are supported. */
861 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300862 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300863
864 *result = ctl;
865 return 0;
866}
867
Yang, Sheng002c7f72007-07-31 14:23:01 +0300868static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300869{
870 u32 vmx_msr_low, vmx_msr_high;
871 u32 min, opt;
872 u32 _pin_based_exec_control = 0;
873 u32 _cpu_based_exec_control = 0;
874 u32 _vmexit_control = 0;
875 u32 _vmentry_control = 0;
876
877 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
878 opt = 0;
879 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
880 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300881 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300882
883 min = CPU_BASED_HLT_EXITING |
884#ifdef CONFIG_X86_64
885 CPU_BASED_CR8_LOAD_EXITING |
886 CPU_BASED_CR8_STORE_EXITING |
887#endif
888 CPU_BASED_USE_IO_BITMAPS |
889 CPU_BASED_MOV_DR_EXITING |
890 CPU_BASED_USE_TSC_OFFSETING;
891 opt = 0;
892 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
893 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300894 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300895
896 min = 0;
897#ifdef CONFIG_X86_64
898 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
899#endif
900 opt = 0;
901 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
902 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300903 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300904
905 min = opt = 0;
906 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
907 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300908 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -0800910 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300911
912 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
913 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300914 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300915
916#ifdef CONFIG_X86_64
917 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
918 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +0300919 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300920#endif
921
922 /* Require Write-Back (WB) memory type for VMCS accesses. */
923 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300924 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300925
Yang, Sheng002c7f72007-07-31 14:23:01 +0300926 vmcs_conf->size = vmx_msr_high & 0x1fff;
927 vmcs_conf->order = get_order(vmcs_config.size);
928 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300929
Yang, Sheng002c7f72007-07-31 14:23:01 +0300930 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
931 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
932 vmcs_conf->vmexit_ctrl = _vmexit_control;
933 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300934
935 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -0800936}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937
938static struct vmcs *alloc_vmcs_cpu(int cpu)
939{
940 int node = cpu_to_node(cpu);
941 struct page *pages;
942 struct vmcs *vmcs;
943
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300944 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 if (!pages)
946 return NULL;
947 vmcs = page_address(pages);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300948 memset(vmcs, 0, vmcs_config.size);
949 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950 return vmcs;
951}
952
953static struct vmcs *alloc_vmcs(void)
954{
Ingo Molnard3b2c332007-01-05 16:36:23 -0800955 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956}
957
958static void free_vmcs(struct vmcs *vmcs)
959{
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300960 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961}
962
Sam Ravnborg39959582007-06-01 00:47:13 -0700963static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964{
965 int cpu;
966
967 for_each_online_cpu(cpu)
968 free_vmcs(per_cpu(vmxarea, cpu));
969}
970
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971static __init int alloc_kvm_area(void)
972{
973 int cpu;
974
975 for_each_online_cpu(cpu) {
976 struct vmcs *vmcs;
977
978 vmcs = alloc_vmcs_cpu(cpu);
979 if (!vmcs) {
980 free_kvm_area();
981 return -ENOMEM;
982 }
983
984 per_cpu(vmxarea, cpu) = vmcs;
985 }
986 return 0;
987}
988
989static __init int hardware_setup(void)
990{
Yang, Sheng002c7f72007-07-31 14:23:01 +0300991 if (setup_vmcs_config(&vmcs_config) < 0)
992 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 return alloc_kvm_area();
994}
995
996static __exit void hardware_unsetup(void)
997{
998 free_kvm_area();
999}
1000
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1002{
1003 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1004
Avi Kivity6af11b92007-03-19 13:18:10 +02001005 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006 vmcs_write16(sf->selector, save->selector);
1007 vmcs_writel(sf->base, save->base);
1008 vmcs_write32(sf->limit, save->limit);
1009 vmcs_write32(sf->ar_bytes, save->ar);
1010 } else {
1011 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1012 << AR_DPL_SHIFT;
1013 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1014 }
1015}
1016
1017static void enter_pmode(struct kvm_vcpu *vcpu)
1018{
1019 unsigned long flags;
1020
1021 vcpu->rmode.active = 0;
1022
1023 vmcs_writel(GUEST_TR_BASE, vcpu->rmode.tr.base);
1024 vmcs_write32(GUEST_TR_LIMIT, vcpu->rmode.tr.limit);
1025 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->rmode.tr.ar);
1026
1027 flags = vmcs_readl(GUEST_RFLAGS);
1028 flags &= ~(IOPL_MASK | X86_EFLAGS_VM);
1029 flags |= (vcpu->rmode.save_iopl << IOPL_SHIFT);
1030 vmcs_writel(GUEST_RFLAGS, flags);
1031
Rusty Russell66aee912007-07-17 23:34:16 +10001032 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1033 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034
1035 update_exception_bitmap(vcpu);
1036
1037 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->rmode.es);
1038 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->rmode.ds);
1039 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->rmode.gs);
1040 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->rmode.fs);
1041
1042 vmcs_write16(GUEST_SS_SELECTOR, 0);
1043 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1044
1045 vmcs_write16(GUEST_CS_SELECTOR,
1046 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1047 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1048}
1049
Izik Eidus33f5fa12007-08-19 22:24:58 +03001050static gva_t rmode_tss_base(struct kvm* kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051{
1052 gfn_t base_gfn = kvm->memslots[0].base_gfn + kvm->memslots[0].npages - 3;
1053 return base_gfn << PAGE_SHIFT;
1054}
1055
1056static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1057{
1058 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1059
1060 save->selector = vmcs_read16(sf->selector);
1061 save->base = vmcs_readl(sf->base);
1062 save->limit = vmcs_read32(sf->limit);
1063 save->ar = vmcs_read32(sf->ar_bytes);
1064 vmcs_write16(sf->selector, vmcs_readl(sf->base) >> 4);
1065 vmcs_write32(sf->limit, 0xffff);
1066 vmcs_write32(sf->ar_bytes, 0xf3);
1067}
1068
1069static void enter_rmode(struct kvm_vcpu *vcpu)
1070{
1071 unsigned long flags;
1072
1073 vcpu->rmode.active = 1;
1074
1075 vcpu->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
1076 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1077
1078 vcpu->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
1079 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1080
1081 vcpu->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
1082 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1083
1084 flags = vmcs_readl(GUEST_RFLAGS);
1085 vcpu->rmode.save_iopl = (flags & IOPL_MASK) >> IOPL_SHIFT;
1086
1087 flags |= IOPL_MASK | X86_EFLAGS_VM;
1088
1089 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001090 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091 update_exception_bitmap(vcpu);
1092
1093 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1094 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1095 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1096
1097 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001098 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001099 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1100 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1102
1103 fix_rmode_seg(VCPU_SREG_ES, &vcpu->rmode.es);
1104 fix_rmode_seg(VCPU_SREG_DS, &vcpu->rmode.ds);
1105 fix_rmode_seg(VCPU_SREG_GS, &vcpu->rmode.gs);
1106 fix_rmode_seg(VCPU_SREG_FS, &vcpu->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001107
1108 init_rmode_tss(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109}
1110
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001111#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112
1113static void enter_lmode(struct kvm_vcpu *vcpu)
1114{
1115 u32 guest_tr_ar;
1116
1117 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1118 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1119 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
1120 __FUNCTION__);
1121 vmcs_write32(GUEST_TR_AR_BYTES,
1122 (guest_tr_ar & ~AR_TYPE_MASK)
1123 | AR_TYPE_BUSY_64_TSS);
1124 }
1125
1126 vcpu->shadow_efer |= EFER_LMA;
1127
Rusty Russell8b9cf982007-07-30 16:31:43 +10001128 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129 vmcs_write32(VM_ENTRY_CONTROLS,
1130 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001131 | VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001132}
1133
1134static void exit_lmode(struct kvm_vcpu *vcpu)
1135{
1136 vcpu->shadow_efer &= ~EFER_LMA;
1137
1138 vmcs_write32(VM_ENTRY_CONTROLS,
1139 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001140 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141}
1142
1143#endif
1144
Anthony Liguori25c4c272007-04-27 09:29:21 +03001145static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001146{
Avi Kivity399badf2007-01-05 16:36:38 -08001147 vcpu->cr4 &= KVM_GUEST_CR4_MASK;
1148 vcpu->cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
1149}
1150
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1152{
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001153 vmx_fpu_deactivate(vcpu);
1154
Rusty Russell707d92f2007-07-17 23:19:08 +10001155 if (vcpu->rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156 enter_pmode(vcpu);
1157
Rusty Russell707d92f2007-07-17 23:19:08 +10001158 if (!vcpu->rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159 enter_rmode(vcpu);
1160
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001161#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001162 if (vcpu->shadow_efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001163 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10001165 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001166 exit_lmode(vcpu);
1167 }
1168#endif
1169
1170 vmcs_writel(CR0_READ_SHADOW, cr0);
1171 vmcs_writel(GUEST_CR0,
1172 (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON);
1173 vcpu->cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001174
Rusty Russell707d92f2007-07-17 23:19:08 +10001175 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001176 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001177}
1178
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1180{
1181 vmcs_writel(GUEST_CR3, cr3);
Rusty Russell707d92f2007-07-17 23:19:08 +10001182 if (vcpu->cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001183 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184}
1185
1186static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1187{
1188 vmcs_writel(CR4_READ_SHADOW, cr4);
1189 vmcs_writel(GUEST_CR4, cr4 | (vcpu->rmode.active ?
1190 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON));
1191 vcpu->cr4 = cr4;
1192}
1193
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001194#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001195
1196static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1197{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001198 struct vcpu_vmx *vmx = to_vmx(vcpu);
1199 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001200
1201 vcpu->shadow_efer = efer;
1202 if (efer & EFER_LMA) {
1203 vmcs_write32(VM_ENTRY_CONTROLS,
1204 vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001205 VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001206 msr->data = efer;
1207
1208 } else {
1209 vmcs_write32(VM_ENTRY_CONTROLS,
1210 vmcs_read32(VM_ENTRY_CONTROLS) &
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001211 ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212
1213 msr->data = efer & ~EFER_LME;
1214 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001215 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216}
1217
1218#endif
1219
1220static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1221{
1222 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1223
1224 return vmcs_readl(sf->base);
1225}
1226
1227static void vmx_get_segment(struct kvm_vcpu *vcpu,
1228 struct kvm_segment *var, int seg)
1229{
1230 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1231 u32 ar;
1232
1233 var->base = vmcs_readl(sf->base);
1234 var->limit = vmcs_read32(sf->limit);
1235 var->selector = vmcs_read16(sf->selector);
1236 ar = vmcs_read32(sf->ar_bytes);
1237 if (ar & AR_UNUSABLE_MASK)
1238 ar = 0;
1239 var->type = ar & 15;
1240 var->s = (ar >> 4) & 1;
1241 var->dpl = (ar >> 5) & 3;
1242 var->present = (ar >> 7) & 1;
1243 var->avl = (ar >> 12) & 1;
1244 var->l = (ar >> 13) & 1;
1245 var->db = (ar >> 14) & 1;
1246 var->g = (ar >> 15) & 1;
1247 var->unusable = (ar >> 16) & 1;
1248}
1249
Avi Kivity653e3102007-05-07 10:55:37 +03001250static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001252 u32 ar;
1253
Avi Kivity653e3102007-05-07 10:55:37 +03001254 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255 ar = 1 << 16;
1256 else {
1257 ar = var->type & 15;
1258 ar |= (var->s & 1) << 4;
1259 ar |= (var->dpl & 3) << 5;
1260 ar |= (var->present & 1) << 7;
1261 ar |= (var->avl & 1) << 12;
1262 ar |= (var->l & 1) << 13;
1263 ar |= (var->db & 1) << 14;
1264 ar |= (var->g & 1) << 15;
1265 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001266 if (ar == 0) /* a 0 value means unusable */
1267 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001268
1269 return ar;
1270}
1271
1272static void vmx_set_segment(struct kvm_vcpu *vcpu,
1273 struct kvm_segment *var, int seg)
1274{
1275 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1276 u32 ar;
1277
1278 if (vcpu->rmode.active && seg == VCPU_SREG_TR) {
1279 vcpu->rmode.tr.selector = var->selector;
1280 vcpu->rmode.tr.base = var->base;
1281 vcpu->rmode.tr.limit = var->limit;
1282 vcpu->rmode.tr.ar = vmx_segment_access_rights(var);
1283 return;
1284 }
1285 vmcs_writel(sf->base, var->base);
1286 vmcs_write32(sf->limit, var->limit);
1287 vmcs_write16(sf->selector, var->selector);
1288 if (vcpu->rmode.active && var->s) {
1289 /*
1290 * Hack real-mode segments into vm86 compatibility.
1291 */
1292 if (var->base == 0xffff0000 && var->selector == 0xf000)
1293 vmcs_writel(sf->base, 0xf0000);
1294 ar = 0xf3;
1295 } else
1296 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297 vmcs_write32(sf->ar_bytes, ar);
1298}
1299
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1301{
1302 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1303
1304 *db = (ar >> 14) & 1;
1305 *l = (ar >> 13) & 1;
1306}
1307
1308static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1309{
1310 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1311 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1312}
1313
1314static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1315{
1316 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1317 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1318}
1319
1320static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1321{
1322 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1323 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1324}
1325
1326static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1327{
1328 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1329 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1330}
1331
1332static int init_rmode_tss(struct kvm* kvm)
1333{
1334 struct page *p1, *p2, *p3;
1335 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
1336 char *page;
1337
Avi Kivity954bbbc22007-03-30 14:02:32 +03001338 p1 = gfn_to_page(kvm, fn++);
1339 p2 = gfn_to_page(kvm, fn++);
1340 p3 = gfn_to_page(kvm, fn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001341
1342 if (!p1 || !p2 || !p3) {
1343 kvm_printf(kvm,"%s: gfn_to_page failed\n", __FUNCTION__);
1344 return 0;
1345 }
1346
1347 page = kmap_atomic(p1, KM_USER0);
Shani Moideena3870c42007-06-11 09:31:33 +05301348 clear_page(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001349 *(u16*)(page + 0x66) = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
1350 kunmap_atomic(page, KM_USER0);
1351
1352 page = kmap_atomic(p2, KM_USER0);
Shani Moideena3870c42007-06-11 09:31:33 +05301353 clear_page(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001354 kunmap_atomic(page, KM_USER0);
1355
1356 page = kmap_atomic(p3, KM_USER0);
Shani Moideena3870c42007-06-11 09:31:33 +05301357 clear_page(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358 *(page + RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1) = ~0;
1359 kunmap_atomic(page, KM_USER0);
1360
1361 return 1;
1362}
1363
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364static void seg_setup(int seg)
1365{
1366 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1367
1368 vmcs_write16(sf->selector, 0);
1369 vmcs_writel(sf->base, 0);
1370 vmcs_write32(sf->limit, 0xffff);
1371 vmcs_write32(sf->ar_bytes, 0x93);
1372}
1373
1374/*
1375 * Sets up the vmcs for emulated real mode.
1376 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001377static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378{
1379 u32 host_sysenter_cs;
1380 u32 junk;
1381 unsigned long a;
1382 struct descriptor_table dt;
1383 int i;
1384 int ret = 0;
Avi Kivitycd2276a2007-05-14 20:41:13 +03001385 unsigned long kvm_vmx_return;
Eddie Dong7017fc32007-07-18 11:34:57 +03001386 u64 msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387
Rusty Russell8b9cf982007-07-30 16:31:43 +10001388 if (!init_rmode_tss(vmx->vcpu.kvm)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389 ret = -ENOMEM;
1390 goto out;
1391 }
1392
Rusty Russell8b9cf982007-07-30 16:31:43 +10001393 vmx->vcpu.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Eddie Dong7017fc32007-07-18 11:34:57 +03001394 set_cr8(&vmx->vcpu, 0);
1395 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Rusty Russell8b9cf982007-07-30 16:31:43 +10001396 if (vmx->vcpu.vcpu_id == 0)
Eddie Dong7017fc32007-07-18 11:34:57 +03001397 msr |= MSR_IA32_APICBASE_BSP;
1398 kvm_set_apic_base(&vmx->vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399
Rusty Russell8b9cf982007-07-30 16:31:43 +10001400 fx_init(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401
1402 /*
1403 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
1404 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
1405 */
1406 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
1407 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
1408 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
1409 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1410
1411 seg_setup(VCPU_SREG_DS);
1412 seg_setup(VCPU_SREG_ES);
1413 seg_setup(VCPU_SREG_FS);
1414 seg_setup(VCPU_SREG_GS);
1415 seg_setup(VCPU_SREG_SS);
1416
1417 vmcs_write16(GUEST_TR_SELECTOR, 0);
1418 vmcs_writel(GUEST_TR_BASE, 0);
1419 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
1420 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1421
1422 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
1423 vmcs_writel(GUEST_LDTR_BASE, 0);
1424 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
1425 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
1426
1427 vmcs_write32(GUEST_SYSENTER_CS, 0);
1428 vmcs_writel(GUEST_SYSENTER_ESP, 0);
1429 vmcs_writel(GUEST_SYSENTER_EIP, 0);
1430
1431 vmcs_writel(GUEST_RFLAGS, 0x02);
1432 vmcs_writel(GUEST_RIP, 0xfff0);
1433 vmcs_writel(GUEST_RSP, 0);
1434
Avi Kivity6aa8b732006-12-10 02:21:36 -08001435 //todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0
1436 vmcs_writel(GUEST_DR7, 0x400);
1437
1438 vmcs_writel(GUEST_GDTR_BASE, 0);
1439 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
1440
1441 vmcs_writel(GUEST_IDTR_BASE, 0);
1442 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
1443
1444 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
1445 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
1446 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
1447
1448 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03001449 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
1450 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001451
1452 guest_write_tsc(0);
1453
1454 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
1455
1456 /* Special registers */
1457 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
1458
1459 /* Control */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001460 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
1461 vmcs_config.pin_based_exec_ctrl);
1462 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
1463 vmcs_config.cpu_based_exec_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001464
Avi Kivity6aa8b732006-12-10 02:21:36 -08001465 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
1466 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
1467 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
1468
1469 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
1470 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
1471 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1472
1473 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
1474 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1475 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1476 vmcs_write16(HOST_FS_SELECTOR, read_fs()); /* 22.2.4 */
1477 vmcs_write16(HOST_GS_SELECTOR, read_gs()); /* 22.2.4 */
1478 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001479#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480 rdmsrl(MSR_FS_BASE, a);
1481 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
1482 rdmsrl(MSR_GS_BASE, a);
1483 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
1484#else
1485 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
1486 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
1487#endif
1488
1489 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
1490
1491 get_idt(&dt);
1492 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
1493
Avi Kivitycd2276a2007-05-14 20:41:13 +03001494 asm ("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
1495 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03001496 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
1497 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
1498 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499
1500 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
1501 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
1502 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
1503 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
1504 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
1505 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
1506
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507 for (i = 0; i < NR_VMX_MSR; ++i) {
1508 u32 index = vmx_msr_index[i];
1509 u32 data_low, data_high;
1510 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001511 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512
1513 if (rdmsr_safe(index, &data_low, &data_high) < 0)
1514 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08001515 if (wrmsr_safe(index, data_low, data_high) < 0)
1516 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001518 vmx->host_msrs[j].index = index;
1519 vmx->host_msrs[j].reserved = 0;
1520 vmx->host_msrs[j].data = data;
1521 vmx->guest_msrs[j] = vmx->host_msrs[j];
1522 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001523 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524
Rusty Russell8b9cf982007-07-30 16:31:43 +10001525 setup_msrs(vmx);
Avi Kivitye38aea32007-04-19 13:22:48 +03001526
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001527 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528
1529 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001530 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
1531
Avi Kivity6aa8b732006-12-10 02:21:36 -08001532 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
1533
Michael Riepe3b99ab22006-12-13 00:34:15 -08001534#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001535 vmcs_writel(VIRTUAL_APIC_PAGE_ADDR, 0);
1536 vmcs_writel(TPR_THRESHOLD, 0);
Michael Riepe3b99ab22006-12-13 00:34:15 -08001537#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001538
Anthony Liguori25c4c272007-04-27 09:29:21 +03001539 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001540 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
1541
Rusty Russell8b9cf982007-07-30 16:31:43 +10001542 vmx->vcpu.cr0 = 0x60000010;
1543 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.cr0); // enter rmode
1544 vmx_set_cr4(&vmx->vcpu, 0);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001545#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001546 vmx_set_efer(&vmx->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001547#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +10001548 vmx_fpu_activate(&vmx->vcpu);
1549 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001550
1551 return 0;
1552
Avi Kivity6aa8b732006-12-10 02:21:36 -08001553out:
1554 return ret;
1555}
1556
1557static void inject_rmode_irq(struct kvm_vcpu *vcpu, int irq)
1558{
1559 u16 ent[2];
1560 u16 cs;
1561 u16 ip;
1562 unsigned long flags;
1563 unsigned long ss_base = vmcs_readl(GUEST_SS_BASE);
1564 u16 sp = vmcs_readl(GUEST_RSP);
1565 u32 ss_limit = vmcs_read32(GUEST_SS_LIMIT);
1566
Eric Sesterhenn / Snakebyte39649942007-04-09 16:15:05 +02001567 if (sp > ss_limit || sp < 6 ) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001568 vcpu_printf(vcpu, "%s: #SS, rsp 0x%lx ss 0x%lx limit 0x%x\n",
1569 __FUNCTION__,
1570 vmcs_readl(GUEST_RSP),
1571 vmcs_readl(GUEST_SS_BASE),
1572 vmcs_read32(GUEST_SS_LIMIT));
1573 return;
1574 }
1575
Laurent Viviere7d5d762007-07-30 13:41:19 +03001576 if (emulator_read_std(irq * sizeof(ent), &ent, sizeof(ent), vcpu) !=
1577 X86EMUL_CONTINUE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001578 vcpu_printf(vcpu, "%s: read guest err\n", __FUNCTION__);
1579 return;
1580 }
1581
1582 flags = vmcs_readl(GUEST_RFLAGS);
1583 cs = vmcs_readl(GUEST_CS_BASE) >> 4;
1584 ip = vmcs_readl(GUEST_RIP);
1585
1586
Laurent Viviere7d5d762007-07-30 13:41:19 +03001587 if (emulator_write_emulated(ss_base + sp - 2, &flags, 2, vcpu) != X86EMUL_CONTINUE ||
1588 emulator_write_emulated(ss_base + sp - 4, &cs, 2, vcpu) != X86EMUL_CONTINUE ||
1589 emulator_write_emulated(ss_base + sp - 6, &ip, 2, vcpu) != X86EMUL_CONTINUE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001590 vcpu_printf(vcpu, "%s: write guest err\n", __FUNCTION__);
1591 return;
1592 }
1593
1594 vmcs_writel(GUEST_RFLAGS, flags &
1595 ~( X86_EFLAGS_IF | X86_EFLAGS_AC | X86_EFLAGS_TF));
1596 vmcs_write16(GUEST_CS_SELECTOR, ent[1]) ;
1597 vmcs_writel(GUEST_CS_BASE, ent[1] << 4);
1598 vmcs_writel(GUEST_RIP, ent[0]);
1599 vmcs_writel(GUEST_RSP, (vmcs_readl(GUEST_RSP) & ~0xffff) | (sp - 6));
1600}
1601
Eddie Dong85f455f2007-07-06 12:20:49 +03001602static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
1603{
1604 if (vcpu->rmode.active) {
1605 inject_rmode_irq(vcpu, irq);
1606 return;
1607 }
1608 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
1609 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1610}
1611
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
1613{
1614 int word_index = __ffs(vcpu->irq_summary);
1615 int bit_index = __ffs(vcpu->irq_pending[word_index]);
1616 int irq = word_index * BITS_PER_LONG + bit_index;
1617
1618 clear_bit(bit_index, &vcpu->irq_pending[word_index]);
1619 if (!vcpu->irq_pending[word_index])
1620 clear_bit(word_index, &vcpu->irq_summary);
Eddie Dong85f455f2007-07-06 12:20:49 +03001621 vmx_inject_irq(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622}
1623
Dor Laorc1150d82007-01-05 16:36:24 -08001624
1625static void do_interrupt_requests(struct kvm_vcpu *vcpu,
1626 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627{
Dor Laorc1150d82007-01-05 16:36:24 -08001628 u32 cpu_based_vm_exec_control;
1629
1630 vcpu->interrupt_window_open =
1631 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
1632 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
1633
1634 if (vcpu->interrupt_window_open &&
1635 vcpu->irq_summary &&
1636 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001637 /*
Dor Laorc1150d82007-01-05 16:36:24 -08001638 * If interrupts enabled, and not blocked by sti or mov ss. Good.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639 */
1640 kvm_do_inject_irq(vcpu);
Dor Laorc1150d82007-01-05 16:36:24 -08001641
1642 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
1643 if (!vcpu->interrupt_window_open &&
1644 (vcpu->irq_summary || kvm_run->request_interrupt_window))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645 /*
1646 * Interrupts blocked. Wait for unblock.
1647 */
Dor Laorc1150d82007-01-05 16:36:24 -08001648 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
1649 else
1650 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
1651 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652}
1653
1654static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
1655{
1656 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
1657
1658 set_debugreg(dbg->bp[0], 0);
1659 set_debugreg(dbg->bp[1], 1);
1660 set_debugreg(dbg->bp[2], 2);
1661 set_debugreg(dbg->bp[3], 3);
1662
1663 if (dbg->singlestep) {
1664 unsigned long flags;
1665
1666 flags = vmcs_readl(GUEST_RFLAGS);
1667 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1668 vmcs_writel(GUEST_RFLAGS, flags);
1669 }
1670}
1671
1672static int handle_rmode_exception(struct kvm_vcpu *vcpu,
1673 int vec, u32 err_code)
1674{
1675 if (!vcpu->rmode.active)
1676 return 0;
1677
Nitin A Kambleb3f37702007-05-17 15:50:34 +03001678 /*
1679 * Instruction with address size override prefix opcode 0x67
1680 * Cause the #SS fault with 0 error code in VM86 mode.
1681 */
1682 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001683 if (emulate_instruction(vcpu, NULL, 0, 0) == EMULATE_DONE)
1684 return 1;
1685 return 0;
1686}
1687
1688static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1689{
1690 u32 intr_info, error_code;
1691 unsigned long cr2, rip;
1692 u32 vect_info;
1693 enum emulation_result er;
Avi Kivitye2dec932007-01-05 16:36:54 -08001694 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001695
1696 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
1697 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
1698
1699 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
1700 !is_page_fault(intr_info)) {
1701 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
1702 "intr info 0x%x\n", __FUNCTION__, vect_info, intr_info);
1703 }
1704
Eddie Dong85f455f2007-07-06 12:20:49 +03001705 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
1707 set_bit(irq, vcpu->irq_pending);
1708 set_bit(irq / BITS_PER_LONG, &vcpu->irq_summary);
1709 }
1710
1711 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) { /* nmi */
1712 asm ("int $2");
1713 return 1;
1714 }
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001715
1716 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001717 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001718 return 1;
1719 }
1720
Avi Kivity6aa8b732006-12-10 02:21:36 -08001721 error_code = 0;
1722 rip = vmcs_readl(GUEST_RIP);
1723 if (intr_info & INTR_INFO_DELIEVER_CODE_MASK)
1724 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
1725 if (is_page_fault(intr_info)) {
1726 cr2 = vmcs_readl(EXIT_QUALIFICATION);
1727
Shaohua Li11ec2802007-07-23 14:51:37 +08001728 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001729 r = kvm_mmu_page_fault(vcpu, cr2, error_code);
1730 if (r < 0) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001731 mutex_unlock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001732 return r;
1733 }
1734 if (!r) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001735 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001736 return 1;
1737 }
1738
1739 er = emulate_instruction(vcpu, kvm_run, cr2, error_code);
Shaohua Li11ec2802007-07-23 14:51:37 +08001740 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001741
1742 switch (er) {
1743 case EMULATE_DONE:
1744 return 1;
1745 case EMULATE_DO_MMIO:
Avi Kivity1165f5f2007-04-19 17:27:43 +03001746 ++vcpu->stat.mmio_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001747 return 0;
1748 case EMULATE_FAIL:
1749 vcpu_printf(vcpu, "%s: emulate fail\n", __FUNCTION__);
1750 break;
1751 default:
1752 BUG();
1753 }
1754 }
1755
1756 if (vcpu->rmode.active &&
1757 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03001758 error_code)) {
1759 if (vcpu->halt_request) {
1760 vcpu->halt_request = 0;
1761 return kvm_emulate_halt(vcpu);
1762 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001763 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03001764 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001765
1766 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) == (INTR_TYPE_EXCEPTION | 1)) {
1767 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1768 return 0;
1769 }
1770 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
1771 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
1772 kvm_run->ex.error_code = error_code;
1773 return 0;
1774}
1775
1776static int handle_external_interrupt(struct kvm_vcpu *vcpu,
1777 struct kvm_run *kvm_run)
1778{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001779 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001780 return 1;
1781}
1782
Avi Kivity988ad742007-02-12 00:54:36 -08001783static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1784{
1785 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1786 return 0;
1787}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001788
Avi Kivity6aa8b732006-12-10 02:21:36 -08001789static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1790{
1791 u64 exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02001792 int size, down, in, string, rep;
1793 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001794
Avi Kivity1165f5f2007-04-19 17:27:43 +03001795 ++vcpu->stat.io_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001796 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02001797 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001798
1799 if (string) {
1800 if (emulate_instruction(vcpu, kvm_run, 0, 0) == EMULATE_DO_MMIO)
1801 return 0;
1802 return 1;
1803 }
1804
1805 size = (exit_qualification & 7) + 1;
1806 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001807 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001808 rep = (exit_qualification & 32) != 0;
1809 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03001810
Laurent Vivier3090dd72007-08-05 10:43:32 +03001811 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001812}
1813
Ingo Molnar102d8322007-02-19 14:37:47 +02001814static void
1815vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
1816{
1817 /*
1818 * Patch in the VMCALL instruction:
1819 */
1820 hypercall[0] = 0x0f;
1821 hypercall[1] = 0x01;
1822 hypercall[2] = 0xc1;
1823 hypercall[3] = 0xc3;
1824}
1825
Avi Kivity6aa8b732006-12-10 02:21:36 -08001826static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1827{
1828 u64 exit_qualification;
1829 int cr;
1830 int reg;
1831
1832 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
1833 cr = exit_qualification & 15;
1834 reg = (exit_qualification >> 8) & 15;
1835 switch ((exit_qualification >> 4) & 3) {
1836 case 0: /* mov to cr */
1837 switch (cr) {
1838 case 0:
1839 vcpu_load_rsp_rip(vcpu);
1840 set_cr0(vcpu, vcpu->regs[reg]);
1841 skip_emulated_instruction(vcpu);
1842 return 1;
1843 case 3:
1844 vcpu_load_rsp_rip(vcpu);
1845 set_cr3(vcpu, vcpu->regs[reg]);
1846 skip_emulated_instruction(vcpu);
1847 return 1;
1848 case 4:
1849 vcpu_load_rsp_rip(vcpu);
1850 set_cr4(vcpu, vcpu->regs[reg]);
1851 skip_emulated_instruction(vcpu);
1852 return 1;
1853 case 8:
1854 vcpu_load_rsp_rip(vcpu);
1855 set_cr8(vcpu, vcpu->regs[reg]);
1856 skip_emulated_instruction(vcpu);
Yang, Sheng253abde2007-08-16 13:01:00 +03001857 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
1858 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001859 };
1860 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03001861 case 2: /* clts */
1862 vcpu_load_rsp_rip(vcpu);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001863 vmx_fpu_deactivate(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10001864 vcpu->cr0 &= ~X86_CR0_TS;
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001865 vmcs_writel(CR0_READ_SHADOW, vcpu->cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001866 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03001867 skip_emulated_instruction(vcpu);
1868 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001869 case 1: /*mov from cr*/
1870 switch (cr) {
1871 case 3:
1872 vcpu_load_rsp_rip(vcpu);
1873 vcpu->regs[reg] = vcpu->cr3;
1874 vcpu_put_rsp_rip(vcpu);
1875 skip_emulated_instruction(vcpu);
1876 return 1;
1877 case 8:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001878 vcpu_load_rsp_rip(vcpu);
Eddie Dong7017fc32007-07-18 11:34:57 +03001879 vcpu->regs[reg] = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001880 vcpu_put_rsp_rip(vcpu);
1881 skip_emulated_instruction(vcpu);
1882 return 1;
1883 }
1884 break;
1885 case 3: /* lmsw */
1886 lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
1887
1888 skip_emulated_instruction(vcpu);
1889 return 1;
1890 default:
1891 break;
1892 }
1893 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10001894 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08001895 (int)(exit_qualification >> 4) & 3, cr);
1896 return 0;
1897}
1898
1899static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1900{
1901 u64 exit_qualification;
1902 unsigned long val;
1903 int dr, reg;
1904
1905 /*
1906 * FIXME: this code assumes the host is debugging the guest.
1907 * need to deal with guest debugging itself too.
1908 */
1909 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
1910 dr = exit_qualification & 7;
1911 reg = (exit_qualification >> 8) & 15;
1912 vcpu_load_rsp_rip(vcpu);
1913 if (exit_qualification & 16) {
1914 /* mov from dr */
1915 switch (dr) {
1916 case 6:
1917 val = 0xffff0ff0;
1918 break;
1919 case 7:
1920 val = 0x400;
1921 break;
1922 default:
1923 val = 0;
1924 }
1925 vcpu->regs[reg] = val;
1926 } else {
1927 /* mov to dr */
1928 }
1929 vcpu_put_rsp_rip(vcpu);
1930 skip_emulated_instruction(vcpu);
1931 return 1;
1932}
1933
1934static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1935{
Avi Kivity06465c52007-02-28 20:46:53 +02001936 kvm_emulate_cpuid(vcpu);
1937 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938}
1939
1940static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1941{
1942 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
1943 u64 data;
1944
1945 if (vmx_get_msr(vcpu, ecx, &data)) {
1946 vmx_inject_gp(vcpu, 0);
1947 return 1;
1948 }
1949
1950 /* FIXME: handling of bits 32:63 of rax, rdx */
1951 vcpu->regs[VCPU_REGS_RAX] = data & -1u;
1952 vcpu->regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
1953 skip_emulated_instruction(vcpu);
1954 return 1;
1955}
1956
1957static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1958{
1959 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
1960 u64 data = (vcpu->regs[VCPU_REGS_RAX] & -1u)
1961 | ((u64)(vcpu->regs[VCPU_REGS_RDX] & -1u) << 32);
1962
1963 if (vmx_set_msr(vcpu, ecx, data) != 0) {
1964 vmx_inject_gp(vcpu, 0);
1965 return 1;
1966 }
1967
1968 skip_emulated_instruction(vcpu);
1969 return 1;
1970}
1971
Dor Laorc1150d82007-01-05 16:36:24 -08001972static void post_kvm_run_save(struct kvm_vcpu *vcpu,
1973 struct kvm_run *kvm_run)
1974{
1975 kvm_run->if_flag = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) != 0;
Eddie Dong7017fc32007-07-18 11:34:57 +03001976 kvm_run->cr8 = get_cr8(vcpu);
1977 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001978 if (irqchip_in_kernel(vcpu->kvm))
1979 kvm_run->ready_for_interrupt_injection = 1;
1980 else
1981 kvm_run->ready_for_interrupt_injection =
1982 (vcpu->interrupt_window_open &&
1983 vcpu->irq_summary == 0);
Dor Laorc1150d82007-01-05 16:36:24 -08001984}
1985
Avi Kivity6aa8b732006-12-10 02:21:36 -08001986static int handle_interrupt_window(struct kvm_vcpu *vcpu,
1987 struct kvm_run *kvm_run)
1988{
Eddie Dong85f455f2007-07-06 12:20:49 +03001989 u32 cpu_based_vm_exec_control;
1990
1991 /* clear pending irq */
1992 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
1993 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
1994 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Dor Laorc1150d82007-01-05 16:36:24 -08001995 /*
1996 * If the user space waits to inject interrupts, exit as soon as
1997 * possible
1998 */
1999 if (kvm_run->request_interrupt_window &&
Dor Laor022a9302007-01-05 16:37:00 -08002000 !vcpu->irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002001 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002002 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002003 return 0;
2004 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002005 return 1;
2006}
2007
2008static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2009{
2010 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002011 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002012}
2013
Ingo Molnarc21415e2007-02-19 14:37:47 +02002014static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2015{
Dor Laor510043d2007-02-19 18:25:43 +02002016 skip_emulated_instruction(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02002017 return kvm_hypercall(vcpu, kvm_run);
Ingo Molnarc21415e2007-02-19 14:37:47 +02002018}
2019
Avi Kivity6aa8b732006-12-10 02:21:36 -08002020/*
2021 * The exit handlers return 1 if the exit was handled fully and guest execution
2022 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2023 * to be done to userspace and return 0.
2024 */
2025static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
2026 struct kvm_run *kvm_run) = {
2027 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
2028 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08002029 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002030 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002031 [EXIT_REASON_CR_ACCESS] = handle_cr,
2032 [EXIT_REASON_DR_ACCESS] = handle_dr,
2033 [EXIT_REASON_CPUID] = handle_cpuid,
2034 [EXIT_REASON_MSR_READ] = handle_rdmsr,
2035 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
2036 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
2037 [EXIT_REASON_HLT] = handle_halt,
Ingo Molnarc21415e2007-02-19 14:37:47 +02002038 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002039};
2040
2041static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04002042 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002043
2044/*
2045 * The guest has exited. See if we can fix it or if we need userspace
2046 * assistance.
2047 */
2048static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2049{
2050 u32 vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2051 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
2052
2053 if ( (vectoring_info & VECTORING_INFO_VALID_MASK) &&
2054 exit_reason != EXIT_REASON_EXCEPTION_NMI )
2055 printk(KERN_WARNING "%s: unexpected, valid vectoring info and "
2056 "exit reason is 0x%x\n", __FUNCTION__, exit_reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002057 if (exit_reason < kvm_vmx_max_exit_handlers
2058 && kvm_vmx_exit_handlers[exit_reason])
2059 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
2060 else {
2061 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2062 kvm_run->hw.hardware_exit_reason = exit_reason;
2063 }
2064 return 0;
2065}
2066
Dor Laorc1150d82007-01-05 16:36:24 -08002067/*
2068 * Check if userspace requested an interrupt window, and that the
2069 * interrupt window is open.
2070 *
2071 * No need to exit to userspace if we already have an interrupt queued.
2072 */
2073static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2074 struct kvm_run *kvm_run)
2075{
2076 return (!vcpu->irq_summary &&
2077 kvm_run->request_interrupt_window &&
2078 vcpu->interrupt_window_open &&
2079 (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF));
2080}
2081
Avi Kivityd9e368d2007-06-07 19:18:30 +03002082static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
2083{
Avi Kivityd9e368d2007-06-07 19:18:30 +03002084}
2085
Eddie Dong85f455f2007-07-06 12:20:49 +03002086static void enable_irq_window(struct kvm_vcpu *vcpu)
2087{
2088 u32 cpu_based_vm_exec_control;
2089
2090 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2091 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2092 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2093}
2094
2095static void vmx_intr_assist(struct kvm_vcpu *vcpu)
2096{
2097 u32 idtv_info_field, intr_info_field;
2098 int has_ext_irq, interrupt_window_open;
2099
2100 has_ext_irq = kvm_cpu_has_interrupt(vcpu);
2101 intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
2102 idtv_info_field = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2103 if (intr_info_field & INTR_INFO_VALID_MASK) {
2104 if (idtv_info_field & INTR_INFO_VALID_MASK) {
2105 /* TODO: fault when IDT_Vectoring */
2106 printk(KERN_ERR "Fault when IDT_Vectoring\n");
2107 }
2108 if (has_ext_irq)
2109 enable_irq_window(vcpu);
2110 return;
2111 }
2112 if (unlikely(idtv_info_field & INTR_INFO_VALID_MASK)) {
2113 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, idtv_info_field);
2114 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2115 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
2116
2117 if (unlikely(idtv_info_field & INTR_INFO_DELIEVER_CODE_MASK))
2118 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2119 vmcs_read32(IDT_VECTORING_ERROR_CODE));
2120 if (unlikely(has_ext_irq))
2121 enable_irq_window(vcpu);
2122 return;
2123 }
2124 if (!has_ext_irq)
2125 return;
2126 interrupt_window_open =
2127 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2128 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
2129 if (interrupt_window_open)
2130 vmx_inject_irq(vcpu, kvm_cpu_get_interrupt(vcpu));
2131 else
2132 enable_irq_window(vcpu);
2133}
2134
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135static int vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2136{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002137 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002138 u8 fail;
Avi Kivitye2dec932007-01-05 16:36:54 -08002139 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002140
Avi Kivitye6adf282007-04-30 16:07:54 +03002141preempted:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002142 if (vcpu->guest_debug.enabled)
2143 kvm_guest_debug_pre(vcpu);
2144
Avi Kivitye6adf282007-04-30 16:07:54 +03002145again:
Shaohua Li9ae04482007-07-23 14:51:32 +08002146 r = kvm_mmu_reload(vcpu);
2147 if (unlikely(r))
2148 goto out;
2149
Avi Kivity15ad7142007-07-11 18:17:21 +03002150 preempt_disable();
2151
Rusty Russell8b9cf982007-07-30 16:31:43 +10002152 vmx_save_host_state(vmx);
Avi Kivitye6adf282007-04-30 16:07:54 +03002153 kvm_load_guest_fpu(vcpu);
2154
2155 /*
2156 * Loading guest fpu may have cleared host cr0.ts
2157 */
2158 vmcs_writel(HOST_CR0, read_cr0());
2159
Avi Kivityd9e368d2007-06-07 19:18:30 +03002160 local_irq_disable();
2161
Avi Kivity7e66f352007-08-15 15:23:34 +03002162 if (signal_pending(current)) {
2163 local_irq_enable();
2164 preempt_enable();
2165 r = -EINTR;
2166 kvm_run->exit_reason = KVM_EXIT_INTR;
2167 ++vcpu->stat.signal_exits;
2168 goto out;
2169 }
2170
Eddie Dong85f455f2007-07-06 12:20:49 +03002171 if (irqchip_in_kernel(vcpu->kvm))
2172 vmx_intr_assist(vcpu);
2173 else if (!vcpu->mmio_read_completed)
Avi Kivity7e66f352007-08-15 15:23:34 +03002174 do_interrupt_requests(vcpu, kvm_run);
2175
Avi Kivityd9e368d2007-06-07 19:18:30 +03002176 vcpu->guest_mode = 1;
2177 if (vcpu->requests)
2178 if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
2179 vmx_flush_tlb(vcpu);
2180
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181 asm (
2182 /* Store host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002183#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184 "push %%rax; push %%rbx; push %%rdx;"
2185 "push %%rsi; push %%rdi; push %%rbp;"
2186 "push %%r8; push %%r9; push %%r10; push %%r11;"
2187 "push %%r12; push %%r13; push %%r14; push %%r15;"
2188 "push %%rcx \n\t"
2189 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
2190#else
2191 "pusha; push %%ecx \n\t"
2192 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
2193#endif
2194 /* Check if vmlaunch of vmresume is needed */
2195 "cmp $0, %1 \n\t"
2196 /* Load guest registers. Don't clobber flags. */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002197#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002198 "mov %c[cr2](%3), %%rax \n\t"
2199 "mov %%rax, %%cr2 \n\t"
2200 "mov %c[rax](%3), %%rax \n\t"
2201 "mov %c[rbx](%3), %%rbx \n\t"
2202 "mov %c[rdx](%3), %%rdx \n\t"
2203 "mov %c[rsi](%3), %%rsi \n\t"
2204 "mov %c[rdi](%3), %%rdi \n\t"
2205 "mov %c[rbp](%3), %%rbp \n\t"
2206 "mov %c[r8](%3), %%r8 \n\t"
2207 "mov %c[r9](%3), %%r9 \n\t"
2208 "mov %c[r10](%3), %%r10 \n\t"
2209 "mov %c[r11](%3), %%r11 \n\t"
2210 "mov %c[r12](%3), %%r12 \n\t"
2211 "mov %c[r13](%3), %%r13 \n\t"
2212 "mov %c[r14](%3), %%r14 \n\t"
2213 "mov %c[r15](%3), %%r15 \n\t"
2214 "mov %c[rcx](%3), %%rcx \n\t" /* kills %3 (rcx) */
2215#else
2216 "mov %c[cr2](%3), %%eax \n\t"
2217 "mov %%eax, %%cr2 \n\t"
2218 "mov %c[rax](%3), %%eax \n\t"
2219 "mov %c[rbx](%3), %%ebx \n\t"
2220 "mov %c[rdx](%3), %%edx \n\t"
2221 "mov %c[rsi](%3), %%esi \n\t"
2222 "mov %c[rdi](%3), %%edi \n\t"
2223 "mov %c[rbp](%3), %%ebp \n\t"
2224 "mov %c[rcx](%3), %%ecx \n\t" /* kills %3 (ecx) */
2225#endif
2226 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03002227 "jne .Llaunched \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228 ASM_VMX_VMLAUNCH "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03002229 "jmp .Lkvm_vmx_return \n\t"
2230 ".Llaunched: " ASM_VMX_VMRESUME "\n\t"
2231 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232 /* Save guest registers, load host registers, keep flags */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002233#ifdef CONFIG_X86_64
Ingo Molnar96958232007-02-12 00:54:33 -08002234 "xchg %3, (%%rsp) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002235 "mov %%rax, %c[rax](%3) \n\t"
2236 "mov %%rbx, %c[rbx](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002237 "pushq (%%rsp); popq %c[rcx](%3) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002238 "mov %%rdx, %c[rdx](%3) \n\t"
2239 "mov %%rsi, %c[rsi](%3) \n\t"
2240 "mov %%rdi, %c[rdi](%3) \n\t"
2241 "mov %%rbp, %c[rbp](%3) \n\t"
2242 "mov %%r8, %c[r8](%3) \n\t"
2243 "mov %%r9, %c[r9](%3) \n\t"
2244 "mov %%r10, %c[r10](%3) \n\t"
2245 "mov %%r11, %c[r11](%3) \n\t"
2246 "mov %%r12, %c[r12](%3) \n\t"
2247 "mov %%r13, %c[r13](%3) \n\t"
2248 "mov %%r14, %c[r14](%3) \n\t"
2249 "mov %%r15, %c[r15](%3) \n\t"
2250 "mov %%cr2, %%rax \n\t"
2251 "mov %%rax, %c[cr2](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002252 "mov (%%rsp), %3 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253
2254 "pop %%rcx; pop %%r15; pop %%r14; pop %%r13; pop %%r12;"
2255 "pop %%r11; pop %%r10; pop %%r9; pop %%r8;"
2256 "pop %%rbp; pop %%rdi; pop %%rsi;"
2257 "pop %%rdx; pop %%rbx; pop %%rax \n\t"
2258#else
Ingo Molnar96958232007-02-12 00:54:33 -08002259 "xchg %3, (%%esp) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260 "mov %%eax, %c[rax](%3) \n\t"
2261 "mov %%ebx, %c[rbx](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002262 "pushl (%%esp); popl %c[rcx](%3) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002263 "mov %%edx, %c[rdx](%3) \n\t"
2264 "mov %%esi, %c[rsi](%3) \n\t"
2265 "mov %%edi, %c[rdi](%3) \n\t"
2266 "mov %%ebp, %c[rbp](%3) \n\t"
2267 "mov %%cr2, %%eax \n\t"
2268 "mov %%eax, %c[cr2](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002269 "mov (%%esp), %3 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270
2271 "pop %%ecx; popa \n\t"
2272#endif
2273 "setbe %0 \n\t"
Herbert Xue0015482007-01-23 14:10:00 +11002274 : "=q" (fail)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002275 : "r"(vmx->launched), "d"((unsigned long)HOST_RSP),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002276 "c"(vcpu),
2277 [rax]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RAX])),
2278 [rbx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBX])),
2279 [rcx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RCX])),
2280 [rdx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDX])),
2281 [rsi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RSI])),
2282 [rdi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDI])),
2283 [rbp]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002284#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285 [r8 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R8 ])),
2286 [r9 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R9 ])),
2287 [r10]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R10])),
2288 [r11]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R11])),
2289 [r12]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R12])),
2290 [r13]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R13])),
2291 [r14]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R14])),
2292 [r15]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R15])),
2293#endif
2294 [cr2]"i"(offsetof(struct kvm_vcpu, cr2))
2295 : "cc", "memory" );
2296
Avi Kivityd9e368d2007-06-07 19:18:30 +03002297 vcpu->guest_mode = 0;
2298 local_irq_enable();
2299
Avi Kivity1165f5f2007-04-19 17:27:43 +03002300 ++vcpu->stat.exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002301
Dor Laorc1150d82007-01-05 16:36:24 -08002302 vcpu->interrupt_window_open = (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002303
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304 asm ("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03002305 vmx->launched = 1;
2306
2307 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308
Avi Kivity05e0c8c2007-04-30 16:15:58 +03002309 if (unlikely(fail)) {
Avi Kivity8eb7d332007-03-04 14:17:08 +02002310 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2311 kvm_run->fail_entry.hardware_entry_failure_reason
2312 = vmcs_read32(VM_INSTRUCTION_ERROR);
Avi Kivitye2dec932007-01-05 16:36:54 -08002313 r = 0;
Avi Kivity05e0c8c2007-04-30 16:15:58 +03002314 goto out;
2315 }
2316 /*
2317 * Profile KVM exit RIPs:
2318 */
2319 if (unlikely(prof_on == KVM_PROFILING))
2320 profile_hit(KVM_PROFILING, (void *)vmcs_readl(GUEST_RIP));
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +01002321
Avi Kivity05e0c8c2007-04-30 16:15:58 +03002322 r = kvm_handle_exit(kvm_run, vcpu);
2323 if (r > 0) {
Avi Kivity05e0c8c2007-04-30 16:15:58 +03002324 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2325 r = -EINTR;
2326 kvm_run->exit_reason = KVM_EXIT_INTR;
2327 ++vcpu->stat.request_irq_exits;
2328 goto out;
2329 }
2330 if (!need_resched()) {
2331 ++vcpu->stat.light_exits;
2332 goto again;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333 }
2334 }
Dor Laorc1150d82007-01-05 16:36:24 -08002335
Avi Kivitye6adf282007-04-30 16:07:54 +03002336out:
Avi Kivitye6adf282007-04-30 16:07:54 +03002337 if (r > 0) {
2338 kvm_resched(vcpu);
2339 goto preempted;
2340 }
2341
Dor Laorc1150d82007-01-05 16:36:24 -08002342 post_kvm_run_save(vcpu, kvm_run);
Avi Kivitye2dec932007-01-05 16:36:54 -08002343 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344}
2345
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346static void vmx_inject_page_fault(struct kvm_vcpu *vcpu,
2347 unsigned long addr,
2348 u32 err_code)
2349{
2350 u32 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2351
Avi Kivity1165f5f2007-04-19 17:27:43 +03002352 ++vcpu->stat.pf_guest;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002353
2354 if (is_page_fault(vect_info)) {
2355 printk(KERN_DEBUG "inject_page_fault: "
2356 "double fault 0x%lx @ 0x%lx\n",
2357 addr, vmcs_readl(GUEST_RIP));
2358 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
2359 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2360 DF_VECTOR |
2361 INTR_TYPE_EXCEPTION |
2362 INTR_INFO_DELIEVER_CODE_MASK |
2363 INTR_INFO_VALID_MASK);
2364 return;
2365 }
2366 vcpu->cr2 = addr;
2367 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, err_code);
2368 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2369 PF_VECTOR |
2370 INTR_TYPE_EXCEPTION |
2371 INTR_INFO_DELIEVER_CODE_MASK |
2372 INTR_INFO_VALID_MASK);
2373
2374}
2375
2376static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
2377{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002378 struct vcpu_vmx *vmx = to_vmx(vcpu);
2379
2380 if (vmx->vmcs) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002381 on_each_cpu(__vcpu_clear, vmx, 0, 1);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002382 free_vmcs(vmx->vmcs);
2383 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002384 }
2385}
2386
2387static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
2388{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002389 struct vcpu_vmx *vmx = to_vmx(vcpu);
2390
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002392 kfree(vmx->host_msrs);
2393 kfree(vmx->guest_msrs);
2394 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10002395 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396}
2397
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002398static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002400 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10002401 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03002402 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002403
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002404 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002405 return ERR_PTR(-ENOMEM);
2406
2407 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
2408 if (err)
2409 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002410
Eddie Dong97222cc2007-09-12 10:58:04 +03002411 if (irqchip_in_kernel(kvm)) {
2412 err = kvm_create_lapic(&vmx->vcpu);
2413 if (err < 0)
2414 goto free_vcpu;
2415 }
2416
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002417 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002418 if (!vmx->guest_msrs) {
2419 err = -ENOMEM;
2420 goto uninit_vcpu;
2421 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08002422
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002423 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
2424 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002425 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002426
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002427 vmx->vmcs = alloc_vmcs();
2428 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002429 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002430
2431 vmcs_clear(vmx->vmcs);
2432
Avi Kivity15ad7142007-07-11 18:17:21 +03002433 cpu = get_cpu();
2434 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002435 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002436 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002437 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002438 if (err)
2439 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002440
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002441 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002442
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002443free_vmcs:
2444 free_vmcs(vmx->vmcs);
2445free_msrs:
2446 kfree(vmx->host_msrs);
2447free_guest_msrs:
2448 kfree(vmx->guest_msrs);
2449uninit_vcpu:
2450 kvm_vcpu_uninit(&vmx->vcpu);
2451free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10002452 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002453 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454}
2455
Yang, Sheng002c7f72007-07-31 14:23:01 +03002456static void __init vmx_check_processor_compat(void *rtn)
2457{
2458 struct vmcs_config vmcs_conf;
2459
2460 *(int *)rtn = 0;
2461 if (setup_vmcs_config(&vmcs_conf) < 0)
2462 *(int *)rtn = -EIO;
2463 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
2464 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
2465 smp_processor_id());
2466 *(int *)rtn = -EIO;
2467 }
2468}
2469
Avi Kivity6aa8b732006-12-10 02:21:36 -08002470static struct kvm_arch_ops vmx_arch_ops = {
2471 .cpu_has_kvm_support = cpu_has_kvm_support,
2472 .disabled_by_bios = vmx_disabled_by_bios,
2473 .hardware_setup = hardware_setup,
2474 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03002475 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002476 .hardware_enable = hardware_enable,
2477 .hardware_disable = hardware_disable,
2478
2479 .vcpu_create = vmx_create_vcpu,
2480 .vcpu_free = vmx_free_vcpu,
2481
2482 .vcpu_load = vmx_vcpu_load,
2483 .vcpu_put = vmx_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08002484 .vcpu_decache = vmx_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002485
2486 .set_guest_debug = set_guest_debug,
2487 .get_msr = vmx_get_msr,
2488 .set_msr = vmx_set_msr,
2489 .get_segment_base = vmx_get_segment_base,
2490 .get_segment = vmx_get_segment,
2491 .set_segment = vmx_set_segment,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03002493 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002494 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002495 .set_cr3 = vmx_set_cr3,
2496 .set_cr4 = vmx_set_cr4,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002497#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498 .set_efer = vmx_set_efer,
2499#endif
2500 .get_idt = vmx_get_idt,
2501 .set_idt = vmx_set_idt,
2502 .get_gdt = vmx_get_gdt,
2503 .set_gdt = vmx_set_gdt,
2504 .cache_regs = vcpu_load_rsp_rip,
2505 .decache_regs = vcpu_put_rsp_rip,
2506 .get_rflags = vmx_get_rflags,
2507 .set_rflags = vmx_set_rflags,
2508
2509 .tlb_flush = vmx_flush_tlb,
2510 .inject_page_fault = vmx_inject_page_fault,
2511
2512 .inject_gp = vmx_inject_gp,
2513
2514 .run = vmx_vcpu_run,
2515 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02002516 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03002517 .get_irq = vmx_get_irq,
2518 .set_irq = vmx_inject_irq,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519};
2520
2521static int __init vmx_init(void)
2522{
He, Qingfdef3ad2007-04-30 09:45:24 +03002523 void *iova;
2524 int r;
2525
2526 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2527 if (!vmx_io_bitmap_a)
2528 return -ENOMEM;
2529
2530 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2531 if (!vmx_io_bitmap_b) {
2532 r = -ENOMEM;
2533 goto out;
2534 }
2535
2536 /*
2537 * Allow direct access to the PC debug port (it is often used for I/O
2538 * delays, but the vmexits simply slow things down).
2539 */
2540 iova = kmap(vmx_io_bitmap_a);
2541 memset(iova, 0xff, PAGE_SIZE);
2542 clear_bit(0x80, iova);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002543 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03002544
2545 iova = kmap(vmx_io_bitmap_b);
2546 memset(iova, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002547 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03002548
Rusty Russellc16f8622007-07-30 21:12:19 +10002549 r = kvm_init_arch(&vmx_arch_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03002550 if (r)
2551 goto out1;
2552
2553 return 0;
2554
2555out1:
2556 __free_page(vmx_io_bitmap_b);
2557out:
2558 __free_page(vmx_io_bitmap_a);
2559 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002560}
2561
2562static void __exit vmx_exit(void)
2563{
He, Qingfdef3ad2007-04-30 09:45:24 +03002564 __free_page(vmx_io_bitmap_b);
2565 __free_page(vmx_io_bitmap_a);
2566
Avi Kivity6aa8b732006-12-10 02:21:36 -08002567 kvm_exit_arch();
2568}
2569
2570module_init(vmx_init)
2571module_exit(vmx_exit)