blob: 2b30274656f43b4a1533a4e63c08df186e09cecd [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"
Avi Kivity6aa8b732006-12-10 02:21:36 -080020#include "vmx.h"
Avi Kivitye4956062007-06-28 14:15:57 -040021#include "segment_descriptor.h"
22
Avi Kivity6aa8b732006-12-10 02:21:36 -080023#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020024#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/mm.h>
26#include <linux/highmem.h>
Ingo Molnar07031e12007-01-10 23:15:38 -080027#include <linux/profile.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040028#include <linux/sched.h>
Avi Kivitye4956062007-06-28 14:15:57 -040029
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080031#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032
Avi Kivity6aa8b732006-12-10 02:21:36 -080033MODULE_AUTHOR("Qumranet");
34MODULE_LICENSE("GPL");
35
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040036struct vmcs {
37 u32 revision_id;
38 u32 abort;
39 char data[0];
40};
41
42struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100043 struct kvm_vcpu vcpu;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040044 int launched;
45 struct kvm_msr_entry *guest_msrs;
46 struct kvm_msr_entry *host_msrs;
47 int nmsrs;
48 int save_nmsrs;
49 int msr_offset_efer;
50#ifdef CONFIG_X86_64
51 int msr_offset_kernel_gs_base;
52#endif
53 struct vmcs *vmcs;
54 struct {
55 int loaded;
56 u16 fs_sel, gs_sel, ldt_sel;
57 int fs_gs_ldt_reload_needed;
58 }host_state;
59
60};
61
62static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
63{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100064 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040065}
66
Avi Kivity75880a02007-06-20 11:20:04 +030067static int init_rmode_tss(struct kvm *kvm);
68
Avi Kivity6aa8b732006-12-10 02:21:36 -080069static DEFINE_PER_CPU(struct vmcs *, vmxarea);
70static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
71
He, Qingfdef3ad2007-04-30 09:45:24 +030072static struct page *vmx_io_bitmap_a;
73static struct page *vmx_io_bitmap_b;
74
Eddie Dong2cc51562007-05-21 07:28:09 +030075#define EFER_SAVE_RESTORE_BITS ((u64)EFER_SCE)
Avi Kivity6aa8b732006-12-10 02:21:36 -080076
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +030077static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -080078 int size;
79 int order;
80 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +030081 u32 pin_based_exec_ctrl;
82 u32 cpu_based_exec_ctrl;
83 u32 vmexit_ctrl;
84 u32 vmentry_ctrl;
85} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -080086
87#define VMX_SEGMENT_FIELD(seg) \
88 [VCPU_SREG_##seg] = { \
89 .selector = GUEST_##seg##_SELECTOR, \
90 .base = GUEST_##seg##_BASE, \
91 .limit = GUEST_##seg##_LIMIT, \
92 .ar_bytes = GUEST_##seg##_AR_BYTES, \
93 }
94
95static struct kvm_vmx_segment_field {
96 unsigned selector;
97 unsigned base;
98 unsigned limit;
99 unsigned ar_bytes;
100} kvm_vmx_segment_fields[] = {
101 VMX_SEGMENT_FIELD(CS),
102 VMX_SEGMENT_FIELD(DS),
103 VMX_SEGMENT_FIELD(ES),
104 VMX_SEGMENT_FIELD(FS),
105 VMX_SEGMENT_FIELD(GS),
106 VMX_SEGMENT_FIELD(SS),
107 VMX_SEGMENT_FIELD(TR),
108 VMX_SEGMENT_FIELD(LDTR),
109};
110
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300111/*
112 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
113 * away by decrementing the array size.
114 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800115static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800116#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800117 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
118#endif
119 MSR_EFER, MSR_K6_STAR,
120};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200121#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400123static void load_msrs(struct kvm_msr_entry *e, int n)
124{
125 int i;
126
127 for (i = 0; i < n; ++i)
128 wrmsrl(e[i].index, e[i].data);
129}
130
131static void save_msrs(struct kvm_msr_entry *e, int n)
132{
133 int i;
134
135 for (i = 0; i < n; ++i)
136 rdmsrl(e[i].index, e[i].data);
137}
138
139static inline u64 msr_efer_save_restore_bits(struct kvm_msr_entry msr)
Eddie Dong2cc51562007-05-21 07:28:09 +0300140{
141 return (u64)msr.data & EFER_SAVE_RESTORE_BITS;
142}
143
Rusty Russell8b9cf982007-07-30 16:31:43 +1000144static inline int msr_efer_need_save_restore(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300145{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400146 int efer_offset = vmx->msr_offset_efer;
147 return msr_efer_save_restore_bits(vmx->host_msrs[efer_offset]) !=
148 msr_efer_save_restore_bits(vmx->guest_msrs[efer_offset]);
Eddie Dong2cc51562007-05-21 07:28:09 +0300149}
150
Avi Kivity6aa8b732006-12-10 02:21:36 -0800151static inline int is_page_fault(u32 intr_info)
152{
153 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
154 INTR_INFO_VALID_MASK)) ==
155 (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
156}
157
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300158static inline int is_no_device(u32 intr_info)
159{
160 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
161 INTR_INFO_VALID_MASK)) ==
162 (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
163}
164
Avi Kivity6aa8b732006-12-10 02:21:36 -0800165static inline int is_external_interrupt(u32 intr_info)
166{
167 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
168 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
169}
170
Rusty Russell8b9cf982007-07-30 16:31:43 +1000171static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800172{
173 int i;
174
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400175 for (i = 0; i < vmx->nmsrs; ++i)
176 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300177 return i;
178 return -1;
179}
180
Rusty Russell8b9cf982007-07-30 16:31:43 +1000181static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300182{
183 int i;
184
Rusty Russell8b9cf982007-07-30 16:31:43 +1000185 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300186 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400187 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000188 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800189}
190
Avi Kivity6aa8b732006-12-10 02:21:36 -0800191static void vmcs_clear(struct vmcs *vmcs)
192{
193 u64 phys_addr = __pa(vmcs);
194 u8 error;
195
196 asm volatile (ASM_VMX_VMCLEAR_RAX "; setna %0"
197 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
198 : "cc", "memory");
199 if (error)
200 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
201 vmcs, phys_addr);
202}
203
204static void __vcpu_clear(void *arg)
205{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000206 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800207 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208
Rusty Russell8b9cf982007-07-30 16:31:43 +1000209 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400210 vmcs_clear(vmx->vmcs);
211 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212 per_cpu(current_vmcs, cpu) = NULL;
Rusty Russell8b9cf982007-07-30 16:31:43 +1000213 rdtscll(vmx->vcpu.host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800214}
215
Rusty Russell8b9cf982007-07-30 16:31:43 +1000216static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800217{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000218 if (vmx->vcpu.cpu != raw_smp_processor_id() && vmx->vcpu.cpu != -1)
219 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear,
220 vmx, 0, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800221 else
Rusty Russell8b9cf982007-07-30 16:31:43 +1000222 __vcpu_clear(vmx);
223 vmx->launched = 0;
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800224}
225
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226static unsigned long vmcs_readl(unsigned long field)
227{
228 unsigned long value;
229
230 asm volatile (ASM_VMX_VMREAD_RDX_RAX
231 : "=a"(value) : "d"(field) : "cc");
232 return value;
233}
234
235static u16 vmcs_read16(unsigned long field)
236{
237 return vmcs_readl(field);
238}
239
240static u32 vmcs_read32(unsigned long field)
241{
242 return vmcs_readl(field);
243}
244
245static u64 vmcs_read64(unsigned long field)
246{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800247#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248 return vmcs_readl(field);
249#else
250 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
251#endif
252}
253
Avi Kivitye52de1b2007-01-05 16:36:56 -0800254static noinline void vmwrite_error(unsigned long field, unsigned long value)
255{
256 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
257 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
258 dump_stack();
259}
260
Avi Kivity6aa8b732006-12-10 02:21:36 -0800261static void vmcs_writel(unsigned long field, unsigned long value)
262{
263 u8 error;
264
265 asm volatile (ASM_VMX_VMWRITE_RAX_RDX "; setna %0"
266 : "=q"(error) : "a"(value), "d"(field) : "cc" );
Avi Kivitye52de1b2007-01-05 16:36:56 -0800267 if (unlikely(error))
268 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800269}
270
271static void vmcs_write16(unsigned long field, u16 value)
272{
273 vmcs_writel(field, value);
274}
275
276static void vmcs_write32(unsigned long field, u32 value)
277{
278 vmcs_writel(field, value);
279}
280
281static void vmcs_write64(unsigned long field, u64 value)
282{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800283#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800284 vmcs_writel(field, value);
285#else
286 vmcs_writel(field, value);
287 asm volatile ("");
288 vmcs_writel(field+1, value >> 32);
289#endif
290}
291
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300292static void vmcs_clear_bits(unsigned long field, u32 mask)
293{
294 vmcs_writel(field, vmcs_readl(field) & ~mask);
295}
296
297static void vmcs_set_bits(unsigned long field, u32 mask)
298{
299 vmcs_writel(field, vmcs_readl(field) | mask);
300}
301
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300302static void update_exception_bitmap(struct kvm_vcpu *vcpu)
303{
304 u32 eb;
305
306 eb = 1u << PF_VECTOR;
307 if (!vcpu->fpu_active)
308 eb |= 1u << NM_VECTOR;
309 if (vcpu->guest_debug.enabled)
310 eb |= 1u << 1;
311 if (vcpu->rmode.active)
312 eb = ~0;
313 vmcs_write32(EXCEPTION_BITMAP, eb);
314}
315
Avi Kivity33ed6322007-05-02 16:54:03 +0300316static void reload_tss(void)
317{
318#ifndef CONFIG_X86_64
319
320 /*
321 * VT restores TR but not its size. Useless.
322 */
323 struct descriptor_table gdt;
324 struct segment_descriptor *descs;
325
326 get_gdt(&gdt);
327 descs = (void *)gdt.base;
328 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
329 load_TR_desc();
330#endif
331}
332
Rusty Russell8b9cf982007-07-30 16:31:43 +1000333static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300334{
335 u64 trans_efer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400336 int efer_offset = vmx->msr_offset_efer;
Eddie Dong2cc51562007-05-21 07:28:09 +0300337
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400338 trans_efer = vmx->host_msrs[efer_offset].data;
Eddie Dong2cc51562007-05-21 07:28:09 +0300339 trans_efer &= ~EFER_SAVE_RESTORE_BITS;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400340 trans_efer |= msr_efer_save_restore_bits(vmx->guest_msrs[efer_offset]);
Eddie Dong2cc51562007-05-21 07:28:09 +0300341 wrmsrl(MSR_EFER, trans_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000342 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300343}
344
Rusty Russell8b9cf982007-07-30 16:31:43 +1000345static void vmx_save_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300346{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400347 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300348 return;
349
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400350 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300351 /*
352 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
353 * allow segment selectors with cpl > 0 or ti == 1.
354 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400355 vmx->host_state.ldt_sel = read_ldt();
356 vmx->host_state.fs_gs_ldt_reload_needed = vmx->host_state.ldt_sel;
357 vmx->host_state.fs_sel = read_fs();
358 if (!(vmx->host_state.fs_sel & 7))
359 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300360 else {
361 vmcs_write16(HOST_FS_SELECTOR, 0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400362 vmx->host_state.fs_gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300363 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400364 vmx->host_state.gs_sel = read_gs();
365 if (!(vmx->host_state.gs_sel & 7))
366 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300367 else {
368 vmcs_write16(HOST_GS_SELECTOR, 0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400369 vmx->host_state.fs_gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300370 }
371
372#ifdef CONFIG_X86_64
373 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
374 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
375#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400376 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
377 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300378#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300379
380#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000381 if (is_long_mode(&vmx->vcpu)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400382 save_msrs(vmx->host_msrs +
383 vmx->msr_offset_kernel_gs_base, 1);
Avi Kivity707c0872007-05-02 17:33:43 +0300384 }
385#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400386 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000387 if (msr_efer_need_save_restore(vmx))
388 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300389}
390
Rusty Russell8b9cf982007-07-30 16:31:43 +1000391static void vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300392{
Avi Kivity15ad7142007-07-11 18:17:21 +0300393 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300394
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400395 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300396 return;
397
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400398 vmx->host_state.loaded = 0;
399 if (vmx->host_state.fs_gs_ldt_reload_needed) {
400 load_ldt(vmx->host_state.ldt_sel);
401 load_fs(vmx->host_state.fs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300402 /*
403 * If we have to reload gs, we must take care to
404 * preserve our gs base.
405 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300406 local_irq_save(flags);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400407 load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300408#ifdef CONFIG_X86_64
409 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
410#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300411 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300412
413 reload_tss();
414 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400415 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
416 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000417 if (msr_efer_need_save_restore(vmx))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400418 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
Avi Kivity33ed6322007-05-02 16:54:03 +0300419}
420
Avi Kivity6aa8b732006-12-10 02:21:36 -0800421/*
422 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
423 * vcpu mutex is already taken.
424 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300425static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800426{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400427 struct vcpu_vmx *vmx = to_vmx(vcpu);
428 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity77002702007-06-13 19:55:28 +0300429 u64 tsc_this, delta;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800430
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800431 if (vcpu->cpu != cpu)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000432 vcpu_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400434 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435 u8 error;
436
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400437 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438 asm volatile (ASM_VMX_VMPTRLD_RAX "; setna %0"
439 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
440 : "cc");
441 if (error)
442 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400443 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800444 }
445
446 if (vcpu->cpu != cpu) {
447 struct descriptor_table dt;
448 unsigned long sysenter_esp;
449
450 vcpu->cpu = cpu;
451 /*
452 * Linux uses per-cpu TSS and GDT, so set these when switching
453 * processors.
454 */
455 vmcs_writel(HOST_TR_BASE, read_tr_base()); /* 22.2.4 */
456 get_gdt(&dt);
457 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
458
459 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
460 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300461
462 /*
463 * Make sure the time stamp counter is monotonous.
464 */
465 rdtscll(tsc_this);
466 delta = vcpu->host_tsc - tsc_this;
467 vmcs_write64(TSC_OFFSET, vmcs_read64(TSC_OFFSET) + delta);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800468 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469}
470
471static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
472{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000473 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity7702fd12007-06-14 16:27:40 +0300474 kvm_put_guest_fpu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475}
476
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300477static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
478{
479 if (vcpu->fpu_active)
480 return;
481 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000482 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
483 if (vcpu->cr0 & X86_CR0_TS)
484 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300485 update_exception_bitmap(vcpu);
486}
487
488static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
489{
490 if (!vcpu->fpu_active)
491 return;
492 vcpu->fpu_active = 0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000493 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300494 update_exception_bitmap(vcpu);
495}
496
Avi Kivity774c47f2007-02-12 00:54:47 -0800497static void vmx_vcpu_decache(struct kvm_vcpu *vcpu)
498{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000499 vcpu_clear(to_vmx(vcpu));
Avi Kivity774c47f2007-02-12 00:54:47 -0800500}
501
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
503{
504 return vmcs_readl(GUEST_RFLAGS);
505}
506
507static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
508{
509 vmcs_writel(GUEST_RFLAGS, rflags);
510}
511
512static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
513{
514 unsigned long rip;
515 u32 interruptibility;
516
517 rip = vmcs_readl(GUEST_RIP);
518 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
519 vmcs_writel(GUEST_RIP, rip);
520
521 /*
522 * We emulated an instruction, so temporary interrupt blocking
523 * should be removed, if set.
524 */
525 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
526 if (interruptibility & 3)
527 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
528 interruptibility & ~3);
Dor Laorc1150d82007-01-05 16:36:24 -0800529 vcpu->interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530}
531
532static void vmx_inject_gp(struct kvm_vcpu *vcpu, unsigned error_code)
533{
534 printk(KERN_DEBUG "inject_general_protection: rip 0x%lx\n",
535 vmcs_readl(GUEST_RIP));
536 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
537 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
538 GP_VECTOR |
539 INTR_TYPE_EXCEPTION |
540 INTR_INFO_DELIEVER_CODE_MASK |
541 INTR_INFO_VALID_MASK);
542}
543
544/*
Eddie Donga75beee2007-05-17 18:55:15 +0300545 * Swap MSR entry in host/guest MSR entry array.
546 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000547static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300548{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400549 struct kvm_msr_entry tmp;
550
551 tmp = vmx->guest_msrs[to];
552 vmx->guest_msrs[to] = vmx->guest_msrs[from];
553 vmx->guest_msrs[from] = tmp;
554 tmp = vmx->host_msrs[to];
555 vmx->host_msrs[to] = vmx->host_msrs[from];
556 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300557}
558
559/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300560 * Set up the vmcs to automatically save and restore system
561 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
562 * mode, as fiddling with msrs is very expensive.
563 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000564static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300565{
Eddie Dong2cc51562007-05-21 07:28:09 +0300566 int save_nmsrs;
Avi Kivitye38aea32007-04-19 13:22:48 +0300567
Eddie Donga75beee2007-05-17 18:55:15 +0300568 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300569#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000570 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300571 int index;
572
Rusty Russell8b9cf982007-07-30 16:31:43 +1000573 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300574 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000575 move_msr_up(vmx, index, save_nmsrs++);
576 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300577 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000578 move_msr_up(vmx, index, save_nmsrs++);
579 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300580 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000581 move_msr_up(vmx, index, save_nmsrs++);
582 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300583 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000584 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300585 /*
586 * MSR_K6_STAR is only needed on long mode guests, and only
587 * if efer.sce is enabled.
588 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000589 index = __find_msr_index(vmx, MSR_K6_STAR);
590 if ((index >= 0) && (vmx->vcpu.shadow_efer & EFER_SCE))
591 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300592 }
Eddie Donga75beee2007-05-17 18:55:15 +0300593#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400594 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300595
Eddie Donga75beee2007-05-17 18:55:15 +0300596#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400597 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000598 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300599#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000600 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivitye38aea32007-04-19 13:22:48 +0300601}
602
603/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800604 * reads and returns guest's timestamp counter "register"
605 * guest_tsc = host_tsc + tsc_offset -- 21.3
606 */
607static u64 guest_read_tsc(void)
608{
609 u64 host_tsc, tsc_offset;
610
611 rdtscll(host_tsc);
612 tsc_offset = vmcs_read64(TSC_OFFSET);
613 return host_tsc + tsc_offset;
614}
615
616/*
617 * writes 'guest_tsc' into guest's timestamp counter "register"
618 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
619 */
620static void guest_write_tsc(u64 guest_tsc)
621{
622 u64 host_tsc;
623
624 rdtscll(host_tsc);
625 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
626}
627
Avi Kivity6aa8b732006-12-10 02:21:36 -0800628/*
629 * Reads an msr value (of 'msr_index') into 'pdata'.
630 * Returns 0 on success, non-0 otherwise.
631 * Assumes vcpu_load() was already called.
632 */
633static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
634{
635 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400636 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637
638 if (!pdata) {
639 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
640 return -EINVAL;
641 }
642
643 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800644#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645 case MSR_FS_BASE:
646 data = vmcs_readl(GUEST_FS_BASE);
647 break;
648 case MSR_GS_BASE:
649 data = vmcs_readl(GUEST_GS_BASE);
650 break;
651 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800652 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653#endif
654 case MSR_IA32_TIME_STAMP_COUNTER:
655 data = guest_read_tsc();
656 break;
657 case MSR_IA32_SYSENTER_CS:
658 data = vmcs_read32(GUEST_SYSENTER_CS);
659 break;
660 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200661 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800662 break;
663 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200664 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800665 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000667 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800668 if (msr) {
669 data = msr->data;
670 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800672 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800673 }
674
675 *pdata = data;
676 return 0;
677}
678
679/*
680 * Writes msr value into into the appropriate "register".
681 * Returns 0 on success, non-0 otherwise.
682 * Assumes vcpu_load() was already called.
683 */
684static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
685{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400686 struct vcpu_vmx *vmx = to_vmx(vcpu);
687 struct kvm_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +0300688 int ret = 0;
689
Avi Kivity6aa8b732006-12-10 02:21:36 -0800690 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800691#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800692 case MSR_EFER:
Eddie Dong2cc51562007-05-21 07:28:09 +0300693 ret = kvm_set_msr_common(vcpu, msr_index, data);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400694 if (vmx->host_state.loaded)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000695 load_transition_efer(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +0300696 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800697 case MSR_FS_BASE:
698 vmcs_writel(GUEST_FS_BASE, data);
699 break;
700 case MSR_GS_BASE:
701 vmcs_writel(GUEST_GS_BASE, data);
702 break;
703#endif
704 case MSR_IA32_SYSENTER_CS:
705 vmcs_write32(GUEST_SYSENTER_CS, data);
706 break;
707 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200708 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709 break;
710 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200711 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200713 case MSR_IA32_TIME_STAMP_COUNTER:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714 guest_write_tsc(data);
715 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000717 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800718 if (msr) {
719 msr->data = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400720 if (vmx->host_state.loaded)
721 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800722 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300724 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800725 }
726
Eddie Dong2cc51562007-05-21 07:28:09 +0300727 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728}
729
730/*
731 * Sync the rsp and rip registers into the vcpu structure. This allows
732 * registers to be accessed by indexing vcpu->regs.
733 */
734static void vcpu_load_rsp_rip(struct kvm_vcpu *vcpu)
735{
736 vcpu->regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
737 vcpu->rip = vmcs_readl(GUEST_RIP);
738}
739
740/*
741 * Syncs rsp and rip back into the vmcs. Should be called after possible
742 * modification.
743 */
744static void vcpu_put_rsp_rip(struct kvm_vcpu *vcpu)
745{
746 vmcs_writel(GUEST_RSP, vcpu->regs[VCPU_REGS_RSP]);
747 vmcs_writel(GUEST_RIP, vcpu->rip);
748}
749
750static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
751{
752 unsigned long dr7 = 0x400;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753 int old_singlestep;
754
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755 old_singlestep = vcpu->guest_debug.singlestep;
756
757 vcpu->guest_debug.enabled = dbg->enabled;
758 if (vcpu->guest_debug.enabled) {
759 int i;
760
761 dr7 |= 0x200; /* exact */
762 for (i = 0; i < 4; ++i) {
763 if (!dbg->breakpoints[i].enabled)
764 continue;
765 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
766 dr7 |= 2 << (i*2); /* global enable */
767 dr7 |= 0 << (i*4+16); /* execution breakpoint */
768 }
769
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770 vcpu->guest_debug.singlestep = dbg->singlestep;
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300771 } else
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772 vcpu->guest_debug.singlestep = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773
774 if (old_singlestep && !vcpu->guest_debug.singlestep) {
775 unsigned long flags;
776
777 flags = vmcs_readl(GUEST_RFLAGS);
778 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
779 vmcs_writel(GUEST_RFLAGS, flags);
780 }
781
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300782 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783 vmcs_writel(GUEST_DR7, dr7);
784
785 return 0;
786}
787
788static __init int cpu_has_kvm_support(void)
789{
790 unsigned long ecx = cpuid_ecx(1);
791 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
792}
793
794static __init int vmx_disabled_by_bios(void)
795{
796 u64 msr;
797
798 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300799 return (msr & (MSR_IA32_FEATURE_CONTROL_LOCKED |
800 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
801 == MSR_IA32_FEATURE_CONTROL_LOCKED;
802 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803}
804
Avi Kivity774c47f2007-02-12 00:54:47 -0800805static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806{
807 int cpu = raw_smp_processor_id();
808 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
809 u64 old;
810
811 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300812 if ((old & (MSR_IA32_FEATURE_CONTROL_LOCKED |
813 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
814 != (MSR_IA32_FEATURE_CONTROL_LOCKED |
815 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300817 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
818 MSR_IA32_FEATURE_CONTROL_LOCKED |
819 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +1000820 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821 asm volatile (ASM_VMX_VMXON_RAX : : "a"(&phys_addr), "m"(phys_addr)
822 : "memory", "cc");
823}
824
825static void hardware_disable(void *garbage)
826{
827 asm volatile (ASM_VMX_VMXOFF : : : "cc");
828}
829
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300830static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
831 u32 msr, u32* result)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832{
833 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300834 u32 ctl = ctl_min | ctl_opt;
835
836 rdmsr(msr, vmx_msr_low, vmx_msr_high);
837
838 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
839 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
840
841 /* Ensure minimum (required) set of control bits are supported. */
842 if (ctl_min & ~ctl)
843 return -1;
844
845 *result = ctl;
846 return 0;
847}
848
849static __init int setup_vmcs_config(void)
850{
851 u32 vmx_msr_low, vmx_msr_high;
852 u32 min, opt;
853 u32 _pin_based_exec_control = 0;
854 u32 _cpu_based_exec_control = 0;
855 u32 _vmexit_control = 0;
856 u32 _vmentry_control = 0;
857
858 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
859 opt = 0;
860 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
861 &_pin_based_exec_control) < 0)
862 return -1;
863
864 min = CPU_BASED_HLT_EXITING |
865#ifdef CONFIG_X86_64
866 CPU_BASED_CR8_LOAD_EXITING |
867 CPU_BASED_CR8_STORE_EXITING |
868#endif
869 CPU_BASED_USE_IO_BITMAPS |
870 CPU_BASED_MOV_DR_EXITING |
871 CPU_BASED_USE_TSC_OFFSETING;
872 opt = 0;
873 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
874 &_cpu_based_exec_control) < 0)
875 return -1;
876
877 min = 0;
878#ifdef CONFIG_X86_64
879 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
880#endif
881 opt = 0;
882 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
883 &_vmexit_control) < 0)
884 return -1;
885
886 min = opt = 0;
887 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
888 &_vmentry_control) < 0)
889 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -0800891 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300892
893 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
894 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
895 return -1;
896
897#ifdef CONFIG_X86_64
898 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
899 if (vmx_msr_high & (1u<<16))
900 return -1;
901#endif
902
903 /* Require Write-Back (WB) memory type for VMCS accesses. */
904 if (((vmx_msr_high >> 18) & 15) != 6)
905 return -1;
906
907 vmcs_config.size = vmx_msr_high & 0x1fff;
908 vmcs_config.order = get_order(vmcs_config.size);
909 vmcs_config.revision_id = vmx_msr_low;
910
911 vmcs_config.pin_based_exec_ctrl = _pin_based_exec_control;
912 vmcs_config.cpu_based_exec_ctrl = _cpu_based_exec_control;
913 vmcs_config.vmexit_ctrl = _vmexit_control;
914 vmcs_config.vmentry_ctrl = _vmentry_control;
915
916 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -0800917}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918
919static struct vmcs *alloc_vmcs_cpu(int cpu)
920{
921 int node = cpu_to_node(cpu);
922 struct page *pages;
923 struct vmcs *vmcs;
924
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300925 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926 if (!pages)
927 return NULL;
928 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300929 memset(vmcs, 0, vmcs_config.size);
930 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931 return vmcs;
932}
933
934static struct vmcs *alloc_vmcs(void)
935{
Ingo Molnard3b2c332007-01-05 16:36:23 -0800936 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937}
938
939static void free_vmcs(struct vmcs *vmcs)
940{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300941 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942}
943
Sam Ravnborg39959582007-06-01 00:47:13 -0700944static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945{
946 int cpu;
947
948 for_each_online_cpu(cpu)
949 free_vmcs(per_cpu(vmxarea, cpu));
950}
951
952extern struct vmcs *alloc_vmcs_cpu(int cpu);
953
954static __init int alloc_kvm_area(void)
955{
956 int cpu;
957
958 for_each_online_cpu(cpu) {
959 struct vmcs *vmcs;
960
961 vmcs = alloc_vmcs_cpu(cpu);
962 if (!vmcs) {
963 free_kvm_area();
964 return -ENOMEM;
965 }
966
967 per_cpu(vmxarea, cpu) = vmcs;
968 }
969 return 0;
970}
971
972static __init int hardware_setup(void)
973{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300974 if (setup_vmcs_config() < 0)
975 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 return alloc_kvm_area();
977}
978
979static __exit void hardware_unsetup(void)
980{
981 free_kvm_area();
982}
983
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
985{
986 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
987
Avi Kivity6af11b92007-03-19 13:18:10 +0200988 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989 vmcs_write16(sf->selector, save->selector);
990 vmcs_writel(sf->base, save->base);
991 vmcs_write32(sf->limit, save->limit);
992 vmcs_write32(sf->ar_bytes, save->ar);
993 } else {
994 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
995 << AR_DPL_SHIFT;
996 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
997 }
998}
999
1000static void enter_pmode(struct kvm_vcpu *vcpu)
1001{
1002 unsigned long flags;
1003
1004 vcpu->rmode.active = 0;
1005
1006 vmcs_writel(GUEST_TR_BASE, vcpu->rmode.tr.base);
1007 vmcs_write32(GUEST_TR_LIMIT, vcpu->rmode.tr.limit);
1008 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->rmode.tr.ar);
1009
1010 flags = vmcs_readl(GUEST_RFLAGS);
1011 flags &= ~(IOPL_MASK | X86_EFLAGS_VM);
1012 flags |= (vcpu->rmode.save_iopl << IOPL_SHIFT);
1013 vmcs_writel(GUEST_RFLAGS, flags);
1014
Rusty Russell66aee912007-07-17 23:34:16 +10001015 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1016 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017
1018 update_exception_bitmap(vcpu);
1019
1020 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->rmode.es);
1021 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->rmode.ds);
1022 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->rmode.gs);
1023 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->rmode.fs);
1024
1025 vmcs_write16(GUEST_SS_SELECTOR, 0);
1026 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1027
1028 vmcs_write16(GUEST_CS_SELECTOR,
1029 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1030 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1031}
1032
1033static int rmode_tss_base(struct kvm* kvm)
1034{
1035 gfn_t base_gfn = kvm->memslots[0].base_gfn + kvm->memslots[0].npages - 3;
1036 return base_gfn << PAGE_SHIFT;
1037}
1038
1039static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1040{
1041 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1042
1043 save->selector = vmcs_read16(sf->selector);
1044 save->base = vmcs_readl(sf->base);
1045 save->limit = vmcs_read32(sf->limit);
1046 save->ar = vmcs_read32(sf->ar_bytes);
1047 vmcs_write16(sf->selector, vmcs_readl(sf->base) >> 4);
1048 vmcs_write32(sf->limit, 0xffff);
1049 vmcs_write32(sf->ar_bytes, 0xf3);
1050}
1051
1052static void enter_rmode(struct kvm_vcpu *vcpu)
1053{
1054 unsigned long flags;
1055
1056 vcpu->rmode.active = 1;
1057
1058 vcpu->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
1059 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1060
1061 vcpu->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
1062 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1063
1064 vcpu->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
1065 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1066
1067 flags = vmcs_readl(GUEST_RFLAGS);
1068 vcpu->rmode.save_iopl = (flags & IOPL_MASK) >> IOPL_SHIFT;
1069
1070 flags |= IOPL_MASK | X86_EFLAGS_VM;
1071
1072 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001073 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074 update_exception_bitmap(vcpu);
1075
1076 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1077 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1078 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1079
1080 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001081 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001082 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1083 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1085
1086 fix_rmode_seg(VCPU_SREG_ES, &vcpu->rmode.es);
1087 fix_rmode_seg(VCPU_SREG_DS, &vcpu->rmode.ds);
1088 fix_rmode_seg(VCPU_SREG_GS, &vcpu->rmode.gs);
1089 fix_rmode_seg(VCPU_SREG_FS, &vcpu->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001090
1091 init_rmode_tss(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092}
1093
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001094#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095
1096static void enter_lmode(struct kvm_vcpu *vcpu)
1097{
1098 u32 guest_tr_ar;
1099
1100 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1101 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1102 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
1103 __FUNCTION__);
1104 vmcs_write32(GUEST_TR_AR_BYTES,
1105 (guest_tr_ar & ~AR_TYPE_MASK)
1106 | AR_TYPE_BUSY_64_TSS);
1107 }
1108
1109 vcpu->shadow_efer |= EFER_LMA;
1110
Rusty Russell8b9cf982007-07-30 16:31:43 +10001111 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112 vmcs_write32(VM_ENTRY_CONTROLS,
1113 vmcs_read32(VM_ENTRY_CONTROLS)
1114 | VM_ENTRY_CONTROLS_IA32E_MASK);
1115}
1116
1117static void exit_lmode(struct kvm_vcpu *vcpu)
1118{
1119 vcpu->shadow_efer &= ~EFER_LMA;
1120
1121 vmcs_write32(VM_ENTRY_CONTROLS,
1122 vmcs_read32(VM_ENTRY_CONTROLS)
1123 & ~VM_ENTRY_CONTROLS_IA32E_MASK);
1124}
1125
1126#endif
1127
Anthony Liguori25c4c272007-04-27 09:29:21 +03001128static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001129{
Avi Kivity399badf2007-01-05 16:36:38 -08001130 vcpu->cr4 &= KVM_GUEST_CR4_MASK;
1131 vcpu->cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
1132}
1133
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1135{
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001136 vmx_fpu_deactivate(vcpu);
1137
Rusty Russell707d92fa2007-07-17 23:19:08 +10001138 if (vcpu->rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139 enter_pmode(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_rmode(vcpu);
1143
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001144#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145 if (vcpu->shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001146 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001148 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149 exit_lmode(vcpu);
1150 }
1151#endif
1152
1153 vmcs_writel(CR0_READ_SHADOW, cr0);
1154 vmcs_writel(GUEST_CR0,
1155 (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON);
1156 vcpu->cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001157
Rusty Russell707d92fa2007-07-17 23:19:08 +10001158 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001159 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160}
1161
Avi Kivity6aa8b732006-12-10 02:21:36 -08001162static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1163{
1164 vmcs_writel(GUEST_CR3, cr3);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001165 if (vcpu->cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001166 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167}
1168
1169static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1170{
1171 vmcs_writel(CR4_READ_SHADOW, cr4);
1172 vmcs_writel(GUEST_CR4, cr4 | (vcpu->rmode.active ?
1173 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON));
1174 vcpu->cr4 = cr4;
1175}
1176
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001177#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178
1179static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1180{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001181 struct vcpu_vmx *vmx = to_vmx(vcpu);
1182 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001183
1184 vcpu->shadow_efer = efer;
1185 if (efer & EFER_LMA) {
1186 vmcs_write32(VM_ENTRY_CONTROLS,
1187 vmcs_read32(VM_ENTRY_CONTROLS) |
1188 VM_ENTRY_CONTROLS_IA32E_MASK);
1189 msr->data = efer;
1190
1191 } else {
1192 vmcs_write32(VM_ENTRY_CONTROLS,
1193 vmcs_read32(VM_ENTRY_CONTROLS) &
1194 ~VM_ENTRY_CONTROLS_IA32E_MASK);
1195
1196 msr->data = efer & ~EFER_LME;
1197 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001198 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001199}
1200
1201#endif
1202
1203static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1204{
1205 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1206
1207 return vmcs_readl(sf->base);
1208}
1209
1210static void vmx_get_segment(struct kvm_vcpu *vcpu,
1211 struct kvm_segment *var, int seg)
1212{
1213 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1214 u32 ar;
1215
1216 var->base = vmcs_readl(sf->base);
1217 var->limit = vmcs_read32(sf->limit);
1218 var->selector = vmcs_read16(sf->selector);
1219 ar = vmcs_read32(sf->ar_bytes);
1220 if (ar & AR_UNUSABLE_MASK)
1221 ar = 0;
1222 var->type = ar & 15;
1223 var->s = (ar >> 4) & 1;
1224 var->dpl = (ar >> 5) & 3;
1225 var->present = (ar >> 7) & 1;
1226 var->avl = (ar >> 12) & 1;
1227 var->l = (ar >> 13) & 1;
1228 var->db = (ar >> 14) & 1;
1229 var->g = (ar >> 15) & 1;
1230 var->unusable = (ar >> 16) & 1;
1231}
1232
Avi Kivity653e3102007-05-07 10:55:37 +03001233static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001234{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001235 u32 ar;
1236
Avi Kivity653e3102007-05-07 10:55:37 +03001237 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001238 ar = 1 << 16;
1239 else {
1240 ar = var->type & 15;
1241 ar |= (var->s & 1) << 4;
1242 ar |= (var->dpl & 3) << 5;
1243 ar |= (var->present & 1) << 7;
1244 ar |= (var->avl & 1) << 12;
1245 ar |= (var->l & 1) << 13;
1246 ar |= (var->db & 1) << 14;
1247 ar |= (var->g & 1) << 15;
1248 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001249 if (ar == 0) /* a 0 value means unusable */
1250 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001251
1252 return ar;
1253}
1254
1255static void vmx_set_segment(struct kvm_vcpu *vcpu,
1256 struct kvm_segment *var, int seg)
1257{
1258 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1259 u32 ar;
1260
1261 if (vcpu->rmode.active && seg == VCPU_SREG_TR) {
1262 vcpu->rmode.tr.selector = var->selector;
1263 vcpu->rmode.tr.base = var->base;
1264 vcpu->rmode.tr.limit = var->limit;
1265 vcpu->rmode.tr.ar = vmx_segment_access_rights(var);
1266 return;
1267 }
1268 vmcs_writel(sf->base, var->base);
1269 vmcs_write32(sf->limit, var->limit);
1270 vmcs_write16(sf->selector, var->selector);
1271 if (vcpu->rmode.active && var->s) {
1272 /*
1273 * Hack real-mode segments into vm86 compatibility.
1274 */
1275 if (var->base == 0xffff0000 && var->selector == 0xf000)
1276 vmcs_writel(sf->base, 0xf0000);
1277 ar = 0xf3;
1278 } else
1279 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001280 vmcs_write32(sf->ar_bytes, ar);
1281}
1282
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1284{
1285 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1286
1287 *db = (ar >> 14) & 1;
1288 *l = (ar >> 13) & 1;
1289}
1290
1291static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1292{
1293 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1294 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1295}
1296
1297static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1298{
1299 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1300 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1301}
1302
1303static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1304{
1305 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1306 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1307}
1308
1309static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1310{
1311 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1312 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1313}
1314
1315static int init_rmode_tss(struct kvm* kvm)
1316{
1317 struct page *p1, *p2, *p3;
1318 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
1319 char *page;
1320
Avi Kivity954bbbc2007-03-30 14:02:32 +03001321 p1 = gfn_to_page(kvm, fn++);
1322 p2 = gfn_to_page(kvm, fn++);
1323 p3 = gfn_to_page(kvm, fn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324
1325 if (!p1 || !p2 || !p3) {
1326 kvm_printf(kvm,"%s: gfn_to_page failed\n", __FUNCTION__);
1327 return 0;
1328 }
1329
1330 page = kmap_atomic(p1, KM_USER0);
Shani Moideena3870c42007-06-11 09:31:33 +05301331 clear_page(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001332 *(u16*)(page + 0x66) = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
1333 kunmap_atomic(page, KM_USER0);
1334
1335 page = kmap_atomic(p2, KM_USER0);
Shani Moideena3870c42007-06-11 09:31:33 +05301336 clear_page(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337 kunmap_atomic(page, KM_USER0);
1338
1339 page = kmap_atomic(p3, KM_USER0);
Shani Moideena3870c42007-06-11 09:31:33 +05301340 clear_page(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001341 *(page + RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1) = ~0;
1342 kunmap_atomic(page, KM_USER0);
1343
1344 return 1;
1345}
1346
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347static void seg_setup(int seg)
1348{
1349 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1350
1351 vmcs_write16(sf->selector, 0);
1352 vmcs_writel(sf->base, 0);
1353 vmcs_write32(sf->limit, 0xffff);
1354 vmcs_write32(sf->ar_bytes, 0x93);
1355}
1356
1357/*
1358 * Sets up the vmcs for emulated real mode.
1359 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001360static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001361{
1362 u32 host_sysenter_cs;
1363 u32 junk;
1364 unsigned long a;
1365 struct descriptor_table dt;
1366 int i;
1367 int ret = 0;
Avi Kivitycd2276a2007-05-14 20:41:13 +03001368 unsigned long kvm_vmx_return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001369
Rusty Russell8b9cf982007-07-30 16:31:43 +10001370 if (!init_rmode_tss(vmx->vcpu.kvm)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371 ret = -ENOMEM;
1372 goto out;
1373 }
1374
Rusty Russell8b9cf982007-07-30 16:31:43 +10001375 vmx->vcpu.regs[VCPU_REGS_RDX] = get_rdx_init_val();
1376 vmx->vcpu.cr8 = 0;
1377 vmx->vcpu.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
1378 if (vmx->vcpu.vcpu_id == 0)
1379 vmx->vcpu.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380
Rusty Russell8b9cf982007-07-30 16:31:43 +10001381 fx_init(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382
1383 /*
1384 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
1385 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
1386 */
1387 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
1388 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
1389 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
1390 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1391
1392 seg_setup(VCPU_SREG_DS);
1393 seg_setup(VCPU_SREG_ES);
1394 seg_setup(VCPU_SREG_FS);
1395 seg_setup(VCPU_SREG_GS);
1396 seg_setup(VCPU_SREG_SS);
1397
1398 vmcs_write16(GUEST_TR_SELECTOR, 0);
1399 vmcs_writel(GUEST_TR_BASE, 0);
1400 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
1401 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1402
1403 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
1404 vmcs_writel(GUEST_LDTR_BASE, 0);
1405 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
1406 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
1407
1408 vmcs_write32(GUEST_SYSENTER_CS, 0);
1409 vmcs_writel(GUEST_SYSENTER_ESP, 0);
1410 vmcs_writel(GUEST_SYSENTER_EIP, 0);
1411
1412 vmcs_writel(GUEST_RFLAGS, 0x02);
1413 vmcs_writel(GUEST_RIP, 0xfff0);
1414 vmcs_writel(GUEST_RSP, 0);
1415
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416 //todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0
1417 vmcs_writel(GUEST_DR7, 0x400);
1418
1419 vmcs_writel(GUEST_GDTR_BASE, 0);
1420 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
1421
1422 vmcs_writel(GUEST_IDTR_BASE, 0);
1423 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
1424
1425 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
1426 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
1427 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
1428
1429 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03001430 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
1431 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432
1433 guest_write_tsc(0);
1434
1435 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
1436
1437 /* Special registers */
1438 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
1439
1440 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001441 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
1442 vmcs_config.pin_based_exec_ctrl);
1443 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
1444 vmcs_config.cpu_based_exec_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001445
Avi Kivity6aa8b732006-12-10 02:21:36 -08001446 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
1447 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
1448 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
1449
1450 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
1451 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
1452 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1453
1454 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
1455 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1456 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1457 vmcs_write16(HOST_FS_SELECTOR, read_fs()); /* 22.2.4 */
1458 vmcs_write16(HOST_GS_SELECTOR, read_gs()); /* 22.2.4 */
1459 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001460#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001461 rdmsrl(MSR_FS_BASE, a);
1462 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
1463 rdmsrl(MSR_GS_BASE, a);
1464 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
1465#else
1466 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
1467 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
1468#endif
1469
1470 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
1471
1472 get_idt(&dt);
1473 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
1474
Avi Kivitycd2276a2007-05-14 20:41:13 +03001475 asm ("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
1476 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03001477 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
1478 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
1479 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480
1481 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
1482 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
1483 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
1484 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
1485 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
1486 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
1487
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488 for (i = 0; i < NR_VMX_MSR; ++i) {
1489 u32 index = vmx_msr_index[i];
1490 u32 data_low, data_high;
1491 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001492 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001493
1494 if (rdmsr_safe(index, &data_low, &data_high) < 0)
1495 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08001496 if (wrmsr_safe(index, data_low, data_high) < 0)
1497 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001499 vmx->host_msrs[j].index = index;
1500 vmx->host_msrs[j].reserved = 0;
1501 vmx->host_msrs[j].data = data;
1502 vmx->guest_msrs[j] = vmx->host_msrs[j];
1503 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505
Rusty Russell8b9cf982007-07-30 16:31:43 +10001506 setup_msrs(vmx);
Avi Kivitye38aea32007-04-19 13:22:48 +03001507
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001508 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001509
1510 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001511 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
1512
Avi Kivity6aa8b732006-12-10 02:21:36 -08001513 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
1514
Michael Riepe3b99ab22006-12-13 00:34:15 -08001515#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516 vmcs_writel(VIRTUAL_APIC_PAGE_ADDR, 0);
1517 vmcs_writel(TPR_THRESHOLD, 0);
Michael Riepe3b99ab22006-12-13 00:34:15 -08001518#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519
Anthony Liguori25c4c272007-04-27 09:29:21 +03001520 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
1522
Rusty Russell8b9cf982007-07-30 16:31:43 +10001523 vmx->vcpu.cr0 = 0x60000010;
1524 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.cr0); // enter rmode
1525 vmx_set_cr4(&vmx->vcpu, 0);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001526#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001527 vmx_set_efer(&vmx->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +10001529 vmx_fpu_activate(&vmx->vcpu);
1530 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001531
1532 return 0;
1533
Avi Kivity6aa8b732006-12-10 02:21:36 -08001534out:
1535 return ret;
1536}
1537
1538static void inject_rmode_irq(struct kvm_vcpu *vcpu, int irq)
1539{
1540 u16 ent[2];
1541 u16 cs;
1542 u16 ip;
1543 unsigned long flags;
1544 unsigned long ss_base = vmcs_readl(GUEST_SS_BASE);
1545 u16 sp = vmcs_readl(GUEST_RSP);
1546 u32 ss_limit = vmcs_read32(GUEST_SS_LIMIT);
1547
Eric Sesterhenn / Snakebyte39649942007-04-09 16:15:05 +02001548 if (sp > ss_limit || sp < 6 ) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001549 vcpu_printf(vcpu, "%s: #SS, rsp 0x%lx ss 0x%lx limit 0x%x\n",
1550 __FUNCTION__,
1551 vmcs_readl(GUEST_RSP),
1552 vmcs_readl(GUEST_SS_BASE),
1553 vmcs_read32(GUEST_SS_LIMIT));
1554 return;
1555 }
1556
Laurent Viviere7d5d762007-07-30 13:41:19 +03001557 if (emulator_read_std(irq * sizeof(ent), &ent, sizeof(ent), vcpu) !=
1558 X86EMUL_CONTINUE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001559 vcpu_printf(vcpu, "%s: read guest err\n", __FUNCTION__);
1560 return;
1561 }
1562
1563 flags = vmcs_readl(GUEST_RFLAGS);
1564 cs = vmcs_readl(GUEST_CS_BASE) >> 4;
1565 ip = vmcs_readl(GUEST_RIP);
1566
1567
Laurent Viviere7d5d762007-07-30 13:41:19 +03001568 if (emulator_write_emulated(ss_base + sp - 2, &flags, 2, vcpu) != X86EMUL_CONTINUE ||
1569 emulator_write_emulated(ss_base + sp - 4, &cs, 2, vcpu) != X86EMUL_CONTINUE ||
1570 emulator_write_emulated(ss_base + sp - 6, &ip, 2, vcpu) != X86EMUL_CONTINUE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571 vcpu_printf(vcpu, "%s: write guest err\n", __FUNCTION__);
1572 return;
1573 }
1574
1575 vmcs_writel(GUEST_RFLAGS, flags &
1576 ~( X86_EFLAGS_IF | X86_EFLAGS_AC | X86_EFLAGS_TF));
1577 vmcs_write16(GUEST_CS_SELECTOR, ent[1]) ;
1578 vmcs_writel(GUEST_CS_BASE, ent[1] << 4);
1579 vmcs_writel(GUEST_RIP, ent[0]);
1580 vmcs_writel(GUEST_RSP, (vmcs_readl(GUEST_RSP) & ~0xffff) | (sp - 6));
1581}
1582
1583static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
1584{
1585 int word_index = __ffs(vcpu->irq_summary);
1586 int bit_index = __ffs(vcpu->irq_pending[word_index]);
1587 int irq = word_index * BITS_PER_LONG + bit_index;
1588
1589 clear_bit(bit_index, &vcpu->irq_pending[word_index]);
1590 if (!vcpu->irq_pending[word_index])
1591 clear_bit(word_index, &vcpu->irq_summary);
1592
1593 if (vcpu->rmode.active) {
1594 inject_rmode_irq(vcpu, irq);
1595 return;
1596 }
1597 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
1598 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1599}
1600
Dor Laorc1150d82007-01-05 16:36:24 -08001601
1602static void do_interrupt_requests(struct kvm_vcpu *vcpu,
1603 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001604{
Dor Laorc1150d82007-01-05 16:36:24 -08001605 u32 cpu_based_vm_exec_control;
1606
1607 vcpu->interrupt_window_open =
1608 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
1609 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
1610
1611 if (vcpu->interrupt_window_open &&
1612 vcpu->irq_summary &&
1613 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614 /*
Dor Laorc1150d82007-01-05 16:36:24 -08001615 * If interrupts enabled, and not blocked by sti or mov ss. Good.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616 */
1617 kvm_do_inject_irq(vcpu);
Dor Laorc1150d82007-01-05 16:36:24 -08001618
1619 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
1620 if (!vcpu->interrupt_window_open &&
1621 (vcpu->irq_summary || kvm_run->request_interrupt_window))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622 /*
1623 * Interrupts blocked. Wait for unblock.
1624 */
Dor Laorc1150d82007-01-05 16:36:24 -08001625 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
1626 else
1627 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
1628 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629}
1630
1631static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
1632{
1633 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
1634
1635 set_debugreg(dbg->bp[0], 0);
1636 set_debugreg(dbg->bp[1], 1);
1637 set_debugreg(dbg->bp[2], 2);
1638 set_debugreg(dbg->bp[3], 3);
1639
1640 if (dbg->singlestep) {
1641 unsigned long flags;
1642
1643 flags = vmcs_readl(GUEST_RFLAGS);
1644 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1645 vmcs_writel(GUEST_RFLAGS, flags);
1646 }
1647}
1648
1649static int handle_rmode_exception(struct kvm_vcpu *vcpu,
1650 int vec, u32 err_code)
1651{
1652 if (!vcpu->rmode.active)
1653 return 0;
1654
Nitin A Kambleb3f37702007-05-17 15:50:34 +03001655 /*
1656 * Instruction with address size override prefix opcode 0x67
1657 * Cause the #SS fault with 0 error code in VM86 mode.
1658 */
1659 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660 if (emulate_instruction(vcpu, NULL, 0, 0) == EMULATE_DONE)
1661 return 1;
1662 return 0;
1663}
1664
1665static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1666{
1667 u32 intr_info, error_code;
1668 unsigned long cr2, rip;
1669 u32 vect_info;
1670 enum emulation_result er;
Avi Kivitye2dec932007-01-05 16:36:54 -08001671 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001672
1673 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
1674 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
1675
1676 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
1677 !is_page_fault(intr_info)) {
1678 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
1679 "intr info 0x%x\n", __FUNCTION__, vect_info, intr_info);
1680 }
1681
1682 if (is_external_interrupt(vect_info)) {
1683 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
1684 set_bit(irq, vcpu->irq_pending);
1685 set_bit(irq / BITS_PER_LONG, &vcpu->irq_summary);
1686 }
1687
1688 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) { /* nmi */
1689 asm ("int $2");
1690 return 1;
1691 }
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001692
1693 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001694 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001695 return 1;
1696 }
1697
Avi Kivity6aa8b732006-12-10 02:21:36 -08001698 error_code = 0;
1699 rip = vmcs_readl(GUEST_RIP);
1700 if (intr_info & INTR_INFO_DELIEVER_CODE_MASK)
1701 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
1702 if (is_page_fault(intr_info)) {
1703 cr2 = vmcs_readl(EXIT_QUALIFICATION);
1704
Shaohua Li11ec2802007-07-23 14:51:37 +08001705 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001706 r = kvm_mmu_page_fault(vcpu, cr2, error_code);
1707 if (r < 0) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001708 mutex_unlock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001709 return r;
1710 }
1711 if (!r) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001712 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001713 return 1;
1714 }
1715
1716 er = emulate_instruction(vcpu, kvm_run, cr2, error_code);
Shaohua Li11ec2802007-07-23 14:51:37 +08001717 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001718
1719 switch (er) {
1720 case EMULATE_DONE:
1721 return 1;
1722 case EMULATE_DO_MMIO:
Avi Kivity1165f5f2007-04-19 17:27:43 +03001723 ++vcpu->stat.mmio_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001724 return 0;
1725 case EMULATE_FAIL:
1726 vcpu_printf(vcpu, "%s: emulate fail\n", __FUNCTION__);
1727 break;
1728 default:
1729 BUG();
1730 }
1731 }
1732
1733 if (vcpu->rmode.active &&
1734 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03001735 error_code)) {
1736 if (vcpu->halt_request) {
1737 vcpu->halt_request = 0;
1738 return kvm_emulate_halt(vcpu);
1739 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001740 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03001741 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001742
1743 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) == (INTR_TYPE_EXCEPTION | 1)) {
1744 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1745 return 0;
1746 }
1747 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
1748 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
1749 kvm_run->ex.error_code = error_code;
1750 return 0;
1751}
1752
1753static int handle_external_interrupt(struct kvm_vcpu *vcpu,
1754 struct kvm_run *kvm_run)
1755{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001756 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001757 return 1;
1758}
1759
Avi Kivity988ad742007-02-12 00:54:36 -08001760static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1761{
1762 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1763 return 0;
1764}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001765
Avi Kivity039576c2007-03-20 12:46:50 +02001766static int get_io_count(struct kvm_vcpu *vcpu, unsigned long *count)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001767{
1768 u64 inst;
1769 gva_t rip;
1770 int countr_size;
Laurent Viviere7d5d762007-07-30 13:41:19 +03001771 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001772
1773 if ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_VM)) {
1774 countr_size = 2;
1775 } else {
1776 u32 cs_ar = vmcs_read32(GUEST_CS_AR_BYTES);
1777
1778 countr_size = (cs_ar & AR_L_MASK) ? 8:
1779 (cs_ar & AR_DB_MASK) ? 4: 2;
1780 }
1781
1782 rip = vmcs_readl(GUEST_RIP);
1783 if (countr_size != 8)
1784 rip += vmcs_readl(GUEST_CS_BASE);
1785
Laurent Viviere7d5d762007-07-30 13:41:19 +03001786 if (emulator_read_std(rip, &inst, sizeof(inst), vcpu) !=
1787 X86EMUL_CONTINUE)
1788 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001789
Laurent Viviere7d5d762007-07-30 13:41:19 +03001790 for (i = 0; i < sizeof(inst); i++) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001791 switch (((u8*)&inst)[i]) {
1792 case 0xf0:
1793 case 0xf2:
1794 case 0xf3:
1795 case 0x2e:
1796 case 0x36:
1797 case 0x3e:
1798 case 0x26:
1799 case 0x64:
1800 case 0x65:
1801 case 0x66:
1802 break;
1803 case 0x67:
1804 countr_size = (countr_size == 2) ? 4: (countr_size >> 1);
1805 default:
1806 goto done;
1807 }
1808 }
1809 return 0;
1810done:
1811 countr_size *= 8;
1812 *count = vcpu->regs[VCPU_REGS_RCX] & (~0ULL >> (64 - countr_size));
Avi Kivity039576c2007-03-20 12:46:50 +02001813 //printk("cx: %lx\n", vcpu->regs[VCPU_REGS_RCX]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001814 return 1;
1815}
1816
1817static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1818{
1819 u64 exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02001820 int size, down, in, string, rep;
1821 unsigned port;
1822 unsigned long count;
1823 gva_t address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001824
Avi Kivity1165f5f2007-04-19 17:27:43 +03001825 ++vcpu->stat.io_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001826 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02001827 in = (exit_qualification & 8) != 0;
1828 size = (exit_qualification & 7) + 1;
1829 string = (exit_qualification & 16) != 0;
1830 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
1831 count = 1;
1832 rep = (exit_qualification & 32) != 0;
1833 port = exit_qualification >> 16;
1834 address = 0;
1835 if (string) {
1836 if (rep && !get_io_count(vcpu, &count))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001837 return 1;
Avi Kivity039576c2007-03-20 12:46:50 +02001838 address = vmcs_readl(GUEST_LINEAR_ADDRESS);
1839 }
1840 return kvm_setup_pio(vcpu, kvm_run, in, size, count, string, down,
1841 address, rep, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001842}
1843
Ingo Molnar102d8322007-02-19 14:37:47 +02001844static void
1845vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
1846{
1847 /*
1848 * Patch in the VMCALL instruction:
1849 */
1850 hypercall[0] = 0x0f;
1851 hypercall[1] = 0x01;
1852 hypercall[2] = 0xc1;
1853 hypercall[3] = 0xc3;
1854}
1855
Avi Kivity6aa8b732006-12-10 02:21:36 -08001856static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1857{
1858 u64 exit_qualification;
1859 int cr;
1860 int reg;
1861
1862 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
1863 cr = exit_qualification & 15;
1864 reg = (exit_qualification >> 8) & 15;
1865 switch ((exit_qualification >> 4) & 3) {
1866 case 0: /* mov to cr */
1867 switch (cr) {
1868 case 0:
1869 vcpu_load_rsp_rip(vcpu);
1870 set_cr0(vcpu, vcpu->regs[reg]);
1871 skip_emulated_instruction(vcpu);
1872 return 1;
1873 case 3:
1874 vcpu_load_rsp_rip(vcpu);
1875 set_cr3(vcpu, vcpu->regs[reg]);
1876 skip_emulated_instruction(vcpu);
1877 return 1;
1878 case 4:
1879 vcpu_load_rsp_rip(vcpu);
1880 set_cr4(vcpu, vcpu->regs[reg]);
1881 skip_emulated_instruction(vcpu);
1882 return 1;
1883 case 8:
1884 vcpu_load_rsp_rip(vcpu);
1885 set_cr8(vcpu, vcpu->regs[reg]);
1886 skip_emulated_instruction(vcpu);
1887 return 1;
1888 };
1889 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03001890 case 2: /* clts */
1891 vcpu_load_rsp_rip(vcpu);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001892 vmx_fpu_deactivate(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001893 vcpu->cr0 &= ~X86_CR0_TS;
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001894 vmcs_writel(CR0_READ_SHADOW, vcpu->cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001895 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03001896 skip_emulated_instruction(vcpu);
1897 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001898 case 1: /*mov from cr*/
1899 switch (cr) {
1900 case 3:
1901 vcpu_load_rsp_rip(vcpu);
1902 vcpu->regs[reg] = vcpu->cr3;
1903 vcpu_put_rsp_rip(vcpu);
1904 skip_emulated_instruction(vcpu);
1905 return 1;
1906 case 8:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001907 vcpu_load_rsp_rip(vcpu);
1908 vcpu->regs[reg] = vcpu->cr8;
1909 vcpu_put_rsp_rip(vcpu);
1910 skip_emulated_instruction(vcpu);
1911 return 1;
1912 }
1913 break;
1914 case 3: /* lmsw */
1915 lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
1916
1917 skip_emulated_instruction(vcpu);
1918 return 1;
1919 default:
1920 break;
1921 }
1922 kvm_run->exit_reason = 0;
1923 printk(KERN_ERR "kvm: unhandled control register: op %d cr %d\n",
1924 (int)(exit_qualification >> 4) & 3, cr);
1925 return 0;
1926}
1927
1928static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1929{
1930 u64 exit_qualification;
1931 unsigned long val;
1932 int dr, reg;
1933
1934 /*
1935 * FIXME: this code assumes the host is debugging the guest.
1936 * need to deal with guest debugging itself too.
1937 */
1938 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
1939 dr = exit_qualification & 7;
1940 reg = (exit_qualification >> 8) & 15;
1941 vcpu_load_rsp_rip(vcpu);
1942 if (exit_qualification & 16) {
1943 /* mov from dr */
1944 switch (dr) {
1945 case 6:
1946 val = 0xffff0ff0;
1947 break;
1948 case 7:
1949 val = 0x400;
1950 break;
1951 default:
1952 val = 0;
1953 }
1954 vcpu->regs[reg] = val;
1955 } else {
1956 /* mov to dr */
1957 }
1958 vcpu_put_rsp_rip(vcpu);
1959 skip_emulated_instruction(vcpu);
1960 return 1;
1961}
1962
1963static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1964{
Avi Kivity06465c52007-02-28 20:46:53 +02001965 kvm_emulate_cpuid(vcpu);
1966 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001967}
1968
1969static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1970{
1971 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
1972 u64 data;
1973
1974 if (vmx_get_msr(vcpu, ecx, &data)) {
1975 vmx_inject_gp(vcpu, 0);
1976 return 1;
1977 }
1978
1979 /* FIXME: handling of bits 32:63 of rax, rdx */
1980 vcpu->regs[VCPU_REGS_RAX] = data & -1u;
1981 vcpu->regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
1982 skip_emulated_instruction(vcpu);
1983 return 1;
1984}
1985
1986static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1987{
1988 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
1989 u64 data = (vcpu->regs[VCPU_REGS_RAX] & -1u)
1990 | ((u64)(vcpu->regs[VCPU_REGS_RDX] & -1u) << 32);
1991
1992 if (vmx_set_msr(vcpu, ecx, data) != 0) {
1993 vmx_inject_gp(vcpu, 0);
1994 return 1;
1995 }
1996
1997 skip_emulated_instruction(vcpu);
1998 return 1;
1999}
2000
Dor Laorc1150d82007-01-05 16:36:24 -08002001static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2002 struct kvm_run *kvm_run)
2003{
2004 kvm_run->if_flag = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) != 0;
2005 kvm_run->cr8 = vcpu->cr8;
2006 kvm_run->apic_base = vcpu->apic_base;
2007 kvm_run->ready_for_interrupt_injection = (vcpu->interrupt_window_open &&
2008 vcpu->irq_summary == 0);
2009}
2010
Avi Kivity6aa8b732006-12-10 02:21:36 -08002011static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2012 struct kvm_run *kvm_run)
2013{
Dor Laorc1150d82007-01-05 16:36:24 -08002014 /*
2015 * If the user space waits to inject interrupts, exit as soon as
2016 * possible
2017 */
2018 if (kvm_run->request_interrupt_window &&
Dor Laor022a9302007-01-05 16:37:00 -08002019 !vcpu->irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002020 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002021 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002022 return 0;
2023 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002024 return 1;
2025}
2026
2027static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2028{
2029 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002030 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002031}
2032
Ingo Molnarc21415e2007-02-19 14:37:47 +02002033static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2034{
Dor Laor510043d2007-02-19 18:25:43 +02002035 skip_emulated_instruction(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02002036 return kvm_hypercall(vcpu, kvm_run);
Ingo Molnarc21415e2007-02-19 14:37:47 +02002037}
2038
Avi Kivity6aa8b732006-12-10 02:21:36 -08002039/*
2040 * The exit handlers return 1 if the exit was handled fully and guest execution
2041 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2042 * to be done to userspace and return 0.
2043 */
2044static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
2045 struct kvm_run *kvm_run) = {
2046 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
2047 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08002048 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002049 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002050 [EXIT_REASON_CR_ACCESS] = handle_cr,
2051 [EXIT_REASON_DR_ACCESS] = handle_dr,
2052 [EXIT_REASON_CPUID] = handle_cpuid,
2053 [EXIT_REASON_MSR_READ] = handle_rdmsr,
2054 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
2055 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
2056 [EXIT_REASON_HLT] = handle_halt,
Ingo Molnarc21415e2007-02-19 14:37:47 +02002057 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002058};
2059
2060static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04002061 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002062
2063/*
2064 * The guest has exited. See if we can fix it or if we need userspace
2065 * assistance.
2066 */
2067static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2068{
2069 u32 vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2070 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
2071
2072 if ( (vectoring_info & VECTORING_INFO_VALID_MASK) &&
2073 exit_reason != EXIT_REASON_EXCEPTION_NMI )
2074 printk(KERN_WARNING "%s: unexpected, valid vectoring info and "
2075 "exit reason is 0x%x\n", __FUNCTION__, exit_reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002076 if (exit_reason < kvm_vmx_max_exit_handlers
2077 && kvm_vmx_exit_handlers[exit_reason])
2078 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
2079 else {
2080 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2081 kvm_run->hw.hardware_exit_reason = exit_reason;
2082 }
2083 return 0;
2084}
2085
Dor Laorc1150d82007-01-05 16:36:24 -08002086/*
2087 * Check if userspace requested an interrupt window, and that the
2088 * interrupt window is open.
2089 *
2090 * No need to exit to userspace if we already have an interrupt queued.
2091 */
2092static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2093 struct kvm_run *kvm_run)
2094{
2095 return (!vcpu->irq_summary &&
2096 kvm_run->request_interrupt_window &&
2097 vcpu->interrupt_window_open &&
2098 (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF));
2099}
2100
Avi Kivityd9e368d2007-06-07 19:18:30 +03002101static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
2102{
Avi Kivityd9e368d2007-06-07 19:18:30 +03002103}
2104
Avi Kivity6aa8b732006-12-10 02:21:36 -08002105static int vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2106{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002107 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002108 u8 fail;
Avi Kivitye2dec932007-01-05 16:36:54 -08002109 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110
Avi Kivitye6adf282007-04-30 16:07:54 +03002111preempted:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112 if (vcpu->guest_debug.enabled)
2113 kvm_guest_debug_pre(vcpu);
2114
Avi Kivitye6adf282007-04-30 16:07:54 +03002115again:
Shaohua Li9ae04482007-07-23 14:51:32 +08002116 r = kvm_mmu_reload(vcpu);
2117 if (unlikely(r))
2118 goto out;
2119
Avi Kivity15ad7142007-07-11 18:17:21 +03002120 preempt_disable();
2121
Gregory Haskinsff1dc792007-05-31 14:08:58 -04002122 if (!vcpu->mmio_read_completed)
2123 do_interrupt_requests(vcpu, kvm_run);
2124
Rusty Russell8b9cf982007-07-30 16:31:43 +10002125 vmx_save_host_state(vmx);
Avi Kivitye6adf282007-04-30 16:07:54 +03002126 kvm_load_guest_fpu(vcpu);
2127
2128 /*
2129 * Loading guest fpu may have cleared host cr0.ts
2130 */
2131 vmcs_writel(HOST_CR0, read_cr0());
2132
Avi Kivityd9e368d2007-06-07 19:18:30 +03002133 local_irq_disable();
2134
2135 vcpu->guest_mode = 1;
2136 if (vcpu->requests)
2137 if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
2138 vmx_flush_tlb(vcpu);
2139
Avi Kivity6aa8b732006-12-10 02:21:36 -08002140 asm (
2141 /* Store host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002142#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143 "push %%rax; push %%rbx; push %%rdx;"
2144 "push %%rsi; push %%rdi; push %%rbp;"
2145 "push %%r8; push %%r9; push %%r10; push %%r11;"
2146 "push %%r12; push %%r13; push %%r14; push %%r15;"
2147 "push %%rcx \n\t"
2148 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
2149#else
2150 "pusha; push %%ecx \n\t"
2151 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
2152#endif
2153 /* Check if vmlaunch of vmresume is needed */
2154 "cmp $0, %1 \n\t"
2155 /* Load guest registers. Don't clobber flags. */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002156#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002157 "mov %c[cr2](%3), %%rax \n\t"
2158 "mov %%rax, %%cr2 \n\t"
2159 "mov %c[rax](%3), %%rax \n\t"
2160 "mov %c[rbx](%3), %%rbx \n\t"
2161 "mov %c[rdx](%3), %%rdx \n\t"
2162 "mov %c[rsi](%3), %%rsi \n\t"
2163 "mov %c[rdi](%3), %%rdi \n\t"
2164 "mov %c[rbp](%3), %%rbp \n\t"
2165 "mov %c[r8](%3), %%r8 \n\t"
2166 "mov %c[r9](%3), %%r9 \n\t"
2167 "mov %c[r10](%3), %%r10 \n\t"
2168 "mov %c[r11](%3), %%r11 \n\t"
2169 "mov %c[r12](%3), %%r12 \n\t"
2170 "mov %c[r13](%3), %%r13 \n\t"
2171 "mov %c[r14](%3), %%r14 \n\t"
2172 "mov %c[r15](%3), %%r15 \n\t"
2173 "mov %c[rcx](%3), %%rcx \n\t" /* kills %3 (rcx) */
2174#else
2175 "mov %c[cr2](%3), %%eax \n\t"
2176 "mov %%eax, %%cr2 \n\t"
2177 "mov %c[rax](%3), %%eax \n\t"
2178 "mov %c[rbx](%3), %%ebx \n\t"
2179 "mov %c[rdx](%3), %%edx \n\t"
2180 "mov %c[rsi](%3), %%esi \n\t"
2181 "mov %c[rdi](%3), %%edi \n\t"
2182 "mov %c[rbp](%3), %%ebp \n\t"
2183 "mov %c[rcx](%3), %%ecx \n\t" /* kills %3 (ecx) */
2184#endif
2185 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03002186 "jne .Llaunched \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002187 ASM_VMX_VMLAUNCH "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03002188 "jmp .Lkvm_vmx_return \n\t"
2189 ".Llaunched: " ASM_VMX_VMRESUME "\n\t"
2190 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191 /* Save guest registers, load host registers, keep flags */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002192#ifdef CONFIG_X86_64
Ingo Molnar96958232007-02-12 00:54:33 -08002193 "xchg %3, (%%rsp) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002194 "mov %%rax, %c[rax](%3) \n\t"
2195 "mov %%rbx, %c[rbx](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002196 "pushq (%%rsp); popq %c[rcx](%3) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002197 "mov %%rdx, %c[rdx](%3) \n\t"
2198 "mov %%rsi, %c[rsi](%3) \n\t"
2199 "mov %%rdi, %c[rdi](%3) \n\t"
2200 "mov %%rbp, %c[rbp](%3) \n\t"
2201 "mov %%r8, %c[r8](%3) \n\t"
2202 "mov %%r9, %c[r9](%3) \n\t"
2203 "mov %%r10, %c[r10](%3) \n\t"
2204 "mov %%r11, %c[r11](%3) \n\t"
2205 "mov %%r12, %c[r12](%3) \n\t"
2206 "mov %%r13, %c[r13](%3) \n\t"
2207 "mov %%r14, %c[r14](%3) \n\t"
2208 "mov %%r15, %c[r15](%3) \n\t"
2209 "mov %%cr2, %%rax \n\t"
2210 "mov %%rax, %c[cr2](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002211 "mov (%%rsp), %3 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002212
2213 "pop %%rcx; pop %%r15; pop %%r14; pop %%r13; pop %%r12;"
2214 "pop %%r11; pop %%r10; pop %%r9; pop %%r8;"
2215 "pop %%rbp; pop %%rdi; pop %%rsi;"
2216 "pop %%rdx; pop %%rbx; pop %%rax \n\t"
2217#else
Ingo Molnar96958232007-02-12 00:54:33 -08002218 "xchg %3, (%%esp) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002219 "mov %%eax, %c[rax](%3) \n\t"
2220 "mov %%ebx, %c[rbx](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002221 "pushl (%%esp); popl %c[rcx](%3) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002222 "mov %%edx, %c[rdx](%3) \n\t"
2223 "mov %%esi, %c[rsi](%3) \n\t"
2224 "mov %%edi, %c[rdi](%3) \n\t"
2225 "mov %%ebp, %c[rbp](%3) \n\t"
2226 "mov %%cr2, %%eax \n\t"
2227 "mov %%eax, %c[cr2](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002228 "mov (%%esp), %3 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002229
2230 "pop %%ecx; popa \n\t"
2231#endif
2232 "setbe %0 \n\t"
Herbert Xue0015482007-01-23 14:10:00 +11002233 : "=q" (fail)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002234 : "r"(vmx->launched), "d"((unsigned long)HOST_RSP),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002235 "c"(vcpu),
2236 [rax]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RAX])),
2237 [rbx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBX])),
2238 [rcx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RCX])),
2239 [rdx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDX])),
2240 [rsi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RSI])),
2241 [rdi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDI])),
2242 [rbp]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002243#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244 [r8 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R8 ])),
2245 [r9 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R9 ])),
2246 [r10]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R10])),
2247 [r11]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R11])),
2248 [r12]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R12])),
2249 [r13]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R13])),
2250 [r14]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R14])),
2251 [r15]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R15])),
2252#endif
2253 [cr2]"i"(offsetof(struct kvm_vcpu, cr2))
2254 : "cc", "memory" );
2255
Avi Kivityd9e368d2007-06-07 19:18:30 +03002256 vcpu->guest_mode = 0;
2257 local_irq_enable();
2258
Avi Kivity1165f5f2007-04-19 17:27:43 +03002259 ++vcpu->stat.exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260
Dor Laorc1150d82007-01-05 16:36:24 -08002261 vcpu->interrupt_window_open = (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262
Avi Kivity6aa8b732006-12-10 02:21:36 -08002263 asm ("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03002264 vmx->launched = 1;
2265
2266 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267
Avi Kivity05e0c8c2007-04-30 16:15:58 +03002268 if (unlikely(fail)) {
Avi Kivity8eb7d332007-03-04 14:17:08 +02002269 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2270 kvm_run->fail_entry.hardware_entry_failure_reason
2271 = vmcs_read32(VM_INSTRUCTION_ERROR);
Avi Kivitye2dec932007-01-05 16:36:54 -08002272 r = 0;
Avi Kivity05e0c8c2007-04-30 16:15:58 +03002273 goto out;
2274 }
2275 /*
2276 * Profile KVM exit RIPs:
2277 */
2278 if (unlikely(prof_on == KVM_PROFILING))
2279 profile_hit(KVM_PROFILING, (void *)vmcs_readl(GUEST_RIP));
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +01002280
Avi Kivity05e0c8c2007-04-30 16:15:58 +03002281 r = kvm_handle_exit(kvm_run, vcpu);
2282 if (r > 0) {
2283 /* Give scheduler a change to reschedule. */
2284 if (signal_pending(current)) {
2285 r = -EINTR;
2286 kvm_run->exit_reason = KVM_EXIT_INTR;
2287 ++vcpu->stat.signal_exits;
2288 goto out;
2289 }
Dor Laorc1150d82007-01-05 16:36:24 -08002290
Avi Kivity05e0c8c2007-04-30 16:15:58 +03002291 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2292 r = -EINTR;
2293 kvm_run->exit_reason = KVM_EXIT_INTR;
2294 ++vcpu->stat.request_irq_exits;
2295 goto out;
2296 }
2297 if (!need_resched()) {
2298 ++vcpu->stat.light_exits;
2299 goto again;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002300 }
2301 }
Dor Laorc1150d82007-01-05 16:36:24 -08002302
Avi Kivitye6adf282007-04-30 16:07:54 +03002303out:
Avi Kivitye6adf282007-04-30 16:07:54 +03002304 if (r > 0) {
2305 kvm_resched(vcpu);
2306 goto preempted;
2307 }
2308
Dor Laorc1150d82007-01-05 16:36:24 -08002309 post_kvm_run_save(vcpu, kvm_run);
Avi Kivitye2dec932007-01-05 16:36:54 -08002310 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002311}
2312
Avi Kivity6aa8b732006-12-10 02:21:36 -08002313static void vmx_inject_page_fault(struct kvm_vcpu *vcpu,
2314 unsigned long addr,
2315 u32 err_code)
2316{
2317 u32 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2318
Avi Kivity1165f5f2007-04-19 17:27:43 +03002319 ++vcpu->stat.pf_guest;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002320
2321 if (is_page_fault(vect_info)) {
2322 printk(KERN_DEBUG "inject_page_fault: "
2323 "double fault 0x%lx @ 0x%lx\n",
2324 addr, vmcs_readl(GUEST_RIP));
2325 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
2326 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2327 DF_VECTOR |
2328 INTR_TYPE_EXCEPTION |
2329 INTR_INFO_DELIEVER_CODE_MASK |
2330 INTR_INFO_VALID_MASK);
2331 return;
2332 }
2333 vcpu->cr2 = addr;
2334 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, err_code);
2335 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2336 PF_VECTOR |
2337 INTR_TYPE_EXCEPTION |
2338 INTR_INFO_DELIEVER_CODE_MASK |
2339 INTR_INFO_VALID_MASK);
2340
2341}
2342
2343static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
2344{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002345 struct vcpu_vmx *vmx = to_vmx(vcpu);
2346
2347 if (vmx->vmcs) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002348 on_each_cpu(__vcpu_clear, vmx, 0, 1);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002349 free_vmcs(vmx->vmcs);
2350 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351 }
2352}
2353
2354static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
2355{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002356 struct vcpu_vmx *vmx = to_vmx(vcpu);
2357
Avi Kivity6aa8b732006-12-10 02:21:36 -08002358 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002359 kfree(vmx->host_msrs);
2360 kfree(vmx->guest_msrs);
2361 kvm_vcpu_uninit(vcpu);
2362 kfree(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363}
2364
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002365static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002367 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10002368 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03002369 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002371 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002372 return ERR_PTR(-ENOMEM);
2373
2374 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
2375 if (err)
2376 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002377
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002378 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002379 if (!vmx->guest_msrs) {
2380 err = -ENOMEM;
2381 goto uninit_vcpu;
2382 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08002383
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002384 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
2385 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002386 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002387
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002388 vmx->vmcs = alloc_vmcs();
2389 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002390 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002391
2392 vmcs_clear(vmx->vmcs);
2393
Avi Kivity15ad7142007-07-11 18:17:21 +03002394 cpu = get_cpu();
2395 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002396 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002397 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002398 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002399 if (err)
2400 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002401
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002402 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002403
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002404free_vmcs:
2405 free_vmcs(vmx->vmcs);
2406free_msrs:
2407 kfree(vmx->host_msrs);
2408free_guest_msrs:
2409 kfree(vmx->guest_msrs);
2410uninit_vcpu:
2411 kvm_vcpu_uninit(&vmx->vcpu);
2412free_vcpu:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002413 kfree(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002414 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002415}
2416
2417static struct kvm_arch_ops vmx_arch_ops = {
2418 .cpu_has_kvm_support = cpu_has_kvm_support,
2419 .disabled_by_bios = vmx_disabled_by_bios,
2420 .hardware_setup = hardware_setup,
2421 .hardware_unsetup = hardware_unsetup,
2422 .hardware_enable = hardware_enable,
2423 .hardware_disable = hardware_disable,
2424
2425 .vcpu_create = vmx_create_vcpu,
2426 .vcpu_free = vmx_free_vcpu,
2427
2428 .vcpu_load = vmx_vcpu_load,
2429 .vcpu_put = vmx_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08002430 .vcpu_decache = vmx_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431
2432 .set_guest_debug = set_guest_debug,
2433 .get_msr = vmx_get_msr,
2434 .set_msr = vmx_set_msr,
2435 .get_segment_base = vmx_get_segment_base,
2436 .get_segment = vmx_get_segment,
2437 .set_segment = vmx_set_segment,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03002439 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002441 .set_cr3 = vmx_set_cr3,
2442 .set_cr4 = vmx_set_cr4,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002443#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444 .set_efer = vmx_set_efer,
2445#endif
2446 .get_idt = vmx_get_idt,
2447 .set_idt = vmx_set_idt,
2448 .get_gdt = vmx_get_gdt,
2449 .set_gdt = vmx_set_gdt,
2450 .cache_regs = vcpu_load_rsp_rip,
2451 .decache_regs = vcpu_put_rsp_rip,
2452 .get_rflags = vmx_get_rflags,
2453 .set_rflags = vmx_set_rflags,
2454
2455 .tlb_flush = vmx_flush_tlb,
2456 .inject_page_fault = vmx_inject_page_fault,
2457
2458 .inject_gp = vmx_inject_gp,
2459
2460 .run = vmx_vcpu_run,
2461 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02002462 .patch_hypercall = vmx_patch_hypercall,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463};
2464
2465static int __init vmx_init(void)
2466{
He, Qingfdef3ad2007-04-30 09:45:24 +03002467 void *iova;
2468 int r;
2469
2470 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2471 if (!vmx_io_bitmap_a)
2472 return -ENOMEM;
2473
2474 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2475 if (!vmx_io_bitmap_b) {
2476 r = -ENOMEM;
2477 goto out;
2478 }
2479
2480 /*
2481 * Allow direct access to the PC debug port (it is often used for I/O
2482 * delays, but the vmexits simply slow things down).
2483 */
2484 iova = kmap(vmx_io_bitmap_a);
2485 memset(iova, 0xff, PAGE_SIZE);
2486 clear_bit(0x80, iova);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002487 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03002488
2489 iova = kmap(vmx_io_bitmap_b);
2490 memset(iova, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002491 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03002492
Rusty Russellc16f8622007-07-30 21:12:19 +10002493 r = kvm_init_arch(&vmx_arch_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03002494 if (r)
2495 goto out1;
2496
2497 return 0;
2498
2499out1:
2500 __free_page(vmx_io_bitmap_b);
2501out:
2502 __free_page(vmx_io_bitmap_a);
2503 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002504}
2505
2506static void __exit vmx_exit(void)
2507{
He, Qingfdef3ad2007-04-30 09:45:24 +03002508 __free_page(vmx_io_bitmap_b);
2509 __free_page(vmx_io_bitmap_a);
2510
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511 kvm_exit_arch();
2512}
2513
2514module_init(vmx_init)
2515module_exit(vmx_exit)