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