blob: 03b29aca6e7fc1e52e8041da06f82fda13dbbf4e [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
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300122#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300123
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400124struct vmcs {
125 u32 revision_id;
126 u32 abort;
127 char data[0];
128};
129
Nadav Har'Eld462b812011-05-24 15:26:10 +0300130/*
131 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
132 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
133 * loaded on this CPU (so we can clear them if the CPU goes down).
134 */
135struct loaded_vmcs {
136 struct vmcs *vmcs;
137 int cpu;
138 int launched;
139 struct list_head loaded_vmcss_on_cpu_link;
140};
141
Avi Kivity26bb0982009-09-07 11:14:12 +0300142struct shared_msr_entry {
143 unsigned index;
144 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200145 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300146};
147
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300148/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300149 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
150 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
151 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
152 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
153 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
154 * More than one of these structures may exist, if L1 runs multiple L2 guests.
155 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
156 * underlying hardware which will be used to run L2.
157 * This structure is packed to ensure that its layout is identical across
158 * machines (necessary for live migration).
159 * If there are changes in this struct, VMCS12_REVISION must be changed.
160 */
161struct __packed vmcs12 {
162 /* According to the Intel spec, a VMCS region must start with the
163 * following two fields. Then follow implementation-specific data.
164 */
165 u32 revision_id;
166 u32 abort;
167};
168
169/*
170 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
171 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
172 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
173 */
174#define VMCS12_REVISION 0x11e57ed0
175
176/*
177 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
178 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
179 * current implementation, 4K are reserved to avoid future complications.
180 */
181#define VMCS12_SIZE 0x1000
182
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300183/* Used to remember the last vmcs02 used for some recently used vmcs12s */
184struct vmcs02_list {
185 struct list_head list;
186 gpa_t vmptr;
187 struct loaded_vmcs vmcs02;
188};
189
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300190/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300191 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
192 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
193 */
194struct nested_vmx {
195 /* Has the level1 guest done vmxon? */
196 bool vmxon;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300197
198 /* The guest-physical address of the current VMCS L1 keeps for L2 */
199 gpa_t current_vmptr;
200 /* The host-usable pointer to the above */
201 struct page *current_vmcs12_page;
202 struct vmcs12 *current_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300203
204 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
205 struct list_head vmcs02_pool;
206 int vmcs02_num;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300207};
208
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400209struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000210 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300211 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300212 u8 fail;
Avi Kivity69c73022011-03-07 15:26:44 +0200213 u8 cpl;
Avi Kivity9d58b932011-03-07 16:52:07 +0200214 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300215 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200216 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200217 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300218 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400219 int nmsrs;
220 int save_nmsrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400221#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300222 u64 msr_host_kernel_gs_base;
223 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400224#endif
Nadav Har'Eld462b812011-05-24 15:26:10 +0300225 /*
226 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
227 * non-nested (L1) guest, it always points to vmcs01. For a nested
228 * guest (L2), it points to a different VMCS.
229 */
230 struct loaded_vmcs vmcs01;
231 struct loaded_vmcs *loaded_vmcs;
232 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300233 struct msr_autoload {
234 unsigned nr;
235 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
236 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
237 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400238 struct {
239 int loaded;
240 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200241 int gs_ldt_reload_needed;
242 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400243 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200244 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300245 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300246 ulong save_rflags;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300247 struct kvm_save_segment {
248 u16 selector;
249 unsigned long base;
250 u32 limit;
251 u32 ar;
252 } tr, es, ds, fs, gs;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200253 } rmode;
Avi Kivity2fb92db2011-04-27 19:42:18 +0300254 struct {
255 u32 bitmask; /* 4 bits per segment (1 bit per field) */
256 struct kvm_save_segment seg[8];
257 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800258 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300259 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200260
261 /* Support for vnmi-less CPUs */
262 int soft_vnmi_blocked;
263 ktime_t entry_time;
264 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800265 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800266
267 bool rdtscp_enabled;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300268
269 /* Support for a guest hypervisor (nested VMX) */
270 struct nested_vmx nested;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400271};
272
Avi Kivity2fb92db2011-04-27 19:42:18 +0300273enum segment_cache_field {
274 SEG_FIELD_SEL = 0,
275 SEG_FIELD_BASE = 1,
276 SEG_FIELD_LIMIT = 2,
277 SEG_FIELD_AR = 3,
278
279 SEG_FIELD_NR = 4
280};
281
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400282static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
283{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000284 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400285}
286
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300287static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
288{
289 return to_vmx(vcpu)->nested.current_vmcs12;
290}
291
292static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
293{
294 struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
295 if (is_error_page(page)) {
296 kvm_release_page_clean(page);
297 return NULL;
298 }
299 return page;
300}
301
302static void nested_release_page(struct page *page)
303{
304 kvm_release_page_dirty(page);
305}
306
307static void nested_release_page_clean(struct page *page)
308{
309 kvm_release_page_clean(page);
310}
311
Sheng Yang4e1096d2008-07-06 19:16:51 +0800312static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800313static void kvm_cpu_vmxon(u64 addr);
314static void kvm_cpu_vmxoff(void);
Avi Kivityaff48ba2010-12-05 18:56:11 +0200315static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200316static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Avi Kivity75880a02007-06-20 11:20:04 +0300317
Avi Kivity6aa8b732006-12-10 02:21:36 -0800318static DEFINE_PER_CPU(struct vmcs *, vmxarea);
319static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300320/*
321 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
322 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
323 */
324static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300325static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800326
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200327static unsigned long *vmx_io_bitmap_a;
328static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200329static unsigned long *vmx_msr_bitmap_legacy;
330static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300331
Avi Kivity110312c2010-12-21 12:54:20 +0200332static bool cpu_has_load_ia32_efer;
333
Sheng Yang2384d2b2008-01-17 15:14:33 +0800334static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
335static DEFINE_SPINLOCK(vmx_vpid_lock);
336
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300337static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338 int size;
339 int order;
340 u32 revision_id;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300341 u32 pin_based_exec_ctrl;
342 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800343 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300344 u32 vmexit_ctrl;
345 u32 vmentry_ctrl;
346} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347
Hannes Ederefff9e52008-11-28 17:02:06 +0100348static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800349 u32 ept;
350 u32 vpid;
351} vmx_capability;
352
Avi Kivity6aa8b732006-12-10 02:21:36 -0800353#define VMX_SEGMENT_FIELD(seg) \
354 [VCPU_SREG_##seg] = { \
355 .selector = GUEST_##seg##_SELECTOR, \
356 .base = GUEST_##seg##_BASE, \
357 .limit = GUEST_##seg##_LIMIT, \
358 .ar_bytes = GUEST_##seg##_AR_BYTES, \
359 }
360
361static struct kvm_vmx_segment_field {
362 unsigned selector;
363 unsigned base;
364 unsigned limit;
365 unsigned ar_bytes;
366} kvm_vmx_segment_fields[] = {
367 VMX_SEGMENT_FIELD(CS),
368 VMX_SEGMENT_FIELD(DS),
369 VMX_SEGMENT_FIELD(ES),
370 VMX_SEGMENT_FIELD(FS),
371 VMX_SEGMENT_FIELD(GS),
372 VMX_SEGMENT_FIELD(SS),
373 VMX_SEGMENT_FIELD(TR),
374 VMX_SEGMENT_FIELD(LDTR),
375};
376
Avi Kivity26bb0982009-09-07 11:14:12 +0300377static u64 host_efer;
378
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300379static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
380
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300381/*
Brian Gerst8c065852010-07-17 09:03:26 -0400382 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300383 * away by decrementing the array size.
384 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800386#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300387 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400389 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200391#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800392
Gui Jianfeng31299942010-03-15 17:29:09 +0800393static inline bool is_page_fault(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394{
395 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
396 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100397 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398}
399
Gui Jianfeng31299942010-03-15 17:29:09 +0800400static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300401{
402 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
403 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100404 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300405}
406
Gui Jianfeng31299942010-03-15 17:29:09 +0800407static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500408{
409 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
410 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100411 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500412}
413
Gui Jianfeng31299942010-03-15 17:29:09 +0800414static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800415{
416 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
417 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
418}
419
Gui Jianfeng31299942010-03-15 17:29:09 +0800420static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +0800421{
422 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
423 INTR_INFO_VALID_MASK)) ==
424 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
425}
426
Gui Jianfeng31299942010-03-15 17:29:09 +0800427static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +0800428{
Sheng Yang04547152009-04-01 15:52:31 +0800429 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800430}
431
Gui Jianfeng31299942010-03-15 17:29:09 +0800432static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800433{
Sheng Yang04547152009-04-01 15:52:31 +0800434 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800435}
436
Gui Jianfeng31299942010-03-15 17:29:09 +0800437static inline bool vm_need_tpr_shadow(struct kvm *kvm)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800438{
Sheng Yang04547152009-04-01 15:52:31 +0800439 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800440}
441
Gui Jianfeng31299942010-03-15 17:29:09 +0800442static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800443{
Sheng Yang04547152009-04-01 15:52:31 +0800444 return vmcs_config.cpu_based_exec_ctrl &
445 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800446}
447
Avi Kivity774ead32007-12-26 13:57:04 +0200448static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800449{
Sheng Yang04547152009-04-01 15:52:31 +0800450 return vmcs_config.cpu_based_2nd_exec_ctrl &
451 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
452}
453
454static inline bool cpu_has_vmx_flexpriority(void)
455{
456 return cpu_has_vmx_tpr_shadow() &&
457 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800458}
459
Marcelo Tosattie7997942009-06-11 12:07:40 -0300460static inline bool cpu_has_vmx_ept_execute_only(void)
461{
Gui Jianfeng31299942010-03-15 17:29:09 +0800462 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300463}
464
465static inline bool cpu_has_vmx_eptp_uncacheable(void)
466{
Gui Jianfeng31299942010-03-15 17:29:09 +0800467 return vmx_capability.ept & VMX_EPTP_UC_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300468}
469
470static inline bool cpu_has_vmx_eptp_writeback(void)
471{
Gui Jianfeng31299942010-03-15 17:29:09 +0800472 return vmx_capability.ept & VMX_EPTP_WB_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300473}
474
475static inline bool cpu_has_vmx_ept_2m_page(void)
476{
Gui Jianfeng31299942010-03-15 17:29:09 +0800477 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300478}
479
Sheng Yang878403b2010-01-05 19:02:29 +0800480static inline bool cpu_has_vmx_ept_1g_page(void)
481{
Gui Jianfeng31299942010-03-15 17:29:09 +0800482 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +0800483}
484
Sheng Yang4bc9b982010-06-02 14:05:24 +0800485static inline bool cpu_has_vmx_ept_4levels(void)
486{
487 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
488}
489
Gui Jianfeng31299942010-03-15 17:29:09 +0800490static inline bool cpu_has_vmx_invept_individual_addr(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800491{
Gui Jianfeng31299942010-03-15 17:29:09 +0800492 return vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800493}
494
Gui Jianfeng31299942010-03-15 17:29:09 +0800495static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800496{
Gui Jianfeng31299942010-03-15 17:29:09 +0800497 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800498}
499
Gui Jianfeng31299942010-03-15 17:29:09 +0800500static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800501{
Gui Jianfeng31299942010-03-15 17:29:09 +0800502 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800503}
504
Gui Jianfeng518c8ae2010-06-04 08:51:39 +0800505static inline bool cpu_has_vmx_invvpid_single(void)
506{
507 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
508}
509
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800510static inline bool cpu_has_vmx_invvpid_global(void)
511{
512 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
513}
514
Gui Jianfeng31299942010-03-15 17:29:09 +0800515static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800516{
Sheng Yang04547152009-04-01 15:52:31 +0800517 return vmcs_config.cpu_based_2nd_exec_ctrl &
518 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800519}
520
Gui Jianfeng31299942010-03-15 17:29:09 +0800521static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700522{
523 return vmcs_config.cpu_based_2nd_exec_ctrl &
524 SECONDARY_EXEC_UNRESTRICTED_GUEST;
525}
526
Gui Jianfeng31299942010-03-15 17:29:09 +0800527static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800528{
529 return vmcs_config.cpu_based_2nd_exec_ctrl &
530 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
531}
532
Gui Jianfeng31299942010-03-15 17:29:09 +0800533static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800534{
Gui Jianfeng6d3e4352010-01-29 15:36:59 +0800535 return flexpriority_enabled && irqchip_in_kernel(kvm);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800536}
537
Gui Jianfeng31299942010-03-15 17:29:09 +0800538static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800539{
Sheng Yang04547152009-04-01 15:52:31 +0800540 return vmcs_config.cpu_based_2nd_exec_ctrl &
541 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800542}
543
Gui Jianfeng31299942010-03-15 17:29:09 +0800544static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800545{
546 return vmcs_config.cpu_based_2nd_exec_ctrl &
547 SECONDARY_EXEC_RDTSCP;
548}
549
Gui Jianfeng31299942010-03-15 17:29:09 +0800550static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +0800551{
552 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
553}
554
Sheng Yangf5f48ee2010-06-30 12:25:15 +0800555static inline bool cpu_has_vmx_wbinvd_exit(void)
556{
557 return vmcs_config.cpu_based_2nd_exec_ctrl &
558 SECONDARY_EXEC_WBINVD_EXITING;
559}
560
Sheng Yang04547152009-04-01 15:52:31 +0800561static inline bool report_flexpriority(void)
562{
563 return flexpriority_enabled;
564}
565
Rusty Russell8b9cf982007-07-30 16:31:43 +1000566static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800567{
568 int i;
569
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400570 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300571 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300572 return i;
573 return -1;
574}
575
Sheng Yang2384d2b2008-01-17 15:14:33 +0800576static inline void __invvpid(int ext, u16 vpid, gva_t gva)
577{
578 struct {
579 u64 vpid : 16;
580 u64 rsvd : 48;
581 u64 gva;
582 } operand = { vpid, 0, gva };
583
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300584 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800585 /* CF==1 or ZF==1 --> rc = -1 */
586 "; ja 1f ; ud2 ; 1:"
587 : : "a"(&operand), "c"(ext) : "cc", "memory");
588}
589
Sheng Yang14394422008-04-28 12:24:45 +0800590static inline void __invept(int ext, u64 eptp, gpa_t gpa)
591{
592 struct {
593 u64 eptp, gpa;
594 } operand = {eptp, gpa};
595
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300596 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800597 /* CF==1 or ZF==1 --> rc = -1 */
598 "; ja 1f ; ud2 ; 1:\n"
599 : : "a" (&operand), "c" (ext) : "cc", "memory");
600}
601
Avi Kivity26bb0982009-09-07 11:14:12 +0300602static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300603{
604 int i;
605
Rusty Russell8b9cf982007-07-30 16:31:43 +1000606 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300607 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400608 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000609 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800610}
611
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612static void vmcs_clear(struct vmcs *vmcs)
613{
614 u64 phys_addr = __pa(vmcs);
615 u8 error;
616
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300617 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +0200618 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800619 : "cc", "memory");
620 if (error)
621 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
622 vmcs, phys_addr);
623}
624
Nadav Har'Eld462b812011-05-24 15:26:10 +0300625static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
626{
627 vmcs_clear(loaded_vmcs->vmcs);
628 loaded_vmcs->cpu = -1;
629 loaded_vmcs->launched = 0;
630}
631
Dongxiao Xu7725b892010-05-11 18:29:38 +0800632static void vmcs_load(struct vmcs *vmcs)
633{
634 u64 phys_addr = __pa(vmcs);
635 u8 error;
636
637 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +0200638 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +0800639 : "cc", "memory");
640 if (error)
641 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
642 vmcs, phys_addr);
643}
644
Nadav Har'Eld462b812011-05-24 15:26:10 +0300645static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300647 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800648 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800649
Nadav Har'Eld462b812011-05-24 15:26:10 +0300650 if (loaded_vmcs->cpu != cpu)
651 return; /* vcpu migration can race with cpu offline */
652 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653 per_cpu(current_vmcs, cpu) = NULL;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300654 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
655 loaded_vmcs_init(loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656}
657
Nadav Har'Eld462b812011-05-24 15:26:10 +0300658static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800659{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300660 if (loaded_vmcs->cpu != -1)
661 smp_call_function_single(
662 loaded_vmcs->cpu, __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800663}
664
Gui Jianfeng1760dd42010-06-07 10:33:27 +0800665static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800666{
667 if (vmx->vpid == 0)
668 return;
669
Gui Jianfeng518c8ae2010-06-04 08:51:39 +0800670 if (cpu_has_vmx_invvpid_single())
671 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800672}
673
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800674static inline void vpid_sync_vcpu_global(void)
675{
676 if (cpu_has_vmx_invvpid_global())
677 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
678}
679
680static inline void vpid_sync_context(struct vcpu_vmx *vmx)
681{
682 if (cpu_has_vmx_invvpid_single())
Gui Jianfeng1760dd42010-06-07 10:33:27 +0800683 vpid_sync_vcpu_single(vmx);
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800684 else
685 vpid_sync_vcpu_global();
686}
687
Sheng Yang14394422008-04-28 12:24:45 +0800688static inline void ept_sync_global(void)
689{
690 if (cpu_has_vmx_invept_global())
691 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
692}
693
694static inline void ept_sync_context(u64 eptp)
695{
Avi Kivity089d0342009-03-23 18:26:32 +0200696 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800697 if (cpu_has_vmx_invept_context())
698 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
699 else
700 ept_sync_global();
701 }
702}
703
704static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
705{
Avi Kivity089d0342009-03-23 18:26:32 +0200706 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800707 if (cpu_has_vmx_invept_individual_addr())
708 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
709 eptp, gpa);
710 else
711 ept_sync_context(eptp);
712 }
713}
714
Avi Kivity96304212011-05-15 10:13:13 -0400715static __always_inline unsigned long vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716{
Avi Kivity5e520e62011-05-15 10:13:12 -0400717 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800718
Avi Kivity5e520e62011-05-15 10:13:12 -0400719 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
720 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721 return value;
722}
723
Avi Kivity96304212011-05-15 10:13:13 -0400724static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800725{
726 return vmcs_readl(field);
727}
728
Avi Kivity96304212011-05-15 10:13:13 -0400729static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730{
731 return vmcs_readl(field);
732}
733
Avi Kivity96304212011-05-15 10:13:13 -0400734static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800736#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737 return vmcs_readl(field);
738#else
739 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
740#endif
741}
742
Avi Kivitye52de1b2007-01-05 16:36:56 -0800743static noinline void vmwrite_error(unsigned long field, unsigned long value)
744{
745 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
746 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
747 dump_stack();
748}
749
Avi Kivity6aa8b732006-12-10 02:21:36 -0800750static void vmcs_writel(unsigned long field, unsigned long value)
751{
752 u8 error;
753
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300754 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400755 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800756 if (unlikely(error))
757 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758}
759
760static void vmcs_write16(unsigned long field, u16 value)
761{
762 vmcs_writel(field, value);
763}
764
765static void vmcs_write32(unsigned long field, u32 value)
766{
767 vmcs_writel(field, value);
768}
769
770static void vmcs_write64(unsigned long field, u64 value)
771{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300773#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774 asm volatile ("");
775 vmcs_writel(field+1, value >> 32);
776#endif
777}
778
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300779static void vmcs_clear_bits(unsigned long field, u32 mask)
780{
781 vmcs_writel(field, vmcs_readl(field) & ~mask);
782}
783
784static void vmcs_set_bits(unsigned long field, u32 mask)
785{
786 vmcs_writel(field, vmcs_readl(field) | mask);
787}
788
Avi Kivity2fb92db2011-04-27 19:42:18 +0300789static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
790{
791 vmx->segment_cache.bitmask = 0;
792}
793
794static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
795 unsigned field)
796{
797 bool ret;
798 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
799
800 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
801 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
802 vmx->segment_cache.bitmask = 0;
803 }
804 ret = vmx->segment_cache.bitmask & mask;
805 vmx->segment_cache.bitmask |= mask;
806 return ret;
807}
808
809static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
810{
811 u16 *p = &vmx->segment_cache.seg[seg].selector;
812
813 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
814 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
815 return *p;
816}
817
818static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
819{
820 ulong *p = &vmx->segment_cache.seg[seg].base;
821
822 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
823 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
824 return *p;
825}
826
827static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
828{
829 u32 *p = &vmx->segment_cache.seg[seg].limit;
830
831 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
832 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
833 return *p;
834}
835
836static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
837{
838 u32 *p = &vmx->segment_cache.seg[seg].ar;
839
840 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
841 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
842 return *p;
843}
844
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300845static void update_exception_bitmap(struct kvm_vcpu *vcpu)
846{
847 u32 eb;
848
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100849 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
850 (1u << NM_VECTOR) | (1u << DB_VECTOR);
851 if ((vcpu->guest_debug &
852 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
853 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
854 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300855 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300856 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200857 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800858 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +0200859 if (vcpu->fpu_active)
860 eb &= ~(1u << NM_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300861 vmcs_write32(EXCEPTION_BITMAP, eb);
862}
863
Avi Kivity61d2ef22010-04-28 16:40:38 +0300864static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
865{
866 unsigned i;
867 struct msr_autoload *m = &vmx->msr_autoload;
868
Avi Kivity110312c2010-12-21 12:54:20 +0200869 if (msr == MSR_EFER && cpu_has_load_ia32_efer) {
870 vmcs_clear_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER);
871 vmcs_clear_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER);
872 return;
873 }
874
Avi Kivity61d2ef22010-04-28 16:40:38 +0300875 for (i = 0; i < m->nr; ++i)
876 if (m->guest[i].index == msr)
877 break;
878
879 if (i == m->nr)
880 return;
881 --m->nr;
882 m->guest[i] = m->guest[m->nr];
883 m->host[i] = m->host[m->nr];
884 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
885 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
886}
887
888static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
889 u64 guest_val, u64 host_val)
890{
891 unsigned i;
892 struct msr_autoload *m = &vmx->msr_autoload;
893
Avi Kivity110312c2010-12-21 12:54:20 +0200894 if (msr == MSR_EFER && cpu_has_load_ia32_efer) {
895 vmcs_write64(GUEST_IA32_EFER, guest_val);
896 vmcs_write64(HOST_IA32_EFER, host_val);
897 vmcs_set_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER);
898 vmcs_set_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER);
899 return;
900 }
901
Avi Kivity61d2ef22010-04-28 16:40:38 +0300902 for (i = 0; i < m->nr; ++i)
903 if (m->guest[i].index == msr)
904 break;
905
906 if (i == m->nr) {
907 ++m->nr;
908 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
909 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
910 }
911
912 m->guest[i].index = msr;
913 m->guest[i].value = guest_val;
914 m->host[i].index = msr;
915 m->host[i].value = host_val;
916}
917
Avi Kivity33ed6322007-05-02 16:54:03 +0300918static void reload_tss(void)
919{
Avi Kivity33ed6322007-05-02 16:54:03 +0300920 /*
921 * VT restores TR but not its size. Useless.
922 */
Avi Kivityd3591922010-07-26 18:32:39 +0300923 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +0200924 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300925
Avi Kivityd3591922010-07-26 18:32:39 +0300926 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +0300927 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
928 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300929}
930
Avi Kivity92c0d902009-10-29 11:00:16 +0200931static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +0300932{
Roel Kluin3a34a882009-08-04 02:08:45 -0700933 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300934 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300935
Avi Kivityf6801df2010-01-21 15:31:50 +0200936 guest_efer = vmx->vcpu.arch.efer;
Roel Kluin3a34a882009-08-04 02:08:45 -0700937
Avi Kivity51c6cf62007-08-29 03:48:05 +0300938 /*
939 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
940 * outside long mode
941 */
942 ignore_bits = EFER_NX | EFER_SCE;
943#ifdef CONFIG_X86_64
944 ignore_bits |= EFER_LMA | EFER_LME;
945 /* SCE is meaningful only in long mode on Intel */
946 if (guest_efer & EFER_LMA)
947 ignore_bits &= ~(u64)EFER_SCE;
948#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +0300949 guest_efer &= ~ignore_bits;
950 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +0300951 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +0200952 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +0300953
954 clear_atomic_switch_msr(vmx, MSR_EFER);
955 /* On ept, can't emulate nx, and must switch nx atomically */
956 if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
957 guest_efer = vmx->vcpu.arch.efer;
958 if (!(guest_efer & EFER_LMA))
959 guest_efer &= ~EFER_LME;
960 add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
961 return false;
962 }
963
Avi Kivity26bb0982009-09-07 11:14:12 +0300964 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300965}
966
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200967static unsigned long segment_base(u16 selector)
968{
Avi Kivityd3591922010-07-26 18:32:39 +0300969 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200970 struct desc_struct *d;
971 unsigned long table_base;
972 unsigned long v;
973
974 if (!(selector & ~3))
975 return 0;
976
Avi Kivityd3591922010-07-26 18:32:39 +0300977 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200978
979 if (selector & 4) { /* from ldt */
980 u16 ldt_selector = kvm_read_ldt();
981
982 if (!(ldt_selector & ~3))
983 return 0;
984
985 table_base = segment_base(ldt_selector);
986 }
987 d = (struct desc_struct *)(table_base + (selector & ~7));
988 v = get_desc_base(d);
989#ifdef CONFIG_X86_64
990 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
991 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
992#endif
993 return v;
994}
995
996static inline unsigned long kvm_read_tr_base(void)
997{
998 u16 tr;
999 asm("str %0" : "=g"(tr));
1000 return segment_base(tr);
1001}
1002
Avi Kivity04d2cc72007-09-10 18:10:54 +03001003static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001004{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001005 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001006 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001007
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001008 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001009 return;
1010
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001011 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001012 /*
1013 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1014 * allow segment selectors with cpl > 0 or ti == 1.
1015 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001016 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02001017 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02001018 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001019 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001020 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001021 vmx->host_state.fs_reload_needed = 0;
1022 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03001023 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001024 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001025 }
Avi Kivity9581d442010-10-19 16:46:55 +02001026 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001027 if (!(vmx->host_state.gs_sel & 7))
1028 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001029 else {
1030 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001031 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001032 }
1033
1034#ifdef CONFIG_X86_64
1035 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
1036 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
1037#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001038 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
1039 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03001040#endif
Avi Kivity707c0872007-05-02 17:33:43 +03001041
1042#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001043 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
1044 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03001045 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03001046#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03001047 for (i = 0; i < vmx->save_nmsrs; ++i)
1048 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02001049 vmx->guest_msrs[i].data,
1050 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03001051}
1052
Avi Kivitya9b21b62008-06-24 11:48:49 +03001053static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001054{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001055 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001056 return;
1057
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001058 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001059 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02001060#ifdef CONFIG_X86_64
1061 if (is_long_mode(&vmx->vcpu))
1062 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1063#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001064 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001065 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001066#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02001067 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001068#else
1069 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001070#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001071 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02001072 if (vmx->host_state.fs_reload_needed)
1073 loadsegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001074 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001075#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001076 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001077#endif
Avi Kivity1c11e712010-05-03 16:05:44 +03001078 if (current_thread_info()->status & TS_USEDFPU)
1079 clts();
Avi Kivity3444d7d2010-07-26 18:32:38 +03001080 load_gdt(&__get_cpu_var(host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03001081}
1082
Avi Kivitya9b21b62008-06-24 11:48:49 +03001083static void vmx_load_host_state(struct vcpu_vmx *vmx)
1084{
1085 preempt_disable();
1086 __vmx_load_host_state(vmx);
1087 preempt_enable();
1088}
1089
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090/*
1091 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1092 * vcpu mutex is already taken.
1093 */
Avi Kivity15ad7142007-07-11 18:17:21 +03001094static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001096 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001097 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001099 if (!vmm_exclusive)
1100 kvm_cpu_vmxon(phys_addr);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001101 else if (vmx->loaded_vmcs->cpu != cpu)
1102 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001103
Nadav Har'Eld462b812011-05-24 15:26:10 +03001104 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1105 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1106 vmcs_load(vmx->loaded_vmcs->vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107 }
1108
Nadav Har'Eld462b812011-05-24 15:26:10 +03001109 if (vmx->loaded_vmcs->cpu != cpu) {
Avi Kivityd3591922010-07-26 18:32:39 +03001110 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111 unsigned long sysenter_esp;
1112
Avi Kivitya8eeb042010-05-10 12:34:53 +03001113 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001114 local_irq_disable();
Nadav Har'Eld462b812011-05-24 15:26:10 +03001115 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1116 &per_cpu(loaded_vmcss_on_cpu, cpu));
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001117 local_irq_enable();
1118
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119 /*
1120 * Linux uses per-cpu TSS and GDT, so set these when switching
1121 * processors.
1122 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001123 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03001124 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125
1126 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1127 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Nadav Har'Eld462b812011-05-24 15:26:10 +03001128 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130}
1131
1132static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1133{
Avi Kivitya9b21b62008-06-24 11:48:49 +03001134 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001135 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03001136 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
1137 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001138 kvm_cpu_vmxoff();
1139 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001140}
1141
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001142static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
1143{
Avi Kivity81231c62010-01-24 16:26:40 +02001144 ulong cr0;
1145
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001146 if (vcpu->fpu_active)
1147 return;
1148 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02001149 cr0 = vmcs_readl(GUEST_CR0);
1150 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
1151 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
1152 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001153 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001154 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
1155 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001156}
1157
Avi Kivityedcafe32009-12-30 18:07:40 +02001158static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1159
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001160static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
1161{
Avi Kivityedcafe32009-12-30 18:07:40 +02001162 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02001163 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001164 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001165 vcpu->arch.cr0_guest_owned_bits = 0;
1166 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
1167 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001168}
1169
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1171{
Avi Kivity78ac8b42010-04-08 18:19:35 +03001172 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001173
Avi Kivity6de12732011-03-07 12:51:22 +02001174 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1175 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1176 rflags = vmcs_readl(GUEST_RFLAGS);
1177 if (to_vmx(vcpu)->rmode.vm86_active) {
1178 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1179 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1180 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1181 }
1182 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001183 }
Avi Kivity6de12732011-03-07 12:51:22 +02001184 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185}
1186
1187static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1188{
Avi Kivity6de12732011-03-07 12:51:22 +02001189 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity69c73022011-03-07 15:26:44 +02001190 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity6de12732011-03-07 12:51:22 +02001191 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001192 if (to_vmx(vcpu)->rmode.vm86_active) {
1193 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001194 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001195 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196 vmcs_writel(GUEST_RFLAGS, rflags);
1197}
1198
Glauber Costa2809f5d2009-05-12 16:21:05 -04001199static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1200{
1201 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1202 int ret = 0;
1203
1204 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001205 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001206 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001207 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001208
1209 return ret & mask;
1210}
1211
1212static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1213{
1214 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1215 u32 interruptibility = interruptibility_old;
1216
1217 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1218
Jan Kiszka48005f62010-02-19 19:38:07 +01001219 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001220 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001221 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001222 interruptibility |= GUEST_INTR_STATE_STI;
1223
1224 if ((interruptibility != interruptibility_old))
1225 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1226}
1227
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1229{
1230 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001232 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001233 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001234 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001235
Glauber Costa2809f5d2009-05-12 16:21:05 -04001236 /* skipping an emulated instruction also counts */
1237 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001238}
1239
Anthony Liguori443381a2010-12-06 10:53:38 -06001240static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
1241{
1242 /* Ensure that we clear the HLT state in the VMCS. We don't need to
1243 * explicitly skip the instruction because if the HLT state is set, then
1244 * the instruction is already executing and RIP has already been
1245 * advanced. */
1246 if (!yield_on_hlt &&
1247 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
1248 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
1249}
1250
Avi Kivity298101d2007-11-25 13:41:11 +02001251static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02001252 bool has_error_code, u32 error_code,
1253 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02001254{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001255 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001256 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001257
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001258 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001259 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001260 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1261 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001262
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001263 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001264 int inc_eip = 0;
1265 if (kvm_exception_is_soft(nr))
1266 inc_eip = vcpu->arch.event_exit_inst_len;
1267 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02001268 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001269 return;
1270 }
1271
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001272 if (kvm_exception_is_soft(nr)) {
1273 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1274 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001275 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1276 } else
1277 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1278
1279 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Anthony Liguori443381a2010-12-06 10:53:38 -06001280 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02001281}
1282
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001283static bool vmx_rdtscp_supported(void)
1284{
1285 return cpu_has_vmx_rdtscp();
1286}
1287
Avi Kivity6aa8b732006-12-10 02:21:36 -08001288/*
Eddie Donga75beee2007-05-17 18:55:15 +03001289 * Swap MSR entry in host/guest MSR entry array.
1290 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001291static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03001292{
Avi Kivity26bb0982009-09-07 11:14:12 +03001293 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001294
1295 tmp = vmx->guest_msrs[to];
1296 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1297 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001298}
1299
1300/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001301 * Set up the vmcs to automatically save and restore system
1302 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1303 * mode, as fiddling with msrs is very expensive.
1304 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001305static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001306{
Avi Kivity26bb0982009-09-07 11:14:12 +03001307 int save_nmsrs, index;
Avi Kivity58972972009-02-24 22:26:47 +02001308 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +03001309
Avi Kivity33f9c502008-02-27 16:06:57 +02001310 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +03001311 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001312#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001313 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10001314 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03001315 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001316 move_msr_up(vmx, index, save_nmsrs++);
1317 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001318 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001319 move_msr_up(vmx, index, save_nmsrs++);
1320 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001321 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001322 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001323 index = __find_msr_index(vmx, MSR_TSC_AUX);
1324 if (index >= 0 && vmx->rdtscp_enabled)
1325 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03001326 /*
Brian Gerst8c065852010-07-17 09:03:26 -04001327 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03001328 * if efer.sce is enabled.
1329 */
Brian Gerst8c065852010-07-17 09:03:26 -04001330 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02001331 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10001332 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001333 }
Eddie Donga75beee2007-05-17 18:55:15 +03001334#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02001335 index = __find_msr_index(vmx, MSR_EFER);
1336 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03001337 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001338
Avi Kivity26bb0982009-09-07 11:14:12 +03001339 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02001340
1341 if (cpu_has_vmx_msr_bitmap()) {
1342 if (is_long_mode(&vmx->vcpu))
1343 msr_bitmap = vmx_msr_bitmap_longmode;
1344 else
1345 msr_bitmap = vmx_msr_bitmap_legacy;
1346
1347 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
1348 }
Avi Kivitye38aea32007-04-19 13:22:48 +03001349}
1350
1351/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352 * reads and returns guest's timestamp counter "register"
1353 * guest_tsc = host_tsc + tsc_offset -- 21.3
1354 */
1355static u64 guest_read_tsc(void)
1356{
1357 u64 host_tsc, tsc_offset;
1358
1359 rdtscll(host_tsc);
1360 tsc_offset = vmcs_read64(TSC_OFFSET);
1361 return host_tsc + tsc_offset;
1362}
1363
1364/*
Joerg Roedel4051b182011-03-25 09:44:49 +01001365 * Empty call-back. Needs to be implemented when VMX enables the SET_TSC_KHZ
1366 * ioctl. In this case the call-back should update internal vmx state to make
1367 * the changes effective.
1368 */
1369static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
1370{
1371 /* Nothing to do here */
1372}
1373
1374/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10001375 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10001377static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378{
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001379 vmcs_write64(TSC_OFFSET, offset);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380}
1381
Zachary Amsdene48672f2010-08-19 22:07:23 -10001382static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
1383{
1384 u64 offset = vmcs_read64(TSC_OFFSET);
1385 vmcs_write64(TSC_OFFSET, offset + adjustment);
1386}
1387
Joerg Roedel857e4092011-03-25 09:44:50 +01001388static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1389{
1390 return target_tsc - native_read_tsc();
1391}
1392
Nadav Har'El801d3422011-05-25 23:02:23 +03001393static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
1394{
1395 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
1396 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
1397}
1398
1399/*
1400 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1401 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1402 * all guests if the "nested" module option is off, and can also be disabled
1403 * for a single guest by disabling its VMX cpuid bit.
1404 */
1405static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
1406{
1407 return nested && guest_cpuid_has_vmx(vcpu);
1408}
1409
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001411 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
1412 * returned for the various VMX controls MSRs when nested VMX is enabled.
1413 * The same values should also be used to verify that vmcs12 control fields are
1414 * valid during nested entry from L1 to L2.
1415 * Each of these control msrs has a low and high 32-bit half: A low bit is on
1416 * if the corresponding bit in the (32-bit) control field *must* be on, and a
1417 * bit in the high half is on if the corresponding bit in the control field
1418 * may be on. See also vmx_control_verify().
1419 * TODO: allow these variables to be modified (downgraded) by module options
1420 * or other means.
1421 */
1422static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high;
1423static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high;
1424static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high;
1425static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high;
1426static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high;
1427static __init void nested_vmx_setup_ctls_msrs(void)
1428{
1429 /*
1430 * Note that as a general rule, the high half of the MSRs (bits in
1431 * the control fields which may be 1) should be initialized by the
1432 * intersection of the underlying hardware's MSR (i.e., features which
1433 * can be supported) and the list of features we want to expose -
1434 * because they are known to be properly supported in our code.
1435 * Also, usually, the low half of the MSRs (bits which must be 1) can
1436 * be set to 0, meaning that L1 may turn off any of these bits. The
1437 * reason is that if one of these bits is necessary, it will appear
1438 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
1439 * fields of vmcs01 and vmcs02, will turn these bits off - and
1440 * nested_vmx_exit_handled() will not pass related exits to L1.
1441 * These rules have exceptions below.
1442 */
1443
1444 /* pin-based controls */
1445 /*
1446 * According to the Intel spec, if bit 55 of VMX_BASIC is off (as it is
1447 * in our case), bits 1, 2 and 4 (i.e., 0x16) must be 1 in this MSR.
1448 */
1449 nested_vmx_pinbased_ctls_low = 0x16 ;
1450 nested_vmx_pinbased_ctls_high = 0x16 |
1451 PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING |
1452 PIN_BASED_VIRTUAL_NMIS;
1453
1454 /* exit controls */
1455 nested_vmx_exit_ctls_low = 0;
1456#ifdef CONFIG_X86_64
1457 nested_vmx_exit_ctls_high = VM_EXIT_HOST_ADDR_SPACE_SIZE;
1458#else
1459 nested_vmx_exit_ctls_high = 0;
1460#endif
1461
1462 /* entry controls */
1463 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
1464 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high);
1465 nested_vmx_entry_ctls_low = 0;
1466 nested_vmx_entry_ctls_high &=
1467 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_IA32E_MODE;
1468
1469 /* cpu-based controls */
1470 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
1471 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high);
1472 nested_vmx_procbased_ctls_low = 0;
1473 nested_vmx_procbased_ctls_high &=
1474 CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_USE_TSC_OFFSETING |
1475 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
1476 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
1477 CPU_BASED_CR3_STORE_EXITING |
1478#ifdef CONFIG_X86_64
1479 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
1480#endif
1481 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
1482 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING |
1483 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
1484 /*
1485 * We can allow some features even when not supported by the
1486 * hardware. For example, L1 can specify an MSR bitmap - and we
1487 * can use it to avoid exits to L1 - even when L0 runs L2
1488 * without MSR bitmaps.
1489 */
1490 nested_vmx_procbased_ctls_high |= CPU_BASED_USE_MSR_BITMAPS;
1491
1492 /* secondary cpu-based controls */
1493 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
1494 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high);
1495 nested_vmx_secondary_ctls_low = 0;
1496 nested_vmx_secondary_ctls_high &=
1497 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1498}
1499
1500static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
1501{
1502 /*
1503 * Bits 0 in high must be 0, and bits 1 in low must be 1.
1504 */
1505 return ((control & high) | low) == control;
1506}
1507
1508static inline u64 vmx_control_msr(u32 low, u32 high)
1509{
1510 return low | ((u64)high << 32);
1511}
1512
1513/*
1514 * If we allow our guest to use VMX instructions (i.e., nested VMX), we should
1515 * also let it use VMX-specific MSRs.
1516 * vmx_get_vmx_msr() and vmx_set_vmx_msr() return 1 when we handled a
1517 * VMX-specific MSR, or 0 when we haven't (and the caller should handle it
1518 * like all other MSRs).
1519 */
1520static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1521{
1522 if (!nested_vmx_allowed(vcpu) && msr_index >= MSR_IA32_VMX_BASIC &&
1523 msr_index <= MSR_IA32_VMX_TRUE_ENTRY_CTLS) {
1524 /*
1525 * According to the spec, processors which do not support VMX
1526 * should throw a #GP(0) when VMX capability MSRs are read.
1527 */
1528 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
1529 return 1;
1530 }
1531
1532 switch (msr_index) {
1533 case MSR_IA32_FEATURE_CONTROL:
1534 *pdata = 0;
1535 break;
1536 case MSR_IA32_VMX_BASIC:
1537 /*
1538 * This MSR reports some information about VMX support. We
1539 * should return information about the VMX we emulate for the
1540 * guest, and the VMCS structure we give it - not about the
1541 * VMX support of the underlying hardware.
1542 */
1543 *pdata = VMCS12_REVISION |
1544 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
1545 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
1546 break;
1547 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1548 case MSR_IA32_VMX_PINBASED_CTLS:
1549 *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low,
1550 nested_vmx_pinbased_ctls_high);
1551 break;
1552 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1553 case MSR_IA32_VMX_PROCBASED_CTLS:
1554 *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low,
1555 nested_vmx_procbased_ctls_high);
1556 break;
1557 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1558 case MSR_IA32_VMX_EXIT_CTLS:
1559 *pdata = vmx_control_msr(nested_vmx_exit_ctls_low,
1560 nested_vmx_exit_ctls_high);
1561 break;
1562 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1563 case MSR_IA32_VMX_ENTRY_CTLS:
1564 *pdata = vmx_control_msr(nested_vmx_entry_ctls_low,
1565 nested_vmx_entry_ctls_high);
1566 break;
1567 case MSR_IA32_VMX_MISC:
1568 *pdata = 0;
1569 break;
1570 /*
1571 * These MSRs specify bits which the guest must keep fixed (on or off)
1572 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
1573 * We picked the standard core2 setting.
1574 */
1575#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
1576#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
1577 case MSR_IA32_VMX_CR0_FIXED0:
1578 *pdata = VMXON_CR0_ALWAYSON;
1579 break;
1580 case MSR_IA32_VMX_CR0_FIXED1:
1581 *pdata = -1ULL;
1582 break;
1583 case MSR_IA32_VMX_CR4_FIXED0:
1584 *pdata = VMXON_CR4_ALWAYSON;
1585 break;
1586 case MSR_IA32_VMX_CR4_FIXED1:
1587 *pdata = -1ULL;
1588 break;
1589 case MSR_IA32_VMX_VMCS_ENUM:
1590 *pdata = 0x1f;
1591 break;
1592 case MSR_IA32_VMX_PROCBASED_CTLS2:
1593 *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low,
1594 nested_vmx_secondary_ctls_high);
1595 break;
1596 case MSR_IA32_VMX_EPT_VPID_CAP:
1597 /* Currently, no nested ept or nested vpid */
1598 *pdata = 0;
1599 break;
1600 default:
1601 return 0;
1602 }
1603
1604 return 1;
1605}
1606
1607static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1608{
1609 if (!nested_vmx_allowed(vcpu))
1610 return 0;
1611
1612 if (msr_index == MSR_IA32_FEATURE_CONTROL)
1613 /* TODO: the right thing. */
1614 return 1;
1615 /*
1616 * No need to treat VMX capability MSRs specially: If we don't handle
1617 * them, handle_wrmsr will #GP(0), which is correct (they are readonly)
1618 */
1619 return 0;
1620}
1621
1622/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001623 * Reads an msr value (of 'msr_index') into 'pdata'.
1624 * Returns 0 on success, non-0 otherwise.
1625 * Assumes vcpu_load() was already called.
1626 */
1627static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1628{
1629 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +03001630 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631
1632 if (!pdata) {
1633 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
1634 return -EINVAL;
1635 }
1636
1637 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001638#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639 case MSR_FS_BASE:
1640 data = vmcs_readl(GUEST_FS_BASE);
1641 break;
1642 case MSR_GS_BASE:
1643 data = vmcs_readl(GUEST_GS_BASE);
1644 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001645 case MSR_KERNEL_GS_BASE:
1646 vmx_load_host_state(to_vmx(vcpu));
1647 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
1648 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001649#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001650 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001651 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301652 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001653 data = guest_read_tsc();
1654 break;
1655 case MSR_IA32_SYSENTER_CS:
1656 data = vmcs_read32(GUEST_SYSENTER_CS);
1657 break;
1658 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001659 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660 break;
1661 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001662 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001663 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001664 case MSR_TSC_AUX:
1665 if (!to_vmx(vcpu)->rdtscp_enabled)
1666 return 1;
1667 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001668 default:
Avi Kivity26bb0982009-09-07 11:14:12 +03001669 vmx_load_host_state(to_vmx(vcpu));
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001670 if (vmx_get_vmx_msr(vcpu, msr_index, pdata))
1671 return 0;
Rusty Russell8b9cf982007-07-30 16:31:43 +10001672 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001673 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001674 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity3bab1f52006-12-29 16:49:48 -08001675 data = msr->data;
1676 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001677 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001678 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679 }
1680
1681 *pdata = data;
1682 return 0;
1683}
1684
1685/*
1686 * Writes msr value into into the appropriate "register".
1687 * Returns 0 on success, non-0 otherwise.
1688 * Assumes vcpu_load() was already called.
1689 */
1690static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1691{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001692 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001693 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03001694 int ret = 0;
1695
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001697 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001698 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001699 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001700 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001701#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001702 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001703 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001704 vmcs_writel(GUEST_FS_BASE, data);
1705 break;
1706 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001707 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001708 vmcs_writel(GUEST_GS_BASE, data);
1709 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001710 case MSR_KERNEL_GS_BASE:
1711 vmx_load_host_state(vmx);
1712 vmx->msr_guest_kernel_gs_base = data;
1713 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714#endif
1715 case MSR_IA32_SYSENTER_CS:
1716 vmcs_write32(GUEST_SYSENTER_CS, data);
1717 break;
1718 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001719 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720 break;
1721 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001722 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001723 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301724 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10001725 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001727 case MSR_IA32_CR_PAT:
1728 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1729 vmcs_write64(GUEST_IA32_PAT, data);
1730 vcpu->arch.pat = data;
1731 break;
1732 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001733 ret = kvm_set_msr_common(vcpu, msr_index, data);
1734 break;
1735 case MSR_TSC_AUX:
1736 if (!vmx->rdtscp_enabled)
1737 return 1;
1738 /* Check reserved bit, higher 32 bits should be zero */
1739 if ((data >> 32) != 0)
1740 return 1;
1741 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001742 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001743 if (vmx_set_vmx_msr(vcpu, msr_index, data))
1744 break;
Rusty Russell8b9cf982007-07-30 16:31:43 +10001745 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001746 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001747 vmx_load_host_state(vmx);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001748 msr->data = data;
1749 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001751 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001752 }
1753
Eddie Dong2cc51562007-05-21 07:28:09 +03001754 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001755}
1756
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001757static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001758{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001759 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1760 switch (reg) {
1761 case VCPU_REGS_RSP:
1762 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1763 break;
1764 case VCPU_REGS_RIP:
1765 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1766 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001767 case VCPU_EXREG_PDPTR:
1768 if (enable_ept)
1769 ept_save_pdptrs(vcpu);
1770 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001771 default:
1772 break;
1773 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001774}
1775
Jan Kiszka355be0b2009-10-03 00:31:21 +02001776static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001777{
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001778 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1779 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1780 else
1781 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1782
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001783 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001784}
1785
1786static __init int cpu_has_kvm_support(void)
1787{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001788 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001789}
1790
1791static __init int vmx_disabled_by_bios(void)
1792{
1793 u64 msr;
1794
1795 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04001796 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08001797 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04001798 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
1799 && tboot_enabled())
1800 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08001801 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04001802 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08001803 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08001804 && !tboot_enabled()) {
1805 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08001806 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04001807 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08001808 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08001809 /* launched w/o TXT and VMX disabled */
1810 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
1811 && !tboot_enabled())
1812 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04001813 }
1814
1815 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001816}
1817
Dongxiao Xu7725b892010-05-11 18:29:38 +08001818static void kvm_cpu_vmxon(u64 addr)
1819{
1820 asm volatile (ASM_VMX_VMXON_RAX
1821 : : "a"(&addr), "m"(addr)
1822 : "memory", "cc");
1823}
1824
Alexander Graf10474ae2009-09-15 11:37:46 +02001825static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001826{
1827 int cpu = raw_smp_processor_id();
1828 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04001829 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001830
Alexander Graf10474ae2009-09-15 11:37:46 +02001831 if (read_cr4() & X86_CR4_VMXE)
1832 return -EBUSY;
1833
Nadav Har'Eld462b812011-05-24 15:26:10 +03001834 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04001836
1837 test_bits = FEATURE_CONTROL_LOCKED;
1838 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
1839 if (tboot_enabled())
1840 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
1841
1842 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001843 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04001844 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
1845 }
Rusty Russell66aee912007-07-17 23:34:16 +10001846 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Alexander Graf10474ae2009-09-15 11:37:46 +02001847
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001848 if (vmm_exclusive) {
1849 kvm_cpu_vmxon(phys_addr);
1850 ept_sync_global();
1851 }
Alexander Graf10474ae2009-09-15 11:37:46 +02001852
Avi Kivity3444d7d2010-07-26 18:32:38 +03001853 store_gdt(&__get_cpu_var(host_gdt));
1854
Alexander Graf10474ae2009-09-15 11:37:46 +02001855 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001856}
1857
Nadav Har'Eld462b812011-05-24 15:26:10 +03001858static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03001859{
1860 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03001861 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03001862
Nadav Har'Eld462b812011-05-24 15:26:10 +03001863 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
1864 loaded_vmcss_on_cpu_link)
1865 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03001866}
1867
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001868
1869/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1870 * tricks.
1871 */
1872static void kvm_cpu_vmxoff(void)
1873{
1874 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001875}
1876
Avi Kivity6aa8b732006-12-10 02:21:36 -08001877static void hardware_disable(void *garbage)
1878{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001879 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03001880 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001881 kvm_cpu_vmxoff();
1882 }
Dongxiao Xu7725b892010-05-11 18:29:38 +08001883 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884}
1885
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001886static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001887 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001888{
1889 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001890 u32 ctl = ctl_min | ctl_opt;
1891
1892 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1893
1894 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1895 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1896
1897 /* Ensure minimum (required) set of control bits are supported. */
1898 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001899 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001900
1901 *result = ctl;
1902 return 0;
1903}
1904
Avi Kivity110312c2010-12-21 12:54:20 +02001905static __init bool allow_1_setting(u32 msr, u32 ctl)
1906{
1907 u32 vmx_msr_low, vmx_msr_high;
1908
1909 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1910 return vmx_msr_high & ctl;
1911}
1912
Yang, Sheng002c7f72007-07-31 14:23:01 +03001913static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001914{
1915 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001916 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001917 u32 _pin_based_exec_control = 0;
1918 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001919 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001920 u32 _vmexit_control = 0;
1921 u32 _vmentry_control = 0;
1922
1923 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001924 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001925 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1926 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001927 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001928
Anthony Liguori443381a2010-12-06 10:53:38 -06001929 min =
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001930#ifdef CONFIG_X86_64
1931 CPU_BASED_CR8_LOAD_EXITING |
1932 CPU_BASED_CR8_STORE_EXITING |
1933#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001934 CPU_BASED_CR3_LOAD_EXITING |
1935 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001936 CPU_BASED_USE_IO_BITMAPS |
1937 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001938 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08001939 CPU_BASED_MWAIT_EXITING |
1940 CPU_BASED_MONITOR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001941 CPU_BASED_INVLPG_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06001942
1943 if (yield_on_hlt)
1944 min |= CPU_BASED_HLT_EXITING;
1945
Sheng Yangf78e0e22007-10-29 09:40:42 +08001946 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001947 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001948 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001949 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1950 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001951 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001952#ifdef CONFIG_X86_64
1953 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1954 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1955 ~CPU_BASED_CR8_STORE_EXITING;
1956#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001957 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001958 min2 = 0;
1959 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001960 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001961 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001962 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001963 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001964 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
1965 SECONDARY_EXEC_RDTSCP;
Sheng Yangd56f5462008-04-25 10:13:16 +08001966 if (adjust_vmx_controls(min2, opt2,
1967 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001968 &_cpu_based_2nd_exec_control) < 0)
1969 return -EIO;
1970 }
1971#ifndef CONFIG_X86_64
1972 if (!(_cpu_based_2nd_exec_control &
1973 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1974 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1975#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001976 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001977 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1978 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03001979 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
1980 CPU_BASED_CR3_STORE_EXITING |
1981 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001982 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1983 vmx_capability.ept, vmx_capability.vpid);
1984 }
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001985
1986 min = 0;
1987#ifdef CONFIG_X86_64
1988 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1989#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001990 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001991 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1992 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001993 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001994
Sheng Yang468d4722008-10-09 16:01:55 +08001995 min = 0;
1996 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001997 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1998 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001999 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002000
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002001 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002002
2003 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2004 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002005 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002006
2007#ifdef CONFIG_X86_64
2008 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2009 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002010 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002011#endif
2012
2013 /* Require Write-Back (WB) memory type for VMCS accesses. */
2014 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002015 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002016
Yang, Sheng002c7f72007-07-31 14:23:01 +03002017 vmcs_conf->size = vmx_msr_high & 0x1fff;
2018 vmcs_conf->order = get_order(vmcs_config.size);
2019 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002020
Yang, Sheng002c7f72007-07-31 14:23:01 +03002021 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2022 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002023 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002024 vmcs_conf->vmexit_ctrl = _vmexit_control;
2025 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002026
Avi Kivity110312c2010-12-21 12:54:20 +02002027 cpu_has_load_ia32_efer =
2028 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2029 VM_ENTRY_LOAD_IA32_EFER)
2030 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2031 VM_EXIT_LOAD_IA32_EFER);
2032
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002033 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002034}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002035
2036static struct vmcs *alloc_vmcs_cpu(int cpu)
2037{
2038 int node = cpu_to_node(cpu);
2039 struct page *pages;
2040 struct vmcs *vmcs;
2041
Mel Gorman6484eb32009-06-16 15:31:54 -07002042 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002043 if (!pages)
2044 return NULL;
2045 vmcs = page_address(pages);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002046 memset(vmcs, 0, vmcs_config.size);
2047 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002048 return vmcs;
2049}
2050
2051static struct vmcs *alloc_vmcs(void)
2052{
Ingo Molnard3b2c332007-01-05 16:36:23 -08002053 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054}
2055
2056static void free_vmcs(struct vmcs *vmcs)
2057{
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002058 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002059}
2060
Nadav Har'Eld462b812011-05-24 15:26:10 +03002061/*
2062 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2063 */
2064static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
2065{
2066 if (!loaded_vmcs->vmcs)
2067 return;
2068 loaded_vmcs_clear(loaded_vmcs);
2069 free_vmcs(loaded_vmcs->vmcs);
2070 loaded_vmcs->vmcs = NULL;
2071}
2072
Sam Ravnborg39959582007-06-01 00:47:13 -07002073static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002074{
2075 int cpu;
2076
Zachary Amsden3230bb42009-09-29 11:38:37 -10002077 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10002079 per_cpu(vmxarea, cpu) = NULL;
2080 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002081}
2082
Avi Kivity6aa8b732006-12-10 02:21:36 -08002083static __init int alloc_kvm_area(void)
2084{
2085 int cpu;
2086
Zachary Amsden3230bb42009-09-29 11:38:37 -10002087 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088 struct vmcs *vmcs;
2089
2090 vmcs = alloc_vmcs_cpu(cpu);
2091 if (!vmcs) {
2092 free_kvm_area();
2093 return -ENOMEM;
2094 }
2095
2096 per_cpu(vmxarea, cpu) = vmcs;
2097 }
2098 return 0;
2099}
2100
2101static __init int hardware_setup(void)
2102{
Yang, Sheng002c7f72007-07-31 14:23:01 +03002103 if (setup_vmcs_config(&vmcs_config) < 0)
2104 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01002105
2106 if (boot_cpu_has(X86_FEATURE_NX))
2107 kvm_enable_efer_bits(EFER_NX);
2108
Sheng Yang93ba03c2009-04-01 15:52:32 +08002109 if (!cpu_has_vmx_vpid())
2110 enable_vpid = 0;
2111
Sheng Yang4bc9b982010-06-02 14:05:24 +08002112 if (!cpu_has_vmx_ept() ||
2113 !cpu_has_vmx_ept_4levels()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08002114 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002115 enable_unrestricted_guest = 0;
2116 }
2117
2118 if (!cpu_has_vmx_unrestricted_guest())
2119 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08002120
2121 if (!cpu_has_vmx_flexpriority())
2122 flexpriority_enabled = 0;
2123
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002124 if (!cpu_has_vmx_tpr_shadow())
2125 kvm_x86_ops->update_cr8_intercept = NULL;
2126
Marcelo Tosatti54dee992009-06-11 12:07:44 -03002127 if (enable_ept && !cpu_has_vmx_ept_2m_page())
2128 kvm_disable_largepages();
2129
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002130 if (!cpu_has_vmx_ple())
2131 ple_gap = 0;
2132
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002133 if (nested)
2134 nested_vmx_setup_ctls_msrs();
2135
Avi Kivity6aa8b732006-12-10 02:21:36 -08002136 return alloc_kvm_area();
2137}
2138
2139static __exit void hardware_unsetup(void)
2140{
2141 free_kvm_area();
2142}
2143
Avi Kivity6aa8b732006-12-10 02:21:36 -08002144static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
2145{
2146 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2147
Avi Kivity6af11b92007-03-19 13:18:10 +02002148 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002149 vmcs_write16(sf->selector, save->selector);
2150 vmcs_writel(sf->base, save->base);
2151 vmcs_write32(sf->limit, save->limit);
2152 vmcs_write32(sf->ar_bytes, save->ar);
2153 } else {
2154 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
2155 << AR_DPL_SHIFT;
2156 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
2157 }
2158}
2159
2160static void enter_pmode(struct kvm_vcpu *vcpu)
2161{
2162 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002163 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002164
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002165 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002166 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002167
Avi Kivity2fb92db2011-04-27 19:42:18 +03002168 vmx_segment_cache_clear(vmx);
2169
Avi Kivityd0ba64f2011-01-03 14:28:51 +02002170 vmcs_write16(GUEST_TR_SELECTOR, vmx->rmode.tr.selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002171 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
2172 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
2173 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002174
2175 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002176 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2177 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002178 vmcs_writel(GUEST_RFLAGS, flags);
2179
Rusty Russell66aee912007-07-17 23:34:16 +10002180 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
2181 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002182
2183 update_exception_bitmap(vcpu);
2184
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002185 if (emulate_invalid_guest_state)
2186 return;
2187
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002188 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
2189 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
2190 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
2191 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002192
Avi Kivity2fb92db2011-04-27 19:42:18 +03002193 vmx_segment_cache_clear(vmx);
2194
Avi Kivity6aa8b732006-12-10 02:21:36 -08002195 vmcs_write16(GUEST_SS_SELECTOR, 0);
2196 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
2197
2198 vmcs_write16(GUEST_CS_SELECTOR,
2199 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
2200 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
2201}
2202
Mike Dayd77c26f2007-10-08 09:02:08 -04002203static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002204{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002205 if (!kvm->arch.tss_addr) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02002206 struct kvm_memslots *slots;
2207 gfn_t base_gfn;
2208
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002209 slots = kvm_memslots(kvm);
Avi Kivityf495c6e2010-06-10 17:21:29 +03002210 base_gfn = slots->memslots[0].base_gfn +
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02002211 kvm->memslots->memslots[0].npages - 3;
Izik Eiduscbc94022007-10-25 00:29:55 +02002212 return base_gfn << PAGE_SHIFT;
2213 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002214 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002215}
2216
2217static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
2218{
2219 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2220
2221 save->selector = vmcs_read16(sf->selector);
2222 save->base = vmcs_readl(sf->base);
2223 save->limit = vmcs_read32(sf->limit);
2224 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01002225 vmcs_write16(sf->selector, save->base >> 4);
Gleb Natapov444e8632010-12-27 17:25:04 +02002226 vmcs_write32(sf->base, save->base & 0xffff0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002227 vmcs_write32(sf->limit, 0xffff);
2228 vmcs_write32(sf->ar_bytes, 0xf3);
Gleb Natapov444e8632010-12-27 17:25:04 +02002229 if (save->base & 0xf)
2230 printk_once(KERN_WARNING "kvm: segment base is not paragraph"
2231 " aligned when entering protected mode (seg=%d)",
2232 seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002233}
2234
2235static void enter_rmode(struct kvm_vcpu *vcpu)
2236{
2237 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002238 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002239
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002240 if (enable_unrestricted_guest)
2241 return;
2242
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002243 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002244 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002245
Gleb Natapov776e58e2011-03-13 12:34:27 +02002246 /*
2247 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
2248 * vcpu. Call it here with phys address pointing 16M below 4G.
2249 */
2250 if (!vcpu->kvm->arch.tss_addr) {
2251 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
2252 "called before entering vcpu\n");
2253 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
2254 vmx_set_tss_addr(vcpu->kvm, 0xfeffd000);
2255 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2256 }
2257
Avi Kivity2fb92db2011-04-27 19:42:18 +03002258 vmx_segment_cache_clear(vmx);
2259
Avi Kivityd0ba64f2011-01-03 14:28:51 +02002260 vmx->rmode.tr.selector = vmcs_read16(GUEST_TR_SELECTOR);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002261 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
2263
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002264 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
2266
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002267 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2269
2270 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002271 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002272
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002273 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002274
2275 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10002276 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002277 update_exception_bitmap(vcpu);
2278
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002279 if (emulate_invalid_guest_state)
2280 goto continue_rmode;
2281
Avi Kivity6aa8b732006-12-10 02:21:36 -08002282 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
2283 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
2284 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
2285
2286 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08002287 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02002288 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
2289 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002290 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
2291
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002292 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
2293 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
2294 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
2295 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03002296
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002297continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08002298 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299}
2300
Amit Shah401d10d2009-02-20 22:53:37 +05302301static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
2302{
2303 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002304 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
2305
2306 if (!msr)
2307 return;
Amit Shah401d10d2009-02-20 22:53:37 +05302308
Avi Kivity44ea2b12009-09-06 15:55:37 +03002309 /*
2310 * Force kernel_gs_base reloading before EFER changes, as control
2311 * of this msr depends on is_long_mode().
2312 */
2313 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02002314 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05302315 if (efer & EFER_LMA) {
2316 vmcs_write32(VM_ENTRY_CONTROLS,
2317 vmcs_read32(VM_ENTRY_CONTROLS) |
2318 VM_ENTRY_IA32E_MODE);
2319 msr->data = efer;
2320 } else {
2321 vmcs_write32(VM_ENTRY_CONTROLS,
2322 vmcs_read32(VM_ENTRY_CONTROLS) &
2323 ~VM_ENTRY_IA32E_MODE);
2324
2325 msr->data = efer & ~EFER_LME;
2326 }
2327 setup_msrs(vmx);
2328}
2329
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002330#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331
2332static void enter_lmode(struct kvm_vcpu *vcpu)
2333{
2334 u32 guest_tr_ar;
2335
Avi Kivity2fb92db2011-04-27 19:42:18 +03002336 vmx_segment_cache_clear(to_vmx(vcpu));
2337
Avi Kivity6aa8b732006-12-10 02:21:36 -08002338 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
2339 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
2340 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002341 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002342 vmcs_write32(GUEST_TR_AR_BYTES,
2343 (guest_tr_ar & ~AR_TYPE_MASK)
2344 | AR_TYPE_BUSY_64_TSS);
2345 }
Avi Kivityda38f432010-07-06 11:30:49 +03002346 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347}
2348
2349static void exit_lmode(struct kvm_vcpu *vcpu)
2350{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351 vmcs_write32(VM_ENTRY_CONTROLS,
2352 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03002353 & ~VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03002354 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355}
2356
2357#endif
2358
Sheng Yang2384d2b2008-01-17 15:14:33 +08002359static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
2360{
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002361 vpid_sync_context(to_vmx(vcpu));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08002362 if (enable_ept) {
2363 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2364 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08002365 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08002366 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08002367}
2368
Avi Kivitye8467fd2009-12-29 18:43:06 +02002369static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2370{
2371 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
2372
2373 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
2374 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
2375}
2376
Avi Kivityaff48ba2010-12-05 18:56:11 +02002377static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
2378{
2379 if (enable_ept && is_paging(vcpu))
2380 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2381 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
2382}
2383
Anthony Liguori25c4c272007-04-27 09:29:21 +03002384static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002385{
Avi Kivityfc78f512009-12-07 12:16:48 +02002386 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
2387
2388 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
2389 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08002390}
2391
Sheng Yang14394422008-04-28 12:24:45 +08002392static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
2393{
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002394 if (!test_bit(VCPU_EXREG_PDPTR,
2395 (unsigned long *)&vcpu->arch.regs_dirty))
2396 return;
2397
Sheng Yang14394422008-04-28 12:24:45 +08002398 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Joerg Roedelff03a072010-09-10 17:30:57 +02002399 vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]);
2400 vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]);
2401 vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]);
2402 vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08002403 }
2404}
2405
Avi Kivity8f5d5492009-05-31 18:41:29 +03002406static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
2407{
2408 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Joerg Roedelff03a072010-09-10 17:30:57 +02002409 vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
2410 vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
2411 vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
2412 vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002413 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002414
2415 __set_bit(VCPU_EXREG_PDPTR,
2416 (unsigned long *)&vcpu->arch.regs_avail);
2417 __set_bit(VCPU_EXREG_PDPTR,
2418 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002419}
2420
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002421static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08002422
2423static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
2424 unsigned long cr0,
2425 struct kvm_vcpu *vcpu)
2426{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03002427 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
2428 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002429 if (!(cr0 & X86_CR0_PG)) {
2430 /* From paging/starting to nonpaging */
2431 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08002432 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08002433 (CPU_BASED_CR3_LOAD_EXITING |
2434 CPU_BASED_CR3_STORE_EXITING));
2435 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002436 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002437 } else if (!is_paging(vcpu)) {
2438 /* From nonpaging to paging */
2439 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08002440 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08002441 ~(CPU_BASED_CR3_LOAD_EXITING |
2442 CPU_BASED_CR3_STORE_EXITING));
2443 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002444 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002445 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08002446
2447 if (!(cr0 & X86_CR0_WP))
2448 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08002449}
2450
Avi Kivity6aa8b732006-12-10 02:21:36 -08002451static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2452{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002453 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002454 unsigned long hw_cr0;
2455
2456 if (enable_unrestricted_guest)
2457 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
2458 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
2459 else
2460 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08002461
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002462 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463 enter_pmode(vcpu);
2464
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002465 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466 enter_rmode(vcpu);
2467
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002468#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002469 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10002470 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10002472 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002473 exit_lmode(vcpu);
2474 }
2475#endif
2476
Avi Kivity089d0342009-03-23 18:26:32 +02002477 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002478 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
2479
Avi Kivity02daab22009-12-30 12:40:26 +02002480 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02002481 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02002482
Avi Kivity6aa8b732006-12-10 02:21:36 -08002483 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08002484 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002485 vcpu->arch.cr0 = cr0;
Avi Kivity69c73022011-03-07 15:26:44 +02002486 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002487}
2488
Sheng Yang14394422008-04-28 12:24:45 +08002489static u64 construct_eptp(unsigned long root_hpa)
2490{
2491 u64 eptp;
2492
2493 /* TODO write the value reading from MSR */
2494 eptp = VMX_EPT_DEFAULT_MT |
2495 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
2496 eptp |= (root_hpa & PAGE_MASK);
2497
2498 return eptp;
2499}
2500
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
2502{
Sheng Yang14394422008-04-28 12:24:45 +08002503 unsigned long guest_cr3;
2504 u64 eptp;
2505
2506 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02002507 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08002508 eptp = construct_eptp(cr3);
2509 vmcs_write64(EPT_POINTER, eptp);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002510 guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
Sheng Yangb927a3c2009-07-21 10:42:48 +08002511 vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02002512 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002513 }
2514
Sheng Yang2384d2b2008-01-17 15:14:33 +08002515 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002516 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517}
2518
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002519static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002520{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002521 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08002522 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
2523
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002524 if (cr4 & X86_CR4_VMXE) {
2525 /*
2526 * To use VMXON (and later other VMX instructions), a guest
2527 * must first be able to turn on cr4.VMXE (see handle_vmon()).
2528 * So basically the check on whether to allow nested VMX
2529 * is here.
2530 */
2531 if (!nested_vmx_allowed(vcpu))
2532 return 1;
2533 } else if (to_vmx(vcpu)->nested.vmxon)
2534 return 1;
2535
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002536 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02002537 if (enable_ept) {
2538 if (!is_paging(vcpu)) {
2539 hw_cr4 &= ~X86_CR4_PAE;
2540 hw_cr4 |= X86_CR4_PSE;
2541 } else if (!(cr4 & X86_CR4_PAE)) {
2542 hw_cr4 &= ~X86_CR4_PAE;
2543 }
2544 }
Sheng Yang14394422008-04-28 12:24:45 +08002545
2546 vmcs_writel(CR4_READ_SHADOW, cr4);
2547 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002548 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002549}
2550
Avi Kivity6aa8b732006-12-10 02:21:36 -08002551static void vmx_get_segment(struct kvm_vcpu *vcpu,
2552 struct kvm_segment *var, int seg)
2553{
Avi Kivitya9179492011-01-03 14:28:52 +02002554 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitya9179492011-01-03 14:28:52 +02002555 struct kvm_save_segment *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002556 u32 ar;
2557
Avi Kivitya9179492011-01-03 14:28:52 +02002558 if (vmx->rmode.vm86_active
2559 && (seg == VCPU_SREG_TR || seg == VCPU_SREG_ES
2560 || seg == VCPU_SREG_DS || seg == VCPU_SREG_FS
2561 || seg == VCPU_SREG_GS)
2562 && !emulate_invalid_guest_state) {
2563 switch (seg) {
2564 case VCPU_SREG_TR: save = &vmx->rmode.tr; break;
2565 case VCPU_SREG_ES: save = &vmx->rmode.es; break;
2566 case VCPU_SREG_DS: save = &vmx->rmode.ds; break;
2567 case VCPU_SREG_FS: save = &vmx->rmode.fs; break;
2568 case VCPU_SREG_GS: save = &vmx->rmode.gs; break;
2569 default: BUG();
2570 }
2571 var->selector = save->selector;
2572 var->base = save->base;
2573 var->limit = save->limit;
2574 ar = save->ar;
2575 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03002576 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivitya9179492011-01-03 14:28:52 +02002577 goto use_saved_rmode_seg;
2578 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03002579 var->base = vmx_read_guest_seg_base(vmx, seg);
2580 var->limit = vmx_read_guest_seg_limit(vmx, seg);
2581 var->selector = vmx_read_guest_seg_selector(vmx, seg);
2582 ar = vmx_read_guest_seg_ar(vmx, seg);
Avi Kivitya9179492011-01-03 14:28:52 +02002583use_saved_rmode_seg:
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02002584 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002585 ar = 0;
2586 var->type = ar & 15;
2587 var->s = (ar >> 4) & 1;
2588 var->dpl = (ar >> 5) & 3;
2589 var->present = (ar >> 7) & 1;
2590 var->avl = (ar >> 12) & 1;
2591 var->l = (ar >> 13) & 1;
2592 var->db = (ar >> 14) & 1;
2593 var->g = (ar >> 15) & 1;
2594 var->unusable = (ar >> 16) & 1;
2595}
2596
Avi Kivitya9179492011-01-03 14:28:52 +02002597static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2598{
Avi Kivitya9179492011-01-03 14:28:52 +02002599 struct kvm_segment s;
2600
2601 if (to_vmx(vcpu)->rmode.vm86_active) {
2602 vmx_get_segment(vcpu, &s, seg);
2603 return s.base;
2604 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03002605 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02002606}
2607
Avi Kivity69c73022011-03-07 15:26:44 +02002608static int __vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02002609{
Avi Kivity3eeb3282010-01-21 15:31:48 +02002610 if (!is_protmode(vcpu))
Izik Eidus2e4d2652008-03-24 19:38:34 +02002611 return 0;
2612
Avi Kivityf4c63e52011-03-07 14:54:28 +02002613 if (!is_long_mode(vcpu)
2614 && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
Izik Eidus2e4d2652008-03-24 19:38:34 +02002615 return 3;
2616
Avi Kivity2fb92db2011-04-27 19:42:18 +03002617 return vmx_read_guest_seg_selector(to_vmx(vcpu), VCPU_SREG_CS) & 3;
Izik Eidus2e4d2652008-03-24 19:38:34 +02002618}
2619
Avi Kivity69c73022011-03-07 15:26:44 +02002620static int vmx_get_cpl(struct kvm_vcpu *vcpu)
2621{
2622 if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
2623 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
2624 to_vmx(vcpu)->cpl = __vmx_get_cpl(vcpu);
2625 }
2626 return to_vmx(vcpu)->cpl;
2627}
2628
2629
Avi Kivity653e3102007-05-07 10:55:37 +03002630static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002631{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632 u32 ar;
2633
Avi Kivity653e3102007-05-07 10:55:37 +03002634 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635 ar = 1 << 16;
2636 else {
2637 ar = var->type & 15;
2638 ar |= (var->s & 1) << 4;
2639 ar |= (var->dpl & 3) << 5;
2640 ar |= (var->present & 1) << 7;
2641 ar |= (var->avl & 1) << 12;
2642 ar |= (var->l & 1) << 13;
2643 ar |= (var->db & 1) << 14;
2644 ar |= (var->g & 1) << 15;
2645 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08002646 if (ar == 0) /* a 0 value means unusable */
2647 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03002648
2649 return ar;
2650}
2651
2652static void vmx_set_segment(struct kvm_vcpu *vcpu,
2653 struct kvm_segment *var, int seg)
2654{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002655 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03002656 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2657 u32 ar;
2658
Avi Kivity2fb92db2011-04-27 19:42:18 +03002659 vmx_segment_cache_clear(vmx);
2660
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002661 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
Gleb Natapova8ba6c22011-02-21 12:07:58 +02002662 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002663 vmx->rmode.tr.selector = var->selector;
2664 vmx->rmode.tr.base = var->base;
2665 vmx->rmode.tr.limit = var->limit;
2666 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03002667 return;
2668 }
2669 vmcs_writel(sf->base, var->base);
2670 vmcs_write32(sf->limit, var->limit);
2671 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002672 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03002673 /*
2674 * Hack real-mode segments into vm86 compatibility.
2675 */
2676 if (var->base == 0xffff0000 && var->selector == 0xf000)
2677 vmcs_writel(sf->base, 0xf0000);
2678 ar = 0xf3;
2679 } else
2680 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002681
2682 /*
2683 * Fix the "Accessed" bit in AR field of segment registers for older
2684 * qemu binaries.
2685 * IA32 arch specifies that at the time of processor reset the
2686 * "Accessed" bit in the AR field of segment registers is 1. And qemu
2687 * is setting it to 0 in the usedland code. This causes invalid guest
2688 * state vmexit when "unrestricted guest" mode is turned on.
2689 * Fix for this setup issue in cpu_reset is being pushed in the qemu
2690 * tree. Newer qemu binaries with that qemu fix would not need this
2691 * kvm hack.
2692 */
2693 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
2694 ar |= 0x1; /* Accessed */
2695
Avi Kivity6aa8b732006-12-10 02:21:36 -08002696 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity69c73022011-03-07 15:26:44 +02002697 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698}
2699
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2701{
Avi Kivity2fb92db2011-04-27 19:42:18 +03002702 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002703
2704 *db = (ar >> 14) & 1;
2705 *l = (ar >> 13) & 1;
2706}
2707
Gleb Natapov89a27f42010-02-16 10:51:48 +02002708static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002709{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002710 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
2711 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712}
2713
Gleb Natapov89a27f42010-02-16 10:51:48 +02002714static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002716 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
2717 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002718}
2719
Gleb Natapov89a27f42010-02-16 10:51:48 +02002720static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002722 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
2723 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724}
2725
Gleb Natapov89a27f42010-02-16 10:51:48 +02002726static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002728 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
2729 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002730}
2731
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002732static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
2733{
2734 struct kvm_segment var;
2735 u32 ar;
2736
2737 vmx_get_segment(vcpu, &var, seg);
2738 ar = vmx_segment_access_rights(&var);
2739
2740 if (var.base != (var.selector << 4))
2741 return false;
2742 if (var.limit != 0xffff)
2743 return false;
2744 if (ar != 0xf3)
2745 return false;
2746
2747 return true;
2748}
2749
2750static bool code_segment_valid(struct kvm_vcpu *vcpu)
2751{
2752 struct kvm_segment cs;
2753 unsigned int cs_rpl;
2754
2755 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2756 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
2757
Avi Kivity1872a3f2009-01-04 23:26:52 +02002758 if (cs.unusable)
2759 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002760 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
2761 return false;
2762 if (!cs.s)
2763 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002764 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002765 if (cs.dpl > cs_rpl)
2766 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002767 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002768 if (cs.dpl != cs_rpl)
2769 return false;
2770 }
2771 if (!cs.present)
2772 return false;
2773
2774 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
2775 return true;
2776}
2777
2778static bool stack_segment_valid(struct kvm_vcpu *vcpu)
2779{
2780 struct kvm_segment ss;
2781 unsigned int ss_rpl;
2782
2783 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2784 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
2785
Avi Kivity1872a3f2009-01-04 23:26:52 +02002786 if (ss.unusable)
2787 return true;
2788 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002789 return false;
2790 if (!ss.s)
2791 return false;
2792 if (ss.dpl != ss_rpl) /* DPL != RPL */
2793 return false;
2794 if (!ss.present)
2795 return false;
2796
2797 return true;
2798}
2799
2800static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
2801{
2802 struct kvm_segment var;
2803 unsigned int rpl;
2804
2805 vmx_get_segment(vcpu, &var, seg);
2806 rpl = var.selector & SELECTOR_RPL_MASK;
2807
Avi Kivity1872a3f2009-01-04 23:26:52 +02002808 if (var.unusable)
2809 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002810 if (!var.s)
2811 return false;
2812 if (!var.present)
2813 return false;
2814 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
2815 if (var.dpl < rpl) /* DPL < RPL */
2816 return false;
2817 }
2818
2819 /* TODO: Add other members to kvm_segment_field to allow checking for other access
2820 * rights flags
2821 */
2822 return true;
2823}
2824
2825static bool tr_valid(struct kvm_vcpu *vcpu)
2826{
2827 struct kvm_segment tr;
2828
2829 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
2830
Avi Kivity1872a3f2009-01-04 23:26:52 +02002831 if (tr.unusable)
2832 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002833 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2834 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002835 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002836 return false;
2837 if (!tr.present)
2838 return false;
2839
2840 return true;
2841}
2842
2843static bool ldtr_valid(struct kvm_vcpu *vcpu)
2844{
2845 struct kvm_segment ldtr;
2846
2847 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2848
Avi Kivity1872a3f2009-01-04 23:26:52 +02002849 if (ldtr.unusable)
2850 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002851 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2852 return false;
2853 if (ldtr.type != 2)
2854 return false;
2855 if (!ldtr.present)
2856 return false;
2857
2858 return true;
2859}
2860
2861static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2862{
2863 struct kvm_segment cs, ss;
2864
2865 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2866 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2867
2868 return ((cs.selector & SELECTOR_RPL_MASK) ==
2869 (ss.selector & SELECTOR_RPL_MASK));
2870}
2871
2872/*
2873 * Check if guest state is valid. Returns true if valid, false if
2874 * not.
2875 * We assume that registers are always usable
2876 */
2877static bool guest_state_valid(struct kvm_vcpu *vcpu)
2878{
2879 /* real mode guest state checks */
Avi Kivity3eeb3282010-01-21 15:31:48 +02002880 if (!is_protmode(vcpu)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002881 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2882 return false;
2883 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2884 return false;
2885 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2886 return false;
2887 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2888 return false;
2889 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2890 return false;
2891 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2892 return false;
2893 } else {
2894 /* protected mode guest state checks */
2895 if (!cs_ss_rpl_check(vcpu))
2896 return false;
2897 if (!code_segment_valid(vcpu))
2898 return false;
2899 if (!stack_segment_valid(vcpu))
2900 return false;
2901 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2902 return false;
2903 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2904 return false;
2905 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2906 return false;
2907 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2908 return false;
2909 if (!tr_valid(vcpu))
2910 return false;
2911 if (!ldtr_valid(vcpu))
2912 return false;
2913 }
2914 /* TODO:
2915 * - Add checks on RIP
2916 * - Add checks on RFLAGS
2917 */
2918
2919 return true;
2920}
2921
Mike Dayd77c26f2007-10-08 09:02:08 -04002922static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002923{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002924 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02002925 u16 data = 0;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002926 int r, idx, ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002927
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002928 idx = srcu_read_lock(&kvm->srcu);
2929 fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002930 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2931 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002932 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002933 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002934 r = kvm_write_guest_page(kvm, fn++, &data,
2935 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002936 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002937 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002938 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2939 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002940 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002941 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2942 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002943 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002944 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002945 r = kvm_write_guest_page(kvm, fn, &data,
2946 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2947 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002948 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002949 goto out;
2950
2951 ret = 1;
2952out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002953 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002954 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002955}
2956
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002957static int init_rmode_identity_map(struct kvm *kvm)
2958{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002959 int i, idx, r, ret;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002960 pfn_t identity_map_pfn;
2961 u32 tmp;
2962
Avi Kivity089d0342009-03-23 18:26:32 +02002963 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002964 return 1;
2965 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2966 printk(KERN_ERR "EPT: identity-mapping pagetable "
2967 "haven't been allocated!\n");
2968 return 0;
2969 }
2970 if (likely(kvm->arch.ept_identity_pagetable_done))
2971 return 1;
2972 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002973 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002974 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002975 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2976 if (r < 0)
2977 goto out;
2978 /* Set up identity-mapping pagetable for EPT in real mode */
2979 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2980 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2981 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2982 r = kvm_write_guest_page(kvm, identity_map_pfn,
2983 &tmp, i * sizeof(tmp), sizeof(tmp));
2984 if (r < 0)
2985 goto out;
2986 }
2987 kvm->arch.ept_identity_pagetable_done = true;
2988 ret = 1;
2989out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002990 srcu_read_unlock(&kvm->srcu, idx);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002991 return ret;
2992}
2993
Avi Kivity6aa8b732006-12-10 02:21:36 -08002994static void seg_setup(int seg)
2995{
2996 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002997 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002998
2999 vmcs_write16(sf->selector, 0);
3000 vmcs_writel(sf->base, 0);
3001 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003002 if (enable_unrestricted_guest) {
3003 ar = 0x93;
3004 if (seg == VCPU_SREG_CS)
3005 ar |= 0x08; /* code segment */
3006 } else
3007 ar = 0xf3;
3008
3009 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003010}
3011
Sheng Yangf78e0e22007-10-29 09:40:42 +08003012static int alloc_apic_access_page(struct kvm *kvm)
3013{
3014 struct kvm_userspace_memory_region kvm_userspace_mem;
3015 int r = 0;
3016
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003017 mutex_lock(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003018 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003019 goto out;
3020 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
3021 kvm_userspace_mem.flags = 0;
3022 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
3023 kvm_userspace_mem.memory_size = PAGE_SIZE;
3024 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
3025 if (r)
3026 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003027
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003028 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003029out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003030 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003031 return r;
3032}
3033
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003034static int alloc_identity_pagetable(struct kvm *kvm)
3035{
3036 struct kvm_userspace_memory_region kvm_userspace_mem;
3037 int r = 0;
3038
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003039 mutex_lock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003040 if (kvm->arch.ept_identity_pagetable)
3041 goto out;
3042 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
3043 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003044 kvm_userspace_mem.guest_phys_addr =
3045 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003046 kvm_userspace_mem.memory_size = PAGE_SIZE;
3047 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
3048 if (r)
3049 goto out;
3050
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003051 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
Sheng Yangb927a3c2009-07-21 10:42:48 +08003052 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003053out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003054 mutex_unlock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003055 return r;
3056}
3057
Sheng Yang2384d2b2008-01-17 15:14:33 +08003058static void allocate_vpid(struct vcpu_vmx *vmx)
3059{
3060 int vpid;
3061
3062 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02003063 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003064 return;
3065 spin_lock(&vmx_vpid_lock);
3066 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
3067 if (vpid < VMX_NR_VPIDS) {
3068 vmx->vpid = vpid;
3069 __set_bit(vpid, vmx_vpid_bitmap);
3070 }
3071 spin_unlock(&vmx_vpid_lock);
3072}
3073
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003074static void free_vpid(struct vcpu_vmx *vmx)
3075{
3076 if (!enable_vpid)
3077 return;
3078 spin_lock(&vmx_vpid_lock);
3079 if (vmx->vpid != 0)
3080 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3081 spin_unlock(&vmx_vpid_lock);
3082}
3083
Avi Kivity58972972009-02-24 22:26:47 +02003084static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08003085{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003086 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08003087
3088 if (!cpu_has_vmx_msr_bitmap())
3089 return;
3090
3091 /*
3092 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3093 * have the write-low and read-high bitmap offsets the wrong way round.
3094 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3095 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003096 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003097 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
3098 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08003099 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3100 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003101 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
3102 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08003103 }
Sheng Yang25c5f222008-03-28 13:18:56 +08003104}
3105
Avi Kivity58972972009-02-24 22:26:47 +02003106static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
3107{
3108 if (!longmode_only)
3109 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
3110 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
3111}
3112
Avi Kivity6aa8b732006-12-10 02:21:36 -08003113/*
3114 * Sets up the vmcs for emulated real mode.
3115 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003116static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003117{
Sheng Yang468d4722008-10-09 16:01:55 +08003118 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003119 u32 junk;
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10003120 u64 host_pat;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003121 unsigned long a;
Gleb Natapov89a27f42010-02-16 10:51:48 +02003122 struct desc_ptr dt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003123 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03003124 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003125 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003126
Avi Kivity6aa8b732006-12-10 02:21:36 -08003127 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003128 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
3129 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003130
Sheng Yang25c5f222008-03-28 13:18:56 +08003131 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02003132 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08003133
Avi Kivity6aa8b732006-12-10 02:21:36 -08003134 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
3135
Avi Kivity6aa8b732006-12-10 02:21:36 -08003136 /* Control */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03003137 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
3138 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003139
3140 exec_control = vmcs_config.cpu_based_exec_ctrl;
3141 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
3142 exec_control &= ~CPU_BASED_TPR_SHADOW;
3143#ifdef CONFIG_X86_64
3144 exec_control |= CPU_BASED_CR8_STORE_EXITING |
3145 CPU_BASED_CR8_LOAD_EXITING;
3146#endif
3147 }
Avi Kivity089d0342009-03-23 18:26:32 +02003148 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08003149 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03003150 CPU_BASED_CR3_LOAD_EXITING |
3151 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003152 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003153
Sheng Yang83ff3b92007-11-21 14:33:25 +08003154 if (cpu_has_secondary_exec_ctrls()) {
3155 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
3156 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
3157 exec_control &=
3158 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003159 if (vmx->vpid == 0)
3160 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang046d8712009-11-27 16:46:26 +08003161 if (!enable_ept) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003162 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang046d8712009-11-27 16:46:26 +08003163 enable_unrestricted_guest = 0;
3164 }
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003165 if (!enable_unrestricted_guest)
3166 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003167 if (!ple_gap)
3168 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yang83ff3b92007-11-21 14:33:25 +08003169 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
3170 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08003171
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003172 if (ple_gap) {
3173 vmcs_write32(PLE_GAP, ple_gap);
3174 vmcs_write32(PLE_WINDOW, ple_window);
3175 }
3176
Avi Kivityc7addb92007-09-16 18:58:32 +02003177 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
3178 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003179 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
3180
Avi Kivity1c11e712010-05-03 16:05:44 +03003181 vmcs_writel(HOST_CR0, read_cr0() | X86_CR0_TS); /* 22.2.3 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003182 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
3183 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
3184
3185 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
3186 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3187 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity9581d442010-10-19 16:46:55 +02003188 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
3189 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003190 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003191#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003192 rdmsrl(MSR_FS_BASE, a);
3193 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
3194 rdmsrl(MSR_GS_BASE, a);
3195 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
3196#else
3197 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
3198 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
3199#endif
3200
3201 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
3202
Wei Yongjunec687982010-03-05 12:11:48 +08003203 native_store_idt(&dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02003204 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003205
Mike Dayd77c26f2007-10-08 09:02:08 -04003206 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03003207 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03003208 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
3209 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03003210 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03003211 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03003212 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213
3214 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
3215 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
3216 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
3217 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
3218 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
3219 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
3220
Sheng Yang468d4722008-10-09 16:01:55 +08003221 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
3222 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
3223 host_pat = msr_low | ((u64) msr_high << 32);
3224 vmcs_write64(HOST_IA32_PAT, host_pat);
3225 }
3226 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
3227 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
3228 host_pat = msr_low | ((u64) msr_high << 32);
3229 /* Write the default value follow host pat */
3230 vmcs_write64(GUEST_IA32_PAT, host_pat);
3231 /* Keep arch.pat sync with GUEST_IA32_PAT */
3232 vmx->vcpu.arch.pat = host_pat;
3233 }
3234
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235 for (i = 0; i < NR_VMX_MSR; ++i) {
3236 u32 index = vmx_msr_index[i];
3237 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003238 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003239
3240 if (rdmsr_safe(index, &data_low, &data_high) < 0)
3241 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08003242 if (wrmsr_safe(index, data_low, data_high) < 0)
3243 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03003244 vmx->guest_msrs[j].index = i;
3245 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02003246 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003247 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003248 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003249
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03003250 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003251
3252 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03003253 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
3254
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003255 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Avi Kivity4c386092009-12-07 12:26:18 +02003256 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
Avi Kivityce03e4f2009-12-07 12:29:14 +02003257 if (enable_ept)
3258 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Avi Kivity4c386092009-12-07 12:26:18 +02003259 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003260
Zachary Amsden99e3e302010-08-19 22:07:17 -10003261 kvm_write_tsc(&vmx->vcpu, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003262
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003263 return 0;
3264}
3265
3266static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
3267{
3268 struct vcpu_vmx *vmx = to_vmx(vcpu);
3269 u64 msr;
Xiao Guangrong4b9d3a02010-06-08 10:15:51 +08003270 int ret;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003271
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003272 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003273
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003274 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003275
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003276 vmx->soft_vnmi_blocked = 0;
3277
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003278 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003279 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003280 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03003281 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003282 msr |= MSR_IA32_APICBASE_BSP;
3283 kvm_set_apic_base(&vmx->vcpu, msr);
3284
Jan Kiszka10ab25c2010-05-25 16:01:50 +02003285 ret = fx_init(&vmx->vcpu);
3286 if (ret != 0)
3287 goto out;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003288
Avi Kivity2fb92db2011-04-27 19:42:18 +03003289 vmx_segment_cache_clear(vmx);
3290
Avi Kivity5706be02008-08-20 15:07:31 +03003291 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003292 /*
3293 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
3294 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
3295 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03003296 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003297 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
3298 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
3299 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003300 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
3301 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003302 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003303
3304 seg_setup(VCPU_SREG_DS);
3305 seg_setup(VCPU_SREG_ES);
3306 seg_setup(VCPU_SREG_FS);
3307 seg_setup(VCPU_SREG_GS);
3308 seg_setup(VCPU_SREG_SS);
3309
3310 vmcs_write16(GUEST_TR_SELECTOR, 0);
3311 vmcs_writel(GUEST_TR_BASE, 0);
3312 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
3313 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3314
3315 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
3316 vmcs_writel(GUEST_LDTR_BASE, 0);
3317 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
3318 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
3319
3320 vmcs_write32(GUEST_SYSENTER_CS, 0);
3321 vmcs_writel(GUEST_SYSENTER_ESP, 0);
3322 vmcs_writel(GUEST_SYSENTER_EIP, 0);
3323
3324 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03003325 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003326 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003327 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003328 kvm_rip_write(vcpu, 0);
3329 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003330
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003331 vmcs_writel(GUEST_DR7, 0x400);
3332
3333 vmcs_writel(GUEST_GDTR_BASE, 0);
3334 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
3335
3336 vmcs_writel(GUEST_IDTR_BASE, 0);
3337 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
3338
Anthony Liguori443381a2010-12-06 10:53:38 -06003339 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003340 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
3341 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
3342
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003343 /* Special registers */
3344 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
3345
3346 setup_msrs(vmx);
3347
Avi Kivity6aa8b732006-12-10 02:21:36 -08003348 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
3349
Sheng Yangf78e0e22007-10-29 09:40:42 +08003350 if (cpu_has_vmx_tpr_shadow()) {
3351 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
3352 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
3353 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09003354 __pa(vmx->vcpu.arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08003355 vmcs_write32(TPR_THRESHOLD, 0);
3356 }
3357
3358 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
3359 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003360 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003361
Sheng Yang2384d2b2008-01-17 15:14:33 +08003362 if (vmx->vpid != 0)
3363 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
3364
Eduardo Habkostfa400522009-10-24 02:49:58 -02003365 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Avi Kivity4d4ec082009-12-29 18:07:30 +02003366 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003367 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003368 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003369 vmx_fpu_activate(&vmx->vcpu);
3370 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003371
Gui Jianfengb9d762f2010-06-07 10:32:29 +08003372 vpid_sync_context(vmx);
Sheng Yang2384d2b2008-01-17 15:14:33 +08003373
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003374 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003375
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003376 /* HACK: Don't enable emulation on guest boot/reset */
3377 vmx->emulation_required = 0;
3378
Avi Kivity6aa8b732006-12-10 02:21:36 -08003379out:
3380 return ret;
3381}
3382
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003383static void enable_irq_window(struct kvm_vcpu *vcpu)
3384{
3385 u32 cpu_based_vm_exec_control;
3386
3387 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3388 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
3389 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3390}
3391
3392static void enable_nmi_window(struct kvm_vcpu *vcpu)
3393{
3394 u32 cpu_based_vm_exec_control;
3395
3396 if (!cpu_has_virtual_nmis()) {
3397 enable_irq_window(vcpu);
3398 return;
3399 }
3400
Avi Kivity30bd0c42010-11-01 23:20:48 +02003401 if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
3402 enable_irq_window(vcpu);
3403 return;
3404 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003405 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3406 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
3407 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3408}
3409
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003410static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03003411{
Avi Kivity9c8cba32007-11-22 11:42:59 +02003412 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003413 uint32_t intr;
3414 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02003415
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003416 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003417
Avi Kivityfa89a812008-09-01 15:57:51 +03003418 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003419 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003420 int inc_eip = 0;
3421 if (vcpu->arch.interrupt.soft)
3422 inc_eip = vcpu->arch.event_exit_inst_len;
3423 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003424 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003425 return;
3426 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003427 intr = irq | INTR_INFO_VALID_MASK;
3428 if (vcpu->arch.interrupt.soft) {
3429 intr |= INTR_TYPE_SOFT_INTR;
3430 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3431 vmx->vcpu.arch.event_exit_inst_len);
3432 } else
3433 intr |= INTR_TYPE_EXT_INTR;
3434 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Anthony Liguori443381a2010-12-06 10:53:38 -06003435 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003436}
3437
Sheng Yangf08864b2008-05-15 18:23:25 +08003438static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
3439{
Jan Kiszka66a5a342008-09-26 09:30:51 +02003440 struct vcpu_vmx *vmx = to_vmx(vcpu);
3441
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003442 if (!cpu_has_virtual_nmis()) {
3443 /*
3444 * Tracking the NMI-blocked state in software is built upon
3445 * finding the next open IRQ window. This, in turn, depends on
3446 * well-behaving guests: They have to keep IRQs disabled at
3447 * least as long as the NMI handler runs. Otherwise we may
3448 * cause NMI nesting, maybe breaking the guest. But as this is
3449 * highly unlikely, we can live with the residual risk.
3450 */
3451 vmx->soft_vnmi_blocked = 1;
3452 vmx->vnmi_blocked_time = 0;
3453 }
3454
Jan Kiszka487b3912008-09-26 09:30:56 +02003455 ++vcpu->stat.nmi_injections;
Avi Kivity9d58b932011-03-07 16:52:07 +02003456 vmx->nmi_known_unmasked = false;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003457 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003458 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003459 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02003460 return;
3461 }
Sheng Yangf08864b2008-05-15 18:23:25 +08003462 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
3463 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Anthony Liguori443381a2010-12-06 10:53:38 -06003464 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003465}
3466
Gleb Natapovc4282df2009-04-21 17:45:07 +03003467static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02003468{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003469 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03003470 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02003471
Gleb Natapovc4282df2009-04-21 17:45:07 +03003472 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
Avi Kivity30bd0c42010-11-01 23:20:48 +02003473 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
3474 | GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02003475}
3476
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003477static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
3478{
3479 if (!cpu_has_virtual_nmis())
3480 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02003481 if (to_vmx(vcpu)->nmi_known_unmasked)
3482 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03003483 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003484}
3485
3486static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3487{
3488 struct vcpu_vmx *vmx = to_vmx(vcpu);
3489
3490 if (!cpu_has_virtual_nmis()) {
3491 if (vmx->soft_vnmi_blocked != masked) {
3492 vmx->soft_vnmi_blocked = masked;
3493 vmx->vnmi_blocked_time = 0;
3494 }
3495 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02003496 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003497 if (masked)
3498 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3499 GUEST_INTR_STATE_NMI);
3500 else
3501 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3502 GUEST_INTR_STATE_NMI);
3503 }
3504}
3505
Gleb Natapov78646122009-03-23 12:12:11 +02003506static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
3507{
Gleb Natapovc4282df2009-04-21 17:45:07 +03003508 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
3509 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
3510 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02003511}
3512
Izik Eiduscbc94022007-10-25 00:29:55 +02003513static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
3514{
3515 int ret;
3516 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08003517 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02003518 .guest_phys_addr = addr,
3519 .memory_size = PAGE_SIZE * 3,
3520 .flags = 0,
3521 };
3522
3523 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
3524 if (ret)
3525 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003526 kvm->arch.tss_addr = addr;
Gleb Natapov93ea5382011-02-21 12:07:59 +02003527 if (!init_rmode_tss(kvm))
3528 return -ENOMEM;
3529
Izik Eiduscbc94022007-10-25 00:29:55 +02003530 return 0;
3531}
3532
Avi Kivity6aa8b732006-12-10 02:21:36 -08003533static int handle_rmode_exception(struct kvm_vcpu *vcpu,
3534 int vec, u32 err_code)
3535{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03003536 /*
3537 * Instruction with address size override prefix opcode 0x67
3538 * Cause the #SS fault with 0 error code in VM86 mode.
3539 */
3540 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Andre Przywara51d8b662010-12-21 11:12:02 +01003541 if (emulate_instruction(vcpu, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003542 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003543 /*
3544 * Forward all other exceptions that are valid in real mode.
3545 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
3546 * the required debugging infrastructure rework.
3547 */
3548 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003549 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003550 if (vcpu->guest_debug &
3551 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
3552 return 0;
3553 kvm_queue_exception(vcpu, vec);
3554 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003555 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01003556 /*
3557 * Update instruction length as we may reinject the exception
3558 * from user space while in guest debugging mode.
3559 */
3560 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
3561 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003562 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
3563 return 0;
3564 /* fall through */
3565 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003566 case OF_VECTOR:
3567 case BR_VECTOR:
3568 case UD_VECTOR:
3569 case DF_VECTOR:
3570 case SS_VECTOR:
3571 case GP_VECTOR:
3572 case MF_VECTOR:
3573 kvm_queue_exception(vcpu, vec);
3574 return 1;
3575 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003576 return 0;
3577}
3578
Andi Kleena0861c02009-06-08 17:37:09 +08003579/*
3580 * Trigger machine check on the host. We assume all the MSRs are already set up
3581 * by the CPU and that we still run on the same CPU as the MCE occurred on.
3582 * We pass a fake environment to the machine check handler because we want
3583 * the guest to be always treated like user space, no matter what context
3584 * it used internally.
3585 */
3586static void kvm_machine_check(void)
3587{
3588#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
3589 struct pt_regs regs = {
3590 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
3591 .flags = X86_EFLAGS_IF,
3592 };
3593
3594 do_machine_check(&regs, 0);
3595#endif
3596}
3597
Avi Kivity851ba692009-08-24 11:10:17 +03003598static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08003599{
3600 /* already handled by vcpu_run */
3601 return 1;
3602}
3603
Avi Kivity851ba692009-08-24 11:10:17 +03003604static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003605{
Avi Kivity1155f762007-11-22 11:30:47 +02003606 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003607 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003608 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003609 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003610 u32 vect_info;
3611 enum emulation_result er;
3612
Avi Kivity1155f762007-11-22 11:30:47 +02003613 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02003614 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003615
Andi Kleena0861c02009-06-08 17:37:09 +08003616 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03003617 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003618
Avi Kivity6aa8b732006-12-10 02:21:36 -08003619 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Avi Kivity65ac7262009-11-04 11:59:01 +02003620 !is_page_fault(intr_info)) {
3621 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3622 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
3623 vcpu->run->internal.ndata = 2;
3624 vcpu->run->internal.data[0] = vect_info;
3625 vcpu->run->internal.data[1] = intr_info;
3626 return 0;
3627 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003628
Jan Kiszkae4a41882008-09-26 09:30:46 +02003629 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02003630 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03003631
3632 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03003633 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03003634 return 1;
3635 }
3636
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003637 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01003638 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003639 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02003640 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003641 return 1;
3642 }
3643
Avi Kivity6aa8b732006-12-10 02:21:36 -08003644 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06003645 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003646 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
3647 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08003648 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02003649 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003650 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003651 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003652 trace_kvm_page_fault(cr2, error_code);
3653
Gleb Natapov3298b752009-05-11 13:35:46 +03003654 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03003655 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01003656 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003657 }
3658
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003659 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08003660 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03003661 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003662 if (vcpu->arch.halt_request) {
3663 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03003664 return kvm_emulate_halt(vcpu);
3665 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003666 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03003667 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003668
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003669 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003670 switch (ex_no) {
3671 case DB_VECTOR:
3672 dr6 = vmcs_readl(EXIT_QUALIFICATION);
3673 if (!(vcpu->guest_debug &
3674 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
3675 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
3676 kvm_queue_exception(vcpu, DB_VECTOR);
3677 return 1;
3678 }
3679 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
3680 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
3681 /* fall through */
3682 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01003683 /*
3684 * Update instruction length as we may reinject #BP from
3685 * user space while in guest debugging mode. Reading it for
3686 * #DB as well causes no harm, it is not used in that case.
3687 */
3688 vmx->vcpu.arch.event_exit_inst_len =
3689 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003690 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03003691 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003692 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
3693 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003694 break;
3695 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003696 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
3697 kvm_run->ex.exception = ex_no;
3698 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003699 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003700 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003701 return 0;
3702}
3703
Avi Kivity851ba692009-08-24 11:10:17 +03003704static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003705{
Avi Kivity1165f5f2007-04-19 17:27:43 +03003706 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003707 return 1;
3708}
3709
Avi Kivity851ba692009-08-24 11:10:17 +03003710static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08003711{
Avi Kivity851ba692009-08-24 11:10:17 +03003712 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08003713 return 0;
3714}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003715
Avi Kivity851ba692009-08-24 11:10:17 +03003716static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003717{
He, Qingbfdaab02007-09-12 14:18:28 +08003718 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01003719 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02003720 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003721
He, Qingbfdaab02007-09-12 14:18:28 +08003722 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02003723 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03003724 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03003725
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003726 ++vcpu->stat.io_exits;
3727
3728 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01003729 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003730
3731 port = exit_qualification >> 16;
3732 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003733 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003734
3735 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003736}
3737
Ingo Molnar102d8322007-02-19 14:37:47 +02003738static void
3739vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3740{
3741 /*
3742 * Patch in the VMCALL instruction:
3743 */
3744 hypercall[0] = 0x0f;
3745 hypercall[1] = 0x01;
3746 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02003747}
3748
Avi Kivity851ba692009-08-24 11:10:17 +03003749static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003750{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003751 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003752 int cr;
3753 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03003754 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003755
He, Qingbfdaab02007-09-12 14:18:28 +08003756 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003757 cr = exit_qualification & 15;
3758 reg = (exit_qualification >> 8) & 15;
3759 switch ((exit_qualification >> 4) & 3) {
3760 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003761 val = kvm_register_read(vcpu, reg);
3762 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003763 switch (cr) {
3764 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03003765 err = kvm_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003766 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003767 return 1;
3768 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03003769 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003770 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003771 return 1;
3772 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03003773 err = kvm_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003774 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003775 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003776 case 8: {
3777 u8 cr8_prev = kvm_get_cr8(vcpu);
3778 u8 cr8 = kvm_register_read(vcpu, reg);
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003779 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003780 kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003781 if (irqchip_in_kernel(vcpu->kvm))
3782 return 1;
3783 if (cr8_prev <= cr8)
3784 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03003785 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003786 return 0;
3787 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003788 };
3789 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03003790 case 2: /* clts */
Avi Kivityedcafe32009-12-30 18:07:40 +02003791 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02003792 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03003793 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02003794 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03003795 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003796 case 1: /*mov from cr*/
3797 switch (cr) {
3798 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003799 val = kvm_read_cr3(vcpu);
3800 kvm_register_write(vcpu, reg, val);
3801 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003802 skip_emulated_instruction(vcpu);
3803 return 1;
3804 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003805 val = kvm_get_cr8(vcpu);
3806 kvm_register_write(vcpu, reg, val);
3807 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003808 skip_emulated_instruction(vcpu);
3809 return 1;
3810 }
3811 break;
3812 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02003813 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02003814 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02003815 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003816
3817 skip_emulated_instruction(vcpu);
3818 return 1;
3819 default:
3820 break;
3821 }
Avi Kivity851ba692009-08-24 11:10:17 +03003822 vcpu->run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10003823 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08003824 (int)(exit_qualification >> 4) & 3, cr);
3825 return 0;
3826}
3827
Avi Kivity851ba692009-08-24 11:10:17 +03003828static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003829{
He, Qingbfdaab02007-09-12 14:18:28 +08003830 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003831 int dr, reg;
3832
Jan Kiszkaf2483412010-01-20 18:20:20 +01003833 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03003834 if (!kvm_require_cpl(vcpu, 0))
3835 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003836 dr = vmcs_readl(GUEST_DR7);
3837 if (dr & DR7_GD) {
3838 /*
3839 * As the vm-exit takes precedence over the debug trap, we
3840 * need to emulate the latter, either for the host or the
3841 * guest debugging itself.
3842 */
3843 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03003844 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
3845 vcpu->run->debug.arch.dr7 = dr;
3846 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003847 vmcs_readl(GUEST_CS_BASE) +
3848 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03003849 vcpu->run->debug.arch.exception = DB_VECTOR;
3850 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003851 return 0;
3852 } else {
3853 vcpu->arch.dr7 &= ~DR7_GD;
3854 vcpu->arch.dr6 |= DR6_BD;
3855 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3856 kvm_queue_exception(vcpu, DB_VECTOR);
3857 return 1;
3858 }
3859 }
3860
He, Qingbfdaab02007-09-12 14:18:28 +08003861 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003862 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
3863 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
3864 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03003865 unsigned long val;
3866 if (!kvm_get_dr(vcpu, dr, &val))
3867 kvm_register_write(vcpu, reg, val);
3868 } else
3869 kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003870 skip_emulated_instruction(vcpu);
3871 return 1;
3872}
3873
Gleb Natapov020df072010-04-13 10:05:23 +03003874static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
3875{
3876 vmcs_writel(GUEST_DR7, val);
3877}
3878
Avi Kivity851ba692009-08-24 11:10:17 +03003879static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003880{
Avi Kivity06465c52007-02-28 20:46:53 +02003881 kvm_emulate_cpuid(vcpu);
3882 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003883}
3884
Avi Kivity851ba692009-08-24 11:10:17 +03003885static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003886{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003887 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003888 u64 data;
3889
3890 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivity59200272010-01-25 19:47:02 +02003891 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003892 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003893 return 1;
3894 }
3895
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003896 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003897
Avi Kivity6aa8b732006-12-10 02:21:36 -08003898 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003899 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3900 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003901 skip_emulated_instruction(vcpu);
3902 return 1;
3903}
3904
Avi Kivity851ba692009-08-24 11:10:17 +03003905static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003906{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003907 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3908 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3909 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003910
3911 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02003912 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003913 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003914 return 1;
3915 }
3916
Avi Kivity59200272010-01-25 19:47:02 +02003917 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003918 skip_emulated_instruction(vcpu);
3919 return 1;
3920}
3921
Avi Kivity851ba692009-08-24 11:10:17 +03003922static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003923{
Avi Kivity3842d132010-07-27 12:30:24 +03003924 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003925 return 1;
3926}
3927
Avi Kivity851ba692009-08-24 11:10:17 +03003928static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003929{
Eddie Dong85f455f2007-07-06 12:20:49 +03003930 u32 cpu_based_vm_exec_control;
3931
3932 /* clear pending irq */
3933 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3934 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3935 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003936
Avi Kivity3842d132010-07-27 12:30:24 +03003937 kvm_make_request(KVM_REQ_EVENT, vcpu);
3938
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003939 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003940
Dor Laorc1150d82007-01-05 16:36:24 -08003941 /*
3942 * If the user space waits to inject interrupts, exit as soon as
3943 * possible
3944 */
Gleb Natapov80618232009-04-21 17:44:56 +03003945 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003946 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03003947 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003948 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003949 return 0;
3950 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003951 return 1;
3952}
3953
Avi Kivity851ba692009-08-24 11:10:17 +03003954static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003955{
3956 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003957 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003958}
3959
Avi Kivity851ba692009-08-24 11:10:17 +03003960static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02003961{
Dor Laor510043d2007-02-19 18:25:43 +02003962 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003963 kvm_emulate_hypercall(vcpu);
3964 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003965}
3966
Avi Kivity851ba692009-08-24 11:10:17 +03003967static int handle_vmx_insn(struct kvm_vcpu *vcpu)
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003968{
3969 kvm_queue_exception(vcpu, UD_VECTOR);
3970 return 1;
3971}
3972
Gleb Natapovec25d5e2010-11-01 15:35:01 +02003973static int handle_invd(struct kvm_vcpu *vcpu)
3974{
Andre Przywara51d8b662010-12-21 11:12:02 +01003975 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02003976}
3977
Avi Kivity851ba692009-08-24 11:10:17 +03003978static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003979{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003980 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003981
3982 kvm_mmu_invlpg(vcpu, exit_qualification);
3983 skip_emulated_instruction(vcpu);
3984 return 1;
3985}
3986
Avi Kivity851ba692009-08-24 11:10:17 +03003987static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02003988{
3989 skip_emulated_instruction(vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003990 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02003991 return 1;
3992}
3993
Dexuan Cui2acf9232010-06-10 11:27:12 +08003994static int handle_xsetbv(struct kvm_vcpu *vcpu)
3995{
3996 u64 new_bv = kvm_read_edx_eax(vcpu);
3997 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
3998
3999 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
4000 skip_emulated_instruction(vcpu);
4001 return 1;
4002}
4003
Avi Kivity851ba692009-08-24 11:10:17 +03004004static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004005{
Andre Przywara51d8b662010-12-21 11:12:02 +01004006 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004007}
4008
Avi Kivity851ba692009-08-24 11:10:17 +03004009static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02004010{
Jan Kiszka60637aa2008-09-26 09:30:47 +02004011 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02004012 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02004013 bool has_error_code = false;
4014 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02004015 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004016 int reason, type, idt_v;
4017
4018 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
4019 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02004020
4021 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4022
4023 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004024 if (reason == TASK_SWITCH_GATE && idt_v) {
4025 switch (type) {
4026 case INTR_TYPE_NMI_INTR:
4027 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02004028 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004029 break;
4030 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004031 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004032 kvm_clear_interrupt_queue(vcpu);
4033 break;
4034 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02004035 if (vmx->idt_vectoring_info &
4036 VECTORING_INFO_DELIVER_CODE_MASK) {
4037 has_error_code = true;
4038 error_code =
4039 vmcs_read32(IDT_VECTORING_ERROR_CODE);
4040 }
4041 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004042 case INTR_TYPE_SOFT_EXCEPTION:
4043 kvm_clear_exception_queue(vcpu);
4044 break;
4045 default:
4046 break;
4047 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02004048 }
Izik Eidus37817f22008-03-24 23:14:53 +02004049 tss_selector = exit_qualification;
4050
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004051 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
4052 type != INTR_TYPE_EXT_INTR &&
4053 type != INTR_TYPE_NMI_INTR))
4054 skip_emulated_instruction(vcpu);
4055
Gleb Natapovacb54512010-04-15 21:03:50 +03004056 if (kvm_task_switch(vcpu, tss_selector, reason,
4057 has_error_code, error_code) == EMULATE_FAIL) {
4058 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4059 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
4060 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004061 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03004062 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004063
4064 /* clear all local breakpoint enable flags */
4065 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
4066
4067 /*
4068 * TODO: What about debug traps on tss switch?
4069 * Are we supposed to inject them and update dr6?
4070 */
4071
4072 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02004073}
4074
Avi Kivity851ba692009-08-24 11:10:17 +03004075static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08004076{
Sheng Yangf9c617f2009-03-25 10:08:52 +08004077 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08004078 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08004079 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08004080
Sheng Yangf9c617f2009-03-25 10:08:52 +08004081 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08004082
4083 if (exit_qualification & (1 << 6)) {
4084 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
Jan Kiszka7f582ab2009-07-22 23:53:01 +02004085 return -EINVAL;
Sheng Yang14394422008-04-28 12:24:45 +08004086 }
4087
4088 gla_validity = (exit_qualification >> 7) & 0x3;
4089 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
4090 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
4091 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
4092 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08004093 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08004094 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
4095 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03004096 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
4097 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03004098 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08004099 }
4100
4101 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004102 trace_kvm_page_fault(gpa, exit_qualification);
Andre Przywaradc25e892010-12-21 11:12:07 +01004103 return kvm_mmu_page_fault(vcpu, gpa, exit_qualification & 0x3, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08004104}
4105
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004106static u64 ept_rsvd_mask(u64 spte, int level)
4107{
4108 int i;
4109 u64 mask = 0;
4110
4111 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
4112 mask |= (1ULL << i);
4113
4114 if (level > 2)
4115 /* bits 7:3 reserved */
4116 mask |= 0xf8;
4117 else if (level == 2) {
4118 if (spte & (1ULL << 7))
4119 /* 2MB ref, bits 20:12 reserved */
4120 mask |= 0x1ff000;
4121 else
4122 /* bits 6:3 reserved */
4123 mask |= 0x78;
4124 }
4125
4126 return mask;
4127}
4128
4129static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
4130 int level)
4131{
4132 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
4133
4134 /* 010b (write-only) */
4135 WARN_ON((spte & 0x7) == 0x2);
4136
4137 /* 110b (write/execute) */
4138 WARN_ON((spte & 0x7) == 0x6);
4139
4140 /* 100b (execute-only) and value not supported by logical processor */
4141 if (!cpu_has_vmx_ept_execute_only())
4142 WARN_ON((spte & 0x7) == 0x4);
4143
4144 /* not 000b */
4145 if ((spte & 0x7)) {
4146 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
4147
4148 if (rsvd_bits != 0) {
4149 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
4150 __func__, rsvd_bits);
4151 WARN_ON(1);
4152 }
4153
4154 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
4155 u64 ept_mem_type = (spte & 0x38) >> 3;
4156
4157 if (ept_mem_type == 2 || ept_mem_type == 3 ||
4158 ept_mem_type == 7) {
4159 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
4160 __func__, ept_mem_type);
4161 WARN_ON(1);
4162 }
4163 }
4164 }
4165}
4166
Avi Kivity851ba692009-08-24 11:10:17 +03004167static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004168{
4169 u64 sptes[4];
4170 int nr_sptes, i;
4171 gpa_t gpa;
4172
4173 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
4174
4175 printk(KERN_ERR "EPT: Misconfiguration.\n");
4176 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
4177
4178 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
4179
4180 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
4181 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
4182
Avi Kivity851ba692009-08-24 11:10:17 +03004183 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
4184 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004185
4186 return 0;
4187}
4188
Avi Kivity851ba692009-08-24 11:10:17 +03004189static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08004190{
4191 u32 cpu_based_vm_exec_control;
4192
4193 /* clear pending NMI */
4194 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4195 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
4196 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4197 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03004198 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08004199
4200 return 1;
4201}
4202
Mohammed Gamal80ced182009-09-01 12:48:18 +02004203static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004204{
Avi Kivity8b3079a2009-01-05 12:10:54 +02004205 struct vcpu_vmx *vmx = to_vmx(vcpu);
4206 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02004207 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02004208 u32 cpu_exec_ctrl;
4209 bool intr_window_requested;
4210
4211 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4212 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004213
4214 while (!guest_state_valid(vcpu)) {
Avi Kivity49e9d552010-09-19 14:34:08 +02004215 if (intr_window_requested
4216 && (kvm_get_rflags(&vmx->vcpu) & X86_EFLAGS_IF))
4217 return handle_interrupt_window(&vmx->vcpu);
4218
Andre Przywara51d8b662010-12-21 11:12:02 +01004219 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004220
Mohammed Gamal80ced182009-09-01 12:48:18 +02004221 if (err == EMULATE_DO_MMIO) {
4222 ret = 0;
4223 goto out;
4224 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01004225
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03004226 if (err != EMULATE_DONE)
4227 return 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004228
4229 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02004230 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004231 if (need_resched())
4232 schedule();
4233 }
4234
Mohammed Gamal80ced182009-09-01 12:48:18 +02004235 vmx->emulation_required = 0;
4236out:
4237 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004238}
4239
Avi Kivity6aa8b732006-12-10 02:21:36 -08004240/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004241 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
4242 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
4243 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03004244static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004245{
4246 skip_emulated_instruction(vcpu);
4247 kvm_vcpu_on_spin(vcpu);
4248
4249 return 1;
4250}
4251
Sheng Yang59708672009-12-15 13:29:54 +08004252static int handle_invalid_op(struct kvm_vcpu *vcpu)
4253{
4254 kvm_queue_exception(vcpu, UD_VECTOR);
4255 return 1;
4256}
4257
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004258/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03004259 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
4260 * We could reuse a single VMCS for all the L2 guests, but we also want the
4261 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
4262 * allows keeping them loaded on the processor, and in the future will allow
4263 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
4264 * every entry if they never change.
4265 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
4266 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
4267 *
4268 * The following functions allocate and free a vmcs02 in this pool.
4269 */
4270
4271/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
4272static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
4273{
4274 struct vmcs02_list *item;
4275 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
4276 if (item->vmptr == vmx->nested.current_vmptr) {
4277 list_move(&item->list, &vmx->nested.vmcs02_pool);
4278 return &item->vmcs02;
4279 }
4280
4281 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
4282 /* Recycle the least recently used VMCS. */
4283 item = list_entry(vmx->nested.vmcs02_pool.prev,
4284 struct vmcs02_list, list);
4285 item->vmptr = vmx->nested.current_vmptr;
4286 list_move(&item->list, &vmx->nested.vmcs02_pool);
4287 return &item->vmcs02;
4288 }
4289
4290 /* Create a new VMCS */
4291 item = (struct vmcs02_list *)
4292 kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
4293 if (!item)
4294 return NULL;
4295 item->vmcs02.vmcs = alloc_vmcs();
4296 if (!item->vmcs02.vmcs) {
4297 kfree(item);
4298 return NULL;
4299 }
4300 loaded_vmcs_init(&item->vmcs02);
4301 item->vmptr = vmx->nested.current_vmptr;
4302 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
4303 vmx->nested.vmcs02_num++;
4304 return &item->vmcs02;
4305}
4306
4307/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
4308static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
4309{
4310 struct vmcs02_list *item;
4311 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
4312 if (item->vmptr == vmptr) {
4313 free_loaded_vmcs(&item->vmcs02);
4314 list_del(&item->list);
4315 kfree(item);
4316 vmx->nested.vmcs02_num--;
4317 return;
4318 }
4319}
4320
4321/*
4322 * Free all VMCSs saved for this vcpu, except the one pointed by
4323 * vmx->loaded_vmcs. These include the VMCSs in vmcs02_pool (except the one
4324 * currently used, if running L2), and vmcs01 when running L2.
4325 */
4326static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
4327{
4328 struct vmcs02_list *item, *n;
4329 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
4330 if (vmx->loaded_vmcs != &item->vmcs02)
4331 free_loaded_vmcs(&item->vmcs02);
4332 list_del(&item->list);
4333 kfree(item);
4334 }
4335 vmx->nested.vmcs02_num = 0;
4336
4337 if (vmx->loaded_vmcs != &vmx->vmcs01)
4338 free_loaded_vmcs(&vmx->vmcs01);
4339}
4340
4341/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03004342 * Emulate the VMXON instruction.
4343 * Currently, we just remember that VMX is active, and do not save or even
4344 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
4345 * do not currently need to store anything in that guest-allocated memory
4346 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
4347 * argument is different from the VMXON pointer (which the spec says they do).
4348 */
4349static int handle_vmon(struct kvm_vcpu *vcpu)
4350{
4351 struct kvm_segment cs;
4352 struct vcpu_vmx *vmx = to_vmx(vcpu);
4353
4354 /* The Intel VMX Instruction Reference lists a bunch of bits that
4355 * are prerequisite to running VMXON, most notably cr4.VMXE must be
4356 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
4357 * Otherwise, we should fail with #UD. We test these now:
4358 */
4359 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
4360 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
4361 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
4362 kvm_queue_exception(vcpu, UD_VECTOR);
4363 return 1;
4364 }
4365
4366 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4367 if (is_long_mode(vcpu) && !cs.l) {
4368 kvm_queue_exception(vcpu, UD_VECTOR);
4369 return 1;
4370 }
4371
4372 if (vmx_get_cpl(vcpu)) {
4373 kvm_inject_gp(vcpu, 0);
4374 return 1;
4375 }
4376
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03004377 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
4378 vmx->nested.vmcs02_num = 0;
4379
Nadav Har'Elec378ae2011-05-25 23:02:54 +03004380 vmx->nested.vmxon = true;
4381
4382 skip_emulated_instruction(vcpu);
4383 return 1;
4384}
4385
4386/*
4387 * Intel's VMX Instruction Reference specifies a common set of prerequisites
4388 * for running VMX instructions (except VMXON, whose prerequisites are
4389 * slightly different). It also specifies what exception to inject otherwise.
4390 */
4391static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
4392{
4393 struct kvm_segment cs;
4394 struct vcpu_vmx *vmx = to_vmx(vcpu);
4395
4396 if (!vmx->nested.vmxon) {
4397 kvm_queue_exception(vcpu, UD_VECTOR);
4398 return 0;
4399 }
4400
4401 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4402 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
4403 (is_long_mode(vcpu) && !cs.l)) {
4404 kvm_queue_exception(vcpu, UD_VECTOR);
4405 return 0;
4406 }
4407
4408 if (vmx_get_cpl(vcpu)) {
4409 kvm_inject_gp(vcpu, 0);
4410 return 0;
4411 }
4412
4413 return 1;
4414}
4415
4416/*
4417 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
4418 * just stops using VMX.
4419 */
4420static void free_nested(struct vcpu_vmx *vmx)
4421{
4422 if (!vmx->nested.vmxon)
4423 return;
4424 vmx->nested.vmxon = false;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03004425 if (vmx->nested.current_vmptr != -1ull) {
4426 kunmap(vmx->nested.current_vmcs12_page);
4427 nested_release_page(vmx->nested.current_vmcs12_page);
4428 vmx->nested.current_vmptr = -1ull;
4429 vmx->nested.current_vmcs12 = NULL;
4430 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03004431
4432 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03004433}
4434
4435/* Emulate the VMXOFF instruction */
4436static int handle_vmoff(struct kvm_vcpu *vcpu)
4437{
4438 if (!nested_vmx_check_permission(vcpu))
4439 return 1;
4440 free_nested(to_vmx(vcpu));
4441 skip_emulated_instruction(vcpu);
4442 return 1;
4443}
4444
4445/*
Nadav Har'El064aea72011-05-25 23:04:56 +03004446 * Decode the memory-address operand of a vmx instruction, as recorded on an
4447 * exit caused by such an instruction (run by a guest hypervisor).
4448 * On success, returns 0. When the operand is invalid, returns 1 and throws
4449 * #UD or #GP.
4450 */
4451static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
4452 unsigned long exit_qualification,
4453 u32 vmx_instruction_info, gva_t *ret)
4454{
4455 /*
4456 * According to Vol. 3B, "Information for VM Exits Due to Instruction
4457 * Execution", on an exit, vmx_instruction_info holds most of the
4458 * addressing components of the operand. Only the displacement part
4459 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
4460 * For how an actual address is calculated from all these components,
4461 * refer to Vol. 1, "Operand Addressing".
4462 */
4463 int scaling = vmx_instruction_info & 3;
4464 int addr_size = (vmx_instruction_info >> 7) & 7;
4465 bool is_reg = vmx_instruction_info & (1u << 10);
4466 int seg_reg = (vmx_instruction_info >> 15) & 7;
4467 int index_reg = (vmx_instruction_info >> 18) & 0xf;
4468 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
4469 int base_reg = (vmx_instruction_info >> 23) & 0xf;
4470 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
4471
4472 if (is_reg) {
4473 kvm_queue_exception(vcpu, UD_VECTOR);
4474 return 1;
4475 }
4476
4477 /* Addr = segment_base + offset */
4478 /* offset = base + [index * scale] + displacement */
4479 *ret = vmx_get_segment_base(vcpu, seg_reg);
4480 if (base_is_valid)
4481 *ret += kvm_register_read(vcpu, base_reg);
4482 if (index_is_valid)
4483 *ret += kvm_register_read(vcpu, index_reg)<<scaling;
4484 *ret += exit_qualification; /* holds the displacement */
4485
4486 if (addr_size == 1) /* 32 bit */
4487 *ret &= 0xffffffff;
4488
4489 /*
4490 * TODO: throw #GP (and return 1) in various cases that the VM*
4491 * instructions require it - e.g., offset beyond segment limit,
4492 * unusable or unreadable/unwritable segment, non-canonical 64-bit
4493 * address, and so on. Currently these are not checked.
4494 */
4495 return 0;
4496}
4497
4498/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004499 * The exit handlers return 1 if the exit was handled fully and guest execution
4500 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
4501 * to be done to userspace and return 0.
4502 */
Avi Kivity851ba692009-08-24 11:10:17 +03004503static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004504 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
4505 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08004506 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08004507 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004508 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004509 [EXIT_REASON_CR_ACCESS] = handle_cr,
4510 [EXIT_REASON_DR_ACCESS] = handle_dr,
4511 [EXIT_REASON_CPUID] = handle_cpuid,
4512 [EXIT_REASON_MSR_READ] = handle_rdmsr,
4513 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
4514 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
4515 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02004516 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03004517 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02004518 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03004519 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
4520 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
4521 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
4522 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
4523 [EXIT_REASON_VMREAD] = handle_vmx_insn,
4524 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
4525 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03004526 [EXIT_REASON_VMOFF] = handle_vmoff,
4527 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004528 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
4529 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02004530 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08004531 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02004532 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08004533 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004534 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
4535 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004536 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sheng Yang59708672009-12-15 13:29:54 +08004537 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
4538 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004539};
4540
4541static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04004542 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004543
Avi Kivity586f9602010-11-18 13:09:54 +02004544static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
4545{
4546 *info1 = vmcs_readl(EXIT_QUALIFICATION);
4547 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
4548}
4549
Avi Kivity6aa8b732006-12-10 02:21:36 -08004550/*
4551 * The guest has exited. See if we can fix it or if we need userspace
4552 * assistance.
4553 */
Avi Kivity851ba692009-08-24 11:10:17 +03004554static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004555{
Avi Kivity29bd8a72007-09-10 17:27:03 +03004556 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08004557 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02004558 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03004559
Avi Kivityaa179112010-11-17 18:44:19 +02004560 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004561
Mohammed Gamal80ced182009-09-01 12:48:18 +02004562 /* If guest state is invalid, start emulating */
4563 if (vmx->emulation_required && emulate_invalid_guest_state)
4564 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01004565
Mohammed Gamal51207022010-05-31 22:40:54 +03004566 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
4567 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4568 vcpu->run->fail_entry.hardware_entry_failure_reason
4569 = exit_reason;
4570 return 0;
4571 }
4572
Avi Kivity29bd8a72007-09-10 17:27:03 +03004573 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004574 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4575 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03004576 = vmcs_read32(VM_INSTRUCTION_ERROR);
4577 return 0;
4578 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004579
Mike Dayd77c26f2007-10-08 09:02:08 -04004580 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08004581 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02004582 exit_reason != EXIT_REASON_EPT_VIOLATION &&
4583 exit_reason != EXIT_REASON_TASK_SWITCH))
4584 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
4585 "(0x%x) and exit reason is 0x%x\n",
4586 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004587
4588 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03004589 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004590 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004591 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01004592 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004593 /*
4594 * This CPU don't support us in finding the end of an
4595 * NMI-blocked window if the guest runs with IRQs
4596 * disabled. So we pull the trigger after 1 s of
4597 * futile waiting, but inform the user about this.
4598 */
4599 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
4600 "state on VCPU %d after 1 s timeout\n",
4601 __func__, vcpu->vcpu_id);
4602 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004603 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004604 }
4605
Avi Kivity6aa8b732006-12-10 02:21:36 -08004606 if (exit_reason < kvm_vmx_max_exit_handlers
4607 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03004608 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004609 else {
Avi Kivity851ba692009-08-24 11:10:17 +03004610 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
4611 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004612 }
4613 return 0;
4614}
4615
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004616static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004617{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004618 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004619 vmcs_write32(TPR_THRESHOLD, 0);
4620 return;
4621 }
4622
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004623 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004624}
4625
Avi Kivity51aa01d2010-07-20 14:31:20 +03004626static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03004627{
Avi Kivity00eba012011-03-07 17:24:54 +02004628 u32 exit_intr_info;
4629
4630 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
4631 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
4632 return;
4633
Avi Kivityc5ca8e52011-03-07 17:37:37 +02004634 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02004635 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08004636
4637 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02004638 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08004639 kvm_machine_check();
4640
Gleb Natapov20f65982009-05-11 13:35:55 +03004641 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02004642 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004643 (exit_intr_info & INTR_INFO_VALID_MASK)) {
4644 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03004645 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004646 kvm_after_handle_nmi(&vmx->vcpu);
4647 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03004648}
Gleb Natapov20f65982009-05-11 13:35:55 +03004649
Avi Kivity51aa01d2010-07-20 14:31:20 +03004650static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
4651{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02004652 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03004653 bool unblock_nmi;
4654 u8 vector;
4655 bool idtv_info_valid;
4656
4657 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03004658
Avi Kivitycf393f72008-07-01 16:20:21 +03004659 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02004660 if (vmx->nmi_known_unmasked)
4661 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02004662 /*
4663 * Can't use vmx->exit_intr_info since we're not sure what
4664 * the exit reason is.
4665 */
4666 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03004667 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
4668 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
4669 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004670 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03004671 * Re-set bit "block by NMI" before VM entry if vmexit caused by
4672 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004673 * SDM 3: 23.2.2 (September 2008)
4674 * Bit 12 is undefined in any of the following cases:
4675 * If the VM exit sets the valid bit in the IDT-vectoring
4676 * information field.
4677 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03004678 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004679 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
4680 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03004681 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4682 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02004683 else
4684 vmx->nmi_known_unmasked =
4685 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
4686 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004687 } else if (unlikely(vmx->soft_vnmi_blocked))
4688 vmx->vnmi_blocked_time +=
4689 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03004690}
4691
Avi Kivity83422e12010-07-20 14:43:23 +03004692static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
4693 u32 idt_vectoring_info,
4694 int instr_len_field,
4695 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03004696{
Avi Kivity51aa01d2010-07-20 14:31:20 +03004697 u8 vector;
4698 int type;
4699 bool idtv_info_valid;
4700
4701 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03004702
Gleb Natapov37b96e92009-03-30 16:03:13 +03004703 vmx->vcpu.arch.nmi_injected = false;
4704 kvm_clear_exception_queue(&vmx->vcpu);
4705 kvm_clear_interrupt_queue(&vmx->vcpu);
4706
4707 if (!idtv_info_valid)
4708 return;
4709
Avi Kivity3842d132010-07-27 12:30:24 +03004710 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
4711
Avi Kivity668f6122008-07-02 09:28:55 +03004712 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
4713 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03004714
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004715 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03004716 case INTR_TYPE_NMI_INTR:
4717 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03004718 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004719 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03004720 * Clear bit "block by NMI" before VM entry if a NMI
4721 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03004722 */
Avi Kivity654f06f2011-03-23 15:02:47 +02004723 vmx_set_nmi_mask(&vmx->vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004724 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03004725 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004726 vmx->vcpu.arch.event_exit_inst_len =
Avi Kivity83422e12010-07-20 14:43:23 +03004727 vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004728 /* fall through */
4729 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03004730 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03004731 u32 err = vmcs_read32(error_code_field);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004732 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03004733 } else
4734 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004735 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004736 case INTR_TYPE_SOFT_INTR:
4737 vmx->vcpu.arch.event_exit_inst_len =
Avi Kivity83422e12010-07-20 14:43:23 +03004738 vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004739 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03004740 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004741 kvm_queue_interrupt(&vmx->vcpu, vector,
4742 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004743 break;
4744 default:
4745 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03004746 }
Avi Kivitycf393f72008-07-01 16:20:21 +03004747}
4748
Avi Kivity83422e12010-07-20 14:43:23 +03004749static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
4750{
4751 __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info,
4752 VM_EXIT_INSTRUCTION_LEN,
4753 IDT_VECTORING_ERROR_CODE);
4754}
4755
Avi Kivityb463a6f2010-07-20 15:06:17 +03004756static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
4757{
4758 __vmx_complete_interrupts(to_vmx(vcpu),
4759 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
4760 VM_ENTRY_INSTRUCTION_LEN,
4761 VM_ENTRY_EXCEPTION_ERROR_CODE);
4762
4763 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
4764}
4765
Avi Kivityc8019492008-07-14 14:44:59 +03004766#ifdef CONFIG_X86_64
4767#define R "r"
4768#define Q "q"
4769#else
4770#define R "e"
4771#define Q "l"
4772#endif
4773
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08004774static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004775{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004776 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity104f2262010-11-18 13:12:52 +02004777
4778 /* Record the guest's net vcpu time for enforced NMI injections. */
4779 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
4780 vmx->entry_time = ktime_get();
4781
4782 /* Don't enter VMX if guest state is invalid, let the exit handler
4783 start emulation until we arrive back to a valid state */
4784 if (vmx->emulation_required && emulate_invalid_guest_state)
4785 return;
4786
4787 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
4788 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
4789 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
4790 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
4791
4792 /* When single-stepping over STI and MOV SS, we must clear the
4793 * corresponding interruptibility bits in the guest state. Otherwise
4794 * vmentry fails as it then expects bit 14 (BS) in pending debug
4795 * exceptions being set, but that's not correct for the guest debugging
4796 * case. */
4797 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
4798 vmx_set_interrupt_shadow(vcpu, 0);
4799
Nadav Har'Eld462b812011-05-24 15:26:10 +03004800 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02004801 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08004802 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03004803 "push %%"R"dx; push %%"R"bp;"
Avi Kivity40712fa2011-01-06 18:09:12 +02004804 "push %%"R"cx \n\t" /* placeholder for guest rcx */
Avi Kivityc8019492008-07-14 14:44:59 +03004805 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03004806 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
4807 "je 1f \n\t"
4808 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004809 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03004810 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03004811 /* Reload cr2 if changed */
4812 "mov %c[cr2](%0), %%"R"ax \n\t"
4813 "mov %%cr2, %%"R"dx \n\t"
4814 "cmp %%"R"ax, %%"R"dx \n\t"
4815 "je 2f \n\t"
4816 "mov %%"R"ax, %%cr2 \n\t"
4817 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004818 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02004819 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004820 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03004821 "mov %c[rax](%0), %%"R"ax \n\t"
4822 "mov %c[rbx](%0), %%"R"bx \n\t"
4823 "mov %c[rdx](%0), %%"R"dx \n\t"
4824 "mov %c[rsi](%0), %%"R"si \n\t"
4825 "mov %c[rdi](%0), %%"R"di \n\t"
4826 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004827#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02004828 "mov %c[r8](%0), %%r8 \n\t"
4829 "mov %c[r9](%0), %%r9 \n\t"
4830 "mov %c[r10](%0), %%r10 \n\t"
4831 "mov %c[r11](%0), %%r11 \n\t"
4832 "mov %c[r12](%0), %%r12 \n\t"
4833 "mov %c[r13](%0), %%r13 \n\t"
4834 "mov %c[r14](%0), %%r14 \n\t"
4835 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004836#endif
Avi Kivityc8019492008-07-14 14:44:59 +03004837 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
4838
Avi Kivity6aa8b732006-12-10 02:21:36 -08004839 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03004840 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004841 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03004842 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004843 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03004844 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004845 /* Save guest registers, load host registers, keep flags */
Avi Kivity40712fa2011-01-06 18:09:12 +02004846 "mov %0, %c[wordsize](%%"R"sp) \n\t"
4847 "pop %0 \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03004848 "mov %%"R"ax, %c[rax](%0) \n\t"
4849 "mov %%"R"bx, %c[rbx](%0) \n\t"
Avi Kivity1c696d02011-01-06 18:09:11 +02004850 "pop"Q" %c[rcx](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03004851 "mov %%"R"dx, %c[rdx](%0) \n\t"
4852 "mov %%"R"si, %c[rsi](%0) \n\t"
4853 "mov %%"R"di, %c[rdi](%0) \n\t"
4854 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004855#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02004856 "mov %%r8, %c[r8](%0) \n\t"
4857 "mov %%r9, %c[r9](%0) \n\t"
4858 "mov %%r10, %c[r10](%0) \n\t"
4859 "mov %%r11, %c[r11](%0) \n\t"
4860 "mov %%r12, %c[r12](%0) \n\t"
4861 "mov %%r13, %c[r13](%0) \n\t"
4862 "mov %%r14, %c[r14](%0) \n\t"
4863 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004864#endif
Avi Kivityc8019492008-07-14 14:44:59 +03004865 "mov %%cr2, %%"R"ax \n\t"
4866 "mov %%"R"ax, %c[cr2](%0) \n\t"
4867
Avi Kivity1c696d02011-01-06 18:09:11 +02004868 "pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02004869 "setbe %c[fail](%0) \n\t"
4870 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03004871 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02004872 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03004873 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004874 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
4875 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
4876 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
4877 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
4878 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
4879 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
4880 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004881#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004882 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
4883 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
4884 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
4885 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
4886 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
4887 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
4888 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
4889 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08004890#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02004891 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
4892 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02004893 : "cc", "memory"
Jan Kiszka07d6f552010-09-28 16:37:42 +02004894 , R"ax", R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02004895#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02004896 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
4897#endif
4898 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08004899
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004900 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02004901 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivity69c73022011-03-07 15:26:44 +02004902 | (1 << VCPU_EXREG_CPL)
Avi Kivityaff48ba2010-12-05 18:56:11 +02004903 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03004904 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02004905 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004906 vcpu->arch.regs_dirty = 0;
4907
Avi Kivity1155f762007-11-22 11:30:47 +02004908 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
4909
Mike Dayd77c26f2007-10-08 09:02:08 -04004910 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Nadav Har'Eld462b812011-05-24 15:26:10 +03004911 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02004912
Avi Kivity51aa01d2010-07-20 14:31:20 +03004913 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03004914
4915 vmx_complete_atomic_exit(vmx);
4916 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03004917 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004918}
4919
Avi Kivityc8019492008-07-14 14:44:59 +03004920#undef R
4921#undef Q
4922
Avi Kivity6aa8b732006-12-10 02:21:36 -08004923static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
4924{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004925 struct vcpu_vmx *vmx = to_vmx(vcpu);
4926
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004927 free_vpid(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03004928 free_nested(vmx);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004929 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004930 kfree(vmx->guest_msrs);
4931 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10004932 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004933}
4934
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004935static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004936{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004937 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10004938 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03004939 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004940
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004941 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004942 return ERR_PTR(-ENOMEM);
4943
Sheng Yang2384d2b2008-01-17 15:14:33 +08004944 allocate_vpid(vmx);
4945
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004946 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
4947 if (err)
4948 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08004949
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004950 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02004951 err = -ENOMEM;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004952 if (!vmx->guest_msrs) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004953 goto uninit_vcpu;
4954 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08004955
Nadav Har'Eld462b812011-05-24 15:26:10 +03004956 vmx->loaded_vmcs = &vmx->vmcs01;
4957 vmx->loaded_vmcs->vmcs = alloc_vmcs();
4958 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004959 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03004960 if (!vmm_exclusive)
4961 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
4962 loaded_vmcs_init(vmx->loaded_vmcs);
4963 if (!vmm_exclusive)
4964 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004965
Avi Kivity15ad7142007-07-11 18:17:21 +03004966 cpu = get_cpu();
4967 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10004968 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10004969 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004970 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03004971 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004972 if (err)
4973 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02004974 if (vm_need_virtualize_apic_accesses(kvm))
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02004975 err = alloc_apic_access_page(kvm);
4976 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02004977 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08004978
Sheng Yangb927a3c2009-07-21 10:42:48 +08004979 if (enable_ept) {
4980 if (!kvm->arch.ept_identity_map_addr)
4981 kvm->arch.ept_identity_map_addr =
4982 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Gleb Natapov93ea5382011-02-21 12:07:59 +02004983 err = -ENOMEM;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004984 if (alloc_identity_pagetable(kvm) != 0)
4985 goto free_vmcs;
Gleb Natapov93ea5382011-02-21 12:07:59 +02004986 if (!init_rmode_identity_map(kvm))
4987 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004988 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004989
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03004990 vmx->nested.current_vmptr = -1ull;
4991 vmx->nested.current_vmcs12 = NULL;
4992
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004993 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08004994
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004995free_vmcs:
Nadav Har'Eld462b812011-05-24 15:26:10 +03004996 free_vmcs(vmx->loaded_vmcs->vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004997free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004998 kfree(vmx->guest_msrs);
4999uninit_vcpu:
5000 kvm_vcpu_uninit(&vmx->vcpu);
5001free_vcpu:
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005002 free_vpid(vmx);
Rusty Russella4770342007-08-01 14:46:11 +10005003 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005004 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005005}
5006
Yang, Sheng002c7f72007-07-31 14:23:01 +03005007static void __init vmx_check_processor_compat(void *rtn)
5008{
5009 struct vmcs_config vmcs_conf;
5010
5011 *(int *)rtn = 0;
5012 if (setup_vmcs_config(&vmcs_conf) < 0)
5013 *(int *)rtn = -EIO;
5014 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
5015 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
5016 smp_processor_id());
5017 *(int *)rtn = -EIO;
5018 }
5019}
5020
Sheng Yang67253af2008-04-25 10:20:22 +08005021static int get_ept_level(void)
5022{
5023 return VMX_EPT_DEFAULT_GAW + 1;
5024}
5025
Sheng Yang4b12f0d2009-04-27 20:35:42 +08005026static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08005027{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08005028 u64 ret;
5029
Sheng Yang522c68c2009-04-27 20:35:43 +08005030 /* For VT-d and EPT combination
5031 * 1. MMIO: always map as UC
5032 * 2. EPT with VT-d:
5033 * a. VT-d without snooping control feature: can't guarantee the
5034 * result, try to trust guest.
5035 * b. VT-d with snooping control feature: snooping control feature of
5036 * VT-d engine can guarantee the cache correctness. Just set it
5037 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08005038 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08005039 * consistent with host MTRR
5040 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08005041 if (is_mmio)
5042 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08005043 else if (vcpu->kvm->arch.iommu_domain &&
5044 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
5045 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
5046 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08005047 else
Sheng Yang522c68c2009-04-27 20:35:43 +08005048 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
Sheng Yanga19a6d12010-02-09 16:41:53 +08005049 | VMX_EPT_IPAT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08005050
5051 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08005052}
5053
Avi Kivityf4c9e872009-12-28 16:06:35 +02005054#define _ER(x) { EXIT_REASON_##x, #x }
5055
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005056static const struct trace_print_flags vmx_exit_reasons_str[] = {
Avi Kivityf4c9e872009-12-28 16:06:35 +02005057 _ER(EXCEPTION_NMI),
5058 _ER(EXTERNAL_INTERRUPT),
5059 _ER(TRIPLE_FAULT),
5060 _ER(PENDING_INTERRUPT),
5061 _ER(NMI_WINDOW),
5062 _ER(TASK_SWITCH),
5063 _ER(CPUID),
5064 _ER(HLT),
5065 _ER(INVLPG),
5066 _ER(RDPMC),
5067 _ER(RDTSC),
5068 _ER(VMCALL),
5069 _ER(VMCLEAR),
5070 _ER(VMLAUNCH),
5071 _ER(VMPTRLD),
5072 _ER(VMPTRST),
5073 _ER(VMREAD),
5074 _ER(VMRESUME),
5075 _ER(VMWRITE),
5076 _ER(VMOFF),
5077 _ER(VMON),
5078 _ER(CR_ACCESS),
5079 _ER(DR_ACCESS),
5080 _ER(IO_INSTRUCTION),
5081 _ER(MSR_READ),
5082 _ER(MSR_WRITE),
5083 _ER(MWAIT_INSTRUCTION),
5084 _ER(MONITOR_INSTRUCTION),
5085 _ER(PAUSE_INSTRUCTION),
5086 _ER(MCE_DURING_VMENTRY),
5087 _ER(TPR_BELOW_THRESHOLD),
5088 _ER(APIC_ACCESS),
5089 _ER(EPT_VIOLATION),
5090 _ER(EPT_MISCONFIG),
5091 _ER(WBINVD),
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005092 { -1, NULL }
5093};
5094
Avi Kivityf4c9e872009-12-28 16:06:35 +02005095#undef _ER
5096
Sheng Yang17cc3932010-01-05 19:02:27 +08005097static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02005098{
Sheng Yang878403b2010-01-05 19:02:29 +08005099 if (enable_ept && !cpu_has_vmx_ept_1g_page())
5100 return PT_DIRECTORY_LEVEL;
5101 else
5102 /* For shadow and EPT supported 1GB page */
5103 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02005104}
5105
Sheng Yang0e851882009-12-18 16:48:46 +08005106static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
5107{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005108 struct kvm_cpuid_entry2 *best;
5109 struct vcpu_vmx *vmx = to_vmx(vcpu);
5110 u32 exec_control;
5111
5112 vmx->rdtscp_enabled = false;
5113 if (vmx_rdtscp_supported()) {
5114 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
5115 if (exec_control & SECONDARY_EXEC_RDTSCP) {
5116 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
5117 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
5118 vmx->rdtscp_enabled = true;
5119 else {
5120 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5121 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5122 exec_control);
5123 }
5124 }
5125 }
Sheng Yang0e851882009-12-18 16:48:46 +08005126}
5127
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005128static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
5129{
5130}
5131
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005132static int vmx_check_intercept(struct kvm_vcpu *vcpu,
5133 struct x86_instruction_info *info,
5134 enum x86_intercept_stage stage)
5135{
5136 return X86EMUL_CONTINUE;
5137}
5138
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03005139static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005140 .cpu_has_kvm_support = cpu_has_kvm_support,
5141 .disabled_by_bios = vmx_disabled_by_bios,
5142 .hardware_setup = hardware_setup,
5143 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03005144 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005145 .hardware_enable = hardware_enable,
5146 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08005147 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005148
5149 .vcpu_create = vmx_create_vcpu,
5150 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03005151 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005152
Avi Kivity04d2cc72007-09-10 18:10:54 +03005153 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005154 .vcpu_load = vmx_vcpu_load,
5155 .vcpu_put = vmx_vcpu_put,
5156
5157 .set_guest_debug = set_guest_debug,
5158 .get_msr = vmx_get_msr,
5159 .set_msr = vmx_set_msr,
5160 .get_segment_base = vmx_get_segment_base,
5161 .get_segment = vmx_get_segment,
5162 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02005163 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005164 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02005165 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02005166 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03005167 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005168 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005169 .set_cr3 = vmx_set_cr3,
5170 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005171 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005172 .get_idt = vmx_get_idt,
5173 .set_idt = vmx_set_idt,
5174 .get_gdt = vmx_get_gdt,
5175 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03005176 .set_dr7 = vmx_set_dr7,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005177 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005178 .get_rflags = vmx_get_rflags,
5179 .set_rflags = vmx_set_rflags,
Avi Kivityebcbab42010-02-07 11:56:52 +02005180 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02005181 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005182
5183 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005184
Avi Kivity6aa8b732006-12-10 02:21:36 -08005185 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02005186 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005187 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04005188 .set_interrupt_shadow = vmx_set_interrupt_shadow,
5189 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02005190 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03005191 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005192 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02005193 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03005194 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02005195 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005196 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005197 .get_nmi_mask = vmx_get_nmi_mask,
5198 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005199 .enable_nmi_window = enable_nmi_window,
5200 .enable_irq_window = enable_irq_window,
5201 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005202
Izik Eiduscbc94022007-10-25 00:29:55 +02005203 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08005204 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08005205 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005206
Avi Kivity586f9602010-11-18 13:09:54 +02005207 .get_exit_info = vmx_get_exit_info,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005208 .exit_reasons_str = vmx_exit_reasons_str,
Avi Kivity586f9602010-11-18 13:09:54 +02005209
Sheng Yang17cc3932010-01-05 19:02:27 +08005210 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08005211
5212 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005213
5214 .rdtscp_supported = vmx_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005215
5216 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005217
5218 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10005219
Joerg Roedel4051b182011-03-25 09:44:49 +01005220 .set_tsc_khz = vmx_set_tsc_khz,
Zachary Amsden99e3e302010-08-19 22:07:17 -10005221 .write_tsc_offset = vmx_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10005222 .adjust_tsc_offset = vmx_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01005223 .compute_tsc_offset = vmx_compute_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005224
5225 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005226
5227 .check_intercept = vmx_check_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005228};
5229
5230static int __init vmx_init(void)
5231{
Avi Kivity26bb0982009-09-07 11:14:12 +03005232 int r, i;
5233
5234 rdmsrl_safe(MSR_EFER, &host_efer);
5235
5236 for (i = 0; i < NR_VMX_MSR; ++i)
5237 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03005238
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005239 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03005240 if (!vmx_io_bitmap_a)
5241 return -ENOMEM;
5242
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005243 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03005244 if (!vmx_io_bitmap_b) {
5245 r = -ENOMEM;
5246 goto out;
5247 }
5248
Avi Kivity58972972009-02-24 22:26:47 +02005249 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
5250 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08005251 r = -ENOMEM;
5252 goto out1;
5253 }
5254
Avi Kivity58972972009-02-24 22:26:47 +02005255 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
5256 if (!vmx_msr_bitmap_longmode) {
5257 r = -ENOMEM;
5258 goto out2;
5259 }
5260
He, Qingfdef3ad2007-04-30 09:45:24 +03005261 /*
5262 * Allow direct access to the PC debug port (it is often used for I/O
5263 * delays, but the vmexits simply slow things down).
5264 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005265 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
5266 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03005267
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005268 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03005269
Avi Kivity58972972009-02-24 22:26:47 +02005270 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
5271 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08005272
Sheng Yang2384d2b2008-01-17 15:14:33 +08005273 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
5274
Avi Kivity0ee75be2010-04-28 15:39:01 +03005275 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
5276 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03005277 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02005278 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08005279
Avi Kivity58972972009-02-24 22:26:47 +02005280 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
5281 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
5282 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
5283 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
5284 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
5285 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03005286
Avi Kivity089d0342009-03-23 18:26:32 +02005287 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08005288 bypass_guest_pf = 0;
Sheng Yang534e38b2008-09-08 15:12:30 +08005289 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08005290 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08005291 kvm_enable_tdp();
5292 } else
5293 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08005294
Avi Kivityc7addb92007-09-16 18:58:32 +02005295 if (bypass_guest_pf)
5296 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
5297
He, Qingfdef3ad2007-04-30 09:45:24 +03005298 return 0;
5299
Avi Kivity58972972009-02-24 22:26:47 +02005300out3:
5301 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08005302out2:
Avi Kivity58972972009-02-24 22:26:47 +02005303 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03005304out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005305 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03005306out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005307 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03005308 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005309}
5310
5311static void __exit vmx_exit(void)
5312{
Avi Kivity58972972009-02-24 22:26:47 +02005313 free_page((unsigned long)vmx_msr_bitmap_legacy);
5314 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005315 free_page((unsigned long)vmx_io_bitmap_b);
5316 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03005317
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08005318 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08005319}
5320
5321module_init(vmx_init)
5322module_exit(vmx_exit)