blob: ad978a6c282e896145d2424dd4f799591218d5ba [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 Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Avi Kivitye4956062007-06-28 14:15:57 -040022
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020025#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/mm.h>
27#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040028#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020029#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070030#include <linux/mod_devicetable.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030031#include <linux/ftrace_event.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040033#include <linux/tboot.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030034#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030035#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040036
Avi Kivity6aa8b732006-12-10 02:21:36 -080037#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080038#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020039#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020040#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080041#include <asm/mce.h>
Dexuan Cui2acf9232010-06-10 11:27:12 +080042#include <asm/i387.h>
43#include <asm/xcr.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020044#include <asm/perf_event.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080045#include <asm/kexec.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080046
Marcelo Tosatti229456f2009-06-17 09:22:14 -030047#include "trace.h"
48
Avi Kivity4ecac3f2008-05-13 13:23:38 +030049#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040050#define __ex_clear(x, reg) \
51 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030052
Avi Kivity6aa8b732006-12-10 02:21:36 -080053MODULE_AUTHOR("Qumranet");
54MODULE_LICENSE("GPL");
55
Josh Triplette9bda3b2012-03-20 23:33:51 -070056static const struct x86_cpu_id vmx_cpu_id[] = {
57 X86_FEATURE_MATCH(X86_FEATURE_VMX),
58 {}
59};
60MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
61
Rusty Russell476bc002012-01-13 09:32:18 +103062static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020063module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080064
Rusty Russell476bc002012-01-13 09:32:18 +103065static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020066module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020067
Rusty Russell476bc002012-01-13 09:32:18 +103068static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020069module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080070
Rusty Russell476bc002012-01-13 09:32:18 +103071static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070072module_param_named(unrestricted_guest,
73 enable_unrestricted_guest, bool, S_IRUGO);
74
Xudong Hao83c3a332012-05-28 19:33:35 +080075static bool __read_mostly enable_ept_ad_bits = 1;
76module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
77
Avi Kivitya27685c2012-06-12 20:30:18 +030078static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020079module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030080
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly vmm_exclusive = 1;
Dongxiao Xub923e622010-05-11 18:29:45 +080082module_param(vmm_exclusive, bool, S_IRUGO);
83
Rusty Russell476bc002012-01-13 09:32:18 +103084static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030085module_param(fasteoi, bool, S_IRUGO);
86
Yang Zhang257090f2013-02-10 22:57:18 +080087static bool __read_mostly enable_apicv_reg_vid;
Yang Zhang83d4c282013-01-25 10:18:49 +080088
Nadav Har'El801d3422011-05-25 23:02:23 +030089/*
90 * If nested=1, nested virtualization is supported, i.e., guests may use
91 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
92 * use VMX instructions.
93 */
Rusty Russell476bc002012-01-13 09:32:18 +103094static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +030095module_param(nested, bool, S_IRUGO);
96
Gleb Natapov50378782013-02-04 16:00:28 +020097#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
98#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +020099#define KVM_VM_CR0_ALWAYS_ON \
100 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200101#define KVM_CR4_GUEST_OWNED_BITS \
102 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
103 | X86_CR4_OSXMMEXCPT)
104
Avi Kivitycdc0e242009-12-06 17:21:14 +0200105#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
106#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
107
Avi Kivity78ac8b42010-04-08 18:19:35 +0300108#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
109
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800110/*
111 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
112 * ple_gap: upper bound on the amount of time between two successive
113 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500114 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800115 * ple_window: upper bound on the amount of time a guest is allowed to execute
116 * in a PAUSE loop. Tests indicate that most spinlocks are held for
117 * less than 2^12 cycles
118 * Time is measured based on a counter that runs at the same rate as the TSC,
119 * refer SDM volume 3b section 21.6.13 & 22.1.3.
120 */
Rik van Riel00c25bc2011-01-04 09:51:33 -0500121#define KVM_VMX_DEFAULT_PLE_GAP 128
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800122#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
123static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
124module_param(ple_gap, int, S_IRUGO);
125
126static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
127module_param(ple_window, int, S_IRUGO);
128
Avi Kivity83287ea422012-09-16 15:10:57 +0300129extern const ulong vmx_return;
130
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200131#define NR_AUTOLOAD_MSRS 8
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300132#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300133
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400134struct vmcs {
135 u32 revision_id;
136 u32 abort;
137 char data[0];
138};
139
Nadav Har'Eld462b812011-05-24 15:26:10 +0300140/*
141 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
142 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
143 * loaded on this CPU (so we can clear them if the CPU goes down).
144 */
145struct loaded_vmcs {
146 struct vmcs *vmcs;
147 int cpu;
148 int launched;
149 struct list_head loaded_vmcss_on_cpu_link;
150};
151
Avi Kivity26bb0982009-09-07 11:14:12 +0300152struct shared_msr_entry {
153 unsigned index;
154 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200155 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300156};
157
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300158/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300159 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
160 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
161 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
162 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
163 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
164 * More than one of these structures may exist, if L1 runs multiple L2 guests.
165 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
166 * underlying hardware which will be used to run L2.
167 * This structure is packed to ensure that its layout is identical across
168 * machines (necessary for live migration).
169 * If there are changes in this struct, VMCS12_REVISION must be changed.
170 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300171typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300172struct __packed vmcs12 {
173 /* According to the Intel spec, a VMCS region must start with the
174 * following two fields. Then follow implementation-specific data.
175 */
176 u32 revision_id;
177 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300178
Nadav Har'El27d6c862011-05-25 23:06:59 +0300179 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
180 u32 padding[7]; /* room for future expansion */
181
Nadav Har'El22bd0352011-05-25 23:05:57 +0300182 u64 io_bitmap_a;
183 u64 io_bitmap_b;
184 u64 msr_bitmap;
185 u64 vm_exit_msr_store_addr;
186 u64 vm_exit_msr_load_addr;
187 u64 vm_entry_msr_load_addr;
188 u64 tsc_offset;
189 u64 virtual_apic_page_addr;
190 u64 apic_access_addr;
191 u64 ept_pointer;
192 u64 guest_physical_address;
193 u64 vmcs_link_pointer;
194 u64 guest_ia32_debugctl;
195 u64 guest_ia32_pat;
196 u64 guest_ia32_efer;
197 u64 guest_ia32_perf_global_ctrl;
198 u64 guest_pdptr0;
199 u64 guest_pdptr1;
200 u64 guest_pdptr2;
201 u64 guest_pdptr3;
202 u64 host_ia32_pat;
203 u64 host_ia32_efer;
204 u64 host_ia32_perf_global_ctrl;
205 u64 padding64[8]; /* room for future expansion */
206 /*
207 * To allow migration of L1 (complete with its L2 guests) between
208 * machines of different natural widths (32 or 64 bit), we cannot have
209 * unsigned long fields with no explict size. We use u64 (aliased
210 * natural_width) instead. Luckily, x86 is little-endian.
211 */
212 natural_width cr0_guest_host_mask;
213 natural_width cr4_guest_host_mask;
214 natural_width cr0_read_shadow;
215 natural_width cr4_read_shadow;
216 natural_width cr3_target_value0;
217 natural_width cr3_target_value1;
218 natural_width cr3_target_value2;
219 natural_width cr3_target_value3;
220 natural_width exit_qualification;
221 natural_width guest_linear_address;
222 natural_width guest_cr0;
223 natural_width guest_cr3;
224 natural_width guest_cr4;
225 natural_width guest_es_base;
226 natural_width guest_cs_base;
227 natural_width guest_ss_base;
228 natural_width guest_ds_base;
229 natural_width guest_fs_base;
230 natural_width guest_gs_base;
231 natural_width guest_ldtr_base;
232 natural_width guest_tr_base;
233 natural_width guest_gdtr_base;
234 natural_width guest_idtr_base;
235 natural_width guest_dr7;
236 natural_width guest_rsp;
237 natural_width guest_rip;
238 natural_width guest_rflags;
239 natural_width guest_pending_dbg_exceptions;
240 natural_width guest_sysenter_esp;
241 natural_width guest_sysenter_eip;
242 natural_width host_cr0;
243 natural_width host_cr3;
244 natural_width host_cr4;
245 natural_width host_fs_base;
246 natural_width host_gs_base;
247 natural_width host_tr_base;
248 natural_width host_gdtr_base;
249 natural_width host_idtr_base;
250 natural_width host_ia32_sysenter_esp;
251 natural_width host_ia32_sysenter_eip;
252 natural_width host_rsp;
253 natural_width host_rip;
254 natural_width paddingl[8]; /* room for future expansion */
255 u32 pin_based_vm_exec_control;
256 u32 cpu_based_vm_exec_control;
257 u32 exception_bitmap;
258 u32 page_fault_error_code_mask;
259 u32 page_fault_error_code_match;
260 u32 cr3_target_count;
261 u32 vm_exit_controls;
262 u32 vm_exit_msr_store_count;
263 u32 vm_exit_msr_load_count;
264 u32 vm_entry_controls;
265 u32 vm_entry_msr_load_count;
266 u32 vm_entry_intr_info_field;
267 u32 vm_entry_exception_error_code;
268 u32 vm_entry_instruction_len;
269 u32 tpr_threshold;
270 u32 secondary_vm_exec_control;
271 u32 vm_instruction_error;
272 u32 vm_exit_reason;
273 u32 vm_exit_intr_info;
274 u32 vm_exit_intr_error_code;
275 u32 idt_vectoring_info_field;
276 u32 idt_vectoring_error_code;
277 u32 vm_exit_instruction_len;
278 u32 vmx_instruction_info;
279 u32 guest_es_limit;
280 u32 guest_cs_limit;
281 u32 guest_ss_limit;
282 u32 guest_ds_limit;
283 u32 guest_fs_limit;
284 u32 guest_gs_limit;
285 u32 guest_ldtr_limit;
286 u32 guest_tr_limit;
287 u32 guest_gdtr_limit;
288 u32 guest_idtr_limit;
289 u32 guest_es_ar_bytes;
290 u32 guest_cs_ar_bytes;
291 u32 guest_ss_ar_bytes;
292 u32 guest_ds_ar_bytes;
293 u32 guest_fs_ar_bytes;
294 u32 guest_gs_ar_bytes;
295 u32 guest_ldtr_ar_bytes;
296 u32 guest_tr_ar_bytes;
297 u32 guest_interruptibility_info;
298 u32 guest_activity_state;
299 u32 guest_sysenter_cs;
300 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100301 u32 vmx_preemption_timer_value;
302 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300303 u16 virtual_processor_id;
304 u16 guest_es_selector;
305 u16 guest_cs_selector;
306 u16 guest_ss_selector;
307 u16 guest_ds_selector;
308 u16 guest_fs_selector;
309 u16 guest_gs_selector;
310 u16 guest_ldtr_selector;
311 u16 guest_tr_selector;
312 u16 host_es_selector;
313 u16 host_cs_selector;
314 u16 host_ss_selector;
315 u16 host_ds_selector;
316 u16 host_fs_selector;
317 u16 host_gs_selector;
318 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300319};
320
321/*
322 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
323 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
324 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
325 */
326#define VMCS12_REVISION 0x11e57ed0
327
328/*
329 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
330 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
331 * current implementation, 4K are reserved to avoid future complications.
332 */
333#define VMCS12_SIZE 0x1000
334
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300335/* Used to remember the last vmcs02 used for some recently used vmcs12s */
336struct vmcs02_list {
337 struct list_head list;
338 gpa_t vmptr;
339 struct loaded_vmcs vmcs02;
340};
341
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300342/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300343 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
344 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
345 */
346struct nested_vmx {
347 /* Has the level1 guest done vmxon? */
348 bool vmxon;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300349
350 /* The guest-physical address of the current VMCS L1 keeps for L2 */
351 gpa_t current_vmptr;
352 /* The host-usable pointer to the above */
353 struct page *current_vmcs12_page;
354 struct vmcs12 *current_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300355
356 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
357 struct list_head vmcs02_pool;
358 int vmcs02_num;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300359 u64 vmcs01_tsc_offset;
Nadav Har'El644d7112011-05-25 23:12:35 +0300360 /* L2 must run next, and mustn't decide to exit to L1. */
361 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300362 /*
363 * Guest pages referred to in vmcs02 with host-physical pointers, so
364 * we must keep them pinned while L2 runs.
365 */
366 struct page *apic_access_page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300367};
368
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400369struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000370 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300371 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300372 u8 fail;
Avi Kivity69c73022011-03-07 15:26:44 +0200373 u8 cpl;
Avi Kivity9d58b932011-03-07 16:52:07 +0200374 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300375 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200376 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200377 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300378 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400379 int nmsrs;
380 int save_nmsrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400381#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300382 u64 msr_host_kernel_gs_base;
383 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400384#endif
Nadav Har'Eld462b812011-05-24 15:26:10 +0300385 /*
386 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
387 * non-nested (L1) guest, it always points to vmcs01. For a nested
388 * guest (L2), it points to a different VMCS.
389 */
390 struct loaded_vmcs vmcs01;
391 struct loaded_vmcs *loaded_vmcs;
392 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300393 struct msr_autoload {
394 unsigned nr;
395 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
396 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
397 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400398 struct {
399 int loaded;
400 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300401#ifdef CONFIG_X86_64
402 u16 ds_sel, es_sel;
403#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200404 int gs_ldt_reload_needed;
405 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400406 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200407 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300408 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300409 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300410 struct kvm_segment segs[8];
411 } rmode;
412 struct {
413 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300414 struct kvm_save_segment {
415 u16 selector;
416 unsigned long base;
417 u32 limit;
418 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300419 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300420 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800421 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300422 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200423
424 /* Support for vnmi-less CPUs */
425 int soft_vnmi_blocked;
426 ktime_t entry_time;
427 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800428 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800429
430 bool rdtscp_enabled;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300431
432 /* Support for a guest hypervisor (nested VMX) */
433 struct nested_vmx nested;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400434};
435
Avi Kivity2fb92db2011-04-27 19:42:18 +0300436enum segment_cache_field {
437 SEG_FIELD_SEL = 0,
438 SEG_FIELD_BASE = 1,
439 SEG_FIELD_LIMIT = 2,
440 SEG_FIELD_AR = 3,
441
442 SEG_FIELD_NR = 4
443};
444
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400445static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
446{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000447 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400448}
449
Nadav Har'El22bd0352011-05-25 23:05:57 +0300450#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
451#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
452#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
453 [number##_HIGH] = VMCS12_OFFSET(name)+4
454
Mathias Krause772e0312012-08-30 01:30:19 +0200455static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300456 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
457 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
458 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
459 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
460 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
461 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
462 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
463 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
464 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
465 FIELD(HOST_ES_SELECTOR, host_es_selector),
466 FIELD(HOST_CS_SELECTOR, host_cs_selector),
467 FIELD(HOST_SS_SELECTOR, host_ss_selector),
468 FIELD(HOST_DS_SELECTOR, host_ds_selector),
469 FIELD(HOST_FS_SELECTOR, host_fs_selector),
470 FIELD(HOST_GS_SELECTOR, host_gs_selector),
471 FIELD(HOST_TR_SELECTOR, host_tr_selector),
472 FIELD64(IO_BITMAP_A, io_bitmap_a),
473 FIELD64(IO_BITMAP_B, io_bitmap_b),
474 FIELD64(MSR_BITMAP, msr_bitmap),
475 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
476 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
477 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
478 FIELD64(TSC_OFFSET, tsc_offset),
479 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
480 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
481 FIELD64(EPT_POINTER, ept_pointer),
482 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
483 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
484 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
485 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
486 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
487 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
488 FIELD64(GUEST_PDPTR0, guest_pdptr0),
489 FIELD64(GUEST_PDPTR1, guest_pdptr1),
490 FIELD64(GUEST_PDPTR2, guest_pdptr2),
491 FIELD64(GUEST_PDPTR3, guest_pdptr3),
492 FIELD64(HOST_IA32_PAT, host_ia32_pat),
493 FIELD64(HOST_IA32_EFER, host_ia32_efer),
494 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
495 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
496 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
497 FIELD(EXCEPTION_BITMAP, exception_bitmap),
498 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
499 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
500 FIELD(CR3_TARGET_COUNT, cr3_target_count),
501 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
502 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
503 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
504 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
505 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
506 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
507 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
508 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
509 FIELD(TPR_THRESHOLD, tpr_threshold),
510 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
511 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
512 FIELD(VM_EXIT_REASON, vm_exit_reason),
513 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
514 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
515 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
516 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
517 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
518 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
519 FIELD(GUEST_ES_LIMIT, guest_es_limit),
520 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
521 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
522 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
523 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
524 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
525 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
526 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
527 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
528 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
529 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
530 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
531 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
532 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
533 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
534 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
535 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
536 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
537 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
538 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
539 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
540 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100541 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300542 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
543 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
544 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
545 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
546 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
547 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
548 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
549 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
550 FIELD(EXIT_QUALIFICATION, exit_qualification),
551 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
552 FIELD(GUEST_CR0, guest_cr0),
553 FIELD(GUEST_CR3, guest_cr3),
554 FIELD(GUEST_CR4, guest_cr4),
555 FIELD(GUEST_ES_BASE, guest_es_base),
556 FIELD(GUEST_CS_BASE, guest_cs_base),
557 FIELD(GUEST_SS_BASE, guest_ss_base),
558 FIELD(GUEST_DS_BASE, guest_ds_base),
559 FIELD(GUEST_FS_BASE, guest_fs_base),
560 FIELD(GUEST_GS_BASE, guest_gs_base),
561 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
562 FIELD(GUEST_TR_BASE, guest_tr_base),
563 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
564 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
565 FIELD(GUEST_DR7, guest_dr7),
566 FIELD(GUEST_RSP, guest_rsp),
567 FIELD(GUEST_RIP, guest_rip),
568 FIELD(GUEST_RFLAGS, guest_rflags),
569 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
570 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
571 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
572 FIELD(HOST_CR0, host_cr0),
573 FIELD(HOST_CR3, host_cr3),
574 FIELD(HOST_CR4, host_cr4),
575 FIELD(HOST_FS_BASE, host_fs_base),
576 FIELD(HOST_GS_BASE, host_gs_base),
577 FIELD(HOST_TR_BASE, host_tr_base),
578 FIELD(HOST_GDTR_BASE, host_gdtr_base),
579 FIELD(HOST_IDTR_BASE, host_idtr_base),
580 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
581 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
582 FIELD(HOST_RSP, host_rsp),
583 FIELD(HOST_RIP, host_rip),
584};
585static const int max_vmcs_field = ARRAY_SIZE(vmcs_field_to_offset_table);
586
587static inline short vmcs_field_to_offset(unsigned long field)
588{
589 if (field >= max_vmcs_field || vmcs_field_to_offset_table[field] == 0)
590 return -1;
591 return vmcs_field_to_offset_table[field];
592}
593
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300594static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
595{
596 return to_vmx(vcpu)->nested.current_vmcs12;
597}
598
599static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
600{
601 struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800602 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300603 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +0800604
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300605 return page;
606}
607
608static void nested_release_page(struct page *page)
609{
610 kvm_release_page_dirty(page);
611}
612
613static void nested_release_page_clean(struct page *page)
614{
615 kvm_release_page_clean(page);
616}
617
Sheng Yang4e1096d2008-07-06 19:16:51 +0800618static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800619static void kvm_cpu_vmxon(u64 addr);
620static void kvm_cpu_vmxoff(void);
Avi Kivityaff48ba2010-12-05 18:56:11 +0200621static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200622static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300623static void vmx_set_segment(struct kvm_vcpu *vcpu,
624 struct kvm_segment *var, int seg);
625static void vmx_get_segment(struct kvm_vcpu *vcpu,
626 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200627static bool guest_state_valid(struct kvm_vcpu *vcpu);
628static u32 vmx_segment_access_rights(struct kvm_segment *var);
Avi Kivity75880a02007-06-20 11:20:04 +0300629
Avi Kivity6aa8b732006-12-10 02:21:36 -0800630static DEFINE_PER_CPU(struct vmcs *, vmxarea);
631static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300632/*
633 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
634 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
635 */
636static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300637static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800638
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200639static unsigned long *vmx_io_bitmap_a;
640static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200641static unsigned long *vmx_msr_bitmap_legacy;
642static unsigned long *vmx_msr_bitmap_longmode;
Yang Zhang8d146952013-01-25 10:18:50 +0800643static unsigned long *vmx_msr_bitmap_legacy_x2apic;
644static unsigned long *vmx_msr_bitmap_longmode_x2apic;
He, Qingfdef3ad2007-04-30 09:45:24 +0300645
Avi Kivity110312c2010-12-21 12:54:20 +0200646static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200647static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200648
Sheng Yang2384d2b2008-01-17 15:14:33 +0800649static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
650static DEFINE_SPINLOCK(vmx_vpid_lock);
651
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300652static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653 int size;
654 int order;
655 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300656 u32 pin_based_exec_ctrl;
657 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800658 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300659 u32 vmexit_ctrl;
660 u32 vmentry_ctrl;
661} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800662
Hannes Ederefff9e52008-11-28 17:02:06 +0100663static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800664 u32 ept;
665 u32 vpid;
666} vmx_capability;
667
Avi Kivity6aa8b732006-12-10 02:21:36 -0800668#define VMX_SEGMENT_FIELD(seg) \
669 [VCPU_SREG_##seg] = { \
670 .selector = GUEST_##seg##_SELECTOR, \
671 .base = GUEST_##seg##_BASE, \
672 .limit = GUEST_##seg##_LIMIT, \
673 .ar_bytes = GUEST_##seg##_AR_BYTES, \
674 }
675
Mathias Krause772e0312012-08-30 01:30:19 +0200676static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677 unsigned selector;
678 unsigned base;
679 unsigned limit;
680 unsigned ar_bytes;
681} kvm_vmx_segment_fields[] = {
682 VMX_SEGMENT_FIELD(CS),
683 VMX_SEGMENT_FIELD(DS),
684 VMX_SEGMENT_FIELD(ES),
685 VMX_SEGMENT_FIELD(FS),
686 VMX_SEGMENT_FIELD(GS),
687 VMX_SEGMENT_FIELD(SS),
688 VMX_SEGMENT_FIELD(TR),
689 VMX_SEGMENT_FIELD(LDTR),
690};
691
Avi Kivity26bb0982009-09-07 11:14:12 +0300692static u64 host_efer;
693
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300694static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
695
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300696/*
Brian Gerst8c065852010-07-17 09:03:26 -0400697 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300698 * away by decrementing the array size.
699 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800701#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300702 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400704 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800705};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200706#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800707
Gui Jianfeng31299942010-03-15 17:29:09 +0800708static inline bool is_page_fault(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709{
710 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
711 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100712 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713}
714
Gui Jianfeng31299942010-03-15 17:29:09 +0800715static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300716{
717 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
718 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100719 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300720}
721
Gui Jianfeng31299942010-03-15 17:29:09 +0800722static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500723{
724 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
725 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100726 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500727}
728
Gui Jianfeng31299942010-03-15 17:29:09 +0800729static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730{
731 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
732 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
733}
734
Gui Jianfeng31299942010-03-15 17:29:09 +0800735static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +0800736{
737 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
738 INTR_INFO_VALID_MASK)) ==
739 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
740}
741
Gui Jianfeng31299942010-03-15 17:29:09 +0800742static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +0800743{
Sheng Yang04547152009-04-01 15:52:31 +0800744 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800745}
746
Gui Jianfeng31299942010-03-15 17:29:09 +0800747static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800748{
Sheng Yang04547152009-04-01 15:52:31 +0800749 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800750}
751
Gui Jianfeng31299942010-03-15 17:29:09 +0800752static inline bool vm_need_tpr_shadow(struct kvm *kvm)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800753{
Sheng Yang04547152009-04-01 15:52:31 +0800754 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800755}
756
Gui Jianfeng31299942010-03-15 17:29:09 +0800757static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800758{
Sheng Yang04547152009-04-01 15:52:31 +0800759 return vmcs_config.cpu_based_exec_ctrl &
760 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800761}
762
Avi Kivity774ead32007-12-26 13:57:04 +0200763static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800764{
Sheng Yang04547152009-04-01 15:52:31 +0800765 return vmcs_config.cpu_based_2nd_exec_ctrl &
766 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
767}
768
Yang Zhang8d146952013-01-25 10:18:50 +0800769static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
770{
771 return vmcs_config.cpu_based_2nd_exec_ctrl &
772 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
773}
774
Yang Zhang83d4c282013-01-25 10:18:49 +0800775static inline bool cpu_has_vmx_apic_register_virt(void)
776{
777 return vmcs_config.cpu_based_2nd_exec_ctrl &
778 SECONDARY_EXEC_APIC_REGISTER_VIRT;
779}
780
Yang Zhangc7c9c562013-01-25 10:18:51 +0800781static inline bool cpu_has_vmx_virtual_intr_delivery(void)
782{
783 return vmcs_config.cpu_based_2nd_exec_ctrl &
784 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
785}
786
Sheng Yang04547152009-04-01 15:52:31 +0800787static inline bool cpu_has_vmx_flexpriority(void)
788{
789 return cpu_has_vmx_tpr_shadow() &&
790 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800791}
792
Marcelo Tosattie7997942009-06-11 12:07:40 -0300793static inline bool cpu_has_vmx_ept_execute_only(void)
794{
Gui Jianfeng31299942010-03-15 17:29:09 +0800795 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300796}
797
798static inline bool cpu_has_vmx_eptp_uncacheable(void)
799{
Gui Jianfeng31299942010-03-15 17:29:09 +0800800 return vmx_capability.ept & VMX_EPTP_UC_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300801}
802
803static inline bool cpu_has_vmx_eptp_writeback(void)
804{
Gui Jianfeng31299942010-03-15 17:29:09 +0800805 return vmx_capability.ept & VMX_EPTP_WB_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300806}
807
808static inline bool cpu_has_vmx_ept_2m_page(void)
809{
Gui Jianfeng31299942010-03-15 17:29:09 +0800810 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300811}
812
Sheng Yang878403b2010-01-05 19:02:29 +0800813static inline bool cpu_has_vmx_ept_1g_page(void)
814{
Gui Jianfeng31299942010-03-15 17:29:09 +0800815 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +0800816}
817
Sheng Yang4bc9b982010-06-02 14:05:24 +0800818static inline bool cpu_has_vmx_ept_4levels(void)
819{
820 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
821}
822
Xudong Hao83c3a332012-05-28 19:33:35 +0800823static inline bool cpu_has_vmx_ept_ad_bits(void)
824{
825 return vmx_capability.ept & VMX_EPT_AD_BIT;
826}
827
Gui Jianfeng31299942010-03-15 17:29:09 +0800828static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800829{
Gui Jianfeng31299942010-03-15 17:29:09 +0800830 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800831}
832
Gui Jianfeng31299942010-03-15 17:29:09 +0800833static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800834{
Gui Jianfeng31299942010-03-15 17:29:09 +0800835 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800836}
837
Gui Jianfeng518c8ae2010-06-04 08:51:39 +0800838static inline bool cpu_has_vmx_invvpid_single(void)
839{
840 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
841}
842
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800843static inline bool cpu_has_vmx_invvpid_global(void)
844{
845 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
846}
847
Gui Jianfeng31299942010-03-15 17:29:09 +0800848static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800849{
Sheng Yang04547152009-04-01 15:52:31 +0800850 return vmcs_config.cpu_based_2nd_exec_ctrl &
851 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800852}
853
Gui Jianfeng31299942010-03-15 17:29:09 +0800854static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700855{
856 return vmcs_config.cpu_based_2nd_exec_ctrl &
857 SECONDARY_EXEC_UNRESTRICTED_GUEST;
858}
859
Gui Jianfeng31299942010-03-15 17:29:09 +0800860static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800861{
862 return vmcs_config.cpu_based_2nd_exec_ctrl &
863 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
864}
865
Gui Jianfeng31299942010-03-15 17:29:09 +0800866static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800867{
Gui Jianfeng6d3e4352010-01-29 15:36:59 +0800868 return flexpriority_enabled && irqchip_in_kernel(kvm);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800869}
870
Gui Jianfeng31299942010-03-15 17:29:09 +0800871static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800872{
Sheng Yang04547152009-04-01 15:52:31 +0800873 return vmcs_config.cpu_based_2nd_exec_ctrl &
874 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800875}
876
Gui Jianfeng31299942010-03-15 17:29:09 +0800877static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800878{
879 return vmcs_config.cpu_based_2nd_exec_ctrl &
880 SECONDARY_EXEC_RDTSCP;
881}
882
Mao, Junjiead756a12012-07-02 01:18:48 +0000883static inline bool cpu_has_vmx_invpcid(void)
884{
885 return vmcs_config.cpu_based_2nd_exec_ctrl &
886 SECONDARY_EXEC_ENABLE_INVPCID;
887}
888
Gui Jianfeng31299942010-03-15 17:29:09 +0800889static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +0800890{
891 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
892}
893
Sheng Yangf5f48ee2010-06-30 12:25:15 +0800894static inline bool cpu_has_vmx_wbinvd_exit(void)
895{
896 return vmcs_config.cpu_based_2nd_exec_ctrl &
897 SECONDARY_EXEC_WBINVD_EXITING;
898}
899
Sheng Yang04547152009-04-01 15:52:31 +0800900static inline bool report_flexpriority(void)
901{
902 return flexpriority_enabled;
903}
904
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300905static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
906{
907 return vmcs12->cpu_based_vm_exec_control & bit;
908}
909
910static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
911{
912 return (vmcs12->cpu_based_vm_exec_control &
913 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
914 (vmcs12->secondary_vm_exec_control & bit);
915}
916
Nadav Har'El644d7112011-05-25 23:12:35 +0300917static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12,
918 struct kvm_vcpu *vcpu)
919{
920 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
921}
922
923static inline bool is_exception(u32 intr_info)
924{
925 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
926 == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
927}
928
929static void nested_vmx_vmexit(struct kvm_vcpu *vcpu);
Nadav Har'El7c177932011-05-25 23:12:04 +0300930static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
931 struct vmcs12 *vmcs12,
932 u32 reason, unsigned long qualification);
933
Rusty Russell8b9cf982007-07-30 16:31:43 +1000934static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800935{
936 int i;
937
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400938 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300939 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300940 return i;
941 return -1;
942}
943
Sheng Yang2384d2b2008-01-17 15:14:33 +0800944static inline void __invvpid(int ext, u16 vpid, gva_t gva)
945{
946 struct {
947 u64 vpid : 16;
948 u64 rsvd : 48;
949 u64 gva;
950 } operand = { vpid, 0, gva };
951
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300952 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800953 /* CF==1 or ZF==1 --> rc = -1 */
954 "; ja 1f ; ud2 ; 1:"
955 : : "a"(&operand), "c"(ext) : "cc", "memory");
956}
957
Sheng Yang14394422008-04-28 12:24:45 +0800958static inline void __invept(int ext, u64 eptp, gpa_t gpa)
959{
960 struct {
961 u64 eptp, gpa;
962 } operand = {eptp, gpa};
963
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300964 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800965 /* CF==1 or ZF==1 --> rc = -1 */
966 "; ja 1f ; ud2 ; 1:\n"
967 : : "a" (&operand), "c" (ext) : "cc", "memory");
968}
969
Avi Kivity26bb0982009-09-07 11:14:12 +0300970static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300971{
972 int i;
973
Rusty Russell8b9cf982007-07-30 16:31:43 +1000974 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300975 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400976 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000977 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800978}
979
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980static void vmcs_clear(struct vmcs *vmcs)
981{
982 u64 phys_addr = __pa(vmcs);
983 u8 error;
984
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300985 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +0200986 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987 : "cc", "memory");
988 if (error)
989 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
990 vmcs, phys_addr);
991}
992
Nadav Har'Eld462b812011-05-24 15:26:10 +0300993static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
994{
995 vmcs_clear(loaded_vmcs->vmcs);
996 loaded_vmcs->cpu = -1;
997 loaded_vmcs->launched = 0;
998}
999
Dongxiao Xu7725b892010-05-11 18:29:38 +08001000static void vmcs_load(struct vmcs *vmcs)
1001{
1002 u64 phys_addr = __pa(vmcs);
1003 u8 error;
1004
1005 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001006 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001007 : "cc", "memory");
1008 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001009 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001010 vmcs, phys_addr);
1011}
1012
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001013#ifdef CONFIG_KEXEC
1014/*
1015 * This bitmap is used to indicate whether the vmclear
1016 * operation is enabled on all cpus. All disabled by
1017 * default.
1018 */
1019static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1020
1021static inline void crash_enable_local_vmclear(int cpu)
1022{
1023 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1024}
1025
1026static inline void crash_disable_local_vmclear(int cpu)
1027{
1028 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1029}
1030
1031static inline int crash_local_vmclear_enabled(int cpu)
1032{
1033 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1034}
1035
1036static void crash_vmclear_local_loaded_vmcss(void)
1037{
1038 int cpu = raw_smp_processor_id();
1039 struct loaded_vmcs *v;
1040
1041 if (!crash_local_vmclear_enabled(cpu))
1042 return;
1043
1044 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1045 loaded_vmcss_on_cpu_link)
1046 vmcs_clear(v->vmcs);
1047}
1048#else
1049static inline void crash_enable_local_vmclear(int cpu) { }
1050static inline void crash_disable_local_vmclear(int cpu) { }
1051#endif /* CONFIG_KEXEC */
1052
Nadav Har'Eld462b812011-05-24 15:26:10 +03001053static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001054{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001055 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001056 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057
Nadav Har'Eld462b812011-05-24 15:26:10 +03001058 if (loaded_vmcs->cpu != cpu)
1059 return; /* vcpu migration can race with cpu offline */
1060 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001062 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001063 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001064
1065 /*
1066 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1067 * is before setting loaded_vmcs->vcpu to -1 which is done in
1068 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1069 * then adds the vmcs into percpu list before it is deleted.
1070 */
1071 smp_wmb();
1072
Nadav Har'Eld462b812011-05-24 15:26:10 +03001073 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001074 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075}
1076
Nadav Har'Eld462b812011-05-24 15:26:10 +03001077static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001078{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001079 int cpu = loaded_vmcs->cpu;
1080
1081 if (cpu != -1)
1082 smp_call_function_single(cpu,
1083 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001084}
1085
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001086static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001087{
1088 if (vmx->vpid == 0)
1089 return;
1090
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001091 if (cpu_has_vmx_invvpid_single())
1092 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001093}
1094
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001095static inline void vpid_sync_vcpu_global(void)
1096{
1097 if (cpu_has_vmx_invvpid_global())
1098 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1099}
1100
1101static inline void vpid_sync_context(struct vcpu_vmx *vmx)
1102{
1103 if (cpu_has_vmx_invvpid_single())
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001104 vpid_sync_vcpu_single(vmx);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001105 else
1106 vpid_sync_vcpu_global();
1107}
1108
Sheng Yang14394422008-04-28 12:24:45 +08001109static inline void ept_sync_global(void)
1110{
1111 if (cpu_has_vmx_invept_global())
1112 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1113}
1114
1115static inline void ept_sync_context(u64 eptp)
1116{
Avi Kivity089d0342009-03-23 18:26:32 +02001117 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001118 if (cpu_has_vmx_invept_context())
1119 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1120 else
1121 ept_sync_global();
1122 }
1123}
1124
Avi Kivity96304212011-05-15 10:13:13 -04001125static __always_inline unsigned long vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126{
Avi Kivity5e520e62011-05-15 10:13:12 -04001127 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128
Avi Kivity5e520e62011-05-15 10:13:12 -04001129 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1130 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131 return value;
1132}
1133
Avi Kivity96304212011-05-15 10:13:13 -04001134static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135{
1136 return vmcs_readl(field);
1137}
1138
Avi Kivity96304212011-05-15 10:13:13 -04001139static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001140{
1141 return vmcs_readl(field);
1142}
1143
Avi Kivity96304212011-05-15 10:13:13 -04001144static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145{
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001146#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147 return vmcs_readl(field);
1148#else
1149 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
1150#endif
1151}
1152
Avi Kivitye52de1b2007-01-05 16:36:56 -08001153static noinline void vmwrite_error(unsigned long field, unsigned long value)
1154{
1155 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1156 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1157 dump_stack();
1158}
1159
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160static void vmcs_writel(unsigned long field, unsigned long value)
1161{
1162 u8 error;
1163
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001164 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001165 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001166 if (unlikely(error))
1167 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168}
1169
1170static void vmcs_write16(unsigned long field, u16 value)
1171{
1172 vmcs_writel(field, value);
1173}
1174
1175static void vmcs_write32(unsigned long field, u32 value)
1176{
1177 vmcs_writel(field, value);
1178}
1179
1180static void vmcs_write64(unsigned long field, u64 value)
1181{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001182 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001183#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184 asm volatile ("");
1185 vmcs_writel(field+1, value >> 32);
1186#endif
1187}
1188
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001189static void vmcs_clear_bits(unsigned long field, u32 mask)
1190{
1191 vmcs_writel(field, vmcs_readl(field) & ~mask);
1192}
1193
1194static void vmcs_set_bits(unsigned long field, u32 mask)
1195{
1196 vmcs_writel(field, vmcs_readl(field) | mask);
1197}
1198
Avi Kivity2fb92db2011-04-27 19:42:18 +03001199static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1200{
1201 vmx->segment_cache.bitmask = 0;
1202}
1203
1204static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1205 unsigned field)
1206{
1207 bool ret;
1208 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1209
1210 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1211 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1212 vmx->segment_cache.bitmask = 0;
1213 }
1214 ret = vmx->segment_cache.bitmask & mask;
1215 vmx->segment_cache.bitmask |= mask;
1216 return ret;
1217}
1218
1219static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1220{
1221 u16 *p = &vmx->segment_cache.seg[seg].selector;
1222
1223 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1224 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1225 return *p;
1226}
1227
1228static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1229{
1230 ulong *p = &vmx->segment_cache.seg[seg].base;
1231
1232 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1233 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1234 return *p;
1235}
1236
1237static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1238{
1239 u32 *p = &vmx->segment_cache.seg[seg].limit;
1240
1241 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1242 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1243 return *p;
1244}
1245
1246static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1247{
1248 u32 *p = &vmx->segment_cache.seg[seg].ar;
1249
1250 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1251 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1252 return *p;
1253}
1254
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001255static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1256{
1257 u32 eb;
1258
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001259 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
1260 (1u << NM_VECTOR) | (1u << DB_VECTOR);
1261 if ((vcpu->guest_debug &
1262 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1263 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1264 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001265 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001266 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001267 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001268 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02001269 if (vcpu->fpu_active)
1270 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001271
1272 /* When we are running a nested L2 guest and L1 specified for it a
1273 * certain exception bitmap, we must trap the same exceptions and pass
1274 * them to L1. When running L2, we will only handle the exceptions
1275 * specified above if L1 did not want them.
1276 */
1277 if (is_guest_mode(vcpu))
1278 eb |= get_vmcs12(vcpu)->exception_bitmap;
1279
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001280 vmcs_write32(EXCEPTION_BITMAP, eb);
1281}
1282
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001283static void clear_atomic_switch_msr_special(unsigned long entry,
1284 unsigned long exit)
1285{
1286 vmcs_clear_bits(VM_ENTRY_CONTROLS, entry);
1287 vmcs_clear_bits(VM_EXIT_CONTROLS, exit);
1288}
1289
Avi Kivity61d2ef22010-04-28 16:40:38 +03001290static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1291{
1292 unsigned i;
1293 struct msr_autoload *m = &vmx->msr_autoload;
1294
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001295 switch (msr) {
1296 case MSR_EFER:
1297 if (cpu_has_load_ia32_efer) {
1298 clear_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER,
1299 VM_EXIT_LOAD_IA32_EFER);
1300 return;
1301 }
1302 break;
1303 case MSR_CORE_PERF_GLOBAL_CTRL:
1304 if (cpu_has_load_perf_global_ctrl) {
1305 clear_atomic_switch_msr_special(
1306 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1307 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1308 return;
1309 }
1310 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001311 }
1312
Avi Kivity61d2ef22010-04-28 16:40:38 +03001313 for (i = 0; i < m->nr; ++i)
1314 if (m->guest[i].index == msr)
1315 break;
1316
1317 if (i == m->nr)
1318 return;
1319 --m->nr;
1320 m->guest[i] = m->guest[m->nr];
1321 m->host[i] = m->host[m->nr];
1322 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1323 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1324}
1325
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001326static void add_atomic_switch_msr_special(unsigned long entry,
1327 unsigned long exit, unsigned long guest_val_vmcs,
1328 unsigned long host_val_vmcs, u64 guest_val, u64 host_val)
1329{
1330 vmcs_write64(guest_val_vmcs, guest_val);
1331 vmcs_write64(host_val_vmcs, host_val);
1332 vmcs_set_bits(VM_ENTRY_CONTROLS, entry);
1333 vmcs_set_bits(VM_EXIT_CONTROLS, exit);
1334}
1335
Avi Kivity61d2ef22010-04-28 16:40:38 +03001336static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1337 u64 guest_val, u64 host_val)
1338{
1339 unsigned i;
1340 struct msr_autoload *m = &vmx->msr_autoload;
1341
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001342 switch (msr) {
1343 case MSR_EFER:
1344 if (cpu_has_load_ia32_efer) {
1345 add_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER,
1346 VM_EXIT_LOAD_IA32_EFER,
1347 GUEST_IA32_EFER,
1348 HOST_IA32_EFER,
1349 guest_val, host_val);
1350 return;
1351 }
1352 break;
1353 case MSR_CORE_PERF_GLOBAL_CTRL:
1354 if (cpu_has_load_perf_global_ctrl) {
1355 add_atomic_switch_msr_special(
1356 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1357 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1358 GUEST_IA32_PERF_GLOBAL_CTRL,
1359 HOST_IA32_PERF_GLOBAL_CTRL,
1360 guest_val, host_val);
1361 return;
1362 }
1363 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001364 }
1365
Avi Kivity61d2ef22010-04-28 16:40:38 +03001366 for (i = 0; i < m->nr; ++i)
1367 if (m->guest[i].index == msr)
1368 break;
1369
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001370 if (i == NR_AUTOLOAD_MSRS) {
1371 printk_once(KERN_WARNING"Not enough mst switch entries. "
1372 "Can't add msr %x\n", msr);
1373 return;
1374 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001375 ++m->nr;
1376 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1377 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1378 }
1379
1380 m->guest[i].index = msr;
1381 m->guest[i].value = guest_val;
1382 m->host[i].index = msr;
1383 m->host[i].value = host_val;
1384}
1385
Avi Kivity33ed6322007-05-02 16:54:03 +03001386static void reload_tss(void)
1387{
Avi Kivity33ed6322007-05-02 16:54:03 +03001388 /*
1389 * VT restores TR but not its size. Useless.
1390 */
Avi Kivityd3591922010-07-26 18:32:39 +03001391 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02001392 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03001393
Avi Kivityd3591922010-07-26 18:32:39 +03001394 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03001395 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
1396 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03001397}
1398
Avi Kivity92c0d902009-10-29 11:00:16 +02001399static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001400{
Roel Kluin3a34a882009-08-04 02:08:45 -07001401 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001402 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +03001403
Avi Kivityf6801df2010-01-21 15:31:50 +02001404 guest_efer = vmx->vcpu.arch.efer;
Roel Kluin3a34a882009-08-04 02:08:45 -07001405
Avi Kivity51c6cf62007-08-29 03:48:05 +03001406 /*
Guo Chao0fa06072012-06-28 15:16:19 +08001407 * NX is emulated; LMA and LME handled by hardware; SCE meaningless
Avi Kivity51c6cf62007-08-29 03:48:05 +03001408 * outside long mode
1409 */
1410 ignore_bits = EFER_NX | EFER_SCE;
1411#ifdef CONFIG_X86_64
1412 ignore_bits |= EFER_LMA | EFER_LME;
1413 /* SCE is meaningful only in long mode on Intel */
1414 if (guest_efer & EFER_LMA)
1415 ignore_bits &= ~(u64)EFER_SCE;
1416#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +03001417 guest_efer &= ~ignore_bits;
1418 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +03001419 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +02001420 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03001421
1422 clear_atomic_switch_msr(vmx, MSR_EFER);
1423 /* On ept, can't emulate nx, and must switch nx atomically */
1424 if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
1425 guest_efer = vmx->vcpu.arch.efer;
1426 if (!(guest_efer & EFER_LMA))
1427 guest_efer &= ~EFER_LME;
1428 add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
1429 return false;
1430 }
1431
Avi Kivity26bb0982009-09-07 11:14:12 +03001432 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001433}
1434
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001435static unsigned long segment_base(u16 selector)
1436{
Avi Kivityd3591922010-07-26 18:32:39 +03001437 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001438 struct desc_struct *d;
1439 unsigned long table_base;
1440 unsigned long v;
1441
1442 if (!(selector & ~3))
1443 return 0;
1444
Avi Kivityd3591922010-07-26 18:32:39 +03001445 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001446
1447 if (selector & 4) { /* from ldt */
1448 u16 ldt_selector = kvm_read_ldt();
1449
1450 if (!(ldt_selector & ~3))
1451 return 0;
1452
1453 table_base = segment_base(ldt_selector);
1454 }
1455 d = (struct desc_struct *)(table_base + (selector & ~7));
1456 v = get_desc_base(d);
1457#ifdef CONFIG_X86_64
1458 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
1459 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
1460#endif
1461 return v;
1462}
1463
1464static inline unsigned long kvm_read_tr_base(void)
1465{
1466 u16 tr;
1467 asm("str %0" : "=g"(tr));
1468 return segment_base(tr);
1469}
1470
Avi Kivity04d2cc72007-09-10 18:10:54 +03001471static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001472{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001473 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001474 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001475
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001476 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001477 return;
1478
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001479 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001480 /*
1481 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1482 * allow segment selectors with cpl > 0 or ti == 1.
1483 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001484 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02001485 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02001486 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001487 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001488 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001489 vmx->host_state.fs_reload_needed = 0;
1490 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03001491 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001492 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001493 }
Avi Kivity9581d442010-10-19 16:46:55 +02001494 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001495 if (!(vmx->host_state.gs_sel & 7))
1496 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001497 else {
1498 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001499 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001500 }
1501
1502#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03001503 savesegment(ds, vmx->host_state.ds_sel);
1504 savesegment(es, vmx->host_state.es_sel);
1505#endif
1506
1507#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03001508 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
1509 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
1510#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001511 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
1512 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03001513#endif
Avi Kivity707c0872007-05-02 17:33:43 +03001514
1515#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001516 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
1517 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03001518 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03001519#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03001520 for (i = 0; i < vmx->save_nmsrs; ++i)
1521 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02001522 vmx->guest_msrs[i].data,
1523 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03001524}
1525
Avi Kivitya9b21b62008-06-24 11:48:49 +03001526static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001527{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001528 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001529 return;
1530
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001531 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001532 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02001533#ifdef CONFIG_X86_64
1534 if (is_long_mode(&vmx->vcpu))
1535 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1536#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001537 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001538 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001539#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02001540 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001541#else
1542 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001543#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001544 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02001545 if (vmx->host_state.fs_reload_needed)
1546 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001547#ifdef CONFIG_X86_64
1548 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
1549 loadsegment(ds, vmx->host_state.ds_sel);
1550 loadsegment(es, vmx->host_state.es_sel);
1551 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001552#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001553 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001554#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001555 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001556#endif
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07001557 /*
1558 * If the FPU is not active (through the host task or
1559 * the guest vcpu), then restore the cr0.TS bit.
1560 */
1561 if (!user_has_fpu() && !vmx->vcpu.guest_fpu_loaded)
1562 stts();
Avi Kivity3444d7d2010-07-26 18:32:38 +03001563 load_gdt(&__get_cpu_var(host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03001564}
1565
Avi Kivitya9b21b62008-06-24 11:48:49 +03001566static void vmx_load_host_state(struct vcpu_vmx *vmx)
1567{
1568 preempt_disable();
1569 __vmx_load_host_state(vmx);
1570 preempt_enable();
1571}
1572
Avi Kivity6aa8b732006-12-10 02:21:36 -08001573/*
1574 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1575 * vcpu mutex is already taken.
1576 */
Avi Kivity15ad7142007-07-11 18:17:21 +03001577static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001578{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001579 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001580 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001582 if (!vmm_exclusive)
1583 kvm_cpu_vmxon(phys_addr);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001584 else if (vmx->loaded_vmcs->cpu != cpu)
1585 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586
Nadav Har'Eld462b812011-05-24 15:26:10 +03001587 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1588 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1589 vmcs_load(vmx->loaded_vmcs->vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001590 }
1591
Nadav Har'Eld462b812011-05-24 15:26:10 +03001592 if (vmx->loaded_vmcs->cpu != cpu) {
Avi Kivityd3591922010-07-26 18:32:39 +03001593 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594 unsigned long sysenter_esp;
1595
Avi Kivitya8eeb042010-05-10 12:34:53 +03001596 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001597 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001598 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001599
1600 /*
1601 * Read loaded_vmcs->cpu should be before fetching
1602 * loaded_vmcs->loaded_vmcss_on_cpu_link.
1603 * See the comments in __loaded_vmcs_clear().
1604 */
1605 smp_rmb();
1606
Nadav Har'Eld462b812011-05-24 15:26:10 +03001607 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1608 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001609 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001610 local_irq_enable();
1611
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612 /*
1613 * Linux uses per-cpu TSS and GDT, so set these when switching
1614 * processors.
1615 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001616 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03001617 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618
1619 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1620 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Nadav Har'Eld462b812011-05-24 15:26:10 +03001621 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001623}
1624
1625static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1626{
Avi Kivitya9b21b62008-06-24 11:48:49 +03001627 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001628 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03001629 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
1630 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001631 kvm_cpu_vmxoff();
1632 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633}
1634
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001635static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
1636{
Avi Kivity81231c62010-01-24 16:26:40 +02001637 ulong cr0;
1638
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001639 if (vcpu->fpu_active)
1640 return;
1641 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02001642 cr0 = vmcs_readl(GUEST_CR0);
1643 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
1644 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
1645 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001646 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001647 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001648 if (is_guest_mode(vcpu))
1649 vcpu->arch.cr0_guest_owned_bits &=
1650 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02001651 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001652}
1653
Avi Kivityedcafe32009-12-30 18:07:40 +02001654static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1655
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001656/*
1657 * Return the cr0 value that a nested guest would read. This is a combination
1658 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
1659 * its hypervisor (cr0_read_shadow).
1660 */
1661static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
1662{
1663 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
1664 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
1665}
1666static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
1667{
1668 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
1669 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
1670}
1671
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001672static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
1673{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001674 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
1675 * set this *before* calling this function.
1676 */
Avi Kivityedcafe32009-12-30 18:07:40 +02001677 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02001678 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001679 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001680 vcpu->arch.cr0_guest_owned_bits = 0;
1681 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001682 if (is_guest_mode(vcpu)) {
1683 /*
1684 * L1's specified read shadow might not contain the TS bit,
1685 * so now that we turned on shadowing of this bit, we need to
1686 * set this bit of the shadow. Like in nested_vmx_run we need
1687 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
1688 * up-to-date here because we just decached cr0.TS (and we'll
1689 * only update vmcs12->guest_cr0 on nested exit).
1690 */
1691 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1692 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
1693 (vcpu->arch.cr0 & X86_CR0_TS);
1694 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
1695 } else
1696 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001697}
1698
Avi Kivity6aa8b732006-12-10 02:21:36 -08001699static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1700{
Avi Kivity78ac8b42010-04-08 18:19:35 +03001701 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001702
Avi Kivity6de12732011-03-07 12:51:22 +02001703 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1704 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1705 rflags = vmcs_readl(GUEST_RFLAGS);
1706 if (to_vmx(vcpu)->rmode.vm86_active) {
1707 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1708 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1709 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1710 }
1711 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001712 }
Avi Kivity6de12732011-03-07 12:51:22 +02001713 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714}
1715
1716static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1717{
Avi Kivity6de12732011-03-07 12:51:22 +02001718 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1719 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001720 if (to_vmx(vcpu)->rmode.vm86_active) {
1721 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001722 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001723 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001724 vmcs_writel(GUEST_RFLAGS, rflags);
1725}
1726
Glauber Costa2809f5d2009-05-12 16:21:05 -04001727static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1728{
1729 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1730 int ret = 0;
1731
1732 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001733 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001734 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001735 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001736
1737 return ret & mask;
1738}
1739
1740static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1741{
1742 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1743 u32 interruptibility = interruptibility_old;
1744
1745 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1746
Jan Kiszka48005f62010-02-19 19:38:07 +01001747 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001748 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001749 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001750 interruptibility |= GUEST_INTR_STATE_STI;
1751
1752 if ((interruptibility != interruptibility_old))
1753 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1754}
1755
Avi Kivity6aa8b732006-12-10 02:21:36 -08001756static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1757{
1758 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001759
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001760 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001761 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001762 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001763
Glauber Costa2809f5d2009-05-12 16:21:05 -04001764 /* skipping an emulated instruction also counts */
1765 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001766}
1767
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001768/*
1769 * KVM wants to inject page-faults which it got to the guest. This function
1770 * checks whether in a nested guest, we need to inject them to L1 or L2.
1771 * This function assumes it is called with the exit reason in vmcs02 being
1772 * a #PF exception (this is the only case in which KVM injects a #PF when L2
1773 * is running).
1774 */
1775static int nested_pf_handled(struct kvm_vcpu *vcpu)
1776{
1777 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1778
1779 /* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */
Nadav Har'El95871902012-03-06 16:39:22 +02001780 if (!(vmcs12->exception_bitmap & (1u << PF_VECTOR)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001781 return 0;
1782
1783 nested_vmx_vmexit(vcpu);
1784 return 1;
1785}
1786
Avi Kivity298101d2007-11-25 13:41:11 +02001787static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02001788 bool has_error_code, u32 error_code,
1789 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02001790{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001791 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001792 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001793
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001794 if (nr == PF_VECTOR && is_guest_mode(vcpu) &&
1795 nested_pf_handled(vcpu))
1796 return;
1797
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001798 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001799 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001800 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1801 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001802
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001803 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001804 int inc_eip = 0;
1805 if (kvm_exception_is_soft(nr))
1806 inc_eip = vcpu->arch.event_exit_inst_len;
1807 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02001808 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001809 return;
1810 }
1811
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001812 if (kvm_exception_is_soft(nr)) {
1813 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1814 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001815 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1816 } else
1817 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1818
1819 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02001820}
1821
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001822static bool vmx_rdtscp_supported(void)
1823{
1824 return cpu_has_vmx_rdtscp();
1825}
1826
Mao, Junjiead756a12012-07-02 01:18:48 +00001827static bool vmx_invpcid_supported(void)
1828{
1829 return cpu_has_vmx_invpcid() && enable_ept;
1830}
1831
Avi Kivity6aa8b732006-12-10 02:21:36 -08001832/*
Eddie Donga75beee2007-05-17 18:55:15 +03001833 * Swap MSR entry in host/guest MSR entry array.
1834 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001835static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03001836{
Avi Kivity26bb0982009-09-07 11:14:12 +03001837 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001838
1839 tmp = vmx->guest_msrs[to];
1840 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1841 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001842}
1843
Yang Zhang8d146952013-01-25 10:18:50 +08001844static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
1845{
1846 unsigned long *msr_bitmap;
1847
1848 if (irqchip_in_kernel(vcpu->kvm) && apic_x2apic_mode(vcpu->arch.apic)) {
1849 if (is_long_mode(vcpu))
1850 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
1851 else
1852 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
1853 } else {
1854 if (is_long_mode(vcpu))
1855 msr_bitmap = vmx_msr_bitmap_longmode;
1856 else
1857 msr_bitmap = vmx_msr_bitmap_legacy;
1858 }
1859
1860 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
1861}
1862
Eddie Donga75beee2007-05-17 18:55:15 +03001863/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001864 * Set up the vmcs to automatically save and restore system
1865 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1866 * mode, as fiddling with msrs is very expensive.
1867 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001868static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001869{
Avi Kivity26bb0982009-09-07 11:14:12 +03001870 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03001871
Eddie Donga75beee2007-05-17 18:55:15 +03001872 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001873#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001874 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10001875 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03001876 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001877 move_msr_up(vmx, index, save_nmsrs++);
1878 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001879 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001880 move_msr_up(vmx, index, save_nmsrs++);
1881 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001882 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001883 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001884 index = __find_msr_index(vmx, MSR_TSC_AUX);
1885 if (index >= 0 && vmx->rdtscp_enabled)
1886 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03001887 /*
Brian Gerst8c065852010-07-17 09:03:26 -04001888 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03001889 * if efer.sce is enabled.
1890 */
Brian Gerst8c065852010-07-17 09:03:26 -04001891 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02001892 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10001893 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001894 }
Eddie Donga75beee2007-05-17 18:55:15 +03001895#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02001896 index = __find_msr_index(vmx, MSR_EFER);
1897 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03001898 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001899
Avi Kivity26bb0982009-09-07 11:14:12 +03001900 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02001901
Yang Zhang8d146952013-01-25 10:18:50 +08001902 if (cpu_has_vmx_msr_bitmap())
1903 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03001904}
1905
1906/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001907 * reads and returns guest's timestamp counter "register"
1908 * guest_tsc = host_tsc + tsc_offset -- 21.3
1909 */
1910static u64 guest_read_tsc(void)
1911{
1912 u64 host_tsc, tsc_offset;
1913
1914 rdtscll(host_tsc);
1915 tsc_offset = vmcs_read64(TSC_OFFSET);
1916 return host_tsc + tsc_offset;
1917}
1918
1919/*
Nadav Har'Eld5c17852011-08-02 15:54:20 +03001920 * Like guest_read_tsc, but always returns L1's notion of the timestamp
1921 * counter, even if a nested guest (L2) is currently running.
1922 */
Marcelo Tosatti886b4702012-11-27 23:28:58 -02001923u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Nadav Har'Eld5c17852011-08-02 15:54:20 +03001924{
Marcelo Tosatti886b4702012-11-27 23:28:58 -02001925 u64 tsc_offset;
Nadav Har'Eld5c17852011-08-02 15:54:20 +03001926
Nadav Har'Eld5c17852011-08-02 15:54:20 +03001927 tsc_offset = is_guest_mode(vcpu) ?
1928 to_vmx(vcpu)->nested.vmcs01_tsc_offset :
1929 vmcs_read64(TSC_OFFSET);
1930 return host_tsc + tsc_offset;
1931}
1932
1933/*
Zachary Amsdencc578282012-02-03 15:43:50 -02001934 * Engage any workarounds for mis-matched TSC rates. Currently limited to
1935 * software catchup for faster rates on slower CPUs.
Joerg Roedel4051b182011-03-25 09:44:49 +01001936 */
Zachary Amsdencc578282012-02-03 15:43:50 -02001937static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
Joerg Roedel4051b182011-03-25 09:44:49 +01001938{
Zachary Amsdencc578282012-02-03 15:43:50 -02001939 if (!scale)
1940 return;
1941
1942 if (user_tsc_khz > tsc_khz) {
1943 vcpu->arch.tsc_catchup = 1;
1944 vcpu->arch.tsc_always_catchup = 1;
1945 } else
1946 WARN(1, "user requested TSC rate below hardware speed\n");
Joerg Roedel4051b182011-03-25 09:44:49 +01001947}
1948
Will Auldba904632012-11-29 12:42:50 -08001949static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu)
1950{
1951 return vmcs_read64(TSC_OFFSET);
1952}
1953
Joerg Roedel4051b182011-03-25 09:44:49 +01001954/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10001955 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08001956 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10001957static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001958{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03001959 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03001960 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03001961 * We're here if L1 chose not to trap WRMSR to TSC. According
1962 * to the spec, this should set L1's TSC; The offset that L1
1963 * set for L2 remains unchanged, and still needs to be added
1964 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03001965 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03001966 struct vmcs12 *vmcs12;
1967 to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
1968 /* recalculate vmcs02.TSC_OFFSET: */
1969 vmcs12 = get_vmcs12(vcpu);
1970 vmcs_write64(TSC_OFFSET, offset +
1971 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
1972 vmcs12->tsc_offset : 0));
1973 } else {
1974 vmcs_write64(TSC_OFFSET, offset);
1975 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001976}
1977
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001978static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
Zachary Amsdene48672f2010-08-19 22:07:23 -10001979{
1980 u64 offset = vmcs_read64(TSC_OFFSET);
1981 vmcs_write64(TSC_OFFSET, offset + adjustment);
Nadav Har'El79918252011-05-25 23:15:39 +03001982 if (is_guest_mode(vcpu)) {
1983 /* Even when running L2, the adjustment needs to apply to L1 */
1984 to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
1985 }
Zachary Amsdene48672f2010-08-19 22:07:23 -10001986}
1987
Joerg Roedel857e4092011-03-25 09:44:50 +01001988static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1989{
1990 return target_tsc - native_read_tsc();
1991}
1992
Nadav Har'El801d3422011-05-25 23:02:23 +03001993static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
1994{
1995 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
1996 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
1997}
1998
1999/*
2000 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2001 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2002 * all guests if the "nested" module option is off, and can also be disabled
2003 * for a single guest by disabling its VMX cpuid bit.
2004 */
2005static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2006{
2007 return nested && guest_cpuid_has_vmx(vcpu);
2008}
2009
Avi Kivity6aa8b732006-12-10 02:21:36 -08002010/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002011 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2012 * returned for the various VMX controls MSRs when nested VMX is enabled.
2013 * The same values should also be used to verify that vmcs12 control fields are
2014 * valid during nested entry from L1 to L2.
2015 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2016 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2017 * bit in the high half is on if the corresponding bit in the control field
2018 * may be on. See also vmx_control_verify().
2019 * TODO: allow these variables to be modified (downgraded) by module options
2020 * or other means.
2021 */
2022static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high;
2023static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high;
2024static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high;
2025static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high;
2026static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002027static u32 nested_vmx_misc_low, nested_vmx_misc_high;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002028static __init void nested_vmx_setup_ctls_msrs(void)
2029{
2030 /*
2031 * Note that as a general rule, the high half of the MSRs (bits in
2032 * the control fields which may be 1) should be initialized by the
2033 * intersection of the underlying hardware's MSR (i.e., features which
2034 * can be supported) and the list of features we want to expose -
2035 * because they are known to be properly supported in our code.
2036 * Also, usually, the low half of the MSRs (bits which must be 1) can
2037 * be set to 0, meaning that L1 may turn off any of these bits. The
2038 * reason is that if one of these bits is necessary, it will appear
2039 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2040 * fields of vmcs01 and vmcs02, will turn these bits off - and
2041 * nested_vmx_exit_handled() will not pass related exits to L1.
2042 * These rules have exceptions below.
2043 */
2044
2045 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002046 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
2047 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002048 /*
2049 * According to the Intel spec, if bit 55 of VMX_BASIC is off (as it is
2050 * in our case), bits 1, 2 and 4 (i.e., 0x16) must be 1 in this MSR.
2051 */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002052 nested_vmx_pinbased_ctls_low |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2053 nested_vmx_pinbased_ctls_high &= PIN_BASED_EXT_INTR_MASK |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002054 PIN_BASED_NMI_EXITING | PIN_BASED_VIRTUAL_NMIS |
2055 PIN_BASED_VMX_PREEMPTION_TIMER;
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002056 nested_vmx_pinbased_ctls_high |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002057
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002058 /*
2059 * Exit controls
2060 * If bit 55 of VMX_BASIC is off, bits 0-8 and 10, 11, 13, 14, 16 and
2061 * 17 must be 1.
2062 */
2063 nested_vmx_exit_ctls_low = VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb6f12502011-05-25 23:13:06 +03002064 /* Note that guest use of VM_EXIT_ACK_INTR_ON_EXIT is not supported. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002065#ifdef CONFIG_X86_64
2066 nested_vmx_exit_ctls_high = VM_EXIT_HOST_ADDR_SPACE_SIZE;
2067#else
2068 nested_vmx_exit_ctls_high = 0;
2069#endif
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002070 nested_vmx_exit_ctls_high |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002071
2072 /* entry controls */
2073 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
2074 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high);
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002075 /* If bit 55 of VMX_BASIC is off, bits 0-8 and 12 must be 1. */
2076 nested_vmx_entry_ctls_low = VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002077 nested_vmx_entry_ctls_high &=
2078 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_IA32E_MODE;
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002079 nested_vmx_entry_ctls_high |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002080
2081 /* cpu-based controls */
2082 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
2083 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high);
2084 nested_vmx_procbased_ctls_low = 0;
2085 nested_vmx_procbased_ctls_high &=
2086 CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_USE_TSC_OFFSETING |
2087 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2088 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2089 CPU_BASED_CR3_STORE_EXITING |
2090#ifdef CONFIG_X86_64
2091 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2092#endif
2093 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
2094 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING |
Avi Kivitydbcb4e72012-08-13 15:38:22 +03002095 CPU_BASED_RDPMC_EXITING | CPU_BASED_RDTSC_EXITING |
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002096 CPU_BASED_PAUSE_EXITING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002097 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
2098 /*
2099 * We can allow some features even when not supported by the
2100 * hardware. For example, L1 can specify an MSR bitmap - and we
2101 * can use it to avoid exits to L1 - even when L0 runs L2
2102 * without MSR bitmaps.
2103 */
2104 nested_vmx_procbased_ctls_high |= CPU_BASED_USE_MSR_BITMAPS;
2105
2106 /* secondary cpu-based controls */
2107 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
2108 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high);
2109 nested_vmx_secondary_ctls_low = 0;
2110 nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002111 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2112 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002113
2114 /* miscellaneous data */
2115 rdmsr(MSR_IA32_VMX_MISC, nested_vmx_misc_low, nested_vmx_misc_high);
Jan Kiszka0238ea92013-03-13 11:31:24 +01002116 nested_vmx_misc_low &= VMX_MISC_PREEMPTION_TIMER_RATE_MASK |
2117 VMX_MISC_SAVE_EFER_LMA;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002118 nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002119}
2120
2121static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2122{
2123 /*
2124 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2125 */
2126 return ((control & high) | low) == control;
2127}
2128
2129static inline u64 vmx_control_msr(u32 low, u32 high)
2130{
2131 return low | ((u64)high << 32);
2132}
2133
2134/*
2135 * If we allow our guest to use VMX instructions (i.e., nested VMX), we should
2136 * also let it use VMX-specific MSRs.
2137 * vmx_get_vmx_msr() and vmx_set_vmx_msr() return 1 when we handled a
2138 * VMX-specific MSR, or 0 when we haven't (and the caller should handle it
2139 * like all other MSRs).
2140 */
2141static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2142{
2143 if (!nested_vmx_allowed(vcpu) && msr_index >= MSR_IA32_VMX_BASIC &&
2144 msr_index <= MSR_IA32_VMX_TRUE_ENTRY_CTLS) {
2145 /*
2146 * According to the spec, processors which do not support VMX
2147 * should throw a #GP(0) when VMX capability MSRs are read.
2148 */
2149 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
2150 return 1;
2151 }
2152
2153 switch (msr_index) {
2154 case MSR_IA32_FEATURE_CONTROL:
2155 *pdata = 0;
2156 break;
2157 case MSR_IA32_VMX_BASIC:
2158 /*
2159 * This MSR reports some information about VMX support. We
2160 * should return information about the VMX we emulate for the
2161 * guest, and the VMCS structure we give it - not about the
2162 * VMX support of the underlying hardware.
2163 */
2164 *pdata = VMCS12_REVISION |
2165 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2166 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2167 break;
2168 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2169 case MSR_IA32_VMX_PINBASED_CTLS:
2170 *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low,
2171 nested_vmx_pinbased_ctls_high);
2172 break;
2173 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2174 case MSR_IA32_VMX_PROCBASED_CTLS:
2175 *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low,
2176 nested_vmx_procbased_ctls_high);
2177 break;
2178 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2179 case MSR_IA32_VMX_EXIT_CTLS:
2180 *pdata = vmx_control_msr(nested_vmx_exit_ctls_low,
2181 nested_vmx_exit_ctls_high);
2182 break;
2183 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2184 case MSR_IA32_VMX_ENTRY_CTLS:
2185 *pdata = vmx_control_msr(nested_vmx_entry_ctls_low,
2186 nested_vmx_entry_ctls_high);
2187 break;
2188 case MSR_IA32_VMX_MISC:
Jan Kiszkac18911a2013-03-13 16:06:41 +01002189 *pdata = vmx_control_msr(nested_vmx_misc_low,
2190 nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002191 break;
2192 /*
2193 * These MSRs specify bits which the guest must keep fixed (on or off)
2194 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2195 * We picked the standard core2 setting.
2196 */
2197#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2198#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2199 case MSR_IA32_VMX_CR0_FIXED0:
2200 *pdata = VMXON_CR0_ALWAYSON;
2201 break;
2202 case MSR_IA32_VMX_CR0_FIXED1:
2203 *pdata = -1ULL;
2204 break;
2205 case MSR_IA32_VMX_CR4_FIXED0:
2206 *pdata = VMXON_CR4_ALWAYSON;
2207 break;
2208 case MSR_IA32_VMX_CR4_FIXED1:
2209 *pdata = -1ULL;
2210 break;
2211 case MSR_IA32_VMX_VMCS_ENUM:
2212 *pdata = 0x1f;
2213 break;
2214 case MSR_IA32_VMX_PROCBASED_CTLS2:
2215 *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low,
2216 nested_vmx_secondary_ctls_high);
2217 break;
2218 case MSR_IA32_VMX_EPT_VPID_CAP:
2219 /* Currently, no nested ept or nested vpid */
2220 *pdata = 0;
2221 break;
2222 default:
2223 return 0;
2224 }
2225
2226 return 1;
2227}
2228
2229static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
2230{
2231 if (!nested_vmx_allowed(vcpu))
2232 return 0;
2233
2234 if (msr_index == MSR_IA32_FEATURE_CONTROL)
2235 /* TODO: the right thing. */
2236 return 1;
2237 /*
2238 * No need to treat VMX capability MSRs specially: If we don't handle
2239 * them, handle_wrmsr will #GP(0), which is correct (they are readonly)
2240 */
2241 return 0;
2242}
2243
2244/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002245 * Reads an msr value (of 'msr_index') into 'pdata'.
2246 * Returns 0 on success, non-0 otherwise.
2247 * Assumes vcpu_load() was already called.
2248 */
2249static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2250{
2251 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +03002252 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253
2254 if (!pdata) {
2255 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
2256 return -EINVAL;
2257 }
2258
2259 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002260#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261 case MSR_FS_BASE:
2262 data = vmcs_readl(GUEST_FS_BASE);
2263 break;
2264 case MSR_GS_BASE:
2265 data = vmcs_readl(GUEST_GS_BASE);
2266 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002267 case MSR_KERNEL_GS_BASE:
2268 vmx_load_host_state(to_vmx(vcpu));
2269 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
2270 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03002271#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08002272 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002273 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302274 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002275 data = guest_read_tsc();
2276 break;
2277 case MSR_IA32_SYSENTER_CS:
2278 data = vmcs_read32(GUEST_SYSENTER_CS);
2279 break;
2280 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002281 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002282 break;
2283 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002284 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002286 case MSR_TSC_AUX:
2287 if (!to_vmx(vcpu)->rdtscp_enabled)
2288 return 1;
2289 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002290 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002291 if (vmx_get_vmx_msr(vcpu, msr_index, pdata))
2292 return 0;
Rusty Russell8b9cf982007-07-30 16:31:43 +10002293 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002294 if (msr) {
2295 data = msr->data;
2296 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002297 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002298 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299 }
2300
2301 *pdata = data;
2302 return 0;
2303}
2304
2305/*
2306 * Writes msr value into into the appropriate "register".
2307 * Returns 0 on success, non-0 otherwise.
2308 * Assumes vcpu_load() was already called.
2309 */
Will Auld8fe8ab42012-11-29 12:42:12 -08002310static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002311{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002312 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002313 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03002314 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08002315 u32 msr_index = msr_info->index;
2316 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03002317
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08002319 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08002320 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03002321 break;
Avi Kivity16175a72009-03-23 22:13:44 +02002322#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002323 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002324 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325 vmcs_writel(GUEST_FS_BASE, data);
2326 break;
2327 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002328 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002329 vmcs_writel(GUEST_GS_BASE, data);
2330 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002331 case MSR_KERNEL_GS_BASE:
2332 vmx_load_host_state(vmx);
2333 vmx->msr_guest_kernel_gs_base = data;
2334 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002335#endif
2336 case MSR_IA32_SYSENTER_CS:
2337 vmcs_write32(GUEST_SYSENTER_CS, data);
2338 break;
2339 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002340 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341 break;
2342 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002343 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302345 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08002346 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002348 case MSR_IA32_CR_PAT:
2349 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2350 vmcs_write64(GUEST_IA32_PAT, data);
2351 vcpu->arch.pat = data;
2352 break;
2353 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002354 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002355 break;
Will Auldba904632012-11-29 12:42:50 -08002356 case MSR_IA32_TSC_ADJUST:
2357 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002358 break;
2359 case MSR_TSC_AUX:
2360 if (!vmx->rdtscp_enabled)
2361 return 1;
2362 /* Check reserved bit, higher 32 bits should be zero */
2363 if ((data >> 32) != 0)
2364 return 1;
2365 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002367 if (vmx_set_vmx_msr(vcpu, msr_index, data))
2368 break;
Rusty Russell8b9cf982007-07-30 16:31:43 +10002369 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002370 if (msr) {
2371 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03002372 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
2373 preempt_disable();
Avi Kivity9ee73972012-03-06 14:16:33 +02002374 kvm_set_shared_msr(msr->index, msr->data,
2375 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03002376 preempt_enable();
2377 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002378 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002380 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002381 }
2382
Eddie Dong2cc51562007-05-21 07:28:09 +03002383 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002384}
2385
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002386static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002388 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
2389 switch (reg) {
2390 case VCPU_REGS_RSP:
2391 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2392 break;
2393 case VCPU_REGS_RIP:
2394 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2395 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002396 case VCPU_EXREG_PDPTR:
2397 if (enable_ept)
2398 ept_save_pdptrs(vcpu);
2399 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002400 default:
2401 break;
2402 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002403}
2404
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405static __init int cpu_has_kvm_support(void)
2406{
Eduardo Habkost6210e372008-11-17 19:03:16 -02002407 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408}
2409
2410static __init int vmx_disabled_by_bios(void)
2411{
2412 u64 msr;
2413
2414 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04002415 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08002416 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04002417 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
2418 && tboot_enabled())
2419 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08002420 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04002421 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08002422 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08002423 && !tboot_enabled()) {
2424 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08002425 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04002426 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08002427 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08002428 /* launched w/o TXT and VMX disabled */
2429 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
2430 && !tboot_enabled())
2431 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04002432 }
2433
2434 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435}
2436
Dongxiao Xu7725b892010-05-11 18:29:38 +08002437static void kvm_cpu_vmxon(u64 addr)
2438{
2439 asm volatile (ASM_VMX_VMXON_RAX
2440 : : "a"(&addr), "m"(addr)
2441 : "memory", "cc");
2442}
2443
Alexander Graf10474ae2009-09-15 11:37:46 +02002444static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002445{
2446 int cpu = raw_smp_processor_id();
2447 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04002448 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002449
Alexander Graf10474ae2009-09-15 11:37:46 +02002450 if (read_cr4() & X86_CR4_VMXE)
2451 return -EBUSY;
2452
Nadav Har'Eld462b812011-05-24 15:26:10 +03002453 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002454
2455 /*
2456 * Now we can enable the vmclear operation in kdump
2457 * since the loaded_vmcss_on_cpu list on this cpu
2458 * has been initialized.
2459 *
2460 * Though the cpu is not in VMX operation now, there
2461 * is no problem to enable the vmclear operation
2462 * for the loaded_vmcss_on_cpu list is empty!
2463 */
2464 crash_enable_local_vmclear(cpu);
2465
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04002467
2468 test_bits = FEATURE_CONTROL_LOCKED;
2469 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
2470 if (tboot_enabled())
2471 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
2472
2473 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002474 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04002475 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
2476 }
Rusty Russell66aee912007-07-17 23:34:16 +10002477 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Alexander Graf10474ae2009-09-15 11:37:46 +02002478
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002479 if (vmm_exclusive) {
2480 kvm_cpu_vmxon(phys_addr);
2481 ept_sync_global();
2482 }
Alexander Graf10474ae2009-09-15 11:37:46 +02002483
Avi Kivity3444d7d2010-07-26 18:32:38 +03002484 store_gdt(&__get_cpu_var(host_gdt));
2485
Alexander Graf10474ae2009-09-15 11:37:46 +02002486 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002487}
2488
Nadav Har'Eld462b812011-05-24 15:26:10 +03002489static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03002490{
2491 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03002492 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03002493
Nadav Har'Eld462b812011-05-24 15:26:10 +03002494 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2495 loaded_vmcss_on_cpu_link)
2496 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03002497}
2498
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002499
2500/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2501 * tricks.
2502 */
2503static void kvm_cpu_vmxoff(void)
2504{
2505 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002506}
2507
Avi Kivity6aa8b732006-12-10 02:21:36 -08002508static void hardware_disable(void *garbage)
2509{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002510 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002511 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002512 kvm_cpu_vmxoff();
2513 }
Dongxiao Xu7725b892010-05-11 18:29:38 +08002514 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515}
2516
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002517static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04002518 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519{
2520 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002521 u32 ctl = ctl_min | ctl_opt;
2522
2523 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2524
2525 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2526 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2527
2528 /* Ensure minimum (required) set of control bits are supported. */
2529 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002530 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002531
2532 *result = ctl;
2533 return 0;
2534}
2535
Avi Kivity110312c2010-12-21 12:54:20 +02002536static __init bool allow_1_setting(u32 msr, u32 ctl)
2537{
2538 u32 vmx_msr_low, vmx_msr_high;
2539
2540 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2541 return vmx_msr_high & ctl;
2542}
2543
Yang, Sheng002c7f72007-07-31 14:23:01 +03002544static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002545{
2546 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08002547 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002548 u32 _pin_based_exec_control = 0;
2549 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002550 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002551 u32 _vmexit_control = 0;
2552 u32 _vmentry_control = 0;
2553
2554 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08002555 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002556 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2557 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002558 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002559
Raghavendra K T10166742012-02-07 23:19:20 +05302560 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002561#ifdef CONFIG_X86_64
2562 CPU_BASED_CR8_LOAD_EXITING |
2563 CPU_BASED_CR8_STORE_EXITING |
2564#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08002565 CPU_BASED_CR3_LOAD_EXITING |
2566 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002567 CPU_BASED_USE_IO_BITMAPS |
2568 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03002569 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08002570 CPU_BASED_MWAIT_EXITING |
2571 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02002572 CPU_BASED_INVLPG_EXITING |
2573 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06002574
Sheng Yangf78e0e22007-10-29 09:40:42 +08002575 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08002576 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08002577 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002578 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2579 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002580 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002581#ifdef CONFIG_X86_64
2582 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2583 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2584 ~CPU_BASED_CR8_STORE_EXITING;
2585#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08002586 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002587 min2 = 0;
2588 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08002589 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08002590 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08002591 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002592 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002593 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002594 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00002595 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08002596 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002597 SECONDARY_EXEC_APIC_REGISTER_VIRT |
2598 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
Sheng Yangd56f5462008-04-25 10:13:16 +08002599 if (adjust_vmx_controls(min2, opt2,
2600 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002601 &_cpu_based_2nd_exec_control) < 0)
2602 return -EIO;
2603 }
2604#ifndef CONFIG_X86_64
2605 if (!(_cpu_based_2nd_exec_control &
2606 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2607 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2608#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08002609
2610 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2611 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08002612 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002613 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2614 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08002615
Sheng Yangd56f5462008-04-25 10:13:16 +08002616 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03002617 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2618 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03002619 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2620 CPU_BASED_CR3_STORE_EXITING |
2621 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08002622 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
2623 vmx_capability.ept, vmx_capability.vpid);
2624 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002625
2626 min = 0;
2627#ifdef CONFIG_X86_64
2628 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2629#endif
Sheng Yang468d4722008-10-09 16:01:55 +08002630 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002631 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2632 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002633 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002634
Sheng Yang468d4722008-10-09 16:01:55 +08002635 min = 0;
2636 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002637 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2638 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002639 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002640
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002641 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002642
2643 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2644 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002645 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002646
2647#ifdef CONFIG_X86_64
2648 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2649 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002650 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002651#endif
2652
2653 /* Require Write-Back (WB) memory type for VMCS accesses. */
2654 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002655 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002656
Yang, Sheng002c7f72007-07-31 14:23:01 +03002657 vmcs_conf->size = vmx_msr_high & 0x1fff;
2658 vmcs_conf->order = get_order(vmcs_config.size);
2659 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002660
Yang, Sheng002c7f72007-07-31 14:23:01 +03002661 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2662 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002663 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002664 vmcs_conf->vmexit_ctrl = _vmexit_control;
2665 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002666
Avi Kivity110312c2010-12-21 12:54:20 +02002667 cpu_has_load_ia32_efer =
2668 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2669 VM_ENTRY_LOAD_IA32_EFER)
2670 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2671 VM_EXIT_LOAD_IA32_EFER);
2672
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002673 cpu_has_load_perf_global_ctrl =
2674 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2675 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
2676 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2677 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2678
2679 /*
2680 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
2681 * but due to arrata below it can't be used. Workaround is to use
2682 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2683 *
2684 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
2685 *
2686 * AAK155 (model 26)
2687 * AAP115 (model 30)
2688 * AAT100 (model 37)
2689 * BC86,AAY89,BD102 (model 44)
2690 * BA97 (model 46)
2691 *
2692 */
2693 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
2694 switch (boot_cpu_data.x86_model) {
2695 case 26:
2696 case 30:
2697 case 37:
2698 case 44:
2699 case 46:
2700 cpu_has_load_perf_global_ctrl = false;
2701 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2702 "does not work properly. Using workaround\n");
2703 break;
2704 default:
2705 break;
2706 }
2707 }
2708
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002709 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002710}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711
2712static struct vmcs *alloc_vmcs_cpu(int cpu)
2713{
2714 int node = cpu_to_node(cpu);
2715 struct page *pages;
2716 struct vmcs *vmcs;
2717
Mel Gorman6484eb32009-06-16 15:31:54 -07002718 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719 if (!pages)
2720 return NULL;
2721 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002722 memset(vmcs, 0, vmcs_config.size);
2723 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724 return vmcs;
2725}
2726
2727static struct vmcs *alloc_vmcs(void)
2728{
Ingo Molnard3b2c332007-01-05 16:36:23 -08002729 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08002730}
2731
2732static void free_vmcs(struct vmcs *vmcs)
2733{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002734 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735}
2736
Nadav Har'Eld462b812011-05-24 15:26:10 +03002737/*
2738 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2739 */
2740static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
2741{
2742 if (!loaded_vmcs->vmcs)
2743 return;
2744 loaded_vmcs_clear(loaded_vmcs);
2745 free_vmcs(loaded_vmcs->vmcs);
2746 loaded_vmcs->vmcs = NULL;
2747}
2748
Sam Ravnborg39959582007-06-01 00:47:13 -07002749static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750{
2751 int cpu;
2752
Zachary Amsden3230bb42009-09-29 11:38:37 -10002753 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002754 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10002755 per_cpu(vmxarea, cpu) = NULL;
2756 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002757}
2758
Avi Kivity6aa8b732006-12-10 02:21:36 -08002759static __init int alloc_kvm_area(void)
2760{
2761 int cpu;
2762
Zachary Amsden3230bb42009-09-29 11:38:37 -10002763 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764 struct vmcs *vmcs;
2765
2766 vmcs = alloc_vmcs_cpu(cpu);
2767 if (!vmcs) {
2768 free_kvm_area();
2769 return -ENOMEM;
2770 }
2771
2772 per_cpu(vmxarea, cpu) = vmcs;
2773 }
2774 return 0;
2775}
2776
2777static __init int hardware_setup(void)
2778{
Yang, Sheng002c7f72007-07-31 14:23:01 +03002779 if (setup_vmcs_config(&vmcs_config) < 0)
2780 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01002781
2782 if (boot_cpu_has(X86_FEATURE_NX))
2783 kvm_enable_efer_bits(EFER_NX);
2784
Sheng Yang93ba03c2009-04-01 15:52:32 +08002785 if (!cpu_has_vmx_vpid())
2786 enable_vpid = 0;
2787
Sheng Yang4bc9b982010-06-02 14:05:24 +08002788 if (!cpu_has_vmx_ept() ||
2789 !cpu_has_vmx_ept_4levels()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08002790 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002791 enable_unrestricted_guest = 0;
Xudong Hao83c3a332012-05-28 19:33:35 +08002792 enable_ept_ad_bits = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002793 }
2794
Xudong Hao83c3a332012-05-28 19:33:35 +08002795 if (!cpu_has_vmx_ept_ad_bits())
2796 enable_ept_ad_bits = 0;
2797
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002798 if (!cpu_has_vmx_unrestricted_guest())
2799 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08002800
2801 if (!cpu_has_vmx_flexpriority())
2802 flexpriority_enabled = 0;
2803
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002804 if (!cpu_has_vmx_tpr_shadow())
2805 kvm_x86_ops->update_cr8_intercept = NULL;
2806
Marcelo Tosatti54dee992009-06-11 12:07:44 -03002807 if (enable_ept && !cpu_has_vmx_ept_2m_page())
2808 kvm_disable_largepages();
2809
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002810 if (!cpu_has_vmx_ple())
2811 ple_gap = 0;
2812
Yang Zhangc7c9c562013-01-25 10:18:51 +08002813 if (!cpu_has_vmx_apic_register_virt() ||
2814 !cpu_has_vmx_virtual_intr_delivery())
2815 enable_apicv_reg_vid = 0;
2816
2817 if (enable_apicv_reg_vid)
2818 kvm_x86_ops->update_cr8_intercept = NULL;
2819 else
2820 kvm_x86_ops->hwapic_irr_update = NULL;
Yang Zhang83d4c282013-01-25 10:18:49 +08002821
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002822 if (nested)
2823 nested_vmx_setup_ctls_msrs();
2824
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825 return alloc_kvm_area();
2826}
2827
2828static __exit void hardware_unsetup(void)
2829{
2830 free_kvm_area();
2831}
2832
Gleb Natapov14168782013-01-21 15:36:49 +02002833static bool emulation_required(struct kvm_vcpu *vcpu)
2834{
2835 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2836}
2837
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002838static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02002839 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002840{
Gleb Natapovd99e4152012-12-20 16:57:45 +02002841 if (!emulate_invalid_guest_state) {
2842 /*
2843 * CS and SS RPL should be equal during guest entry according
2844 * to VMX spec, but in reality it is not always so. Since vcpu
2845 * is in the middle of the transition from real mode to
2846 * protected mode it is safe to assume that RPL 0 is a good
2847 * default value.
2848 */
2849 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
2850 save->selector &= ~SELECTOR_RPL_MASK;
2851 save->dpl = save->selector & SELECTOR_RPL_MASK;
2852 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02002854 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002855}
2856
2857static void enter_pmode(struct kvm_vcpu *vcpu)
2858{
2859 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002860 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861
Gleb Natapovd99e4152012-12-20 16:57:45 +02002862 /*
2863 * Update real mode segment cache. It may be not up-to-date if sement
2864 * register was written while vcpu was in a guest mode.
2865 */
2866 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2867 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2868 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2869 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
2870 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2871 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
2872
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002873 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874
Avi Kivity2fb92db2011-04-27 19:42:18 +03002875 vmx_segment_cache_clear(vmx);
2876
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002877 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878
2879 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002880 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2881 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882 vmcs_writel(GUEST_RFLAGS, flags);
2883
Rusty Russell66aee912007-07-17 23:34:16 +10002884 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
2885 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886
2887 update_exception_bitmap(vcpu);
2888
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002889 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2890 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2891 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2892 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2893 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
2894 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Gleb Natapov1f3141e2013-01-21 15:36:41 +02002895
2896 /* CPL is always 0 when CPU enters protected mode */
2897 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
2898 vmx->cpl = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899}
2900
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002901static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902{
Mathias Krause772e0312012-08-30 01:30:19 +02002903 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02002904 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002905
Gleb Natapovd99e4152012-12-20 16:57:45 +02002906 var.dpl = 0x3;
2907 if (seg == VCPU_SREG_CS)
2908 var.type = 0x3;
2909
2910 if (!emulate_invalid_guest_state) {
2911 var.selector = var.base >> 4;
2912 var.base = var.base & 0xffff0;
2913 var.limit = 0xffff;
2914 var.g = 0;
2915 var.db = 0;
2916 var.present = 1;
2917 var.s = 1;
2918 var.l = 0;
2919 var.unusable = 0;
2920 var.type = 0x3;
2921 var.avl = 0;
2922 if (save->base & 0xf)
2923 printk_once(KERN_WARNING "kvm: segment base is not "
2924 "paragraph aligned when entering "
2925 "protected mode (seg=%d)", seg);
2926 }
2927
2928 vmcs_write16(sf->selector, var.selector);
2929 vmcs_write32(sf->base, var.base);
2930 vmcs_write32(sf->limit, var.limit);
2931 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002932}
2933
2934static void enter_rmode(struct kvm_vcpu *vcpu)
2935{
2936 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002937 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002938
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002939 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
2940 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2941 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2942 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2943 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02002944 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2945 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002946
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002947 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002948
Gleb Natapov776e58e2011-03-13 12:34:27 +02002949 /*
2950 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01002951 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02002952 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01002953 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02002954 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
2955 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02002956
Avi Kivity2fb92db2011-04-27 19:42:18 +03002957 vmx_segment_cache_clear(vmx);
2958
Jan Kiszka4918c6c2013-03-15 08:38:56 +01002959 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002960 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002961 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2962
2963 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002964 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002966 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967
2968 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10002969 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002970 update_exception_bitmap(vcpu);
2971
Gleb Natapovd99e4152012-12-20 16:57:45 +02002972 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2973 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2974 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2975 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2976 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
2977 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002978
Eddie Dong8668a3c2007-10-10 14:26:45 +08002979 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002980}
2981
Amit Shah401d10d2009-02-20 22:53:37 +05302982static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
2983{
2984 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002985 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
2986
2987 if (!msr)
2988 return;
Amit Shah401d10d2009-02-20 22:53:37 +05302989
Avi Kivity44ea2b12009-09-06 15:55:37 +03002990 /*
2991 * Force kernel_gs_base reloading before EFER changes, as control
2992 * of this msr depends on is_long_mode().
2993 */
2994 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02002995 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05302996 if (efer & EFER_LMA) {
2997 vmcs_write32(VM_ENTRY_CONTROLS,
2998 vmcs_read32(VM_ENTRY_CONTROLS) |
2999 VM_ENTRY_IA32E_MODE);
3000 msr->data = efer;
3001 } else {
3002 vmcs_write32(VM_ENTRY_CONTROLS,
3003 vmcs_read32(VM_ENTRY_CONTROLS) &
3004 ~VM_ENTRY_IA32E_MODE);
3005
3006 msr->data = efer & ~EFER_LME;
3007 }
3008 setup_msrs(vmx);
3009}
3010
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003011#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003012
3013static void enter_lmode(struct kvm_vcpu *vcpu)
3014{
3015 u32 guest_tr_ar;
3016
Avi Kivity2fb92db2011-04-27 19:42:18 +03003017 vmx_segment_cache_clear(to_vmx(vcpu));
3018
Avi Kivity6aa8b732006-12-10 02:21:36 -08003019 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
3020 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003021 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3022 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003023 vmcs_write32(GUEST_TR_AR_BYTES,
3024 (guest_tr_ar & ~AR_TYPE_MASK)
3025 | AR_TYPE_BUSY_64_TSS);
3026 }
Avi Kivityda38f432010-07-06 11:30:49 +03003027 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003028}
3029
3030static void exit_lmode(struct kvm_vcpu *vcpu)
3031{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003032 vmcs_write32(VM_ENTRY_CONTROLS,
3033 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03003034 & ~VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003035 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003036}
3037
3038#endif
3039
Sheng Yang2384d2b2008-01-17 15:14:33 +08003040static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3041{
Gui Jianfengb9d762f2010-06-07 10:32:29 +08003042 vpid_sync_context(to_vmx(vcpu));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003043 if (enable_ept) {
3044 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3045 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08003046 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003047 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003048}
3049
Avi Kivitye8467fd2009-12-29 18:43:06 +02003050static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3051{
3052 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3053
3054 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3055 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3056}
3057
Avi Kivityaff48ba2010-12-05 18:56:11 +02003058static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3059{
3060 if (enable_ept && is_paging(vcpu))
3061 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3062 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3063}
3064
Anthony Liguori25c4c272007-04-27 09:29:21 +03003065static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08003066{
Avi Kivityfc78f512009-12-07 12:16:48 +02003067 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3068
3069 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3070 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08003071}
3072
Sheng Yang14394422008-04-28 12:24:45 +08003073static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3074{
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003075 if (!test_bit(VCPU_EXREG_PDPTR,
3076 (unsigned long *)&vcpu->arch.regs_dirty))
3077 return;
3078
Sheng Yang14394422008-04-28 12:24:45 +08003079 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Joerg Roedelff03a072010-09-10 17:30:57 +02003080 vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]);
3081 vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]);
3082 vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]);
3083 vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08003084 }
3085}
3086
Avi Kivity8f5d5492009-05-31 18:41:29 +03003087static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3088{
3089 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Joerg Roedelff03a072010-09-10 17:30:57 +02003090 vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3091 vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3092 vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3093 vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003094 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003095
3096 __set_bit(VCPU_EXREG_PDPTR,
3097 (unsigned long *)&vcpu->arch.regs_avail);
3098 __set_bit(VCPU_EXREG_PDPTR,
3099 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003100}
3101
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003102static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08003103
3104static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3105 unsigned long cr0,
3106 struct kvm_vcpu *vcpu)
3107{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03003108 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3109 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003110 if (!(cr0 & X86_CR0_PG)) {
3111 /* From paging/starting to nonpaging */
3112 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003113 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08003114 (CPU_BASED_CR3_LOAD_EXITING |
3115 CPU_BASED_CR3_STORE_EXITING));
3116 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003117 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003118 } else if (!is_paging(vcpu)) {
3119 /* From nonpaging to paging */
3120 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003121 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08003122 ~(CPU_BASED_CR3_LOAD_EXITING |
3123 CPU_BASED_CR3_STORE_EXITING));
3124 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003125 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003126 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003127
3128 if (!(cr0 & X86_CR0_WP))
3129 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003130}
3131
Avi Kivity6aa8b732006-12-10 02:21:36 -08003132static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3133{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003134 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003135 unsigned long hw_cr0;
3136
Gleb Natapov50378782013-02-04 16:00:28 +02003137 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003138 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02003139 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003140 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003141 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003142
Gleb Natapov218e7632013-01-21 15:36:45 +02003143 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3144 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003145
Gleb Natapov218e7632013-01-21 15:36:45 +02003146 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3147 enter_rmode(vcpu);
3148 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003149
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003150#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003151 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003152 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003153 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003154 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003155 exit_lmode(vcpu);
3156 }
3157#endif
3158
Avi Kivity089d0342009-03-23 18:26:32 +02003159 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003160 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3161
Avi Kivity02daab22009-12-30 12:40:26 +02003162 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02003163 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02003164
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003166 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003167 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02003168
3169 /* depends on vcpu->arch.cr0 to be set to a new value */
3170 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003171}
3172
Sheng Yang14394422008-04-28 12:24:45 +08003173static u64 construct_eptp(unsigned long root_hpa)
3174{
3175 u64 eptp;
3176
3177 /* TODO write the value reading from MSR */
3178 eptp = VMX_EPT_DEFAULT_MT |
3179 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08003180 if (enable_ept_ad_bits)
3181 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003182 eptp |= (root_hpa & PAGE_MASK);
3183
3184 return eptp;
3185}
3186
Avi Kivity6aa8b732006-12-10 02:21:36 -08003187static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3188{
Sheng Yang14394422008-04-28 12:24:45 +08003189 unsigned long guest_cr3;
3190 u64 eptp;
3191
3192 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02003193 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003194 eptp = construct_eptp(cr3);
3195 vmcs_write64(EPT_POINTER, eptp);
Avi Kivity9f8fe502010-12-05 17:30:00 +02003196 guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
Sheng Yangb927a3c2009-07-21 10:42:48 +08003197 vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02003198 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003199 }
3200
Sheng Yang2384d2b2008-01-17 15:14:33 +08003201 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003202 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203}
3204
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003205static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003207 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08003208 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
3209
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003210 if (cr4 & X86_CR4_VMXE) {
3211 /*
3212 * To use VMXON (and later other VMX instructions), a guest
3213 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3214 * So basically the check on whether to allow nested VMX
3215 * is here.
3216 */
3217 if (!nested_vmx_allowed(vcpu))
3218 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01003219 }
3220 if (to_vmx(vcpu)->nested.vmxon &&
3221 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003222 return 1;
3223
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003224 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02003225 if (enable_ept) {
3226 if (!is_paging(vcpu)) {
3227 hw_cr4 &= ~X86_CR4_PAE;
3228 hw_cr4 |= X86_CR4_PSE;
Dongxiao Xuc08800a2013-02-04 11:50:43 +08003229 /*
3230 * SMEP is disabled if CPU is in non-paging mode in
3231 * hardware. However KVM always uses paging mode to
3232 * emulate guest non-paging mode with TDP.
3233 * To emulate this behavior, SMEP needs to be manually
3234 * disabled when guest switches to non-paging mode.
3235 */
3236 hw_cr4 &= ~X86_CR4_SMEP;
Avi Kivitybc230082009-12-08 12:14:42 +02003237 } else if (!(cr4 & X86_CR4_PAE)) {
3238 hw_cr4 &= ~X86_CR4_PAE;
3239 }
3240 }
Sheng Yang14394422008-04-28 12:24:45 +08003241
3242 vmcs_writel(CR4_READ_SHADOW, cr4);
3243 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003244 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003245}
3246
Avi Kivity6aa8b732006-12-10 02:21:36 -08003247static void vmx_get_segment(struct kvm_vcpu *vcpu,
3248 struct kvm_segment *var, int seg)
3249{
Avi Kivitya9179492011-01-03 14:28:52 +02003250 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003251 u32 ar;
3252
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003253 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003254 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02003255 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003256 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003257 return;
Avi Kivity1390a282012-08-21 17:07:08 +03003258 var->base = vmx_read_guest_seg_base(vmx, seg);
3259 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3260 return;
Avi Kivitya9179492011-01-03 14:28:52 +02003261 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003262 var->base = vmx_read_guest_seg_base(vmx, seg);
3263 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3264 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3265 ar = vmx_read_guest_seg_ar(vmx, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003266 var->type = ar & 15;
3267 var->s = (ar >> 4) & 1;
3268 var->dpl = (ar >> 5) & 3;
3269 var->present = (ar >> 7) & 1;
3270 var->avl = (ar >> 12) & 1;
3271 var->l = (ar >> 13) & 1;
3272 var->db = (ar >> 14) & 1;
3273 var->g = (ar >> 15) & 1;
3274 var->unusable = (ar >> 16) & 1;
3275}
3276
Avi Kivitya9179492011-01-03 14:28:52 +02003277static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3278{
Avi Kivitya9179492011-01-03 14:28:52 +02003279 struct kvm_segment s;
3280
3281 if (to_vmx(vcpu)->rmode.vm86_active) {
3282 vmx_get_segment(vcpu, &s, seg);
3283 return s.base;
3284 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003285 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003286}
3287
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003288static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003289{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003290 struct vcpu_vmx *vmx = to_vmx(vcpu);
3291
Avi Kivity3eeb3282010-01-21 15:31:48 +02003292 if (!is_protmode(vcpu))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003293 return 0;
3294
Avi Kivityf4c63e52011-03-07 14:54:28 +02003295 if (!is_long_mode(vcpu)
3296 && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
Izik Eidus2e4d2652008-03-24 19:38:34 +02003297 return 3;
3298
Avi Kivity69c73022011-03-07 15:26:44 +02003299 if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
3300 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003301 vmx->cpl = vmx_read_guest_seg_selector(vmx, VCPU_SREG_CS) & 3;
Avi Kivity69c73022011-03-07 15:26:44 +02003302 }
Avi Kivityd881e6f2012-06-06 18:36:48 +03003303
3304 return vmx->cpl;
Avi Kivity69c73022011-03-07 15:26:44 +02003305}
3306
3307
Avi Kivity653e3102007-05-07 10:55:37 +03003308static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003309{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003310 u32 ar;
3311
Avi Kivityf0495f92012-06-07 17:06:10 +03003312 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003313 ar = 1 << 16;
3314 else {
3315 ar = var->type & 15;
3316 ar |= (var->s & 1) << 4;
3317 ar |= (var->dpl & 3) << 5;
3318 ar |= (var->present & 1) << 7;
3319 ar |= (var->avl & 1) << 12;
3320 ar |= (var->l & 1) << 13;
3321 ar |= (var->db & 1) << 14;
3322 ar |= (var->g & 1) << 15;
3323 }
Avi Kivity653e3102007-05-07 10:55:37 +03003324
3325 return ar;
3326}
3327
3328static void vmx_set_segment(struct kvm_vcpu *vcpu,
3329 struct kvm_segment *var, int seg)
3330{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003331 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02003332 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03003333
Avi Kivity2fb92db2011-04-27 19:42:18 +03003334 vmx_segment_cache_clear(vmx);
Gleb Natapov2f143242013-01-21 15:36:42 +02003335 if (seg == VCPU_SREG_CS)
3336 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity2fb92db2011-04-27 19:42:18 +03003337
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003338 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3339 vmx->rmode.segs[seg] = *var;
3340 if (seg == VCPU_SREG_TR)
3341 vmcs_write16(sf->selector, var->selector);
3342 else if (var->s)
3343 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003344 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03003345 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003346
Avi Kivity653e3102007-05-07 10:55:37 +03003347 vmcs_writel(sf->base, var->base);
3348 vmcs_write32(sf->limit, var->limit);
3349 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003350
3351 /*
3352 * Fix the "Accessed" bit in AR field of segment registers for older
3353 * qemu binaries.
3354 * IA32 arch specifies that at the time of processor reset the
3355 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08003356 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003357 * state vmexit when "unrestricted guest" mode is turned on.
3358 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3359 * tree. Newer qemu binaries with that qemu fix would not need this
3360 * kvm hack.
3361 */
3362 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02003363 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003364
Gleb Natapovf924d662012-12-12 19:10:55 +02003365 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02003366
3367out:
Gleb Natapov14168782013-01-21 15:36:49 +02003368 vmx->emulation_required |= emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003369}
3370
Avi Kivity6aa8b732006-12-10 02:21:36 -08003371static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3372{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003373 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003374
3375 *db = (ar >> 14) & 1;
3376 *l = (ar >> 13) & 1;
3377}
3378
Gleb Natapov89a27f42010-02-16 10:51:48 +02003379static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003380{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003381 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3382 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003383}
3384
Gleb Natapov89a27f42010-02-16 10:51:48 +02003385static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003386{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003387 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3388 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003389}
3390
Gleb Natapov89a27f42010-02-16 10:51:48 +02003391static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003392{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003393 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3394 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003395}
3396
Gleb Natapov89a27f42010-02-16 10:51:48 +02003397static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003398{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003399 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3400 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003401}
3402
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003403static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3404{
3405 struct kvm_segment var;
3406 u32 ar;
3407
3408 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02003409 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02003410 if (seg == VCPU_SREG_CS)
3411 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003412 ar = vmx_segment_access_rights(&var);
3413
3414 if (var.base != (var.selector << 4))
3415 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02003416 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003417 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02003418 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003419 return false;
3420
3421 return true;
3422}
3423
3424static bool code_segment_valid(struct kvm_vcpu *vcpu)
3425{
3426 struct kvm_segment cs;
3427 unsigned int cs_rpl;
3428
3429 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3430 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
3431
Avi Kivity1872a3f2009-01-04 23:26:52 +02003432 if (cs.unusable)
3433 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003434 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
3435 return false;
3436 if (!cs.s)
3437 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003438 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003439 if (cs.dpl > cs_rpl)
3440 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003441 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003442 if (cs.dpl != cs_rpl)
3443 return false;
3444 }
3445 if (!cs.present)
3446 return false;
3447
3448 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3449 return true;
3450}
3451
3452static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3453{
3454 struct kvm_segment ss;
3455 unsigned int ss_rpl;
3456
3457 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3458 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
3459
Avi Kivity1872a3f2009-01-04 23:26:52 +02003460 if (ss.unusable)
3461 return true;
3462 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003463 return false;
3464 if (!ss.s)
3465 return false;
3466 if (ss.dpl != ss_rpl) /* DPL != RPL */
3467 return false;
3468 if (!ss.present)
3469 return false;
3470
3471 return true;
3472}
3473
3474static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3475{
3476 struct kvm_segment var;
3477 unsigned int rpl;
3478
3479 vmx_get_segment(vcpu, &var, seg);
3480 rpl = var.selector & SELECTOR_RPL_MASK;
3481
Avi Kivity1872a3f2009-01-04 23:26:52 +02003482 if (var.unusable)
3483 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003484 if (!var.s)
3485 return false;
3486 if (!var.present)
3487 return false;
3488 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
3489 if (var.dpl < rpl) /* DPL < RPL */
3490 return false;
3491 }
3492
3493 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3494 * rights flags
3495 */
3496 return true;
3497}
3498
3499static bool tr_valid(struct kvm_vcpu *vcpu)
3500{
3501 struct kvm_segment tr;
3502
3503 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3504
Avi Kivity1872a3f2009-01-04 23:26:52 +02003505 if (tr.unusable)
3506 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003507 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3508 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003509 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003510 return false;
3511 if (!tr.present)
3512 return false;
3513
3514 return true;
3515}
3516
3517static bool ldtr_valid(struct kvm_vcpu *vcpu)
3518{
3519 struct kvm_segment ldtr;
3520
3521 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3522
Avi Kivity1872a3f2009-01-04 23:26:52 +02003523 if (ldtr.unusable)
3524 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003525 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3526 return false;
3527 if (ldtr.type != 2)
3528 return false;
3529 if (!ldtr.present)
3530 return false;
3531
3532 return true;
3533}
3534
3535static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3536{
3537 struct kvm_segment cs, ss;
3538
3539 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3540 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3541
3542 return ((cs.selector & SELECTOR_RPL_MASK) ==
3543 (ss.selector & SELECTOR_RPL_MASK));
3544}
3545
3546/*
3547 * Check if guest state is valid. Returns true if valid, false if
3548 * not.
3549 * We assume that registers are always usable
3550 */
3551static bool guest_state_valid(struct kvm_vcpu *vcpu)
3552{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02003553 if (enable_unrestricted_guest)
3554 return true;
3555
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003556 /* real mode guest state checks */
Avi Kivity3eeb3282010-01-21 15:31:48 +02003557 if (!is_protmode(vcpu)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003558 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3559 return false;
3560 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3561 return false;
3562 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3563 return false;
3564 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3565 return false;
3566 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3567 return false;
3568 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3569 return false;
3570 } else {
3571 /* protected mode guest state checks */
3572 if (!cs_ss_rpl_check(vcpu))
3573 return false;
3574 if (!code_segment_valid(vcpu))
3575 return false;
3576 if (!stack_segment_valid(vcpu))
3577 return false;
3578 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3579 return false;
3580 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3581 return false;
3582 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3583 return false;
3584 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3585 return false;
3586 if (!tr_valid(vcpu))
3587 return false;
3588 if (!ldtr_valid(vcpu))
3589 return false;
3590 }
3591 /* TODO:
3592 * - Add checks on RIP
3593 * - Add checks on RFLAGS
3594 */
3595
3596 return true;
3597}
3598
Mike Dayd77c26f2007-10-08 09:02:08 -04003599static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003600{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003601 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02003602 u16 data = 0;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003603 int r, idx, ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003604
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003605 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003606 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02003607 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3608 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003609 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003610 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08003611 r = kvm_write_guest_page(kvm, fn++, &data,
3612 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02003613 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003614 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003615 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
3616 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003617 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003618 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3619 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003620 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003621 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003622 r = kvm_write_guest_page(kvm, fn, &data,
3623 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3624 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02003625 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003626 goto out;
3627
3628 ret = 1;
3629out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003630 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003631 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003632}
3633
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003634static int init_rmode_identity_map(struct kvm *kvm)
3635{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003636 int i, idx, r, ret;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003637 pfn_t identity_map_pfn;
3638 u32 tmp;
3639
Avi Kivity089d0342009-03-23 18:26:32 +02003640 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003641 return 1;
3642 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
3643 printk(KERN_ERR "EPT: identity-mapping pagetable "
3644 "haven't been allocated!\n");
3645 return 0;
3646 }
3647 if (likely(kvm->arch.ept_identity_pagetable_done))
3648 return 1;
3649 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003650 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003651 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003652 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3653 if (r < 0)
3654 goto out;
3655 /* Set up identity-mapping pagetable for EPT in real mode */
3656 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3657 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3658 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3659 r = kvm_write_guest_page(kvm, identity_map_pfn,
3660 &tmp, i * sizeof(tmp), sizeof(tmp));
3661 if (r < 0)
3662 goto out;
3663 }
3664 kvm->arch.ept_identity_pagetable_done = true;
3665 ret = 1;
3666out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003667 srcu_read_unlock(&kvm->srcu, idx);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003668 return ret;
3669}
3670
Avi Kivity6aa8b732006-12-10 02:21:36 -08003671static void seg_setup(int seg)
3672{
Mathias Krause772e0312012-08-30 01:30:19 +02003673 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003674 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003675
3676 vmcs_write16(sf->selector, 0);
3677 vmcs_writel(sf->base, 0);
3678 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02003679 ar = 0x93;
3680 if (seg == VCPU_SREG_CS)
3681 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003682
3683 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003684}
3685
Sheng Yangf78e0e22007-10-29 09:40:42 +08003686static int alloc_apic_access_page(struct kvm *kvm)
3687{
Xiao Guangrong44841412012-09-07 14:14:20 +08003688 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003689 struct kvm_userspace_memory_region kvm_userspace_mem;
3690 int r = 0;
3691
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003692 mutex_lock(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003693 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003694 goto out;
3695 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
3696 kvm_userspace_mem.flags = 0;
3697 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
3698 kvm_userspace_mem.memory_size = PAGE_SIZE;
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09003699 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003700 if (r)
3701 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003702
Xiao Guangrong44841412012-09-07 14:14:20 +08003703 page = gfn_to_page(kvm, 0xfee00);
3704 if (is_error_page(page)) {
3705 r = -EFAULT;
3706 goto out;
3707 }
3708
3709 kvm->arch.apic_access_page = page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003710out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003711 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003712 return r;
3713}
3714
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003715static int alloc_identity_pagetable(struct kvm *kvm)
3716{
Xiao Guangrong44841412012-09-07 14:14:20 +08003717 struct page *page;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003718 struct kvm_userspace_memory_region kvm_userspace_mem;
3719 int r = 0;
3720
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003721 mutex_lock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003722 if (kvm->arch.ept_identity_pagetable)
3723 goto out;
3724 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
3725 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003726 kvm_userspace_mem.guest_phys_addr =
3727 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003728 kvm_userspace_mem.memory_size = PAGE_SIZE;
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09003729 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003730 if (r)
3731 goto out;
3732
Xiao Guangrong44841412012-09-07 14:14:20 +08003733 page = gfn_to_page(kvm, kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
3734 if (is_error_page(page)) {
3735 r = -EFAULT;
3736 goto out;
3737 }
3738
3739 kvm->arch.ept_identity_pagetable = page;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003740out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003741 mutex_unlock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003742 return r;
3743}
3744
Sheng Yang2384d2b2008-01-17 15:14:33 +08003745static void allocate_vpid(struct vcpu_vmx *vmx)
3746{
3747 int vpid;
3748
3749 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02003750 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003751 return;
3752 spin_lock(&vmx_vpid_lock);
3753 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
3754 if (vpid < VMX_NR_VPIDS) {
3755 vmx->vpid = vpid;
3756 __set_bit(vpid, vmx_vpid_bitmap);
3757 }
3758 spin_unlock(&vmx_vpid_lock);
3759}
3760
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003761static void free_vpid(struct vcpu_vmx *vmx)
3762{
3763 if (!enable_vpid)
3764 return;
3765 spin_lock(&vmx_vpid_lock);
3766 if (vmx->vpid != 0)
3767 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3768 spin_unlock(&vmx_vpid_lock);
3769}
3770
Yang Zhang8d146952013-01-25 10:18:50 +08003771#define MSR_TYPE_R 1
3772#define MSR_TYPE_W 2
3773static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
3774 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08003775{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003776 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08003777
3778 if (!cpu_has_vmx_msr_bitmap())
3779 return;
3780
3781 /*
3782 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3783 * have the write-low and read-high bitmap offsets the wrong way round.
3784 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3785 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003786 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08003787 if (type & MSR_TYPE_R)
3788 /* read-low */
3789 __clear_bit(msr, msr_bitmap + 0x000 / f);
3790
3791 if (type & MSR_TYPE_W)
3792 /* write-low */
3793 __clear_bit(msr, msr_bitmap + 0x800 / f);
3794
Sheng Yang25c5f222008-03-28 13:18:56 +08003795 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3796 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08003797 if (type & MSR_TYPE_R)
3798 /* read-high */
3799 __clear_bit(msr, msr_bitmap + 0x400 / f);
3800
3801 if (type & MSR_TYPE_W)
3802 /* write-high */
3803 __clear_bit(msr, msr_bitmap + 0xc00 / f);
3804
3805 }
3806}
3807
3808static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
3809 u32 msr, int type)
3810{
3811 int f = sizeof(unsigned long);
3812
3813 if (!cpu_has_vmx_msr_bitmap())
3814 return;
3815
3816 /*
3817 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3818 * have the write-low and read-high bitmap offsets the wrong way round.
3819 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3820 */
3821 if (msr <= 0x1fff) {
3822 if (type & MSR_TYPE_R)
3823 /* read-low */
3824 __set_bit(msr, msr_bitmap + 0x000 / f);
3825
3826 if (type & MSR_TYPE_W)
3827 /* write-low */
3828 __set_bit(msr, msr_bitmap + 0x800 / f);
3829
3830 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3831 msr &= 0x1fff;
3832 if (type & MSR_TYPE_R)
3833 /* read-high */
3834 __set_bit(msr, msr_bitmap + 0x400 / f);
3835
3836 if (type & MSR_TYPE_W)
3837 /* write-high */
3838 __set_bit(msr, msr_bitmap + 0xc00 / f);
3839
Sheng Yang25c5f222008-03-28 13:18:56 +08003840 }
Sheng Yang25c5f222008-03-28 13:18:56 +08003841}
3842
Avi Kivity58972972009-02-24 22:26:47 +02003843static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
3844{
3845 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08003846 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
3847 msr, MSR_TYPE_R | MSR_TYPE_W);
3848 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
3849 msr, MSR_TYPE_R | MSR_TYPE_W);
3850}
3851
3852static void vmx_enable_intercept_msr_read_x2apic(u32 msr)
3853{
3854 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
3855 msr, MSR_TYPE_R);
3856 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
3857 msr, MSR_TYPE_R);
3858}
3859
3860static void vmx_disable_intercept_msr_read_x2apic(u32 msr)
3861{
3862 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
3863 msr, MSR_TYPE_R);
3864 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
3865 msr, MSR_TYPE_R);
3866}
3867
3868static void vmx_disable_intercept_msr_write_x2apic(u32 msr)
3869{
3870 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
3871 msr, MSR_TYPE_W);
3872 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
3873 msr, MSR_TYPE_W);
Avi Kivity58972972009-02-24 22:26:47 +02003874}
3875
Avi Kivity6aa8b732006-12-10 02:21:36 -08003876/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003877 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
3878 * will not change in the lifetime of the guest.
3879 * Note that host-state that does change is set elsewhere. E.g., host-state
3880 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
3881 */
3882static void vmx_set_constant_host_state(void)
3883{
3884 u32 low32, high32;
3885 unsigned long tmpl;
3886 struct desc_ptr dt;
3887
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07003888 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003889 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
3890 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
3891
3892 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03003893#ifdef CONFIG_X86_64
3894 /*
3895 * Load null selectors, so we can avoid reloading them in
3896 * __vmx_load_host_state(), in case userspace uses the null selectors
3897 * too (the expected case).
3898 */
3899 vmcs_write16(HOST_DS_SELECTOR, 0);
3900 vmcs_write16(HOST_ES_SELECTOR, 0);
3901#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003902 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3903 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03003904#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003905 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3906 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
3907
3908 native_store_idt(&dt);
3909 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
3910
Avi Kivity83287ea422012-09-16 15:10:57 +03003911 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003912
3913 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
3914 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
3915 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
3916 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
3917
3918 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
3919 rdmsr(MSR_IA32_CR_PAT, low32, high32);
3920 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
3921 }
3922}
3923
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003924static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
3925{
3926 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
3927 if (enable_ept)
3928 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003929 if (is_guest_mode(&vmx->vcpu))
3930 vmx->vcpu.arch.cr4_guest_owned_bits &=
3931 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003932 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
3933}
3934
3935static u32 vmx_exec_control(struct vcpu_vmx *vmx)
3936{
3937 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
3938 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
3939 exec_control &= ~CPU_BASED_TPR_SHADOW;
3940#ifdef CONFIG_X86_64
3941 exec_control |= CPU_BASED_CR8_STORE_EXITING |
3942 CPU_BASED_CR8_LOAD_EXITING;
3943#endif
3944 }
3945 if (!enable_ept)
3946 exec_control |= CPU_BASED_CR3_STORE_EXITING |
3947 CPU_BASED_CR3_LOAD_EXITING |
3948 CPU_BASED_INVLPG_EXITING;
3949 return exec_control;
3950}
3951
Yang Zhangc7c9c562013-01-25 10:18:51 +08003952static int vmx_vm_has_apicv(struct kvm *kvm)
3953{
3954 return enable_apicv_reg_vid && irqchip_in_kernel(kvm);
3955}
3956
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003957static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
3958{
3959 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
3960 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
3961 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3962 if (vmx->vpid == 0)
3963 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
3964 if (!enable_ept) {
3965 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
3966 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00003967 /* Enable INVPCID for non-ept guests may cause performance regression. */
3968 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003969 }
3970 if (!enable_unrestricted_guest)
3971 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
3972 if (!ple_gap)
3973 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Yang Zhangc7c9c562013-01-25 10:18:51 +08003974 if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
3975 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
3976 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08003977 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003978 return exec_control;
3979}
3980
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003981static void ept_set_mmio_spte_mask(void)
3982{
3983 /*
3984 * EPT Misconfigurations can be generated if the value of bits 2:0
3985 * of an EPT paging-structure entry is 110b (write/execute).
3986 * Also, magic bits (0xffull << 49) is set to quickly identify mmio
3987 * spte.
3988 */
3989 kvm_mmu_set_mmio_spte_mask(0xffull << 49 | 0x6ull);
3990}
3991
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003992/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003993 * Sets up the vmcs for emulated real mode.
3994 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003995static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003996{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02003997#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003998 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02003999#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004000 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004001
Avi Kivity6aa8b732006-12-10 02:21:36 -08004002 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004003 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
4004 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004005
Sheng Yang25c5f222008-03-28 13:18:56 +08004006 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02004007 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08004008
Avi Kivity6aa8b732006-12-10 02:21:36 -08004009 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4010
Avi Kivity6aa8b732006-12-10 02:21:36 -08004011 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004012 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
4013 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004014
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004015 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004016
Sheng Yang83ff3b92007-11-21 14:33:25 +08004017 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004018 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4019 vmx_secondary_exec_control(vmx));
Sheng Yang83ff3b92007-11-21 14:33:25 +08004020 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08004021
Yang Zhangc7c9c562013-01-25 10:18:51 +08004022 if (enable_apicv_reg_vid) {
4023 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4024 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4025 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4026 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4027
4028 vmcs_write16(GUEST_INTR_STATUS, 0);
4029 }
4030
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004031 if (ple_gap) {
4032 vmcs_write32(PLE_GAP, ple_gap);
4033 vmcs_write32(PLE_WINDOW, ple_window);
4034 }
4035
Xiao Guangrongc3707952011-07-12 03:28:04 +08004036 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4037 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004038 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4039
Avi Kivity9581d442010-10-19 16:46:55 +02004040 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4041 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004042 vmx_set_constant_host_state();
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004043#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004044 rdmsrl(MSR_FS_BASE, a);
4045 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
4046 rdmsrl(MSR_GS_BASE, a);
4047 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
4048#else
4049 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4050 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
4051#endif
4052
Eddie Dong2cc51562007-05-21 07:28:09 +03004053 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4054 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004055 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03004056 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004057 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004058
Sheng Yang468d4722008-10-09 16:01:55 +08004059 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004060 u32 msr_low, msr_high;
4061 u64 host_pat;
Sheng Yang468d4722008-10-09 16:01:55 +08004062 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
4063 host_pat = msr_low | ((u64) msr_high << 32);
4064 /* Write the default value follow host pat */
4065 vmcs_write64(GUEST_IA32_PAT, host_pat);
4066 /* Keep arch.pat sync with GUEST_IA32_PAT */
4067 vmx->vcpu.arch.pat = host_pat;
4068 }
4069
Avi Kivity6aa8b732006-12-10 02:21:36 -08004070 for (i = 0; i < NR_VMX_MSR; ++i) {
4071 u32 index = vmx_msr_index[i];
4072 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004073 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004074
4075 if (rdmsr_safe(index, &data_low, &data_high) < 0)
4076 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08004077 if (wrmsr_safe(index, data_low, data_high) < 0)
4078 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03004079 vmx->guest_msrs[j].index = i;
4080 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02004081 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004082 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004083 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004084
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004085 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004086
4087 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004088 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
4089
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004090 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004091 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004092
4093 return 0;
4094}
4095
Jan Kiszka57f252f2013-03-12 10:20:24 +01004096static void vmx_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004097{
4098 struct vcpu_vmx *vmx = to_vmx(vcpu);
4099 u64 msr;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004100
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004101 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004102
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004103 vmx->soft_vnmi_blocked = 0;
4104
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004105 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004106 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004107 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03004108 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004109 msr |= MSR_IA32_APICBASE_BSP;
4110 kvm_set_apic_base(&vmx->vcpu, msr);
4111
Avi Kivity2fb92db2011-04-27 19:42:18 +03004112 vmx_segment_cache_clear(vmx);
4113
Avi Kivity5706be02008-08-20 15:07:31 +03004114 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01004115 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004116
4117 seg_setup(VCPU_SREG_DS);
4118 seg_setup(VCPU_SREG_ES);
4119 seg_setup(VCPU_SREG_FS);
4120 seg_setup(VCPU_SREG_GS);
4121 seg_setup(VCPU_SREG_SS);
4122
4123 vmcs_write16(GUEST_TR_SELECTOR, 0);
4124 vmcs_writel(GUEST_TR_BASE, 0);
4125 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4126 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4127
4128 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4129 vmcs_writel(GUEST_LDTR_BASE, 0);
4130 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4131 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4132
4133 vmcs_write32(GUEST_SYSENTER_CS, 0);
4134 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4135 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4136
4137 vmcs_writel(GUEST_RFLAGS, 0x02);
Jan Kiszka66450a22013-03-13 12:42:34 +01004138 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004139
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004140 vmcs_writel(GUEST_GDTR_BASE, 0);
4141 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4142
4143 vmcs_writel(GUEST_IDTR_BASE, 0);
4144 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4145
Anthony Liguori443381a2010-12-06 10:53:38 -06004146 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004147 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
4148 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
4149
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004150 /* Special registers */
4151 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4152
4153 setup_msrs(vmx);
4154
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4156
Sheng Yangf78e0e22007-10-29 09:40:42 +08004157 if (cpu_has_vmx_tpr_shadow()) {
4158 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
4159 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
4160 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09004161 __pa(vmx->vcpu.arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004162 vmcs_write32(TPR_THRESHOLD, 0);
4163 }
4164
4165 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
4166 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08004167 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004168
Sheng Yang2384d2b2008-01-17 15:14:33 +08004169 if (vmx->vpid != 0)
4170 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4171
Eduardo Habkostfa400522009-10-24 02:49:58 -02004172 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Avi Kivity4d4ec082009-12-29 18:07:30 +02004173 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10004174 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10004175 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10004176 vmx_fpu_activate(&vmx->vcpu);
4177 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004178
Gui Jianfengb9d762f2010-06-07 10:32:29 +08004179 vpid_sync_context(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004180}
4181
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004182/*
4183 * In nested virtualization, check if L1 asked to exit on external interrupts.
4184 * For most existing hypervisors, this will always return true.
4185 */
4186static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
4187{
4188 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4189 PIN_BASED_EXT_INTR_MASK;
4190}
4191
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004192static void enable_irq_window(struct kvm_vcpu *vcpu)
4193{
4194 u32 cpu_based_vm_exec_control;
Nadav Har'Eld6185f22011-09-22 13:52:56 +03004195 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) {
4196 /*
4197 * We get here if vmx_interrupt_allowed() said we can't
4198 * inject to L1 now because L2 must run. Ask L2 to exit
4199 * right after entry, so we can inject to L1 more promptly.
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004200 */
Nadav Har'Eld6185f22011-09-22 13:52:56 +03004201 kvm_make_request(KVM_REQ_IMMEDIATE_EXIT, vcpu);
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004202 return;
Nadav Har'Eld6185f22011-09-22 13:52:56 +03004203 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004204
4205 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4206 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
4207 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4208}
4209
4210static void enable_nmi_window(struct kvm_vcpu *vcpu)
4211{
4212 u32 cpu_based_vm_exec_control;
4213
4214 if (!cpu_has_virtual_nmis()) {
4215 enable_irq_window(vcpu);
4216 return;
4217 }
4218
Avi Kivity30bd0c42010-11-01 23:20:48 +02004219 if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
4220 enable_irq_window(vcpu);
4221 return;
4222 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004223 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4224 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
4225 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4226}
4227
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004228static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004229{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004230 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004231 uint32_t intr;
4232 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004233
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004234 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004235
Avi Kivityfa89a812008-09-01 15:57:51 +03004236 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004237 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004238 int inc_eip = 0;
4239 if (vcpu->arch.interrupt.soft)
4240 inc_eip = vcpu->arch.event_exit_inst_len;
4241 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004242 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004243 return;
4244 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004245 intr = irq | INTR_INFO_VALID_MASK;
4246 if (vcpu->arch.interrupt.soft) {
4247 intr |= INTR_TYPE_SOFT_INTR;
4248 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4249 vmx->vcpu.arch.event_exit_inst_len);
4250 } else
4251 intr |= INTR_TYPE_EXT_INTR;
4252 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03004253}
4254
Sheng Yangf08864b2008-05-15 18:23:25 +08004255static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4256{
Jan Kiszka66a5a342008-09-26 09:30:51 +02004257 struct vcpu_vmx *vmx = to_vmx(vcpu);
4258
Nadav Har'El0b6ac342011-05-25 23:13:36 +03004259 if (is_guest_mode(vcpu))
4260 return;
4261
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004262 if (!cpu_has_virtual_nmis()) {
4263 /*
4264 * Tracking the NMI-blocked state in software is built upon
4265 * finding the next open IRQ window. This, in turn, depends on
4266 * well-behaving guests: They have to keep IRQs disabled at
4267 * least as long as the NMI handler runs. Otherwise we may
4268 * cause NMI nesting, maybe breaking the guest. But as this is
4269 * highly unlikely, we can live with the residual risk.
4270 */
4271 vmx->soft_vnmi_blocked = 1;
4272 vmx->vnmi_blocked_time = 0;
4273 }
4274
Jan Kiszka487b3912008-09-26 09:30:56 +02004275 ++vcpu->stat.nmi_injections;
Avi Kivity9d58b932011-03-07 16:52:07 +02004276 vmx->nmi_known_unmasked = false;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004277 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004278 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004279 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02004280 return;
4281 }
Sheng Yangf08864b2008-05-15 18:23:25 +08004282 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4283 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08004284}
4285
Gleb Natapovc4282df2009-04-21 17:45:07 +03004286static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02004287{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004288 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03004289 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02004290
Gleb Natapovc4282df2009-04-21 17:45:07 +03004291 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
Avi Kivity30bd0c42010-11-01 23:20:48 +02004292 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
4293 | GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02004294}
4295
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004296static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
4297{
4298 if (!cpu_has_virtual_nmis())
4299 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02004300 if (to_vmx(vcpu)->nmi_known_unmasked)
4301 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03004302 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004303}
4304
4305static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
4306{
4307 struct vcpu_vmx *vmx = to_vmx(vcpu);
4308
4309 if (!cpu_has_virtual_nmis()) {
4310 if (vmx->soft_vnmi_blocked != masked) {
4311 vmx->soft_vnmi_blocked = masked;
4312 vmx->vnmi_blocked_time = 0;
4313 }
4314 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02004315 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004316 if (masked)
4317 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4318 GUEST_INTR_STATE_NMI);
4319 else
4320 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4321 GUEST_INTR_STATE_NMI);
4322 }
4323}
4324
Gleb Natapov78646122009-03-23 12:12:11 +02004325static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
4326{
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004327 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) {
Nadav Har'El51cfe382011-09-22 13:53:26 +03004328 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4329 if (to_vmx(vcpu)->nested.nested_run_pending ||
4330 (vmcs12->idt_vectoring_info_field &
4331 VECTORING_INFO_VALID_MASK))
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004332 return 0;
4333 nested_vmx_vmexit(vcpu);
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004334 vmcs12->vm_exit_reason = EXIT_REASON_EXTERNAL_INTERRUPT;
4335 vmcs12->vm_exit_intr_info = 0;
4336 /* fall through to normal code, but now in L1, not L2 */
4337 }
4338
Gleb Natapovc4282df2009-04-21 17:45:07 +03004339 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
4340 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4341 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02004342}
4343
Izik Eiduscbc94022007-10-25 00:29:55 +02004344static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4345{
4346 int ret;
4347 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08004348 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02004349 .guest_phys_addr = addr,
4350 .memory_size = PAGE_SIZE * 3,
4351 .flags = 0,
4352 };
4353
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09004354 ret = kvm_set_memory_region(kvm, &tss_mem);
Izik Eiduscbc94022007-10-25 00:29:55 +02004355 if (ret)
4356 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08004357 kvm->arch.tss_addr = addr;
Gleb Natapov93ea5382011-02-21 12:07:59 +02004358 if (!init_rmode_tss(kvm))
4359 return -ENOMEM;
4360
Izik Eiduscbc94022007-10-25 00:29:55 +02004361 return 0;
4362}
4363
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004364static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004365{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004366 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004367 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01004368 /*
4369 * Update instruction length as we may reinject the exception
4370 * from user space while in guest debugging mode.
4371 */
4372 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4373 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004374 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004375 return false;
4376 /* fall through */
4377 case DB_VECTOR:
4378 if (vcpu->guest_debug &
4379 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
4380 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004381 /* fall through */
4382 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004383 case OF_VECTOR:
4384 case BR_VECTOR:
4385 case UD_VECTOR:
4386 case DF_VECTOR:
4387 case SS_VECTOR:
4388 case GP_VECTOR:
4389 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004390 return true;
4391 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004392 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004393 return false;
4394}
4395
4396static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4397 int vec, u32 err_code)
4398{
4399 /*
4400 * Instruction with address size override prefix opcode 0x67
4401 * Cause the #SS fault with 0 error code in VM86 mode.
4402 */
4403 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
4404 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
4405 if (vcpu->arch.halt_request) {
4406 vcpu->arch.halt_request = 0;
4407 return kvm_emulate_halt(vcpu);
4408 }
4409 return 1;
4410 }
4411 return 0;
4412 }
4413
4414 /*
4415 * Forward all other exceptions that are valid in real mode.
4416 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4417 * the required debugging infrastructure rework.
4418 */
4419 kvm_queue_exception(vcpu, vec);
4420 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004421}
4422
Andi Kleena0861c02009-06-08 17:37:09 +08004423/*
4424 * Trigger machine check on the host. We assume all the MSRs are already set up
4425 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4426 * We pass a fake environment to the machine check handler because we want
4427 * the guest to be always treated like user space, no matter what context
4428 * it used internally.
4429 */
4430static void kvm_machine_check(void)
4431{
4432#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
4433 struct pt_regs regs = {
4434 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4435 .flags = X86_EFLAGS_IF,
4436 };
4437
4438 do_machine_check(&regs, 0);
4439#endif
4440}
4441
Avi Kivity851ba692009-08-24 11:10:17 +03004442static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004443{
4444 /* already handled by vcpu_run */
4445 return 1;
4446}
4447
Avi Kivity851ba692009-08-24 11:10:17 +03004448static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004449{
Avi Kivity1155f762007-11-22 11:30:47 +02004450 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004451 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004452 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004453 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004454 u32 vect_info;
4455 enum emulation_result er;
4456
Avi Kivity1155f762007-11-22 11:30:47 +02004457 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02004458 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004459
Andi Kleena0861c02009-06-08 17:37:09 +08004460 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03004461 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08004462
Jan Kiszkae4a41882008-09-26 09:30:46 +02004463 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02004464 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004465
4466 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03004467 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004468 return 1;
4469 }
4470
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004471 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01004472 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004473 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02004474 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004475 return 1;
4476 }
4477
Avi Kivity6aa8b732006-12-10 02:21:36 -08004478 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004479 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004480 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004481
4482 /*
4483 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4484 * MMIO, it is better to report an internal error.
4485 * See the comments in vmx_handle_exit.
4486 */
4487 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4488 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4489 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4490 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
4491 vcpu->run->internal.ndata = 2;
4492 vcpu->run->internal.data[0] = vect_info;
4493 vcpu->run->internal.data[1] = intr_info;
4494 return 0;
4495 }
4496
Avi Kivity6aa8b732006-12-10 02:21:36 -08004497 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08004498 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02004499 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004500 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004501 trace_kvm_page_fault(cr2, error_code);
4502
Gleb Natapov3298b752009-05-11 13:35:46 +03004503 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03004504 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01004505 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004506 }
4507
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004508 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004509
4510 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4511 return handle_rmode_exception(vcpu, ex_no, error_code);
4512
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004513 switch (ex_no) {
4514 case DB_VECTOR:
4515 dr6 = vmcs_readl(EXIT_QUALIFICATION);
4516 if (!(vcpu->guest_debug &
4517 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
4518 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
4519 kvm_queue_exception(vcpu, DB_VECTOR);
4520 return 1;
4521 }
4522 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
4523 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
4524 /* fall through */
4525 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01004526 /*
4527 * Update instruction length as we may reinject #BP from
4528 * user space while in guest debugging mode. Reading it for
4529 * #DB as well causes no harm, it is not used in that case.
4530 */
4531 vmx->vcpu.arch.event_exit_inst_len =
4532 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004533 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004534 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004535 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4536 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004537 break;
4538 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004539 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4540 kvm_run->ex.exception = ex_no;
4541 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004542 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004543 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004544 return 0;
4545}
4546
Avi Kivity851ba692009-08-24 11:10:17 +03004547static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004548{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004549 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004550 return 1;
4551}
4552
Avi Kivity851ba692009-08-24 11:10:17 +03004553static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004554{
Avi Kivity851ba692009-08-24 11:10:17 +03004555 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08004556 return 0;
4557}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004558
Avi Kivity851ba692009-08-24 11:10:17 +03004559static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004560{
He, Qingbfdaab02007-09-12 14:18:28 +08004561 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01004562 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004563 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004564
He, Qingbfdaab02007-09-12 14:18:28 +08004565 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02004566 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004567 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004568
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004569 ++vcpu->stat.io_exits;
4570
4571 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01004572 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004573
4574 port = exit_qualification >> 16;
4575 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01004576 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004577
4578 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004579}
4580
Ingo Molnar102d8322007-02-19 14:37:47 +02004581static void
4582vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4583{
4584 /*
4585 * Patch in the VMCALL instruction:
4586 */
4587 hypercall[0] = 0x0f;
4588 hypercall[1] = 0x01;
4589 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02004590}
4591
Guo Chao0fa06072012-06-28 15:16:19 +08004592/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004593static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4594{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004595 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004596 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4597 unsigned long orig_val = val;
4598
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004599 /*
4600 * We get here when L2 changed cr0 in a way that did not change
4601 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004602 * but did change L0 shadowed bits. So we first calculate the
4603 * effective cr0 value that L1 would like to write into the
4604 * hardware. It consists of the L2-owned bits from the new
4605 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004606 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004607 val = (val & ~vmcs12->cr0_guest_host_mask) |
4608 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
4609
4610 /* TODO: will have to take unrestricted guest mode into
4611 * account */
4612 if ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON)
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004613 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004614
4615 if (kvm_set_cr0(vcpu, val))
4616 return 1;
4617 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004618 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004619 } else {
4620 if (to_vmx(vcpu)->nested.vmxon &&
4621 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
4622 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004623 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004624 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004625}
4626
4627static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
4628{
4629 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004630 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4631 unsigned long orig_val = val;
4632
4633 /* analogously to handle_set_cr0 */
4634 val = (val & ~vmcs12->cr4_guest_host_mask) |
4635 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
4636 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004637 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004638 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004639 return 0;
4640 } else
4641 return kvm_set_cr4(vcpu, val);
4642}
4643
4644/* called to set cr0 as approriate for clts instruction exit. */
4645static void handle_clts(struct kvm_vcpu *vcpu)
4646{
4647 if (is_guest_mode(vcpu)) {
4648 /*
4649 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
4650 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
4651 * just pretend it's off (also in arch.cr0 for fpu_activate).
4652 */
4653 vmcs_writel(CR0_READ_SHADOW,
4654 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
4655 vcpu->arch.cr0 &= ~X86_CR0_TS;
4656 } else
4657 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
4658}
4659
Avi Kivity851ba692009-08-24 11:10:17 +03004660static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004661{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004662 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004663 int cr;
4664 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03004665 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004666
He, Qingbfdaab02007-09-12 14:18:28 +08004667 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004668 cr = exit_qualification & 15;
4669 reg = (exit_qualification >> 8) & 15;
4670 switch ((exit_qualification >> 4) & 3) {
4671 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004672 val = kvm_register_read(vcpu, reg);
4673 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004674 switch (cr) {
4675 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004676 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01004677 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004678 return 1;
4679 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03004680 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01004681 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004682 return 1;
4683 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004684 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01004685 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004686 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004687 case 8: {
4688 u8 cr8_prev = kvm_get_cr8(vcpu);
4689 u8 cr8 = kvm_register_read(vcpu, reg);
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004690 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01004691 kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004692 if (irqchip_in_kernel(vcpu->kvm))
4693 return 1;
4694 if (cr8_prev <= cr8)
4695 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03004696 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004697 return 0;
4698 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02004699 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004700 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03004701 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004702 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02004703 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03004704 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02004705 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03004706 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004707 case 1: /*mov from cr*/
4708 switch (cr) {
4709 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02004710 val = kvm_read_cr3(vcpu);
4711 kvm_register_write(vcpu, reg, val);
4712 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004713 skip_emulated_instruction(vcpu);
4714 return 1;
4715 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004716 val = kvm_get_cr8(vcpu);
4717 kvm_register_write(vcpu, reg, val);
4718 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004719 skip_emulated_instruction(vcpu);
4720 return 1;
4721 }
4722 break;
4723 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02004724 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02004725 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02004726 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004727
4728 skip_emulated_instruction(vcpu);
4729 return 1;
4730 default:
4731 break;
4732 }
Avi Kivity851ba692009-08-24 11:10:17 +03004733 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03004734 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08004735 (int)(exit_qualification >> 4) & 3, cr);
4736 return 0;
4737}
4738
Avi Kivity851ba692009-08-24 11:10:17 +03004739static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004740{
He, Qingbfdaab02007-09-12 14:18:28 +08004741 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004742 int dr, reg;
4743
Jan Kiszkaf2483412010-01-20 18:20:20 +01004744 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03004745 if (!kvm_require_cpl(vcpu, 0))
4746 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004747 dr = vmcs_readl(GUEST_DR7);
4748 if (dr & DR7_GD) {
4749 /*
4750 * As the vm-exit takes precedence over the debug trap, we
4751 * need to emulate the latter, either for the host or the
4752 * guest debugging itself.
4753 */
4754 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03004755 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
4756 vcpu->run->debug.arch.dr7 = dr;
4757 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004758 vmcs_readl(GUEST_CS_BASE) +
4759 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03004760 vcpu->run->debug.arch.exception = DB_VECTOR;
4761 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004762 return 0;
4763 } else {
4764 vcpu->arch.dr7 &= ~DR7_GD;
4765 vcpu->arch.dr6 |= DR6_BD;
4766 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
4767 kvm_queue_exception(vcpu, DB_VECTOR);
4768 return 1;
4769 }
4770 }
4771
He, Qingbfdaab02007-09-12 14:18:28 +08004772 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004773 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
4774 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
4775 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03004776 unsigned long val;
4777 if (!kvm_get_dr(vcpu, dr, &val))
4778 kvm_register_write(vcpu, reg, val);
4779 } else
4780 kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004781 skip_emulated_instruction(vcpu);
4782 return 1;
4783}
4784
Gleb Natapov020df072010-04-13 10:05:23 +03004785static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
4786{
4787 vmcs_writel(GUEST_DR7, val);
4788}
4789
Avi Kivity851ba692009-08-24 11:10:17 +03004790static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004791{
Avi Kivity06465c52007-02-28 20:46:53 +02004792 kvm_emulate_cpuid(vcpu);
4793 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004794}
4795
Avi Kivity851ba692009-08-24 11:10:17 +03004796static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004797{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004798 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004799 u64 data;
4800
4801 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivity59200272010-01-25 19:47:02 +02004802 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004803 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004804 return 1;
4805 }
4806
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004807 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004808
Avi Kivity6aa8b732006-12-10 02:21:36 -08004809 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004810 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
4811 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004812 skip_emulated_instruction(vcpu);
4813 return 1;
4814}
4815
Avi Kivity851ba692009-08-24 11:10:17 +03004816static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004817{
Will Auld8fe8ab42012-11-29 12:42:12 -08004818 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004819 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
4820 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
4821 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004822
Will Auld8fe8ab42012-11-29 12:42:12 -08004823 msr.data = data;
4824 msr.index = ecx;
4825 msr.host_initiated = false;
4826 if (vmx_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02004827 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004828 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004829 return 1;
4830 }
4831
Avi Kivity59200272010-01-25 19:47:02 +02004832 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004833 skip_emulated_instruction(vcpu);
4834 return 1;
4835}
4836
Avi Kivity851ba692009-08-24 11:10:17 +03004837static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004838{
Avi Kivity3842d132010-07-27 12:30:24 +03004839 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004840 return 1;
4841}
4842
Avi Kivity851ba692009-08-24 11:10:17 +03004843static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004844{
Eddie Dong85f455f2007-07-06 12:20:49 +03004845 u32 cpu_based_vm_exec_control;
4846
4847 /* clear pending irq */
4848 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4849 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
4850 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004851
Avi Kivity3842d132010-07-27 12:30:24 +03004852 kvm_make_request(KVM_REQ_EVENT, vcpu);
4853
Jan Kiszkaa26bf122008-09-26 09:30:45 +02004854 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004855
Dor Laorc1150d82007-01-05 16:36:24 -08004856 /*
4857 * If the user space waits to inject interrupts, exit as soon as
4858 * possible
4859 */
Gleb Natapov80618232009-04-21 17:44:56 +03004860 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03004861 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03004862 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004863 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08004864 return 0;
4865 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004866 return 1;
4867}
4868
Avi Kivity851ba692009-08-24 11:10:17 +03004869static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004870{
4871 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03004872 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004873}
4874
Avi Kivity851ba692009-08-24 11:10:17 +03004875static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02004876{
Dor Laor510043d2007-02-19 18:25:43 +02004877 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004878 kvm_emulate_hypercall(vcpu);
4879 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02004880}
4881
Gleb Natapovec25d5e2010-11-01 15:35:01 +02004882static int handle_invd(struct kvm_vcpu *vcpu)
4883{
Andre Przywara51d8b662010-12-21 11:12:02 +01004884 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02004885}
4886
Avi Kivity851ba692009-08-24 11:10:17 +03004887static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03004888{
Sheng Yangf9c617f2009-03-25 10:08:52 +08004889 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03004890
4891 kvm_mmu_invlpg(vcpu, exit_qualification);
4892 skip_emulated_instruction(vcpu);
4893 return 1;
4894}
4895
Avi Kivityfee84b02011-11-10 14:57:25 +02004896static int handle_rdpmc(struct kvm_vcpu *vcpu)
4897{
4898 int err;
4899
4900 err = kvm_rdpmc(vcpu);
4901 kvm_complete_insn_gp(vcpu, err);
4902
4903 return 1;
4904}
4905
Avi Kivity851ba692009-08-24 11:10:17 +03004906static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02004907{
4908 skip_emulated_instruction(vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004909 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02004910 return 1;
4911}
4912
Dexuan Cui2acf9232010-06-10 11:27:12 +08004913static int handle_xsetbv(struct kvm_vcpu *vcpu)
4914{
4915 u64 new_bv = kvm_read_edx_eax(vcpu);
4916 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
4917
4918 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
4919 skip_emulated_instruction(vcpu);
4920 return 1;
4921}
4922
Avi Kivity851ba692009-08-24 11:10:17 +03004923static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004924{
Kevin Tian58fbbf22011-08-30 13:56:17 +03004925 if (likely(fasteoi)) {
4926 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4927 int access_type, offset;
4928
4929 access_type = exit_qualification & APIC_ACCESS_TYPE;
4930 offset = exit_qualification & APIC_ACCESS_OFFSET;
4931 /*
4932 * Sane guest uses MOV to write EOI, with written value
4933 * not cared. So make a short-circuit here by avoiding
4934 * heavy instruction emulation.
4935 */
4936 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
4937 (offset == APIC_EOI)) {
4938 kvm_lapic_set_eoi(vcpu);
4939 skip_emulated_instruction(vcpu);
4940 return 1;
4941 }
4942 }
Andre Przywara51d8b662010-12-21 11:12:02 +01004943 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004944}
4945
Yang Zhangc7c9c562013-01-25 10:18:51 +08004946static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
4947{
4948 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4949 int vector = exit_qualification & 0xff;
4950
4951 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
4952 kvm_apic_set_eoi_accelerated(vcpu, vector);
4953 return 1;
4954}
4955
Yang Zhang83d4c282013-01-25 10:18:49 +08004956static int handle_apic_write(struct kvm_vcpu *vcpu)
4957{
4958 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4959 u32 offset = exit_qualification & 0xfff;
4960
4961 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
4962 kvm_apic_write_nodecode(vcpu, offset);
4963 return 1;
4964}
4965
Avi Kivity851ba692009-08-24 11:10:17 +03004966static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02004967{
Jan Kiszka60637aa2008-09-26 09:30:47 +02004968 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02004969 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02004970 bool has_error_code = false;
4971 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02004972 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01004973 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004974
4975 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01004976 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004977 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02004978
4979 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4980
4981 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004982 if (reason == TASK_SWITCH_GATE && idt_v) {
4983 switch (type) {
4984 case INTR_TYPE_NMI_INTR:
4985 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02004986 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004987 break;
4988 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004989 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004990 kvm_clear_interrupt_queue(vcpu);
4991 break;
4992 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02004993 if (vmx->idt_vectoring_info &
4994 VECTORING_INFO_DELIVER_CODE_MASK) {
4995 has_error_code = true;
4996 error_code =
4997 vmcs_read32(IDT_VECTORING_ERROR_CODE);
4998 }
4999 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005000 case INTR_TYPE_SOFT_EXCEPTION:
5001 kvm_clear_exception_queue(vcpu);
5002 break;
5003 default:
5004 break;
5005 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005006 }
Izik Eidus37817f22008-03-24 23:14:53 +02005007 tss_selector = exit_qualification;
5008
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005009 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5010 type != INTR_TYPE_EXT_INTR &&
5011 type != INTR_TYPE_NMI_INTR))
5012 skip_emulated_instruction(vcpu);
5013
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005014 if (kvm_task_switch(vcpu, tss_selector,
5015 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
5016 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03005017 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5018 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5019 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005020 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03005021 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005022
5023 /* clear all local breakpoint enable flags */
5024 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
5025
5026 /*
5027 * TODO: What about debug traps on tss switch?
5028 * Are we supposed to inject them and update dr6?
5029 */
5030
5031 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02005032}
5033
Avi Kivity851ba692009-08-24 11:10:17 +03005034static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005035{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005036 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005037 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005038 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005039 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08005040
Sheng Yangf9c617f2009-03-25 10:08:52 +08005041 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08005042
Sheng Yang14394422008-04-28 12:24:45 +08005043 gla_validity = (exit_qualification >> 7) & 0x3;
5044 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
5045 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
5046 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
5047 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08005048 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08005049 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
5050 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03005051 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5052 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03005053 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08005054 }
5055
5056 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005057 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005058
5059 /* It is a write fault? */
5060 error_code = exit_qualification & (1U << 1);
5061 /* ept page table is present? */
5062 error_code |= (exit_qualification >> 3) & 0x1;
5063
5064 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005065}
5066
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005067static u64 ept_rsvd_mask(u64 spte, int level)
5068{
5069 int i;
5070 u64 mask = 0;
5071
5072 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
5073 mask |= (1ULL << i);
5074
5075 if (level > 2)
5076 /* bits 7:3 reserved */
5077 mask |= 0xf8;
5078 else if (level == 2) {
5079 if (spte & (1ULL << 7))
5080 /* 2MB ref, bits 20:12 reserved */
5081 mask |= 0x1ff000;
5082 else
5083 /* bits 6:3 reserved */
5084 mask |= 0x78;
5085 }
5086
5087 return mask;
5088}
5089
5090static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
5091 int level)
5092{
5093 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
5094
5095 /* 010b (write-only) */
5096 WARN_ON((spte & 0x7) == 0x2);
5097
5098 /* 110b (write/execute) */
5099 WARN_ON((spte & 0x7) == 0x6);
5100
5101 /* 100b (execute-only) and value not supported by logical processor */
5102 if (!cpu_has_vmx_ept_execute_only())
5103 WARN_ON((spte & 0x7) == 0x4);
5104
5105 /* not 000b */
5106 if ((spte & 0x7)) {
5107 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
5108
5109 if (rsvd_bits != 0) {
5110 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
5111 __func__, rsvd_bits);
5112 WARN_ON(1);
5113 }
5114
5115 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
5116 u64 ept_mem_type = (spte & 0x38) >> 3;
5117
5118 if (ept_mem_type == 2 || ept_mem_type == 3 ||
5119 ept_mem_type == 7) {
5120 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
5121 __func__, ept_mem_type);
5122 WARN_ON(1);
5123 }
5124 }
5125 }
5126}
5127
Avi Kivity851ba692009-08-24 11:10:17 +03005128static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005129{
5130 u64 sptes[4];
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005131 int nr_sptes, i, ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005132 gpa_t gpa;
5133
5134 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
5135
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005136 ret = handle_mmio_page_fault_common(vcpu, gpa, true);
5137 if (likely(ret == 1))
5138 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
5139 EMULATE_DONE;
5140 if (unlikely(!ret))
5141 return 1;
5142
5143 /* It is the real ept misconfig */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005144 printk(KERN_ERR "EPT: Misconfiguration.\n");
5145 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
5146
5147 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
5148
5149 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
5150 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
5151
Avi Kivity851ba692009-08-24 11:10:17 +03005152 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5153 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005154
5155 return 0;
5156}
5157
Avi Kivity851ba692009-08-24 11:10:17 +03005158static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005159{
5160 u32 cpu_based_vm_exec_control;
5161
5162 /* clear pending NMI */
5163 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5164 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
5165 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5166 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005167 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005168
5169 return 1;
5170}
5171
Mohammed Gamal80ced182009-09-01 12:48:18 +02005172static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005173{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005174 struct vcpu_vmx *vmx = to_vmx(vcpu);
5175 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005176 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02005177 u32 cpu_exec_ctrl;
5178 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005179 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005180
5181 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5182 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005183
Avi Kivityb8405c12012-06-07 17:08:48 +03005184 while (!guest_state_valid(vcpu) && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03005185 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005186 return handle_interrupt_window(&vmx->vcpu);
5187
Avi Kivityde87dcdd2012-06-12 20:21:38 +03005188 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
5189 return 1;
5190
Andre Przywara51d8b662010-12-21 11:12:02 +01005191 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005192
Mohammed Gamal80ced182009-09-01 12:48:18 +02005193 if (err == EMULATE_DO_MMIO) {
5194 ret = 0;
5195 goto out;
5196 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005197
Avi Kivityde5f70e2012-06-12 20:22:28 +03005198 if (err != EMULATE_DONE) {
5199 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5200 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5201 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005202 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03005203 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005204
5205 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02005206 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005207 if (need_resched())
5208 schedule();
5209 }
5210
Gleb Natapov14168782013-01-21 15:36:49 +02005211 vmx->emulation_required = emulation_required(vcpu);
Mohammed Gamal80ced182009-09-01 12:48:18 +02005212out:
5213 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005214}
5215
Avi Kivity6aa8b732006-12-10 02:21:36 -08005216/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005217 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5218 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5219 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03005220static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005221{
5222 skip_emulated_instruction(vcpu);
5223 kvm_vcpu_on_spin(vcpu);
5224
5225 return 1;
5226}
5227
Sheng Yang59708672009-12-15 13:29:54 +08005228static int handle_invalid_op(struct kvm_vcpu *vcpu)
5229{
5230 kvm_queue_exception(vcpu, UD_VECTOR);
5231 return 1;
5232}
5233
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005234/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005235 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
5236 * We could reuse a single VMCS for all the L2 guests, but we also want the
5237 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
5238 * allows keeping them loaded on the processor, and in the future will allow
5239 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
5240 * every entry if they never change.
5241 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
5242 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
5243 *
5244 * The following functions allocate and free a vmcs02 in this pool.
5245 */
5246
5247/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
5248static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
5249{
5250 struct vmcs02_list *item;
5251 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5252 if (item->vmptr == vmx->nested.current_vmptr) {
5253 list_move(&item->list, &vmx->nested.vmcs02_pool);
5254 return &item->vmcs02;
5255 }
5256
5257 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
5258 /* Recycle the least recently used VMCS. */
5259 item = list_entry(vmx->nested.vmcs02_pool.prev,
5260 struct vmcs02_list, list);
5261 item->vmptr = vmx->nested.current_vmptr;
5262 list_move(&item->list, &vmx->nested.vmcs02_pool);
5263 return &item->vmcs02;
5264 }
5265
5266 /* Create a new VMCS */
Ioan Orghici0fa24ce2013-03-10 15:46:00 +02005267 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005268 if (!item)
5269 return NULL;
5270 item->vmcs02.vmcs = alloc_vmcs();
5271 if (!item->vmcs02.vmcs) {
5272 kfree(item);
5273 return NULL;
5274 }
5275 loaded_vmcs_init(&item->vmcs02);
5276 item->vmptr = vmx->nested.current_vmptr;
5277 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
5278 vmx->nested.vmcs02_num++;
5279 return &item->vmcs02;
5280}
5281
5282/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
5283static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
5284{
5285 struct vmcs02_list *item;
5286 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5287 if (item->vmptr == vmptr) {
5288 free_loaded_vmcs(&item->vmcs02);
5289 list_del(&item->list);
5290 kfree(item);
5291 vmx->nested.vmcs02_num--;
5292 return;
5293 }
5294}
5295
5296/*
5297 * Free all VMCSs saved for this vcpu, except the one pointed by
5298 * vmx->loaded_vmcs. These include the VMCSs in vmcs02_pool (except the one
5299 * currently used, if running L2), and vmcs01 when running L2.
5300 */
5301static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
5302{
5303 struct vmcs02_list *item, *n;
5304 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
5305 if (vmx->loaded_vmcs != &item->vmcs02)
5306 free_loaded_vmcs(&item->vmcs02);
5307 list_del(&item->list);
5308 kfree(item);
5309 }
5310 vmx->nested.vmcs02_num = 0;
5311
5312 if (vmx->loaded_vmcs != &vmx->vmcs01)
5313 free_loaded_vmcs(&vmx->vmcs01);
5314}
5315
5316/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005317 * Emulate the VMXON instruction.
5318 * Currently, we just remember that VMX is active, and do not save or even
5319 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
5320 * do not currently need to store anything in that guest-allocated memory
5321 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
5322 * argument is different from the VMXON pointer (which the spec says they do).
5323 */
5324static int handle_vmon(struct kvm_vcpu *vcpu)
5325{
5326 struct kvm_segment cs;
5327 struct vcpu_vmx *vmx = to_vmx(vcpu);
5328
5329 /* The Intel VMX Instruction Reference lists a bunch of bits that
5330 * are prerequisite to running VMXON, most notably cr4.VMXE must be
5331 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
5332 * Otherwise, we should fail with #UD. We test these now:
5333 */
5334 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
5335 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
5336 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
5337 kvm_queue_exception(vcpu, UD_VECTOR);
5338 return 1;
5339 }
5340
5341 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5342 if (is_long_mode(vcpu) && !cs.l) {
5343 kvm_queue_exception(vcpu, UD_VECTOR);
5344 return 1;
5345 }
5346
5347 if (vmx_get_cpl(vcpu)) {
5348 kvm_inject_gp(vcpu, 0);
5349 return 1;
5350 }
5351
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005352 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
5353 vmx->nested.vmcs02_num = 0;
5354
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005355 vmx->nested.vmxon = true;
5356
5357 skip_emulated_instruction(vcpu);
5358 return 1;
5359}
5360
5361/*
5362 * Intel's VMX Instruction Reference specifies a common set of prerequisites
5363 * for running VMX instructions (except VMXON, whose prerequisites are
5364 * slightly different). It also specifies what exception to inject otherwise.
5365 */
5366static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
5367{
5368 struct kvm_segment cs;
5369 struct vcpu_vmx *vmx = to_vmx(vcpu);
5370
5371 if (!vmx->nested.vmxon) {
5372 kvm_queue_exception(vcpu, UD_VECTOR);
5373 return 0;
5374 }
5375
5376 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5377 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
5378 (is_long_mode(vcpu) && !cs.l)) {
5379 kvm_queue_exception(vcpu, UD_VECTOR);
5380 return 0;
5381 }
5382
5383 if (vmx_get_cpl(vcpu)) {
5384 kvm_inject_gp(vcpu, 0);
5385 return 0;
5386 }
5387
5388 return 1;
5389}
5390
5391/*
5392 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
5393 * just stops using VMX.
5394 */
5395static void free_nested(struct vcpu_vmx *vmx)
5396{
5397 if (!vmx->nested.vmxon)
5398 return;
5399 vmx->nested.vmxon = false;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03005400 if (vmx->nested.current_vmptr != -1ull) {
5401 kunmap(vmx->nested.current_vmcs12_page);
5402 nested_release_page(vmx->nested.current_vmcs12_page);
5403 vmx->nested.current_vmptr = -1ull;
5404 vmx->nested.current_vmcs12 = NULL;
5405 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005406 /* Unpin physical memory we referred to in current vmcs02 */
5407 if (vmx->nested.apic_access_page) {
5408 nested_release_page(vmx->nested.apic_access_page);
5409 vmx->nested.apic_access_page = 0;
5410 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005411
5412 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005413}
5414
5415/* Emulate the VMXOFF instruction */
5416static int handle_vmoff(struct kvm_vcpu *vcpu)
5417{
5418 if (!nested_vmx_check_permission(vcpu))
5419 return 1;
5420 free_nested(to_vmx(vcpu));
5421 skip_emulated_instruction(vcpu);
5422 return 1;
5423}
5424
5425/*
Nadav Har'El064aea72011-05-25 23:04:56 +03005426 * Decode the memory-address operand of a vmx instruction, as recorded on an
5427 * exit caused by such an instruction (run by a guest hypervisor).
5428 * On success, returns 0. When the operand is invalid, returns 1 and throws
5429 * #UD or #GP.
5430 */
5431static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
5432 unsigned long exit_qualification,
5433 u32 vmx_instruction_info, gva_t *ret)
5434{
5435 /*
5436 * According to Vol. 3B, "Information for VM Exits Due to Instruction
5437 * Execution", on an exit, vmx_instruction_info holds most of the
5438 * addressing components of the operand. Only the displacement part
5439 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
5440 * For how an actual address is calculated from all these components,
5441 * refer to Vol. 1, "Operand Addressing".
5442 */
5443 int scaling = vmx_instruction_info & 3;
5444 int addr_size = (vmx_instruction_info >> 7) & 7;
5445 bool is_reg = vmx_instruction_info & (1u << 10);
5446 int seg_reg = (vmx_instruction_info >> 15) & 7;
5447 int index_reg = (vmx_instruction_info >> 18) & 0xf;
5448 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
5449 int base_reg = (vmx_instruction_info >> 23) & 0xf;
5450 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
5451
5452 if (is_reg) {
5453 kvm_queue_exception(vcpu, UD_VECTOR);
5454 return 1;
5455 }
5456
5457 /* Addr = segment_base + offset */
5458 /* offset = base + [index * scale] + displacement */
5459 *ret = vmx_get_segment_base(vcpu, seg_reg);
5460 if (base_is_valid)
5461 *ret += kvm_register_read(vcpu, base_reg);
5462 if (index_is_valid)
5463 *ret += kvm_register_read(vcpu, index_reg)<<scaling;
5464 *ret += exit_qualification; /* holds the displacement */
5465
5466 if (addr_size == 1) /* 32 bit */
5467 *ret &= 0xffffffff;
5468
5469 /*
5470 * TODO: throw #GP (and return 1) in various cases that the VM*
5471 * instructions require it - e.g., offset beyond segment limit,
5472 * unusable or unreadable/unwritable segment, non-canonical 64-bit
5473 * address, and so on. Currently these are not checked.
5474 */
5475 return 0;
5476}
5477
5478/*
Nadav Har'El0140cae2011-05-25 23:06:28 +03005479 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
5480 * set the success or error code of an emulated VMX instruction, as specified
5481 * by Vol 2B, VMX Instruction Reference, "Conventions".
5482 */
5483static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
5484{
5485 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
5486 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5487 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
5488}
5489
5490static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
5491{
5492 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5493 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
5494 X86_EFLAGS_SF | X86_EFLAGS_OF))
5495 | X86_EFLAGS_CF);
5496}
5497
5498static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
5499 u32 vm_instruction_error)
5500{
5501 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
5502 /*
5503 * failValid writes the error number to the current VMCS, which
5504 * can't be done there isn't a current VMCS.
5505 */
5506 nested_vmx_failInvalid(vcpu);
5507 return;
5508 }
5509 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5510 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5511 X86_EFLAGS_SF | X86_EFLAGS_OF))
5512 | X86_EFLAGS_ZF);
5513 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
5514}
5515
Nadav Har'El27d6c862011-05-25 23:06:59 +03005516/* Emulate the VMCLEAR instruction */
5517static int handle_vmclear(struct kvm_vcpu *vcpu)
5518{
5519 struct vcpu_vmx *vmx = to_vmx(vcpu);
5520 gva_t gva;
5521 gpa_t vmptr;
5522 struct vmcs12 *vmcs12;
5523 struct page *page;
5524 struct x86_exception e;
5525
5526 if (!nested_vmx_check_permission(vcpu))
5527 return 1;
5528
5529 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
5530 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
5531 return 1;
5532
5533 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
5534 sizeof(vmptr), &e)) {
5535 kvm_inject_page_fault(vcpu, &e);
5536 return 1;
5537 }
5538
5539 if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
5540 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
5541 skip_emulated_instruction(vcpu);
5542 return 1;
5543 }
5544
5545 if (vmptr == vmx->nested.current_vmptr) {
5546 kunmap(vmx->nested.current_vmcs12_page);
5547 nested_release_page(vmx->nested.current_vmcs12_page);
5548 vmx->nested.current_vmptr = -1ull;
5549 vmx->nested.current_vmcs12 = NULL;
5550 }
5551
5552 page = nested_get_page(vcpu, vmptr);
5553 if (page == NULL) {
5554 /*
5555 * For accurate processor emulation, VMCLEAR beyond available
5556 * physical memory should do nothing at all. However, it is
5557 * possible that a nested vmx bug, not a guest hypervisor bug,
5558 * resulted in this case, so let's shut down before doing any
5559 * more damage:
5560 */
5561 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
5562 return 1;
5563 }
5564 vmcs12 = kmap(page);
5565 vmcs12->launch_state = 0;
5566 kunmap(page);
5567 nested_release_page(page);
5568
5569 nested_free_vmcs02(vmx, vmptr);
5570
5571 skip_emulated_instruction(vcpu);
5572 nested_vmx_succeed(vcpu);
5573 return 1;
5574}
5575
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03005576static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
5577
5578/* Emulate the VMLAUNCH instruction */
5579static int handle_vmlaunch(struct kvm_vcpu *vcpu)
5580{
5581 return nested_vmx_run(vcpu, true);
5582}
5583
5584/* Emulate the VMRESUME instruction */
5585static int handle_vmresume(struct kvm_vcpu *vcpu)
5586{
5587
5588 return nested_vmx_run(vcpu, false);
5589}
5590
Nadav Har'El49f705c2011-05-25 23:08:30 +03005591enum vmcs_field_type {
5592 VMCS_FIELD_TYPE_U16 = 0,
5593 VMCS_FIELD_TYPE_U64 = 1,
5594 VMCS_FIELD_TYPE_U32 = 2,
5595 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
5596};
5597
5598static inline int vmcs_field_type(unsigned long field)
5599{
5600 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
5601 return VMCS_FIELD_TYPE_U32;
5602 return (field >> 13) & 0x3 ;
5603}
5604
5605static inline int vmcs_field_readonly(unsigned long field)
5606{
5607 return (((field >> 10) & 0x3) == 1);
5608}
5609
5610/*
5611 * Read a vmcs12 field. Since these can have varying lengths and we return
5612 * one type, we chose the biggest type (u64) and zero-extend the return value
5613 * to that size. Note that the caller, handle_vmread, might need to use only
5614 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
5615 * 64-bit fields are to be returned).
5616 */
5617static inline bool vmcs12_read_any(struct kvm_vcpu *vcpu,
5618 unsigned long field, u64 *ret)
5619{
5620 short offset = vmcs_field_to_offset(field);
5621 char *p;
5622
5623 if (offset < 0)
5624 return 0;
5625
5626 p = ((char *)(get_vmcs12(vcpu))) + offset;
5627
5628 switch (vmcs_field_type(field)) {
5629 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
5630 *ret = *((natural_width *)p);
5631 return 1;
5632 case VMCS_FIELD_TYPE_U16:
5633 *ret = *((u16 *)p);
5634 return 1;
5635 case VMCS_FIELD_TYPE_U32:
5636 *ret = *((u32 *)p);
5637 return 1;
5638 case VMCS_FIELD_TYPE_U64:
5639 *ret = *((u64 *)p);
5640 return 1;
5641 default:
5642 return 0; /* can never happen. */
5643 }
5644}
5645
5646/*
5647 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
5648 * used before) all generate the same failure when it is missing.
5649 */
5650static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
5651{
5652 struct vcpu_vmx *vmx = to_vmx(vcpu);
5653 if (vmx->nested.current_vmptr == -1ull) {
5654 nested_vmx_failInvalid(vcpu);
5655 skip_emulated_instruction(vcpu);
5656 return 0;
5657 }
5658 return 1;
5659}
5660
5661static int handle_vmread(struct kvm_vcpu *vcpu)
5662{
5663 unsigned long field;
5664 u64 field_value;
5665 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5666 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5667 gva_t gva = 0;
5668
5669 if (!nested_vmx_check_permission(vcpu) ||
5670 !nested_vmx_check_vmcs12(vcpu))
5671 return 1;
5672
5673 /* Decode instruction info and find the field to read */
5674 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5675 /* Read the field, zero-extended to a u64 field_value */
5676 if (!vmcs12_read_any(vcpu, field, &field_value)) {
5677 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5678 skip_emulated_instruction(vcpu);
5679 return 1;
5680 }
5681 /*
5682 * Now copy part of this value to register or memory, as requested.
5683 * Note that the number of bits actually copied is 32 or 64 depending
5684 * on the guest's mode (32 or 64 bit), not on the given field's length.
5685 */
5686 if (vmx_instruction_info & (1u << 10)) {
5687 kvm_register_write(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
5688 field_value);
5689 } else {
5690 if (get_vmx_mem_address(vcpu, exit_qualification,
5691 vmx_instruction_info, &gva))
5692 return 1;
5693 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
5694 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
5695 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
5696 }
5697
5698 nested_vmx_succeed(vcpu);
5699 skip_emulated_instruction(vcpu);
5700 return 1;
5701}
5702
5703
5704static int handle_vmwrite(struct kvm_vcpu *vcpu)
5705{
5706 unsigned long field;
5707 gva_t gva;
5708 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5709 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5710 char *p;
5711 short offset;
5712 /* The value to write might be 32 or 64 bits, depending on L1's long
5713 * mode, and eventually we need to write that into a field of several
5714 * possible lengths. The code below first zero-extends the value to 64
5715 * bit (field_value), and then copies only the approriate number of
5716 * bits into the vmcs12 field.
5717 */
5718 u64 field_value = 0;
5719 struct x86_exception e;
5720
5721 if (!nested_vmx_check_permission(vcpu) ||
5722 !nested_vmx_check_vmcs12(vcpu))
5723 return 1;
5724
5725 if (vmx_instruction_info & (1u << 10))
5726 field_value = kvm_register_read(vcpu,
5727 (((vmx_instruction_info) >> 3) & 0xf));
5728 else {
5729 if (get_vmx_mem_address(vcpu, exit_qualification,
5730 vmx_instruction_info, &gva))
5731 return 1;
5732 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
5733 &field_value, (is_long_mode(vcpu) ? 8 : 4), &e)) {
5734 kvm_inject_page_fault(vcpu, &e);
5735 return 1;
5736 }
5737 }
5738
5739
5740 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5741 if (vmcs_field_readonly(field)) {
5742 nested_vmx_failValid(vcpu,
5743 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
5744 skip_emulated_instruction(vcpu);
5745 return 1;
5746 }
5747
5748 offset = vmcs_field_to_offset(field);
5749 if (offset < 0) {
5750 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5751 skip_emulated_instruction(vcpu);
5752 return 1;
5753 }
5754 p = ((char *) get_vmcs12(vcpu)) + offset;
5755
5756 switch (vmcs_field_type(field)) {
5757 case VMCS_FIELD_TYPE_U16:
5758 *(u16 *)p = field_value;
5759 break;
5760 case VMCS_FIELD_TYPE_U32:
5761 *(u32 *)p = field_value;
5762 break;
5763 case VMCS_FIELD_TYPE_U64:
5764 *(u64 *)p = field_value;
5765 break;
5766 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
5767 *(natural_width *)p = field_value;
5768 break;
5769 default:
5770 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5771 skip_emulated_instruction(vcpu);
5772 return 1;
5773 }
5774
5775 nested_vmx_succeed(vcpu);
5776 skip_emulated_instruction(vcpu);
5777 return 1;
5778}
5779
Nadav Har'El63846662011-05-25 23:07:29 +03005780/* Emulate the VMPTRLD instruction */
5781static int handle_vmptrld(struct kvm_vcpu *vcpu)
5782{
5783 struct vcpu_vmx *vmx = to_vmx(vcpu);
5784 gva_t gva;
5785 gpa_t vmptr;
5786 struct x86_exception e;
5787
5788 if (!nested_vmx_check_permission(vcpu))
5789 return 1;
5790
5791 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
5792 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
5793 return 1;
5794
5795 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
5796 sizeof(vmptr), &e)) {
5797 kvm_inject_page_fault(vcpu, &e);
5798 return 1;
5799 }
5800
5801 if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
5802 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
5803 skip_emulated_instruction(vcpu);
5804 return 1;
5805 }
5806
5807 if (vmx->nested.current_vmptr != vmptr) {
5808 struct vmcs12 *new_vmcs12;
5809 struct page *page;
5810 page = nested_get_page(vcpu, vmptr);
5811 if (page == NULL) {
5812 nested_vmx_failInvalid(vcpu);
5813 skip_emulated_instruction(vcpu);
5814 return 1;
5815 }
5816 new_vmcs12 = kmap(page);
5817 if (new_vmcs12->revision_id != VMCS12_REVISION) {
5818 kunmap(page);
5819 nested_release_page_clean(page);
5820 nested_vmx_failValid(vcpu,
5821 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5822 skip_emulated_instruction(vcpu);
5823 return 1;
5824 }
5825 if (vmx->nested.current_vmptr != -1ull) {
5826 kunmap(vmx->nested.current_vmcs12_page);
5827 nested_release_page(vmx->nested.current_vmcs12_page);
5828 }
5829
5830 vmx->nested.current_vmptr = vmptr;
5831 vmx->nested.current_vmcs12 = new_vmcs12;
5832 vmx->nested.current_vmcs12_page = page;
5833 }
5834
5835 nested_vmx_succeed(vcpu);
5836 skip_emulated_instruction(vcpu);
5837 return 1;
5838}
5839
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005840/* Emulate the VMPTRST instruction */
5841static int handle_vmptrst(struct kvm_vcpu *vcpu)
5842{
5843 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5844 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5845 gva_t vmcs_gva;
5846 struct x86_exception e;
5847
5848 if (!nested_vmx_check_permission(vcpu))
5849 return 1;
5850
5851 if (get_vmx_mem_address(vcpu, exit_qualification,
5852 vmx_instruction_info, &vmcs_gva))
5853 return 1;
5854 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
5855 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
5856 (void *)&to_vmx(vcpu)->nested.current_vmptr,
5857 sizeof(u64), &e)) {
5858 kvm_inject_page_fault(vcpu, &e);
5859 return 1;
5860 }
5861 nested_vmx_succeed(vcpu);
5862 skip_emulated_instruction(vcpu);
5863 return 1;
5864}
5865
Nadav Har'El0140cae2011-05-25 23:06:28 +03005866/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005867 * The exit handlers return 1 if the exit was handled fully and guest execution
5868 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
5869 * to be done to userspace and return 0.
5870 */
Mathias Krause772e0312012-08-30 01:30:19 +02005871static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005872 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
5873 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08005874 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08005875 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005876 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005877 [EXIT_REASON_CR_ACCESS] = handle_cr,
5878 [EXIT_REASON_DR_ACCESS] = handle_dr,
5879 [EXIT_REASON_CPUID] = handle_cpuid,
5880 [EXIT_REASON_MSR_READ] = handle_rdmsr,
5881 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
5882 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
5883 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005884 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03005885 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02005886 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02005887 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03005888 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03005889 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03005890 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005891 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03005892 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03005893 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03005894 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005895 [EXIT_REASON_VMOFF] = handle_vmoff,
5896 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08005897 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
5898 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08005899 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08005900 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02005901 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08005902 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02005903 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08005904 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005905 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
5906 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005907 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sheng Yang59708672009-12-15 13:29:54 +08005908 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
5909 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005910};
5911
5912static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04005913 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005914
Jan Kiszka908a7bd2013-02-18 11:21:16 +01005915static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
5916 struct vmcs12 *vmcs12)
5917{
5918 unsigned long exit_qualification;
5919 gpa_t bitmap, last_bitmap;
5920 unsigned int port;
5921 int size;
5922 u8 b;
5923
5924 if (nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING))
5925 return 1;
5926
5927 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
5928 return 0;
5929
5930 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5931
5932 port = exit_qualification >> 16;
5933 size = (exit_qualification & 7) + 1;
5934
5935 last_bitmap = (gpa_t)-1;
5936 b = -1;
5937
5938 while (size > 0) {
5939 if (port < 0x8000)
5940 bitmap = vmcs12->io_bitmap_a;
5941 else if (port < 0x10000)
5942 bitmap = vmcs12->io_bitmap_b;
5943 else
5944 return 1;
5945 bitmap += (port & 0x7fff) / 8;
5946
5947 if (last_bitmap != bitmap)
5948 if (kvm_read_guest(vcpu->kvm, bitmap, &b, 1))
5949 return 1;
5950 if (b & (1 << (port & 7)))
5951 return 1;
5952
5953 port++;
5954 size--;
5955 last_bitmap = bitmap;
5956 }
5957
5958 return 0;
5959}
5960
Nadav Har'El644d7112011-05-25 23:12:35 +03005961/*
5962 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
5963 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
5964 * disinterest in the current event (read or write a specific MSR) by using an
5965 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
5966 */
5967static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
5968 struct vmcs12 *vmcs12, u32 exit_reason)
5969{
5970 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
5971 gpa_t bitmap;
5972
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01005973 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Nadav Har'El644d7112011-05-25 23:12:35 +03005974 return 1;
5975
5976 /*
5977 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
5978 * for the four combinations of read/write and low/high MSR numbers.
5979 * First we need to figure out which of the four to use:
5980 */
5981 bitmap = vmcs12->msr_bitmap;
5982 if (exit_reason == EXIT_REASON_MSR_WRITE)
5983 bitmap += 2048;
5984 if (msr_index >= 0xc0000000) {
5985 msr_index -= 0xc0000000;
5986 bitmap += 1024;
5987 }
5988
5989 /* Then read the msr_index'th bit from this bitmap: */
5990 if (msr_index < 1024*8) {
5991 unsigned char b;
Jan Kiszkabd31a7f2013-02-14 19:46:27 +01005992 if (kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1))
5993 return 1;
Nadav Har'El644d7112011-05-25 23:12:35 +03005994 return 1 & (b >> (msr_index & 7));
5995 } else
5996 return 1; /* let L1 handle the wrong parameter */
5997}
5998
5999/*
6000 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
6001 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
6002 * intercept (via guest_host_mask etc.) the current event.
6003 */
6004static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
6005 struct vmcs12 *vmcs12)
6006{
6007 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6008 int cr = exit_qualification & 15;
6009 int reg = (exit_qualification >> 8) & 15;
6010 unsigned long val = kvm_register_read(vcpu, reg);
6011
6012 switch ((exit_qualification >> 4) & 3) {
6013 case 0: /* mov to cr */
6014 switch (cr) {
6015 case 0:
6016 if (vmcs12->cr0_guest_host_mask &
6017 (val ^ vmcs12->cr0_read_shadow))
6018 return 1;
6019 break;
6020 case 3:
6021 if ((vmcs12->cr3_target_count >= 1 &&
6022 vmcs12->cr3_target_value0 == val) ||
6023 (vmcs12->cr3_target_count >= 2 &&
6024 vmcs12->cr3_target_value1 == val) ||
6025 (vmcs12->cr3_target_count >= 3 &&
6026 vmcs12->cr3_target_value2 == val) ||
6027 (vmcs12->cr3_target_count >= 4 &&
6028 vmcs12->cr3_target_value3 == val))
6029 return 0;
6030 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
6031 return 1;
6032 break;
6033 case 4:
6034 if (vmcs12->cr4_guest_host_mask &
6035 (vmcs12->cr4_read_shadow ^ val))
6036 return 1;
6037 break;
6038 case 8:
6039 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
6040 return 1;
6041 break;
6042 }
6043 break;
6044 case 2: /* clts */
6045 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
6046 (vmcs12->cr0_read_shadow & X86_CR0_TS))
6047 return 1;
6048 break;
6049 case 1: /* mov from cr */
6050 switch (cr) {
6051 case 3:
6052 if (vmcs12->cpu_based_vm_exec_control &
6053 CPU_BASED_CR3_STORE_EXITING)
6054 return 1;
6055 break;
6056 case 8:
6057 if (vmcs12->cpu_based_vm_exec_control &
6058 CPU_BASED_CR8_STORE_EXITING)
6059 return 1;
6060 break;
6061 }
6062 break;
6063 case 3: /* lmsw */
6064 /*
6065 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
6066 * cr0. Other attempted changes are ignored, with no exit.
6067 */
6068 if (vmcs12->cr0_guest_host_mask & 0xe &
6069 (val ^ vmcs12->cr0_read_shadow))
6070 return 1;
6071 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
6072 !(vmcs12->cr0_read_shadow & 0x1) &&
6073 (val & 0x1))
6074 return 1;
6075 break;
6076 }
6077 return 0;
6078}
6079
6080/*
6081 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
6082 * should handle it ourselves in L0 (and then continue L2). Only call this
6083 * when in is_guest_mode (L2).
6084 */
6085static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
6086{
Nadav Har'El644d7112011-05-25 23:12:35 +03006087 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
6088 struct vcpu_vmx *vmx = to_vmx(vcpu);
6089 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01006090 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03006091
6092 if (vmx->nested.nested_run_pending)
6093 return 0;
6094
6095 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02006096 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
6097 vmcs_read32(VM_INSTRUCTION_ERROR));
Nadav Har'El644d7112011-05-25 23:12:35 +03006098 return 1;
6099 }
6100
6101 switch (exit_reason) {
6102 case EXIT_REASON_EXCEPTION_NMI:
6103 if (!is_exception(intr_info))
6104 return 0;
6105 else if (is_page_fault(intr_info))
6106 return enable_ept;
6107 return vmcs12->exception_bitmap &
6108 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
6109 case EXIT_REASON_EXTERNAL_INTERRUPT:
6110 return 0;
6111 case EXIT_REASON_TRIPLE_FAULT:
6112 return 1;
6113 case EXIT_REASON_PENDING_INTERRUPT:
6114 case EXIT_REASON_NMI_WINDOW:
6115 /*
6116 * prepare_vmcs02() set the CPU_BASED_VIRTUAL_INTR_PENDING bit
6117 * (aka Interrupt Window Exiting) only when L1 turned it on,
6118 * so if we got a PENDING_INTERRUPT exit, this must be for L1.
6119 * Same for NMI Window Exiting.
6120 */
6121 return 1;
6122 case EXIT_REASON_TASK_SWITCH:
6123 return 1;
6124 case EXIT_REASON_CPUID:
6125 return 1;
6126 case EXIT_REASON_HLT:
6127 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
6128 case EXIT_REASON_INVD:
6129 return 1;
6130 case EXIT_REASON_INVLPG:
6131 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
6132 case EXIT_REASON_RDPMC:
6133 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
6134 case EXIT_REASON_RDTSC:
6135 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
6136 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
6137 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
6138 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
6139 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
6140 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
6141 /*
6142 * VMX instructions trap unconditionally. This allows L1 to
6143 * emulate them for its L2 guest, i.e., allows 3-level nesting!
6144 */
6145 return 1;
6146 case EXIT_REASON_CR_ACCESS:
6147 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
6148 case EXIT_REASON_DR_ACCESS:
6149 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
6150 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006151 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03006152 case EXIT_REASON_MSR_READ:
6153 case EXIT_REASON_MSR_WRITE:
6154 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
6155 case EXIT_REASON_INVALID_STATE:
6156 return 1;
6157 case EXIT_REASON_MWAIT_INSTRUCTION:
6158 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
6159 case EXIT_REASON_MONITOR_INSTRUCTION:
6160 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
6161 case EXIT_REASON_PAUSE_INSTRUCTION:
6162 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
6163 nested_cpu_has2(vmcs12,
6164 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
6165 case EXIT_REASON_MCE_DURING_VMENTRY:
6166 return 0;
6167 case EXIT_REASON_TPR_BELOW_THRESHOLD:
6168 return 1;
6169 case EXIT_REASON_APIC_ACCESS:
6170 return nested_cpu_has2(vmcs12,
6171 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
6172 case EXIT_REASON_EPT_VIOLATION:
6173 case EXIT_REASON_EPT_MISCONFIG:
6174 return 0;
Jan Kiszka0238ea92013-03-13 11:31:24 +01006175 case EXIT_REASON_PREEMPTION_TIMER:
6176 return vmcs12->pin_based_vm_exec_control &
6177 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'El644d7112011-05-25 23:12:35 +03006178 case EXIT_REASON_WBINVD:
6179 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
6180 case EXIT_REASON_XSETBV:
6181 return 1;
6182 default:
6183 return 1;
6184 }
6185}
6186
Avi Kivity586f9602010-11-18 13:09:54 +02006187static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
6188{
6189 *info1 = vmcs_readl(EXIT_QUALIFICATION);
6190 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
6191}
6192
Avi Kivity6aa8b732006-12-10 02:21:36 -08006193/*
6194 * The guest has exited. See if we can fix it or if we need userspace
6195 * assistance.
6196 */
Avi Kivity851ba692009-08-24 11:10:17 +03006197static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006198{
Avi Kivity29bd8a72007-09-10 17:27:03 +03006199 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006200 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02006201 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03006202
Mohammed Gamal80ced182009-09-01 12:48:18 +02006203 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02006204 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02006205 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006206
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006207 /*
6208 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
6209 * we did not inject a still-pending event to L1 now because of
6210 * nested_run_pending, we need to re-enable this bit.
6211 */
6212 if (vmx->nested.nested_run_pending)
6213 kvm_make_request(KVM_REQ_EVENT, vcpu);
6214
Nadav Har'El509c75e2011-06-02 11:54:52 +03006215 if (!is_guest_mode(vcpu) && (exit_reason == EXIT_REASON_VMLAUNCH ||
6216 exit_reason == EXIT_REASON_VMRESUME))
Nadav Har'El644d7112011-05-25 23:12:35 +03006217 vmx->nested.nested_run_pending = 1;
6218 else
6219 vmx->nested.nested_run_pending = 0;
6220
6221 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
6222 nested_vmx_vmexit(vcpu);
6223 return 1;
6224 }
6225
Mohammed Gamal51207022010-05-31 22:40:54 +03006226 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
6227 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6228 vcpu->run->fail_entry.hardware_entry_failure_reason
6229 = exit_reason;
6230 return 0;
6231 }
6232
Avi Kivity29bd8a72007-09-10 17:27:03 +03006233 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006234 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6235 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03006236 = vmcs_read32(VM_INSTRUCTION_ERROR);
6237 return 0;
6238 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006239
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006240 /*
6241 * Note:
6242 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
6243 * delivery event since it indicates guest is accessing MMIO.
6244 * The vm-exit can be triggered again after return to guest that
6245 * will cause infinite loop.
6246 */
Mike Dayd77c26f2007-10-08 09:02:08 -04006247 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08006248 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02006249 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006250 exit_reason != EXIT_REASON_TASK_SWITCH)) {
6251 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6252 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
6253 vcpu->run->internal.ndata = 2;
6254 vcpu->run->internal.data[0] = vectoring_info;
6255 vcpu->run->internal.data[1] = exit_reason;
6256 return 0;
6257 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006258
Nadav Har'El644d7112011-05-25 23:12:35 +03006259 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
6260 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
6261 get_vmcs12(vcpu), vcpu)))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03006262 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006263 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006264 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01006265 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006266 /*
6267 * This CPU don't support us in finding the end of an
6268 * NMI-blocked window if the guest runs with IRQs
6269 * disabled. So we pull the trigger after 1 s of
6270 * futile waiting, but inform the user about this.
6271 */
6272 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
6273 "state on VCPU %d after 1 s timeout\n",
6274 __func__, vcpu->vcpu_id);
6275 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006276 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006277 }
6278
Avi Kivity6aa8b732006-12-10 02:21:36 -08006279 if (exit_reason < kvm_vmx_max_exit_handlers
6280 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03006281 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006282 else {
Avi Kivity851ba692009-08-24 11:10:17 +03006283 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6284 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006285 }
6286 return 0;
6287}
6288
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006289static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006290{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006291 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006292 vmcs_write32(TPR_THRESHOLD, 0);
6293 return;
6294 }
6295
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006296 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006297}
6298
Yang Zhang8d146952013-01-25 10:18:50 +08006299static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
6300{
6301 u32 sec_exec_control;
6302
6303 /*
6304 * There is not point to enable virtualize x2apic without enable
6305 * apicv
6306 */
Yang Zhangc7c9c562013-01-25 10:18:51 +08006307 if (!cpu_has_vmx_virtualize_x2apic_mode() ||
6308 !vmx_vm_has_apicv(vcpu->kvm))
Yang Zhang8d146952013-01-25 10:18:50 +08006309 return;
6310
6311 if (!vm_need_tpr_shadow(vcpu->kvm))
6312 return;
6313
6314 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6315
6316 if (set) {
6317 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6318 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6319 } else {
6320 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6321 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6322 }
6323 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
6324
6325 vmx_set_msr_bitmap(vcpu);
6326}
6327
Yang Zhangc7c9c562013-01-25 10:18:51 +08006328static void vmx_hwapic_isr_update(struct kvm *kvm, int isr)
6329{
6330 u16 status;
6331 u8 old;
6332
6333 if (!vmx_vm_has_apicv(kvm))
6334 return;
6335
6336 if (isr == -1)
6337 isr = 0;
6338
6339 status = vmcs_read16(GUEST_INTR_STATUS);
6340 old = status >> 8;
6341 if (isr != old) {
6342 status &= 0xff;
6343 status |= isr << 8;
6344 vmcs_write16(GUEST_INTR_STATUS, status);
6345 }
6346}
6347
6348static void vmx_set_rvi(int vector)
6349{
6350 u16 status;
6351 u8 old;
6352
6353 status = vmcs_read16(GUEST_INTR_STATUS);
6354 old = (u8)status & 0xff;
6355 if ((u8)vector != old) {
6356 status &= ~0xff;
6357 status |= (u8)vector;
6358 vmcs_write16(GUEST_INTR_STATUS, status);
6359 }
6360}
6361
6362static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
6363{
6364 if (max_irr == -1)
6365 return;
6366
6367 vmx_set_rvi(max_irr);
6368}
6369
6370static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
6371{
6372 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
6373 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
6374 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
6375 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
6376}
6377
Avi Kivity51aa01d2010-07-20 14:31:20 +03006378static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03006379{
Avi Kivity00eba012011-03-07 17:24:54 +02006380 u32 exit_intr_info;
6381
6382 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
6383 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
6384 return;
6385
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006386 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02006387 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08006388
6389 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02006390 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08006391 kvm_machine_check();
6392
Gleb Natapov20f65982009-05-11 13:35:55 +03006393 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02006394 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006395 (exit_intr_info & INTR_INFO_VALID_MASK)) {
6396 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03006397 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006398 kvm_after_handle_nmi(&vmx->vcpu);
6399 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03006400}
Gleb Natapov20f65982009-05-11 13:35:55 +03006401
Avi Kivity51aa01d2010-07-20 14:31:20 +03006402static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
6403{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006404 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03006405 bool unblock_nmi;
6406 u8 vector;
6407 bool idtv_info_valid;
6408
6409 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03006410
Avi Kivitycf393f72008-07-01 16:20:21 +03006411 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02006412 if (vmx->nmi_known_unmasked)
6413 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006414 /*
6415 * Can't use vmx->exit_intr_info since we're not sure what
6416 * the exit reason is.
6417 */
6418 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03006419 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
6420 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
6421 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006422 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03006423 * Re-set bit "block by NMI" before VM entry if vmexit caused by
6424 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006425 * SDM 3: 23.2.2 (September 2008)
6426 * Bit 12 is undefined in any of the following cases:
6427 * If the VM exit sets the valid bit in the IDT-vectoring
6428 * information field.
6429 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03006430 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006431 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
6432 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03006433 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6434 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02006435 else
6436 vmx->nmi_known_unmasked =
6437 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
6438 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006439 } else if (unlikely(vmx->soft_vnmi_blocked))
6440 vmx->vnmi_blocked_time +=
6441 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03006442}
6443
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006444static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03006445 u32 idt_vectoring_info,
6446 int instr_len_field,
6447 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03006448{
Avi Kivity51aa01d2010-07-20 14:31:20 +03006449 u8 vector;
6450 int type;
6451 bool idtv_info_valid;
6452
6453 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03006454
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006455 vcpu->arch.nmi_injected = false;
6456 kvm_clear_exception_queue(vcpu);
6457 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006458
6459 if (!idtv_info_valid)
6460 return;
6461
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006462 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03006463
Avi Kivity668f6122008-07-02 09:28:55 +03006464 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
6465 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006466
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006467 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03006468 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006469 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03006470 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006471 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03006472 * Clear bit "block by NMI" before VM entry if a NMI
6473 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03006474 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006475 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006476 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006477 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006478 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006479 /* fall through */
6480 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03006481 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03006482 u32 err = vmcs_read32(error_code_field);
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006483 kvm_queue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03006484 } else
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006485 kvm_queue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006486 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006487 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006488 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006489 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03006490 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006491 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006492 break;
6493 default:
6494 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03006495 }
Avi Kivitycf393f72008-07-01 16:20:21 +03006496}
6497
Avi Kivity83422e12010-07-20 14:43:23 +03006498static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
6499{
Nadav Har'El66c78ae2011-05-25 23:14:07 +03006500 if (is_guest_mode(&vmx->vcpu))
6501 return;
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006502 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03006503 VM_EXIT_INSTRUCTION_LEN,
6504 IDT_VECTORING_ERROR_CODE);
6505}
6506
Avi Kivityb463a6f2010-07-20 15:06:17 +03006507static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
6508{
Nadav Har'El66c78ae2011-05-25 23:14:07 +03006509 if (is_guest_mode(vcpu))
6510 return;
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006511 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03006512 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
6513 VM_ENTRY_INSTRUCTION_LEN,
6514 VM_ENTRY_EXCEPTION_ERROR_CODE);
6515
6516 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
6517}
6518
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006519static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
6520{
6521 int i, nr_msrs;
6522 struct perf_guest_switch_msr *msrs;
6523
6524 msrs = perf_guest_get_msrs(&nr_msrs);
6525
6526 if (!msrs)
6527 return;
6528
6529 for (i = 0; i < nr_msrs; i++)
6530 if (msrs[i].host == msrs[i].guest)
6531 clear_atomic_switch_msr(vmx, msrs[i].msr);
6532 else
6533 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
6534 msrs[i].host);
6535}
6536
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08006537static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006538{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006539 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006540 unsigned long debugctlmsr;
Avi Kivity104f2262010-11-18 13:12:52 +02006541
Nadav Har'El66c78ae2011-05-25 23:14:07 +03006542 if (is_guest_mode(vcpu) && !vmx->nested.nested_run_pending) {
6543 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6544 if (vmcs12->idt_vectoring_info_field &
6545 VECTORING_INFO_VALID_MASK) {
6546 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6547 vmcs12->idt_vectoring_info_field);
6548 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6549 vmcs12->vm_exit_instruction_len);
6550 if (vmcs12->idt_vectoring_info_field &
6551 VECTORING_INFO_DELIVER_CODE_MASK)
6552 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
6553 vmcs12->idt_vectoring_error_code);
6554 }
6555 }
6556
Avi Kivity104f2262010-11-18 13:12:52 +02006557 /* Record the guest's net vcpu time for enforced NMI injections. */
6558 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
6559 vmx->entry_time = ktime_get();
6560
6561 /* Don't enter VMX if guest state is invalid, let the exit handler
6562 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02006563 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02006564 return;
6565
6566 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
6567 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
6568 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
6569 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
6570
6571 /* When single-stepping over STI and MOV SS, we must clear the
6572 * corresponding interruptibility bits in the guest state. Otherwise
6573 * vmentry fails as it then expects bit 14 (BS) in pending debug
6574 * exceptions being set, but that's not correct for the guest debugging
6575 * case. */
6576 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6577 vmx_set_interrupt_shadow(vcpu, 0);
6578
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006579 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006580 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006581
Nadav Har'Eld462b812011-05-24 15:26:10 +03006582 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02006583 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08006584 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006585 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
6586 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
6587 "push %%" _ASM_CX " \n\t"
6588 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03006589 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006590 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03006591 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03006592 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03006593 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006594 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
6595 "mov %%cr2, %%" _ASM_DX " \n\t"
6596 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03006597 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006598 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03006599 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006600 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02006601 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006602 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006603 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
6604 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
6605 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
6606 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
6607 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
6608 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006609#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02006610 "mov %c[r8](%0), %%r8 \n\t"
6611 "mov %c[r9](%0), %%r9 \n\t"
6612 "mov %c[r10](%0), %%r10 \n\t"
6613 "mov %c[r11](%0), %%r11 \n\t"
6614 "mov %c[r12](%0), %%r12 \n\t"
6615 "mov %c[r13](%0), %%r13 \n\t"
6616 "mov %c[r14](%0), %%r14 \n\t"
6617 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006618#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03006619 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03006620
Avi Kivity6aa8b732006-12-10 02:21:36 -08006621 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03006622 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03006623 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03006624 "jmp 2f \n\t"
6625 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
6626 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08006627 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006628 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02006629 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006630 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
6631 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
6632 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
6633 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
6634 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
6635 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
6636 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006637#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02006638 "mov %%r8, %c[r8](%0) \n\t"
6639 "mov %%r9, %c[r9](%0) \n\t"
6640 "mov %%r10, %c[r10](%0) \n\t"
6641 "mov %%r11, %c[r11](%0) \n\t"
6642 "mov %%r12, %c[r12](%0) \n\t"
6643 "mov %%r13, %c[r13](%0) \n\t"
6644 "mov %%r14, %c[r14](%0) \n\t"
6645 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006646#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03006647 "mov %%cr2, %%" _ASM_AX " \n\t"
6648 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03006649
Avi Kivityb188c81f2012-09-16 15:10:58 +03006650 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02006651 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03006652 ".pushsection .rodata \n\t"
6653 ".global vmx_return \n\t"
6654 "vmx_return: " _ASM_PTR " 2b \n\t"
6655 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02006656 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03006657 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02006658 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03006659 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006660 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
6661 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
6662 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
6663 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
6664 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
6665 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
6666 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006667#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006668 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
6669 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
6670 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
6671 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
6672 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
6673 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
6674 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
6675 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08006676#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02006677 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
6678 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02006679 : "cc", "memory"
6680#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03006681 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02006682 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006683#else
6684 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02006685#endif
6686 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08006687
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006688 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
6689 if (debugctlmsr)
6690 update_debugctlmsr(debugctlmsr);
6691
Avi Kivityaa67f602012-08-01 16:48:03 +03006692#ifndef CONFIG_X86_64
6693 /*
6694 * The sysexit path does not restore ds/es, so we must set them to
6695 * a reasonable value ourselves.
6696 *
6697 * We can't defer this to vmx_load_host_state() since that function
6698 * may be executed in interrupt context, which saves and restore segments
6699 * around it, nullifying its effect.
6700 */
6701 loadsegment(ds, __USER_DS);
6702 loadsegment(es, __USER_DS);
6703#endif
6704
Avi Kivity6de4f3a2009-05-31 22:58:47 +03006705 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02006706 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivity69c73022011-03-07 15:26:44 +02006707 | (1 << VCPU_EXREG_CPL)
Avi Kivityaff48ba2010-12-05 18:56:11 +02006708 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03006709 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02006710 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006711 vcpu->arch.regs_dirty = 0;
6712
Avi Kivity1155f762007-11-22 11:30:47 +02006713 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
6714
Nadav Har'El66c78ae2011-05-25 23:14:07 +03006715 if (is_guest_mode(vcpu)) {
6716 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6717 vmcs12->idt_vectoring_info_field = vmx->idt_vectoring_info;
6718 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
6719 vmcs12->idt_vectoring_error_code =
6720 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6721 vmcs12->vm_exit_instruction_len =
6722 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6723 }
6724 }
6725
Nadav Har'Eld462b812011-05-24 15:26:10 +03006726 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02006727
Avi Kivity51aa01d2010-07-20 14:31:20 +03006728 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02006729 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
Avi Kivity51aa01d2010-07-20 14:31:20 +03006730
6731 vmx_complete_atomic_exit(vmx);
6732 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03006733 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006734}
6735
Avi Kivity6aa8b732006-12-10 02:21:36 -08006736static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
6737{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006738 struct vcpu_vmx *vmx = to_vmx(vcpu);
6739
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08006740 free_vpid(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006741 free_nested(vmx);
Nadav Har'Eld462b812011-05-24 15:26:10 +03006742 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006743 kfree(vmx->guest_msrs);
6744 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10006745 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006746}
6747
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006748static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006749{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006750 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10006751 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03006752 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006753
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006754 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006755 return ERR_PTR(-ENOMEM);
6756
Sheng Yang2384d2b2008-01-17 15:14:33 +08006757 allocate_vpid(vmx);
6758
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006759 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
6760 if (err)
6761 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006762
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006763 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02006764 err = -ENOMEM;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006765 if (!vmx->guest_msrs) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006766 goto uninit_vcpu;
6767 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08006768
Nadav Har'Eld462b812011-05-24 15:26:10 +03006769 vmx->loaded_vmcs = &vmx->vmcs01;
6770 vmx->loaded_vmcs->vmcs = alloc_vmcs();
6771 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006772 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03006773 if (!vmm_exclusive)
6774 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
6775 loaded_vmcs_init(vmx->loaded_vmcs);
6776 if (!vmm_exclusive)
6777 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006778
Avi Kivity15ad7142007-07-11 18:17:21 +03006779 cpu = get_cpu();
6780 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10006781 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10006782 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006783 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03006784 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006785 if (err)
6786 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02006787 if (vm_need_virtualize_apic_accesses(kvm))
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02006788 err = alloc_apic_access_page(kvm);
6789 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02006790 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006791
Sheng Yangb927a3c2009-07-21 10:42:48 +08006792 if (enable_ept) {
6793 if (!kvm->arch.ept_identity_map_addr)
6794 kvm->arch.ept_identity_map_addr =
6795 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Gleb Natapov93ea5382011-02-21 12:07:59 +02006796 err = -ENOMEM;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08006797 if (alloc_identity_pagetable(kvm) != 0)
6798 goto free_vmcs;
Gleb Natapov93ea5382011-02-21 12:07:59 +02006799 if (!init_rmode_identity_map(kvm))
6800 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08006801 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08006802
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006803 vmx->nested.current_vmptr = -1ull;
6804 vmx->nested.current_vmcs12 = NULL;
6805
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006806 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006807
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006808free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08006809 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006810free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006811 kfree(vmx->guest_msrs);
6812uninit_vcpu:
6813 kvm_vcpu_uninit(&vmx->vcpu);
6814free_vcpu:
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08006815 free_vpid(vmx);
Rusty Russella4770342007-08-01 14:46:11 +10006816 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006817 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006818}
6819
Yang, Sheng002c7f72007-07-31 14:23:01 +03006820static void __init vmx_check_processor_compat(void *rtn)
6821{
6822 struct vmcs_config vmcs_conf;
6823
6824 *(int *)rtn = 0;
6825 if (setup_vmcs_config(&vmcs_conf) < 0)
6826 *(int *)rtn = -EIO;
6827 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
6828 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
6829 smp_processor_id());
6830 *(int *)rtn = -EIO;
6831 }
6832}
6833
Sheng Yang67253af2008-04-25 10:20:22 +08006834static int get_ept_level(void)
6835{
6836 return VMX_EPT_DEFAULT_GAW + 1;
6837}
6838
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006839static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08006840{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006841 u64 ret;
6842
Sheng Yang522c68c2009-04-27 20:35:43 +08006843 /* For VT-d and EPT combination
6844 * 1. MMIO: always map as UC
6845 * 2. EPT with VT-d:
6846 * a. VT-d without snooping control feature: can't guarantee the
6847 * result, try to trust guest.
6848 * b. VT-d with snooping control feature: snooping control feature of
6849 * VT-d engine can guarantee the cache correctness. Just set it
6850 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08006851 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08006852 * consistent with host MTRR
6853 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006854 if (is_mmio)
6855 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08006856 else if (vcpu->kvm->arch.iommu_domain &&
6857 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
6858 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
6859 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006860 else
Sheng Yang522c68c2009-04-27 20:35:43 +08006861 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
Sheng Yanga19a6d12010-02-09 16:41:53 +08006862 | VMX_EPT_IPAT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006863
6864 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08006865}
6866
Sheng Yang17cc3932010-01-05 19:02:27 +08006867static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02006868{
Sheng Yang878403b2010-01-05 19:02:29 +08006869 if (enable_ept && !cpu_has_vmx_ept_1g_page())
6870 return PT_DIRECTORY_LEVEL;
6871 else
6872 /* For shadow and EPT supported 1GB page */
6873 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02006874}
6875
Sheng Yang0e851882009-12-18 16:48:46 +08006876static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
6877{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08006878 struct kvm_cpuid_entry2 *best;
6879 struct vcpu_vmx *vmx = to_vmx(vcpu);
6880 u32 exec_control;
6881
6882 vmx->rdtscp_enabled = false;
6883 if (vmx_rdtscp_supported()) {
6884 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6885 if (exec_control & SECONDARY_EXEC_RDTSCP) {
6886 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
6887 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
6888 vmx->rdtscp_enabled = true;
6889 else {
6890 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6891 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
6892 exec_control);
6893 }
6894 }
6895 }
Mao, Junjiead756a12012-07-02 01:18:48 +00006896
Mao, Junjiead756a12012-07-02 01:18:48 +00006897 /* Exposing INVPCID only when PCID is exposed */
6898 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
6899 if (vmx_invpcid_supported() &&
Ren, Yongjie4f977042012-09-07 07:36:59 +00006900 best && (best->ebx & bit(X86_FEATURE_INVPCID)) &&
Mao, Junjiead756a12012-07-02 01:18:48 +00006901 guest_cpuid_has_pcid(vcpu)) {
Takashi Iwai29282fd2012-11-09 15:20:17 +01006902 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Mao, Junjiead756a12012-07-02 01:18:48 +00006903 exec_control |= SECONDARY_EXEC_ENABLE_INVPCID;
6904 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
6905 exec_control);
6906 } else {
Takashi Iwai29282fd2012-11-09 15:20:17 +01006907 if (cpu_has_secondary_exec_ctrls()) {
6908 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6909 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6910 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
6911 exec_control);
6912 }
Mao, Junjiead756a12012-07-02 01:18:48 +00006913 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00006914 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00006915 }
Sheng Yang0e851882009-12-18 16:48:46 +08006916}
6917
Joerg Roedeld4330ef2010-04-22 12:33:11 +02006918static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
6919{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03006920 if (func == 1 && nested)
6921 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02006922}
6923
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006924/*
6925 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
6926 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
6927 * with L0's requirements for its guest (a.k.a. vmsc01), so we can run the L2
6928 * guest in a way that will both be appropriate to L1's requests, and our
6929 * needs. In addition to modifying the active vmcs (which is vmcs02), this
6930 * function also has additional necessary side-effects, like setting various
6931 * vcpu->arch fields.
6932 */
6933static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
6934{
6935 struct vcpu_vmx *vmx = to_vmx(vcpu);
6936 u32 exec_control;
6937
6938 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
6939 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
6940 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
6941 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
6942 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
6943 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
6944 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
6945 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
6946 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
6947 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
6948 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
6949 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
6950 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
6951 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
6952 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
6953 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
6954 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
6955 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
6956 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
6957 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
6958 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
6959 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
6960 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
6961 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
6962 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
6963 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
6964 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
6965 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
6966 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
6967 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
6968 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
6969 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
6970 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
6971 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
6972 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
6973 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
6974
6975 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
6976 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6977 vmcs12->vm_entry_intr_info_field);
6978 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
6979 vmcs12->vm_entry_exception_error_code);
6980 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6981 vmcs12->vm_entry_instruction_len);
6982 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
6983 vmcs12->guest_interruptibility_info);
6984 vmcs_write32(GUEST_ACTIVITY_STATE, vmcs12->guest_activity_state);
6985 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Jan Kiszka503cd0c2013-03-03 13:05:44 +01006986 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006987 vmcs_writel(GUEST_RFLAGS, vmcs12->guest_rflags);
6988 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
6989 vmcs12->guest_pending_dbg_exceptions);
6990 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
6991 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
6992
6993 vmcs_write64(VMCS_LINK_POINTER, -1ull);
6994
6995 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
6996 (vmcs_config.pin_based_exec_ctrl |
6997 vmcs12->pin_based_vm_exec_control));
6998
Jan Kiszka0238ea92013-03-13 11:31:24 +01006999 if (vmcs12->pin_based_vm_exec_control & PIN_BASED_VMX_PREEMPTION_TIMER)
7000 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
7001 vmcs12->vmx_preemption_timer_value);
7002
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007003 /*
7004 * Whether page-faults are trapped is determined by a combination of
7005 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
7006 * If enable_ept, L0 doesn't care about page faults and we should
7007 * set all of these to L1's desires. However, if !enable_ept, L0 does
7008 * care about (at least some) page faults, and because it is not easy
7009 * (if at all possible?) to merge L0 and L1's desires, we simply ask
7010 * to exit on each and every L2 page fault. This is done by setting
7011 * MASK=MATCH=0 and (see below) EB.PF=1.
7012 * Note that below we don't need special code to set EB.PF beyond the
7013 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
7014 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
7015 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
7016 *
7017 * A problem with this approach (when !enable_ept) is that L1 may be
7018 * injected with more page faults than it asked for. This could have
7019 * caused problems, but in practice existing hypervisors don't care.
7020 * To fix this, we will need to emulate the PFEC checking (on the L1
7021 * page tables), using walk_addr(), when injecting PFs to L1.
7022 */
7023 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
7024 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
7025 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
7026 enable_ept ? vmcs12->page_fault_error_code_match : 0);
7027
7028 if (cpu_has_secondary_exec_ctrls()) {
7029 u32 exec_control = vmx_secondary_exec_control(vmx);
7030 if (!vmx->rdtscp_enabled)
7031 exec_control &= ~SECONDARY_EXEC_RDTSCP;
7032 /* Take the following fields only from vmcs12 */
7033 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7034 if (nested_cpu_has(vmcs12,
7035 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
7036 exec_control |= vmcs12->secondary_vm_exec_control;
7037
7038 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
7039 /*
7040 * Translate L1 physical address to host physical
7041 * address for vmcs02. Keep the page pinned, so this
7042 * physical address remains valid. We keep a reference
7043 * to it so we can release it later.
7044 */
7045 if (vmx->nested.apic_access_page) /* shouldn't happen */
7046 nested_release_page(vmx->nested.apic_access_page);
7047 vmx->nested.apic_access_page =
7048 nested_get_page(vcpu, vmcs12->apic_access_addr);
7049 /*
7050 * If translation failed, no matter: This feature asks
7051 * to exit when accessing the given address, and if it
7052 * can never be accessed, this feature won't do
7053 * anything anyway.
7054 */
7055 if (!vmx->nested.apic_access_page)
7056 exec_control &=
7057 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7058 else
7059 vmcs_write64(APIC_ACCESS_ADDR,
7060 page_to_phys(vmx->nested.apic_access_page));
7061 }
7062
7063 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
7064 }
7065
7066
7067 /*
7068 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
7069 * Some constant fields are set here by vmx_set_constant_host_state().
7070 * Other fields are different per CPU, and will be set later when
7071 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
7072 */
7073 vmx_set_constant_host_state();
7074
7075 /*
7076 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
7077 * entry, but only if the current (host) sp changed from the value
7078 * we wrote last (vmx->host_rsp). This cache is no longer relevant
7079 * if we switch vmcs, and rather than hold a separate cache per vmcs,
7080 * here we just force the write to happen on entry.
7081 */
7082 vmx->host_rsp = 0;
7083
7084 exec_control = vmx_exec_control(vmx); /* L0's desires */
7085 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
7086 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
7087 exec_control &= ~CPU_BASED_TPR_SHADOW;
7088 exec_control |= vmcs12->cpu_based_vm_exec_control;
7089 /*
7090 * Merging of IO and MSR bitmaps not currently supported.
7091 * Rather, exit every time.
7092 */
7093 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
7094 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
7095 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
7096
7097 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
7098
7099 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
7100 * bitwise-or of what L1 wants to trap for L2, and what we want to
7101 * trap. Note that CR0.TS also needs updating - we do this later.
7102 */
7103 update_exception_bitmap(vcpu);
7104 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
7105 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
7106
7107 /* Note: IA32_MODE, LOAD_IA32_EFER are modified by vmx_set_efer below */
7108 vmcs_write32(VM_EXIT_CONTROLS,
7109 vmcs12->vm_exit_controls | vmcs_config.vmexit_ctrl);
7110 vmcs_write32(VM_ENTRY_CONTROLS, vmcs12->vm_entry_controls |
7111 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
7112
7113 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)
7114 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
7115 else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
7116 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
7117
7118
7119 set_cr4_guest_host_mask(vmx);
7120
Nadav Har'El27fc51b2011-08-02 15:54:52 +03007121 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
7122 vmcs_write64(TSC_OFFSET,
7123 vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
7124 else
7125 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007126
7127 if (enable_vpid) {
7128 /*
7129 * Trivially support vpid by letting L2s share their parent
7130 * L1's vpid. TODO: move to a more elaborate solution, giving
7131 * each L2 its own vpid and exposing the vpid feature to L1.
7132 */
7133 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
7134 vmx_flush_tlb(vcpu);
7135 }
7136
7137 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
7138 vcpu->arch.efer = vmcs12->guest_ia32_efer;
7139 if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
7140 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
7141 else
7142 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
7143 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
7144 vmx_set_efer(vcpu, vcpu->arch.efer);
7145
7146 /*
7147 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
7148 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
7149 * The CR0_READ_SHADOW is what L2 should have expected to read given
7150 * the specifications by L1; It's not enough to take
7151 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
7152 * have more bits than L1 expected.
7153 */
7154 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
7155 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
7156
7157 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
7158 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
7159
7160 /* shadow page tables on either EPT or shadow page tables */
7161 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
7162 kvm_mmu_reset_context(vcpu);
7163
7164 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
7165 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
7166}
7167
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007168/*
7169 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
7170 * for running an L2 nested guest.
7171 */
7172static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
7173{
7174 struct vmcs12 *vmcs12;
7175 struct vcpu_vmx *vmx = to_vmx(vcpu);
7176 int cpu;
7177 struct loaded_vmcs *vmcs02;
7178
7179 if (!nested_vmx_check_permission(vcpu) ||
7180 !nested_vmx_check_vmcs12(vcpu))
7181 return 1;
7182
7183 skip_emulated_instruction(vcpu);
7184 vmcs12 = get_vmcs12(vcpu);
7185
Nadav Har'El7c177932011-05-25 23:12:04 +03007186 /*
7187 * The nested entry process starts with enforcing various prerequisites
7188 * on vmcs12 as required by the Intel SDM, and act appropriately when
7189 * they fail: As the SDM explains, some conditions should cause the
7190 * instruction to fail, while others will cause the instruction to seem
7191 * to succeed, but return an EXIT_REASON_INVALID_STATE.
7192 * To speed up the normal (success) code path, we should avoid checking
7193 * for misconfigurations which will anyway be caught by the processor
7194 * when using the merged vmcs02.
7195 */
7196 if (vmcs12->launch_state == launch) {
7197 nested_vmx_failValid(vcpu,
7198 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
7199 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
7200 return 1;
7201 }
7202
7203 if ((vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_MSR_BITMAPS) &&
7204 !IS_ALIGNED(vmcs12->msr_bitmap, PAGE_SIZE)) {
7205 /*TODO: Also verify bits beyond physical address width are 0*/
7206 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7207 return 1;
7208 }
7209
7210 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
7211 !IS_ALIGNED(vmcs12->apic_access_addr, PAGE_SIZE)) {
7212 /*TODO: Also verify bits beyond physical address width are 0*/
7213 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7214 return 1;
7215 }
7216
7217 if (vmcs12->vm_entry_msr_load_count > 0 ||
7218 vmcs12->vm_exit_msr_load_count > 0 ||
7219 vmcs12->vm_exit_msr_store_count > 0) {
Jan Kiszkabd801582011-09-12 11:26:22 +02007220 pr_warn_ratelimited("%s: VMCS MSR_{LOAD,STORE} unsupported\n",
7221 __func__);
Nadav Har'El7c177932011-05-25 23:12:04 +03007222 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7223 return 1;
7224 }
7225
7226 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
7227 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high) ||
7228 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
7229 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high) ||
7230 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
7231 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high) ||
7232 !vmx_control_verify(vmcs12->vm_exit_controls,
7233 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high) ||
7234 !vmx_control_verify(vmcs12->vm_entry_controls,
7235 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high))
7236 {
7237 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7238 return 1;
7239 }
7240
7241 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
7242 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
7243 nested_vmx_failValid(vcpu,
7244 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
7245 return 1;
7246 }
7247
7248 if (((vmcs12->guest_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
7249 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
7250 nested_vmx_entry_failure(vcpu, vmcs12,
7251 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
7252 return 1;
7253 }
7254 if (vmcs12->vmcs_link_pointer != -1ull) {
7255 nested_vmx_entry_failure(vcpu, vmcs12,
7256 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
7257 return 1;
7258 }
7259
7260 /*
7261 * We're finally done with prerequisite checking, and can start with
7262 * the nested entry.
7263 */
7264
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007265 vmcs02 = nested_get_current_vmcs02(vmx);
7266 if (!vmcs02)
7267 return -ENOMEM;
7268
7269 enter_guest_mode(vcpu);
7270
7271 vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
7272
7273 cpu = get_cpu();
7274 vmx->loaded_vmcs = vmcs02;
7275 vmx_vcpu_put(vcpu);
7276 vmx_vcpu_load(vcpu, cpu);
7277 vcpu->cpu = cpu;
7278 put_cpu();
7279
Jan Kiszka36c3cc42013-02-23 22:35:37 +01007280 vmx_segment_cache_clear(vmx);
7281
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007282 vmcs12->launch_state = 1;
7283
7284 prepare_vmcs02(vcpu, vmcs12);
7285
7286 /*
7287 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
7288 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
7289 * returned as far as L1 is concerned. It will only return (and set
7290 * the success flag) when L2 exits (see nested_vmx_vmexit()).
7291 */
7292 return 1;
7293}
7294
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007295/*
7296 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
7297 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
7298 * This function returns the new value we should put in vmcs12.guest_cr0.
7299 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
7300 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
7301 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
7302 * didn't trap the bit, because if L1 did, so would L0).
7303 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
7304 * been modified by L2, and L1 knows it. So just leave the old value of
7305 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
7306 * isn't relevant, because if L0 traps this bit it can set it to anything.
7307 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
7308 * changed these bits, and therefore they need to be updated, but L0
7309 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
7310 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
7311 */
7312static inline unsigned long
7313vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7314{
7315 return
7316 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
7317 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
7318 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
7319 vcpu->arch.cr0_guest_owned_bits));
7320}
7321
7322static inline unsigned long
7323vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7324{
7325 return
7326 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
7327 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
7328 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
7329 vcpu->arch.cr4_guest_owned_bits));
7330}
7331
7332/*
7333 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
7334 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
7335 * and this function updates it to reflect the changes to the guest state while
7336 * L2 was running (and perhaps made some exits which were handled directly by L0
7337 * without going back to L1), and to reflect the exit reason.
7338 * Note that we do not have to copy here all VMCS fields, just those that
7339 * could have changed by the L2 guest or the exit - i.e., the guest-state and
7340 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
7341 * which already writes to vmcs12 directly.
7342 */
Jan Kiszka733568f2013-02-23 15:07:47 +01007343static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007344{
7345 /* update guest state fields: */
7346 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
7347 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
7348
7349 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
7350 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
7351 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
7352 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
7353
7354 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
7355 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
7356 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
7357 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
7358 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
7359 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
7360 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
7361 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
7362 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
7363 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
7364 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
7365 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
7366 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
7367 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
7368 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
7369 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
7370 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
7371 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
7372 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
7373 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
7374 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
7375 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
7376 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
7377 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
7378 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
7379 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
7380 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
7381 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
7382 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
7383 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
7384 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
7385 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
7386 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
7387 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
7388 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
7389 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
7390
7391 vmcs12->guest_activity_state = vmcs_read32(GUEST_ACTIVITY_STATE);
7392 vmcs12->guest_interruptibility_info =
7393 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
7394 vmcs12->guest_pending_dbg_exceptions =
7395 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
7396
Jan Kiszkac18911a2013-03-13 16:06:41 +01007397 vmcs12->vm_entry_controls =
7398 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
7399 (vmcs_read32(VM_ENTRY_CONTROLS) & VM_ENTRY_IA32E_MODE);
7400
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007401 /* TODO: These cannot have changed unless we have MSR bitmaps and
7402 * the relevant bit asks not to trap the change */
7403 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
7404 if (vmcs12->vm_entry_controls & VM_EXIT_SAVE_IA32_PAT)
7405 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
7406 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
7407 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
7408 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
7409
7410 /* update exit information fields: */
7411
Jan Kiszka957c8972013-02-24 14:11:34 +01007412 vmcs12->vm_exit_reason = to_vmx(vcpu)->exit_reason;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007413 vmcs12->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7414
7415 vmcs12->vm_exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7416 vmcs12->vm_exit_intr_error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka44ceb9d2013-02-20 14:02:23 +01007417 vmcs12->idt_vectoring_info_field = to_vmx(vcpu)->idt_vectoring_info;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007418 vmcs12->idt_vectoring_error_code =
7419 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7420 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
7421 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7422
7423 /* clear vm-entry fields which are to be cleared on exit */
7424 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
7425 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
7426}
7427
7428/*
7429 * A part of what we need to when the nested L2 guest exits and we want to
7430 * run its L1 parent, is to reset L1's guest state to the host state specified
7431 * in vmcs12.
7432 * This function is to be called not only on normal nested exit, but also on
7433 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
7434 * Failures During or After Loading Guest State").
7435 * This function should be called when the active VMCS is L1's (vmcs01).
7436 */
Jan Kiszka733568f2013-02-23 15:07:47 +01007437static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
7438 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007439{
7440 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
7441 vcpu->arch.efer = vmcs12->host_ia32_efer;
7442 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
7443 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
7444 else
7445 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
7446 vmx_set_efer(vcpu, vcpu->arch.efer);
7447
7448 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
7449 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
Jan Kiszkac4627c72013-03-03 20:47:11 +01007450 vmx_set_rflags(vcpu, X86_EFLAGS_BIT1);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007451 /*
7452 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
7453 * actually changed, because it depends on the current state of
7454 * fpu_active (which may have changed).
7455 * Note that vmx_set_cr0 refers to efer set above.
7456 */
7457 kvm_set_cr0(vcpu, vmcs12->host_cr0);
7458 /*
7459 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
7460 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
7461 * but we also need to update cr0_guest_host_mask and exception_bitmap.
7462 */
7463 update_exception_bitmap(vcpu);
7464 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
7465 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
7466
7467 /*
7468 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
7469 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
7470 */
7471 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
7472 kvm_set_cr4(vcpu, vmcs12->host_cr4);
7473
7474 /* shadow page tables on either EPT or shadow page tables */
7475 kvm_set_cr3(vcpu, vmcs12->host_cr3);
7476 kvm_mmu_reset_context(vcpu);
7477
7478 if (enable_vpid) {
7479 /*
7480 * Trivially support vpid by letting L2s share their parent
7481 * L1's vpid. TODO: move to a more elaborate solution, giving
7482 * each L2 its own vpid and exposing the vpid feature to L1.
7483 */
7484 vmx_flush_tlb(vcpu);
7485 }
7486
7487
7488 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
7489 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
7490 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
7491 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
7492 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
7493 vmcs_writel(GUEST_TR_BASE, vmcs12->host_tr_base);
7494 vmcs_writel(GUEST_GS_BASE, vmcs12->host_gs_base);
7495 vmcs_writel(GUEST_FS_BASE, vmcs12->host_fs_base);
7496 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->host_es_selector);
7497 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->host_cs_selector);
7498 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->host_ss_selector);
7499 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->host_ds_selector);
7500 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->host_fs_selector);
7501 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->host_gs_selector);
7502 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->host_tr_selector);
7503
7504 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT)
7505 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
7506 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
7507 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
7508 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +01007509
7510 kvm_set_dr(vcpu, 7, 0x400);
7511 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007512}
7513
7514/*
7515 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
7516 * and modify vmcs12 to make it see what it would expect to see there if
7517 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
7518 */
7519static void nested_vmx_vmexit(struct kvm_vcpu *vcpu)
7520{
7521 struct vcpu_vmx *vmx = to_vmx(vcpu);
7522 int cpu;
7523 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7524
7525 leave_guest_mode(vcpu);
7526 prepare_vmcs12(vcpu, vmcs12);
7527
7528 cpu = get_cpu();
7529 vmx->loaded_vmcs = &vmx->vmcs01;
7530 vmx_vcpu_put(vcpu);
7531 vmx_vcpu_load(vcpu, cpu);
7532 vcpu->cpu = cpu;
7533 put_cpu();
7534
Jan Kiszka36c3cc42013-02-23 22:35:37 +01007535 vmx_segment_cache_clear(vmx);
7536
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007537 /* if no vmcs02 cache requested, remove the one we used */
7538 if (VMCS02_POOL_SIZE == 0)
7539 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
7540
7541 load_vmcs12_host_state(vcpu, vmcs12);
7542
Nadav Har'El27fc51b2011-08-02 15:54:52 +03007543 /* Update TSC_OFFSET if TSC was changed while L2 ran */
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007544 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
7545
7546 /* This is needed for same reason as it was needed in prepare_vmcs02 */
7547 vmx->host_rsp = 0;
7548
7549 /* Unpin physical memory we referred to in vmcs02 */
7550 if (vmx->nested.apic_access_page) {
7551 nested_release_page(vmx->nested.apic_access_page);
7552 vmx->nested.apic_access_page = 0;
7553 }
7554
7555 /*
7556 * Exiting from L2 to L1, we're now back to L1 which thinks it just
7557 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
7558 * success or failure flag accordingly.
7559 */
7560 if (unlikely(vmx->fail)) {
7561 vmx->fail = 0;
7562 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
7563 } else
7564 nested_vmx_succeed(vcpu);
7565}
7566
Nadav Har'El7c177932011-05-25 23:12:04 +03007567/*
7568 * L1's failure to enter L2 is a subset of a normal exit, as explained in
7569 * 23.7 "VM-entry failures during or after loading guest state" (this also
7570 * lists the acceptable exit-reason and exit-qualification parameters).
7571 * It should only be called before L2 actually succeeded to run, and when
7572 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
7573 */
7574static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
7575 struct vmcs12 *vmcs12,
7576 u32 reason, unsigned long qualification)
7577{
7578 load_vmcs12_host_state(vcpu, vmcs12);
7579 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
7580 vmcs12->exit_qualification = qualification;
7581 nested_vmx_succeed(vcpu);
7582}
7583
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007584static int vmx_check_intercept(struct kvm_vcpu *vcpu,
7585 struct x86_instruction_info *info,
7586 enum x86_intercept_stage stage)
7587{
7588 return X86EMUL_CONTINUE;
7589}
7590
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03007591static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007592 .cpu_has_kvm_support = cpu_has_kvm_support,
7593 .disabled_by_bios = vmx_disabled_by_bios,
7594 .hardware_setup = hardware_setup,
7595 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03007596 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007597 .hardware_enable = hardware_enable,
7598 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08007599 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007600
7601 .vcpu_create = vmx_create_vcpu,
7602 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007603 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007604
Avi Kivity04d2cc72007-09-10 18:10:54 +03007605 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007606 .vcpu_load = vmx_vcpu_load,
7607 .vcpu_put = vmx_vcpu_put,
7608
Jan Kiszkac8639012012-09-21 05:42:55 +02007609 .update_db_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007610 .get_msr = vmx_get_msr,
7611 .set_msr = vmx_set_msr,
7612 .get_segment_base = vmx_get_segment_base,
7613 .get_segment = vmx_get_segment,
7614 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007615 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007616 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02007617 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02007618 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03007619 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007620 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007621 .set_cr3 = vmx_set_cr3,
7622 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007623 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007624 .get_idt = vmx_get_idt,
7625 .set_idt = vmx_set_idt,
7626 .get_gdt = vmx_get_gdt,
7627 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03007628 .set_dr7 = vmx_set_dr7,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007629 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007630 .get_rflags = vmx_get_rflags,
7631 .set_rflags = vmx_set_rflags,
Avi Kivityebcbab42010-02-07 11:56:52 +02007632 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02007633 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007634
7635 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007636
Avi Kivity6aa8b732006-12-10 02:21:36 -08007637 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02007638 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007639 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007640 .set_interrupt_shadow = vmx_set_interrupt_shadow,
7641 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007642 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007643 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007644 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007645 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007646 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007647 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007648 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007649 .get_nmi_mask = vmx_get_nmi_mask,
7650 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007651 .enable_nmi_window = enable_nmi_window,
7652 .enable_irq_window = enable_irq_window,
7653 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08007654 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007655 .vm_has_apicv = vmx_vm_has_apicv,
7656 .load_eoi_exitmap = vmx_load_eoi_exitmap,
7657 .hwapic_irr_update = vmx_hwapic_irr_update,
7658 .hwapic_isr_update = vmx_hwapic_isr_update,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007659
Izik Eiduscbc94022007-10-25 00:29:55 +02007660 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08007661 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007662 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007663
Avi Kivity586f9602010-11-18 13:09:54 +02007664 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007665
Sheng Yang17cc3932010-01-05 19:02:27 +08007666 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08007667
7668 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007669
7670 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00007671 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007672
7673 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007674
7675 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007676
Joerg Roedel4051b182011-03-25 09:44:49 +01007677 .set_tsc_khz = vmx_set_tsc_khz,
Will Auldba904632012-11-29 12:42:50 -08007678 .read_tsc_offset = vmx_read_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007679 .write_tsc_offset = vmx_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10007680 .adjust_tsc_offset = vmx_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01007681 .compute_tsc_offset = vmx_compute_tsc_offset,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03007682 .read_l1_tsc = vmx_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007683
7684 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007685
7686 .check_intercept = vmx_check_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007687};
7688
7689static int __init vmx_init(void)
7690{
Yang Zhang8d146952013-01-25 10:18:50 +08007691 int r, i, msr;
Avi Kivity26bb0982009-09-07 11:14:12 +03007692
7693 rdmsrl_safe(MSR_EFER, &host_efer);
7694
7695 for (i = 0; i < NR_VMX_MSR; ++i)
7696 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03007697
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007698 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03007699 if (!vmx_io_bitmap_a)
7700 return -ENOMEM;
7701
Guo Chao2106a542012-06-15 11:31:56 +08007702 r = -ENOMEM;
7703
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007704 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08007705 if (!vmx_io_bitmap_b)
He, Qingfdef3ad2007-04-30 09:45:24 +03007706 goto out;
He, Qingfdef3ad2007-04-30 09:45:24 +03007707
Avi Kivity58972972009-02-24 22:26:47 +02007708 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08007709 if (!vmx_msr_bitmap_legacy)
Sheng Yang25c5f222008-03-28 13:18:56 +08007710 goto out1;
Guo Chao2106a542012-06-15 11:31:56 +08007711
Yang Zhang8d146952013-01-25 10:18:50 +08007712 vmx_msr_bitmap_legacy_x2apic =
7713 (unsigned long *)__get_free_page(GFP_KERNEL);
7714 if (!vmx_msr_bitmap_legacy_x2apic)
7715 goto out2;
Sheng Yang25c5f222008-03-28 13:18:56 +08007716
Avi Kivity58972972009-02-24 22:26:47 +02007717 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08007718 if (!vmx_msr_bitmap_longmode)
Yang Zhang8d146952013-01-25 10:18:50 +08007719 goto out3;
Guo Chao2106a542012-06-15 11:31:56 +08007720
Yang Zhang8d146952013-01-25 10:18:50 +08007721 vmx_msr_bitmap_longmode_x2apic =
7722 (unsigned long *)__get_free_page(GFP_KERNEL);
7723 if (!vmx_msr_bitmap_longmode_x2apic)
7724 goto out4;
Avi Kivity58972972009-02-24 22:26:47 +02007725
He, Qingfdef3ad2007-04-30 09:45:24 +03007726 /*
7727 * Allow direct access to the PC debug port (it is often used for I/O
7728 * delays, but the vmexits simply slow things down).
7729 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007730 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
7731 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03007732
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007733 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03007734
Avi Kivity58972972009-02-24 22:26:47 +02007735 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
7736 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08007737
Sheng Yang2384d2b2008-01-17 15:14:33 +08007738 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7739
Avi Kivity0ee75be2010-04-28 15:39:01 +03007740 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
7741 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03007742 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02007743 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08007744
Zhang Yanfei8f536b72012-12-06 23:43:34 +08007745#ifdef CONFIG_KEXEC
7746 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
7747 crash_vmclear_local_loaded_vmcss);
7748#endif
7749
Avi Kivity58972972009-02-24 22:26:47 +02007750 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
7751 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
7752 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
7753 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
7754 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
7755 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
Yang Zhang8d146952013-01-25 10:18:50 +08007756 memcpy(vmx_msr_bitmap_legacy_x2apic,
7757 vmx_msr_bitmap_legacy, PAGE_SIZE);
7758 memcpy(vmx_msr_bitmap_longmode_x2apic,
7759 vmx_msr_bitmap_longmode, PAGE_SIZE);
7760
Yang Zhangc7c9c562013-01-25 10:18:51 +08007761 if (enable_apicv_reg_vid) {
Yang Zhang8d146952013-01-25 10:18:50 +08007762 for (msr = 0x800; msr <= 0x8ff; msr++)
7763 vmx_disable_intercept_msr_read_x2apic(msr);
7764
7765 /* According SDM, in x2apic mode, the whole id reg is used.
7766 * But in KVM, it only use the highest eight bits. Need to
7767 * intercept it */
7768 vmx_enable_intercept_msr_read_x2apic(0x802);
7769 /* TMCCT */
7770 vmx_enable_intercept_msr_read_x2apic(0x839);
7771 /* TPR */
7772 vmx_disable_intercept_msr_write_x2apic(0x808);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007773 /* EOI */
7774 vmx_disable_intercept_msr_write_x2apic(0x80b);
7775 /* SELF-IPI */
7776 vmx_disable_intercept_msr_write_x2apic(0x83f);
Yang Zhang8d146952013-01-25 10:18:50 +08007777 }
He, Qingfdef3ad2007-04-30 09:45:24 +03007778
Avi Kivity089d0342009-03-23 18:26:32 +02007779 if (enable_ept) {
Xudong Hao3f6d8c82012-05-22 11:23:15 +08007780 kvm_mmu_set_mask_ptes(0ull,
7781 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
7782 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
7783 0ull, VMX_EPT_EXECUTABLE_MASK);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08007784 ept_set_mmio_spte_mask();
Sheng Yang5fdbcb92008-07-16 09:25:40 +08007785 kvm_enable_tdp();
7786 } else
7787 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08007788
He, Qingfdef3ad2007-04-30 09:45:24 +03007789 return 0;
7790
Yang Zhang8d146952013-01-25 10:18:50 +08007791out4:
Avi Kivity58972972009-02-24 22:26:47 +02007792 free_page((unsigned long)vmx_msr_bitmap_longmode);
Yang Zhang8d146952013-01-25 10:18:50 +08007793out3:
7794 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
Sheng Yang25c5f222008-03-28 13:18:56 +08007795out2:
Avi Kivity58972972009-02-24 22:26:47 +02007796 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03007797out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007798 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03007799out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007800 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03007801 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007802}
7803
7804static void __exit vmx_exit(void)
7805{
Yang Zhang8d146952013-01-25 10:18:50 +08007806 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
7807 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
Avi Kivity58972972009-02-24 22:26:47 +02007808 free_page((unsigned long)vmx_msr_bitmap_legacy);
7809 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007810 free_page((unsigned long)vmx_io_bitmap_b);
7811 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03007812
Zhang Yanfei8f536b72012-12-06 23:43:34 +08007813#ifdef CONFIG_KEXEC
7814 rcu_assign_pointer(crash_vmclear_loaded_vmcss, NULL);
7815 synchronize_rcu();
7816#endif
7817
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08007818 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08007819}
7820
7821module_init(vmx_init)
7822module_exit(vmx_exit)