blob: 914dc4e9b37f622dcc7775f506005cbb96eb6c17 [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.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040021
Avi Kivityedf88412007-12-16 11:02:48 +020022#include <linux/kvm_host.h>
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>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040027#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020028#include <linux/moduleparam.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030029#include <linux/ftrace_event.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040031#include <linux/tboot.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030032#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030033#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040034
Avi Kivity6aa8b732006-12-10 02:21:36 -080035#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080036#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020037#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020038#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080039#include <asm/mce.h>
Dexuan Cui2acf9232010-06-10 11:27:12 +080040#include <asm/i387.h>
41#include <asm/xcr.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042
Marcelo Tosatti229456f2009-06-17 09:22:14 -030043#include "trace.h"
44
Avi Kivity4ecac3f2008-05-13 13:23:38 +030045#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040046#define __ex_clear(x, reg) \
47 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030048
Avi Kivity6aa8b732006-12-10 02:21:36 -080049MODULE_AUTHOR("Qumranet");
50MODULE_LICENSE("GPL");
51
Avi Kivity4462d212009-03-23 17:53:37 +020052static int __read_mostly bypass_guest_pf = 1;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020053module_param(bypass_guest_pf, bool, S_IRUGO);
Avi Kivityc7addb92007-09-16 18:58:32 +020054
Avi Kivity4462d212009-03-23 17:53:37 +020055static int __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020056module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080057
Avi Kivity4462d212009-03-23 17:53:37 +020058static int __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020059module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020060
Avi Kivity4462d212009-03-23 17:53:37 +020061static int __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020062module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080063
Nitin A Kamble3a624e22009-06-08 11:34:16 -070064static int __read_mostly enable_unrestricted_guest = 1;
65module_param_named(unrestricted_guest,
66 enable_unrestricted_guest, bool, S_IRUGO);
67
Avi Kivity4462d212009-03-23 17:53:37 +020068static int __read_mostly emulate_invalid_guest_state = 0;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020069module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030070
Dongxiao Xub923e622010-05-11 18:29:45 +080071static int __read_mostly vmm_exclusive = 1;
72module_param(vmm_exclusive, bool, S_IRUGO);
73
Anthony Liguori443381a2010-12-06 10:53:38 -060074static int __read_mostly yield_on_hlt = 1;
75module_param(yield_on_hlt, bool, S_IRUGO);
76
Nadav Har'El801d3422011-05-25 23:02:23 +030077/*
78 * If nested=1, nested virtualization is supported, i.e., guests may use
79 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
80 * use VMX instructions.
81 */
82static int __read_mostly nested = 0;
83module_param(nested, bool, S_IRUGO);
84
Avi Kivitycdc0e242009-12-06 17:21:14 +020085#define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \
86 (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD)
87#define KVM_GUEST_CR0_MASK \
88 (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
89#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \
Avi Kivity81231c62010-01-24 16:26:40 +020090 (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +020091#define KVM_VM_CR0_ALWAYS_ON \
92 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +020093#define KVM_CR4_GUEST_OWNED_BITS \
94 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
95 | X86_CR4_OSXMMEXCPT)
96
Avi Kivitycdc0e242009-12-06 17:21:14 +020097#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
98#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
99
Avi Kivity78ac8b42010-04-08 18:19:35 +0300100#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
101
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800102/*
103 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
104 * ple_gap: upper bound on the amount of time between two successive
105 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500106 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800107 * ple_window: upper bound on the amount of time a guest is allowed to execute
108 * in a PAUSE loop. Tests indicate that most spinlocks are held for
109 * less than 2^12 cycles
110 * Time is measured based on a counter that runs at the same rate as the TSC,
111 * refer SDM volume 3b section 21.6.13 & 22.1.3.
112 */
Rik van Riel00c25bc2011-01-04 09:51:33 -0500113#define KVM_VMX_DEFAULT_PLE_GAP 128
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800114#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
115static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
116module_param(ple_gap, int, S_IRUGO);
117
118static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
119module_param(ple_window, int, S_IRUGO);
120
Avi Kivity61d2ef22010-04-28 16:40:38 +0300121#define NR_AUTOLOAD_MSRS 1
122
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400123struct vmcs {
124 u32 revision_id;
125 u32 abort;
126 char data[0];
127};
128
Nadav Har'Eld462b812011-05-24 15:26:10 +0300129/*
130 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
131 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
132 * loaded on this CPU (so we can clear them if the CPU goes down).
133 */
134struct loaded_vmcs {
135 struct vmcs *vmcs;
136 int cpu;
137 int launched;
138 struct list_head loaded_vmcss_on_cpu_link;
139};
140
Avi Kivity26bb0982009-09-07 11:14:12 +0300141struct shared_msr_entry {
142 unsigned index;
143 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200144 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300145};
146
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300147/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300148 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
149 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
150 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
151 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
152 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
153 * More than one of these structures may exist, if L1 runs multiple L2 guests.
154 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
155 * underlying hardware which will be used to run L2.
156 * This structure is packed to ensure that its layout is identical across
157 * machines (necessary for live migration).
158 * If there are changes in this struct, VMCS12_REVISION must be changed.
159 */
160struct __packed vmcs12 {
161 /* According to the Intel spec, a VMCS region must start with the
162 * following two fields. Then follow implementation-specific data.
163 */
164 u32 revision_id;
165 u32 abort;
166};
167
168/*
169 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
170 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
171 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
172 */
173#define VMCS12_REVISION 0x11e57ed0
174
175/*
176 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
177 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
178 * current implementation, 4K are reserved to avoid future complications.
179 */
180#define VMCS12_SIZE 0x1000
181
182/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300183 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
184 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
185 */
186struct nested_vmx {
187 /* Has the level1 guest done vmxon? */
188 bool vmxon;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300189
190 /* The guest-physical address of the current VMCS L1 keeps for L2 */
191 gpa_t current_vmptr;
192 /* The host-usable pointer to the above */
193 struct page *current_vmcs12_page;
194 struct vmcs12 *current_vmcs12;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300195};
196
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400197struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000198 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300199 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300200 u8 fail;
Avi Kivity69c73022011-03-07 15:26:44 +0200201 u8 cpl;
Avi Kivity9d58b932011-03-07 16:52:07 +0200202 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300203 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200204 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200205 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300206 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400207 int nmsrs;
208 int save_nmsrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400209#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300210 u64 msr_host_kernel_gs_base;
211 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400212#endif
Nadav Har'Eld462b812011-05-24 15:26:10 +0300213 /*
214 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
215 * non-nested (L1) guest, it always points to vmcs01. For a nested
216 * guest (L2), it points to a different VMCS.
217 */
218 struct loaded_vmcs vmcs01;
219 struct loaded_vmcs *loaded_vmcs;
220 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300221 struct msr_autoload {
222 unsigned nr;
223 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
224 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
225 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400226 struct {
227 int loaded;
228 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200229 int gs_ldt_reload_needed;
230 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400231 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200232 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300233 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300234 ulong save_rflags;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300235 struct kvm_save_segment {
236 u16 selector;
237 unsigned long base;
238 u32 limit;
239 u32 ar;
240 } tr, es, ds, fs, gs;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200241 } rmode;
Avi Kivity2fb92db2011-04-27 19:42:18 +0300242 struct {
243 u32 bitmask; /* 4 bits per segment (1 bit per field) */
244 struct kvm_save_segment seg[8];
245 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800246 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300247 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200248
249 /* Support for vnmi-less CPUs */
250 int soft_vnmi_blocked;
251 ktime_t entry_time;
252 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800253 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800254
255 bool rdtscp_enabled;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300256
257 /* Support for a guest hypervisor (nested VMX) */
258 struct nested_vmx nested;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400259};
260
Avi Kivity2fb92db2011-04-27 19:42:18 +0300261enum segment_cache_field {
262 SEG_FIELD_SEL = 0,
263 SEG_FIELD_BASE = 1,
264 SEG_FIELD_LIMIT = 2,
265 SEG_FIELD_AR = 3,
266
267 SEG_FIELD_NR = 4
268};
269
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400270static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
271{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000272 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400273}
274
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300275static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
276{
277 return to_vmx(vcpu)->nested.current_vmcs12;
278}
279
280static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
281{
282 struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
283 if (is_error_page(page)) {
284 kvm_release_page_clean(page);
285 return NULL;
286 }
287 return page;
288}
289
290static void nested_release_page(struct page *page)
291{
292 kvm_release_page_dirty(page);
293}
294
295static void nested_release_page_clean(struct page *page)
296{
297 kvm_release_page_clean(page);
298}
299
Sheng Yang4e1096d2008-07-06 19:16:51 +0800300static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800301static void kvm_cpu_vmxon(u64 addr);
302static void kvm_cpu_vmxoff(void);
Avi Kivityaff48ba2010-12-05 18:56:11 +0200303static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200304static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Avi Kivity75880a02007-06-20 11:20:04 +0300305
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306static DEFINE_PER_CPU(struct vmcs *, vmxarea);
307static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300308/*
309 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
310 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
311 */
312static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300313static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800314
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200315static unsigned long *vmx_io_bitmap_a;
316static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200317static unsigned long *vmx_msr_bitmap_legacy;
318static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300319
Avi Kivity110312c2010-12-21 12:54:20 +0200320static bool cpu_has_load_ia32_efer;
321
Sheng Yang2384d2b2008-01-17 15:14:33 +0800322static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
323static DEFINE_SPINLOCK(vmx_vpid_lock);
324
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300325static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800326 int size;
327 int order;
328 u32 revision_id;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300329 u32 pin_based_exec_ctrl;
330 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800331 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300332 u32 vmexit_ctrl;
333 u32 vmentry_ctrl;
334} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800335
Hannes Ederefff9e52008-11-28 17:02:06 +0100336static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800337 u32 ept;
338 u32 vpid;
339} vmx_capability;
340
Avi Kivity6aa8b732006-12-10 02:21:36 -0800341#define VMX_SEGMENT_FIELD(seg) \
342 [VCPU_SREG_##seg] = { \
343 .selector = GUEST_##seg##_SELECTOR, \
344 .base = GUEST_##seg##_BASE, \
345 .limit = GUEST_##seg##_LIMIT, \
346 .ar_bytes = GUEST_##seg##_AR_BYTES, \
347 }
348
349static struct kvm_vmx_segment_field {
350 unsigned selector;
351 unsigned base;
352 unsigned limit;
353 unsigned ar_bytes;
354} kvm_vmx_segment_fields[] = {
355 VMX_SEGMENT_FIELD(CS),
356 VMX_SEGMENT_FIELD(DS),
357 VMX_SEGMENT_FIELD(ES),
358 VMX_SEGMENT_FIELD(FS),
359 VMX_SEGMENT_FIELD(GS),
360 VMX_SEGMENT_FIELD(SS),
361 VMX_SEGMENT_FIELD(TR),
362 VMX_SEGMENT_FIELD(LDTR),
363};
364
Avi Kivity26bb0982009-09-07 11:14:12 +0300365static u64 host_efer;
366
Avi Kivity6de4f3ad2009-05-31 22:58:47 +0300367static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
368
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300369/*
Brian Gerst8c065852010-07-17 09:03:26 -0400370 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300371 * away by decrementing the array size.
372 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800373static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800374#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300375 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800376#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400377 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200379#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380
Gui Jianfeng31299942010-03-15 17:29:09 +0800381static inline bool is_page_fault(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800382{
383 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
384 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100385 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386}
387
Gui Jianfeng31299942010-03-15 17:29:09 +0800388static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300389{
390 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
391 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100392 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300393}
394
Gui Jianfeng31299942010-03-15 17:29:09 +0800395static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500396{
397 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
398 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100399 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500400}
401
Gui Jianfeng31299942010-03-15 17:29:09 +0800402static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403{
404 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
405 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
406}
407
Gui Jianfeng31299942010-03-15 17:29:09 +0800408static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +0800409{
410 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
411 INTR_INFO_VALID_MASK)) ==
412 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
413}
414
Gui Jianfeng31299942010-03-15 17:29:09 +0800415static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +0800416{
Sheng Yang04547152009-04-01 15:52:31 +0800417 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800418}
419
Gui Jianfeng31299942010-03-15 17:29:09 +0800420static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800421{
Sheng Yang04547152009-04-01 15:52:31 +0800422 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800423}
424
Gui Jianfeng31299942010-03-15 17:29:09 +0800425static inline bool vm_need_tpr_shadow(struct kvm *kvm)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800426{
Sheng Yang04547152009-04-01 15:52:31 +0800427 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800428}
429
Gui Jianfeng31299942010-03-15 17:29:09 +0800430static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800431{
Sheng Yang04547152009-04-01 15:52:31 +0800432 return vmcs_config.cpu_based_exec_ctrl &
433 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800434}
435
Avi Kivity774ead32007-12-26 13:57:04 +0200436static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800437{
Sheng Yang04547152009-04-01 15:52:31 +0800438 return vmcs_config.cpu_based_2nd_exec_ctrl &
439 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
440}
441
442static inline bool cpu_has_vmx_flexpriority(void)
443{
444 return cpu_has_vmx_tpr_shadow() &&
445 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800446}
447
Marcelo Tosattie7997942009-06-11 12:07:40 -0300448static inline bool cpu_has_vmx_ept_execute_only(void)
449{
Gui Jianfeng31299942010-03-15 17:29:09 +0800450 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300451}
452
453static inline bool cpu_has_vmx_eptp_uncacheable(void)
454{
Gui Jianfeng31299942010-03-15 17:29:09 +0800455 return vmx_capability.ept & VMX_EPTP_UC_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300456}
457
458static inline bool cpu_has_vmx_eptp_writeback(void)
459{
Gui Jianfeng31299942010-03-15 17:29:09 +0800460 return vmx_capability.ept & VMX_EPTP_WB_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300461}
462
463static inline bool cpu_has_vmx_ept_2m_page(void)
464{
Gui Jianfeng31299942010-03-15 17:29:09 +0800465 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300466}
467
Sheng Yang878403b2010-01-05 19:02:29 +0800468static inline bool cpu_has_vmx_ept_1g_page(void)
469{
Gui Jianfeng31299942010-03-15 17:29:09 +0800470 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +0800471}
472
Sheng Yang4bc9b982010-06-02 14:05:24 +0800473static inline bool cpu_has_vmx_ept_4levels(void)
474{
475 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
476}
477
Gui Jianfeng31299942010-03-15 17:29:09 +0800478static inline bool cpu_has_vmx_invept_individual_addr(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800479{
Gui Jianfeng31299942010-03-15 17:29:09 +0800480 return vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800481}
482
Gui Jianfeng31299942010-03-15 17:29:09 +0800483static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800484{
Gui Jianfeng31299942010-03-15 17:29:09 +0800485 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800486}
487
Gui Jianfeng31299942010-03-15 17:29:09 +0800488static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800489{
Gui Jianfeng31299942010-03-15 17:29:09 +0800490 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800491}
492
Gui Jianfeng518c8ae2010-06-04 08:51:39 +0800493static inline bool cpu_has_vmx_invvpid_single(void)
494{
495 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
496}
497
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800498static inline bool cpu_has_vmx_invvpid_global(void)
499{
500 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
501}
502
Gui Jianfeng31299942010-03-15 17:29:09 +0800503static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800504{
Sheng Yang04547152009-04-01 15:52:31 +0800505 return vmcs_config.cpu_based_2nd_exec_ctrl &
506 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800507}
508
Gui Jianfeng31299942010-03-15 17:29:09 +0800509static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700510{
511 return vmcs_config.cpu_based_2nd_exec_ctrl &
512 SECONDARY_EXEC_UNRESTRICTED_GUEST;
513}
514
Gui Jianfeng31299942010-03-15 17:29:09 +0800515static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800516{
517 return vmcs_config.cpu_based_2nd_exec_ctrl &
518 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
519}
520
Gui Jianfeng31299942010-03-15 17:29:09 +0800521static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800522{
Gui Jianfeng6d3e4352010-01-29 15:36:59 +0800523 return flexpriority_enabled && irqchip_in_kernel(kvm);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800524}
525
Gui Jianfeng31299942010-03-15 17:29:09 +0800526static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800527{
Sheng Yang04547152009-04-01 15:52:31 +0800528 return vmcs_config.cpu_based_2nd_exec_ctrl &
529 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800530}
531
Gui Jianfeng31299942010-03-15 17:29:09 +0800532static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800533{
534 return vmcs_config.cpu_based_2nd_exec_ctrl &
535 SECONDARY_EXEC_RDTSCP;
536}
537
Gui Jianfeng31299942010-03-15 17:29:09 +0800538static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +0800539{
540 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
541}
542
Sheng Yangf5f48ee2010-06-30 12:25:15 +0800543static inline bool cpu_has_vmx_wbinvd_exit(void)
544{
545 return vmcs_config.cpu_based_2nd_exec_ctrl &
546 SECONDARY_EXEC_WBINVD_EXITING;
547}
548
Sheng Yang04547152009-04-01 15:52:31 +0800549static inline bool report_flexpriority(void)
550{
551 return flexpriority_enabled;
552}
553
Rusty Russell8b9cf982007-07-30 16:31:43 +1000554static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800555{
556 int i;
557
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400558 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300559 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300560 return i;
561 return -1;
562}
563
Sheng Yang2384d2b2008-01-17 15:14:33 +0800564static inline void __invvpid(int ext, u16 vpid, gva_t gva)
565{
566 struct {
567 u64 vpid : 16;
568 u64 rsvd : 48;
569 u64 gva;
570 } operand = { vpid, 0, gva };
571
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300572 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800573 /* CF==1 or ZF==1 --> rc = -1 */
574 "; ja 1f ; ud2 ; 1:"
575 : : "a"(&operand), "c"(ext) : "cc", "memory");
576}
577
Sheng Yang14394422008-04-28 12:24:45 +0800578static inline void __invept(int ext, u64 eptp, gpa_t gpa)
579{
580 struct {
581 u64 eptp, gpa;
582 } operand = {eptp, gpa};
583
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300584 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800585 /* CF==1 or ZF==1 --> rc = -1 */
586 "; ja 1f ; ud2 ; 1:\n"
587 : : "a" (&operand), "c" (ext) : "cc", "memory");
588}
589
Avi Kivity26bb0982009-09-07 11:14:12 +0300590static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300591{
592 int i;
593
Rusty Russell8b9cf982007-07-30 16:31:43 +1000594 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300595 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400596 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000597 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800598}
599
Avi Kivity6aa8b732006-12-10 02:21:36 -0800600static void vmcs_clear(struct vmcs *vmcs)
601{
602 u64 phys_addr = __pa(vmcs);
603 u8 error;
604
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300605 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +0200606 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800607 : "cc", "memory");
608 if (error)
609 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
610 vmcs, phys_addr);
611}
612
Nadav Har'Eld462b812011-05-24 15:26:10 +0300613static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
614{
615 vmcs_clear(loaded_vmcs->vmcs);
616 loaded_vmcs->cpu = -1;
617 loaded_vmcs->launched = 0;
618}
619
Dongxiao Xu7725b892010-05-11 18:29:38 +0800620static void vmcs_load(struct vmcs *vmcs)
621{
622 u64 phys_addr = __pa(vmcs);
623 u8 error;
624
625 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +0200626 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +0800627 : "cc", "memory");
628 if (error)
629 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
630 vmcs, phys_addr);
631}
632
Nadav Har'Eld462b812011-05-24 15:26:10 +0300633static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800634{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300635 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800636 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637
Nadav Har'Eld462b812011-05-24 15:26:10 +0300638 if (loaded_vmcs->cpu != cpu)
639 return; /* vcpu migration can race with cpu offline */
640 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641 per_cpu(current_vmcs, cpu) = NULL;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300642 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
643 loaded_vmcs_init(loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800644}
645
Nadav Har'Eld462b812011-05-24 15:26:10 +0300646static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800647{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300648 if (loaded_vmcs->cpu != -1)
649 smp_call_function_single(
650 loaded_vmcs->cpu, __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800651}
652
Gui Jianfeng1760dd42010-06-07 10:33:27 +0800653static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800654{
655 if (vmx->vpid == 0)
656 return;
657
Gui Jianfeng518c8ae2010-06-04 08:51:39 +0800658 if (cpu_has_vmx_invvpid_single())
659 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800660}
661
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800662static inline void vpid_sync_vcpu_global(void)
663{
664 if (cpu_has_vmx_invvpid_global())
665 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
666}
667
668static inline void vpid_sync_context(struct vcpu_vmx *vmx)
669{
670 if (cpu_has_vmx_invvpid_single())
Gui Jianfeng1760dd42010-06-07 10:33:27 +0800671 vpid_sync_vcpu_single(vmx);
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800672 else
673 vpid_sync_vcpu_global();
674}
675
Sheng Yang14394422008-04-28 12:24:45 +0800676static inline void ept_sync_global(void)
677{
678 if (cpu_has_vmx_invept_global())
679 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
680}
681
682static inline void ept_sync_context(u64 eptp)
683{
Avi Kivity089d0342009-03-23 18:26:32 +0200684 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800685 if (cpu_has_vmx_invept_context())
686 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
687 else
688 ept_sync_global();
689 }
690}
691
692static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
693{
Avi Kivity089d0342009-03-23 18:26:32 +0200694 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800695 if (cpu_has_vmx_invept_individual_addr())
696 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
697 eptp, gpa);
698 else
699 ept_sync_context(eptp);
700 }
701}
702
Avi Kivity96304212011-05-15 10:13:13 -0400703static __always_inline unsigned long vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704{
Avi Kivity5e520e62011-05-15 10:13:12 -0400705 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706
Avi Kivity5e520e62011-05-15 10:13:12 -0400707 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
708 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709 return value;
710}
711
Avi Kivity96304212011-05-15 10:13:13 -0400712static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713{
714 return vmcs_readl(field);
715}
716
Avi Kivity96304212011-05-15 10:13:13 -0400717static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800718{
719 return vmcs_readl(field);
720}
721
Avi Kivity96304212011-05-15 10:13:13 -0400722static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800724#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800725 return vmcs_readl(field);
726#else
727 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
728#endif
729}
730
Avi Kivitye52de1b2007-01-05 16:36:56 -0800731static noinline void vmwrite_error(unsigned long field, unsigned long value)
732{
733 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
734 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
735 dump_stack();
736}
737
Avi Kivity6aa8b732006-12-10 02:21:36 -0800738static void vmcs_writel(unsigned long field, unsigned long value)
739{
740 u8 error;
741
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300742 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400743 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800744 if (unlikely(error))
745 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746}
747
748static void vmcs_write16(unsigned long field, u16 value)
749{
750 vmcs_writel(field, value);
751}
752
753static void vmcs_write32(unsigned long field, u32 value)
754{
755 vmcs_writel(field, value);
756}
757
758static void vmcs_write64(unsigned long field, u64 value)
759{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300761#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762 asm volatile ("");
763 vmcs_writel(field+1, value >> 32);
764#endif
765}
766
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300767static void vmcs_clear_bits(unsigned long field, u32 mask)
768{
769 vmcs_writel(field, vmcs_readl(field) & ~mask);
770}
771
772static void vmcs_set_bits(unsigned long field, u32 mask)
773{
774 vmcs_writel(field, vmcs_readl(field) | mask);
775}
776
Avi Kivity2fb92db2011-04-27 19:42:18 +0300777static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
778{
779 vmx->segment_cache.bitmask = 0;
780}
781
782static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
783 unsigned field)
784{
785 bool ret;
786 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
787
788 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
789 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
790 vmx->segment_cache.bitmask = 0;
791 }
792 ret = vmx->segment_cache.bitmask & mask;
793 vmx->segment_cache.bitmask |= mask;
794 return ret;
795}
796
797static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
798{
799 u16 *p = &vmx->segment_cache.seg[seg].selector;
800
801 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
802 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
803 return *p;
804}
805
806static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
807{
808 ulong *p = &vmx->segment_cache.seg[seg].base;
809
810 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
811 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
812 return *p;
813}
814
815static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
816{
817 u32 *p = &vmx->segment_cache.seg[seg].limit;
818
819 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
820 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
821 return *p;
822}
823
824static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
825{
826 u32 *p = &vmx->segment_cache.seg[seg].ar;
827
828 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
829 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
830 return *p;
831}
832
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300833static void update_exception_bitmap(struct kvm_vcpu *vcpu)
834{
835 u32 eb;
836
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100837 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
838 (1u << NM_VECTOR) | (1u << DB_VECTOR);
839 if ((vcpu->guest_debug &
840 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
841 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
842 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300843 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300844 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200845 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800846 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +0200847 if (vcpu->fpu_active)
848 eb &= ~(1u << NM_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300849 vmcs_write32(EXCEPTION_BITMAP, eb);
850}
851
Avi Kivity61d2ef22010-04-28 16:40:38 +0300852static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
853{
854 unsigned i;
855 struct msr_autoload *m = &vmx->msr_autoload;
856
Avi Kivity110312c2010-12-21 12:54:20 +0200857 if (msr == MSR_EFER && cpu_has_load_ia32_efer) {
858 vmcs_clear_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER);
859 vmcs_clear_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER);
860 return;
861 }
862
Avi Kivity61d2ef22010-04-28 16:40:38 +0300863 for (i = 0; i < m->nr; ++i)
864 if (m->guest[i].index == msr)
865 break;
866
867 if (i == m->nr)
868 return;
869 --m->nr;
870 m->guest[i] = m->guest[m->nr];
871 m->host[i] = m->host[m->nr];
872 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
873 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
874}
875
876static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
877 u64 guest_val, u64 host_val)
878{
879 unsigned i;
880 struct msr_autoload *m = &vmx->msr_autoload;
881
Avi Kivity110312c2010-12-21 12:54:20 +0200882 if (msr == MSR_EFER && cpu_has_load_ia32_efer) {
883 vmcs_write64(GUEST_IA32_EFER, guest_val);
884 vmcs_write64(HOST_IA32_EFER, host_val);
885 vmcs_set_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER);
886 vmcs_set_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER);
887 return;
888 }
889
Avi Kivity61d2ef22010-04-28 16:40:38 +0300890 for (i = 0; i < m->nr; ++i)
891 if (m->guest[i].index == msr)
892 break;
893
894 if (i == m->nr) {
895 ++m->nr;
896 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
897 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
898 }
899
900 m->guest[i].index = msr;
901 m->guest[i].value = guest_val;
902 m->host[i].index = msr;
903 m->host[i].value = host_val;
904}
905
Avi Kivity33ed6322007-05-02 16:54:03 +0300906static void reload_tss(void)
907{
Avi Kivity33ed6322007-05-02 16:54:03 +0300908 /*
909 * VT restores TR but not its size. Useless.
910 */
Avi Kivityd3591922010-07-26 18:32:39 +0300911 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +0200912 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300913
Avi Kivityd3591922010-07-26 18:32:39 +0300914 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +0300915 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
916 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300917}
918
Avi Kivity92c0d902009-10-29 11:00:16 +0200919static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +0300920{
Roel Kluin3a34a882009-08-04 02:08:45 -0700921 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300922 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300923
Avi Kivityf6801df2010-01-21 15:31:50 +0200924 guest_efer = vmx->vcpu.arch.efer;
Roel Kluin3a34a882009-08-04 02:08:45 -0700925
Avi Kivity51c6cf62007-08-29 03:48:05 +0300926 /*
927 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
928 * outside long mode
929 */
930 ignore_bits = EFER_NX | EFER_SCE;
931#ifdef CONFIG_X86_64
932 ignore_bits |= EFER_LMA | EFER_LME;
933 /* SCE is meaningful only in long mode on Intel */
934 if (guest_efer & EFER_LMA)
935 ignore_bits &= ~(u64)EFER_SCE;
936#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +0300937 guest_efer &= ~ignore_bits;
938 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +0300939 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +0200940 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +0300941
942 clear_atomic_switch_msr(vmx, MSR_EFER);
943 /* On ept, can't emulate nx, and must switch nx atomically */
944 if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
945 guest_efer = vmx->vcpu.arch.efer;
946 if (!(guest_efer & EFER_LMA))
947 guest_efer &= ~EFER_LME;
948 add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
949 return false;
950 }
951
Avi Kivity26bb0982009-09-07 11:14:12 +0300952 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300953}
954
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200955static unsigned long segment_base(u16 selector)
956{
Avi Kivityd3591922010-07-26 18:32:39 +0300957 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200958 struct desc_struct *d;
959 unsigned long table_base;
960 unsigned long v;
961
962 if (!(selector & ~3))
963 return 0;
964
Avi Kivityd3591922010-07-26 18:32:39 +0300965 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200966
967 if (selector & 4) { /* from ldt */
968 u16 ldt_selector = kvm_read_ldt();
969
970 if (!(ldt_selector & ~3))
971 return 0;
972
973 table_base = segment_base(ldt_selector);
974 }
975 d = (struct desc_struct *)(table_base + (selector & ~7));
976 v = get_desc_base(d);
977#ifdef CONFIG_X86_64
978 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
979 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
980#endif
981 return v;
982}
983
984static inline unsigned long kvm_read_tr_base(void)
985{
986 u16 tr;
987 asm("str %0" : "=g"(tr));
988 return segment_base(tr);
989}
990
Avi Kivity04d2cc72007-09-10 18:10:54 +0300991static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300992{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300993 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +0300994 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300995
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400996 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300997 return;
998
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400999 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001000 /*
1001 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1002 * allow segment selectors with cpl > 0 or ti == 1.
1003 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001004 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02001005 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02001006 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001007 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001008 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001009 vmx->host_state.fs_reload_needed = 0;
1010 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03001011 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001012 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001013 }
Avi Kivity9581d442010-10-19 16:46:55 +02001014 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001015 if (!(vmx->host_state.gs_sel & 7))
1016 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001017 else {
1018 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001019 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001020 }
1021
1022#ifdef CONFIG_X86_64
1023 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
1024 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
1025#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001026 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
1027 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03001028#endif
Avi Kivity707c0872007-05-02 17:33:43 +03001029
1030#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001031 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
1032 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03001033 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03001034#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03001035 for (i = 0; i < vmx->save_nmsrs; ++i)
1036 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02001037 vmx->guest_msrs[i].data,
1038 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03001039}
1040
Avi Kivitya9b21b62008-06-24 11:48:49 +03001041static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001042{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001043 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001044 return;
1045
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001046 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001047 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02001048#ifdef CONFIG_X86_64
1049 if (is_long_mode(&vmx->vcpu))
1050 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1051#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001052 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001053 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001054#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02001055 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001056#else
1057 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001058#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001059 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02001060 if (vmx->host_state.fs_reload_needed)
1061 loadsegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001062 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001063#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001064 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001065#endif
Avi Kivity1c11e712010-05-03 16:05:44 +03001066 if (current_thread_info()->status & TS_USEDFPU)
1067 clts();
Avi Kivity3444d7d2010-07-26 18:32:38 +03001068 load_gdt(&__get_cpu_var(host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03001069}
1070
Avi Kivitya9b21b62008-06-24 11:48:49 +03001071static void vmx_load_host_state(struct vcpu_vmx *vmx)
1072{
1073 preempt_disable();
1074 __vmx_load_host_state(vmx);
1075 preempt_enable();
1076}
1077
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078/*
1079 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1080 * vcpu mutex is already taken.
1081 */
Avi Kivity15ad7142007-07-11 18:17:21 +03001082static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001084 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001085 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001087 if (!vmm_exclusive)
1088 kvm_cpu_vmxon(phys_addr);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001089 else if (vmx->loaded_vmcs->cpu != cpu)
1090 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091
Nadav Har'Eld462b812011-05-24 15:26:10 +03001092 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1093 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1094 vmcs_load(vmx->loaded_vmcs->vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095 }
1096
Nadav Har'Eld462b812011-05-24 15:26:10 +03001097 if (vmx->loaded_vmcs->cpu != cpu) {
Avi Kivityd3591922010-07-26 18:32:39 +03001098 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001099 unsigned long sysenter_esp;
1100
Avi Kivitya8eeb042010-05-10 12:34:53 +03001101 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001102 local_irq_disable();
Nadav Har'Eld462b812011-05-24 15:26:10 +03001103 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1104 &per_cpu(loaded_vmcss_on_cpu, cpu));
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001105 local_irq_enable();
1106
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107 /*
1108 * Linux uses per-cpu TSS and GDT, so set these when switching
1109 * processors.
1110 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001111 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03001112 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113
1114 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1115 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Nadav Har'Eld462b812011-05-24 15:26:10 +03001116 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118}
1119
1120static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1121{
Avi Kivitya9b21b62008-06-24 11:48:49 +03001122 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001123 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03001124 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
1125 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001126 kvm_cpu_vmxoff();
1127 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128}
1129
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001130static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
1131{
Avi Kivity81231c62010-01-24 16:26:40 +02001132 ulong cr0;
1133
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001134 if (vcpu->fpu_active)
1135 return;
1136 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02001137 cr0 = vmcs_readl(GUEST_CR0);
1138 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
1139 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
1140 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001141 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001142 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
1143 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001144}
1145
Avi Kivityedcafe32009-12-30 18:07:40 +02001146static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1147
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001148static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
1149{
Avi Kivityedcafe32009-12-30 18:07:40 +02001150 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02001151 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001152 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001153 vcpu->arch.cr0_guest_owned_bits = 0;
1154 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
1155 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001156}
1157
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1159{
Avi Kivity78ac8b42010-04-08 18:19:35 +03001160 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001161
Avi Kivity6de12732011-03-07 12:51:22 +02001162 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1163 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1164 rflags = vmcs_readl(GUEST_RFLAGS);
1165 if (to_vmx(vcpu)->rmode.vm86_active) {
1166 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1167 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1168 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1169 }
1170 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001171 }
Avi Kivity6de12732011-03-07 12:51:22 +02001172 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173}
1174
1175static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1176{
Avi Kivity6de12732011-03-07 12:51:22 +02001177 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity69c73022011-03-07 15:26:44 +02001178 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity6de12732011-03-07 12:51:22 +02001179 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001180 if (to_vmx(vcpu)->rmode.vm86_active) {
1181 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001182 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001183 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184 vmcs_writel(GUEST_RFLAGS, rflags);
1185}
1186
Glauber Costa2809f5d2009-05-12 16:21:05 -04001187static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1188{
1189 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1190 int ret = 0;
1191
1192 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001193 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001194 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001195 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001196
1197 return ret & mask;
1198}
1199
1200static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1201{
1202 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1203 u32 interruptibility = interruptibility_old;
1204
1205 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1206
Jan Kiszka48005f62010-02-19 19:38:07 +01001207 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001208 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001209 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001210 interruptibility |= GUEST_INTR_STATE_STI;
1211
1212 if ((interruptibility != interruptibility_old))
1213 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1214}
1215
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1217{
1218 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001220 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001221 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001222 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223
Glauber Costa2809f5d2009-05-12 16:21:05 -04001224 /* skipping an emulated instruction also counts */
1225 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226}
1227
Anthony Liguori443381a2010-12-06 10:53:38 -06001228static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
1229{
1230 /* Ensure that we clear the HLT state in the VMCS. We don't need to
1231 * explicitly skip the instruction because if the HLT state is set, then
1232 * the instruction is already executing and RIP has already been
1233 * advanced. */
1234 if (!yield_on_hlt &&
1235 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
1236 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
1237}
1238
Avi Kivity298101d2007-11-25 13:41:11 +02001239static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02001240 bool has_error_code, u32 error_code,
1241 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02001242{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001243 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001244 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001245
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001246 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001247 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001248 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1249 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001250
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001251 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001252 int inc_eip = 0;
1253 if (kvm_exception_is_soft(nr))
1254 inc_eip = vcpu->arch.event_exit_inst_len;
1255 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02001256 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001257 return;
1258 }
1259
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001260 if (kvm_exception_is_soft(nr)) {
1261 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1262 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001263 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1264 } else
1265 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1266
1267 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Anthony Liguori443381a2010-12-06 10:53:38 -06001268 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02001269}
1270
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001271static bool vmx_rdtscp_supported(void)
1272{
1273 return cpu_has_vmx_rdtscp();
1274}
1275
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276/*
Eddie Donga75beee2007-05-17 18:55:15 +03001277 * Swap MSR entry in host/guest MSR entry array.
1278 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001279static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03001280{
Avi Kivity26bb0982009-09-07 11:14:12 +03001281 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001282
1283 tmp = vmx->guest_msrs[to];
1284 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1285 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001286}
1287
1288/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001289 * Set up the vmcs to automatically save and restore system
1290 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1291 * mode, as fiddling with msrs is very expensive.
1292 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001293static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001294{
Avi Kivity26bb0982009-09-07 11:14:12 +03001295 int save_nmsrs, index;
Avi Kivity58972972009-02-24 22:26:47 +02001296 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +03001297
Avi Kivity33f9c502008-02-27 16:06:57 +02001298 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +03001299 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001300#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001301 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10001302 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03001303 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001304 move_msr_up(vmx, index, save_nmsrs++);
1305 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001306 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001307 move_msr_up(vmx, index, save_nmsrs++);
1308 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001309 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001310 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001311 index = __find_msr_index(vmx, MSR_TSC_AUX);
1312 if (index >= 0 && vmx->rdtscp_enabled)
1313 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03001314 /*
Brian Gerst8c065852010-07-17 09:03:26 -04001315 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03001316 * if efer.sce is enabled.
1317 */
Brian Gerst8c065852010-07-17 09:03:26 -04001318 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02001319 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10001320 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001321 }
Eddie Donga75beee2007-05-17 18:55:15 +03001322#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02001323 index = __find_msr_index(vmx, MSR_EFER);
1324 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03001325 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001326
Avi Kivity26bb0982009-09-07 11:14:12 +03001327 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02001328
1329 if (cpu_has_vmx_msr_bitmap()) {
1330 if (is_long_mode(&vmx->vcpu))
1331 msr_bitmap = vmx_msr_bitmap_longmode;
1332 else
1333 msr_bitmap = vmx_msr_bitmap_legacy;
1334
1335 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
1336 }
Avi Kivitye38aea32007-04-19 13:22:48 +03001337}
1338
1339/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340 * reads and returns guest's timestamp counter "register"
1341 * guest_tsc = host_tsc + tsc_offset -- 21.3
1342 */
1343static u64 guest_read_tsc(void)
1344{
1345 u64 host_tsc, tsc_offset;
1346
1347 rdtscll(host_tsc);
1348 tsc_offset = vmcs_read64(TSC_OFFSET);
1349 return host_tsc + tsc_offset;
1350}
1351
1352/*
Joerg Roedel4051b182011-03-25 09:44:49 +01001353 * Empty call-back. Needs to be implemented when VMX enables the SET_TSC_KHZ
1354 * ioctl. In this case the call-back should update internal vmx state to make
1355 * the changes effective.
1356 */
1357static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
1358{
1359 /* Nothing to do here */
1360}
1361
1362/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10001363 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10001365static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366{
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001367 vmcs_write64(TSC_OFFSET, offset);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368}
1369
Zachary Amsdene48672f2010-08-19 22:07:23 -10001370static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
1371{
1372 u64 offset = vmcs_read64(TSC_OFFSET);
1373 vmcs_write64(TSC_OFFSET, offset + adjustment);
1374}
1375
Joerg Roedel857e4092011-03-25 09:44:50 +01001376static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1377{
1378 return target_tsc - native_read_tsc();
1379}
1380
Nadav Har'El801d3422011-05-25 23:02:23 +03001381static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
1382{
1383 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
1384 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
1385}
1386
1387/*
1388 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1389 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1390 * all guests if the "nested" module option is off, and can also be disabled
1391 * for a single guest by disabling its VMX cpuid bit.
1392 */
1393static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
1394{
1395 return nested && guest_cpuid_has_vmx(vcpu);
1396}
1397
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398/*
1399 * Reads an msr value (of 'msr_index') into 'pdata'.
1400 * Returns 0 on success, non-0 otherwise.
1401 * Assumes vcpu_load() was already called.
1402 */
1403static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1404{
1405 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +03001406 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407
1408 if (!pdata) {
1409 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
1410 return -EINVAL;
1411 }
1412
1413 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001414#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001415 case MSR_FS_BASE:
1416 data = vmcs_readl(GUEST_FS_BASE);
1417 break;
1418 case MSR_GS_BASE:
1419 data = vmcs_readl(GUEST_GS_BASE);
1420 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001421 case MSR_KERNEL_GS_BASE:
1422 vmx_load_host_state(to_vmx(vcpu));
1423 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
1424 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001425#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001427 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301428 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429 data = guest_read_tsc();
1430 break;
1431 case MSR_IA32_SYSENTER_CS:
1432 data = vmcs_read32(GUEST_SYSENTER_CS);
1433 break;
1434 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001435 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436 break;
1437 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001438 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001440 case MSR_TSC_AUX:
1441 if (!to_vmx(vcpu)->rdtscp_enabled)
1442 return 1;
1443 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444 default:
Avi Kivity26bb0982009-09-07 11:14:12 +03001445 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +10001446 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001447 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001448 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity3bab1f52006-12-29 16:49:48 -08001449 data = msr->data;
1450 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001451 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001452 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453 }
1454
1455 *pdata = data;
1456 return 0;
1457}
1458
1459/*
1460 * Writes msr value into into the appropriate "register".
1461 * Returns 0 on success, non-0 otherwise.
1462 * Assumes vcpu_load() was already called.
1463 */
1464static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1465{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001466 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001467 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03001468 int ret = 0;
1469
Avi Kivity6aa8b732006-12-10 02:21:36 -08001470 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001471 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001472 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001473 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001474 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001475#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001477 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478 vmcs_writel(GUEST_FS_BASE, data);
1479 break;
1480 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001481 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001482 vmcs_writel(GUEST_GS_BASE, data);
1483 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001484 case MSR_KERNEL_GS_BASE:
1485 vmx_load_host_state(vmx);
1486 vmx->msr_guest_kernel_gs_base = data;
1487 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488#endif
1489 case MSR_IA32_SYSENTER_CS:
1490 vmcs_write32(GUEST_SYSENTER_CS, data);
1491 break;
1492 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001493 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494 break;
1495 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001496 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001497 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301498 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10001499 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001500 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001501 case MSR_IA32_CR_PAT:
1502 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1503 vmcs_write64(GUEST_IA32_PAT, data);
1504 vcpu->arch.pat = data;
1505 break;
1506 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001507 ret = kvm_set_msr_common(vcpu, msr_index, data);
1508 break;
1509 case MSR_TSC_AUX:
1510 if (!vmx->rdtscp_enabled)
1511 return 1;
1512 /* Check reserved bit, higher 32 bits should be zero */
1513 if ((data >> 32) != 0)
1514 return 1;
1515 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001517 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001518 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001519 vmx_load_host_state(vmx);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001520 msr->data = data;
1521 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001523 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524 }
1525
Eddie Dong2cc51562007-05-21 07:28:09 +03001526 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527}
1528
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001529static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001530{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001531 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1532 switch (reg) {
1533 case VCPU_REGS_RSP:
1534 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1535 break;
1536 case VCPU_REGS_RIP:
1537 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1538 break;
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03001539 case VCPU_EXREG_PDPTR:
1540 if (enable_ept)
1541 ept_save_pdptrs(vcpu);
1542 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001543 default:
1544 break;
1545 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001546}
1547
Jan Kiszka355be0b2009-10-03 00:31:21 +02001548static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001549{
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001550 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1551 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1552 else
1553 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1554
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001555 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001556}
1557
1558static __init int cpu_has_kvm_support(void)
1559{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001560 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001561}
1562
1563static __init int vmx_disabled_by_bios(void)
1564{
1565 u64 msr;
1566
1567 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04001568 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08001569 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04001570 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
1571 && tboot_enabled())
1572 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08001573 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04001574 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08001575 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08001576 && !tboot_enabled()) {
1577 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08001578 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04001579 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08001580 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08001581 /* launched w/o TXT and VMX disabled */
1582 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
1583 && !tboot_enabled())
1584 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04001585 }
1586
1587 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588}
1589
Dongxiao Xu7725b892010-05-11 18:29:38 +08001590static void kvm_cpu_vmxon(u64 addr)
1591{
1592 asm volatile (ASM_VMX_VMXON_RAX
1593 : : "a"(&addr), "m"(addr)
1594 : "memory", "cc");
1595}
1596
Alexander Graf10474ae2009-09-15 11:37:46 +02001597static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001598{
1599 int cpu = raw_smp_processor_id();
1600 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04001601 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602
Alexander Graf10474ae2009-09-15 11:37:46 +02001603 if (read_cr4() & X86_CR4_VMXE)
1604 return -EBUSY;
1605
Nadav Har'Eld462b812011-05-24 15:26:10 +03001606 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04001608
1609 test_bits = FEATURE_CONTROL_LOCKED;
1610 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
1611 if (tboot_enabled())
1612 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
1613
1614 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04001616 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
1617 }
Rusty Russell66aee912007-07-17 23:34:16 +10001618 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Alexander Graf10474ae2009-09-15 11:37:46 +02001619
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001620 if (vmm_exclusive) {
1621 kvm_cpu_vmxon(phys_addr);
1622 ept_sync_global();
1623 }
Alexander Graf10474ae2009-09-15 11:37:46 +02001624
Avi Kivity3444d7d2010-07-26 18:32:38 +03001625 store_gdt(&__get_cpu_var(host_gdt));
1626
Alexander Graf10474ae2009-09-15 11:37:46 +02001627 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628}
1629
Nadav Har'Eld462b812011-05-24 15:26:10 +03001630static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03001631{
1632 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03001633 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03001634
Nadav Har'Eld462b812011-05-24 15:26:10 +03001635 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
1636 loaded_vmcss_on_cpu_link)
1637 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03001638}
1639
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001640
1641/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1642 * tricks.
1643 */
1644static void kvm_cpu_vmxoff(void)
1645{
1646 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001647}
1648
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649static void hardware_disable(void *garbage)
1650{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001651 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03001652 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001653 kvm_cpu_vmxoff();
1654 }
Dongxiao Xu7725b892010-05-11 18:29:38 +08001655 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001656}
1657
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001658static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001659 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660{
1661 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001662 u32 ctl = ctl_min | ctl_opt;
1663
1664 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1665
1666 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1667 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1668
1669 /* Ensure minimum (required) set of control bits are supported. */
1670 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001671 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001672
1673 *result = ctl;
1674 return 0;
1675}
1676
Avi Kivity110312c2010-12-21 12:54:20 +02001677static __init bool allow_1_setting(u32 msr, u32 ctl)
1678{
1679 u32 vmx_msr_low, vmx_msr_high;
1680
1681 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1682 return vmx_msr_high & ctl;
1683}
1684
Yang, Sheng002c7f72007-07-31 14:23:01 +03001685static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001686{
1687 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001688 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001689 u32 _pin_based_exec_control = 0;
1690 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001691 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001692 u32 _vmexit_control = 0;
1693 u32 _vmentry_control = 0;
1694
1695 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001696 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001697 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1698 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001699 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001700
Anthony Liguori443381a2010-12-06 10:53:38 -06001701 min =
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001702#ifdef CONFIG_X86_64
1703 CPU_BASED_CR8_LOAD_EXITING |
1704 CPU_BASED_CR8_STORE_EXITING |
1705#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001706 CPU_BASED_CR3_LOAD_EXITING |
1707 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001708 CPU_BASED_USE_IO_BITMAPS |
1709 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001710 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08001711 CPU_BASED_MWAIT_EXITING |
1712 CPU_BASED_MONITOR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001713 CPU_BASED_INVLPG_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06001714
1715 if (yield_on_hlt)
1716 min |= CPU_BASED_HLT_EXITING;
1717
Sheng Yangf78e0e22007-10-29 09:40:42 +08001718 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001719 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001720 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001721 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1722 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001723 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001724#ifdef CONFIG_X86_64
1725 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1726 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1727 ~CPU_BASED_CR8_STORE_EXITING;
1728#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001729 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001730 min2 = 0;
1731 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001732 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001733 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001734 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001735 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001736 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
1737 SECONDARY_EXEC_RDTSCP;
Sheng Yangd56f5462008-04-25 10:13:16 +08001738 if (adjust_vmx_controls(min2, opt2,
1739 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001740 &_cpu_based_2nd_exec_control) < 0)
1741 return -EIO;
1742 }
1743#ifndef CONFIG_X86_64
1744 if (!(_cpu_based_2nd_exec_control &
1745 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1746 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1747#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001748 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001749 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1750 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03001751 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
1752 CPU_BASED_CR3_STORE_EXITING |
1753 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001754 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1755 vmx_capability.ept, vmx_capability.vpid);
1756 }
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001757
1758 min = 0;
1759#ifdef CONFIG_X86_64
1760 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1761#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001762 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001763 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1764 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001765 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001766
Sheng Yang468d4722008-10-09 16:01:55 +08001767 min = 0;
1768 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001769 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1770 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001771 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001772
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001773 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001774
1775 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1776 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001777 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001778
1779#ifdef CONFIG_X86_64
1780 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1781 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001782 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001783#endif
1784
1785 /* Require Write-Back (WB) memory type for VMCS accesses. */
1786 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001787 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001788
Yang, Sheng002c7f72007-07-31 14:23:01 +03001789 vmcs_conf->size = vmx_msr_high & 0x1fff;
1790 vmcs_conf->order = get_order(vmcs_config.size);
1791 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001792
Yang, Sheng002c7f72007-07-31 14:23:01 +03001793 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1794 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001795 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001796 vmcs_conf->vmexit_ctrl = _vmexit_control;
1797 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001798
Avi Kivity110312c2010-12-21 12:54:20 +02001799 cpu_has_load_ia32_efer =
1800 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
1801 VM_ENTRY_LOAD_IA32_EFER)
1802 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
1803 VM_EXIT_LOAD_IA32_EFER);
1804
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001805 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001806}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001807
1808static struct vmcs *alloc_vmcs_cpu(int cpu)
1809{
1810 int node = cpu_to_node(cpu);
1811 struct page *pages;
1812 struct vmcs *vmcs;
1813
Mel Gorman6484eb32009-06-16 15:31:54 -07001814 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001815 if (!pages)
1816 return NULL;
1817 vmcs = page_address(pages);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001818 memset(vmcs, 0, vmcs_config.size);
1819 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001820 return vmcs;
1821}
1822
1823static struct vmcs *alloc_vmcs(void)
1824{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001825 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001826}
1827
1828static void free_vmcs(struct vmcs *vmcs)
1829{
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001830 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831}
1832
Nadav Har'Eld462b812011-05-24 15:26:10 +03001833/*
1834 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
1835 */
1836static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
1837{
1838 if (!loaded_vmcs->vmcs)
1839 return;
1840 loaded_vmcs_clear(loaded_vmcs);
1841 free_vmcs(loaded_vmcs->vmcs);
1842 loaded_vmcs->vmcs = NULL;
1843}
1844
Sam Ravnborg39959582007-06-01 00:47:13 -07001845static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001846{
1847 int cpu;
1848
Zachary Amsden3230bb42009-09-29 11:38:37 -10001849 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10001851 per_cpu(vmxarea, cpu) = NULL;
1852 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001853}
1854
Avi Kivity6aa8b732006-12-10 02:21:36 -08001855static __init int alloc_kvm_area(void)
1856{
1857 int cpu;
1858
Zachary Amsden3230bb42009-09-29 11:38:37 -10001859 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001860 struct vmcs *vmcs;
1861
1862 vmcs = alloc_vmcs_cpu(cpu);
1863 if (!vmcs) {
1864 free_kvm_area();
1865 return -ENOMEM;
1866 }
1867
1868 per_cpu(vmxarea, cpu) = vmcs;
1869 }
1870 return 0;
1871}
1872
1873static __init int hardware_setup(void)
1874{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001875 if (setup_vmcs_config(&vmcs_config) < 0)
1876 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001877
1878 if (boot_cpu_has(X86_FEATURE_NX))
1879 kvm_enable_efer_bits(EFER_NX);
1880
Sheng Yang93ba03c2009-04-01 15:52:32 +08001881 if (!cpu_has_vmx_vpid())
1882 enable_vpid = 0;
1883
Sheng Yang4bc9b982010-06-02 14:05:24 +08001884 if (!cpu_has_vmx_ept() ||
1885 !cpu_has_vmx_ept_4levels()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001886 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001887 enable_unrestricted_guest = 0;
1888 }
1889
1890 if (!cpu_has_vmx_unrestricted_guest())
1891 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001892
1893 if (!cpu_has_vmx_flexpriority())
1894 flexpriority_enabled = 0;
1895
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001896 if (!cpu_has_vmx_tpr_shadow())
1897 kvm_x86_ops->update_cr8_intercept = NULL;
1898
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001899 if (enable_ept && !cpu_has_vmx_ept_2m_page())
1900 kvm_disable_largepages();
1901
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001902 if (!cpu_has_vmx_ple())
1903 ple_gap = 0;
1904
Avi Kivity6aa8b732006-12-10 02:21:36 -08001905 return alloc_kvm_area();
1906}
1907
1908static __exit void hardware_unsetup(void)
1909{
1910 free_kvm_area();
1911}
1912
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1914{
1915 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1916
Avi Kivity6af11b92007-03-19 13:18:10 +02001917 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001918 vmcs_write16(sf->selector, save->selector);
1919 vmcs_writel(sf->base, save->base);
1920 vmcs_write32(sf->limit, save->limit);
1921 vmcs_write32(sf->ar_bytes, save->ar);
1922 } else {
1923 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1924 << AR_DPL_SHIFT;
1925 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1926 }
1927}
1928
1929static void enter_pmode(struct kvm_vcpu *vcpu)
1930{
1931 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001932 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001933
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001934 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001935 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936
Avi Kivity2fb92db2011-04-27 19:42:18 +03001937 vmx_segment_cache_clear(vmx);
1938
Avi Kivityd0ba64f2011-01-03 14:28:51 +02001939 vmcs_write16(GUEST_TR_SELECTOR, vmx->rmode.tr.selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001940 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
1941 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
1942 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943
1944 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03001945 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1946 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001947 vmcs_writel(GUEST_RFLAGS, flags);
1948
Rusty Russell66aee912007-07-17 23:34:16 +10001949 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1950 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001951
1952 update_exception_bitmap(vcpu);
1953
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001954 if (emulate_invalid_guest_state)
1955 return;
1956
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001957 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
1958 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
1959 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
1960 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961
Avi Kivity2fb92db2011-04-27 19:42:18 +03001962 vmx_segment_cache_clear(vmx);
1963
Avi Kivity6aa8b732006-12-10 02:21:36 -08001964 vmcs_write16(GUEST_SS_SELECTOR, 0);
1965 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1966
1967 vmcs_write16(GUEST_CS_SELECTOR,
1968 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1969 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1970}
1971
Mike Dayd77c26f2007-10-08 09:02:08 -04001972static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001974 if (!kvm->arch.tss_addr) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001975 struct kvm_memslots *slots;
1976 gfn_t base_gfn;
1977
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08001978 slots = kvm_memslots(kvm);
Avi Kivityf495c6e2010-06-10 17:21:29 +03001979 base_gfn = slots->memslots[0].base_gfn +
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001980 kvm->memslots->memslots[0].npages - 3;
Izik Eiduscbc94022007-10-25 00:29:55 +02001981 return base_gfn << PAGE_SHIFT;
1982 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001983 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001984}
1985
1986static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1987{
1988 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1989
1990 save->selector = vmcs_read16(sf->selector);
1991 save->base = vmcs_readl(sf->base);
1992 save->limit = vmcs_read32(sf->limit);
1993 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001994 vmcs_write16(sf->selector, save->base >> 4);
Gleb Natapov444e8632010-12-27 17:25:04 +02001995 vmcs_write32(sf->base, save->base & 0xffff0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001996 vmcs_write32(sf->limit, 0xffff);
1997 vmcs_write32(sf->ar_bytes, 0xf3);
Gleb Natapov444e8632010-12-27 17:25:04 +02001998 if (save->base & 0xf)
1999 printk_once(KERN_WARNING "kvm: segment base is not paragraph"
2000 " aligned when entering protected mode (seg=%d)",
2001 seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002002}
2003
2004static void enter_rmode(struct kvm_vcpu *vcpu)
2005{
2006 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002007 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002008
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002009 if (enable_unrestricted_guest)
2010 return;
2011
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002012 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002013 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002014
Gleb Natapov776e58e2011-03-13 12:34:27 +02002015 /*
2016 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
2017 * vcpu. Call it here with phys address pointing 16M below 4G.
2018 */
2019 if (!vcpu->kvm->arch.tss_addr) {
2020 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
2021 "called before entering vcpu\n");
2022 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
2023 vmx_set_tss_addr(vcpu->kvm, 0xfeffd000);
2024 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2025 }
2026
Avi Kivity2fb92db2011-04-27 19:42:18 +03002027 vmx_segment_cache_clear(vmx);
2028
Avi Kivityd0ba64f2011-01-03 14:28:51 +02002029 vmx->rmode.tr.selector = vmcs_read16(GUEST_TR_SELECTOR);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002030 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002031 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
2032
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002033 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002034 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
2035
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002036 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002037 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2038
2039 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002040 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002041
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002042 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002043
2044 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10002045 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002046 update_exception_bitmap(vcpu);
2047
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002048 if (emulate_invalid_guest_state)
2049 goto continue_rmode;
2050
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
2052 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
2053 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
2054
2055 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08002056 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02002057 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
2058 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002059 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
2060
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002061 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
2062 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
2063 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
2064 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03002065
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002066continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08002067 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002068}
2069
Amit Shah401d10d2009-02-20 22:53:37 +05302070static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
2071{
2072 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002073 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
2074
2075 if (!msr)
2076 return;
Amit Shah401d10d2009-02-20 22:53:37 +05302077
Avi Kivity44ea2b12009-09-06 15:55:37 +03002078 /*
2079 * Force kernel_gs_base reloading before EFER changes, as control
2080 * of this msr depends on is_long_mode().
2081 */
2082 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02002083 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05302084 if (efer & EFER_LMA) {
2085 vmcs_write32(VM_ENTRY_CONTROLS,
2086 vmcs_read32(VM_ENTRY_CONTROLS) |
2087 VM_ENTRY_IA32E_MODE);
2088 msr->data = efer;
2089 } else {
2090 vmcs_write32(VM_ENTRY_CONTROLS,
2091 vmcs_read32(VM_ENTRY_CONTROLS) &
2092 ~VM_ENTRY_IA32E_MODE);
2093
2094 msr->data = efer & ~EFER_LME;
2095 }
2096 setup_msrs(vmx);
2097}
2098
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002099#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002100
2101static void enter_lmode(struct kvm_vcpu *vcpu)
2102{
2103 u32 guest_tr_ar;
2104
Avi Kivity2fb92db2011-04-27 19:42:18 +03002105 vmx_segment_cache_clear(to_vmx(vcpu));
2106
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
2108 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
2109 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002110 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111 vmcs_write32(GUEST_TR_AR_BYTES,
2112 (guest_tr_ar & ~AR_TYPE_MASK)
2113 | AR_TYPE_BUSY_64_TSS);
2114 }
Avi Kivityda38f432010-07-06 11:30:49 +03002115 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002116}
2117
2118static void exit_lmode(struct kvm_vcpu *vcpu)
2119{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120 vmcs_write32(VM_ENTRY_CONTROLS,
2121 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03002122 & ~VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03002123 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002124}
2125
2126#endif
2127
Sheng Yang2384d2b2008-01-17 15:14:33 +08002128static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
2129{
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002130 vpid_sync_context(to_vmx(vcpu));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08002131 if (enable_ept) {
2132 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2133 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08002134 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08002135 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08002136}
2137
Avi Kivitye8467fd2009-12-29 18:43:06 +02002138static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2139{
2140 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
2141
2142 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
2143 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
2144}
2145
Avi Kivityaff48ba2010-12-05 18:56:11 +02002146static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
2147{
2148 if (enable_ept && is_paging(vcpu))
2149 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2150 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
2151}
2152
Anthony Liguori25c4c272007-04-27 09:29:21 +03002153static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002154{
Avi Kivityfc78f512009-12-07 12:16:48 +02002155 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
2156
2157 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
2158 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08002159}
2160
Sheng Yang14394422008-04-28 12:24:45 +08002161static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
2162{
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03002163 if (!test_bit(VCPU_EXREG_PDPTR,
2164 (unsigned long *)&vcpu->arch.regs_dirty))
2165 return;
2166
Sheng Yang14394422008-04-28 12:24:45 +08002167 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Joerg Roedelff03a072010-09-10 17:30:57 +02002168 vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]);
2169 vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]);
2170 vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]);
2171 vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08002172 }
2173}
2174
Avi Kivity8f5d5492009-05-31 18:41:29 +03002175static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
2176{
2177 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Joerg Roedelff03a072010-09-10 17:30:57 +02002178 vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
2179 vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
2180 vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
2181 vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002182 }
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03002183
2184 __set_bit(VCPU_EXREG_PDPTR,
2185 (unsigned long *)&vcpu->arch.regs_avail);
2186 __set_bit(VCPU_EXREG_PDPTR,
2187 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002188}
2189
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002190static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08002191
2192static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
2193 unsigned long cr0,
2194 struct kvm_vcpu *vcpu)
2195{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03002196 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
2197 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002198 if (!(cr0 & X86_CR0_PG)) {
2199 /* From paging/starting to nonpaging */
2200 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08002201 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08002202 (CPU_BASED_CR3_LOAD_EXITING |
2203 CPU_BASED_CR3_STORE_EXITING));
2204 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002205 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002206 } else if (!is_paging(vcpu)) {
2207 /* From nonpaging to paging */
2208 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08002209 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08002210 ~(CPU_BASED_CR3_LOAD_EXITING |
2211 CPU_BASED_CR3_STORE_EXITING));
2212 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002213 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002214 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08002215
2216 if (!(cr0 & X86_CR0_WP))
2217 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08002218}
2219
Avi Kivity6aa8b732006-12-10 02:21:36 -08002220static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2221{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002222 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002223 unsigned long hw_cr0;
2224
2225 if (enable_unrestricted_guest)
2226 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
2227 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
2228 else
2229 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08002230
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002231 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232 enter_pmode(vcpu);
2233
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002234 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002235 enter_rmode(vcpu);
2236
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002237#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002238 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10002239 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10002241 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002242 exit_lmode(vcpu);
2243 }
2244#endif
2245
Avi Kivity089d0342009-03-23 18:26:32 +02002246 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002247 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
2248
Avi Kivity02daab22009-12-30 12:40:26 +02002249 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02002250 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02002251
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08002253 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002254 vcpu->arch.cr0 = cr0;
Avi Kivity69c73022011-03-07 15:26:44 +02002255 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256}
2257
Sheng Yang14394422008-04-28 12:24:45 +08002258static u64 construct_eptp(unsigned long root_hpa)
2259{
2260 u64 eptp;
2261
2262 /* TODO write the value reading from MSR */
2263 eptp = VMX_EPT_DEFAULT_MT |
2264 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
2265 eptp |= (root_hpa & PAGE_MASK);
2266
2267 return eptp;
2268}
2269
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
2271{
Sheng Yang14394422008-04-28 12:24:45 +08002272 unsigned long guest_cr3;
2273 u64 eptp;
2274
2275 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02002276 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08002277 eptp = construct_eptp(cr3);
2278 vmcs_write64(EPT_POINTER, eptp);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002279 guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
Sheng Yangb927a3c2009-07-21 10:42:48 +08002280 vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02002281 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002282 }
2283
Sheng Yang2384d2b2008-01-17 15:14:33 +08002284 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002285 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002286}
2287
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002288static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002290 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08002291 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
2292
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002293 if (cr4 & X86_CR4_VMXE) {
2294 /*
2295 * To use VMXON (and later other VMX instructions), a guest
2296 * must first be able to turn on cr4.VMXE (see handle_vmon()).
2297 * So basically the check on whether to allow nested VMX
2298 * is here.
2299 */
2300 if (!nested_vmx_allowed(vcpu))
2301 return 1;
2302 } else if (to_vmx(vcpu)->nested.vmxon)
2303 return 1;
2304
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002305 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02002306 if (enable_ept) {
2307 if (!is_paging(vcpu)) {
2308 hw_cr4 &= ~X86_CR4_PAE;
2309 hw_cr4 |= X86_CR4_PSE;
2310 } else if (!(cr4 & X86_CR4_PAE)) {
2311 hw_cr4 &= ~X86_CR4_PAE;
2312 }
2313 }
Sheng Yang14394422008-04-28 12:24:45 +08002314
2315 vmcs_writel(CR4_READ_SHADOW, cr4);
2316 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002317 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318}
2319
Avi Kivity6aa8b732006-12-10 02:21:36 -08002320static void vmx_get_segment(struct kvm_vcpu *vcpu,
2321 struct kvm_segment *var, int seg)
2322{
Avi Kivitya9179492011-01-03 14:28:52 +02002323 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitya9179492011-01-03 14:28:52 +02002324 struct kvm_save_segment *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325 u32 ar;
2326
Avi Kivitya9179492011-01-03 14:28:52 +02002327 if (vmx->rmode.vm86_active
2328 && (seg == VCPU_SREG_TR || seg == VCPU_SREG_ES
2329 || seg == VCPU_SREG_DS || seg == VCPU_SREG_FS
2330 || seg == VCPU_SREG_GS)
2331 && !emulate_invalid_guest_state) {
2332 switch (seg) {
2333 case VCPU_SREG_TR: save = &vmx->rmode.tr; break;
2334 case VCPU_SREG_ES: save = &vmx->rmode.es; break;
2335 case VCPU_SREG_DS: save = &vmx->rmode.ds; break;
2336 case VCPU_SREG_FS: save = &vmx->rmode.fs; break;
2337 case VCPU_SREG_GS: save = &vmx->rmode.gs; break;
2338 default: BUG();
2339 }
2340 var->selector = save->selector;
2341 var->base = save->base;
2342 var->limit = save->limit;
2343 ar = save->ar;
2344 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03002345 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivitya9179492011-01-03 14:28:52 +02002346 goto use_saved_rmode_seg;
2347 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03002348 var->base = vmx_read_guest_seg_base(vmx, seg);
2349 var->limit = vmx_read_guest_seg_limit(vmx, seg);
2350 var->selector = vmx_read_guest_seg_selector(vmx, seg);
2351 ar = vmx_read_guest_seg_ar(vmx, seg);
Avi Kivitya9179492011-01-03 14:28:52 +02002352use_saved_rmode_seg:
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02002353 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002354 ar = 0;
2355 var->type = ar & 15;
2356 var->s = (ar >> 4) & 1;
2357 var->dpl = (ar >> 5) & 3;
2358 var->present = (ar >> 7) & 1;
2359 var->avl = (ar >> 12) & 1;
2360 var->l = (ar >> 13) & 1;
2361 var->db = (ar >> 14) & 1;
2362 var->g = (ar >> 15) & 1;
2363 var->unusable = (ar >> 16) & 1;
2364}
2365
Avi Kivitya9179492011-01-03 14:28:52 +02002366static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2367{
Avi Kivitya9179492011-01-03 14:28:52 +02002368 struct kvm_segment s;
2369
2370 if (to_vmx(vcpu)->rmode.vm86_active) {
2371 vmx_get_segment(vcpu, &s, seg);
2372 return s.base;
2373 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03002374 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02002375}
2376
Avi Kivity69c73022011-03-07 15:26:44 +02002377static int __vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02002378{
Avi Kivity3eeb3282010-01-21 15:31:48 +02002379 if (!is_protmode(vcpu))
Izik Eidus2e4d2652008-03-24 19:38:34 +02002380 return 0;
2381
Avi Kivityf4c63e52011-03-07 14:54:28 +02002382 if (!is_long_mode(vcpu)
2383 && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
Izik Eidus2e4d2652008-03-24 19:38:34 +02002384 return 3;
2385
Avi Kivity2fb92db2011-04-27 19:42:18 +03002386 return vmx_read_guest_seg_selector(to_vmx(vcpu), VCPU_SREG_CS) & 3;
Izik Eidus2e4d2652008-03-24 19:38:34 +02002387}
2388
Avi Kivity69c73022011-03-07 15:26:44 +02002389static int vmx_get_cpl(struct kvm_vcpu *vcpu)
2390{
2391 if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
2392 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
2393 to_vmx(vcpu)->cpl = __vmx_get_cpl(vcpu);
2394 }
2395 return to_vmx(vcpu)->cpl;
2396}
2397
2398
Avi Kivity653e3102007-05-07 10:55:37 +03002399static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002400{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002401 u32 ar;
2402
Avi Kivity653e3102007-05-07 10:55:37 +03002403 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002404 ar = 1 << 16;
2405 else {
2406 ar = var->type & 15;
2407 ar |= (var->s & 1) << 4;
2408 ar |= (var->dpl & 3) << 5;
2409 ar |= (var->present & 1) << 7;
2410 ar |= (var->avl & 1) << 12;
2411 ar |= (var->l & 1) << 13;
2412 ar |= (var->db & 1) << 14;
2413 ar |= (var->g & 1) << 15;
2414 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08002415 if (ar == 0) /* a 0 value means unusable */
2416 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03002417
2418 return ar;
2419}
2420
2421static void vmx_set_segment(struct kvm_vcpu *vcpu,
2422 struct kvm_segment *var, int seg)
2423{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002424 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03002425 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2426 u32 ar;
2427
Avi Kivity2fb92db2011-04-27 19:42:18 +03002428 vmx_segment_cache_clear(vmx);
2429
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002430 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
Gleb Natapova8ba6c22011-02-21 12:07:58 +02002431 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002432 vmx->rmode.tr.selector = var->selector;
2433 vmx->rmode.tr.base = var->base;
2434 vmx->rmode.tr.limit = var->limit;
2435 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03002436 return;
2437 }
2438 vmcs_writel(sf->base, var->base);
2439 vmcs_write32(sf->limit, var->limit);
2440 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002441 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03002442 /*
2443 * Hack real-mode segments into vm86 compatibility.
2444 */
2445 if (var->base == 0xffff0000 && var->selector == 0xf000)
2446 vmcs_writel(sf->base, 0xf0000);
2447 ar = 0xf3;
2448 } else
2449 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002450
2451 /*
2452 * Fix the "Accessed" bit in AR field of segment registers for older
2453 * qemu binaries.
2454 * IA32 arch specifies that at the time of processor reset the
2455 * "Accessed" bit in the AR field of segment registers is 1. And qemu
2456 * is setting it to 0 in the usedland code. This causes invalid guest
2457 * state vmexit when "unrestricted guest" mode is turned on.
2458 * Fix for this setup issue in cpu_reset is being pushed in the qemu
2459 * tree. Newer qemu binaries with that qemu fix would not need this
2460 * kvm hack.
2461 */
2462 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
2463 ar |= 0x1; /* Accessed */
2464
Avi Kivity6aa8b732006-12-10 02:21:36 -08002465 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity69c73022011-03-07 15:26:44 +02002466 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002467}
2468
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2470{
Avi Kivity2fb92db2011-04-27 19:42:18 +03002471 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002472
2473 *db = (ar >> 14) & 1;
2474 *l = (ar >> 13) & 1;
2475}
2476
Gleb Natapov89a27f42010-02-16 10:51:48 +02002477static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002478{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002479 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
2480 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002481}
2482
Gleb Natapov89a27f42010-02-16 10:51:48 +02002483static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002484{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002485 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
2486 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002487}
2488
Gleb Natapov89a27f42010-02-16 10:51:48 +02002489static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002490{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002491 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
2492 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002493}
2494
Gleb Natapov89a27f42010-02-16 10:51:48 +02002495static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002497 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
2498 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002499}
2500
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002501static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
2502{
2503 struct kvm_segment var;
2504 u32 ar;
2505
2506 vmx_get_segment(vcpu, &var, seg);
2507 ar = vmx_segment_access_rights(&var);
2508
2509 if (var.base != (var.selector << 4))
2510 return false;
2511 if (var.limit != 0xffff)
2512 return false;
2513 if (ar != 0xf3)
2514 return false;
2515
2516 return true;
2517}
2518
2519static bool code_segment_valid(struct kvm_vcpu *vcpu)
2520{
2521 struct kvm_segment cs;
2522 unsigned int cs_rpl;
2523
2524 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2525 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
2526
Avi Kivity1872a3f2009-01-04 23:26:52 +02002527 if (cs.unusable)
2528 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002529 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
2530 return false;
2531 if (!cs.s)
2532 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002533 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002534 if (cs.dpl > cs_rpl)
2535 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002536 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002537 if (cs.dpl != cs_rpl)
2538 return false;
2539 }
2540 if (!cs.present)
2541 return false;
2542
2543 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
2544 return true;
2545}
2546
2547static bool stack_segment_valid(struct kvm_vcpu *vcpu)
2548{
2549 struct kvm_segment ss;
2550 unsigned int ss_rpl;
2551
2552 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2553 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
2554
Avi Kivity1872a3f2009-01-04 23:26:52 +02002555 if (ss.unusable)
2556 return true;
2557 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002558 return false;
2559 if (!ss.s)
2560 return false;
2561 if (ss.dpl != ss_rpl) /* DPL != RPL */
2562 return false;
2563 if (!ss.present)
2564 return false;
2565
2566 return true;
2567}
2568
2569static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
2570{
2571 struct kvm_segment var;
2572 unsigned int rpl;
2573
2574 vmx_get_segment(vcpu, &var, seg);
2575 rpl = var.selector & SELECTOR_RPL_MASK;
2576
Avi Kivity1872a3f2009-01-04 23:26:52 +02002577 if (var.unusable)
2578 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002579 if (!var.s)
2580 return false;
2581 if (!var.present)
2582 return false;
2583 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
2584 if (var.dpl < rpl) /* DPL < RPL */
2585 return false;
2586 }
2587
2588 /* TODO: Add other members to kvm_segment_field to allow checking for other access
2589 * rights flags
2590 */
2591 return true;
2592}
2593
2594static bool tr_valid(struct kvm_vcpu *vcpu)
2595{
2596 struct kvm_segment tr;
2597
2598 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
2599
Avi Kivity1872a3f2009-01-04 23:26:52 +02002600 if (tr.unusable)
2601 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002602 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2603 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002604 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002605 return false;
2606 if (!tr.present)
2607 return false;
2608
2609 return true;
2610}
2611
2612static bool ldtr_valid(struct kvm_vcpu *vcpu)
2613{
2614 struct kvm_segment ldtr;
2615
2616 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2617
Avi Kivity1872a3f2009-01-04 23:26:52 +02002618 if (ldtr.unusable)
2619 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002620 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2621 return false;
2622 if (ldtr.type != 2)
2623 return false;
2624 if (!ldtr.present)
2625 return false;
2626
2627 return true;
2628}
2629
2630static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2631{
2632 struct kvm_segment cs, ss;
2633
2634 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2635 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2636
2637 return ((cs.selector & SELECTOR_RPL_MASK) ==
2638 (ss.selector & SELECTOR_RPL_MASK));
2639}
2640
2641/*
2642 * Check if guest state is valid. Returns true if valid, false if
2643 * not.
2644 * We assume that registers are always usable
2645 */
2646static bool guest_state_valid(struct kvm_vcpu *vcpu)
2647{
2648 /* real mode guest state checks */
Avi Kivity3eeb3282010-01-21 15:31:48 +02002649 if (!is_protmode(vcpu)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002650 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2651 return false;
2652 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2653 return false;
2654 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2655 return false;
2656 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2657 return false;
2658 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2659 return false;
2660 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2661 return false;
2662 } else {
2663 /* protected mode guest state checks */
2664 if (!cs_ss_rpl_check(vcpu))
2665 return false;
2666 if (!code_segment_valid(vcpu))
2667 return false;
2668 if (!stack_segment_valid(vcpu))
2669 return false;
2670 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2671 return false;
2672 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2673 return false;
2674 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2675 return false;
2676 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2677 return false;
2678 if (!tr_valid(vcpu))
2679 return false;
2680 if (!ldtr_valid(vcpu))
2681 return false;
2682 }
2683 /* TODO:
2684 * - Add checks on RIP
2685 * - Add checks on RFLAGS
2686 */
2687
2688 return true;
2689}
2690
Mike Dayd77c26f2007-10-08 09:02:08 -04002691static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002693 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02002694 u16 data = 0;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002695 int r, idx, ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002696
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002697 idx = srcu_read_lock(&kvm->srcu);
2698 fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002699 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2700 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002701 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002702 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002703 r = kvm_write_guest_page(kvm, fn++, &data,
2704 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002705 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002706 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002707 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2708 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002709 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002710 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2711 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002712 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002713 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002714 r = kvm_write_guest_page(kvm, fn, &data,
2715 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2716 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002717 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002718 goto out;
2719
2720 ret = 1;
2721out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002722 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002723 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724}
2725
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002726static int init_rmode_identity_map(struct kvm *kvm)
2727{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002728 int i, idx, r, ret;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002729 pfn_t identity_map_pfn;
2730 u32 tmp;
2731
Avi Kivity089d0342009-03-23 18:26:32 +02002732 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002733 return 1;
2734 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2735 printk(KERN_ERR "EPT: identity-mapping pagetable "
2736 "haven't been allocated!\n");
2737 return 0;
2738 }
2739 if (likely(kvm->arch.ept_identity_pagetable_done))
2740 return 1;
2741 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002742 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002743 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002744 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2745 if (r < 0)
2746 goto out;
2747 /* Set up identity-mapping pagetable for EPT in real mode */
2748 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2749 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2750 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2751 r = kvm_write_guest_page(kvm, identity_map_pfn,
2752 &tmp, i * sizeof(tmp), sizeof(tmp));
2753 if (r < 0)
2754 goto out;
2755 }
2756 kvm->arch.ept_identity_pagetable_done = true;
2757 ret = 1;
2758out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002759 srcu_read_unlock(&kvm->srcu, idx);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002760 return ret;
2761}
2762
Avi Kivity6aa8b732006-12-10 02:21:36 -08002763static void seg_setup(int seg)
2764{
2765 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002766 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002767
2768 vmcs_write16(sf->selector, 0);
2769 vmcs_writel(sf->base, 0);
2770 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002771 if (enable_unrestricted_guest) {
2772 ar = 0x93;
2773 if (seg == VCPU_SREG_CS)
2774 ar |= 0x08; /* code segment */
2775 } else
2776 ar = 0xf3;
2777
2778 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779}
2780
Sheng Yangf78e0e22007-10-29 09:40:42 +08002781static int alloc_apic_access_page(struct kvm *kvm)
2782{
2783 struct kvm_userspace_memory_region kvm_userspace_mem;
2784 int r = 0;
2785
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002786 mutex_lock(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002787 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002788 goto out;
2789 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2790 kvm_userspace_mem.flags = 0;
2791 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2792 kvm_userspace_mem.memory_size = PAGE_SIZE;
2793 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2794 if (r)
2795 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002796
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002797 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002798out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002799 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002800 return r;
2801}
2802
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002803static int alloc_identity_pagetable(struct kvm *kvm)
2804{
2805 struct kvm_userspace_memory_region kvm_userspace_mem;
2806 int r = 0;
2807
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002808 mutex_lock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002809 if (kvm->arch.ept_identity_pagetable)
2810 goto out;
2811 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2812 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002813 kvm_userspace_mem.guest_phys_addr =
2814 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002815 kvm_userspace_mem.memory_size = PAGE_SIZE;
2816 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2817 if (r)
2818 goto out;
2819
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002820 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
Sheng Yangb927a3c2009-07-21 10:42:48 +08002821 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002822out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002823 mutex_unlock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002824 return r;
2825}
2826
Sheng Yang2384d2b2008-01-17 15:14:33 +08002827static void allocate_vpid(struct vcpu_vmx *vmx)
2828{
2829 int vpid;
2830
2831 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002832 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002833 return;
2834 spin_lock(&vmx_vpid_lock);
2835 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2836 if (vpid < VMX_NR_VPIDS) {
2837 vmx->vpid = vpid;
2838 __set_bit(vpid, vmx_vpid_bitmap);
2839 }
2840 spin_unlock(&vmx_vpid_lock);
2841}
2842
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08002843static void free_vpid(struct vcpu_vmx *vmx)
2844{
2845 if (!enable_vpid)
2846 return;
2847 spin_lock(&vmx_vpid_lock);
2848 if (vmx->vpid != 0)
2849 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
2850 spin_unlock(&vmx_vpid_lock);
2851}
2852
Avi Kivity58972972009-02-24 22:26:47 +02002853static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002854{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002855 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002856
2857 if (!cpu_has_vmx_msr_bitmap())
2858 return;
2859
2860 /*
2861 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2862 * have the write-low and read-high bitmap offsets the wrong way round.
2863 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2864 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002865 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002866 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2867 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002868 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2869 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002870 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2871 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002872 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002873}
2874
Avi Kivity58972972009-02-24 22:26:47 +02002875static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2876{
2877 if (!longmode_only)
2878 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2879 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2880}
2881
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882/*
2883 * Sets up the vmcs for emulated real mode.
2884 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002885static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886{
Sheng Yang468d4722008-10-09 16:01:55 +08002887 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002888 u32 junk;
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10002889 u64 host_pat;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002890 unsigned long a;
Gleb Natapov89a27f42010-02-16 10:51:48 +02002891 struct desc_ptr dt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002893 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002894 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002895
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002897 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2898 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899
Sheng Yang25c5f222008-03-28 13:18:56 +08002900 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002901 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002902
Avi Kivity6aa8b732006-12-10 02:21:36 -08002903 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2904
Avi Kivity6aa8b732006-12-10 02:21:36 -08002905 /* Control */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002906 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2907 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002908
2909 exec_control = vmcs_config.cpu_based_exec_ctrl;
2910 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2911 exec_control &= ~CPU_BASED_TPR_SHADOW;
2912#ifdef CONFIG_X86_64
2913 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2914 CPU_BASED_CR8_LOAD_EXITING;
2915#endif
2916 }
Avi Kivity089d0342009-03-23 18:26:32 +02002917 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002918 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002919 CPU_BASED_CR3_LOAD_EXITING |
2920 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002921 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002922
Sheng Yang83ff3b92007-11-21 14:33:25 +08002923 if (cpu_has_secondary_exec_ctrls()) {
2924 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2925 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2926 exec_control &=
2927 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002928 if (vmx->vpid == 0)
2929 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang046d8712009-11-27 16:46:26 +08002930 if (!enable_ept) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002931 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang046d8712009-11-27 16:46:26 +08002932 enable_unrestricted_guest = 0;
2933 }
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002934 if (!enable_unrestricted_guest)
2935 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002936 if (!ple_gap)
2937 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002938 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2939 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002940
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002941 if (ple_gap) {
2942 vmcs_write32(PLE_GAP, ple_gap);
2943 vmcs_write32(PLE_WINDOW, ple_window);
2944 }
2945
Avi Kivityc7addb92007-09-16 18:58:32 +02002946 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2947 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002948 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2949
Avi Kivity1c11e712010-05-03 16:05:44 +03002950 vmcs_writel(HOST_CR0, read_cr0() | X86_CR0_TS); /* 22.2.3 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002951 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2952 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2953
2954 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2955 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2956 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity9581d442010-10-19 16:46:55 +02002957 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
2958 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002960#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002961 rdmsrl(MSR_FS_BASE, a);
2962 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2963 rdmsrl(MSR_GS_BASE, a);
2964 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2965#else
2966 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2967 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2968#endif
2969
2970 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2971
Wei Yongjunec687982010-03-05 12:11:48 +08002972 native_store_idt(&dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02002973 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002974
Mike Dayd77c26f2007-10-08 09:02:08 -04002975 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002976 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002977 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2978 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002979 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03002980 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002981 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002982
2983 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2984 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2985 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2986 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2987 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2988 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2989
Sheng Yang468d4722008-10-09 16:01:55 +08002990 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2991 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2992 host_pat = msr_low | ((u64) msr_high << 32);
2993 vmcs_write64(HOST_IA32_PAT, host_pat);
2994 }
2995 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2996 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2997 host_pat = msr_low | ((u64) msr_high << 32);
2998 /* Write the default value follow host pat */
2999 vmcs_write64(GUEST_IA32_PAT, host_pat);
3000 /* Keep arch.pat sync with GUEST_IA32_PAT */
3001 vmx->vcpu.arch.pat = host_pat;
3002 }
3003
Avi Kivity6aa8b732006-12-10 02:21:36 -08003004 for (i = 0; i < NR_VMX_MSR; ++i) {
3005 u32 index = vmx_msr_index[i];
3006 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003007 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003008
3009 if (rdmsr_safe(index, &data_low, &data_high) < 0)
3010 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08003011 if (wrmsr_safe(index, data_low, data_high) < 0)
3012 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03003013 vmx->guest_msrs[j].index = i;
3014 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02003015 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003016 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003017 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003018
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03003019 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003020
3021 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03003022 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
3023
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003024 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Avi Kivity4c386092009-12-07 12:26:18 +02003025 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
Avi Kivityce03e4f2009-12-07 12:29:14 +02003026 if (enable_ept)
3027 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Avi Kivity4c386092009-12-07 12:26:18 +02003028 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003029
Zachary Amsden99e3e302010-08-19 22:07:17 -10003030 kvm_write_tsc(&vmx->vcpu, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003031
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003032 return 0;
3033}
3034
3035static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
3036{
3037 struct vcpu_vmx *vmx = to_vmx(vcpu);
3038 u64 msr;
Xiao Guangrong4b9d3a02010-06-08 10:15:51 +08003039 int ret;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003040
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003041 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003042
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003043 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003044
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003045 vmx->soft_vnmi_blocked = 0;
3046
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003047 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003048 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003049 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03003050 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003051 msr |= MSR_IA32_APICBASE_BSP;
3052 kvm_set_apic_base(&vmx->vcpu, msr);
3053
Jan Kiszka10ab25c2010-05-25 16:01:50 +02003054 ret = fx_init(&vmx->vcpu);
3055 if (ret != 0)
3056 goto out;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003057
Avi Kivity2fb92db2011-04-27 19:42:18 +03003058 vmx_segment_cache_clear(vmx);
3059
Avi Kivity5706be02008-08-20 15:07:31 +03003060 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003061 /*
3062 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
3063 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
3064 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03003065 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003066 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
3067 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
3068 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003069 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
3070 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003071 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003072
3073 seg_setup(VCPU_SREG_DS);
3074 seg_setup(VCPU_SREG_ES);
3075 seg_setup(VCPU_SREG_FS);
3076 seg_setup(VCPU_SREG_GS);
3077 seg_setup(VCPU_SREG_SS);
3078
3079 vmcs_write16(GUEST_TR_SELECTOR, 0);
3080 vmcs_writel(GUEST_TR_BASE, 0);
3081 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
3082 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3083
3084 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
3085 vmcs_writel(GUEST_LDTR_BASE, 0);
3086 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
3087 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
3088
3089 vmcs_write32(GUEST_SYSENTER_CS, 0);
3090 vmcs_writel(GUEST_SYSENTER_ESP, 0);
3091 vmcs_writel(GUEST_SYSENTER_EIP, 0);
3092
3093 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03003094 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003095 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003096 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003097 kvm_rip_write(vcpu, 0);
3098 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003099
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003100 vmcs_writel(GUEST_DR7, 0x400);
3101
3102 vmcs_writel(GUEST_GDTR_BASE, 0);
3103 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
3104
3105 vmcs_writel(GUEST_IDTR_BASE, 0);
3106 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
3107
Anthony Liguori443381a2010-12-06 10:53:38 -06003108 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003109 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
3110 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
3111
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003112 /* Special registers */
3113 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
3114
3115 setup_msrs(vmx);
3116
Avi Kivity6aa8b732006-12-10 02:21:36 -08003117 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
3118
Sheng Yangf78e0e22007-10-29 09:40:42 +08003119 if (cpu_has_vmx_tpr_shadow()) {
3120 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
3121 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
3122 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09003123 __pa(vmx->vcpu.arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08003124 vmcs_write32(TPR_THRESHOLD, 0);
3125 }
3126
3127 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
3128 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003129 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003130
Sheng Yang2384d2b2008-01-17 15:14:33 +08003131 if (vmx->vpid != 0)
3132 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
3133
Eduardo Habkostfa400522009-10-24 02:49:58 -02003134 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Avi Kivity4d4ec082009-12-29 18:07:30 +02003135 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003136 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003137 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003138 vmx_fpu_activate(&vmx->vcpu);
3139 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003140
Gui Jianfengb9d762f2010-06-07 10:32:29 +08003141 vpid_sync_context(vmx);
Sheng Yang2384d2b2008-01-17 15:14:33 +08003142
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003143 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003144
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003145 /* HACK: Don't enable emulation on guest boot/reset */
3146 vmx->emulation_required = 0;
3147
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148out:
3149 return ret;
3150}
3151
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003152static void enable_irq_window(struct kvm_vcpu *vcpu)
3153{
3154 u32 cpu_based_vm_exec_control;
3155
3156 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3157 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
3158 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3159}
3160
3161static void enable_nmi_window(struct kvm_vcpu *vcpu)
3162{
3163 u32 cpu_based_vm_exec_control;
3164
3165 if (!cpu_has_virtual_nmis()) {
3166 enable_irq_window(vcpu);
3167 return;
3168 }
3169
Avi Kivity30bd0c42010-11-01 23:20:48 +02003170 if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
3171 enable_irq_window(vcpu);
3172 return;
3173 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003174 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3175 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
3176 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3177}
3178
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003179static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03003180{
Avi Kivity9c8cba32007-11-22 11:42:59 +02003181 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003182 uint32_t intr;
3183 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02003184
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003185 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003186
Avi Kivityfa89a812008-09-01 15:57:51 +03003187 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003188 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003189 int inc_eip = 0;
3190 if (vcpu->arch.interrupt.soft)
3191 inc_eip = vcpu->arch.event_exit_inst_len;
3192 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003193 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003194 return;
3195 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003196 intr = irq | INTR_INFO_VALID_MASK;
3197 if (vcpu->arch.interrupt.soft) {
3198 intr |= INTR_TYPE_SOFT_INTR;
3199 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3200 vmx->vcpu.arch.event_exit_inst_len);
3201 } else
3202 intr |= INTR_TYPE_EXT_INTR;
3203 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Anthony Liguori443381a2010-12-06 10:53:38 -06003204 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003205}
3206
Sheng Yangf08864b2008-05-15 18:23:25 +08003207static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
3208{
Jan Kiszka66a5a342008-09-26 09:30:51 +02003209 struct vcpu_vmx *vmx = to_vmx(vcpu);
3210
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003211 if (!cpu_has_virtual_nmis()) {
3212 /*
3213 * Tracking the NMI-blocked state in software is built upon
3214 * finding the next open IRQ window. This, in turn, depends on
3215 * well-behaving guests: They have to keep IRQs disabled at
3216 * least as long as the NMI handler runs. Otherwise we may
3217 * cause NMI nesting, maybe breaking the guest. But as this is
3218 * highly unlikely, we can live with the residual risk.
3219 */
3220 vmx->soft_vnmi_blocked = 1;
3221 vmx->vnmi_blocked_time = 0;
3222 }
3223
Jan Kiszka487b3912008-09-26 09:30:56 +02003224 ++vcpu->stat.nmi_injections;
Avi Kivity9d58b932011-03-07 16:52:07 +02003225 vmx->nmi_known_unmasked = false;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003226 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003227 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003228 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02003229 return;
3230 }
Sheng Yangf08864b2008-05-15 18:23:25 +08003231 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
3232 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Anthony Liguori443381a2010-12-06 10:53:38 -06003233 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003234}
3235
Gleb Natapovc4282df2009-04-21 17:45:07 +03003236static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02003237{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003238 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03003239 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02003240
Gleb Natapovc4282df2009-04-21 17:45:07 +03003241 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
Avi Kivity30bd0c42010-11-01 23:20:48 +02003242 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
3243 | GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02003244}
3245
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003246static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
3247{
3248 if (!cpu_has_virtual_nmis())
3249 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02003250 if (to_vmx(vcpu)->nmi_known_unmasked)
3251 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03003252 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003253}
3254
3255static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3256{
3257 struct vcpu_vmx *vmx = to_vmx(vcpu);
3258
3259 if (!cpu_has_virtual_nmis()) {
3260 if (vmx->soft_vnmi_blocked != masked) {
3261 vmx->soft_vnmi_blocked = masked;
3262 vmx->vnmi_blocked_time = 0;
3263 }
3264 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02003265 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003266 if (masked)
3267 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3268 GUEST_INTR_STATE_NMI);
3269 else
3270 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3271 GUEST_INTR_STATE_NMI);
3272 }
3273}
3274
Gleb Natapov78646122009-03-23 12:12:11 +02003275static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
3276{
Gleb Natapovc4282df2009-04-21 17:45:07 +03003277 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
3278 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
3279 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02003280}
3281
Izik Eiduscbc94022007-10-25 00:29:55 +02003282static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
3283{
3284 int ret;
3285 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08003286 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02003287 .guest_phys_addr = addr,
3288 .memory_size = PAGE_SIZE * 3,
3289 .flags = 0,
3290 };
3291
3292 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
3293 if (ret)
3294 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003295 kvm->arch.tss_addr = addr;
Gleb Natapov93ea5382011-02-21 12:07:59 +02003296 if (!init_rmode_tss(kvm))
3297 return -ENOMEM;
3298
Izik Eiduscbc94022007-10-25 00:29:55 +02003299 return 0;
3300}
3301
Avi Kivity6aa8b732006-12-10 02:21:36 -08003302static int handle_rmode_exception(struct kvm_vcpu *vcpu,
3303 int vec, u32 err_code)
3304{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03003305 /*
3306 * Instruction with address size override prefix opcode 0x67
3307 * Cause the #SS fault with 0 error code in VM86 mode.
3308 */
3309 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Andre Przywara51d8b662010-12-21 11:12:02 +01003310 if (emulate_instruction(vcpu, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003311 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003312 /*
3313 * Forward all other exceptions that are valid in real mode.
3314 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
3315 * the required debugging infrastructure rework.
3316 */
3317 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003318 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003319 if (vcpu->guest_debug &
3320 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
3321 return 0;
3322 kvm_queue_exception(vcpu, vec);
3323 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003324 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01003325 /*
3326 * Update instruction length as we may reinject the exception
3327 * from user space while in guest debugging mode.
3328 */
3329 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
3330 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003331 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
3332 return 0;
3333 /* fall through */
3334 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003335 case OF_VECTOR:
3336 case BR_VECTOR:
3337 case UD_VECTOR:
3338 case DF_VECTOR:
3339 case SS_VECTOR:
3340 case GP_VECTOR:
3341 case MF_VECTOR:
3342 kvm_queue_exception(vcpu, vec);
3343 return 1;
3344 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003345 return 0;
3346}
3347
Andi Kleena0861c02009-06-08 17:37:09 +08003348/*
3349 * Trigger machine check on the host. We assume all the MSRs are already set up
3350 * by the CPU and that we still run on the same CPU as the MCE occurred on.
3351 * We pass a fake environment to the machine check handler because we want
3352 * the guest to be always treated like user space, no matter what context
3353 * it used internally.
3354 */
3355static void kvm_machine_check(void)
3356{
3357#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
3358 struct pt_regs regs = {
3359 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
3360 .flags = X86_EFLAGS_IF,
3361 };
3362
3363 do_machine_check(&regs, 0);
3364#endif
3365}
3366
Avi Kivity851ba692009-08-24 11:10:17 +03003367static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08003368{
3369 /* already handled by vcpu_run */
3370 return 1;
3371}
3372
Avi Kivity851ba692009-08-24 11:10:17 +03003373static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003374{
Avi Kivity1155f762007-11-22 11:30:47 +02003375 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003376 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003377 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003378 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003379 u32 vect_info;
3380 enum emulation_result er;
3381
Avi Kivity1155f762007-11-22 11:30:47 +02003382 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02003383 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003384
Andi Kleena0861c02009-06-08 17:37:09 +08003385 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03003386 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003387
Avi Kivity6aa8b732006-12-10 02:21:36 -08003388 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Avi Kivity65ac7262009-11-04 11:59:01 +02003389 !is_page_fault(intr_info)) {
3390 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3391 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
3392 vcpu->run->internal.ndata = 2;
3393 vcpu->run->internal.data[0] = vect_info;
3394 vcpu->run->internal.data[1] = intr_info;
3395 return 0;
3396 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003397
Jan Kiszkae4a41882008-09-26 09:30:46 +02003398 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02003399 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03003400
3401 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03003402 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03003403 return 1;
3404 }
3405
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003406 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01003407 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003408 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02003409 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003410 return 1;
3411 }
3412
Avi Kivity6aa8b732006-12-10 02:21:36 -08003413 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06003414 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003415 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
3416 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08003417 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02003418 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003419 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003420 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003421 trace_kvm_page_fault(cr2, error_code);
3422
Gleb Natapov3298b752009-05-11 13:35:46 +03003423 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03003424 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01003425 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426 }
3427
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003428 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08003429 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03003430 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003431 if (vcpu->arch.halt_request) {
3432 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03003433 return kvm_emulate_halt(vcpu);
3434 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003435 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03003436 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003437
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003438 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003439 switch (ex_no) {
3440 case DB_VECTOR:
3441 dr6 = vmcs_readl(EXIT_QUALIFICATION);
3442 if (!(vcpu->guest_debug &
3443 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
3444 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
3445 kvm_queue_exception(vcpu, DB_VECTOR);
3446 return 1;
3447 }
3448 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
3449 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
3450 /* fall through */
3451 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01003452 /*
3453 * Update instruction length as we may reinject #BP from
3454 * user space while in guest debugging mode. Reading it for
3455 * #DB as well causes no harm, it is not used in that case.
3456 */
3457 vmx->vcpu.arch.event_exit_inst_len =
3458 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003459 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03003460 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003461 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
3462 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003463 break;
3464 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003465 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
3466 kvm_run->ex.exception = ex_no;
3467 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003468 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003469 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003470 return 0;
3471}
3472
Avi Kivity851ba692009-08-24 11:10:17 +03003473static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003474{
Avi Kivity1165f5f2007-04-19 17:27:43 +03003475 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003476 return 1;
3477}
3478
Avi Kivity851ba692009-08-24 11:10:17 +03003479static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08003480{
Avi Kivity851ba692009-08-24 11:10:17 +03003481 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08003482 return 0;
3483}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003484
Avi Kivity851ba692009-08-24 11:10:17 +03003485static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003486{
He, Qingbfdaab02007-09-12 14:18:28 +08003487 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01003488 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02003489 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003490
He, Qingbfdaab02007-09-12 14:18:28 +08003491 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02003492 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03003493 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03003494
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003495 ++vcpu->stat.io_exits;
3496
3497 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01003498 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003499
3500 port = exit_qualification >> 16;
3501 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003502 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003503
3504 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003505}
3506
Ingo Molnar102d8322007-02-19 14:37:47 +02003507static void
3508vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3509{
3510 /*
3511 * Patch in the VMCALL instruction:
3512 */
3513 hypercall[0] = 0x0f;
3514 hypercall[1] = 0x01;
3515 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02003516}
3517
Avi Kivity851ba692009-08-24 11:10:17 +03003518static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003519{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003520 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003521 int cr;
3522 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03003523 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003524
He, Qingbfdaab02007-09-12 14:18:28 +08003525 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003526 cr = exit_qualification & 15;
3527 reg = (exit_qualification >> 8) & 15;
3528 switch ((exit_qualification >> 4) & 3) {
3529 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003530 val = kvm_register_read(vcpu, reg);
3531 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003532 switch (cr) {
3533 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03003534 err = kvm_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003535 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003536 return 1;
3537 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03003538 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003539 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003540 return 1;
3541 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03003542 err = kvm_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003543 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003544 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003545 case 8: {
3546 u8 cr8_prev = kvm_get_cr8(vcpu);
3547 u8 cr8 = kvm_register_read(vcpu, reg);
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003548 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003549 kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003550 if (irqchip_in_kernel(vcpu->kvm))
3551 return 1;
3552 if (cr8_prev <= cr8)
3553 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03003554 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003555 return 0;
3556 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003557 };
3558 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03003559 case 2: /* clts */
Avi Kivityedcafe32009-12-30 18:07:40 +02003560 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02003561 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03003562 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02003563 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03003564 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003565 case 1: /*mov from cr*/
3566 switch (cr) {
3567 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003568 val = kvm_read_cr3(vcpu);
3569 kvm_register_write(vcpu, reg, val);
3570 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003571 skip_emulated_instruction(vcpu);
3572 return 1;
3573 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003574 val = kvm_get_cr8(vcpu);
3575 kvm_register_write(vcpu, reg, val);
3576 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003577 skip_emulated_instruction(vcpu);
3578 return 1;
3579 }
3580 break;
3581 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02003582 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02003583 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02003584 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003585
3586 skip_emulated_instruction(vcpu);
3587 return 1;
3588 default:
3589 break;
3590 }
Avi Kivity851ba692009-08-24 11:10:17 +03003591 vcpu->run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10003592 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08003593 (int)(exit_qualification >> 4) & 3, cr);
3594 return 0;
3595}
3596
Avi Kivity851ba692009-08-24 11:10:17 +03003597static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003598{
He, Qingbfdaab02007-09-12 14:18:28 +08003599 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003600 int dr, reg;
3601
Jan Kiszkaf2483412010-01-20 18:20:20 +01003602 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03003603 if (!kvm_require_cpl(vcpu, 0))
3604 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003605 dr = vmcs_readl(GUEST_DR7);
3606 if (dr & DR7_GD) {
3607 /*
3608 * As the vm-exit takes precedence over the debug trap, we
3609 * need to emulate the latter, either for the host or the
3610 * guest debugging itself.
3611 */
3612 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03003613 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
3614 vcpu->run->debug.arch.dr7 = dr;
3615 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003616 vmcs_readl(GUEST_CS_BASE) +
3617 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03003618 vcpu->run->debug.arch.exception = DB_VECTOR;
3619 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003620 return 0;
3621 } else {
3622 vcpu->arch.dr7 &= ~DR7_GD;
3623 vcpu->arch.dr6 |= DR6_BD;
3624 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3625 kvm_queue_exception(vcpu, DB_VECTOR);
3626 return 1;
3627 }
3628 }
3629
He, Qingbfdaab02007-09-12 14:18:28 +08003630 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003631 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
3632 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
3633 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03003634 unsigned long val;
3635 if (!kvm_get_dr(vcpu, dr, &val))
3636 kvm_register_write(vcpu, reg, val);
3637 } else
3638 kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003639 skip_emulated_instruction(vcpu);
3640 return 1;
3641}
3642
Gleb Natapov020df072010-04-13 10:05:23 +03003643static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
3644{
3645 vmcs_writel(GUEST_DR7, val);
3646}
3647
Avi Kivity851ba692009-08-24 11:10:17 +03003648static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003649{
Avi Kivity06465c52007-02-28 20:46:53 +02003650 kvm_emulate_cpuid(vcpu);
3651 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003652}
3653
Avi Kivity851ba692009-08-24 11:10:17 +03003654static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003655{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003656 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003657 u64 data;
3658
3659 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivity59200272010-01-25 19:47:02 +02003660 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003661 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003662 return 1;
3663 }
3664
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003665 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003666
Avi Kivity6aa8b732006-12-10 02:21:36 -08003667 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003668 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3669 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003670 skip_emulated_instruction(vcpu);
3671 return 1;
3672}
3673
Avi Kivity851ba692009-08-24 11:10:17 +03003674static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003675{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003676 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3677 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3678 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003679
3680 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02003681 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003682 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003683 return 1;
3684 }
3685
Avi Kivity59200272010-01-25 19:47:02 +02003686 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003687 skip_emulated_instruction(vcpu);
3688 return 1;
3689}
3690
Avi Kivity851ba692009-08-24 11:10:17 +03003691static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003692{
Avi Kivity3842d132010-07-27 12:30:24 +03003693 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003694 return 1;
3695}
3696
Avi Kivity851ba692009-08-24 11:10:17 +03003697static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003698{
Eddie Dong85f455f2007-07-06 12:20:49 +03003699 u32 cpu_based_vm_exec_control;
3700
3701 /* clear pending irq */
3702 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3703 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3704 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003705
Avi Kivity3842d132010-07-27 12:30:24 +03003706 kvm_make_request(KVM_REQ_EVENT, vcpu);
3707
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003708 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003709
Dor Laorc1150d82007-01-05 16:36:24 -08003710 /*
3711 * If the user space waits to inject interrupts, exit as soon as
3712 * possible
3713 */
Gleb Natapov80618232009-04-21 17:44:56 +03003714 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003715 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03003716 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003717 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003718 return 0;
3719 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003720 return 1;
3721}
3722
Avi Kivity851ba692009-08-24 11:10:17 +03003723static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003724{
3725 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003726 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003727}
3728
Avi Kivity851ba692009-08-24 11:10:17 +03003729static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02003730{
Dor Laor510043d2007-02-19 18:25:43 +02003731 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003732 kvm_emulate_hypercall(vcpu);
3733 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003734}
3735
Avi Kivity851ba692009-08-24 11:10:17 +03003736static int handle_vmx_insn(struct kvm_vcpu *vcpu)
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003737{
3738 kvm_queue_exception(vcpu, UD_VECTOR);
3739 return 1;
3740}
3741
Gleb Natapovec25d5e2010-11-01 15:35:01 +02003742static int handle_invd(struct kvm_vcpu *vcpu)
3743{
Andre Przywara51d8b662010-12-21 11:12:02 +01003744 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02003745}
3746
Avi Kivity851ba692009-08-24 11:10:17 +03003747static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003748{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003749 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003750
3751 kvm_mmu_invlpg(vcpu, exit_qualification);
3752 skip_emulated_instruction(vcpu);
3753 return 1;
3754}
3755
Avi Kivity851ba692009-08-24 11:10:17 +03003756static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02003757{
3758 skip_emulated_instruction(vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003759 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02003760 return 1;
3761}
3762
Dexuan Cui2acf9232010-06-10 11:27:12 +08003763static int handle_xsetbv(struct kvm_vcpu *vcpu)
3764{
3765 u64 new_bv = kvm_read_edx_eax(vcpu);
3766 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
3767
3768 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
3769 skip_emulated_instruction(vcpu);
3770 return 1;
3771}
3772
Avi Kivity851ba692009-08-24 11:10:17 +03003773static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003774{
Andre Przywara51d8b662010-12-21 11:12:02 +01003775 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003776}
3777
Avi Kivity851ba692009-08-24 11:10:17 +03003778static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02003779{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003780 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003781 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003782 bool has_error_code = false;
3783 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003784 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003785 int reason, type, idt_v;
3786
3787 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3788 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003789
3790 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3791
3792 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003793 if (reason == TASK_SWITCH_GATE && idt_v) {
3794 switch (type) {
3795 case INTR_TYPE_NMI_INTR:
3796 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02003797 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003798 break;
3799 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003800 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003801 kvm_clear_interrupt_queue(vcpu);
3802 break;
3803 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003804 if (vmx->idt_vectoring_info &
3805 VECTORING_INFO_DELIVER_CODE_MASK) {
3806 has_error_code = true;
3807 error_code =
3808 vmcs_read32(IDT_VECTORING_ERROR_CODE);
3809 }
3810 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003811 case INTR_TYPE_SOFT_EXCEPTION:
3812 kvm_clear_exception_queue(vcpu);
3813 break;
3814 default:
3815 break;
3816 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003817 }
Izik Eidus37817f22008-03-24 23:14:53 +02003818 tss_selector = exit_qualification;
3819
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003820 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3821 type != INTR_TYPE_EXT_INTR &&
3822 type != INTR_TYPE_NMI_INTR))
3823 skip_emulated_instruction(vcpu);
3824
Gleb Natapovacb54512010-04-15 21:03:50 +03003825 if (kvm_task_switch(vcpu, tss_selector, reason,
3826 has_error_code, error_code) == EMULATE_FAIL) {
3827 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3828 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3829 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003830 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03003831 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003832
3833 /* clear all local breakpoint enable flags */
3834 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3835
3836 /*
3837 * TODO: What about debug traps on tss switch?
3838 * Are we supposed to inject them and update dr6?
3839 */
3840
3841 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003842}
3843
Avi Kivity851ba692009-08-24 11:10:17 +03003844static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08003845{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003846 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003847 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003848 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003849
Sheng Yangf9c617f2009-03-25 10:08:52 +08003850 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003851
3852 if (exit_qualification & (1 << 6)) {
3853 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003854 return -EINVAL;
Sheng Yang14394422008-04-28 12:24:45 +08003855 }
3856
3857 gla_validity = (exit_qualification >> 7) & 0x3;
3858 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3859 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3860 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3861 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003862 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003863 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3864 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03003865 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3866 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03003867 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003868 }
3869
3870 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003871 trace_kvm_page_fault(gpa, exit_qualification);
Andre Przywaradc25e892010-12-21 11:12:07 +01003872 return kvm_mmu_page_fault(vcpu, gpa, exit_qualification & 0x3, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003873}
3874
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003875static u64 ept_rsvd_mask(u64 spte, int level)
3876{
3877 int i;
3878 u64 mask = 0;
3879
3880 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
3881 mask |= (1ULL << i);
3882
3883 if (level > 2)
3884 /* bits 7:3 reserved */
3885 mask |= 0xf8;
3886 else if (level == 2) {
3887 if (spte & (1ULL << 7))
3888 /* 2MB ref, bits 20:12 reserved */
3889 mask |= 0x1ff000;
3890 else
3891 /* bits 6:3 reserved */
3892 mask |= 0x78;
3893 }
3894
3895 return mask;
3896}
3897
3898static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
3899 int level)
3900{
3901 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
3902
3903 /* 010b (write-only) */
3904 WARN_ON((spte & 0x7) == 0x2);
3905
3906 /* 110b (write/execute) */
3907 WARN_ON((spte & 0x7) == 0x6);
3908
3909 /* 100b (execute-only) and value not supported by logical processor */
3910 if (!cpu_has_vmx_ept_execute_only())
3911 WARN_ON((spte & 0x7) == 0x4);
3912
3913 /* not 000b */
3914 if ((spte & 0x7)) {
3915 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
3916
3917 if (rsvd_bits != 0) {
3918 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
3919 __func__, rsvd_bits);
3920 WARN_ON(1);
3921 }
3922
3923 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
3924 u64 ept_mem_type = (spte & 0x38) >> 3;
3925
3926 if (ept_mem_type == 2 || ept_mem_type == 3 ||
3927 ept_mem_type == 7) {
3928 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
3929 __func__, ept_mem_type);
3930 WARN_ON(1);
3931 }
3932 }
3933 }
3934}
3935
Avi Kivity851ba692009-08-24 11:10:17 +03003936static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003937{
3938 u64 sptes[4];
3939 int nr_sptes, i;
3940 gpa_t gpa;
3941
3942 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3943
3944 printk(KERN_ERR "EPT: Misconfiguration.\n");
3945 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
3946
3947 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
3948
3949 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
3950 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
3951
Avi Kivity851ba692009-08-24 11:10:17 +03003952 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3953 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003954
3955 return 0;
3956}
3957
Avi Kivity851ba692009-08-24 11:10:17 +03003958static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08003959{
3960 u32 cpu_based_vm_exec_control;
3961
3962 /* clear pending NMI */
3963 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3964 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3965 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3966 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03003967 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003968
3969 return 1;
3970}
3971
Mohammed Gamal80ced182009-09-01 12:48:18 +02003972static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003973{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003974 struct vcpu_vmx *vmx = to_vmx(vcpu);
3975 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003976 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02003977 u32 cpu_exec_ctrl;
3978 bool intr_window_requested;
3979
3980 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3981 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003982
3983 while (!guest_state_valid(vcpu)) {
Avi Kivity49e9d552010-09-19 14:34:08 +02003984 if (intr_window_requested
3985 && (kvm_get_rflags(&vmx->vcpu) & X86_EFLAGS_IF))
3986 return handle_interrupt_window(&vmx->vcpu);
3987
Andre Przywara51d8b662010-12-21 11:12:02 +01003988 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003989
Mohammed Gamal80ced182009-09-01 12:48:18 +02003990 if (err == EMULATE_DO_MMIO) {
3991 ret = 0;
3992 goto out;
3993 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003994
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003995 if (err != EMULATE_DONE)
3996 return 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003997
3998 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02003999 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004000 if (need_resched())
4001 schedule();
4002 }
4003
Mohammed Gamal80ced182009-09-01 12:48:18 +02004004 vmx->emulation_required = 0;
4005out:
4006 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004007}
4008
Avi Kivity6aa8b732006-12-10 02:21:36 -08004009/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004010 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
4011 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
4012 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03004013static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004014{
4015 skip_emulated_instruction(vcpu);
4016 kvm_vcpu_on_spin(vcpu);
4017
4018 return 1;
4019}
4020
Sheng Yang59708672009-12-15 13:29:54 +08004021static int handle_invalid_op(struct kvm_vcpu *vcpu)
4022{
4023 kvm_queue_exception(vcpu, UD_VECTOR);
4024 return 1;
4025}
4026
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004027/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03004028 * Emulate the VMXON instruction.
4029 * Currently, we just remember that VMX is active, and do not save or even
4030 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
4031 * do not currently need to store anything in that guest-allocated memory
4032 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
4033 * argument is different from the VMXON pointer (which the spec says they do).
4034 */
4035static int handle_vmon(struct kvm_vcpu *vcpu)
4036{
4037 struct kvm_segment cs;
4038 struct vcpu_vmx *vmx = to_vmx(vcpu);
4039
4040 /* The Intel VMX Instruction Reference lists a bunch of bits that
4041 * are prerequisite to running VMXON, most notably cr4.VMXE must be
4042 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
4043 * Otherwise, we should fail with #UD. We test these now:
4044 */
4045 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
4046 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
4047 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
4048 kvm_queue_exception(vcpu, UD_VECTOR);
4049 return 1;
4050 }
4051
4052 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4053 if (is_long_mode(vcpu) && !cs.l) {
4054 kvm_queue_exception(vcpu, UD_VECTOR);
4055 return 1;
4056 }
4057
4058 if (vmx_get_cpl(vcpu)) {
4059 kvm_inject_gp(vcpu, 0);
4060 return 1;
4061 }
4062
4063 vmx->nested.vmxon = true;
4064
4065 skip_emulated_instruction(vcpu);
4066 return 1;
4067}
4068
4069/*
4070 * Intel's VMX Instruction Reference specifies a common set of prerequisites
4071 * for running VMX instructions (except VMXON, whose prerequisites are
4072 * slightly different). It also specifies what exception to inject otherwise.
4073 */
4074static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
4075{
4076 struct kvm_segment cs;
4077 struct vcpu_vmx *vmx = to_vmx(vcpu);
4078
4079 if (!vmx->nested.vmxon) {
4080 kvm_queue_exception(vcpu, UD_VECTOR);
4081 return 0;
4082 }
4083
4084 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4085 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
4086 (is_long_mode(vcpu) && !cs.l)) {
4087 kvm_queue_exception(vcpu, UD_VECTOR);
4088 return 0;
4089 }
4090
4091 if (vmx_get_cpl(vcpu)) {
4092 kvm_inject_gp(vcpu, 0);
4093 return 0;
4094 }
4095
4096 return 1;
4097}
4098
4099/*
4100 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
4101 * just stops using VMX.
4102 */
4103static void free_nested(struct vcpu_vmx *vmx)
4104{
4105 if (!vmx->nested.vmxon)
4106 return;
4107 vmx->nested.vmxon = false;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03004108 if (vmx->nested.current_vmptr != -1ull) {
4109 kunmap(vmx->nested.current_vmcs12_page);
4110 nested_release_page(vmx->nested.current_vmcs12_page);
4111 vmx->nested.current_vmptr = -1ull;
4112 vmx->nested.current_vmcs12 = NULL;
4113 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03004114}
4115
4116/* Emulate the VMXOFF instruction */
4117static int handle_vmoff(struct kvm_vcpu *vcpu)
4118{
4119 if (!nested_vmx_check_permission(vcpu))
4120 return 1;
4121 free_nested(to_vmx(vcpu));
4122 skip_emulated_instruction(vcpu);
4123 return 1;
4124}
4125
4126/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004127 * The exit handlers return 1 if the exit was handled fully and guest execution
4128 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
4129 * to be done to userspace and return 0.
4130 */
Avi Kivity851ba692009-08-24 11:10:17 +03004131static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004132 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
4133 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08004134 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08004135 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004136 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004137 [EXIT_REASON_CR_ACCESS] = handle_cr,
4138 [EXIT_REASON_DR_ACCESS] = handle_dr,
4139 [EXIT_REASON_CPUID] = handle_cpuid,
4140 [EXIT_REASON_MSR_READ] = handle_rdmsr,
4141 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
4142 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
4143 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02004144 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03004145 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02004146 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03004147 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
4148 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
4149 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
4150 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
4151 [EXIT_REASON_VMREAD] = handle_vmx_insn,
4152 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
4153 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03004154 [EXIT_REASON_VMOFF] = handle_vmoff,
4155 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004156 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
4157 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02004158 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08004159 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02004160 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08004161 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004162 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
4163 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004164 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sheng Yang59708672009-12-15 13:29:54 +08004165 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
4166 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004167};
4168
4169static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04004170 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004171
Avi Kivity586f9602010-11-18 13:09:54 +02004172static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
4173{
4174 *info1 = vmcs_readl(EXIT_QUALIFICATION);
4175 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
4176}
4177
Avi Kivity6aa8b732006-12-10 02:21:36 -08004178/*
4179 * The guest has exited. See if we can fix it or if we need userspace
4180 * assistance.
4181 */
Avi Kivity851ba692009-08-24 11:10:17 +03004182static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004183{
Avi Kivity29bd8a72007-09-10 17:27:03 +03004184 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08004185 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02004186 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03004187
Avi Kivityaa179112010-11-17 18:44:19 +02004188 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004189
Mohammed Gamal80ced182009-09-01 12:48:18 +02004190 /* If guest state is invalid, start emulating */
4191 if (vmx->emulation_required && emulate_invalid_guest_state)
4192 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01004193
Mohammed Gamal51207022010-05-31 22:40:54 +03004194 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
4195 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4196 vcpu->run->fail_entry.hardware_entry_failure_reason
4197 = exit_reason;
4198 return 0;
4199 }
4200
Avi Kivity29bd8a72007-09-10 17:27:03 +03004201 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004202 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4203 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03004204 = vmcs_read32(VM_INSTRUCTION_ERROR);
4205 return 0;
4206 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004207
Mike Dayd77c26f2007-10-08 09:02:08 -04004208 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08004209 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02004210 exit_reason != EXIT_REASON_EPT_VIOLATION &&
4211 exit_reason != EXIT_REASON_TASK_SWITCH))
4212 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
4213 "(0x%x) and exit reason is 0x%x\n",
4214 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004215
4216 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03004217 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004218 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004219 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01004220 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004221 /*
4222 * This CPU don't support us in finding the end of an
4223 * NMI-blocked window if the guest runs with IRQs
4224 * disabled. So we pull the trigger after 1 s of
4225 * futile waiting, but inform the user about this.
4226 */
4227 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
4228 "state on VCPU %d after 1 s timeout\n",
4229 __func__, vcpu->vcpu_id);
4230 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004231 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004232 }
4233
Avi Kivity6aa8b732006-12-10 02:21:36 -08004234 if (exit_reason < kvm_vmx_max_exit_handlers
4235 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03004236 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004237 else {
Avi Kivity851ba692009-08-24 11:10:17 +03004238 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
4239 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004240 }
4241 return 0;
4242}
4243
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004244static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004245{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004246 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004247 vmcs_write32(TPR_THRESHOLD, 0);
4248 return;
4249 }
4250
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004251 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004252}
4253
Avi Kivity51aa01d2010-07-20 14:31:20 +03004254static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03004255{
Avi Kivity00eba012011-03-07 17:24:54 +02004256 u32 exit_intr_info;
4257
4258 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
4259 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
4260 return;
4261
Avi Kivityc5ca8e52011-03-07 17:37:37 +02004262 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02004263 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08004264
4265 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02004266 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08004267 kvm_machine_check();
4268
Gleb Natapov20f65982009-05-11 13:35:55 +03004269 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02004270 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004271 (exit_intr_info & INTR_INFO_VALID_MASK)) {
4272 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03004273 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004274 kvm_after_handle_nmi(&vmx->vcpu);
4275 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03004276}
Gleb Natapov20f65982009-05-11 13:35:55 +03004277
Avi Kivity51aa01d2010-07-20 14:31:20 +03004278static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
4279{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02004280 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03004281 bool unblock_nmi;
4282 u8 vector;
4283 bool idtv_info_valid;
4284
4285 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03004286
Avi Kivitycf393f72008-07-01 16:20:21 +03004287 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02004288 if (vmx->nmi_known_unmasked)
4289 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02004290 /*
4291 * Can't use vmx->exit_intr_info since we're not sure what
4292 * the exit reason is.
4293 */
4294 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03004295 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
4296 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
4297 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004298 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03004299 * Re-set bit "block by NMI" before VM entry if vmexit caused by
4300 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004301 * SDM 3: 23.2.2 (September 2008)
4302 * Bit 12 is undefined in any of the following cases:
4303 * If the VM exit sets the valid bit in the IDT-vectoring
4304 * information field.
4305 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03004306 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004307 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
4308 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03004309 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4310 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02004311 else
4312 vmx->nmi_known_unmasked =
4313 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
4314 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004315 } else if (unlikely(vmx->soft_vnmi_blocked))
4316 vmx->vnmi_blocked_time +=
4317 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03004318}
4319
Avi Kivity83422e12010-07-20 14:43:23 +03004320static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
4321 u32 idt_vectoring_info,
4322 int instr_len_field,
4323 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03004324{
Avi Kivity51aa01d2010-07-20 14:31:20 +03004325 u8 vector;
4326 int type;
4327 bool idtv_info_valid;
4328
4329 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03004330
Gleb Natapov37b96e92009-03-30 16:03:13 +03004331 vmx->vcpu.arch.nmi_injected = false;
4332 kvm_clear_exception_queue(&vmx->vcpu);
4333 kvm_clear_interrupt_queue(&vmx->vcpu);
4334
4335 if (!idtv_info_valid)
4336 return;
4337
Avi Kivity3842d132010-07-27 12:30:24 +03004338 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
4339
Avi Kivity668f6122008-07-02 09:28:55 +03004340 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
4341 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03004342
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004343 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03004344 case INTR_TYPE_NMI_INTR:
4345 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03004346 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004347 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03004348 * Clear bit "block by NMI" before VM entry if a NMI
4349 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03004350 */
Avi Kivity654f06f2011-03-23 15:02:47 +02004351 vmx_set_nmi_mask(&vmx->vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004352 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03004353 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004354 vmx->vcpu.arch.event_exit_inst_len =
Avi Kivity83422e12010-07-20 14:43:23 +03004355 vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004356 /* fall through */
4357 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03004358 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03004359 u32 err = vmcs_read32(error_code_field);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004360 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03004361 } else
4362 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004363 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004364 case INTR_TYPE_SOFT_INTR:
4365 vmx->vcpu.arch.event_exit_inst_len =
Avi Kivity83422e12010-07-20 14:43:23 +03004366 vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004367 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03004368 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004369 kvm_queue_interrupt(&vmx->vcpu, vector,
4370 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004371 break;
4372 default:
4373 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03004374 }
Avi Kivitycf393f72008-07-01 16:20:21 +03004375}
4376
Avi Kivity83422e12010-07-20 14:43:23 +03004377static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
4378{
4379 __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info,
4380 VM_EXIT_INSTRUCTION_LEN,
4381 IDT_VECTORING_ERROR_CODE);
4382}
4383
Avi Kivityb463a6f2010-07-20 15:06:17 +03004384static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
4385{
4386 __vmx_complete_interrupts(to_vmx(vcpu),
4387 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
4388 VM_ENTRY_INSTRUCTION_LEN,
4389 VM_ENTRY_EXCEPTION_ERROR_CODE);
4390
4391 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
4392}
4393
Avi Kivityc8019492008-07-14 14:44:59 +03004394#ifdef CONFIG_X86_64
4395#define R "r"
4396#define Q "q"
4397#else
4398#define R "e"
4399#define Q "l"
4400#endif
4401
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08004402static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004403{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004404 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity104f2262010-11-18 13:12:52 +02004405
4406 /* Record the guest's net vcpu time for enforced NMI injections. */
4407 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
4408 vmx->entry_time = ktime_get();
4409
4410 /* Don't enter VMX if guest state is invalid, let the exit handler
4411 start emulation until we arrive back to a valid state */
4412 if (vmx->emulation_required && emulate_invalid_guest_state)
4413 return;
4414
4415 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
4416 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
4417 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
4418 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
4419
4420 /* When single-stepping over STI and MOV SS, we must clear the
4421 * corresponding interruptibility bits in the guest state. Otherwise
4422 * vmentry fails as it then expects bit 14 (BS) in pending debug
4423 * exceptions being set, but that's not correct for the guest debugging
4424 * case. */
4425 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
4426 vmx_set_interrupt_shadow(vcpu, 0);
4427
Nadav Har'Eld462b812011-05-24 15:26:10 +03004428 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02004429 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08004430 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03004431 "push %%"R"dx; push %%"R"bp;"
Avi Kivity40712fa2011-01-06 18:09:12 +02004432 "push %%"R"cx \n\t" /* placeholder for guest rcx */
Avi Kivityc8019492008-07-14 14:44:59 +03004433 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03004434 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
4435 "je 1f \n\t"
4436 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004437 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03004438 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03004439 /* Reload cr2 if changed */
4440 "mov %c[cr2](%0), %%"R"ax \n\t"
4441 "mov %%cr2, %%"R"dx \n\t"
4442 "cmp %%"R"ax, %%"R"dx \n\t"
4443 "je 2f \n\t"
4444 "mov %%"R"ax, %%cr2 \n\t"
4445 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004446 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02004447 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004448 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03004449 "mov %c[rax](%0), %%"R"ax \n\t"
4450 "mov %c[rbx](%0), %%"R"bx \n\t"
4451 "mov %c[rdx](%0), %%"R"dx \n\t"
4452 "mov %c[rsi](%0), %%"R"si \n\t"
4453 "mov %c[rdi](%0), %%"R"di \n\t"
4454 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004455#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02004456 "mov %c[r8](%0), %%r8 \n\t"
4457 "mov %c[r9](%0), %%r9 \n\t"
4458 "mov %c[r10](%0), %%r10 \n\t"
4459 "mov %c[r11](%0), %%r11 \n\t"
4460 "mov %c[r12](%0), %%r12 \n\t"
4461 "mov %c[r13](%0), %%r13 \n\t"
4462 "mov %c[r14](%0), %%r14 \n\t"
4463 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004464#endif
Avi Kivityc8019492008-07-14 14:44:59 +03004465 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
4466
Avi Kivity6aa8b732006-12-10 02:21:36 -08004467 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03004468 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004469 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03004470 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004471 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03004472 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004473 /* Save guest registers, load host registers, keep flags */
Avi Kivity40712fa2011-01-06 18:09:12 +02004474 "mov %0, %c[wordsize](%%"R"sp) \n\t"
4475 "pop %0 \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03004476 "mov %%"R"ax, %c[rax](%0) \n\t"
4477 "mov %%"R"bx, %c[rbx](%0) \n\t"
Avi Kivity1c696d02011-01-06 18:09:11 +02004478 "pop"Q" %c[rcx](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03004479 "mov %%"R"dx, %c[rdx](%0) \n\t"
4480 "mov %%"R"si, %c[rsi](%0) \n\t"
4481 "mov %%"R"di, %c[rdi](%0) \n\t"
4482 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004483#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02004484 "mov %%r8, %c[r8](%0) \n\t"
4485 "mov %%r9, %c[r9](%0) \n\t"
4486 "mov %%r10, %c[r10](%0) \n\t"
4487 "mov %%r11, %c[r11](%0) \n\t"
4488 "mov %%r12, %c[r12](%0) \n\t"
4489 "mov %%r13, %c[r13](%0) \n\t"
4490 "mov %%r14, %c[r14](%0) \n\t"
4491 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004492#endif
Avi Kivityc8019492008-07-14 14:44:59 +03004493 "mov %%cr2, %%"R"ax \n\t"
4494 "mov %%"R"ax, %c[cr2](%0) \n\t"
4495
Avi Kivity1c696d02011-01-06 18:09:11 +02004496 "pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02004497 "setbe %c[fail](%0) \n\t"
4498 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03004499 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02004500 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03004501 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004502 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
4503 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
4504 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
4505 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
4506 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
4507 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
4508 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004509#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004510 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
4511 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
4512 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
4513 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
4514 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
4515 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
4516 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
4517 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08004518#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02004519 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
4520 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02004521 : "cc", "memory"
Jan Kiszka07d6f552010-09-28 16:37:42 +02004522 , R"ax", R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02004523#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02004524 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
4525#endif
4526 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08004527
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03004528 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02004529 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivity69c73022011-03-07 15:26:44 +02004530 | (1 << VCPU_EXREG_CPL)
Avi Kivityaff48ba2010-12-05 18:56:11 +02004531 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03004532 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02004533 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004534 vcpu->arch.regs_dirty = 0;
4535
Avi Kivity1155f762007-11-22 11:30:47 +02004536 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
4537
Mike Dayd77c26f2007-10-08 09:02:08 -04004538 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Nadav Har'Eld462b812011-05-24 15:26:10 +03004539 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02004540
Avi Kivity51aa01d2010-07-20 14:31:20 +03004541 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03004542
4543 vmx_complete_atomic_exit(vmx);
4544 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03004545 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004546}
4547
Avi Kivityc8019492008-07-14 14:44:59 +03004548#undef R
4549#undef Q
4550
Avi Kivity6aa8b732006-12-10 02:21:36 -08004551static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
4552{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004553 struct vcpu_vmx *vmx = to_vmx(vcpu);
4554
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004555 free_vpid(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03004556 free_nested(vmx);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004557 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004558 kfree(vmx->guest_msrs);
4559 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10004560 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004561}
4562
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004563static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004564{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004565 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10004566 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03004567 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004568
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004569 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004570 return ERR_PTR(-ENOMEM);
4571
Sheng Yang2384d2b2008-01-17 15:14:33 +08004572 allocate_vpid(vmx);
4573
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004574 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
4575 if (err)
4576 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08004577
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004578 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02004579 err = -ENOMEM;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004580 if (!vmx->guest_msrs) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004581 goto uninit_vcpu;
4582 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08004583
Nadav Har'Eld462b812011-05-24 15:26:10 +03004584 vmx->loaded_vmcs = &vmx->vmcs01;
4585 vmx->loaded_vmcs->vmcs = alloc_vmcs();
4586 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004587 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03004588 if (!vmm_exclusive)
4589 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
4590 loaded_vmcs_init(vmx->loaded_vmcs);
4591 if (!vmm_exclusive)
4592 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004593
Avi Kivity15ad7142007-07-11 18:17:21 +03004594 cpu = get_cpu();
4595 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10004596 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10004597 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004598 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03004599 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004600 if (err)
4601 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02004602 if (vm_need_virtualize_apic_accesses(kvm))
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02004603 err = alloc_apic_access_page(kvm);
4604 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02004605 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08004606
Sheng Yangb927a3c2009-07-21 10:42:48 +08004607 if (enable_ept) {
4608 if (!kvm->arch.ept_identity_map_addr)
4609 kvm->arch.ept_identity_map_addr =
4610 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Gleb Natapov93ea5382011-02-21 12:07:59 +02004611 err = -ENOMEM;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004612 if (alloc_identity_pagetable(kvm) != 0)
4613 goto free_vmcs;
Gleb Natapov93ea5382011-02-21 12:07:59 +02004614 if (!init_rmode_identity_map(kvm))
4615 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004616 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004617
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03004618 vmx->nested.current_vmptr = -1ull;
4619 vmx->nested.current_vmcs12 = NULL;
4620
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004621 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08004622
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004623free_vmcs:
Nadav Har'Eld462b812011-05-24 15:26:10 +03004624 free_vmcs(vmx->loaded_vmcs->vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004625free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004626 kfree(vmx->guest_msrs);
4627uninit_vcpu:
4628 kvm_vcpu_uninit(&vmx->vcpu);
4629free_vcpu:
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004630 free_vpid(vmx);
Rusty Russella4770342007-08-01 14:46:11 +10004631 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004632 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004633}
4634
Yang, Sheng002c7f72007-07-31 14:23:01 +03004635static void __init vmx_check_processor_compat(void *rtn)
4636{
4637 struct vmcs_config vmcs_conf;
4638
4639 *(int *)rtn = 0;
4640 if (setup_vmcs_config(&vmcs_conf) < 0)
4641 *(int *)rtn = -EIO;
4642 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
4643 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
4644 smp_processor_id());
4645 *(int *)rtn = -EIO;
4646 }
4647}
4648
Sheng Yang67253af2008-04-25 10:20:22 +08004649static int get_ept_level(void)
4650{
4651 return VMX_EPT_DEFAULT_GAW + 1;
4652}
4653
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004654static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08004655{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004656 u64 ret;
4657
Sheng Yang522c68c2009-04-27 20:35:43 +08004658 /* For VT-d and EPT combination
4659 * 1. MMIO: always map as UC
4660 * 2. EPT with VT-d:
4661 * a. VT-d without snooping control feature: can't guarantee the
4662 * result, try to trust guest.
4663 * b. VT-d with snooping control feature: snooping control feature of
4664 * VT-d engine can guarantee the cache correctness. Just set it
4665 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08004666 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08004667 * consistent with host MTRR
4668 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004669 if (is_mmio)
4670 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08004671 else if (vcpu->kvm->arch.iommu_domain &&
4672 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
4673 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
4674 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004675 else
Sheng Yang522c68c2009-04-27 20:35:43 +08004676 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
Sheng Yanga19a6d12010-02-09 16:41:53 +08004677 | VMX_EPT_IPAT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004678
4679 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08004680}
4681
Avi Kivityf4c9e872009-12-28 16:06:35 +02004682#define _ER(x) { EXIT_REASON_##x, #x }
4683
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004684static const struct trace_print_flags vmx_exit_reasons_str[] = {
Avi Kivityf4c9e872009-12-28 16:06:35 +02004685 _ER(EXCEPTION_NMI),
4686 _ER(EXTERNAL_INTERRUPT),
4687 _ER(TRIPLE_FAULT),
4688 _ER(PENDING_INTERRUPT),
4689 _ER(NMI_WINDOW),
4690 _ER(TASK_SWITCH),
4691 _ER(CPUID),
4692 _ER(HLT),
4693 _ER(INVLPG),
4694 _ER(RDPMC),
4695 _ER(RDTSC),
4696 _ER(VMCALL),
4697 _ER(VMCLEAR),
4698 _ER(VMLAUNCH),
4699 _ER(VMPTRLD),
4700 _ER(VMPTRST),
4701 _ER(VMREAD),
4702 _ER(VMRESUME),
4703 _ER(VMWRITE),
4704 _ER(VMOFF),
4705 _ER(VMON),
4706 _ER(CR_ACCESS),
4707 _ER(DR_ACCESS),
4708 _ER(IO_INSTRUCTION),
4709 _ER(MSR_READ),
4710 _ER(MSR_WRITE),
4711 _ER(MWAIT_INSTRUCTION),
4712 _ER(MONITOR_INSTRUCTION),
4713 _ER(PAUSE_INSTRUCTION),
4714 _ER(MCE_DURING_VMENTRY),
4715 _ER(TPR_BELOW_THRESHOLD),
4716 _ER(APIC_ACCESS),
4717 _ER(EPT_VIOLATION),
4718 _ER(EPT_MISCONFIG),
4719 _ER(WBINVD),
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004720 { -1, NULL }
4721};
4722
Avi Kivityf4c9e872009-12-28 16:06:35 +02004723#undef _ER
4724
Sheng Yang17cc3932010-01-05 19:02:27 +08004725static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02004726{
Sheng Yang878403b2010-01-05 19:02:29 +08004727 if (enable_ept && !cpu_has_vmx_ept_1g_page())
4728 return PT_DIRECTORY_LEVEL;
4729 else
4730 /* For shadow and EPT supported 1GB page */
4731 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02004732}
4733
Sheng Yang0e851882009-12-18 16:48:46 +08004734static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
4735{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004736 struct kvm_cpuid_entry2 *best;
4737 struct vcpu_vmx *vmx = to_vmx(vcpu);
4738 u32 exec_control;
4739
4740 vmx->rdtscp_enabled = false;
4741 if (vmx_rdtscp_supported()) {
4742 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
4743 if (exec_control & SECONDARY_EXEC_RDTSCP) {
4744 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
4745 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
4746 vmx->rdtscp_enabled = true;
4747 else {
4748 exec_control &= ~SECONDARY_EXEC_RDTSCP;
4749 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4750 exec_control);
4751 }
4752 }
4753 }
Sheng Yang0e851882009-12-18 16:48:46 +08004754}
4755
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004756static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
4757{
4758}
4759
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004760static int vmx_check_intercept(struct kvm_vcpu *vcpu,
4761 struct x86_instruction_info *info,
4762 enum x86_intercept_stage stage)
4763{
4764 return X86EMUL_CONTINUE;
4765}
4766
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004767static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004768 .cpu_has_kvm_support = cpu_has_kvm_support,
4769 .disabled_by_bios = vmx_disabled_by_bios,
4770 .hardware_setup = hardware_setup,
4771 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004772 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004773 .hardware_enable = hardware_enable,
4774 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08004775 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004776
4777 .vcpu_create = vmx_create_vcpu,
4778 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004779 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004780
Avi Kivity04d2cc72007-09-10 18:10:54 +03004781 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004782 .vcpu_load = vmx_vcpu_load,
4783 .vcpu_put = vmx_vcpu_put,
4784
4785 .set_guest_debug = set_guest_debug,
4786 .get_msr = vmx_get_msr,
4787 .set_msr = vmx_set_msr,
4788 .get_segment_base = vmx_get_segment_base,
4789 .get_segment = vmx_get_segment,
4790 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004791 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004792 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004793 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02004794 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004795 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004796 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004797 .set_cr3 = vmx_set_cr3,
4798 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004799 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004800 .get_idt = vmx_get_idt,
4801 .set_idt = vmx_set_idt,
4802 .get_gdt = vmx_get_gdt,
4803 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03004804 .set_dr7 = vmx_set_dr7,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004805 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004806 .get_rflags = vmx_get_rflags,
4807 .set_rflags = vmx_set_rflags,
Avi Kivityebcbab42010-02-07 11:56:52 +02004808 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02004809 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004810
4811 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004812
Avi Kivity6aa8b732006-12-10 02:21:36 -08004813 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02004814 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004815 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004816 .set_interrupt_shadow = vmx_set_interrupt_shadow,
4817 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004818 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004819 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004820 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004821 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004822 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004823 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004824 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004825 .get_nmi_mask = vmx_get_nmi_mask,
4826 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004827 .enable_nmi_window = enable_nmi_window,
4828 .enable_irq_window = enable_irq_window,
4829 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004830
Izik Eiduscbc94022007-10-25 00:29:55 +02004831 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004832 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004833 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004834
Avi Kivity586f9602010-11-18 13:09:54 +02004835 .get_exit_info = vmx_get_exit_info,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004836 .exit_reasons_str = vmx_exit_reasons_str,
Avi Kivity586f9602010-11-18 13:09:54 +02004837
Sheng Yang17cc3932010-01-05 19:02:27 +08004838 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08004839
4840 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004841
4842 .rdtscp_supported = vmx_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004843
4844 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004845
4846 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004847
Joerg Roedel4051b182011-03-25 09:44:49 +01004848 .set_tsc_khz = vmx_set_tsc_khz,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004849 .write_tsc_offset = vmx_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10004850 .adjust_tsc_offset = vmx_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01004851 .compute_tsc_offset = vmx_compute_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004852
4853 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004854
4855 .check_intercept = vmx_check_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004856};
4857
4858static int __init vmx_init(void)
4859{
Avi Kivity26bb0982009-09-07 11:14:12 +03004860 int r, i;
4861
4862 rdmsrl_safe(MSR_EFER, &host_efer);
4863
4864 for (i = 0; i < NR_VMX_MSR; ++i)
4865 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03004866
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004867 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004868 if (!vmx_io_bitmap_a)
4869 return -ENOMEM;
4870
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004871 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004872 if (!vmx_io_bitmap_b) {
4873 r = -ENOMEM;
4874 goto out;
4875 }
4876
Avi Kivity58972972009-02-24 22:26:47 +02004877 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
4878 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08004879 r = -ENOMEM;
4880 goto out1;
4881 }
4882
Avi Kivity58972972009-02-24 22:26:47 +02004883 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
4884 if (!vmx_msr_bitmap_longmode) {
4885 r = -ENOMEM;
4886 goto out2;
4887 }
4888
He, Qingfdef3ad2007-04-30 09:45:24 +03004889 /*
4890 * Allow direct access to the PC debug port (it is often used for I/O
4891 * delays, but the vmexits simply slow things down).
4892 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004893 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
4894 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004895
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004896 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004897
Avi Kivity58972972009-02-24 22:26:47 +02004898 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
4899 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08004900
Sheng Yang2384d2b2008-01-17 15:14:33 +08004901 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
4902
Avi Kivity0ee75be2010-04-28 15:39:01 +03004903 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
4904 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004905 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02004906 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08004907
Avi Kivity58972972009-02-24 22:26:47 +02004908 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
4909 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
4910 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
4911 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
4912 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
4913 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03004914
Avi Kivity089d0342009-03-23 18:26:32 +02004915 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004916 bypass_guest_pf = 0;
Sheng Yang534e38b2008-09-08 15:12:30 +08004917 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004918 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004919 kvm_enable_tdp();
4920 } else
4921 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08004922
Avi Kivityc7addb92007-09-16 18:58:32 +02004923 if (bypass_guest_pf)
4924 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
4925
He, Qingfdef3ad2007-04-30 09:45:24 +03004926 return 0;
4927
Avi Kivity58972972009-02-24 22:26:47 +02004928out3:
4929 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08004930out2:
Avi Kivity58972972009-02-24 22:26:47 +02004931 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03004932out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004933 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03004934out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004935 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004936 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004937}
4938
4939static void __exit vmx_exit(void)
4940{
Avi Kivity58972972009-02-24 22:26:47 +02004941 free_page((unsigned long)vmx_msr_bitmap_legacy);
4942 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004943 free_page((unsigned long)vmx_io_bitmap_b);
4944 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004945
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004946 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004947}
4948
4949module_init(vmx_init)
4950module_exit(vmx_exit)