blob: a5fd47e4abfc5b2984e51cd072ef0f6e13583d0e [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>
Jan Kiszkaf41245002014-03-07 20:03:13 +010034#include <linux/hrtimer.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030035#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030036#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040037
Avi Kivity6aa8b732006-12-10 02:21:36 -080038#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080039#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020040#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020041#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080042#include <asm/mce.h>
Dexuan Cui2acf9232010-06-10 11:27:12 +080043#include <asm/i387.h>
44#include <asm/xcr.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020045#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010046#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080047#include <asm/kexec.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080048
Marcelo Tosatti229456f2009-06-17 09:22:14 -030049#include "trace.h"
50
Avi Kivity4ecac3f2008-05-13 13:23:38 +030051#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040052#define __ex_clear(x, reg) \
53 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030054
Avi Kivity6aa8b732006-12-10 02:21:36 -080055MODULE_AUTHOR("Qumranet");
56MODULE_LICENSE("GPL");
57
Josh Triplette9bda3b2012-03-20 23:33:51 -070058static const struct x86_cpu_id vmx_cpu_id[] = {
59 X86_FEATURE_MATCH(X86_FEATURE_VMX),
60 {}
61};
62MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
63
Rusty Russell476bc002012-01-13 09:32:18 +103064static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020065module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080066
Rusty Russell476bc002012-01-13 09:32:18 +103067static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020068module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020069
Rusty Russell476bc002012-01-13 09:32:18 +103070static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020071module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080072
Rusty Russell476bc002012-01-13 09:32:18 +103073static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070074module_param_named(unrestricted_guest,
75 enable_unrestricted_guest, bool, S_IRUGO);
76
Xudong Hao83c3a332012-05-28 19:33:35 +080077static bool __read_mostly enable_ept_ad_bits = 1;
78module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
79
Avi Kivitya27685c2012-06-12 20:30:18 +030080static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020081module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030082
Rusty Russell476bc002012-01-13 09:32:18 +103083static bool __read_mostly vmm_exclusive = 1;
Dongxiao Xub923e622010-05-11 18:29:45 +080084module_param(vmm_exclusive, bool, S_IRUGO);
85
Rusty Russell476bc002012-01-13 09:32:18 +103086static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030087module_param(fasteoi, bool, S_IRUGO);
88
Yang Zhang5a717852013-04-11 19:25:16 +080089static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080090module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080091
Abel Gordonabc4fc52013-04-18 14:35:25 +030092static bool __read_mostly enable_shadow_vmcs = 1;
93module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +030094/*
95 * If nested=1, nested virtualization is supported, i.e., guests may use
96 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
97 * use VMX instructions.
98 */
Rusty Russell476bc002012-01-13 09:32:18 +103099static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300100module_param(nested, bool, S_IRUGO);
101
Gleb Natapov50378782013-02-04 16:00:28 +0200102#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
103#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200104#define KVM_VM_CR0_ALWAYS_ON \
105 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200106#define KVM_CR4_GUEST_OWNED_BITS \
107 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
108 | X86_CR4_OSXMMEXCPT)
109
Avi Kivitycdc0e242009-12-06 17:21:14 +0200110#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
111#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
112
Avi Kivity78ac8b42010-04-08 18:19:35 +0300113#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
114
Jan Kiszkaf41245002014-03-07 20:03:13 +0100115#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
116
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800117/*
118 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
119 * ple_gap: upper bound on the amount of time between two successive
120 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500121 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800122 * ple_window: upper bound on the amount of time a guest is allowed to execute
123 * in a PAUSE loop. Tests indicate that most spinlocks are held for
124 * less than 2^12 cycles
125 * Time is measured based on a counter that runs at the same rate as the TSC,
126 * refer SDM volume 3b section 21.6.13 & 22.1.3.
127 */
Rik van Riel00c25bc2011-01-04 09:51:33 -0500128#define KVM_VMX_DEFAULT_PLE_GAP 128
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800129#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
130static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
131module_param(ple_gap, int, S_IRUGO);
132
133static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
134module_param(ple_window, int, S_IRUGO);
135
Avi Kivity83287ea422012-09-16 15:10:57 +0300136extern const ulong vmx_return;
137
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200138#define NR_AUTOLOAD_MSRS 8
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300139#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300140
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400141struct vmcs {
142 u32 revision_id;
143 u32 abort;
144 char data[0];
145};
146
Nadav Har'Eld462b812011-05-24 15:26:10 +0300147/*
148 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
149 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
150 * loaded on this CPU (so we can clear them if the CPU goes down).
151 */
152struct loaded_vmcs {
153 struct vmcs *vmcs;
154 int cpu;
155 int launched;
156 struct list_head loaded_vmcss_on_cpu_link;
157};
158
Avi Kivity26bb0982009-09-07 11:14:12 +0300159struct shared_msr_entry {
160 unsigned index;
161 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200162 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300163};
164
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300165/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300166 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
167 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
168 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
169 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
170 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
171 * More than one of these structures may exist, if L1 runs multiple L2 guests.
172 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
173 * underlying hardware which will be used to run L2.
174 * This structure is packed to ensure that its layout is identical across
175 * machines (necessary for live migration).
176 * If there are changes in this struct, VMCS12_REVISION must be changed.
177 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300178typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300179struct __packed vmcs12 {
180 /* According to the Intel spec, a VMCS region must start with the
181 * following two fields. Then follow implementation-specific data.
182 */
183 u32 revision_id;
184 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300185
Nadav Har'El27d6c862011-05-25 23:06:59 +0300186 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
187 u32 padding[7]; /* room for future expansion */
188
Nadav Har'El22bd0352011-05-25 23:05:57 +0300189 u64 io_bitmap_a;
190 u64 io_bitmap_b;
191 u64 msr_bitmap;
192 u64 vm_exit_msr_store_addr;
193 u64 vm_exit_msr_load_addr;
194 u64 vm_entry_msr_load_addr;
195 u64 tsc_offset;
196 u64 virtual_apic_page_addr;
197 u64 apic_access_addr;
198 u64 ept_pointer;
199 u64 guest_physical_address;
200 u64 vmcs_link_pointer;
201 u64 guest_ia32_debugctl;
202 u64 guest_ia32_pat;
203 u64 guest_ia32_efer;
204 u64 guest_ia32_perf_global_ctrl;
205 u64 guest_pdptr0;
206 u64 guest_pdptr1;
207 u64 guest_pdptr2;
208 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100209 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300210 u64 host_ia32_pat;
211 u64 host_ia32_efer;
212 u64 host_ia32_perf_global_ctrl;
213 u64 padding64[8]; /* room for future expansion */
214 /*
215 * To allow migration of L1 (complete with its L2 guests) between
216 * machines of different natural widths (32 or 64 bit), we cannot have
217 * unsigned long fields with no explict size. We use u64 (aliased
218 * natural_width) instead. Luckily, x86 is little-endian.
219 */
220 natural_width cr0_guest_host_mask;
221 natural_width cr4_guest_host_mask;
222 natural_width cr0_read_shadow;
223 natural_width cr4_read_shadow;
224 natural_width cr3_target_value0;
225 natural_width cr3_target_value1;
226 natural_width cr3_target_value2;
227 natural_width cr3_target_value3;
228 natural_width exit_qualification;
229 natural_width guest_linear_address;
230 natural_width guest_cr0;
231 natural_width guest_cr3;
232 natural_width guest_cr4;
233 natural_width guest_es_base;
234 natural_width guest_cs_base;
235 natural_width guest_ss_base;
236 natural_width guest_ds_base;
237 natural_width guest_fs_base;
238 natural_width guest_gs_base;
239 natural_width guest_ldtr_base;
240 natural_width guest_tr_base;
241 natural_width guest_gdtr_base;
242 natural_width guest_idtr_base;
243 natural_width guest_dr7;
244 natural_width guest_rsp;
245 natural_width guest_rip;
246 natural_width guest_rflags;
247 natural_width guest_pending_dbg_exceptions;
248 natural_width guest_sysenter_esp;
249 natural_width guest_sysenter_eip;
250 natural_width host_cr0;
251 natural_width host_cr3;
252 natural_width host_cr4;
253 natural_width host_fs_base;
254 natural_width host_gs_base;
255 natural_width host_tr_base;
256 natural_width host_gdtr_base;
257 natural_width host_idtr_base;
258 natural_width host_ia32_sysenter_esp;
259 natural_width host_ia32_sysenter_eip;
260 natural_width host_rsp;
261 natural_width host_rip;
262 natural_width paddingl[8]; /* room for future expansion */
263 u32 pin_based_vm_exec_control;
264 u32 cpu_based_vm_exec_control;
265 u32 exception_bitmap;
266 u32 page_fault_error_code_mask;
267 u32 page_fault_error_code_match;
268 u32 cr3_target_count;
269 u32 vm_exit_controls;
270 u32 vm_exit_msr_store_count;
271 u32 vm_exit_msr_load_count;
272 u32 vm_entry_controls;
273 u32 vm_entry_msr_load_count;
274 u32 vm_entry_intr_info_field;
275 u32 vm_entry_exception_error_code;
276 u32 vm_entry_instruction_len;
277 u32 tpr_threshold;
278 u32 secondary_vm_exec_control;
279 u32 vm_instruction_error;
280 u32 vm_exit_reason;
281 u32 vm_exit_intr_info;
282 u32 vm_exit_intr_error_code;
283 u32 idt_vectoring_info_field;
284 u32 idt_vectoring_error_code;
285 u32 vm_exit_instruction_len;
286 u32 vmx_instruction_info;
287 u32 guest_es_limit;
288 u32 guest_cs_limit;
289 u32 guest_ss_limit;
290 u32 guest_ds_limit;
291 u32 guest_fs_limit;
292 u32 guest_gs_limit;
293 u32 guest_ldtr_limit;
294 u32 guest_tr_limit;
295 u32 guest_gdtr_limit;
296 u32 guest_idtr_limit;
297 u32 guest_es_ar_bytes;
298 u32 guest_cs_ar_bytes;
299 u32 guest_ss_ar_bytes;
300 u32 guest_ds_ar_bytes;
301 u32 guest_fs_ar_bytes;
302 u32 guest_gs_ar_bytes;
303 u32 guest_ldtr_ar_bytes;
304 u32 guest_tr_ar_bytes;
305 u32 guest_interruptibility_info;
306 u32 guest_activity_state;
307 u32 guest_sysenter_cs;
308 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100309 u32 vmx_preemption_timer_value;
310 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300311 u16 virtual_processor_id;
312 u16 guest_es_selector;
313 u16 guest_cs_selector;
314 u16 guest_ss_selector;
315 u16 guest_ds_selector;
316 u16 guest_fs_selector;
317 u16 guest_gs_selector;
318 u16 guest_ldtr_selector;
319 u16 guest_tr_selector;
320 u16 host_es_selector;
321 u16 host_cs_selector;
322 u16 host_ss_selector;
323 u16 host_ds_selector;
324 u16 host_fs_selector;
325 u16 host_gs_selector;
326 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300327};
328
329/*
330 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
331 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
332 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
333 */
334#define VMCS12_REVISION 0x11e57ed0
335
336/*
337 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
338 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
339 * current implementation, 4K are reserved to avoid future complications.
340 */
341#define VMCS12_SIZE 0x1000
342
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300343/* Used to remember the last vmcs02 used for some recently used vmcs12s */
344struct vmcs02_list {
345 struct list_head list;
346 gpa_t vmptr;
347 struct loaded_vmcs vmcs02;
348};
349
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300350/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300351 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
352 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
353 */
354struct nested_vmx {
355 /* Has the level1 guest done vmxon? */
356 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400357 gpa_t vmxon_ptr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300358
359 /* The guest-physical address of the current VMCS L1 keeps for L2 */
360 gpa_t current_vmptr;
361 /* The host-usable pointer to the above */
362 struct page *current_vmcs12_page;
363 struct vmcs12 *current_vmcs12;
Abel Gordon8de48832013-04-18 14:37:25 +0300364 struct vmcs *current_shadow_vmcs;
Abel Gordon012f83c2013-04-18 14:39:25 +0300365 /*
366 * Indicates if the shadow vmcs must be updated with the
367 * data hold by vmcs12
368 */
369 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300370
371 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
372 struct list_head vmcs02_pool;
373 int vmcs02_num;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300374 u64 vmcs01_tsc_offset;
Nadav Har'El644d7112011-05-25 23:12:35 +0300375 /* L2 must run next, and mustn't decide to exit to L1. */
376 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300377 /*
378 * Guest pages referred to in vmcs02 with host-physical pointers, so
379 * we must keep them pinned while L2 runs.
380 */
381 struct page *apic_access_page;
Nadav Har'Elb3897a42013-07-08 19:12:35 +0800382 u64 msr_ia32_feature_control;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100383
384 struct hrtimer preemption_timer;
385 bool preemption_timer_expired;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300386};
387
Yang Zhang01e439b2013-04-11 19:25:12 +0800388#define POSTED_INTR_ON 0
389/* Posted-Interrupt Descriptor */
390struct pi_desc {
391 u32 pir[8]; /* Posted interrupt requested */
392 u32 control; /* bit 0 of control is outstanding notification bit */
393 u32 rsvd[7];
394} __aligned(64);
395
Yang Zhanga20ed542013-04-11 19:25:15 +0800396static bool pi_test_and_set_on(struct pi_desc *pi_desc)
397{
398 return test_and_set_bit(POSTED_INTR_ON,
399 (unsigned long *)&pi_desc->control);
400}
401
402static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
403{
404 return test_and_clear_bit(POSTED_INTR_ON,
405 (unsigned long *)&pi_desc->control);
406}
407
408static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
409{
410 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
411}
412
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400413struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000414 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300415 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300416 u8 fail;
Avi Kivity69c73022011-03-07 15:26:44 +0200417 u8 cpl;
Avi Kivity9d58b932011-03-07 16:52:07 +0200418 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300419 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200420 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200421 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300422 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400423 int nmsrs;
424 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800425 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400426#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300427 u64 msr_host_kernel_gs_base;
428 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400429#endif
Gleb Natapov2961e8762013-11-25 15:37:13 +0200430 u32 vm_entry_controls_shadow;
431 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300432 /*
433 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
434 * non-nested (L1) guest, it always points to vmcs01. For a nested
435 * guest (L2), it points to a different VMCS.
436 */
437 struct loaded_vmcs vmcs01;
438 struct loaded_vmcs *loaded_vmcs;
439 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300440 struct msr_autoload {
441 unsigned nr;
442 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
443 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
444 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400445 struct {
446 int loaded;
447 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300448#ifdef CONFIG_X86_64
449 u16 ds_sel, es_sel;
450#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200451 int gs_ldt_reload_needed;
452 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000453 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400454 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200455 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300456 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300457 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300458 struct kvm_segment segs[8];
459 } rmode;
460 struct {
461 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300462 struct kvm_save_segment {
463 u16 selector;
464 unsigned long base;
465 u32 limit;
466 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300467 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300468 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800469 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300470 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200471
472 /* Support for vnmi-less CPUs */
473 int soft_vnmi_blocked;
474 ktime_t entry_time;
475 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800476 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800477
478 bool rdtscp_enabled;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300479
Yang Zhang01e439b2013-04-11 19:25:12 +0800480 /* Posted interrupt descriptor */
481 struct pi_desc pi_desc;
482
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300483 /* Support for a guest hypervisor (nested VMX) */
484 struct nested_vmx nested;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400485};
486
Avi Kivity2fb92db2011-04-27 19:42:18 +0300487enum segment_cache_field {
488 SEG_FIELD_SEL = 0,
489 SEG_FIELD_BASE = 1,
490 SEG_FIELD_LIMIT = 2,
491 SEG_FIELD_AR = 3,
492
493 SEG_FIELD_NR = 4
494};
495
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400496static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
497{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000498 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400499}
500
Nadav Har'El22bd0352011-05-25 23:05:57 +0300501#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
502#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
503#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
504 [number##_HIGH] = VMCS12_OFFSET(name)+4
505
Abel Gordon4607c2d2013-04-18 14:35:55 +0300506
507static const unsigned long shadow_read_only_fields[] = {
508 /*
509 * We do NOT shadow fields that are modified when L0
510 * traps and emulates any vmx instruction (e.g. VMPTRLD,
511 * VMXON...) executed by L1.
512 * For example, VM_INSTRUCTION_ERROR is read
513 * by L1 if a vmx instruction fails (part of the error path).
514 * Note the code assumes this logic. If for some reason
515 * we start shadowing these fields then we need to
516 * force a shadow sync when L0 emulates vmx instructions
517 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
518 * by nested_vmx_failValid)
519 */
520 VM_EXIT_REASON,
521 VM_EXIT_INTR_INFO,
522 VM_EXIT_INSTRUCTION_LEN,
523 IDT_VECTORING_INFO_FIELD,
524 IDT_VECTORING_ERROR_CODE,
525 VM_EXIT_INTR_ERROR_CODE,
526 EXIT_QUALIFICATION,
527 GUEST_LINEAR_ADDRESS,
528 GUEST_PHYSICAL_ADDRESS
529};
530static const int max_shadow_read_only_fields =
531 ARRAY_SIZE(shadow_read_only_fields);
532
533static const unsigned long shadow_read_write_fields[] = {
534 GUEST_RIP,
535 GUEST_RSP,
536 GUEST_CR0,
537 GUEST_CR3,
538 GUEST_CR4,
539 GUEST_INTERRUPTIBILITY_INFO,
540 GUEST_RFLAGS,
541 GUEST_CS_SELECTOR,
542 GUEST_CS_AR_BYTES,
543 GUEST_CS_LIMIT,
544 GUEST_CS_BASE,
545 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100546 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300547 CR0_GUEST_HOST_MASK,
548 CR0_READ_SHADOW,
549 CR4_READ_SHADOW,
550 TSC_OFFSET,
551 EXCEPTION_BITMAP,
552 CPU_BASED_VM_EXEC_CONTROL,
553 VM_ENTRY_EXCEPTION_ERROR_CODE,
554 VM_ENTRY_INTR_INFO_FIELD,
555 VM_ENTRY_INSTRUCTION_LEN,
556 VM_ENTRY_EXCEPTION_ERROR_CODE,
557 HOST_FS_BASE,
558 HOST_GS_BASE,
559 HOST_FS_SELECTOR,
560 HOST_GS_SELECTOR
561};
562static const int max_shadow_read_write_fields =
563 ARRAY_SIZE(shadow_read_write_fields);
564
Mathias Krause772e0312012-08-30 01:30:19 +0200565static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300566 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
567 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
568 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
569 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
570 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
571 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
572 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
573 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
574 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
575 FIELD(HOST_ES_SELECTOR, host_es_selector),
576 FIELD(HOST_CS_SELECTOR, host_cs_selector),
577 FIELD(HOST_SS_SELECTOR, host_ss_selector),
578 FIELD(HOST_DS_SELECTOR, host_ds_selector),
579 FIELD(HOST_FS_SELECTOR, host_fs_selector),
580 FIELD(HOST_GS_SELECTOR, host_gs_selector),
581 FIELD(HOST_TR_SELECTOR, host_tr_selector),
582 FIELD64(IO_BITMAP_A, io_bitmap_a),
583 FIELD64(IO_BITMAP_B, io_bitmap_b),
584 FIELD64(MSR_BITMAP, msr_bitmap),
585 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
586 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
587 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
588 FIELD64(TSC_OFFSET, tsc_offset),
589 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
590 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
591 FIELD64(EPT_POINTER, ept_pointer),
592 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
593 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
594 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
595 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
596 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
597 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
598 FIELD64(GUEST_PDPTR0, guest_pdptr0),
599 FIELD64(GUEST_PDPTR1, guest_pdptr1),
600 FIELD64(GUEST_PDPTR2, guest_pdptr2),
601 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100602 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300603 FIELD64(HOST_IA32_PAT, host_ia32_pat),
604 FIELD64(HOST_IA32_EFER, host_ia32_efer),
605 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
606 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
607 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
608 FIELD(EXCEPTION_BITMAP, exception_bitmap),
609 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
610 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
611 FIELD(CR3_TARGET_COUNT, cr3_target_count),
612 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
613 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
614 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
615 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
616 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
617 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
618 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
619 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
620 FIELD(TPR_THRESHOLD, tpr_threshold),
621 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
622 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
623 FIELD(VM_EXIT_REASON, vm_exit_reason),
624 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
625 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
626 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
627 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
628 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
629 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
630 FIELD(GUEST_ES_LIMIT, guest_es_limit),
631 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
632 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
633 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
634 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
635 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
636 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
637 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
638 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
639 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
640 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
641 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
642 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
643 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
644 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
645 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
646 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
647 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
648 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
649 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
650 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
651 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100652 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300653 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
654 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
655 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
656 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
657 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
658 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
659 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
660 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
661 FIELD(EXIT_QUALIFICATION, exit_qualification),
662 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
663 FIELD(GUEST_CR0, guest_cr0),
664 FIELD(GUEST_CR3, guest_cr3),
665 FIELD(GUEST_CR4, guest_cr4),
666 FIELD(GUEST_ES_BASE, guest_es_base),
667 FIELD(GUEST_CS_BASE, guest_cs_base),
668 FIELD(GUEST_SS_BASE, guest_ss_base),
669 FIELD(GUEST_DS_BASE, guest_ds_base),
670 FIELD(GUEST_FS_BASE, guest_fs_base),
671 FIELD(GUEST_GS_BASE, guest_gs_base),
672 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
673 FIELD(GUEST_TR_BASE, guest_tr_base),
674 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
675 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
676 FIELD(GUEST_DR7, guest_dr7),
677 FIELD(GUEST_RSP, guest_rsp),
678 FIELD(GUEST_RIP, guest_rip),
679 FIELD(GUEST_RFLAGS, guest_rflags),
680 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
681 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
682 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
683 FIELD(HOST_CR0, host_cr0),
684 FIELD(HOST_CR3, host_cr3),
685 FIELD(HOST_CR4, host_cr4),
686 FIELD(HOST_FS_BASE, host_fs_base),
687 FIELD(HOST_GS_BASE, host_gs_base),
688 FIELD(HOST_TR_BASE, host_tr_base),
689 FIELD(HOST_GDTR_BASE, host_gdtr_base),
690 FIELD(HOST_IDTR_BASE, host_idtr_base),
691 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
692 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
693 FIELD(HOST_RSP, host_rsp),
694 FIELD(HOST_RIP, host_rip),
695};
696static const int max_vmcs_field = ARRAY_SIZE(vmcs_field_to_offset_table);
697
698static inline short vmcs_field_to_offset(unsigned long field)
699{
700 if (field >= max_vmcs_field || vmcs_field_to_offset_table[field] == 0)
701 return -1;
702 return vmcs_field_to_offset_table[field];
703}
704
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300705static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
706{
707 return to_vmx(vcpu)->nested.current_vmcs12;
708}
709
710static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
711{
712 struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800713 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300714 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +0800715
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300716 return page;
717}
718
719static void nested_release_page(struct page *page)
720{
721 kvm_release_page_dirty(page);
722}
723
724static void nested_release_page_clean(struct page *page)
725{
726 kvm_release_page_clean(page);
727}
728
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300729static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800730static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800731static void kvm_cpu_vmxon(u64 addr);
732static void kvm_cpu_vmxoff(void);
Paolo Bonzini93c4adc2014-03-05 23:19:52 +0100733static bool vmx_mpx_supported(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200734static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300735static void vmx_set_segment(struct kvm_vcpu *vcpu,
736 struct kvm_segment *var, int seg);
737static void vmx_get_segment(struct kvm_vcpu *vcpu,
738 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200739static bool guest_state_valid(struct kvm_vcpu *vcpu);
740static u32 vmx_segment_access_rights(struct kvm_segment *var);
Yang Zhanga20ed542013-04-11 19:25:15 +0800741static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu);
Abel Gordonc3114422013-04-18 14:38:55 +0300742static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +0300743static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100744static bool vmx_mpx_supported(void);
Avi Kivity75880a02007-06-20 11:20:04 +0300745
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746static DEFINE_PER_CPU(struct vmcs *, vmxarea);
747static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300748/*
749 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
750 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
751 */
752static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300753static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800754
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200755static unsigned long *vmx_io_bitmap_a;
756static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200757static unsigned long *vmx_msr_bitmap_legacy;
758static unsigned long *vmx_msr_bitmap_longmode;
Yang Zhang8d146952013-01-25 10:18:50 +0800759static unsigned long *vmx_msr_bitmap_legacy_x2apic;
760static unsigned long *vmx_msr_bitmap_longmode_x2apic;
Abel Gordon4607c2d2013-04-18 14:35:55 +0300761static unsigned long *vmx_vmread_bitmap;
762static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300763
Avi Kivity110312c2010-12-21 12:54:20 +0200764static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200765static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200766
Sheng Yang2384d2b2008-01-17 15:14:33 +0800767static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
768static DEFINE_SPINLOCK(vmx_vpid_lock);
769
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300770static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800771 int size;
772 int order;
773 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300774 u32 pin_based_exec_ctrl;
775 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800776 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300777 u32 vmexit_ctrl;
778 u32 vmentry_ctrl;
779} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800780
Hannes Ederefff9e52008-11-28 17:02:06 +0100781static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800782 u32 ept;
783 u32 vpid;
784} vmx_capability;
785
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786#define VMX_SEGMENT_FIELD(seg) \
787 [VCPU_SREG_##seg] = { \
788 .selector = GUEST_##seg##_SELECTOR, \
789 .base = GUEST_##seg##_BASE, \
790 .limit = GUEST_##seg##_LIMIT, \
791 .ar_bytes = GUEST_##seg##_AR_BYTES, \
792 }
793
Mathias Krause772e0312012-08-30 01:30:19 +0200794static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795 unsigned selector;
796 unsigned base;
797 unsigned limit;
798 unsigned ar_bytes;
799} kvm_vmx_segment_fields[] = {
800 VMX_SEGMENT_FIELD(CS),
801 VMX_SEGMENT_FIELD(DS),
802 VMX_SEGMENT_FIELD(ES),
803 VMX_SEGMENT_FIELD(FS),
804 VMX_SEGMENT_FIELD(GS),
805 VMX_SEGMENT_FIELD(SS),
806 VMX_SEGMENT_FIELD(TR),
807 VMX_SEGMENT_FIELD(LDTR),
808};
809
Avi Kivity26bb0982009-09-07 11:14:12 +0300810static u64 host_efer;
811
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300812static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
813
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300814/*
Brian Gerst8c065852010-07-17 09:03:26 -0400815 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300816 * away by decrementing the array size.
817 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800819#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300820 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400822 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200824#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825
Gui Jianfeng31299942010-03-15 17:29:09 +0800826static inline bool is_page_fault(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827{
828 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
829 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100830 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800831}
832
Gui Jianfeng31299942010-03-15 17:29:09 +0800833static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300834{
835 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
836 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100837 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300838}
839
Gui Jianfeng31299942010-03-15 17:29:09 +0800840static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500841{
842 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
843 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100844 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500845}
846
Gui Jianfeng31299942010-03-15 17:29:09 +0800847static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848{
849 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
850 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
851}
852
Gui Jianfeng31299942010-03-15 17:29:09 +0800853static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +0800854{
855 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
856 INTR_INFO_VALID_MASK)) ==
857 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
858}
859
Gui Jianfeng31299942010-03-15 17:29:09 +0800860static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +0800861{
Sheng Yang04547152009-04-01 15:52:31 +0800862 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800863}
864
Gui Jianfeng31299942010-03-15 17:29:09 +0800865static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800866{
Sheng Yang04547152009-04-01 15:52:31 +0800867 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800868}
869
Gui Jianfeng31299942010-03-15 17:29:09 +0800870static inline bool vm_need_tpr_shadow(struct kvm *kvm)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800871{
Sheng Yang04547152009-04-01 15:52:31 +0800872 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800873}
874
Gui Jianfeng31299942010-03-15 17:29:09 +0800875static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800876{
Sheng Yang04547152009-04-01 15:52:31 +0800877 return vmcs_config.cpu_based_exec_ctrl &
878 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800879}
880
Avi Kivity774ead32007-12-26 13:57:04 +0200881static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800882{
Sheng Yang04547152009-04-01 15:52:31 +0800883 return vmcs_config.cpu_based_2nd_exec_ctrl &
884 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
885}
886
Yang Zhang8d146952013-01-25 10:18:50 +0800887static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
888{
889 return vmcs_config.cpu_based_2nd_exec_ctrl &
890 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
891}
892
Yang Zhang83d4c282013-01-25 10:18:49 +0800893static inline bool cpu_has_vmx_apic_register_virt(void)
894{
895 return vmcs_config.cpu_based_2nd_exec_ctrl &
896 SECONDARY_EXEC_APIC_REGISTER_VIRT;
897}
898
Yang Zhangc7c9c562013-01-25 10:18:51 +0800899static inline bool cpu_has_vmx_virtual_intr_delivery(void)
900{
901 return vmcs_config.cpu_based_2nd_exec_ctrl &
902 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
903}
904
Yang Zhang01e439b2013-04-11 19:25:12 +0800905static inline bool cpu_has_vmx_posted_intr(void)
906{
907 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
908}
909
910static inline bool cpu_has_vmx_apicv(void)
911{
912 return cpu_has_vmx_apic_register_virt() &&
913 cpu_has_vmx_virtual_intr_delivery() &&
914 cpu_has_vmx_posted_intr();
915}
916
Sheng Yang04547152009-04-01 15:52:31 +0800917static inline bool cpu_has_vmx_flexpriority(void)
918{
919 return cpu_has_vmx_tpr_shadow() &&
920 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800921}
922
Marcelo Tosattie7997942009-06-11 12:07:40 -0300923static inline bool cpu_has_vmx_ept_execute_only(void)
924{
Gui Jianfeng31299942010-03-15 17:29:09 +0800925 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300926}
927
928static inline bool cpu_has_vmx_eptp_uncacheable(void)
929{
Gui Jianfeng31299942010-03-15 17:29:09 +0800930 return vmx_capability.ept & VMX_EPTP_UC_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300931}
932
933static inline bool cpu_has_vmx_eptp_writeback(void)
934{
Gui Jianfeng31299942010-03-15 17:29:09 +0800935 return vmx_capability.ept & VMX_EPTP_WB_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300936}
937
938static inline bool cpu_has_vmx_ept_2m_page(void)
939{
Gui Jianfeng31299942010-03-15 17:29:09 +0800940 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300941}
942
Sheng Yang878403b2010-01-05 19:02:29 +0800943static inline bool cpu_has_vmx_ept_1g_page(void)
944{
Gui Jianfeng31299942010-03-15 17:29:09 +0800945 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +0800946}
947
Sheng Yang4bc9b982010-06-02 14:05:24 +0800948static inline bool cpu_has_vmx_ept_4levels(void)
949{
950 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
951}
952
Xudong Hao83c3a332012-05-28 19:33:35 +0800953static inline bool cpu_has_vmx_ept_ad_bits(void)
954{
955 return vmx_capability.ept & VMX_EPT_AD_BIT;
956}
957
Gui Jianfeng31299942010-03-15 17:29:09 +0800958static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800959{
Gui Jianfeng31299942010-03-15 17:29:09 +0800960 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800961}
962
Gui Jianfeng31299942010-03-15 17:29:09 +0800963static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800964{
Gui Jianfeng31299942010-03-15 17:29:09 +0800965 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800966}
967
Gui Jianfeng518c8ae2010-06-04 08:51:39 +0800968static inline bool cpu_has_vmx_invvpid_single(void)
969{
970 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
971}
972
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800973static inline bool cpu_has_vmx_invvpid_global(void)
974{
975 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
976}
977
Gui Jianfeng31299942010-03-15 17:29:09 +0800978static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800979{
Sheng Yang04547152009-04-01 15:52:31 +0800980 return vmcs_config.cpu_based_2nd_exec_ctrl &
981 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800982}
983
Gui Jianfeng31299942010-03-15 17:29:09 +0800984static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700985{
986 return vmcs_config.cpu_based_2nd_exec_ctrl &
987 SECONDARY_EXEC_UNRESTRICTED_GUEST;
988}
989
Gui Jianfeng31299942010-03-15 17:29:09 +0800990static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800991{
992 return vmcs_config.cpu_based_2nd_exec_ctrl &
993 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
994}
995
Gui Jianfeng31299942010-03-15 17:29:09 +0800996static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800997{
Gui Jianfeng6d3e4352010-01-29 15:36:59 +0800998 return flexpriority_enabled && irqchip_in_kernel(kvm);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800999}
1000
Gui Jianfeng31299942010-03-15 17:29:09 +08001001static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001002{
Sheng Yang04547152009-04-01 15:52:31 +08001003 return vmcs_config.cpu_based_2nd_exec_ctrl &
1004 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001005}
1006
Gui Jianfeng31299942010-03-15 17:29:09 +08001007static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001008{
1009 return vmcs_config.cpu_based_2nd_exec_ctrl &
1010 SECONDARY_EXEC_RDTSCP;
1011}
1012
Mao, Junjiead756a12012-07-02 01:18:48 +00001013static inline bool cpu_has_vmx_invpcid(void)
1014{
1015 return vmcs_config.cpu_based_2nd_exec_ctrl &
1016 SECONDARY_EXEC_ENABLE_INVPCID;
1017}
1018
Gui Jianfeng31299942010-03-15 17:29:09 +08001019static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +08001020{
1021 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1022}
1023
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001024static inline bool cpu_has_vmx_wbinvd_exit(void)
1025{
1026 return vmcs_config.cpu_based_2nd_exec_ctrl &
1027 SECONDARY_EXEC_WBINVD_EXITING;
1028}
1029
Abel Gordonabc4fc52013-04-18 14:35:25 +03001030static inline bool cpu_has_vmx_shadow_vmcs(void)
1031{
1032 u64 vmx_msr;
1033 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1034 /* check if the cpu supports writing r/o exit information fields */
1035 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1036 return false;
1037
1038 return vmcs_config.cpu_based_2nd_exec_ctrl &
1039 SECONDARY_EXEC_SHADOW_VMCS;
1040}
1041
Sheng Yang04547152009-04-01 15:52:31 +08001042static inline bool report_flexpriority(void)
1043{
1044 return flexpriority_enabled;
1045}
1046
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001047static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1048{
1049 return vmcs12->cpu_based_vm_exec_control & bit;
1050}
1051
1052static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1053{
1054 return (vmcs12->cpu_based_vm_exec_control &
1055 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1056 (vmcs12->secondary_vm_exec_control & bit);
1057}
1058
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001059static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001060{
1061 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1062}
1063
Jan Kiszkaf41245002014-03-07 20:03:13 +01001064static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1065{
1066 return vmcs12->pin_based_vm_exec_control &
1067 PIN_BASED_VMX_PREEMPTION_TIMER;
1068}
1069
Nadav Har'El155a97a2013-08-05 11:07:16 +03001070static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1071{
1072 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1073}
1074
Nadav Har'El644d7112011-05-25 23:12:35 +03001075static inline bool is_exception(u32 intr_info)
1076{
1077 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1078 == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
1079}
1080
Jan Kiszka533558b2014-01-04 18:47:20 +01001081static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1082 u32 exit_intr_info,
1083 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001084static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1085 struct vmcs12 *vmcs12,
1086 u32 reason, unsigned long qualification);
1087
Rusty Russell8b9cf982007-07-30 16:31:43 +10001088static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001089{
1090 int i;
1091
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001092 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001093 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001094 return i;
1095 return -1;
1096}
1097
Sheng Yang2384d2b2008-01-17 15:14:33 +08001098static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1099{
1100 struct {
1101 u64 vpid : 16;
1102 u64 rsvd : 48;
1103 u64 gva;
1104 } operand = { vpid, 0, gva };
1105
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001106 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001107 /* CF==1 or ZF==1 --> rc = -1 */
1108 "; ja 1f ; ud2 ; 1:"
1109 : : "a"(&operand), "c"(ext) : "cc", "memory");
1110}
1111
Sheng Yang14394422008-04-28 12:24:45 +08001112static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1113{
1114 struct {
1115 u64 eptp, gpa;
1116 } operand = {eptp, gpa};
1117
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001118 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001119 /* CF==1 or ZF==1 --> rc = -1 */
1120 "; ja 1f ; ud2 ; 1:\n"
1121 : : "a" (&operand), "c" (ext) : "cc", "memory");
1122}
1123
Avi Kivity26bb0982009-09-07 11:14:12 +03001124static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001125{
1126 int i;
1127
Rusty Russell8b9cf982007-07-30 16:31:43 +10001128 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001129 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001130 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001131 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001132}
1133
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134static void vmcs_clear(struct vmcs *vmcs)
1135{
1136 u64 phys_addr = __pa(vmcs);
1137 u8 error;
1138
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001139 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001140 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141 : "cc", "memory");
1142 if (error)
1143 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1144 vmcs, phys_addr);
1145}
1146
Nadav Har'Eld462b812011-05-24 15:26:10 +03001147static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1148{
1149 vmcs_clear(loaded_vmcs->vmcs);
1150 loaded_vmcs->cpu = -1;
1151 loaded_vmcs->launched = 0;
1152}
1153
Dongxiao Xu7725b892010-05-11 18:29:38 +08001154static void vmcs_load(struct vmcs *vmcs)
1155{
1156 u64 phys_addr = __pa(vmcs);
1157 u8 error;
1158
1159 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001160 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001161 : "cc", "memory");
1162 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001163 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001164 vmcs, phys_addr);
1165}
1166
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001167#ifdef CONFIG_KEXEC
1168/*
1169 * This bitmap is used to indicate whether the vmclear
1170 * operation is enabled on all cpus. All disabled by
1171 * default.
1172 */
1173static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1174
1175static inline void crash_enable_local_vmclear(int cpu)
1176{
1177 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1178}
1179
1180static inline void crash_disable_local_vmclear(int cpu)
1181{
1182 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1183}
1184
1185static inline int crash_local_vmclear_enabled(int cpu)
1186{
1187 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1188}
1189
1190static void crash_vmclear_local_loaded_vmcss(void)
1191{
1192 int cpu = raw_smp_processor_id();
1193 struct loaded_vmcs *v;
1194
1195 if (!crash_local_vmclear_enabled(cpu))
1196 return;
1197
1198 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1199 loaded_vmcss_on_cpu_link)
1200 vmcs_clear(v->vmcs);
1201}
1202#else
1203static inline void crash_enable_local_vmclear(int cpu) { }
1204static inline void crash_disable_local_vmclear(int cpu) { }
1205#endif /* CONFIG_KEXEC */
1206
Nadav Har'Eld462b812011-05-24 15:26:10 +03001207static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001209 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001210 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001211
Nadav Har'Eld462b812011-05-24 15:26:10 +03001212 if (loaded_vmcs->cpu != cpu)
1213 return; /* vcpu migration can race with cpu offline */
1214 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001215 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001216 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001217 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001218
1219 /*
1220 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1221 * is before setting loaded_vmcs->vcpu to -1 which is done in
1222 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1223 * then adds the vmcs into percpu list before it is deleted.
1224 */
1225 smp_wmb();
1226
Nadav Har'Eld462b812011-05-24 15:26:10 +03001227 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001228 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229}
1230
Nadav Har'Eld462b812011-05-24 15:26:10 +03001231static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001232{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001233 int cpu = loaded_vmcs->cpu;
1234
1235 if (cpu != -1)
1236 smp_call_function_single(cpu,
1237 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001238}
1239
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001240static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001241{
1242 if (vmx->vpid == 0)
1243 return;
1244
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001245 if (cpu_has_vmx_invvpid_single())
1246 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001247}
1248
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001249static inline void vpid_sync_vcpu_global(void)
1250{
1251 if (cpu_has_vmx_invvpid_global())
1252 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1253}
1254
1255static inline void vpid_sync_context(struct vcpu_vmx *vmx)
1256{
1257 if (cpu_has_vmx_invvpid_single())
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001258 vpid_sync_vcpu_single(vmx);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001259 else
1260 vpid_sync_vcpu_global();
1261}
1262
Sheng Yang14394422008-04-28 12:24:45 +08001263static inline void ept_sync_global(void)
1264{
1265 if (cpu_has_vmx_invept_global())
1266 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1267}
1268
1269static inline void ept_sync_context(u64 eptp)
1270{
Avi Kivity089d0342009-03-23 18:26:32 +02001271 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001272 if (cpu_has_vmx_invept_context())
1273 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1274 else
1275 ept_sync_global();
1276 }
1277}
1278
Avi Kivity96304212011-05-15 10:13:13 -04001279static __always_inline unsigned long vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001280{
Avi Kivity5e520e62011-05-15 10:13:12 -04001281 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282
Avi Kivity5e520e62011-05-15 10:13:12 -04001283 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1284 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001285 return value;
1286}
1287
Avi Kivity96304212011-05-15 10:13:13 -04001288static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289{
1290 return vmcs_readl(field);
1291}
1292
Avi Kivity96304212011-05-15 10:13:13 -04001293static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294{
1295 return vmcs_readl(field);
1296}
1297
Avi Kivity96304212011-05-15 10:13:13 -04001298static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299{
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001300#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301 return vmcs_readl(field);
1302#else
1303 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
1304#endif
1305}
1306
Avi Kivitye52de1b2007-01-05 16:36:56 -08001307static noinline void vmwrite_error(unsigned long field, unsigned long value)
1308{
1309 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1310 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1311 dump_stack();
1312}
1313
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314static void vmcs_writel(unsigned long field, unsigned long value)
1315{
1316 u8 error;
1317
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001318 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001319 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001320 if (unlikely(error))
1321 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322}
1323
1324static void vmcs_write16(unsigned long field, u16 value)
1325{
1326 vmcs_writel(field, value);
1327}
1328
1329static void vmcs_write32(unsigned long field, u32 value)
1330{
1331 vmcs_writel(field, value);
1332}
1333
1334static void vmcs_write64(unsigned long field, u64 value)
1335{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001337#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338 asm volatile ("");
1339 vmcs_writel(field+1, value >> 32);
1340#endif
1341}
1342
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001343static void vmcs_clear_bits(unsigned long field, u32 mask)
1344{
1345 vmcs_writel(field, vmcs_readl(field) & ~mask);
1346}
1347
1348static void vmcs_set_bits(unsigned long field, u32 mask)
1349{
1350 vmcs_writel(field, vmcs_readl(field) | mask);
1351}
1352
Gleb Natapov2961e8762013-11-25 15:37:13 +02001353static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1354{
1355 vmcs_write32(VM_ENTRY_CONTROLS, val);
1356 vmx->vm_entry_controls_shadow = val;
1357}
1358
1359static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1360{
1361 if (vmx->vm_entry_controls_shadow != val)
1362 vm_entry_controls_init(vmx, val);
1363}
1364
1365static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1366{
1367 return vmx->vm_entry_controls_shadow;
1368}
1369
1370
1371static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1372{
1373 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1374}
1375
1376static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1377{
1378 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1379}
1380
1381static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1382{
1383 vmcs_write32(VM_EXIT_CONTROLS, val);
1384 vmx->vm_exit_controls_shadow = val;
1385}
1386
1387static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1388{
1389 if (vmx->vm_exit_controls_shadow != val)
1390 vm_exit_controls_init(vmx, val);
1391}
1392
1393static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1394{
1395 return vmx->vm_exit_controls_shadow;
1396}
1397
1398
1399static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1400{
1401 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1402}
1403
1404static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1405{
1406 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1407}
1408
Avi Kivity2fb92db2011-04-27 19:42:18 +03001409static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1410{
1411 vmx->segment_cache.bitmask = 0;
1412}
1413
1414static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1415 unsigned field)
1416{
1417 bool ret;
1418 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1419
1420 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1421 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1422 vmx->segment_cache.bitmask = 0;
1423 }
1424 ret = vmx->segment_cache.bitmask & mask;
1425 vmx->segment_cache.bitmask |= mask;
1426 return ret;
1427}
1428
1429static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1430{
1431 u16 *p = &vmx->segment_cache.seg[seg].selector;
1432
1433 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1434 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1435 return *p;
1436}
1437
1438static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1439{
1440 ulong *p = &vmx->segment_cache.seg[seg].base;
1441
1442 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1443 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1444 return *p;
1445}
1446
1447static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1448{
1449 u32 *p = &vmx->segment_cache.seg[seg].limit;
1450
1451 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1452 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1453 return *p;
1454}
1455
1456static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1457{
1458 u32 *p = &vmx->segment_cache.seg[seg].ar;
1459
1460 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1461 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1462 return *p;
1463}
1464
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001465static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1466{
1467 u32 eb;
1468
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001469 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
1470 (1u << NM_VECTOR) | (1u << DB_VECTOR);
1471 if ((vcpu->guest_debug &
1472 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1473 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1474 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001475 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001476 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001477 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001478 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02001479 if (vcpu->fpu_active)
1480 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001481
1482 /* When we are running a nested L2 guest and L1 specified for it a
1483 * certain exception bitmap, we must trap the same exceptions and pass
1484 * them to L1. When running L2, we will only handle the exceptions
1485 * specified above if L1 did not want them.
1486 */
1487 if (is_guest_mode(vcpu))
1488 eb |= get_vmcs12(vcpu)->exception_bitmap;
1489
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001490 vmcs_write32(EXCEPTION_BITMAP, eb);
1491}
1492
Gleb Natapov2961e8762013-11-25 15:37:13 +02001493static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1494 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001495{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001496 vm_entry_controls_clearbit(vmx, entry);
1497 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001498}
1499
Avi Kivity61d2ef22010-04-28 16:40:38 +03001500static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1501{
1502 unsigned i;
1503 struct msr_autoload *m = &vmx->msr_autoload;
1504
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001505 switch (msr) {
1506 case MSR_EFER:
1507 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001508 clear_atomic_switch_msr_special(vmx,
1509 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001510 VM_EXIT_LOAD_IA32_EFER);
1511 return;
1512 }
1513 break;
1514 case MSR_CORE_PERF_GLOBAL_CTRL:
1515 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001516 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001517 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1518 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1519 return;
1520 }
1521 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001522 }
1523
Avi Kivity61d2ef22010-04-28 16:40:38 +03001524 for (i = 0; i < m->nr; ++i)
1525 if (m->guest[i].index == msr)
1526 break;
1527
1528 if (i == m->nr)
1529 return;
1530 --m->nr;
1531 m->guest[i] = m->guest[m->nr];
1532 m->host[i] = m->host[m->nr];
1533 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1534 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1535}
1536
Gleb Natapov2961e8762013-11-25 15:37:13 +02001537static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1538 unsigned long entry, unsigned long exit,
1539 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1540 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001541{
1542 vmcs_write64(guest_val_vmcs, guest_val);
1543 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001544 vm_entry_controls_setbit(vmx, entry);
1545 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001546}
1547
Avi Kivity61d2ef22010-04-28 16:40:38 +03001548static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1549 u64 guest_val, u64 host_val)
1550{
1551 unsigned i;
1552 struct msr_autoload *m = &vmx->msr_autoload;
1553
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001554 switch (msr) {
1555 case MSR_EFER:
1556 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001557 add_atomic_switch_msr_special(vmx,
1558 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001559 VM_EXIT_LOAD_IA32_EFER,
1560 GUEST_IA32_EFER,
1561 HOST_IA32_EFER,
1562 guest_val, host_val);
1563 return;
1564 }
1565 break;
1566 case MSR_CORE_PERF_GLOBAL_CTRL:
1567 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001568 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001569 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1570 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1571 GUEST_IA32_PERF_GLOBAL_CTRL,
1572 HOST_IA32_PERF_GLOBAL_CTRL,
1573 guest_val, host_val);
1574 return;
1575 }
1576 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001577 }
1578
Avi Kivity61d2ef22010-04-28 16:40:38 +03001579 for (i = 0; i < m->nr; ++i)
1580 if (m->guest[i].index == msr)
1581 break;
1582
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001583 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02001584 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001585 "Can't add msr %x\n", msr);
1586 return;
1587 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001588 ++m->nr;
1589 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1590 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1591 }
1592
1593 m->guest[i].index = msr;
1594 m->guest[i].value = guest_val;
1595 m->host[i].index = msr;
1596 m->host[i].value = host_val;
1597}
1598
Avi Kivity33ed6322007-05-02 16:54:03 +03001599static void reload_tss(void)
1600{
Avi Kivity33ed6322007-05-02 16:54:03 +03001601 /*
1602 * VT restores TR but not its size. Useless.
1603 */
Avi Kivityd3591922010-07-26 18:32:39 +03001604 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02001605 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03001606
Avi Kivityd3591922010-07-26 18:32:39 +03001607 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03001608 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
1609 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03001610}
1611
Avi Kivity92c0d902009-10-29 11:00:16 +02001612static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001613{
Roel Kluin3a34a882009-08-04 02:08:45 -07001614 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001615 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +03001616
Avi Kivityf6801df2010-01-21 15:31:50 +02001617 guest_efer = vmx->vcpu.arch.efer;
Roel Kluin3a34a882009-08-04 02:08:45 -07001618
Avi Kivity51c6cf62007-08-29 03:48:05 +03001619 /*
Guo Chao0fa06072012-06-28 15:16:19 +08001620 * NX is emulated; LMA and LME handled by hardware; SCE meaningless
Avi Kivity51c6cf62007-08-29 03:48:05 +03001621 * outside long mode
1622 */
1623 ignore_bits = EFER_NX | EFER_SCE;
1624#ifdef CONFIG_X86_64
1625 ignore_bits |= EFER_LMA | EFER_LME;
1626 /* SCE is meaningful only in long mode on Intel */
1627 if (guest_efer & EFER_LMA)
1628 ignore_bits &= ~(u64)EFER_SCE;
1629#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +03001630 guest_efer &= ~ignore_bits;
1631 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +03001632 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +02001633 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03001634
1635 clear_atomic_switch_msr(vmx, MSR_EFER);
1636 /* On ept, can't emulate nx, and must switch nx atomically */
1637 if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
1638 guest_efer = vmx->vcpu.arch.efer;
1639 if (!(guest_efer & EFER_LMA))
1640 guest_efer &= ~EFER_LME;
1641 add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
1642 return false;
1643 }
1644
Avi Kivity26bb0982009-09-07 11:14:12 +03001645 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001646}
1647
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001648static unsigned long segment_base(u16 selector)
1649{
Avi Kivityd3591922010-07-26 18:32:39 +03001650 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001651 struct desc_struct *d;
1652 unsigned long table_base;
1653 unsigned long v;
1654
1655 if (!(selector & ~3))
1656 return 0;
1657
Avi Kivityd3591922010-07-26 18:32:39 +03001658 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001659
1660 if (selector & 4) { /* from ldt */
1661 u16 ldt_selector = kvm_read_ldt();
1662
1663 if (!(ldt_selector & ~3))
1664 return 0;
1665
1666 table_base = segment_base(ldt_selector);
1667 }
1668 d = (struct desc_struct *)(table_base + (selector & ~7));
1669 v = get_desc_base(d);
1670#ifdef CONFIG_X86_64
1671 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
1672 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
1673#endif
1674 return v;
1675}
1676
1677static inline unsigned long kvm_read_tr_base(void)
1678{
1679 u16 tr;
1680 asm("str %0" : "=g"(tr));
1681 return segment_base(tr);
1682}
1683
Avi Kivity04d2cc72007-09-10 18:10:54 +03001684static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001685{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001686 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001687 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001688
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001689 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001690 return;
1691
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001692 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001693 /*
1694 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1695 * allow segment selectors with cpl > 0 or ti == 1.
1696 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001697 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02001698 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02001699 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001700 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001701 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001702 vmx->host_state.fs_reload_needed = 0;
1703 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03001704 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001705 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001706 }
Avi Kivity9581d442010-10-19 16:46:55 +02001707 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001708 if (!(vmx->host_state.gs_sel & 7))
1709 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001710 else {
1711 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001712 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001713 }
1714
1715#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03001716 savesegment(ds, vmx->host_state.ds_sel);
1717 savesegment(es, vmx->host_state.es_sel);
1718#endif
1719
1720#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03001721 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
1722 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
1723#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001724 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
1725 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03001726#endif
Avi Kivity707c0872007-05-02 17:33:43 +03001727
1728#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001729 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
1730 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03001731 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03001732#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00001733 if (boot_cpu_has(X86_FEATURE_MPX))
1734 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03001735 for (i = 0; i < vmx->save_nmsrs; ++i)
1736 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02001737 vmx->guest_msrs[i].data,
1738 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03001739}
1740
Avi Kivitya9b21b62008-06-24 11:48:49 +03001741static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001742{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001743 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001744 return;
1745
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001746 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001747 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02001748#ifdef CONFIG_X86_64
1749 if (is_long_mode(&vmx->vcpu))
1750 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1751#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001752 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001753 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001754#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02001755 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001756#else
1757 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001758#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001759 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02001760 if (vmx->host_state.fs_reload_needed)
1761 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001762#ifdef CONFIG_X86_64
1763 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
1764 loadsegment(ds, vmx->host_state.ds_sel);
1765 loadsegment(es, vmx->host_state.es_sel);
1766 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001767#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001768 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001769#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001770 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001771#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00001772 if (vmx->host_state.msr_host_bndcfgs)
1773 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07001774 /*
1775 * If the FPU is not active (through the host task or
1776 * the guest vcpu), then restore the cr0.TS bit.
1777 */
1778 if (!user_has_fpu() && !vmx->vcpu.guest_fpu_loaded)
1779 stts();
Avi Kivity3444d7d2010-07-26 18:32:38 +03001780 load_gdt(&__get_cpu_var(host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03001781}
1782
Avi Kivitya9b21b62008-06-24 11:48:49 +03001783static void vmx_load_host_state(struct vcpu_vmx *vmx)
1784{
1785 preempt_disable();
1786 __vmx_load_host_state(vmx);
1787 preempt_enable();
1788}
1789
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790/*
1791 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1792 * vcpu mutex is already taken.
1793 */
Avi Kivity15ad7142007-07-11 18:17:21 +03001794static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001795{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001796 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001797 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001798
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001799 if (!vmm_exclusive)
1800 kvm_cpu_vmxon(phys_addr);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001801 else if (vmx->loaded_vmcs->cpu != cpu)
1802 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001803
Nadav Har'Eld462b812011-05-24 15:26:10 +03001804 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1805 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1806 vmcs_load(vmx->loaded_vmcs->vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001807 }
1808
Nadav Har'Eld462b812011-05-24 15:26:10 +03001809 if (vmx->loaded_vmcs->cpu != cpu) {
Avi Kivityd3591922010-07-26 18:32:39 +03001810 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001811 unsigned long sysenter_esp;
1812
Avi Kivitya8eeb042010-05-10 12:34:53 +03001813 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001814 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001815 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001816
1817 /*
1818 * Read loaded_vmcs->cpu should be before fetching
1819 * loaded_vmcs->loaded_vmcss_on_cpu_link.
1820 * See the comments in __loaded_vmcs_clear().
1821 */
1822 smp_rmb();
1823
Nadav Har'Eld462b812011-05-24 15:26:10 +03001824 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1825 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001826 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001827 local_irq_enable();
1828
Avi Kivity6aa8b732006-12-10 02:21:36 -08001829 /*
1830 * Linux uses per-cpu TSS and GDT, so set these when switching
1831 * processors.
1832 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001833 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03001834 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835
1836 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1837 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Nadav Har'Eld462b812011-05-24 15:26:10 +03001838 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001839 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001840}
1841
1842static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1843{
Avi Kivitya9b21b62008-06-24 11:48:49 +03001844 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001845 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03001846 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
1847 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001848 kvm_cpu_vmxoff();
1849 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850}
1851
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001852static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
1853{
Avi Kivity81231c62010-01-24 16:26:40 +02001854 ulong cr0;
1855
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001856 if (vcpu->fpu_active)
1857 return;
1858 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02001859 cr0 = vmcs_readl(GUEST_CR0);
1860 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
1861 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
1862 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001863 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001864 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001865 if (is_guest_mode(vcpu))
1866 vcpu->arch.cr0_guest_owned_bits &=
1867 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02001868 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001869}
1870
Avi Kivityedcafe32009-12-30 18:07:40 +02001871static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1872
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001873/*
1874 * Return the cr0 value that a nested guest would read. This is a combination
1875 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
1876 * its hypervisor (cr0_read_shadow).
1877 */
1878static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
1879{
1880 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
1881 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
1882}
1883static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
1884{
1885 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
1886 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
1887}
1888
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001889static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
1890{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001891 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
1892 * set this *before* calling this function.
1893 */
Avi Kivityedcafe32009-12-30 18:07:40 +02001894 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02001895 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001896 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001897 vcpu->arch.cr0_guest_owned_bits = 0;
1898 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001899 if (is_guest_mode(vcpu)) {
1900 /*
1901 * L1's specified read shadow might not contain the TS bit,
1902 * so now that we turned on shadowing of this bit, we need to
1903 * set this bit of the shadow. Like in nested_vmx_run we need
1904 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
1905 * up-to-date here because we just decached cr0.TS (and we'll
1906 * only update vmcs12->guest_cr0 on nested exit).
1907 */
1908 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1909 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
1910 (vcpu->arch.cr0 & X86_CR0_TS);
1911 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
1912 } else
1913 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001914}
1915
Avi Kivity6aa8b732006-12-10 02:21:36 -08001916static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1917{
Avi Kivity78ac8b42010-04-08 18:19:35 +03001918 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001919
Avi Kivity6de12732011-03-07 12:51:22 +02001920 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1921 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1922 rflags = vmcs_readl(GUEST_RFLAGS);
1923 if (to_vmx(vcpu)->rmode.vm86_active) {
1924 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1925 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1926 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1927 }
1928 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001929 }
Avi Kivity6de12732011-03-07 12:51:22 +02001930 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931}
1932
1933static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1934{
Avi Kivity6de12732011-03-07 12:51:22 +02001935 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1936 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001937 if (to_vmx(vcpu)->rmode.vm86_active) {
1938 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001939 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001940 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001941 vmcs_writel(GUEST_RFLAGS, rflags);
1942}
1943
Glauber Costa2809f5d2009-05-12 16:21:05 -04001944static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1945{
1946 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1947 int ret = 0;
1948
1949 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001950 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001951 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001952 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001953
1954 return ret & mask;
1955}
1956
1957static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1958{
1959 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1960 u32 interruptibility = interruptibility_old;
1961
1962 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1963
Jan Kiszka48005f62010-02-19 19:38:07 +01001964 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001965 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001966 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001967 interruptibility |= GUEST_INTR_STATE_STI;
1968
1969 if ((interruptibility != interruptibility_old))
1970 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1971}
1972
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1974{
1975 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001976
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001977 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001978 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001979 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001980
Glauber Costa2809f5d2009-05-12 16:21:05 -04001981 /* skipping an emulated instruction also counts */
1982 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001983}
1984
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001985/*
1986 * KVM wants to inject page-faults which it got to the guest. This function
1987 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001988 */
Gleb Natapove011c662013-09-25 12:51:35 +03001989static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001990{
1991 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1992
Gleb Natapove011c662013-09-25 12:51:35 +03001993 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001994 return 0;
1995
Jan Kiszka533558b2014-01-04 18:47:20 +01001996 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
1997 vmcs_read32(VM_EXIT_INTR_INFO),
1998 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001999 return 1;
2000}
2001
Avi Kivity298101d2007-11-25 13:41:11 +02002002static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002003 bool has_error_code, u32 error_code,
2004 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002005{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002006 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002007 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002008
Gleb Natapove011c662013-09-25 12:51:35 +03002009 if (!reinject && is_guest_mode(vcpu) &&
2010 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002011 return;
2012
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002013 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002014 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002015 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2016 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002017
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002018 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002019 int inc_eip = 0;
2020 if (kvm_exception_is_soft(nr))
2021 inc_eip = vcpu->arch.event_exit_inst_len;
2022 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002023 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002024 return;
2025 }
2026
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002027 if (kvm_exception_is_soft(nr)) {
2028 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2029 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002030 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2031 } else
2032 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2033
2034 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002035}
2036
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002037static bool vmx_rdtscp_supported(void)
2038{
2039 return cpu_has_vmx_rdtscp();
2040}
2041
Mao, Junjiead756a12012-07-02 01:18:48 +00002042static bool vmx_invpcid_supported(void)
2043{
2044 return cpu_has_vmx_invpcid() && enable_ept;
2045}
2046
Avi Kivity6aa8b732006-12-10 02:21:36 -08002047/*
Eddie Donga75beee2007-05-17 18:55:15 +03002048 * Swap MSR entry in host/guest MSR entry array.
2049 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002050static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002051{
Avi Kivity26bb0982009-09-07 11:14:12 +03002052 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002053
2054 tmp = vmx->guest_msrs[to];
2055 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2056 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002057}
2058
Yang Zhang8d146952013-01-25 10:18:50 +08002059static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2060{
2061 unsigned long *msr_bitmap;
2062
2063 if (irqchip_in_kernel(vcpu->kvm) && apic_x2apic_mode(vcpu->arch.apic)) {
2064 if (is_long_mode(vcpu))
2065 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2066 else
2067 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
2068 } else {
2069 if (is_long_mode(vcpu))
2070 msr_bitmap = vmx_msr_bitmap_longmode;
2071 else
2072 msr_bitmap = vmx_msr_bitmap_legacy;
2073 }
2074
2075 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2076}
2077
Eddie Donga75beee2007-05-17 18:55:15 +03002078/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002079 * Set up the vmcs to automatically save and restore system
2080 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2081 * mode, as fiddling with msrs is very expensive.
2082 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002083static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002084{
Avi Kivity26bb0982009-09-07 11:14:12 +03002085 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002086
Eddie Donga75beee2007-05-17 18:55:15 +03002087 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002088#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002089 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002090 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002091 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002092 move_msr_up(vmx, index, save_nmsrs++);
2093 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002094 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002095 move_msr_up(vmx, index, save_nmsrs++);
2096 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002097 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002098 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002099 index = __find_msr_index(vmx, MSR_TSC_AUX);
2100 if (index >= 0 && vmx->rdtscp_enabled)
2101 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002102 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002103 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002104 * if efer.sce is enabled.
2105 */
Brian Gerst8c065852010-07-17 09:03:26 -04002106 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002107 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002108 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002109 }
Eddie Donga75beee2007-05-17 18:55:15 +03002110#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002111 index = __find_msr_index(vmx, MSR_EFER);
2112 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002113 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002114
Avi Kivity26bb0982009-09-07 11:14:12 +03002115 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002116
Yang Zhang8d146952013-01-25 10:18:50 +08002117 if (cpu_has_vmx_msr_bitmap())
2118 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002119}
2120
2121/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122 * reads and returns guest's timestamp counter "register"
2123 * guest_tsc = host_tsc + tsc_offset -- 21.3
2124 */
2125static u64 guest_read_tsc(void)
2126{
2127 u64 host_tsc, tsc_offset;
2128
2129 rdtscll(host_tsc);
2130 tsc_offset = vmcs_read64(TSC_OFFSET);
2131 return host_tsc + tsc_offset;
2132}
2133
2134/*
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002135 * Like guest_read_tsc, but always returns L1's notion of the timestamp
2136 * counter, even if a nested guest (L2) is currently running.
2137 */
Marcelo Tosatti886b4702012-11-27 23:28:58 -02002138u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002139{
Marcelo Tosatti886b4702012-11-27 23:28:58 -02002140 u64 tsc_offset;
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002141
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002142 tsc_offset = is_guest_mode(vcpu) ?
2143 to_vmx(vcpu)->nested.vmcs01_tsc_offset :
2144 vmcs_read64(TSC_OFFSET);
2145 return host_tsc + tsc_offset;
2146}
2147
2148/*
Zachary Amsdencc578282012-02-03 15:43:50 -02002149 * Engage any workarounds for mis-matched TSC rates. Currently limited to
2150 * software catchup for faster rates on slower CPUs.
Joerg Roedel4051b182011-03-25 09:44:49 +01002151 */
Zachary Amsdencc578282012-02-03 15:43:50 -02002152static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
Joerg Roedel4051b182011-03-25 09:44:49 +01002153{
Zachary Amsdencc578282012-02-03 15:43:50 -02002154 if (!scale)
2155 return;
2156
2157 if (user_tsc_khz > tsc_khz) {
2158 vcpu->arch.tsc_catchup = 1;
2159 vcpu->arch.tsc_always_catchup = 1;
2160 } else
2161 WARN(1, "user requested TSC rate below hardware speed\n");
Joerg Roedel4051b182011-03-25 09:44:49 +01002162}
2163
Will Auldba904632012-11-29 12:42:50 -08002164static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu)
2165{
2166 return vmcs_read64(TSC_OFFSET);
2167}
2168
Joerg Roedel4051b182011-03-25 09:44:49 +01002169/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002170 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002171 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002172static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002174 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002175 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002176 * We're here if L1 chose not to trap WRMSR to TSC. According
2177 * to the spec, this should set L1's TSC; The offset that L1
2178 * set for L2 remains unchanged, and still needs to be added
2179 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002180 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002181 struct vmcs12 *vmcs12;
2182 to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
2183 /* recalculate vmcs02.TSC_OFFSET: */
2184 vmcs12 = get_vmcs12(vcpu);
2185 vmcs_write64(TSC_OFFSET, offset +
2186 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2187 vmcs12->tsc_offset : 0));
2188 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002189 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2190 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002191 vmcs_write64(TSC_OFFSET, offset);
2192 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193}
2194
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002195static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
Zachary Amsdene48672f2010-08-19 22:07:23 -10002196{
2197 u64 offset = vmcs_read64(TSC_OFFSET);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002198
Zachary Amsdene48672f2010-08-19 22:07:23 -10002199 vmcs_write64(TSC_OFFSET, offset + adjustment);
Nadav Har'El79918252011-05-25 23:15:39 +03002200 if (is_guest_mode(vcpu)) {
2201 /* Even when running L2, the adjustment needs to apply to L1 */
2202 to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002203 } else
2204 trace_kvm_write_tsc_offset(vcpu->vcpu_id, offset,
2205 offset + adjustment);
Zachary Amsdene48672f2010-08-19 22:07:23 -10002206}
2207
Joerg Roedel857e4092011-03-25 09:44:50 +01002208static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
2209{
2210 return target_tsc - native_read_tsc();
2211}
2212
Nadav Har'El801d3422011-05-25 23:02:23 +03002213static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2214{
2215 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2216 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2217}
2218
2219/*
2220 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2221 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2222 * all guests if the "nested" module option is off, and can also be disabled
2223 * for a single guest by disabling its VMX cpuid bit.
2224 */
2225static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2226{
2227 return nested && guest_cpuid_has_vmx(vcpu);
2228}
2229
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002231 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2232 * returned for the various VMX controls MSRs when nested VMX is enabled.
2233 * The same values should also be used to verify that vmcs12 control fields are
2234 * valid during nested entry from L1 to L2.
2235 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2236 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2237 * bit in the high half is on if the corresponding bit in the control field
2238 * may be on. See also vmx_control_verify().
2239 * TODO: allow these variables to be modified (downgraded) by module options
2240 * or other means.
2241 */
2242static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high;
2243static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high;
2244static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high;
2245static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high;
2246static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002247static u32 nested_vmx_misc_low, nested_vmx_misc_high;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03002248static u32 nested_vmx_ept_caps;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002249static __init void nested_vmx_setup_ctls_msrs(void)
2250{
2251 /*
2252 * Note that as a general rule, the high half of the MSRs (bits in
2253 * the control fields which may be 1) should be initialized by the
2254 * intersection of the underlying hardware's MSR (i.e., features which
2255 * can be supported) and the list of features we want to expose -
2256 * because they are known to be properly supported in our code.
2257 * Also, usually, the low half of the MSRs (bits which must be 1) can
2258 * be set to 0, meaning that L1 may turn off any of these bits. The
2259 * reason is that if one of these bits is necessary, it will appear
2260 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2261 * fields of vmcs01 and vmcs02, will turn these bits off - and
2262 * nested_vmx_exit_handled() will not pass related exits to L1.
2263 * These rules have exceptions below.
2264 */
2265
2266 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002267 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
2268 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002269 /*
2270 * According to the Intel spec, if bit 55 of VMX_BASIC is off (as it is
2271 * in our case), bits 1, 2 and 4 (i.e., 0x16) must be 1 in this MSR.
2272 */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002273 nested_vmx_pinbased_ctls_low |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2274 nested_vmx_pinbased_ctls_high &= PIN_BASED_EXT_INTR_MASK |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002275 PIN_BASED_NMI_EXITING | PIN_BASED_VIRTUAL_NMIS;
2276 nested_vmx_pinbased_ctls_high |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002277 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002278
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002279 /*
2280 * Exit controls
2281 * If bit 55 of VMX_BASIC is off, bits 0-8 and 10, 11, 13, 14, 16 and
2282 * 17 must be 1.
2283 */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002284 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
2285 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high);
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002286 nested_vmx_exit_ctls_low = VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002287
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002288 nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002289#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002290 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002291#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01002292 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
2293 nested_vmx_exit_ctls_high |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
2294 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002295 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2296
Paolo Bonzini36be0b92014-02-24 12:30:04 +01002297 if (vmx_mpx_supported())
2298 nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002299
2300 /* entry controls */
2301 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
2302 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high);
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002303 /* If bit 55 of VMX_BASIC is off, bits 0-8 and 12 must be 1. */
2304 nested_vmx_entry_ctls_low = VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002305 nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002306#ifdef CONFIG_X86_64
2307 VM_ENTRY_IA32E_MODE |
2308#endif
2309 VM_ENTRY_LOAD_IA32_PAT;
Nadav Har'El8049d652013-08-05 11:07:06 +03002310 nested_vmx_entry_ctls_high |= (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR |
2311 VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzini36be0b92014-02-24 12:30:04 +01002312 if (vmx_mpx_supported())
2313 nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002314
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002315 /* cpu-based controls */
2316 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
2317 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high);
2318 nested_vmx_procbased_ctls_low = 0;
2319 nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002320 CPU_BASED_VIRTUAL_INTR_PENDING |
2321 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002322 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2323 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2324 CPU_BASED_CR3_STORE_EXITING |
2325#ifdef CONFIG_X86_64
2326 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2327#endif
2328 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
2329 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING |
Avi Kivitydbcb4e72012-08-13 15:38:22 +03002330 CPU_BASED_RDPMC_EXITING | CPU_BASED_RDTSC_EXITING |
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002331 CPU_BASED_PAUSE_EXITING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002332 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
2333 /*
2334 * We can allow some features even when not supported by the
2335 * hardware. For example, L1 can specify an MSR bitmap - and we
2336 * can use it to avoid exits to L1 - even when L0 runs L2
2337 * without MSR bitmaps.
2338 */
2339 nested_vmx_procbased_ctls_high |= CPU_BASED_USE_MSR_BITMAPS;
2340
2341 /* secondary cpu-based controls */
2342 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
2343 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high);
2344 nested_vmx_secondary_ctls_low = 0;
2345 nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002346 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02002347 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002348 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002349
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002350 if (enable_ept) {
2351 /* nested EPT: emulate EPT also to L1 */
2352 nested_vmx_secondary_ctls_high |= SECONDARY_EXEC_ENABLE_EPT;
Jan Kiszkaca72d972013-08-06 10:39:55 +02002353 nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01002354 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2355 VMX_EPT_INVEPT_BIT;
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002356 nested_vmx_ept_caps &= vmx_capability.ept;
2357 /*
Bandan Das4b855072014-04-19 18:17:44 -04002358 * For nested guests, we don't do anything specific
2359 * for single context invalidation. Hence, only advertise
2360 * support for global context invalidation.
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002361 */
Bandan Das4b855072014-04-19 18:17:44 -04002362 nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT;
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002363 } else
2364 nested_vmx_ept_caps = 0;
2365
Jan Kiszkac18911a2013-03-13 16:06:41 +01002366 /* miscellaneous data */
2367 rdmsr(MSR_IA32_VMX_MISC, nested_vmx_misc_low, nested_vmx_misc_high);
Jan Kiszkaf41245002014-03-07 20:03:13 +01002368 nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2369 nested_vmx_misc_low |= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
2370 VMX_MISC_ACTIVITY_HLT;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002371 nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002372}
2373
2374static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2375{
2376 /*
2377 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2378 */
2379 return ((control & high) | low) == control;
2380}
2381
2382static inline u64 vmx_control_msr(u32 low, u32 high)
2383{
2384 return low | ((u64)high << 32);
2385}
2386
Jan Kiszkacae50132014-01-04 18:47:22 +01002387/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002388static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2389{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002390 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002391 case MSR_IA32_VMX_BASIC:
2392 /*
2393 * This MSR reports some information about VMX support. We
2394 * should return information about the VMX we emulate for the
2395 * guest, and the VMCS structure we give it - not about the
2396 * VMX support of the underlying hardware.
2397 */
2398 *pdata = VMCS12_REVISION |
2399 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2400 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2401 break;
2402 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2403 case MSR_IA32_VMX_PINBASED_CTLS:
2404 *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low,
2405 nested_vmx_pinbased_ctls_high);
2406 break;
2407 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2408 case MSR_IA32_VMX_PROCBASED_CTLS:
2409 *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low,
2410 nested_vmx_procbased_ctls_high);
2411 break;
2412 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2413 case MSR_IA32_VMX_EXIT_CTLS:
2414 *pdata = vmx_control_msr(nested_vmx_exit_ctls_low,
2415 nested_vmx_exit_ctls_high);
2416 break;
2417 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2418 case MSR_IA32_VMX_ENTRY_CTLS:
2419 *pdata = vmx_control_msr(nested_vmx_entry_ctls_low,
2420 nested_vmx_entry_ctls_high);
2421 break;
2422 case MSR_IA32_VMX_MISC:
Jan Kiszkac18911a2013-03-13 16:06:41 +01002423 *pdata = vmx_control_msr(nested_vmx_misc_low,
2424 nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002425 break;
2426 /*
2427 * These MSRs specify bits which the guest must keep fixed (on or off)
2428 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2429 * We picked the standard core2 setting.
2430 */
2431#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2432#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2433 case MSR_IA32_VMX_CR0_FIXED0:
2434 *pdata = VMXON_CR0_ALWAYSON;
2435 break;
2436 case MSR_IA32_VMX_CR0_FIXED1:
2437 *pdata = -1ULL;
2438 break;
2439 case MSR_IA32_VMX_CR4_FIXED0:
2440 *pdata = VMXON_CR4_ALWAYSON;
2441 break;
2442 case MSR_IA32_VMX_CR4_FIXED1:
2443 *pdata = -1ULL;
2444 break;
2445 case MSR_IA32_VMX_VMCS_ENUM:
2446 *pdata = 0x1f;
2447 break;
2448 case MSR_IA32_VMX_PROCBASED_CTLS2:
2449 *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low,
2450 nested_vmx_secondary_ctls_high);
2451 break;
2452 case MSR_IA32_VMX_EPT_VPID_CAP:
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002453 /* Currently, no nested vpid support */
2454 *pdata = nested_vmx_ept_caps;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002455 break;
2456 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002457 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08002458 }
2459
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002460 return 0;
2461}
2462
2463/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002464 * Reads an msr value (of 'msr_index') into 'pdata'.
2465 * Returns 0 on success, non-0 otherwise.
2466 * Assumes vcpu_load() was already called.
2467 */
2468static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2469{
2470 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +03002471 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002472
2473 if (!pdata) {
2474 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
2475 return -EINVAL;
2476 }
2477
2478 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002479#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002480 case MSR_FS_BASE:
2481 data = vmcs_readl(GUEST_FS_BASE);
2482 break;
2483 case MSR_GS_BASE:
2484 data = vmcs_readl(GUEST_GS_BASE);
2485 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002486 case MSR_KERNEL_GS_BASE:
2487 vmx_load_host_state(to_vmx(vcpu));
2488 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
2489 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03002490#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08002491 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002492 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302493 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002494 data = guest_read_tsc();
2495 break;
2496 case MSR_IA32_SYSENTER_CS:
2497 data = vmcs_read32(GUEST_SYSENTER_CS);
2498 break;
2499 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002500 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501 break;
2502 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002503 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002504 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002505 case MSR_IA32_BNDCFGS:
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002506 if (!vmx_mpx_supported())
2507 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002508 data = vmcs_read64(GUEST_BNDCFGS);
2509 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01002510 case MSR_IA32_FEATURE_CONTROL:
2511 if (!nested_vmx_allowed(vcpu))
2512 return 1;
2513 data = to_vmx(vcpu)->nested.msr_ia32_feature_control;
2514 break;
2515 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2516 if (!nested_vmx_allowed(vcpu))
2517 return 1;
2518 return vmx_get_vmx_msr(vcpu, msr_index, pdata);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002519 case MSR_TSC_AUX:
2520 if (!to_vmx(vcpu)->rdtscp_enabled)
2521 return 1;
2522 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10002524 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002525 if (msr) {
2526 data = msr->data;
2527 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002529 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002530 }
2531
2532 *pdata = data;
2533 return 0;
2534}
2535
Jan Kiszkacae50132014-01-04 18:47:22 +01002536static void vmx_leave_nested(struct kvm_vcpu *vcpu);
2537
Avi Kivity6aa8b732006-12-10 02:21:36 -08002538/*
2539 * Writes msr value into into the appropriate "register".
2540 * Returns 0 on success, non-0 otherwise.
2541 * Assumes vcpu_load() was already called.
2542 */
Will Auld8fe8ab42012-11-29 12:42:12 -08002543static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002545 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002546 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03002547 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08002548 u32 msr_index = msr_info->index;
2549 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03002550
Avi Kivity6aa8b732006-12-10 02:21:36 -08002551 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08002552 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08002553 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03002554 break;
Avi Kivity16175a72009-03-23 22:13:44 +02002555#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002556 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002557 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002558 vmcs_writel(GUEST_FS_BASE, data);
2559 break;
2560 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002561 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002562 vmcs_writel(GUEST_GS_BASE, data);
2563 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002564 case MSR_KERNEL_GS_BASE:
2565 vmx_load_host_state(vmx);
2566 vmx->msr_guest_kernel_gs_base = data;
2567 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002568#endif
2569 case MSR_IA32_SYSENTER_CS:
2570 vmcs_write32(GUEST_SYSENTER_CS, data);
2571 break;
2572 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002573 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002574 break;
2575 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002576 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002577 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002578 case MSR_IA32_BNDCFGS:
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002579 if (!vmx_mpx_supported())
2580 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002581 vmcs_write64(GUEST_BNDCFGS, data);
2582 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302583 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08002584 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002585 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002586 case MSR_IA32_CR_PAT:
2587 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2588 vmcs_write64(GUEST_IA32_PAT, data);
2589 vcpu->arch.pat = data;
2590 break;
2591 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002592 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002593 break;
Will Auldba904632012-11-29 12:42:50 -08002594 case MSR_IA32_TSC_ADJUST:
2595 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002596 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01002597 case MSR_IA32_FEATURE_CONTROL:
2598 if (!nested_vmx_allowed(vcpu) ||
2599 (to_vmx(vcpu)->nested.msr_ia32_feature_control &
2600 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
2601 return 1;
2602 vmx->nested.msr_ia32_feature_control = data;
2603 if (msr_info->host_initiated && data == 0)
2604 vmx_leave_nested(vcpu);
2605 break;
2606 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2607 return 1; /* they are read-only */
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002608 case MSR_TSC_AUX:
2609 if (!vmx->rdtscp_enabled)
2610 return 1;
2611 /* Check reserved bit, higher 32 bits should be zero */
2612 if ((data >> 32) != 0)
2613 return 1;
2614 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002615 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10002616 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002617 if (msr) {
2618 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03002619 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
2620 preempt_disable();
Avi Kivity9ee73972012-03-06 14:16:33 +02002621 kvm_set_shared_msr(msr->index, msr->data,
2622 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03002623 preempt_enable();
2624 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002625 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002627 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628 }
2629
Eddie Dong2cc51562007-05-21 07:28:09 +03002630 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002631}
2632
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002633static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002635 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
2636 switch (reg) {
2637 case VCPU_REGS_RSP:
2638 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2639 break;
2640 case VCPU_REGS_RIP:
2641 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2642 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002643 case VCPU_EXREG_PDPTR:
2644 if (enable_ept)
2645 ept_save_pdptrs(vcpu);
2646 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002647 default:
2648 break;
2649 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650}
2651
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652static __init int cpu_has_kvm_support(void)
2653{
Eduardo Habkost6210e372008-11-17 19:03:16 -02002654 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002655}
2656
2657static __init int vmx_disabled_by_bios(void)
2658{
2659 u64 msr;
2660
2661 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04002662 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08002663 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04002664 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
2665 && tboot_enabled())
2666 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08002667 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04002668 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08002669 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08002670 && !tboot_enabled()) {
2671 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08002672 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04002673 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08002674 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08002675 /* launched w/o TXT and VMX disabled */
2676 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
2677 && !tboot_enabled())
2678 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04002679 }
2680
2681 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682}
2683
Dongxiao Xu7725b892010-05-11 18:29:38 +08002684static void kvm_cpu_vmxon(u64 addr)
2685{
2686 asm volatile (ASM_VMX_VMXON_RAX
2687 : : "a"(&addr), "m"(addr)
2688 : "memory", "cc");
2689}
2690
Alexander Graf10474ae2009-09-15 11:37:46 +02002691static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692{
2693 int cpu = raw_smp_processor_id();
2694 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04002695 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002696
Alexander Graf10474ae2009-09-15 11:37:46 +02002697 if (read_cr4() & X86_CR4_VMXE)
2698 return -EBUSY;
2699
Nadav Har'Eld462b812011-05-24 15:26:10 +03002700 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002701
2702 /*
2703 * Now we can enable the vmclear operation in kdump
2704 * since the loaded_vmcss_on_cpu list on this cpu
2705 * has been initialized.
2706 *
2707 * Though the cpu is not in VMX operation now, there
2708 * is no problem to enable the vmclear operation
2709 * for the loaded_vmcss_on_cpu list is empty!
2710 */
2711 crash_enable_local_vmclear(cpu);
2712
Avi Kivity6aa8b732006-12-10 02:21:36 -08002713 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04002714
2715 test_bits = FEATURE_CONTROL_LOCKED;
2716 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
2717 if (tboot_enabled())
2718 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
2719
2720 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04002722 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
2723 }
Rusty Russell66aee912007-07-17 23:34:16 +10002724 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Alexander Graf10474ae2009-09-15 11:37:46 +02002725
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002726 if (vmm_exclusive) {
2727 kvm_cpu_vmxon(phys_addr);
2728 ept_sync_global();
2729 }
Alexander Graf10474ae2009-09-15 11:37:46 +02002730
Konrad Rzeszutek Wilk357d1222013-04-05 16:42:23 -04002731 native_store_gdt(&__get_cpu_var(host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03002732
Alexander Graf10474ae2009-09-15 11:37:46 +02002733 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002734}
2735
Nadav Har'Eld462b812011-05-24 15:26:10 +03002736static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03002737{
2738 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03002739 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03002740
Nadav Har'Eld462b812011-05-24 15:26:10 +03002741 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2742 loaded_vmcss_on_cpu_link)
2743 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03002744}
2745
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002746
2747/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2748 * tricks.
2749 */
2750static void kvm_cpu_vmxoff(void)
2751{
2752 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002753}
2754
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755static void hardware_disable(void *garbage)
2756{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002757 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002758 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002759 kvm_cpu_vmxoff();
2760 }
Dongxiao Xu7725b892010-05-11 18:29:38 +08002761 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762}
2763
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002764static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04002765 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766{
2767 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002768 u32 ctl = ctl_min | ctl_opt;
2769
2770 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2771
2772 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2773 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2774
2775 /* Ensure minimum (required) set of control bits are supported. */
2776 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002777 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002778
2779 *result = ctl;
2780 return 0;
2781}
2782
Avi Kivity110312c2010-12-21 12:54:20 +02002783static __init bool allow_1_setting(u32 msr, u32 ctl)
2784{
2785 u32 vmx_msr_low, vmx_msr_high;
2786
2787 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2788 return vmx_msr_high & ctl;
2789}
2790
Yang, Sheng002c7f72007-07-31 14:23:01 +03002791static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002792{
2793 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08002794 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002795 u32 _pin_based_exec_control = 0;
2796 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002797 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002798 u32 _vmexit_control = 0;
2799 u32 _vmentry_control = 0;
2800
Raghavendra K T10166742012-02-07 23:19:20 +05302801 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002802#ifdef CONFIG_X86_64
2803 CPU_BASED_CR8_LOAD_EXITING |
2804 CPU_BASED_CR8_STORE_EXITING |
2805#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08002806 CPU_BASED_CR3_LOAD_EXITING |
2807 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002808 CPU_BASED_USE_IO_BITMAPS |
2809 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03002810 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08002811 CPU_BASED_MWAIT_EXITING |
2812 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02002813 CPU_BASED_INVLPG_EXITING |
2814 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06002815
Sheng Yangf78e0e22007-10-29 09:40:42 +08002816 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08002817 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08002818 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002819 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2820 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002821 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002822#ifdef CONFIG_X86_64
2823 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2824 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2825 ~CPU_BASED_CR8_STORE_EXITING;
2826#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08002827 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002828 min2 = 0;
2829 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08002830 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08002831 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08002832 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002833 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002834 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002835 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00002836 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08002837 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002838 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03002839 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2840 SECONDARY_EXEC_SHADOW_VMCS;
Sheng Yangd56f5462008-04-25 10:13:16 +08002841 if (adjust_vmx_controls(min2, opt2,
2842 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002843 &_cpu_based_2nd_exec_control) < 0)
2844 return -EIO;
2845 }
2846#ifndef CONFIG_X86_64
2847 if (!(_cpu_based_2nd_exec_control &
2848 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2849 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2850#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08002851
2852 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2853 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08002854 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002855 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2856 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08002857
Sheng Yangd56f5462008-04-25 10:13:16 +08002858 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03002859 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2860 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03002861 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2862 CPU_BASED_CR3_STORE_EXITING |
2863 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08002864 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
2865 vmx_capability.ept, vmx_capability.vpid);
2866 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002867
Paolo Bonzini81908bf2014-02-21 10:32:27 +01002868 min = VM_EXIT_SAVE_DEBUG_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002869#ifdef CONFIG_X86_64
2870 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2871#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08002872 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002873 VM_EXIT_ACK_INTR_ON_EXIT | VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002874 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2875 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002876 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002877
Yang Zhang01e439b2013-04-11 19:25:12 +08002878 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2879 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR;
2880 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2881 &_pin_based_exec_control) < 0)
2882 return -EIO;
2883
2884 if (!(_cpu_based_2nd_exec_control &
2885 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) ||
2886 !(_vmexit_control & VM_EXIT_ACK_INTR_ON_EXIT))
2887 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
2888
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01002889 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002890 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002891 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2892 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002893 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002894
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002895 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002896
2897 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2898 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002899 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002900
2901#ifdef CONFIG_X86_64
2902 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2903 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002904 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002905#endif
2906
2907 /* Require Write-Back (WB) memory type for VMCS accesses. */
2908 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002909 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002910
Yang, Sheng002c7f72007-07-31 14:23:01 +03002911 vmcs_conf->size = vmx_msr_high & 0x1fff;
2912 vmcs_conf->order = get_order(vmcs_config.size);
2913 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002914
Yang, Sheng002c7f72007-07-31 14:23:01 +03002915 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2916 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002917 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002918 vmcs_conf->vmexit_ctrl = _vmexit_control;
2919 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002920
Avi Kivity110312c2010-12-21 12:54:20 +02002921 cpu_has_load_ia32_efer =
2922 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2923 VM_ENTRY_LOAD_IA32_EFER)
2924 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2925 VM_EXIT_LOAD_IA32_EFER);
2926
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002927 cpu_has_load_perf_global_ctrl =
2928 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2929 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
2930 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2931 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2932
2933 /*
2934 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
2935 * but due to arrata below it can't be used. Workaround is to use
2936 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2937 *
2938 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
2939 *
2940 * AAK155 (model 26)
2941 * AAP115 (model 30)
2942 * AAT100 (model 37)
2943 * BC86,AAY89,BD102 (model 44)
2944 * BA97 (model 46)
2945 *
2946 */
2947 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
2948 switch (boot_cpu_data.x86_model) {
2949 case 26:
2950 case 30:
2951 case 37:
2952 case 44:
2953 case 46:
2954 cpu_has_load_perf_global_ctrl = false;
2955 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2956 "does not work properly. Using workaround\n");
2957 break;
2958 default:
2959 break;
2960 }
2961 }
2962
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002963 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002964}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965
2966static struct vmcs *alloc_vmcs_cpu(int cpu)
2967{
2968 int node = cpu_to_node(cpu);
2969 struct page *pages;
2970 struct vmcs *vmcs;
2971
Mel Gorman6484eb32009-06-16 15:31:54 -07002972 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002973 if (!pages)
2974 return NULL;
2975 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002976 memset(vmcs, 0, vmcs_config.size);
2977 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002978 return vmcs;
2979}
2980
2981static struct vmcs *alloc_vmcs(void)
2982{
Ingo Molnard3b2c332007-01-05 16:36:23 -08002983 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08002984}
2985
2986static void free_vmcs(struct vmcs *vmcs)
2987{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002988 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002989}
2990
Nadav Har'Eld462b812011-05-24 15:26:10 +03002991/*
2992 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2993 */
2994static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
2995{
2996 if (!loaded_vmcs->vmcs)
2997 return;
2998 loaded_vmcs_clear(loaded_vmcs);
2999 free_vmcs(loaded_vmcs->vmcs);
3000 loaded_vmcs->vmcs = NULL;
3001}
3002
Sam Ravnborg39959582007-06-01 00:47:13 -07003003static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003004{
3005 int cpu;
3006
Zachary Amsden3230bb42009-09-29 11:38:37 -10003007 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003008 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003009 per_cpu(vmxarea, cpu) = NULL;
3010 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003011}
3012
Avi Kivity6aa8b732006-12-10 02:21:36 -08003013static __init int alloc_kvm_area(void)
3014{
3015 int cpu;
3016
Zachary Amsden3230bb42009-09-29 11:38:37 -10003017 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003018 struct vmcs *vmcs;
3019
3020 vmcs = alloc_vmcs_cpu(cpu);
3021 if (!vmcs) {
3022 free_kvm_area();
3023 return -ENOMEM;
3024 }
3025
3026 per_cpu(vmxarea, cpu) = vmcs;
3027 }
3028 return 0;
3029}
3030
3031static __init int hardware_setup(void)
3032{
Yang, Sheng002c7f72007-07-31 14:23:01 +03003033 if (setup_vmcs_config(&vmcs_config) < 0)
3034 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01003035
3036 if (boot_cpu_has(X86_FEATURE_NX))
3037 kvm_enable_efer_bits(EFER_NX);
3038
Sheng Yang93ba03c2009-04-01 15:52:32 +08003039 if (!cpu_has_vmx_vpid())
3040 enable_vpid = 0;
Abel Gordonabc4fc52013-04-18 14:35:25 +03003041 if (!cpu_has_vmx_shadow_vmcs())
3042 enable_shadow_vmcs = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08003043
Sheng Yang4bc9b982010-06-02 14:05:24 +08003044 if (!cpu_has_vmx_ept() ||
3045 !cpu_has_vmx_ept_4levels()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08003046 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003047 enable_unrestricted_guest = 0;
Xudong Hao83c3a332012-05-28 19:33:35 +08003048 enable_ept_ad_bits = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003049 }
3050
Xudong Hao83c3a332012-05-28 19:33:35 +08003051 if (!cpu_has_vmx_ept_ad_bits())
3052 enable_ept_ad_bits = 0;
3053
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003054 if (!cpu_has_vmx_unrestricted_guest())
3055 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08003056
3057 if (!cpu_has_vmx_flexpriority())
3058 flexpriority_enabled = 0;
3059
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003060 if (!cpu_has_vmx_tpr_shadow())
3061 kvm_x86_ops->update_cr8_intercept = NULL;
3062
Marcelo Tosatti54dee992009-06-11 12:07:44 -03003063 if (enable_ept && !cpu_has_vmx_ept_2m_page())
3064 kvm_disable_largepages();
3065
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003066 if (!cpu_has_vmx_ple())
3067 ple_gap = 0;
3068
Yang Zhang01e439b2013-04-11 19:25:12 +08003069 if (!cpu_has_vmx_apicv())
3070 enable_apicv = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08003071
Yang Zhang01e439b2013-04-11 19:25:12 +08003072 if (enable_apicv)
Yang Zhangc7c9c562013-01-25 10:18:51 +08003073 kvm_x86_ops->update_cr8_intercept = NULL;
Yang Zhanga20ed542013-04-11 19:25:15 +08003074 else {
Yang Zhangc7c9c562013-01-25 10:18:51 +08003075 kvm_x86_ops->hwapic_irr_update = NULL;
Yang Zhanga20ed542013-04-11 19:25:15 +08003076 kvm_x86_ops->deliver_posted_interrupt = NULL;
3077 kvm_x86_ops->sync_pir_to_irr = vmx_sync_pir_to_irr_dummy;
3078 }
Yang Zhang83d4c282013-01-25 10:18:49 +08003079
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003080 if (nested)
3081 nested_vmx_setup_ctls_msrs();
3082
Avi Kivity6aa8b732006-12-10 02:21:36 -08003083 return alloc_kvm_area();
3084}
3085
3086static __exit void hardware_unsetup(void)
3087{
3088 free_kvm_area();
3089}
3090
Gleb Natapov14168782013-01-21 15:36:49 +02003091static bool emulation_required(struct kvm_vcpu *vcpu)
3092{
3093 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3094}
3095
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003096static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003097 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003098{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003099 if (!emulate_invalid_guest_state) {
3100 /*
3101 * CS and SS RPL should be equal during guest entry according
3102 * to VMX spec, but in reality it is not always so. Since vcpu
3103 * is in the middle of the transition from real mode to
3104 * protected mode it is safe to assume that RPL 0 is a good
3105 * default value.
3106 */
3107 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
3108 save->selector &= ~SELECTOR_RPL_MASK;
3109 save->dpl = save->selector & SELECTOR_RPL_MASK;
3110 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003111 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003112 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003113}
3114
3115static void enter_pmode(struct kvm_vcpu *vcpu)
3116{
3117 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003118 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003119
Gleb Natapovd99e4152012-12-20 16:57:45 +02003120 /*
3121 * Update real mode segment cache. It may be not up-to-date if sement
3122 * register was written while vcpu was in a guest mode.
3123 */
3124 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3125 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3126 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3127 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3128 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3129 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3130
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003131 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003132
Avi Kivity2fb92db2011-04-27 19:42:18 +03003133 vmx_segment_cache_clear(vmx);
3134
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003135 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003136
3137 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003138 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3139 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003140 vmcs_writel(GUEST_RFLAGS, flags);
3141
Rusty Russell66aee912007-07-17 23:34:16 +10003142 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3143 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003144
3145 update_exception_bitmap(vcpu);
3146
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003147 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3148 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3149 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3150 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3151 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3152 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Gleb Natapov1f3141e2013-01-21 15:36:41 +02003153
3154 /* CPL is always 0 when CPU enters protected mode */
3155 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
3156 vmx->cpl = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157}
3158
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003159static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003160{
Mathias Krause772e0312012-08-30 01:30:19 +02003161 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003162 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163
Gleb Natapovd99e4152012-12-20 16:57:45 +02003164 var.dpl = 0x3;
3165 if (seg == VCPU_SREG_CS)
3166 var.type = 0x3;
3167
3168 if (!emulate_invalid_guest_state) {
3169 var.selector = var.base >> 4;
3170 var.base = var.base & 0xffff0;
3171 var.limit = 0xffff;
3172 var.g = 0;
3173 var.db = 0;
3174 var.present = 1;
3175 var.s = 1;
3176 var.l = 0;
3177 var.unusable = 0;
3178 var.type = 0x3;
3179 var.avl = 0;
3180 if (save->base & 0xf)
3181 printk_once(KERN_WARNING "kvm: segment base is not "
3182 "paragraph aligned when entering "
3183 "protected mode (seg=%d)", seg);
3184 }
3185
3186 vmcs_write16(sf->selector, var.selector);
3187 vmcs_write32(sf->base, var.base);
3188 vmcs_write32(sf->limit, var.limit);
3189 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003190}
3191
3192static void enter_rmode(struct kvm_vcpu *vcpu)
3193{
3194 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003195 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003196
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003197 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3198 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3199 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3200 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3201 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003202 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3203 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003204
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003205 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206
Gleb Natapov776e58e2011-03-13 12:34:27 +02003207 /*
3208 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003209 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003210 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003211 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003212 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3213 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003214
Avi Kivity2fb92db2011-04-27 19:42:18 +03003215 vmx_segment_cache_clear(vmx);
3216
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003217 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3220
3221 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003222 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003223
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003224 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225
3226 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003227 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228 update_exception_bitmap(vcpu);
3229
Gleb Natapovd99e4152012-12-20 16:57:45 +02003230 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3231 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3232 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3233 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3234 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3235 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003236
Eddie Dong8668a3c2007-10-10 14:26:45 +08003237 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003238}
3239
Amit Shah401d10d2009-02-20 22:53:37 +05303240static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3241{
3242 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003243 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3244
3245 if (!msr)
3246 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303247
Avi Kivity44ea2b12009-09-06 15:55:37 +03003248 /*
3249 * Force kernel_gs_base reloading before EFER changes, as control
3250 * of this msr depends on is_long_mode().
3251 */
3252 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003253 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303254 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003255 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303256 msr->data = efer;
3257 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003258 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303259
3260 msr->data = efer & ~EFER_LME;
3261 }
3262 setup_msrs(vmx);
3263}
3264
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003265#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003266
3267static void enter_lmode(struct kvm_vcpu *vcpu)
3268{
3269 u32 guest_tr_ar;
3270
Avi Kivity2fb92db2011-04-27 19:42:18 +03003271 vmx_segment_cache_clear(to_vmx(vcpu));
3272
Avi Kivity6aa8b732006-12-10 02:21:36 -08003273 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
3274 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003275 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3276 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277 vmcs_write32(GUEST_TR_AR_BYTES,
3278 (guest_tr_ar & ~AR_TYPE_MASK)
3279 | AR_TYPE_BUSY_64_TSS);
3280 }
Avi Kivityda38f432010-07-06 11:30:49 +03003281 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003282}
3283
3284static void exit_lmode(struct kvm_vcpu *vcpu)
3285{
Gleb Natapov2961e8762013-11-25 15:37:13 +02003286 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003287 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003288}
3289
3290#endif
3291
Sheng Yang2384d2b2008-01-17 15:14:33 +08003292static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3293{
Gui Jianfengb9d762f2010-06-07 10:32:29 +08003294 vpid_sync_context(to_vmx(vcpu));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003295 if (enable_ept) {
3296 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3297 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08003298 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003299 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003300}
3301
Avi Kivitye8467fd2009-12-29 18:43:06 +02003302static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3303{
3304 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3305
3306 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3307 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3308}
3309
Avi Kivityaff48ba2010-12-05 18:56:11 +02003310static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3311{
3312 if (enable_ept && is_paging(vcpu))
3313 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3314 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3315}
3316
Anthony Liguori25c4c272007-04-27 09:29:21 +03003317static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08003318{
Avi Kivityfc78f512009-12-07 12:16:48 +02003319 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3320
3321 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3322 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08003323}
3324
Sheng Yang14394422008-04-28 12:24:45 +08003325static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3326{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003327 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3328
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003329 if (!test_bit(VCPU_EXREG_PDPTR,
3330 (unsigned long *)&vcpu->arch.regs_dirty))
3331 return;
3332
Sheng Yang14394422008-04-28 12:24:45 +08003333 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003334 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
3335 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
3336 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
3337 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08003338 }
3339}
3340
Avi Kivity8f5d5492009-05-31 18:41:29 +03003341static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3342{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003343 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3344
Avi Kivity8f5d5492009-05-31 18:41:29 +03003345 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003346 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3347 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3348 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3349 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003350 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003351
3352 __set_bit(VCPU_EXREG_PDPTR,
3353 (unsigned long *)&vcpu->arch.regs_avail);
3354 __set_bit(VCPU_EXREG_PDPTR,
3355 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003356}
3357
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003358static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08003359
3360static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3361 unsigned long cr0,
3362 struct kvm_vcpu *vcpu)
3363{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03003364 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3365 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003366 if (!(cr0 & X86_CR0_PG)) {
3367 /* From paging/starting to nonpaging */
3368 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003369 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08003370 (CPU_BASED_CR3_LOAD_EXITING |
3371 CPU_BASED_CR3_STORE_EXITING));
3372 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003373 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003374 } else if (!is_paging(vcpu)) {
3375 /* From nonpaging to paging */
3376 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003377 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08003378 ~(CPU_BASED_CR3_LOAD_EXITING |
3379 CPU_BASED_CR3_STORE_EXITING));
3380 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003381 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003382 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003383
3384 if (!(cr0 & X86_CR0_WP))
3385 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003386}
3387
Avi Kivity6aa8b732006-12-10 02:21:36 -08003388static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3389{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003390 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003391 unsigned long hw_cr0;
3392
Gleb Natapov50378782013-02-04 16:00:28 +02003393 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003394 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02003395 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003396 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003397 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003398
Gleb Natapov218e7632013-01-21 15:36:45 +02003399 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3400 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003401
Gleb Natapov218e7632013-01-21 15:36:45 +02003402 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3403 enter_rmode(vcpu);
3404 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003405
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003406#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003407 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003408 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003409 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003410 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003411 exit_lmode(vcpu);
3412 }
3413#endif
3414
Avi Kivity089d0342009-03-23 18:26:32 +02003415 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003416 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3417
Avi Kivity02daab22009-12-30 12:40:26 +02003418 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02003419 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02003420
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003422 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003423 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02003424
3425 /* depends on vcpu->arch.cr0 to be set to a new value */
3426 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003427}
3428
Sheng Yang14394422008-04-28 12:24:45 +08003429static u64 construct_eptp(unsigned long root_hpa)
3430{
3431 u64 eptp;
3432
3433 /* TODO write the value reading from MSR */
3434 eptp = VMX_EPT_DEFAULT_MT |
3435 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08003436 if (enable_ept_ad_bits)
3437 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003438 eptp |= (root_hpa & PAGE_MASK);
3439
3440 return eptp;
3441}
3442
Avi Kivity6aa8b732006-12-10 02:21:36 -08003443static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3444{
Sheng Yang14394422008-04-28 12:24:45 +08003445 unsigned long guest_cr3;
3446 u64 eptp;
3447
3448 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02003449 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003450 eptp = construct_eptp(cr3);
3451 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02003452 if (is_paging(vcpu) || is_guest_mode(vcpu))
3453 guest_cr3 = kvm_read_cr3(vcpu);
3454 else
3455 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02003456 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003457 }
3458
Sheng Yang2384d2b2008-01-17 15:14:33 +08003459 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003460 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003461}
3462
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003463static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003464{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003465 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08003466 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
3467
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003468 if (cr4 & X86_CR4_VMXE) {
3469 /*
3470 * To use VMXON (and later other VMX instructions), a guest
3471 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3472 * So basically the check on whether to allow nested VMX
3473 * is here.
3474 */
3475 if (!nested_vmx_allowed(vcpu))
3476 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01003477 }
3478 if (to_vmx(vcpu)->nested.vmxon &&
3479 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003480 return 1;
3481
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003482 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02003483 if (enable_ept) {
3484 if (!is_paging(vcpu)) {
3485 hw_cr4 &= ~X86_CR4_PAE;
3486 hw_cr4 |= X86_CR4_PSE;
Dongxiao Xuc08800a2013-02-04 11:50:43 +08003487 /*
Feng Wue1e746b2014-04-01 17:46:35 +08003488 * SMEP/SMAP is disabled if CPU is in non-paging mode
3489 * in hardware. However KVM always uses paging mode to
Dongxiao Xuc08800a2013-02-04 11:50:43 +08003490 * emulate guest non-paging mode with TDP.
Feng Wue1e746b2014-04-01 17:46:35 +08003491 * To emulate this behavior, SMEP/SMAP needs to be
3492 * manually disabled when guest switches to non-paging
3493 * mode.
Dongxiao Xuc08800a2013-02-04 11:50:43 +08003494 */
Feng Wue1e746b2014-04-01 17:46:35 +08003495 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP);
Avi Kivitybc230082009-12-08 12:14:42 +02003496 } else if (!(cr4 & X86_CR4_PAE)) {
3497 hw_cr4 &= ~X86_CR4_PAE;
3498 }
3499 }
Sheng Yang14394422008-04-28 12:24:45 +08003500
3501 vmcs_writel(CR4_READ_SHADOW, cr4);
3502 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003503 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003504}
3505
Avi Kivity6aa8b732006-12-10 02:21:36 -08003506static void vmx_get_segment(struct kvm_vcpu *vcpu,
3507 struct kvm_segment *var, int seg)
3508{
Avi Kivitya9179492011-01-03 14:28:52 +02003509 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003510 u32 ar;
3511
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003512 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003513 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02003514 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003515 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003516 return;
Avi Kivity1390a282012-08-21 17:07:08 +03003517 var->base = vmx_read_guest_seg_base(vmx, seg);
3518 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3519 return;
Avi Kivitya9179492011-01-03 14:28:52 +02003520 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003521 var->base = vmx_read_guest_seg_base(vmx, seg);
3522 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3523 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3524 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03003525 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003526 var->type = ar & 15;
3527 var->s = (ar >> 4) & 1;
3528 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03003529 /*
3530 * Some userspaces do not preserve unusable property. Since usable
3531 * segment has to be present according to VMX spec we can use present
3532 * property to amend userspace bug by making unusable segment always
3533 * nonpresent. vmx_segment_access_rights() already marks nonpresent
3534 * segment as unusable.
3535 */
3536 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003537 var->avl = (ar >> 12) & 1;
3538 var->l = (ar >> 13) & 1;
3539 var->db = (ar >> 14) & 1;
3540 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003541}
3542
Avi Kivitya9179492011-01-03 14:28:52 +02003543static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3544{
Avi Kivitya9179492011-01-03 14:28:52 +02003545 struct kvm_segment s;
3546
3547 if (to_vmx(vcpu)->rmode.vm86_active) {
3548 vmx_get_segment(vcpu, &s, seg);
3549 return s.base;
3550 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003551 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003552}
3553
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003554static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003555{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003556 struct vcpu_vmx *vmx = to_vmx(vcpu);
3557
Avi Kivity3eeb3282010-01-21 15:31:48 +02003558 if (!is_protmode(vcpu))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003559 return 0;
3560
Avi Kivityf4c63e52011-03-07 14:54:28 +02003561 if (!is_long_mode(vcpu)
3562 && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
Izik Eidus2e4d2652008-03-24 19:38:34 +02003563 return 3;
3564
Avi Kivity69c73022011-03-07 15:26:44 +02003565 if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
3566 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003567 vmx->cpl = vmx_read_guest_seg_selector(vmx, VCPU_SREG_CS) & 3;
Avi Kivity69c73022011-03-07 15:26:44 +02003568 }
Avi Kivityd881e6f2012-06-06 18:36:48 +03003569
3570 return vmx->cpl;
Avi Kivity69c73022011-03-07 15:26:44 +02003571}
3572
3573
Avi Kivity653e3102007-05-07 10:55:37 +03003574static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003575{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003576 u32 ar;
3577
Avi Kivityf0495f92012-06-07 17:06:10 +03003578 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003579 ar = 1 << 16;
3580 else {
3581 ar = var->type & 15;
3582 ar |= (var->s & 1) << 4;
3583 ar |= (var->dpl & 3) << 5;
3584 ar |= (var->present & 1) << 7;
3585 ar |= (var->avl & 1) << 12;
3586 ar |= (var->l & 1) << 13;
3587 ar |= (var->db & 1) << 14;
3588 ar |= (var->g & 1) << 15;
3589 }
Avi Kivity653e3102007-05-07 10:55:37 +03003590
3591 return ar;
3592}
3593
3594static void vmx_set_segment(struct kvm_vcpu *vcpu,
3595 struct kvm_segment *var, int seg)
3596{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003597 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02003598 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03003599
Avi Kivity2fb92db2011-04-27 19:42:18 +03003600 vmx_segment_cache_clear(vmx);
Gleb Natapov2f143242013-01-21 15:36:42 +02003601 if (seg == VCPU_SREG_CS)
3602 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity2fb92db2011-04-27 19:42:18 +03003603
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003604 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3605 vmx->rmode.segs[seg] = *var;
3606 if (seg == VCPU_SREG_TR)
3607 vmcs_write16(sf->selector, var->selector);
3608 else if (var->s)
3609 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003610 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03003611 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003612
Avi Kivity653e3102007-05-07 10:55:37 +03003613 vmcs_writel(sf->base, var->base);
3614 vmcs_write32(sf->limit, var->limit);
3615 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003616
3617 /*
3618 * Fix the "Accessed" bit in AR field of segment registers for older
3619 * qemu binaries.
3620 * IA32 arch specifies that at the time of processor reset the
3621 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08003622 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003623 * state vmexit when "unrestricted guest" mode is turned on.
3624 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3625 * tree. Newer qemu binaries with that qemu fix would not need this
3626 * kvm hack.
3627 */
3628 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02003629 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003630
Gleb Natapovf924d662012-12-12 19:10:55 +02003631 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02003632
3633out:
Gleb Natapov14168782013-01-21 15:36:49 +02003634 vmx->emulation_required |= emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003635}
3636
Avi Kivity6aa8b732006-12-10 02:21:36 -08003637static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3638{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003639 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003640
3641 *db = (ar >> 14) & 1;
3642 *l = (ar >> 13) & 1;
3643}
3644
Gleb Natapov89a27f42010-02-16 10:51:48 +02003645static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003646{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003647 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3648 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003649}
3650
Gleb Natapov89a27f42010-02-16 10:51:48 +02003651static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003652{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003653 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3654 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003655}
3656
Gleb Natapov89a27f42010-02-16 10:51:48 +02003657static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003658{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003659 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3660 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003661}
3662
Gleb Natapov89a27f42010-02-16 10:51:48 +02003663static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003664{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003665 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3666 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003667}
3668
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003669static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3670{
3671 struct kvm_segment var;
3672 u32 ar;
3673
3674 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02003675 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02003676 if (seg == VCPU_SREG_CS)
3677 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003678 ar = vmx_segment_access_rights(&var);
3679
3680 if (var.base != (var.selector << 4))
3681 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02003682 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003683 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02003684 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003685 return false;
3686
3687 return true;
3688}
3689
3690static bool code_segment_valid(struct kvm_vcpu *vcpu)
3691{
3692 struct kvm_segment cs;
3693 unsigned int cs_rpl;
3694
3695 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3696 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
3697
Avi Kivity1872a3f2009-01-04 23:26:52 +02003698 if (cs.unusable)
3699 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003700 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
3701 return false;
3702 if (!cs.s)
3703 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003704 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003705 if (cs.dpl > cs_rpl)
3706 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003707 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003708 if (cs.dpl != cs_rpl)
3709 return false;
3710 }
3711 if (!cs.present)
3712 return false;
3713
3714 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3715 return true;
3716}
3717
3718static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3719{
3720 struct kvm_segment ss;
3721 unsigned int ss_rpl;
3722
3723 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3724 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
3725
Avi Kivity1872a3f2009-01-04 23:26:52 +02003726 if (ss.unusable)
3727 return true;
3728 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003729 return false;
3730 if (!ss.s)
3731 return false;
3732 if (ss.dpl != ss_rpl) /* DPL != RPL */
3733 return false;
3734 if (!ss.present)
3735 return false;
3736
3737 return true;
3738}
3739
3740static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3741{
3742 struct kvm_segment var;
3743 unsigned int rpl;
3744
3745 vmx_get_segment(vcpu, &var, seg);
3746 rpl = var.selector & SELECTOR_RPL_MASK;
3747
Avi Kivity1872a3f2009-01-04 23:26:52 +02003748 if (var.unusable)
3749 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003750 if (!var.s)
3751 return false;
3752 if (!var.present)
3753 return false;
3754 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
3755 if (var.dpl < rpl) /* DPL < RPL */
3756 return false;
3757 }
3758
3759 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3760 * rights flags
3761 */
3762 return true;
3763}
3764
3765static bool tr_valid(struct kvm_vcpu *vcpu)
3766{
3767 struct kvm_segment tr;
3768
3769 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3770
Avi Kivity1872a3f2009-01-04 23:26:52 +02003771 if (tr.unusable)
3772 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003773 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3774 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003775 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003776 return false;
3777 if (!tr.present)
3778 return false;
3779
3780 return true;
3781}
3782
3783static bool ldtr_valid(struct kvm_vcpu *vcpu)
3784{
3785 struct kvm_segment ldtr;
3786
3787 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3788
Avi Kivity1872a3f2009-01-04 23:26:52 +02003789 if (ldtr.unusable)
3790 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003791 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3792 return false;
3793 if (ldtr.type != 2)
3794 return false;
3795 if (!ldtr.present)
3796 return false;
3797
3798 return true;
3799}
3800
3801static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3802{
3803 struct kvm_segment cs, ss;
3804
3805 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3806 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3807
3808 return ((cs.selector & SELECTOR_RPL_MASK) ==
3809 (ss.selector & SELECTOR_RPL_MASK));
3810}
3811
3812/*
3813 * Check if guest state is valid. Returns true if valid, false if
3814 * not.
3815 * We assume that registers are always usable
3816 */
3817static bool guest_state_valid(struct kvm_vcpu *vcpu)
3818{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02003819 if (enable_unrestricted_guest)
3820 return true;
3821
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003822 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03003823 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003824 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3825 return false;
3826 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3827 return false;
3828 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3829 return false;
3830 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3831 return false;
3832 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3833 return false;
3834 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3835 return false;
3836 } else {
3837 /* protected mode guest state checks */
3838 if (!cs_ss_rpl_check(vcpu))
3839 return false;
3840 if (!code_segment_valid(vcpu))
3841 return false;
3842 if (!stack_segment_valid(vcpu))
3843 return false;
3844 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3845 return false;
3846 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3847 return false;
3848 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3849 return false;
3850 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3851 return false;
3852 if (!tr_valid(vcpu))
3853 return false;
3854 if (!ldtr_valid(vcpu))
3855 return false;
3856 }
3857 /* TODO:
3858 * - Add checks on RIP
3859 * - Add checks on RFLAGS
3860 */
3861
3862 return true;
3863}
3864
Mike Dayd77c26f2007-10-08 09:02:08 -04003865static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003866{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003867 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02003868 u16 data = 0;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003869 int r, idx, ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003870
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003871 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003872 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02003873 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3874 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003875 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003876 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08003877 r = kvm_write_guest_page(kvm, fn++, &data,
3878 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02003879 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003880 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003881 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
3882 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003883 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003884 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3885 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003886 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003887 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003888 r = kvm_write_guest_page(kvm, fn, &data,
3889 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3890 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02003891 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003892 goto out;
3893
3894 ret = 1;
3895out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003896 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003897 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003898}
3899
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003900static int init_rmode_identity_map(struct kvm *kvm)
3901{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003902 int i, idx, r, ret;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003903 pfn_t identity_map_pfn;
3904 u32 tmp;
3905
Avi Kivity089d0342009-03-23 18:26:32 +02003906 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003907 return 1;
3908 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
3909 printk(KERN_ERR "EPT: identity-mapping pagetable "
3910 "haven't been allocated!\n");
3911 return 0;
3912 }
3913 if (likely(kvm->arch.ept_identity_pagetable_done))
3914 return 1;
3915 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003916 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003917 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003918 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3919 if (r < 0)
3920 goto out;
3921 /* Set up identity-mapping pagetable for EPT in real mode */
3922 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3923 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3924 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3925 r = kvm_write_guest_page(kvm, identity_map_pfn,
3926 &tmp, i * sizeof(tmp), sizeof(tmp));
3927 if (r < 0)
3928 goto out;
3929 }
3930 kvm->arch.ept_identity_pagetable_done = true;
3931 ret = 1;
3932out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003933 srcu_read_unlock(&kvm->srcu, idx);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003934 return ret;
3935}
3936
Avi Kivity6aa8b732006-12-10 02:21:36 -08003937static void seg_setup(int seg)
3938{
Mathias Krause772e0312012-08-30 01:30:19 +02003939 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003940 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003941
3942 vmcs_write16(sf->selector, 0);
3943 vmcs_writel(sf->base, 0);
3944 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02003945 ar = 0x93;
3946 if (seg == VCPU_SREG_CS)
3947 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003948
3949 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003950}
3951
Sheng Yangf78e0e22007-10-29 09:40:42 +08003952static int alloc_apic_access_page(struct kvm *kvm)
3953{
Xiao Guangrong44841412012-09-07 14:14:20 +08003954 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003955 struct kvm_userspace_memory_region kvm_userspace_mem;
3956 int r = 0;
3957
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003958 mutex_lock(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003959 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003960 goto out;
3961 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
3962 kvm_userspace_mem.flags = 0;
3963 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
3964 kvm_userspace_mem.memory_size = PAGE_SIZE;
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09003965 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003966 if (r)
3967 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003968
Xiao Guangrong44841412012-09-07 14:14:20 +08003969 page = gfn_to_page(kvm, 0xfee00);
3970 if (is_error_page(page)) {
3971 r = -EFAULT;
3972 goto out;
3973 }
3974
3975 kvm->arch.apic_access_page = page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003976out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003977 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003978 return r;
3979}
3980
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003981static int alloc_identity_pagetable(struct kvm *kvm)
3982{
Xiao Guangrong44841412012-09-07 14:14:20 +08003983 struct page *page;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003984 struct kvm_userspace_memory_region kvm_userspace_mem;
3985 int r = 0;
3986
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003987 mutex_lock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003988 if (kvm->arch.ept_identity_pagetable)
3989 goto out;
3990 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
3991 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003992 kvm_userspace_mem.guest_phys_addr =
3993 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003994 kvm_userspace_mem.memory_size = PAGE_SIZE;
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09003995 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003996 if (r)
3997 goto out;
3998
Xiao Guangrong44841412012-09-07 14:14:20 +08003999 page = gfn_to_page(kvm, kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
4000 if (is_error_page(page)) {
4001 r = -EFAULT;
4002 goto out;
4003 }
4004
4005 kvm->arch.ept_identity_pagetable = page;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004006out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004007 mutex_unlock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004008 return r;
4009}
4010
Sheng Yang2384d2b2008-01-17 15:14:33 +08004011static void allocate_vpid(struct vcpu_vmx *vmx)
4012{
4013 int vpid;
4014
4015 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02004016 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004017 return;
4018 spin_lock(&vmx_vpid_lock);
4019 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
4020 if (vpid < VMX_NR_VPIDS) {
4021 vmx->vpid = vpid;
4022 __set_bit(vpid, vmx_vpid_bitmap);
4023 }
4024 spin_unlock(&vmx_vpid_lock);
4025}
4026
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004027static void free_vpid(struct vcpu_vmx *vmx)
4028{
4029 if (!enable_vpid)
4030 return;
4031 spin_lock(&vmx_vpid_lock);
4032 if (vmx->vpid != 0)
4033 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
4034 spin_unlock(&vmx_vpid_lock);
4035}
4036
Yang Zhang8d146952013-01-25 10:18:50 +08004037#define MSR_TYPE_R 1
4038#define MSR_TYPE_W 2
4039static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4040 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004041{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004042 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004043
4044 if (!cpu_has_vmx_msr_bitmap())
4045 return;
4046
4047 /*
4048 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4049 * have the write-low and read-high bitmap offsets the wrong way round.
4050 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4051 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004052 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004053 if (type & MSR_TYPE_R)
4054 /* read-low */
4055 __clear_bit(msr, msr_bitmap + 0x000 / f);
4056
4057 if (type & MSR_TYPE_W)
4058 /* write-low */
4059 __clear_bit(msr, msr_bitmap + 0x800 / f);
4060
Sheng Yang25c5f222008-03-28 13:18:56 +08004061 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4062 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004063 if (type & MSR_TYPE_R)
4064 /* read-high */
4065 __clear_bit(msr, msr_bitmap + 0x400 / f);
4066
4067 if (type & MSR_TYPE_W)
4068 /* write-high */
4069 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4070
4071 }
4072}
4073
4074static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4075 u32 msr, int type)
4076{
4077 int f = sizeof(unsigned long);
4078
4079 if (!cpu_has_vmx_msr_bitmap())
4080 return;
4081
4082 /*
4083 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4084 * have the write-low and read-high bitmap offsets the wrong way round.
4085 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4086 */
4087 if (msr <= 0x1fff) {
4088 if (type & MSR_TYPE_R)
4089 /* read-low */
4090 __set_bit(msr, msr_bitmap + 0x000 / f);
4091
4092 if (type & MSR_TYPE_W)
4093 /* write-low */
4094 __set_bit(msr, msr_bitmap + 0x800 / f);
4095
4096 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4097 msr &= 0x1fff;
4098 if (type & MSR_TYPE_R)
4099 /* read-high */
4100 __set_bit(msr, msr_bitmap + 0x400 / f);
4101
4102 if (type & MSR_TYPE_W)
4103 /* write-high */
4104 __set_bit(msr, msr_bitmap + 0xc00 / f);
4105
Sheng Yang25c5f222008-03-28 13:18:56 +08004106 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004107}
4108
Avi Kivity58972972009-02-24 22:26:47 +02004109static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
4110{
4111 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08004112 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
4113 msr, MSR_TYPE_R | MSR_TYPE_W);
4114 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
4115 msr, MSR_TYPE_R | MSR_TYPE_W);
4116}
4117
4118static void vmx_enable_intercept_msr_read_x2apic(u32 msr)
4119{
4120 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4121 msr, MSR_TYPE_R);
4122 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4123 msr, MSR_TYPE_R);
4124}
4125
4126static void vmx_disable_intercept_msr_read_x2apic(u32 msr)
4127{
4128 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4129 msr, MSR_TYPE_R);
4130 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4131 msr, MSR_TYPE_R);
4132}
4133
4134static void vmx_disable_intercept_msr_write_x2apic(u32 msr)
4135{
4136 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4137 msr, MSR_TYPE_W);
4138 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4139 msr, MSR_TYPE_W);
Avi Kivity58972972009-02-24 22:26:47 +02004140}
4141
Yang Zhang01e439b2013-04-11 19:25:12 +08004142static int vmx_vm_has_apicv(struct kvm *kvm)
4143{
4144 return enable_apicv && irqchip_in_kernel(kvm);
4145}
4146
Avi Kivity6aa8b732006-12-10 02:21:36 -08004147/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004148 * Send interrupt to vcpu via posted interrupt way.
4149 * 1. If target vcpu is running(non-root mode), send posted interrupt
4150 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4151 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4152 * interrupt from PIR in next vmentry.
4153 */
4154static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4155{
4156 struct vcpu_vmx *vmx = to_vmx(vcpu);
4157 int r;
4158
4159 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4160 return;
4161
4162 r = pi_test_and_set_on(&vmx->pi_desc);
4163 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang, Yang Z6ffbbbb2013-04-17 23:11:54 -03004164#ifdef CONFIG_SMP
Yang Zhanga20ed542013-04-11 19:25:15 +08004165 if (!r && (vcpu->mode == IN_GUEST_MODE))
4166 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
4167 POSTED_INTR_VECTOR);
4168 else
Zhang, Yang Z6ffbbbb2013-04-17 23:11:54 -03004169#endif
Yang Zhanga20ed542013-04-11 19:25:15 +08004170 kvm_vcpu_kick(vcpu);
4171}
4172
4173static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
4174{
4175 struct vcpu_vmx *vmx = to_vmx(vcpu);
4176
4177 if (!pi_test_and_clear_on(&vmx->pi_desc))
4178 return;
4179
4180 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
4181}
4182
4183static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu)
4184{
4185 return;
4186}
4187
Avi Kivity6aa8b732006-12-10 02:21:36 -08004188/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004189 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4190 * will not change in the lifetime of the guest.
4191 * Note that host-state that does change is set elsewhere. E.g., host-state
4192 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4193 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004194static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004195{
4196 u32 low32, high32;
4197 unsigned long tmpl;
4198 struct desc_ptr dt;
4199
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07004200 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004201 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
4202 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
4203
4204 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004205#ifdef CONFIG_X86_64
4206 /*
4207 * Load null selectors, so we can avoid reloading them in
4208 * __vmx_load_host_state(), in case userspace uses the null selectors
4209 * too (the expected case).
4210 */
4211 vmcs_write16(HOST_DS_SELECTOR, 0);
4212 vmcs_write16(HOST_ES_SELECTOR, 0);
4213#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004214 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4215 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004216#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004217 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4218 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4219
4220 native_store_idt(&dt);
4221 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004222 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004223
Avi Kivity83287ea422012-09-16 15:10:57 +03004224 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004225
4226 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4227 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4228 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4229 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4230
4231 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4232 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4233 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4234 }
4235}
4236
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004237static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4238{
4239 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4240 if (enable_ept)
4241 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004242 if (is_guest_mode(&vmx->vcpu))
4243 vmx->vcpu.arch.cr4_guest_owned_bits &=
4244 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004245 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4246}
4247
Yang Zhang01e439b2013-04-11 19:25:12 +08004248static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4249{
4250 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4251
4252 if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
4253 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
4254 return pin_based_exec_ctrl;
4255}
4256
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004257static u32 vmx_exec_control(struct vcpu_vmx *vmx)
4258{
4259 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01004260
4261 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4262 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4263
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004264 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
4265 exec_control &= ~CPU_BASED_TPR_SHADOW;
4266#ifdef CONFIG_X86_64
4267 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4268 CPU_BASED_CR8_LOAD_EXITING;
4269#endif
4270 }
4271 if (!enable_ept)
4272 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4273 CPU_BASED_CR3_LOAD_EXITING |
4274 CPU_BASED_INVLPG_EXITING;
4275 return exec_control;
4276}
4277
4278static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
4279{
4280 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
4281 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
4282 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4283 if (vmx->vpid == 0)
4284 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4285 if (!enable_ept) {
4286 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4287 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00004288 /* Enable INVPCID for non-ept guests may cause performance regression. */
4289 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004290 }
4291 if (!enable_unrestricted_guest)
4292 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
4293 if (!ple_gap)
4294 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Yang Zhangc7c9c562013-01-25 10:18:51 +08004295 if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
4296 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4297 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004298 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03004299 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4300 (handle_vmptrld).
4301 We can NOT enable shadow_vmcs here because we don't have yet
4302 a current VMCS12
4303 */
4304 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004305 return exec_control;
4306}
4307
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004308static void ept_set_mmio_spte_mask(void)
4309{
4310 /*
4311 * EPT Misconfigurations can be generated if the value of bits 2:0
4312 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08004313 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004314 * spte.
4315 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08004316 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004317}
4318
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004319/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004320 * Sets up the vmcs for emulated real mode.
4321 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10004322static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004323{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004324#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004325 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004326#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004327 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004328
Avi Kivity6aa8b732006-12-10 02:21:36 -08004329 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004330 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
4331 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004332
Abel Gordon4607c2d2013-04-18 14:35:55 +03004333 if (enable_shadow_vmcs) {
4334 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
4335 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
4336 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004337 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02004338 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08004339
Avi Kivity6aa8b732006-12-10 02:21:36 -08004340 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4341
Avi Kivity6aa8b732006-12-10 02:21:36 -08004342 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08004343 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004344
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004345 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004346
Sheng Yang83ff3b92007-11-21 14:33:25 +08004347 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004348 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4349 vmx_secondary_exec_control(vmx));
Sheng Yang83ff3b92007-11-21 14:33:25 +08004350 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08004351
Yang Zhang01e439b2013-04-11 19:25:12 +08004352 if (vmx_vm_has_apicv(vmx->vcpu.kvm)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08004353 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4354 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4355 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4356 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4357
4358 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08004359
4360 vmcs_write64(POSTED_INTR_NV, POSTED_INTR_VECTOR);
4361 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08004362 }
4363
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004364 if (ple_gap) {
4365 vmcs_write32(PLE_GAP, ple_gap);
4366 vmcs_write32(PLE_WINDOW, ple_window);
4367 }
4368
Xiao Guangrongc3707952011-07-12 03:28:04 +08004369 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4370 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004371 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4372
Avi Kivity9581d442010-10-19 16:46:55 +02004373 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4374 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004375 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004376#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004377 rdmsrl(MSR_FS_BASE, a);
4378 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
4379 rdmsrl(MSR_GS_BASE, a);
4380 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
4381#else
4382 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4383 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
4384#endif
4385
Eddie Dong2cc51562007-05-21 07:28:09 +03004386 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4387 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004388 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03004389 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004390 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004391
Sheng Yang468d4722008-10-09 16:01:55 +08004392 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004393 u32 msr_low, msr_high;
4394 u64 host_pat;
Sheng Yang468d4722008-10-09 16:01:55 +08004395 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
4396 host_pat = msr_low | ((u64) msr_high << 32);
4397 /* Write the default value follow host pat */
4398 vmcs_write64(GUEST_IA32_PAT, host_pat);
4399 /* Keep arch.pat sync with GUEST_IA32_PAT */
4400 vmx->vcpu.arch.pat = host_pat;
4401 }
4402
Avi Kivity6aa8b732006-12-10 02:21:36 -08004403 for (i = 0; i < NR_VMX_MSR; ++i) {
4404 u32 index = vmx_msr_index[i];
4405 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004406 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004407
4408 if (rdmsr_safe(index, &data_low, &data_high) < 0)
4409 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08004410 if (wrmsr_safe(index, data_low, data_high) < 0)
4411 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03004412 vmx->guest_msrs[j].index = i;
4413 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02004414 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004415 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004416 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004417
Gleb Natapov2961e8762013-11-25 15:37:13 +02004418
4419 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004420
4421 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02004422 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004423
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004424 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004425 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004426
4427 return 0;
4428}
4429
Jan Kiszka57f252f2013-03-12 10:20:24 +01004430static void vmx_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004431{
4432 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004433 struct msr_data apic_base_msr;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004434
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004435 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004436
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004437 vmx->soft_vnmi_blocked = 0;
4438
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004439 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004440 kvm_set_cr8(&vmx->vcpu, 0);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004441 apic_base_msr.data = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03004442 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Jan Kiszka58cb6282014-01-24 16:48:44 +01004443 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
4444 apic_base_msr.host_initiated = true;
4445 kvm_set_apic_base(&vmx->vcpu, &apic_base_msr);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004446
Avi Kivity2fb92db2011-04-27 19:42:18 +03004447 vmx_segment_cache_clear(vmx);
4448
Avi Kivity5706be02008-08-20 15:07:31 +03004449 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01004450 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzini04b66832013-03-19 16:30:26 +01004451 vmcs_write32(GUEST_CS_BASE, 0xffff0000);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004452
4453 seg_setup(VCPU_SREG_DS);
4454 seg_setup(VCPU_SREG_ES);
4455 seg_setup(VCPU_SREG_FS);
4456 seg_setup(VCPU_SREG_GS);
4457 seg_setup(VCPU_SREG_SS);
4458
4459 vmcs_write16(GUEST_TR_SELECTOR, 0);
4460 vmcs_writel(GUEST_TR_BASE, 0);
4461 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4462 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4463
4464 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4465 vmcs_writel(GUEST_LDTR_BASE, 0);
4466 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4467 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4468
4469 vmcs_write32(GUEST_SYSENTER_CS, 0);
4470 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4471 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4472
4473 vmcs_writel(GUEST_RFLAGS, 0x02);
Jan Kiszka66450a22013-03-13 12:42:34 +01004474 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004475
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004476 vmcs_writel(GUEST_GDTR_BASE, 0);
4477 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4478
4479 vmcs_writel(GUEST_IDTR_BASE, 0);
4480 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4481
Anthony Liguori443381a2010-12-06 10:53:38 -06004482 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004483 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
4484 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
4485
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004486 /* Special registers */
4487 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4488
4489 setup_msrs(vmx);
4490
Avi Kivity6aa8b732006-12-10 02:21:36 -08004491 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4492
Sheng Yangf78e0e22007-10-29 09:40:42 +08004493 if (cpu_has_vmx_tpr_shadow()) {
4494 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
4495 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
4496 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09004497 __pa(vmx->vcpu.arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004498 vmcs_write32(TPR_THRESHOLD, 0);
4499 }
4500
4501 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
4502 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08004503 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004504
Yang Zhang01e439b2013-04-11 19:25:12 +08004505 if (vmx_vm_has_apicv(vcpu->kvm))
4506 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
4507
Sheng Yang2384d2b2008-01-17 15:14:33 +08004508 if (vmx->vpid != 0)
4509 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4510
Eduardo Habkostfa400522009-10-24 02:49:58 -02004511 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Avi Kivity4d4ec082009-12-29 18:07:30 +02004512 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10004513 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10004514 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10004515 vmx_fpu_activate(&vmx->vcpu);
4516 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004517
Gui Jianfengb9d762f2010-06-07 10:32:29 +08004518 vpid_sync_context(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004519}
4520
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004521/*
4522 * In nested virtualization, check if L1 asked to exit on external interrupts.
4523 * For most existing hypervisors, this will always return true.
4524 */
4525static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
4526{
4527 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4528 PIN_BASED_EXT_INTR_MASK;
4529}
4530
Bandan Das77b0f5d2014-04-19 18:17:45 -04004531/*
4532 * In nested virtualization, check if L1 has set
4533 * VM_EXIT_ACK_INTR_ON_EXIT
4534 */
4535static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
4536{
4537 return get_vmcs12(vcpu)->vm_exit_controls &
4538 VM_EXIT_ACK_INTR_ON_EXIT;
4539}
4540
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004541static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
4542{
4543 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4544 PIN_BASED_NMI_EXITING;
4545}
4546
Jan Kiszkac9a79532014-03-07 20:03:15 +01004547static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004548{
4549 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02004550
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004551 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4552 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
4553 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4554}
4555
Jan Kiszkac9a79532014-03-07 20:03:15 +01004556static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004557{
4558 u32 cpu_based_vm_exec_control;
4559
Jan Kiszkac9a79532014-03-07 20:03:15 +01004560 if (!cpu_has_virtual_nmis() ||
4561 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
4562 enable_irq_window(vcpu);
4563 return;
4564 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02004565
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004566 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4567 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
4568 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4569}
4570
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004571static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004572{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004573 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004574 uint32_t intr;
4575 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004576
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004577 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004578
Avi Kivityfa89a812008-09-01 15:57:51 +03004579 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004580 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004581 int inc_eip = 0;
4582 if (vcpu->arch.interrupt.soft)
4583 inc_eip = vcpu->arch.event_exit_inst_len;
4584 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004585 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004586 return;
4587 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004588 intr = irq | INTR_INFO_VALID_MASK;
4589 if (vcpu->arch.interrupt.soft) {
4590 intr |= INTR_TYPE_SOFT_INTR;
4591 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4592 vmx->vcpu.arch.event_exit_inst_len);
4593 } else
4594 intr |= INTR_TYPE_EXT_INTR;
4595 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03004596}
4597
Sheng Yangf08864b2008-05-15 18:23:25 +08004598static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4599{
Jan Kiszka66a5a342008-09-26 09:30:51 +02004600 struct vcpu_vmx *vmx = to_vmx(vcpu);
4601
Nadav Har'El0b6ac342011-05-25 23:13:36 +03004602 if (is_guest_mode(vcpu))
4603 return;
4604
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004605 if (!cpu_has_virtual_nmis()) {
4606 /*
4607 * Tracking the NMI-blocked state in software is built upon
4608 * finding the next open IRQ window. This, in turn, depends on
4609 * well-behaving guests: They have to keep IRQs disabled at
4610 * least as long as the NMI handler runs. Otherwise we may
4611 * cause NMI nesting, maybe breaking the guest. But as this is
4612 * highly unlikely, we can live with the residual risk.
4613 */
4614 vmx->soft_vnmi_blocked = 1;
4615 vmx->vnmi_blocked_time = 0;
4616 }
4617
Jan Kiszka487b3912008-09-26 09:30:56 +02004618 ++vcpu->stat.nmi_injections;
Avi Kivity9d58b932011-03-07 16:52:07 +02004619 vmx->nmi_known_unmasked = false;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004620 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004621 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004622 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02004623 return;
4624 }
Sheng Yangf08864b2008-05-15 18:23:25 +08004625 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4626 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08004627}
4628
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004629static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
4630{
4631 if (!cpu_has_virtual_nmis())
4632 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02004633 if (to_vmx(vcpu)->nmi_known_unmasked)
4634 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03004635 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004636}
4637
4638static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
4639{
4640 struct vcpu_vmx *vmx = to_vmx(vcpu);
4641
4642 if (!cpu_has_virtual_nmis()) {
4643 if (vmx->soft_vnmi_blocked != masked) {
4644 vmx->soft_vnmi_blocked = masked;
4645 vmx->vnmi_blocked_time = 0;
4646 }
4647 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02004648 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004649 if (masked)
4650 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4651 GUEST_INTR_STATE_NMI);
4652 else
4653 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4654 GUEST_INTR_STATE_NMI);
4655 }
4656}
4657
Jan Kiszka2505dc92013-04-14 12:12:47 +02004658static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
4659{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004660 if (to_vmx(vcpu)->nested.nested_run_pending)
4661 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004662
Jan Kiszka2505dc92013-04-14 12:12:47 +02004663 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
4664 return 0;
4665
4666 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4667 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
4668 | GUEST_INTR_STATE_NMI));
4669}
4670
Gleb Natapov78646122009-03-23 12:12:11 +02004671static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
4672{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004673 return (!to_vmx(vcpu)->nested.nested_run_pending &&
4674 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03004675 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4676 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02004677}
4678
Izik Eiduscbc94022007-10-25 00:29:55 +02004679static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4680{
4681 int ret;
4682 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08004683 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02004684 .guest_phys_addr = addr,
4685 .memory_size = PAGE_SIZE * 3,
4686 .flags = 0,
4687 };
4688
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09004689 ret = kvm_set_memory_region(kvm, &tss_mem);
Izik Eiduscbc94022007-10-25 00:29:55 +02004690 if (ret)
4691 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08004692 kvm->arch.tss_addr = addr;
Gleb Natapov93ea5382011-02-21 12:07:59 +02004693 if (!init_rmode_tss(kvm))
4694 return -ENOMEM;
4695
Izik Eiduscbc94022007-10-25 00:29:55 +02004696 return 0;
4697}
4698
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004699static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004700{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004701 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004702 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004703 /*
4704 * Update instruction length as we may reinject the exception
4705 * from user space while in guest debugging mode.
4706 */
4707 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4708 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004709 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004710 return false;
4711 /* fall through */
4712 case DB_VECTOR:
4713 if (vcpu->guest_debug &
4714 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
4715 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004716 /* fall through */
4717 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004718 case OF_VECTOR:
4719 case BR_VECTOR:
4720 case UD_VECTOR:
4721 case DF_VECTOR:
4722 case SS_VECTOR:
4723 case GP_VECTOR:
4724 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004725 return true;
4726 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004727 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004728 return false;
4729}
4730
4731static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4732 int vec, u32 err_code)
4733{
4734 /*
4735 * Instruction with address size override prefix opcode 0x67
4736 * Cause the #SS fault with 0 error code in VM86 mode.
4737 */
4738 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
4739 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
4740 if (vcpu->arch.halt_request) {
4741 vcpu->arch.halt_request = 0;
4742 return kvm_emulate_halt(vcpu);
4743 }
4744 return 1;
4745 }
4746 return 0;
4747 }
4748
4749 /*
4750 * Forward all other exceptions that are valid in real mode.
4751 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4752 * the required debugging infrastructure rework.
4753 */
4754 kvm_queue_exception(vcpu, vec);
4755 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004756}
4757
Andi Kleena0861c02009-06-08 17:37:09 +08004758/*
4759 * Trigger machine check on the host. We assume all the MSRs are already set up
4760 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4761 * We pass a fake environment to the machine check handler because we want
4762 * the guest to be always treated like user space, no matter what context
4763 * it used internally.
4764 */
4765static void kvm_machine_check(void)
4766{
4767#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
4768 struct pt_regs regs = {
4769 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4770 .flags = X86_EFLAGS_IF,
4771 };
4772
4773 do_machine_check(&regs, 0);
4774#endif
4775}
4776
Avi Kivity851ba692009-08-24 11:10:17 +03004777static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004778{
4779 /* already handled by vcpu_run */
4780 return 1;
4781}
4782
Avi Kivity851ba692009-08-24 11:10:17 +03004783static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004784{
Avi Kivity1155f762007-11-22 11:30:47 +02004785 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004786 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004787 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004788 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004789 u32 vect_info;
4790 enum emulation_result er;
4791
Avi Kivity1155f762007-11-22 11:30:47 +02004792 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02004793 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004794
Andi Kleena0861c02009-06-08 17:37:09 +08004795 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03004796 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08004797
Jan Kiszkae4a41882008-09-26 09:30:46 +02004798 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02004799 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004800
4801 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03004802 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004803 return 1;
4804 }
4805
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004806 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01004807 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004808 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02004809 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004810 return 1;
4811 }
4812
Avi Kivity6aa8b732006-12-10 02:21:36 -08004813 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004814 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004815 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004816
4817 /*
4818 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4819 * MMIO, it is better to report an internal error.
4820 * See the comments in vmx_handle_exit.
4821 */
4822 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4823 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4824 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4825 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
4826 vcpu->run->internal.ndata = 2;
4827 vcpu->run->internal.data[0] = vect_info;
4828 vcpu->run->internal.data[1] = intr_info;
4829 return 0;
4830 }
4831
Avi Kivity6aa8b732006-12-10 02:21:36 -08004832 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08004833 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02004834 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004835 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004836 trace_kvm_page_fault(cr2, error_code);
4837
Gleb Natapov3298b752009-05-11 13:35:46 +03004838 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03004839 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01004840 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004841 }
4842
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004843 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004844
4845 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4846 return handle_rmode_exception(vcpu, ex_no, error_code);
4847
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004848 switch (ex_no) {
4849 case DB_VECTOR:
4850 dr6 = vmcs_readl(EXIT_QUALIFICATION);
4851 if (!(vcpu->guest_debug &
4852 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01004853 vcpu->arch.dr6 &= ~15;
4854 vcpu->arch.dr6 |= dr6;
Huw Daviesfd2a4452014-04-16 10:02:51 +01004855 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
4856 skip_emulated_instruction(vcpu);
4857
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004858 kvm_queue_exception(vcpu, DB_VECTOR);
4859 return 1;
4860 }
4861 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
4862 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
4863 /* fall through */
4864 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004865 /*
4866 * Update instruction length as we may reinject #BP from
4867 * user space while in guest debugging mode. Reading it for
4868 * #DB as well causes no harm, it is not used in that case.
4869 */
4870 vmx->vcpu.arch.event_exit_inst_len =
4871 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004872 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004873 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004874 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4875 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004876 break;
4877 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004878 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4879 kvm_run->ex.exception = ex_no;
4880 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004881 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004882 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004883 return 0;
4884}
4885
Avi Kivity851ba692009-08-24 11:10:17 +03004886static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004887{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004888 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004889 return 1;
4890}
4891
Avi Kivity851ba692009-08-24 11:10:17 +03004892static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004893{
Avi Kivity851ba692009-08-24 11:10:17 +03004894 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08004895 return 0;
4896}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004897
Avi Kivity851ba692009-08-24 11:10:17 +03004898static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004899{
He, Qingbfdaab02007-09-12 14:18:28 +08004900 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01004901 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004902 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004903
He, Qingbfdaab02007-09-12 14:18:28 +08004904 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02004905 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004906 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004907
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004908 ++vcpu->stat.io_exits;
4909
4910 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01004911 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004912
4913 port = exit_qualification >> 16;
4914 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01004915 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004916
4917 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004918}
4919
Ingo Molnar102d8322007-02-19 14:37:47 +02004920static void
4921vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4922{
4923 /*
4924 * Patch in the VMCALL instruction:
4925 */
4926 hypercall[0] = 0x0f;
4927 hypercall[1] = 0x01;
4928 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02004929}
4930
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02004931static bool nested_cr0_valid(struct vmcs12 *vmcs12, unsigned long val)
4932{
4933 unsigned long always_on = VMXON_CR0_ALWAYSON;
4934
4935 if (nested_vmx_secondary_ctls_high &
4936 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4937 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4938 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
4939 return (val & always_on) == always_on;
4940}
4941
Guo Chao0fa06072012-06-28 15:16:19 +08004942/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004943static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4944{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004945 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004946 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4947 unsigned long orig_val = val;
4948
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004949 /*
4950 * We get here when L2 changed cr0 in a way that did not change
4951 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004952 * but did change L0 shadowed bits. So we first calculate the
4953 * effective cr0 value that L1 would like to write into the
4954 * hardware. It consists of the L2-owned bits from the new
4955 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004956 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004957 val = (val & ~vmcs12->cr0_guest_host_mask) |
4958 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
4959
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02004960 if (!nested_cr0_valid(vmcs12, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004961 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004962
4963 if (kvm_set_cr0(vcpu, val))
4964 return 1;
4965 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004966 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004967 } else {
4968 if (to_vmx(vcpu)->nested.vmxon &&
4969 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
4970 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004971 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004972 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004973}
4974
4975static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
4976{
4977 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004978 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4979 unsigned long orig_val = val;
4980
4981 /* analogously to handle_set_cr0 */
4982 val = (val & ~vmcs12->cr4_guest_host_mask) |
4983 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
4984 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004985 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004986 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004987 return 0;
4988 } else
4989 return kvm_set_cr4(vcpu, val);
4990}
4991
4992/* called to set cr0 as approriate for clts instruction exit. */
4993static void handle_clts(struct kvm_vcpu *vcpu)
4994{
4995 if (is_guest_mode(vcpu)) {
4996 /*
4997 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
4998 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
4999 * just pretend it's off (also in arch.cr0 for fpu_activate).
5000 */
5001 vmcs_writel(CR0_READ_SHADOW,
5002 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
5003 vcpu->arch.cr0 &= ~X86_CR0_TS;
5004 } else
5005 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
5006}
5007
Avi Kivity851ba692009-08-24 11:10:17 +03005008static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005009{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005010 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005011 int cr;
5012 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005013 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005014
He, Qingbfdaab02007-09-12 14:18:28 +08005015 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005016 cr = exit_qualification & 15;
5017 reg = (exit_qualification >> 8) & 15;
5018 switch ((exit_qualification >> 4) & 3) {
5019 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005020 val = kvm_register_read(vcpu, reg);
5021 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005022 switch (cr) {
5023 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005024 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005025 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005026 return 1;
5027 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005028 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005029 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005030 return 1;
5031 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005032 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005033 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005034 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005035 case 8: {
5036 u8 cr8_prev = kvm_get_cr8(vcpu);
5037 u8 cr8 = kvm_register_read(vcpu, reg);
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005038 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005039 kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005040 if (irqchip_in_kernel(vcpu->kvm))
5041 return 1;
5042 if (cr8_prev <= cr8)
5043 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03005044 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005045 return 0;
5046 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005047 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005048 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005049 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005050 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02005051 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03005052 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02005053 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03005054 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005055 case 1: /*mov from cr*/
5056 switch (cr) {
5057 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005058 val = kvm_read_cr3(vcpu);
5059 kvm_register_write(vcpu, reg, val);
5060 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005061 skip_emulated_instruction(vcpu);
5062 return 1;
5063 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005064 val = kvm_get_cr8(vcpu);
5065 kvm_register_write(vcpu, reg, val);
5066 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005067 skip_emulated_instruction(vcpu);
5068 return 1;
5069 }
5070 break;
5071 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005072 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005073 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005074 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005075
5076 skip_emulated_instruction(vcpu);
5077 return 1;
5078 default:
5079 break;
5080 }
Avi Kivity851ba692009-08-24 11:10:17 +03005081 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005082 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005083 (int)(exit_qualification >> 4) & 3, cr);
5084 return 0;
5085}
5086
Avi Kivity851ba692009-08-24 11:10:17 +03005087static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005088{
He, Qingbfdaab02007-09-12 14:18:28 +08005089 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005090 int dr, reg;
5091
Jan Kiszkaf2483412010-01-20 18:20:20 +01005092 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005093 if (!kvm_require_cpl(vcpu, 0))
5094 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005095 dr = vmcs_readl(GUEST_DR7);
5096 if (dr & DR7_GD) {
5097 /*
5098 * As the vm-exit takes precedence over the debug trap, we
5099 * need to emulate the latter, either for the host or the
5100 * guest debugging itself.
5101 */
5102 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03005103 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
5104 vcpu->run->debug.arch.dr7 = dr;
5105 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005106 vmcs_readl(GUEST_CS_BASE) +
5107 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03005108 vcpu->run->debug.arch.exception = DB_VECTOR;
5109 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005110 return 0;
5111 } else {
5112 vcpu->arch.dr7 &= ~DR7_GD;
5113 vcpu->arch.dr6 |= DR6_BD;
5114 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
5115 kvm_queue_exception(vcpu, DB_VECTOR);
5116 return 1;
5117 }
5118 }
5119
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005120 if (vcpu->guest_debug == 0) {
5121 u32 cpu_based_vm_exec_control;
5122
5123 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5124 cpu_based_vm_exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5125 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5126
5127 /*
5128 * No more DR vmexits; force a reload of the debug registers
5129 * and reenter on this instruction. The next vmexit will
5130 * retrieve the full state of the debug registers.
5131 */
5132 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5133 return 1;
5134 }
5135
He, Qingbfdaab02007-09-12 14:18:28 +08005136 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005137 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5138 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5139 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005140 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005141
5142 if (kvm_get_dr(vcpu, dr, &val))
5143 return 1;
5144 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005145 } else
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005146 if (kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]))
5147 return 1;
5148
Avi Kivity6aa8b732006-12-10 02:21:36 -08005149 skip_emulated_instruction(vcpu);
5150 return 1;
5151}
5152
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005153static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5154{
5155 return vcpu->arch.dr6;
5156}
5157
5158static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5159{
5160}
5161
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005162static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5163{
5164 u32 cpu_based_vm_exec_control;
5165
5166 get_debugreg(vcpu->arch.db[0], 0);
5167 get_debugreg(vcpu->arch.db[1], 1);
5168 get_debugreg(vcpu->arch.db[2], 2);
5169 get_debugreg(vcpu->arch.db[3], 3);
5170 get_debugreg(vcpu->arch.dr6, 6);
5171 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5172
5173 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
5174
5175 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5176 cpu_based_vm_exec_control |= CPU_BASED_MOV_DR_EXITING;
5177 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5178}
5179
Gleb Natapov020df072010-04-13 10:05:23 +03005180static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5181{
5182 vmcs_writel(GUEST_DR7, val);
5183}
5184
Avi Kivity851ba692009-08-24 11:10:17 +03005185static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005186{
Avi Kivity06465c52007-02-28 20:46:53 +02005187 kvm_emulate_cpuid(vcpu);
5188 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005189}
5190
Avi Kivity851ba692009-08-24 11:10:17 +03005191static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005192{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005193 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08005194 u64 data;
5195
5196 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivity59200272010-01-25 19:47:02 +02005197 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005198 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005199 return 1;
5200 }
5201
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005202 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005203
Avi Kivity6aa8b732006-12-10 02:21:36 -08005204 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005205 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
5206 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005207 skip_emulated_instruction(vcpu);
5208 return 1;
5209}
5210
Avi Kivity851ba692009-08-24 11:10:17 +03005211static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005212{
Will Auld8fe8ab42012-11-29 12:42:12 -08005213 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005214 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
5215 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
5216 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005217
Will Auld8fe8ab42012-11-29 12:42:12 -08005218 msr.data = data;
5219 msr.index = ecx;
5220 msr.host_initiated = false;
5221 if (vmx_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02005222 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005223 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005224 return 1;
5225 }
5226
Avi Kivity59200272010-01-25 19:47:02 +02005227 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005228 skip_emulated_instruction(vcpu);
5229 return 1;
5230}
5231
Avi Kivity851ba692009-08-24 11:10:17 +03005232static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005233{
Avi Kivity3842d132010-07-27 12:30:24 +03005234 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005235 return 1;
5236}
5237
Avi Kivity851ba692009-08-24 11:10:17 +03005238static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005239{
Eddie Dong85f455f2007-07-06 12:20:49 +03005240 u32 cpu_based_vm_exec_control;
5241
5242 /* clear pending irq */
5243 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5244 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
5245 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005246
Avi Kivity3842d132010-07-27 12:30:24 +03005247 kvm_make_request(KVM_REQ_EVENT, vcpu);
5248
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005249 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005250
Dor Laorc1150d82007-01-05 16:36:24 -08005251 /*
5252 * If the user space waits to inject interrupts, exit as soon as
5253 * possible
5254 */
Gleb Natapov80618232009-04-21 17:44:56 +03005255 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03005256 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03005257 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03005258 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08005259 return 0;
5260 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005261 return 1;
5262}
5263
Avi Kivity851ba692009-08-24 11:10:17 +03005264static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005265{
5266 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03005267 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005268}
5269
Avi Kivity851ba692009-08-24 11:10:17 +03005270static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005271{
Dor Laor510043d2007-02-19 18:25:43 +02005272 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005273 kvm_emulate_hypercall(vcpu);
5274 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02005275}
5276
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005277static int handle_invd(struct kvm_vcpu *vcpu)
5278{
Andre Przywara51d8b662010-12-21 11:12:02 +01005279 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005280}
5281
Avi Kivity851ba692009-08-24 11:10:17 +03005282static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005283{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005284 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005285
5286 kvm_mmu_invlpg(vcpu, exit_qualification);
5287 skip_emulated_instruction(vcpu);
5288 return 1;
5289}
5290
Avi Kivityfee84b02011-11-10 14:57:25 +02005291static int handle_rdpmc(struct kvm_vcpu *vcpu)
5292{
5293 int err;
5294
5295 err = kvm_rdpmc(vcpu);
5296 kvm_complete_insn_gp(vcpu, err);
5297
5298 return 1;
5299}
5300
Avi Kivity851ba692009-08-24 11:10:17 +03005301static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005302{
5303 skip_emulated_instruction(vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005304 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005305 return 1;
5306}
5307
Dexuan Cui2acf9232010-06-10 11:27:12 +08005308static int handle_xsetbv(struct kvm_vcpu *vcpu)
5309{
5310 u64 new_bv = kvm_read_edx_eax(vcpu);
5311 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
5312
5313 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
5314 skip_emulated_instruction(vcpu);
5315 return 1;
5316}
5317
Avi Kivity851ba692009-08-24 11:10:17 +03005318static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005319{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005320 if (likely(fasteoi)) {
5321 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5322 int access_type, offset;
5323
5324 access_type = exit_qualification & APIC_ACCESS_TYPE;
5325 offset = exit_qualification & APIC_ACCESS_OFFSET;
5326 /*
5327 * Sane guest uses MOV to write EOI, with written value
5328 * not cared. So make a short-circuit here by avoiding
5329 * heavy instruction emulation.
5330 */
5331 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5332 (offset == APIC_EOI)) {
5333 kvm_lapic_set_eoi(vcpu);
5334 skip_emulated_instruction(vcpu);
5335 return 1;
5336 }
5337 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005338 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005339}
5340
Yang Zhangc7c9c562013-01-25 10:18:51 +08005341static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5342{
5343 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5344 int vector = exit_qualification & 0xff;
5345
5346 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5347 kvm_apic_set_eoi_accelerated(vcpu, vector);
5348 return 1;
5349}
5350
Yang Zhang83d4c282013-01-25 10:18:49 +08005351static int handle_apic_write(struct kvm_vcpu *vcpu)
5352{
5353 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5354 u32 offset = exit_qualification & 0xfff;
5355
5356 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5357 kvm_apic_write_nodecode(vcpu, offset);
5358 return 1;
5359}
5360
Avi Kivity851ba692009-08-24 11:10:17 +03005361static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02005362{
Jan Kiszka60637aa2008-09-26 09:30:47 +02005363 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005364 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02005365 bool has_error_code = false;
5366 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02005367 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005368 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005369
5370 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005371 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005372 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02005373
5374 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5375
5376 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005377 if (reason == TASK_SWITCH_GATE && idt_v) {
5378 switch (type) {
5379 case INTR_TYPE_NMI_INTR:
5380 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02005381 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005382 break;
5383 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005384 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005385 kvm_clear_interrupt_queue(vcpu);
5386 break;
5387 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02005388 if (vmx->idt_vectoring_info &
5389 VECTORING_INFO_DELIVER_CODE_MASK) {
5390 has_error_code = true;
5391 error_code =
5392 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5393 }
5394 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005395 case INTR_TYPE_SOFT_EXCEPTION:
5396 kvm_clear_exception_queue(vcpu);
5397 break;
5398 default:
5399 break;
5400 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005401 }
Izik Eidus37817f22008-03-24 23:14:53 +02005402 tss_selector = exit_qualification;
5403
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005404 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5405 type != INTR_TYPE_EXT_INTR &&
5406 type != INTR_TYPE_NMI_INTR))
5407 skip_emulated_instruction(vcpu);
5408
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005409 if (kvm_task_switch(vcpu, tss_selector,
5410 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
5411 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03005412 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5413 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5414 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005415 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03005416 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005417
5418 /* clear all local breakpoint enable flags */
5419 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
5420
5421 /*
5422 * TODO: What about debug traps on tss switch?
5423 * Are we supposed to inject them and update dr6?
5424 */
5425
5426 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02005427}
5428
Avi Kivity851ba692009-08-24 11:10:17 +03005429static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005430{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005431 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005432 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005433 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005434 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08005435
Sheng Yangf9c617f2009-03-25 10:08:52 +08005436 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08005437
Sheng Yang14394422008-04-28 12:24:45 +08005438 gla_validity = (exit_qualification >> 7) & 0x3;
5439 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
5440 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
5441 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
5442 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08005443 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08005444 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
5445 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03005446 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5447 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03005448 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08005449 }
5450
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005451 /*
5452 * EPT violation happened while executing iret from NMI,
5453 * "blocked by NMI" bit has to be set before next VM entry.
5454 * There are errata that may cause this bit to not be set:
5455 * AAK134, BY25.
5456 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005457 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
5458 cpu_has_virtual_nmis() &&
5459 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005460 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5461
Sheng Yang14394422008-04-28 12:24:45 +08005462 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005463 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005464
5465 /* It is a write fault? */
5466 error_code = exit_qualification & (1U << 1);
Yang Zhang25d92082013-08-06 12:00:32 +03005467 /* It is a fetch fault? */
5468 error_code |= (exit_qualification & (1U << 2)) << 2;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005469 /* ept page table is present? */
5470 error_code |= (exit_qualification >> 3) & 0x1;
5471
Yang Zhang25d92082013-08-06 12:00:32 +03005472 vcpu->arch.exit_qualification = exit_qualification;
5473
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005474 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005475}
5476
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005477static u64 ept_rsvd_mask(u64 spte, int level)
5478{
5479 int i;
5480 u64 mask = 0;
5481
5482 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
5483 mask |= (1ULL << i);
5484
5485 if (level > 2)
5486 /* bits 7:3 reserved */
5487 mask |= 0xf8;
5488 else if (level == 2) {
5489 if (spte & (1ULL << 7))
5490 /* 2MB ref, bits 20:12 reserved */
5491 mask |= 0x1ff000;
5492 else
5493 /* bits 6:3 reserved */
5494 mask |= 0x78;
5495 }
5496
5497 return mask;
5498}
5499
5500static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
5501 int level)
5502{
5503 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
5504
5505 /* 010b (write-only) */
5506 WARN_ON((spte & 0x7) == 0x2);
5507
5508 /* 110b (write/execute) */
5509 WARN_ON((spte & 0x7) == 0x6);
5510
5511 /* 100b (execute-only) and value not supported by logical processor */
5512 if (!cpu_has_vmx_ept_execute_only())
5513 WARN_ON((spte & 0x7) == 0x4);
5514
5515 /* not 000b */
5516 if ((spte & 0x7)) {
5517 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
5518
5519 if (rsvd_bits != 0) {
5520 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
5521 __func__, rsvd_bits);
5522 WARN_ON(1);
5523 }
5524
5525 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
5526 u64 ept_mem_type = (spte & 0x38) >> 3;
5527
5528 if (ept_mem_type == 2 || ept_mem_type == 3 ||
5529 ept_mem_type == 7) {
5530 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
5531 __func__, ept_mem_type);
5532 WARN_ON(1);
5533 }
5534 }
5535 }
5536}
5537
Avi Kivity851ba692009-08-24 11:10:17 +03005538static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005539{
5540 u64 sptes[4];
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005541 int nr_sptes, i, ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005542 gpa_t gpa;
5543
5544 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03005545 if (!kvm_io_bus_write(vcpu->kvm, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
5546 skip_emulated_instruction(vcpu);
5547 return 1;
5548 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005549
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005550 ret = handle_mmio_page_fault_common(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08005551 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005552 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
5553 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005554
5555 if (unlikely(ret == RET_MMIO_PF_INVALID))
5556 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
5557
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08005558 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005559 return 1;
5560
5561 /* It is the real ept misconfig */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005562 printk(KERN_ERR "EPT: Misconfiguration.\n");
5563 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
5564
5565 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
5566
5567 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
5568 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
5569
Avi Kivity851ba692009-08-24 11:10:17 +03005570 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5571 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005572
5573 return 0;
5574}
5575
Avi Kivity851ba692009-08-24 11:10:17 +03005576static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005577{
5578 u32 cpu_based_vm_exec_control;
5579
5580 /* clear pending NMI */
5581 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5582 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
5583 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5584 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005585 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005586
5587 return 1;
5588}
5589
Mohammed Gamal80ced182009-09-01 12:48:18 +02005590static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005591{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005592 struct vcpu_vmx *vmx = to_vmx(vcpu);
5593 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005594 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02005595 u32 cpu_exec_ctrl;
5596 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005597 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005598
5599 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5600 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005601
Avi Kivityb8405c12012-06-07 17:08:48 +03005602 while (!guest_state_valid(vcpu) && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03005603 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005604 return handle_interrupt_window(&vmx->vcpu);
5605
Avi Kivityde87dcdd2012-06-12 20:21:38 +03005606 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
5607 return 1;
5608
Gleb Natapov991eebf2013-04-11 12:10:51 +03005609 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005610
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005611 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02005612 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005613 ret = 0;
5614 goto out;
5615 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005616
Avi Kivityde5f70e2012-06-12 20:22:28 +03005617 if (err != EMULATE_DONE) {
5618 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5619 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5620 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005621 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03005622 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005623
Gleb Natapov8d76c492013-05-08 18:38:44 +03005624 if (vcpu->arch.halt_request) {
5625 vcpu->arch.halt_request = 0;
5626 ret = kvm_emulate_halt(vcpu);
5627 goto out;
5628 }
5629
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005630 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02005631 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005632 if (need_resched())
5633 schedule();
5634 }
5635
Gleb Natapov14168782013-01-21 15:36:49 +02005636 vmx->emulation_required = emulation_required(vcpu);
Mohammed Gamal80ced182009-09-01 12:48:18 +02005637out:
5638 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005639}
5640
Avi Kivity6aa8b732006-12-10 02:21:36 -08005641/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005642 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5643 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5644 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03005645static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005646{
5647 skip_emulated_instruction(vcpu);
5648 kvm_vcpu_on_spin(vcpu);
5649
5650 return 1;
5651}
5652
Sheng Yang59708672009-12-15 13:29:54 +08005653static int handle_invalid_op(struct kvm_vcpu *vcpu)
5654{
5655 kvm_queue_exception(vcpu, UD_VECTOR);
5656 return 1;
5657}
5658
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005659/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005660 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
5661 * We could reuse a single VMCS for all the L2 guests, but we also want the
5662 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
5663 * allows keeping them loaded on the processor, and in the future will allow
5664 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
5665 * every entry if they never change.
5666 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
5667 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
5668 *
5669 * The following functions allocate and free a vmcs02 in this pool.
5670 */
5671
5672/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
5673static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
5674{
5675 struct vmcs02_list *item;
5676 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5677 if (item->vmptr == vmx->nested.current_vmptr) {
5678 list_move(&item->list, &vmx->nested.vmcs02_pool);
5679 return &item->vmcs02;
5680 }
5681
5682 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
5683 /* Recycle the least recently used VMCS. */
5684 item = list_entry(vmx->nested.vmcs02_pool.prev,
5685 struct vmcs02_list, list);
5686 item->vmptr = vmx->nested.current_vmptr;
5687 list_move(&item->list, &vmx->nested.vmcs02_pool);
5688 return &item->vmcs02;
5689 }
5690
5691 /* Create a new VMCS */
Ioan Orghici0fa24ce2013-03-10 15:46:00 +02005692 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005693 if (!item)
5694 return NULL;
5695 item->vmcs02.vmcs = alloc_vmcs();
5696 if (!item->vmcs02.vmcs) {
5697 kfree(item);
5698 return NULL;
5699 }
5700 loaded_vmcs_init(&item->vmcs02);
5701 item->vmptr = vmx->nested.current_vmptr;
5702 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
5703 vmx->nested.vmcs02_num++;
5704 return &item->vmcs02;
5705}
5706
5707/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
5708static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
5709{
5710 struct vmcs02_list *item;
5711 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5712 if (item->vmptr == vmptr) {
5713 free_loaded_vmcs(&item->vmcs02);
5714 list_del(&item->list);
5715 kfree(item);
5716 vmx->nested.vmcs02_num--;
5717 return;
5718 }
5719}
5720
5721/*
5722 * Free all VMCSs saved for this vcpu, except the one pointed by
5723 * vmx->loaded_vmcs. These include the VMCSs in vmcs02_pool (except the one
5724 * currently used, if running L2), and vmcs01 when running L2.
5725 */
5726static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
5727{
5728 struct vmcs02_list *item, *n;
5729 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
5730 if (vmx->loaded_vmcs != &item->vmcs02)
5731 free_loaded_vmcs(&item->vmcs02);
5732 list_del(&item->list);
5733 kfree(item);
5734 }
5735 vmx->nested.vmcs02_num = 0;
5736
5737 if (vmx->loaded_vmcs != &vmx->vmcs01)
5738 free_loaded_vmcs(&vmx->vmcs01);
5739}
5740
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08005741/*
5742 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
5743 * set the success or error code of an emulated VMX instruction, as specified
5744 * by Vol 2B, VMX Instruction Reference, "Conventions".
5745 */
5746static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
5747{
5748 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
5749 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5750 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
5751}
5752
5753static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
5754{
5755 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5756 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
5757 X86_EFLAGS_SF | X86_EFLAGS_OF))
5758 | X86_EFLAGS_CF);
5759}
5760
Abel Gordon145c28d2013-04-18 14:36:55 +03005761static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08005762 u32 vm_instruction_error)
5763{
5764 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
5765 /*
5766 * failValid writes the error number to the current VMCS, which
5767 * can't be done there isn't a current VMCS.
5768 */
5769 nested_vmx_failInvalid(vcpu);
5770 return;
5771 }
5772 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5773 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5774 X86_EFLAGS_SF | X86_EFLAGS_OF))
5775 | X86_EFLAGS_ZF);
5776 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
5777 /*
5778 * We don't need to force a shadow sync because
5779 * VM_INSTRUCTION_ERROR is not shadowed
5780 */
5781}
Abel Gordon145c28d2013-04-18 14:36:55 +03005782
Jan Kiszkaf41245002014-03-07 20:03:13 +01005783static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
5784{
5785 struct vcpu_vmx *vmx =
5786 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
5787
5788 vmx->nested.preemption_timer_expired = true;
5789 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
5790 kvm_vcpu_kick(&vmx->vcpu);
5791
5792 return HRTIMER_NORESTART;
5793}
5794
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005795/*
Bandan Das19677e32014-05-06 02:19:15 -04005796 * Decode the memory-address operand of a vmx instruction, as recorded on an
5797 * exit caused by such an instruction (run by a guest hypervisor).
5798 * On success, returns 0. When the operand is invalid, returns 1 and throws
5799 * #UD or #GP.
5800 */
5801static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
5802 unsigned long exit_qualification,
5803 u32 vmx_instruction_info, gva_t *ret)
5804{
5805 /*
5806 * According to Vol. 3B, "Information for VM Exits Due to Instruction
5807 * Execution", on an exit, vmx_instruction_info holds most of the
5808 * addressing components of the operand. Only the displacement part
5809 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
5810 * For how an actual address is calculated from all these components,
5811 * refer to Vol. 1, "Operand Addressing".
5812 */
5813 int scaling = vmx_instruction_info & 3;
5814 int addr_size = (vmx_instruction_info >> 7) & 7;
5815 bool is_reg = vmx_instruction_info & (1u << 10);
5816 int seg_reg = (vmx_instruction_info >> 15) & 7;
5817 int index_reg = (vmx_instruction_info >> 18) & 0xf;
5818 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
5819 int base_reg = (vmx_instruction_info >> 23) & 0xf;
5820 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
5821
5822 if (is_reg) {
5823 kvm_queue_exception(vcpu, UD_VECTOR);
5824 return 1;
5825 }
5826
5827 /* Addr = segment_base + offset */
5828 /* offset = base + [index * scale] + displacement */
5829 *ret = vmx_get_segment_base(vcpu, seg_reg);
5830 if (base_is_valid)
5831 *ret += kvm_register_read(vcpu, base_reg);
5832 if (index_is_valid)
5833 *ret += kvm_register_read(vcpu, index_reg)<<scaling;
5834 *ret += exit_qualification; /* holds the displacement */
5835
5836 if (addr_size == 1) /* 32 bit */
5837 *ret &= 0xffffffff;
5838
5839 /*
5840 * TODO: throw #GP (and return 1) in various cases that the VM*
5841 * instructions require it - e.g., offset beyond segment limit,
5842 * unusable or unreadable/unwritable segment, non-canonical 64-bit
5843 * address, and so on. Currently these are not checked.
5844 */
5845 return 0;
5846}
5847
5848/*
Bandan Das3573e222014-05-06 02:19:16 -04005849 * This function performs the various checks including
5850 * - if it's 4KB aligned
5851 * - No bits beyond the physical address width are set
5852 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04005853 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04005854 */
Bandan Das4291b582014-05-06 02:19:18 -04005855static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
5856 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04005857{
5858 gva_t gva;
5859 gpa_t vmptr;
5860 struct x86_exception e;
5861 struct page *page;
5862 struct vcpu_vmx *vmx = to_vmx(vcpu);
5863 int maxphyaddr = cpuid_maxphyaddr(vcpu);
5864
5865 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
5866 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
5867 return 1;
5868
5869 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
5870 sizeof(vmptr), &e)) {
5871 kvm_inject_page_fault(vcpu, &e);
5872 return 1;
5873 }
5874
5875 switch (exit_reason) {
5876 case EXIT_REASON_VMON:
5877 /*
5878 * SDM 3: 24.11.5
5879 * The first 4 bytes of VMXON region contain the supported
5880 * VMCS revision identifier
5881 *
5882 * Note - IA32_VMX_BASIC[48] will never be 1
5883 * for the nested case;
5884 * which replaces physical address width with 32
5885 *
5886 */
5887 if (!IS_ALIGNED(vmptr, PAGE_SIZE) || (vmptr >> maxphyaddr)) {
5888 nested_vmx_failInvalid(vcpu);
5889 skip_emulated_instruction(vcpu);
5890 return 1;
5891 }
5892
5893 page = nested_get_page(vcpu, vmptr);
5894 if (page == NULL ||
5895 *(u32 *)kmap(page) != VMCS12_REVISION) {
5896 nested_vmx_failInvalid(vcpu);
5897 kunmap(page);
5898 skip_emulated_instruction(vcpu);
5899 return 1;
5900 }
5901 kunmap(page);
5902 vmx->nested.vmxon_ptr = vmptr;
5903 break;
Bandan Das4291b582014-05-06 02:19:18 -04005904 case EXIT_REASON_VMCLEAR:
5905 if (!IS_ALIGNED(vmptr, PAGE_SIZE) || (vmptr >> maxphyaddr)) {
5906 nested_vmx_failValid(vcpu,
5907 VMXERR_VMCLEAR_INVALID_ADDRESS);
5908 skip_emulated_instruction(vcpu);
5909 return 1;
5910 }
Bandan Das3573e222014-05-06 02:19:16 -04005911
Bandan Das4291b582014-05-06 02:19:18 -04005912 if (vmptr == vmx->nested.vmxon_ptr) {
5913 nested_vmx_failValid(vcpu,
5914 VMXERR_VMCLEAR_VMXON_POINTER);
5915 skip_emulated_instruction(vcpu);
5916 return 1;
5917 }
5918 break;
5919 case EXIT_REASON_VMPTRLD:
5920 if (!IS_ALIGNED(vmptr, PAGE_SIZE) || (vmptr >> maxphyaddr)) {
5921 nested_vmx_failValid(vcpu,
5922 VMXERR_VMPTRLD_INVALID_ADDRESS);
5923 skip_emulated_instruction(vcpu);
5924 return 1;
5925 }
5926
5927 if (vmptr == vmx->nested.vmxon_ptr) {
5928 nested_vmx_failValid(vcpu,
5929 VMXERR_VMCLEAR_VMXON_POINTER);
5930 skip_emulated_instruction(vcpu);
5931 return 1;
5932 }
5933 break;
Bandan Das3573e222014-05-06 02:19:16 -04005934 default:
5935 return 1; /* shouldn't happen */
5936 }
5937
Bandan Das4291b582014-05-06 02:19:18 -04005938 if (vmpointer)
5939 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04005940 return 0;
5941}
5942
5943/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005944 * Emulate the VMXON instruction.
5945 * Currently, we just remember that VMX is active, and do not save or even
5946 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
5947 * do not currently need to store anything in that guest-allocated memory
5948 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
5949 * argument is different from the VMXON pointer (which the spec says they do).
5950 */
5951static int handle_vmon(struct kvm_vcpu *vcpu)
5952{
5953 struct kvm_segment cs;
5954 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03005955 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08005956 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
5957 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005958
5959 /* The Intel VMX Instruction Reference lists a bunch of bits that
5960 * are prerequisite to running VMXON, most notably cr4.VMXE must be
5961 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
5962 * Otherwise, we should fail with #UD. We test these now:
5963 */
5964 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
5965 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
5966 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
5967 kvm_queue_exception(vcpu, UD_VECTOR);
5968 return 1;
5969 }
5970
5971 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5972 if (is_long_mode(vcpu) && !cs.l) {
5973 kvm_queue_exception(vcpu, UD_VECTOR);
5974 return 1;
5975 }
5976
5977 if (vmx_get_cpl(vcpu)) {
5978 kvm_inject_gp(vcpu, 0);
5979 return 1;
5980 }
Bandan Das3573e222014-05-06 02:19:16 -04005981
Bandan Das4291b582014-05-06 02:19:18 -04005982 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04005983 return 1;
5984
Abel Gordon145c28d2013-04-18 14:36:55 +03005985 if (vmx->nested.vmxon) {
5986 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
5987 skip_emulated_instruction(vcpu);
5988 return 1;
5989 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08005990
5991 if ((vmx->nested.msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
5992 != VMXON_NEEDED_FEATURES) {
5993 kvm_inject_gp(vcpu, 0);
5994 return 1;
5995 }
5996
Abel Gordon8de48832013-04-18 14:37:25 +03005997 if (enable_shadow_vmcs) {
5998 shadow_vmcs = alloc_vmcs();
5999 if (!shadow_vmcs)
6000 return -ENOMEM;
6001 /* mark vmcs as shadow */
6002 shadow_vmcs->revision_id |= (1u << 31);
6003 /* init shadow vmcs */
6004 vmcs_clear(shadow_vmcs);
6005 vmx->nested.current_shadow_vmcs = shadow_vmcs;
6006 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006007
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006008 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
6009 vmx->nested.vmcs02_num = 0;
6010
Jan Kiszkaf41245002014-03-07 20:03:13 +01006011 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
6012 HRTIMER_MODE_REL);
6013 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
6014
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006015 vmx->nested.vmxon = true;
6016
6017 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08006018 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006019 return 1;
6020}
6021
6022/*
6023 * Intel's VMX Instruction Reference specifies a common set of prerequisites
6024 * for running VMX instructions (except VMXON, whose prerequisites are
6025 * slightly different). It also specifies what exception to inject otherwise.
6026 */
6027static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
6028{
6029 struct kvm_segment cs;
6030 struct vcpu_vmx *vmx = to_vmx(vcpu);
6031
6032 if (!vmx->nested.vmxon) {
6033 kvm_queue_exception(vcpu, UD_VECTOR);
6034 return 0;
6035 }
6036
6037 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
6038 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
6039 (is_long_mode(vcpu) && !cs.l)) {
6040 kvm_queue_exception(vcpu, UD_VECTOR);
6041 return 0;
6042 }
6043
6044 if (vmx_get_cpl(vcpu)) {
6045 kvm_inject_gp(vcpu, 0);
6046 return 0;
6047 }
6048
6049 return 1;
6050}
6051
Abel Gordone7953d72013-04-18 14:37:55 +03006052static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
6053{
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03006054 u32 exec_control;
Abel Gordon012f83c2013-04-18 14:39:25 +03006055 if (enable_shadow_vmcs) {
6056 if (vmx->nested.current_vmcs12 != NULL) {
6057 /* copy to memory all shadowed fields in case
6058 they were modified */
6059 copy_shadow_to_vmcs12(vmx);
6060 vmx->nested.sync_shadow_vmcs = false;
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03006061 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6062 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
6063 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
6064 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03006065 }
6066 }
Abel Gordone7953d72013-04-18 14:37:55 +03006067 kunmap(vmx->nested.current_vmcs12_page);
6068 nested_release_page(vmx->nested.current_vmcs12_page);
6069}
6070
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006071/*
6072 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
6073 * just stops using VMX.
6074 */
6075static void free_nested(struct vcpu_vmx *vmx)
6076{
6077 if (!vmx->nested.vmxon)
6078 return;
6079 vmx->nested.vmxon = false;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006080 if (vmx->nested.current_vmptr != -1ull) {
Abel Gordone7953d72013-04-18 14:37:55 +03006081 nested_release_vmcs12(vmx);
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006082 vmx->nested.current_vmptr = -1ull;
6083 vmx->nested.current_vmcs12 = NULL;
6084 }
Abel Gordone7953d72013-04-18 14:37:55 +03006085 if (enable_shadow_vmcs)
6086 free_vmcs(vmx->nested.current_shadow_vmcs);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006087 /* Unpin physical memory we referred to in current vmcs02 */
6088 if (vmx->nested.apic_access_page) {
6089 nested_release_page(vmx->nested.apic_access_page);
6090 vmx->nested.apic_access_page = 0;
6091 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006092
6093 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006094}
6095
6096/* Emulate the VMXOFF instruction */
6097static int handle_vmoff(struct kvm_vcpu *vcpu)
6098{
6099 if (!nested_vmx_check_permission(vcpu))
6100 return 1;
6101 free_nested(to_vmx(vcpu));
6102 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08006103 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006104 return 1;
6105}
6106
Nadav Har'El27d6c862011-05-25 23:06:59 +03006107/* Emulate the VMCLEAR instruction */
6108static int handle_vmclear(struct kvm_vcpu *vcpu)
6109{
6110 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03006111 gpa_t vmptr;
6112 struct vmcs12 *vmcs12;
6113 struct page *page;
Nadav Har'El27d6c862011-05-25 23:06:59 +03006114
6115 if (!nested_vmx_check_permission(vcpu))
6116 return 1;
6117
Bandan Das4291b582014-05-06 02:19:18 -04006118 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03006119 return 1;
6120
Nadav Har'El27d6c862011-05-25 23:06:59 +03006121 if (vmptr == vmx->nested.current_vmptr) {
Abel Gordone7953d72013-04-18 14:37:55 +03006122 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03006123 vmx->nested.current_vmptr = -1ull;
6124 vmx->nested.current_vmcs12 = NULL;
6125 }
6126
6127 page = nested_get_page(vcpu, vmptr);
6128 if (page == NULL) {
6129 /*
6130 * For accurate processor emulation, VMCLEAR beyond available
6131 * physical memory should do nothing at all. However, it is
6132 * possible that a nested vmx bug, not a guest hypervisor bug,
6133 * resulted in this case, so let's shut down before doing any
6134 * more damage:
6135 */
6136 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6137 return 1;
6138 }
6139 vmcs12 = kmap(page);
6140 vmcs12->launch_state = 0;
6141 kunmap(page);
6142 nested_release_page(page);
6143
6144 nested_free_vmcs02(vmx, vmptr);
6145
6146 skip_emulated_instruction(vcpu);
6147 nested_vmx_succeed(vcpu);
6148 return 1;
6149}
6150
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006151static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
6152
6153/* Emulate the VMLAUNCH instruction */
6154static int handle_vmlaunch(struct kvm_vcpu *vcpu)
6155{
6156 return nested_vmx_run(vcpu, true);
6157}
6158
6159/* Emulate the VMRESUME instruction */
6160static int handle_vmresume(struct kvm_vcpu *vcpu)
6161{
6162
6163 return nested_vmx_run(vcpu, false);
6164}
6165
Nadav Har'El49f705c2011-05-25 23:08:30 +03006166enum vmcs_field_type {
6167 VMCS_FIELD_TYPE_U16 = 0,
6168 VMCS_FIELD_TYPE_U64 = 1,
6169 VMCS_FIELD_TYPE_U32 = 2,
6170 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
6171};
6172
6173static inline int vmcs_field_type(unsigned long field)
6174{
6175 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
6176 return VMCS_FIELD_TYPE_U32;
6177 return (field >> 13) & 0x3 ;
6178}
6179
6180static inline int vmcs_field_readonly(unsigned long field)
6181{
6182 return (((field >> 10) & 0x3) == 1);
6183}
6184
6185/*
6186 * Read a vmcs12 field. Since these can have varying lengths and we return
6187 * one type, we chose the biggest type (u64) and zero-extend the return value
6188 * to that size. Note that the caller, handle_vmread, might need to use only
6189 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
6190 * 64-bit fields are to be returned).
6191 */
6192static inline bool vmcs12_read_any(struct kvm_vcpu *vcpu,
6193 unsigned long field, u64 *ret)
6194{
6195 short offset = vmcs_field_to_offset(field);
6196 char *p;
6197
6198 if (offset < 0)
6199 return 0;
6200
6201 p = ((char *)(get_vmcs12(vcpu))) + offset;
6202
6203 switch (vmcs_field_type(field)) {
6204 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6205 *ret = *((natural_width *)p);
6206 return 1;
6207 case VMCS_FIELD_TYPE_U16:
6208 *ret = *((u16 *)p);
6209 return 1;
6210 case VMCS_FIELD_TYPE_U32:
6211 *ret = *((u32 *)p);
6212 return 1;
6213 case VMCS_FIELD_TYPE_U64:
6214 *ret = *((u64 *)p);
6215 return 1;
6216 default:
6217 return 0; /* can never happen. */
6218 }
6219}
6220
Abel Gordon20b97fe2013-04-18 14:36:25 +03006221
6222static inline bool vmcs12_write_any(struct kvm_vcpu *vcpu,
6223 unsigned long field, u64 field_value){
6224 short offset = vmcs_field_to_offset(field);
6225 char *p = ((char *) get_vmcs12(vcpu)) + offset;
6226 if (offset < 0)
6227 return false;
6228
6229 switch (vmcs_field_type(field)) {
6230 case VMCS_FIELD_TYPE_U16:
6231 *(u16 *)p = field_value;
6232 return true;
6233 case VMCS_FIELD_TYPE_U32:
6234 *(u32 *)p = field_value;
6235 return true;
6236 case VMCS_FIELD_TYPE_U64:
6237 *(u64 *)p = field_value;
6238 return true;
6239 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6240 *(natural_width *)p = field_value;
6241 return true;
6242 default:
6243 return false; /* can never happen. */
6244 }
6245
6246}
6247
Abel Gordon16f5b902013-04-18 14:38:25 +03006248static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
6249{
6250 int i;
6251 unsigned long field;
6252 u64 field_value;
6253 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02006254 const unsigned long *fields = shadow_read_write_fields;
6255 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03006256
6257 vmcs_load(shadow_vmcs);
6258
6259 for (i = 0; i < num_fields; i++) {
6260 field = fields[i];
6261 switch (vmcs_field_type(field)) {
6262 case VMCS_FIELD_TYPE_U16:
6263 field_value = vmcs_read16(field);
6264 break;
6265 case VMCS_FIELD_TYPE_U32:
6266 field_value = vmcs_read32(field);
6267 break;
6268 case VMCS_FIELD_TYPE_U64:
6269 field_value = vmcs_read64(field);
6270 break;
6271 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6272 field_value = vmcs_readl(field);
6273 break;
6274 }
6275 vmcs12_write_any(&vmx->vcpu, field, field_value);
6276 }
6277
6278 vmcs_clear(shadow_vmcs);
6279 vmcs_load(vmx->loaded_vmcs->vmcs);
6280}
6281
Abel Gordonc3114422013-04-18 14:38:55 +03006282static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
6283{
Mathias Krausec2bae892013-06-26 20:36:21 +02006284 const unsigned long *fields[] = {
6285 shadow_read_write_fields,
6286 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03006287 };
Mathias Krausec2bae892013-06-26 20:36:21 +02006288 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03006289 max_shadow_read_write_fields,
6290 max_shadow_read_only_fields
6291 };
6292 int i, q;
6293 unsigned long field;
6294 u64 field_value = 0;
6295 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
6296
6297 vmcs_load(shadow_vmcs);
6298
Mathias Krausec2bae892013-06-26 20:36:21 +02006299 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03006300 for (i = 0; i < max_fields[q]; i++) {
6301 field = fields[q][i];
6302 vmcs12_read_any(&vmx->vcpu, field, &field_value);
6303
6304 switch (vmcs_field_type(field)) {
6305 case VMCS_FIELD_TYPE_U16:
6306 vmcs_write16(field, (u16)field_value);
6307 break;
6308 case VMCS_FIELD_TYPE_U32:
6309 vmcs_write32(field, (u32)field_value);
6310 break;
6311 case VMCS_FIELD_TYPE_U64:
6312 vmcs_write64(field, (u64)field_value);
6313 break;
6314 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6315 vmcs_writel(field, (long)field_value);
6316 break;
6317 }
6318 }
6319 }
6320
6321 vmcs_clear(shadow_vmcs);
6322 vmcs_load(vmx->loaded_vmcs->vmcs);
6323}
6324
Nadav Har'El49f705c2011-05-25 23:08:30 +03006325/*
6326 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
6327 * used before) all generate the same failure when it is missing.
6328 */
6329static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
6330{
6331 struct vcpu_vmx *vmx = to_vmx(vcpu);
6332 if (vmx->nested.current_vmptr == -1ull) {
6333 nested_vmx_failInvalid(vcpu);
6334 skip_emulated_instruction(vcpu);
6335 return 0;
6336 }
6337 return 1;
6338}
6339
6340static int handle_vmread(struct kvm_vcpu *vcpu)
6341{
6342 unsigned long field;
6343 u64 field_value;
6344 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6345 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6346 gva_t gva = 0;
6347
6348 if (!nested_vmx_check_permission(vcpu) ||
6349 !nested_vmx_check_vmcs12(vcpu))
6350 return 1;
6351
6352 /* Decode instruction info and find the field to read */
6353 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
6354 /* Read the field, zero-extended to a u64 field_value */
6355 if (!vmcs12_read_any(vcpu, field, &field_value)) {
6356 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
6357 skip_emulated_instruction(vcpu);
6358 return 1;
6359 }
6360 /*
6361 * Now copy part of this value to register or memory, as requested.
6362 * Note that the number of bits actually copied is 32 or 64 depending
6363 * on the guest's mode (32 or 64 bit), not on the given field's length.
6364 */
6365 if (vmx_instruction_info & (1u << 10)) {
6366 kvm_register_write(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
6367 field_value);
6368 } else {
6369 if (get_vmx_mem_address(vcpu, exit_qualification,
6370 vmx_instruction_info, &gva))
6371 return 1;
6372 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
6373 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
6374 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
6375 }
6376
6377 nested_vmx_succeed(vcpu);
6378 skip_emulated_instruction(vcpu);
6379 return 1;
6380}
6381
6382
6383static int handle_vmwrite(struct kvm_vcpu *vcpu)
6384{
6385 unsigned long field;
6386 gva_t gva;
6387 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6388 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03006389 /* The value to write might be 32 or 64 bits, depending on L1's long
6390 * mode, and eventually we need to write that into a field of several
6391 * possible lengths. The code below first zero-extends the value to 64
6392 * bit (field_value), and then copies only the approriate number of
6393 * bits into the vmcs12 field.
6394 */
6395 u64 field_value = 0;
6396 struct x86_exception e;
6397
6398 if (!nested_vmx_check_permission(vcpu) ||
6399 !nested_vmx_check_vmcs12(vcpu))
6400 return 1;
6401
6402 if (vmx_instruction_info & (1u << 10))
6403 field_value = kvm_register_read(vcpu,
6404 (((vmx_instruction_info) >> 3) & 0xf));
6405 else {
6406 if (get_vmx_mem_address(vcpu, exit_qualification,
6407 vmx_instruction_info, &gva))
6408 return 1;
6409 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
6410 &field_value, (is_long_mode(vcpu) ? 8 : 4), &e)) {
6411 kvm_inject_page_fault(vcpu, &e);
6412 return 1;
6413 }
6414 }
6415
6416
6417 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
6418 if (vmcs_field_readonly(field)) {
6419 nested_vmx_failValid(vcpu,
6420 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
6421 skip_emulated_instruction(vcpu);
6422 return 1;
6423 }
6424
Abel Gordon20b97fe2013-04-18 14:36:25 +03006425 if (!vmcs12_write_any(vcpu, field, field_value)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03006426 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
6427 skip_emulated_instruction(vcpu);
6428 return 1;
6429 }
6430
6431 nested_vmx_succeed(vcpu);
6432 skip_emulated_instruction(vcpu);
6433 return 1;
6434}
6435
Nadav Har'El63846662011-05-25 23:07:29 +03006436/* Emulate the VMPTRLD instruction */
6437static int handle_vmptrld(struct kvm_vcpu *vcpu)
6438{
6439 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03006440 gpa_t vmptr;
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03006441 u32 exec_control;
Nadav Har'El63846662011-05-25 23:07:29 +03006442
6443 if (!nested_vmx_check_permission(vcpu))
6444 return 1;
6445
Bandan Das4291b582014-05-06 02:19:18 -04006446 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03006447 return 1;
6448
Nadav Har'El63846662011-05-25 23:07:29 +03006449 if (vmx->nested.current_vmptr != vmptr) {
6450 struct vmcs12 *new_vmcs12;
6451 struct page *page;
6452 page = nested_get_page(vcpu, vmptr);
6453 if (page == NULL) {
6454 nested_vmx_failInvalid(vcpu);
6455 skip_emulated_instruction(vcpu);
6456 return 1;
6457 }
6458 new_vmcs12 = kmap(page);
6459 if (new_vmcs12->revision_id != VMCS12_REVISION) {
6460 kunmap(page);
6461 nested_release_page_clean(page);
6462 nested_vmx_failValid(vcpu,
6463 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
6464 skip_emulated_instruction(vcpu);
6465 return 1;
6466 }
Abel Gordone7953d72013-04-18 14:37:55 +03006467 if (vmx->nested.current_vmptr != -1ull)
6468 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03006469
6470 vmx->nested.current_vmptr = vmptr;
6471 vmx->nested.current_vmcs12 = new_vmcs12;
6472 vmx->nested.current_vmcs12_page = page;
Abel Gordon012f83c2013-04-18 14:39:25 +03006473 if (enable_shadow_vmcs) {
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03006474 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6475 exec_control |= SECONDARY_EXEC_SHADOW_VMCS;
6476 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
6477 vmcs_write64(VMCS_LINK_POINTER,
6478 __pa(vmx->nested.current_shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03006479 vmx->nested.sync_shadow_vmcs = true;
6480 }
Nadav Har'El63846662011-05-25 23:07:29 +03006481 }
6482
6483 nested_vmx_succeed(vcpu);
6484 skip_emulated_instruction(vcpu);
6485 return 1;
6486}
6487
Nadav Har'El6a4d7552011-05-25 23:08:00 +03006488/* Emulate the VMPTRST instruction */
6489static int handle_vmptrst(struct kvm_vcpu *vcpu)
6490{
6491 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6492 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6493 gva_t vmcs_gva;
6494 struct x86_exception e;
6495
6496 if (!nested_vmx_check_permission(vcpu))
6497 return 1;
6498
6499 if (get_vmx_mem_address(vcpu, exit_qualification,
6500 vmx_instruction_info, &vmcs_gva))
6501 return 1;
6502 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
6503 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
6504 (void *)&to_vmx(vcpu)->nested.current_vmptr,
6505 sizeof(u64), &e)) {
6506 kvm_inject_page_fault(vcpu, &e);
6507 return 1;
6508 }
6509 nested_vmx_succeed(vcpu);
6510 skip_emulated_instruction(vcpu);
6511 return 1;
6512}
6513
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006514/* Emulate the INVEPT instruction */
6515static int handle_invept(struct kvm_vcpu *vcpu)
6516{
6517 u32 vmx_instruction_info, types;
6518 unsigned long type;
6519 gva_t gva;
6520 struct x86_exception e;
6521 struct {
6522 u64 eptp, gpa;
6523 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006524
6525 if (!(nested_vmx_secondary_ctls_high & SECONDARY_EXEC_ENABLE_EPT) ||
6526 !(nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
6527 kvm_queue_exception(vcpu, UD_VECTOR);
6528 return 1;
6529 }
6530
6531 if (!nested_vmx_check_permission(vcpu))
6532 return 1;
6533
6534 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
6535 kvm_queue_exception(vcpu, UD_VECTOR);
6536 return 1;
6537 }
6538
6539 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6540 type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf);
6541
6542 types = (nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
6543
6544 if (!(types & (1UL << type))) {
6545 nested_vmx_failValid(vcpu,
6546 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
6547 return 1;
6548 }
6549
6550 /* According to the Intel VMX instruction reference, the memory
6551 * operand is read even if it isn't needed (e.g., for type==global)
6552 */
6553 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
6554 vmx_instruction_info, &gva))
6555 return 1;
6556 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
6557 sizeof(operand), &e)) {
6558 kvm_inject_page_fault(vcpu, &e);
6559 return 1;
6560 }
6561
6562 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006563 case VMX_EPT_EXTENT_GLOBAL:
6564 kvm_mmu_sync_roots(vcpu);
6565 kvm_mmu_flush_tlb(vcpu);
6566 nested_vmx_succeed(vcpu);
6567 break;
6568 default:
Bandan Das4b855072014-04-19 18:17:44 -04006569 /* Trap single context invalidation invept calls */
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006570 BUG_ON(1);
6571 break;
6572 }
6573
6574 skip_emulated_instruction(vcpu);
6575 return 1;
6576}
6577
Nadav Har'El0140cae2011-05-25 23:06:28 +03006578/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006579 * The exit handlers return 1 if the exit was handled fully and guest execution
6580 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
6581 * to be done to userspace and return 0.
6582 */
Mathias Krause772e0312012-08-30 01:30:19 +02006583static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006584 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
6585 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08006586 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08006587 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006588 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006589 [EXIT_REASON_CR_ACCESS] = handle_cr,
6590 [EXIT_REASON_DR_ACCESS] = handle_dr,
6591 [EXIT_REASON_CPUID] = handle_cpuid,
6592 [EXIT_REASON_MSR_READ] = handle_rdmsr,
6593 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
6594 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
6595 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006596 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03006597 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02006598 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02006599 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03006600 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006601 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03006602 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03006603 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03006604 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006605 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03006606 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006607 [EXIT_REASON_VMOFF] = handle_vmoff,
6608 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08006609 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
6610 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08006611 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08006612 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02006613 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08006614 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02006615 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08006616 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006617 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
6618 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006619 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sheng Yang59708672009-12-15 13:29:54 +08006620 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
6621 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006622 [EXIT_REASON_INVEPT] = handle_invept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006623};
6624
6625static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04006626 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006627
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006628static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
6629 struct vmcs12 *vmcs12)
6630{
6631 unsigned long exit_qualification;
6632 gpa_t bitmap, last_bitmap;
6633 unsigned int port;
6634 int size;
6635 u8 b;
6636
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006637 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05006638 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006639
6640 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6641
6642 port = exit_qualification >> 16;
6643 size = (exit_qualification & 7) + 1;
6644
6645 last_bitmap = (gpa_t)-1;
6646 b = -1;
6647
6648 while (size > 0) {
6649 if (port < 0x8000)
6650 bitmap = vmcs12->io_bitmap_a;
6651 else if (port < 0x10000)
6652 bitmap = vmcs12->io_bitmap_b;
6653 else
6654 return 1;
6655 bitmap += (port & 0x7fff) / 8;
6656
6657 if (last_bitmap != bitmap)
6658 if (kvm_read_guest(vcpu->kvm, bitmap, &b, 1))
6659 return 1;
6660 if (b & (1 << (port & 7)))
6661 return 1;
6662
6663 port++;
6664 size--;
6665 last_bitmap = bitmap;
6666 }
6667
6668 return 0;
6669}
6670
Nadav Har'El644d7112011-05-25 23:12:35 +03006671/*
6672 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
6673 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
6674 * disinterest in the current event (read or write a specific MSR) by using an
6675 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
6676 */
6677static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
6678 struct vmcs12 *vmcs12, u32 exit_reason)
6679{
6680 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
6681 gpa_t bitmap;
6682
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01006683 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Nadav Har'El644d7112011-05-25 23:12:35 +03006684 return 1;
6685
6686 /*
6687 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
6688 * for the four combinations of read/write and low/high MSR numbers.
6689 * First we need to figure out which of the four to use:
6690 */
6691 bitmap = vmcs12->msr_bitmap;
6692 if (exit_reason == EXIT_REASON_MSR_WRITE)
6693 bitmap += 2048;
6694 if (msr_index >= 0xc0000000) {
6695 msr_index -= 0xc0000000;
6696 bitmap += 1024;
6697 }
6698
6699 /* Then read the msr_index'th bit from this bitmap: */
6700 if (msr_index < 1024*8) {
6701 unsigned char b;
Jan Kiszkabd31a7f2013-02-14 19:46:27 +01006702 if (kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1))
6703 return 1;
Nadav Har'El644d7112011-05-25 23:12:35 +03006704 return 1 & (b >> (msr_index & 7));
6705 } else
6706 return 1; /* let L1 handle the wrong parameter */
6707}
6708
6709/*
6710 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
6711 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
6712 * intercept (via guest_host_mask etc.) the current event.
6713 */
6714static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
6715 struct vmcs12 *vmcs12)
6716{
6717 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6718 int cr = exit_qualification & 15;
6719 int reg = (exit_qualification >> 8) & 15;
6720 unsigned long val = kvm_register_read(vcpu, reg);
6721
6722 switch ((exit_qualification >> 4) & 3) {
6723 case 0: /* mov to cr */
6724 switch (cr) {
6725 case 0:
6726 if (vmcs12->cr0_guest_host_mask &
6727 (val ^ vmcs12->cr0_read_shadow))
6728 return 1;
6729 break;
6730 case 3:
6731 if ((vmcs12->cr3_target_count >= 1 &&
6732 vmcs12->cr3_target_value0 == val) ||
6733 (vmcs12->cr3_target_count >= 2 &&
6734 vmcs12->cr3_target_value1 == val) ||
6735 (vmcs12->cr3_target_count >= 3 &&
6736 vmcs12->cr3_target_value2 == val) ||
6737 (vmcs12->cr3_target_count >= 4 &&
6738 vmcs12->cr3_target_value3 == val))
6739 return 0;
6740 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
6741 return 1;
6742 break;
6743 case 4:
6744 if (vmcs12->cr4_guest_host_mask &
6745 (vmcs12->cr4_read_shadow ^ val))
6746 return 1;
6747 break;
6748 case 8:
6749 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
6750 return 1;
6751 break;
6752 }
6753 break;
6754 case 2: /* clts */
6755 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
6756 (vmcs12->cr0_read_shadow & X86_CR0_TS))
6757 return 1;
6758 break;
6759 case 1: /* mov from cr */
6760 switch (cr) {
6761 case 3:
6762 if (vmcs12->cpu_based_vm_exec_control &
6763 CPU_BASED_CR3_STORE_EXITING)
6764 return 1;
6765 break;
6766 case 8:
6767 if (vmcs12->cpu_based_vm_exec_control &
6768 CPU_BASED_CR8_STORE_EXITING)
6769 return 1;
6770 break;
6771 }
6772 break;
6773 case 3: /* lmsw */
6774 /*
6775 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
6776 * cr0. Other attempted changes are ignored, with no exit.
6777 */
6778 if (vmcs12->cr0_guest_host_mask & 0xe &
6779 (val ^ vmcs12->cr0_read_shadow))
6780 return 1;
6781 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
6782 !(vmcs12->cr0_read_shadow & 0x1) &&
6783 (val & 0x1))
6784 return 1;
6785 break;
6786 }
6787 return 0;
6788}
6789
6790/*
6791 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
6792 * should handle it ourselves in L0 (and then continue L2). Only call this
6793 * when in is_guest_mode (L2).
6794 */
6795static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
6796{
Nadav Har'El644d7112011-05-25 23:12:35 +03006797 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
6798 struct vcpu_vmx *vmx = to_vmx(vcpu);
6799 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01006800 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03006801
Jan Kiszka542060e2014-01-04 18:47:21 +01006802 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
6803 vmcs_readl(EXIT_QUALIFICATION),
6804 vmx->idt_vectoring_info,
6805 intr_info,
6806 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
6807 KVM_ISA_VMX);
6808
Nadav Har'El644d7112011-05-25 23:12:35 +03006809 if (vmx->nested.nested_run_pending)
6810 return 0;
6811
6812 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02006813 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
6814 vmcs_read32(VM_INSTRUCTION_ERROR));
Nadav Har'El644d7112011-05-25 23:12:35 +03006815 return 1;
6816 }
6817
6818 switch (exit_reason) {
6819 case EXIT_REASON_EXCEPTION_NMI:
6820 if (!is_exception(intr_info))
6821 return 0;
6822 else if (is_page_fault(intr_info))
6823 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01006824 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01006825 !(vmcs12->guest_cr0 & X86_CR0_TS))
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01006826 return 0;
Nadav Har'El644d7112011-05-25 23:12:35 +03006827 return vmcs12->exception_bitmap &
6828 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
6829 case EXIT_REASON_EXTERNAL_INTERRUPT:
6830 return 0;
6831 case EXIT_REASON_TRIPLE_FAULT:
6832 return 1;
6833 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02006834 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03006835 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02006836 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03006837 case EXIT_REASON_TASK_SWITCH:
6838 return 1;
6839 case EXIT_REASON_CPUID:
6840 return 1;
6841 case EXIT_REASON_HLT:
6842 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
6843 case EXIT_REASON_INVD:
6844 return 1;
6845 case EXIT_REASON_INVLPG:
6846 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
6847 case EXIT_REASON_RDPMC:
6848 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
6849 case EXIT_REASON_RDTSC:
6850 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
6851 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
6852 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
6853 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
6854 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
6855 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006856 case EXIT_REASON_INVEPT:
Nadav Har'El644d7112011-05-25 23:12:35 +03006857 /*
6858 * VMX instructions trap unconditionally. This allows L1 to
6859 * emulate them for its L2 guest, i.e., allows 3-level nesting!
6860 */
6861 return 1;
6862 case EXIT_REASON_CR_ACCESS:
6863 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
6864 case EXIT_REASON_DR_ACCESS:
6865 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
6866 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006867 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03006868 case EXIT_REASON_MSR_READ:
6869 case EXIT_REASON_MSR_WRITE:
6870 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
6871 case EXIT_REASON_INVALID_STATE:
6872 return 1;
6873 case EXIT_REASON_MWAIT_INSTRUCTION:
6874 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
6875 case EXIT_REASON_MONITOR_INSTRUCTION:
6876 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
6877 case EXIT_REASON_PAUSE_INSTRUCTION:
6878 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
6879 nested_cpu_has2(vmcs12,
6880 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
6881 case EXIT_REASON_MCE_DURING_VMENTRY:
6882 return 0;
6883 case EXIT_REASON_TPR_BELOW_THRESHOLD:
6884 return 1;
6885 case EXIT_REASON_APIC_ACCESS:
6886 return nested_cpu_has2(vmcs12,
6887 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
6888 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03006889 /*
6890 * L0 always deals with the EPT violation. If nested EPT is
6891 * used, and the nested mmu code discovers that the address is
6892 * missing in the guest EPT table (EPT12), the EPT violation
6893 * will be injected with nested_ept_inject_page_fault()
6894 */
6895 return 0;
Nadav Har'El644d7112011-05-25 23:12:35 +03006896 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03006897 /*
6898 * L2 never uses directly L1's EPT, but rather L0's own EPT
6899 * table (shadow on EPT) or a merged EPT table that L0 built
6900 * (EPT on EPT). So any problems with the structure of the
6901 * table is L0's fault.
6902 */
Nadav Har'El644d7112011-05-25 23:12:35 +03006903 return 0;
6904 case EXIT_REASON_WBINVD:
6905 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
6906 case EXIT_REASON_XSETBV:
6907 return 1;
6908 default:
6909 return 1;
6910 }
6911}
6912
Avi Kivity586f9602010-11-18 13:09:54 +02006913static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
6914{
6915 *info1 = vmcs_readl(EXIT_QUALIFICATION);
6916 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
6917}
6918
Avi Kivity6aa8b732006-12-10 02:21:36 -08006919/*
6920 * The guest has exited. See if we can fix it or if we need userspace
6921 * assistance.
6922 */
Avi Kivity851ba692009-08-24 11:10:17 +03006923static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006924{
Avi Kivity29bd8a72007-09-10 17:27:03 +03006925 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006926 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02006927 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03006928
Mohammed Gamal80ced182009-09-01 12:48:18 +02006929 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02006930 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02006931 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006932
Nadav Har'El644d7112011-05-25 23:12:35 +03006933 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01006934 nested_vmx_vmexit(vcpu, exit_reason,
6935 vmcs_read32(VM_EXIT_INTR_INFO),
6936 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03006937 return 1;
6938 }
6939
Mohammed Gamal51207022010-05-31 22:40:54 +03006940 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
6941 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6942 vcpu->run->fail_entry.hardware_entry_failure_reason
6943 = exit_reason;
6944 return 0;
6945 }
6946
Avi Kivity29bd8a72007-09-10 17:27:03 +03006947 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006948 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6949 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03006950 = vmcs_read32(VM_INSTRUCTION_ERROR);
6951 return 0;
6952 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006953
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006954 /*
6955 * Note:
6956 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
6957 * delivery event since it indicates guest is accessing MMIO.
6958 * The vm-exit can be triggered again after return to guest that
6959 * will cause infinite loop.
6960 */
Mike Dayd77c26f2007-10-08 09:02:08 -04006961 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08006962 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02006963 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006964 exit_reason != EXIT_REASON_TASK_SWITCH)) {
6965 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6966 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
6967 vcpu->run->internal.ndata = 2;
6968 vcpu->run->internal.data[0] = vectoring_info;
6969 vcpu->run->internal.data[1] = exit_reason;
6970 return 0;
6971 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006972
Nadav Har'El644d7112011-05-25 23:12:35 +03006973 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
6974 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03006975 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03006976 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006977 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006978 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01006979 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006980 /*
6981 * This CPU don't support us in finding the end of an
6982 * NMI-blocked window if the guest runs with IRQs
6983 * disabled. So we pull the trigger after 1 s of
6984 * futile waiting, but inform the user about this.
6985 */
6986 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
6987 "state on VCPU %d after 1 s timeout\n",
6988 __func__, vcpu->vcpu_id);
6989 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006990 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006991 }
6992
Avi Kivity6aa8b732006-12-10 02:21:36 -08006993 if (exit_reason < kvm_vmx_max_exit_handlers
6994 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03006995 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006996 else {
Avi Kivity851ba692009-08-24 11:10:17 +03006997 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6998 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006999 }
7000 return 0;
7001}
7002
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007003static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007004{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007005 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007006 vmcs_write32(TPR_THRESHOLD, 0);
7007 return;
7008 }
7009
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007010 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007011}
7012
Yang Zhang8d146952013-01-25 10:18:50 +08007013static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
7014{
7015 u32 sec_exec_control;
7016
7017 /*
7018 * There is not point to enable virtualize x2apic without enable
7019 * apicv
7020 */
Yang Zhangc7c9c562013-01-25 10:18:51 +08007021 if (!cpu_has_vmx_virtualize_x2apic_mode() ||
7022 !vmx_vm_has_apicv(vcpu->kvm))
Yang Zhang8d146952013-01-25 10:18:50 +08007023 return;
7024
7025 if (!vm_need_tpr_shadow(vcpu->kvm))
7026 return;
7027
7028 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7029
7030 if (set) {
7031 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7032 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
7033 } else {
7034 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
7035 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7036 }
7037 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
7038
7039 vmx_set_msr_bitmap(vcpu);
7040}
7041
Yang Zhangc7c9c562013-01-25 10:18:51 +08007042static void vmx_hwapic_isr_update(struct kvm *kvm, int isr)
7043{
7044 u16 status;
7045 u8 old;
7046
7047 if (!vmx_vm_has_apicv(kvm))
7048 return;
7049
7050 if (isr == -1)
7051 isr = 0;
7052
7053 status = vmcs_read16(GUEST_INTR_STATUS);
7054 old = status >> 8;
7055 if (isr != old) {
7056 status &= 0xff;
7057 status |= isr << 8;
7058 vmcs_write16(GUEST_INTR_STATUS, status);
7059 }
7060}
7061
7062static void vmx_set_rvi(int vector)
7063{
7064 u16 status;
7065 u8 old;
7066
7067 status = vmcs_read16(GUEST_INTR_STATUS);
7068 old = (u8)status & 0xff;
7069 if ((u8)vector != old) {
7070 status &= ~0xff;
7071 status |= (u8)vector;
7072 vmcs_write16(GUEST_INTR_STATUS, status);
7073 }
7074}
7075
7076static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
7077{
7078 if (max_irr == -1)
7079 return;
7080
7081 vmx_set_rvi(max_irr);
7082}
7083
7084static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
7085{
Yang Zhang3d81bc72013-04-11 19:25:13 +08007086 if (!vmx_vm_has_apicv(vcpu->kvm))
7087 return;
7088
Yang Zhangc7c9c562013-01-25 10:18:51 +08007089 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
7090 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
7091 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
7092 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
7093}
7094
Avi Kivity51aa01d2010-07-20 14:31:20 +03007095static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03007096{
Avi Kivity00eba012011-03-07 17:24:54 +02007097 u32 exit_intr_info;
7098
7099 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
7100 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
7101 return;
7102
Avi Kivityc5ca8e52011-03-07 17:37:37 +02007103 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02007104 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08007105
7106 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02007107 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08007108 kvm_machine_check();
7109
Gleb Natapov20f65982009-05-11 13:35:55 +03007110 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02007111 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007112 (exit_intr_info & INTR_INFO_VALID_MASK)) {
7113 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03007114 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007115 kvm_after_handle_nmi(&vmx->vcpu);
7116 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03007117}
Gleb Natapov20f65982009-05-11 13:35:55 +03007118
Yang Zhanga547c6d2013-04-11 19:25:10 +08007119static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
7120{
7121 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7122
7123 /*
7124 * If external interrupt exists, IF bit is set in rflags/eflags on the
7125 * interrupt stack frame, and interrupt will be enabled on a return
7126 * from interrupt handler.
7127 */
7128 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
7129 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
7130 unsigned int vector;
7131 unsigned long entry;
7132 gate_desc *desc;
7133 struct vcpu_vmx *vmx = to_vmx(vcpu);
7134#ifdef CONFIG_X86_64
7135 unsigned long tmp;
7136#endif
7137
7138 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
7139 desc = (gate_desc *)vmx->host_idt_base + vector;
7140 entry = gate_offset(*desc);
7141 asm volatile(
7142#ifdef CONFIG_X86_64
7143 "mov %%" _ASM_SP ", %[sp]\n\t"
7144 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
7145 "push $%c[ss]\n\t"
7146 "push %[sp]\n\t"
7147#endif
7148 "pushf\n\t"
7149 "orl $0x200, (%%" _ASM_SP ")\n\t"
7150 __ASM_SIZE(push) " $%c[cs]\n\t"
7151 "call *%[entry]\n\t"
7152 :
7153#ifdef CONFIG_X86_64
7154 [sp]"=&r"(tmp)
7155#endif
7156 :
7157 [entry]"r"(entry),
7158 [ss]"i"(__KERNEL_DS),
7159 [cs]"i"(__KERNEL_CS)
7160 );
7161 } else
7162 local_irq_enable();
7163}
7164
Liu, Jinsongda8999d2014-02-24 10:55:46 +00007165static bool vmx_mpx_supported(void)
7166{
7167 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
7168 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
7169}
7170
Avi Kivity51aa01d2010-07-20 14:31:20 +03007171static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
7172{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02007173 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03007174 bool unblock_nmi;
7175 u8 vector;
7176 bool idtv_info_valid;
7177
7178 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03007179
Avi Kivitycf393f72008-07-01 16:20:21 +03007180 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02007181 if (vmx->nmi_known_unmasked)
7182 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02007183 /*
7184 * Can't use vmx->exit_intr_info since we're not sure what
7185 * the exit reason is.
7186 */
7187 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03007188 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
7189 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
7190 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007191 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03007192 * Re-set bit "block by NMI" before VM entry if vmexit caused by
7193 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007194 * SDM 3: 23.2.2 (September 2008)
7195 * Bit 12 is undefined in any of the following cases:
7196 * If the VM exit sets the valid bit in the IDT-vectoring
7197 * information field.
7198 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03007199 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007200 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
7201 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03007202 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7203 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02007204 else
7205 vmx->nmi_known_unmasked =
7206 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
7207 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007208 } else if (unlikely(vmx->soft_vnmi_blocked))
7209 vmx->vnmi_blocked_time +=
7210 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03007211}
7212
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007213static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03007214 u32 idt_vectoring_info,
7215 int instr_len_field,
7216 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03007217{
Avi Kivity51aa01d2010-07-20 14:31:20 +03007218 u8 vector;
7219 int type;
7220 bool idtv_info_valid;
7221
7222 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03007223
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007224 vcpu->arch.nmi_injected = false;
7225 kvm_clear_exception_queue(vcpu);
7226 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007227
7228 if (!idtv_info_valid)
7229 return;
7230
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007231 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03007232
Avi Kivity668f6122008-07-02 09:28:55 +03007233 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
7234 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03007235
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007236 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03007237 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007238 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03007239 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007240 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03007241 * Clear bit "block by NMI" before VM entry if a NMI
7242 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03007243 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007244 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007245 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03007246 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007247 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007248 /* fall through */
7249 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03007250 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03007251 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03007252 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03007253 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03007254 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007255 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007256 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007257 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007258 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03007259 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007260 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007261 break;
7262 default:
7263 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03007264 }
Avi Kivitycf393f72008-07-01 16:20:21 +03007265}
7266
Avi Kivity83422e12010-07-20 14:43:23 +03007267static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
7268{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007269 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03007270 VM_EXIT_INSTRUCTION_LEN,
7271 IDT_VECTORING_ERROR_CODE);
7272}
7273
Avi Kivityb463a6f2010-07-20 15:06:17 +03007274static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
7275{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007276 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007277 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
7278 VM_ENTRY_INSTRUCTION_LEN,
7279 VM_ENTRY_EXCEPTION_ERROR_CODE);
7280
7281 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
7282}
7283
Gleb Natapovd7cd9792011-10-05 14:01:23 +02007284static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
7285{
7286 int i, nr_msrs;
7287 struct perf_guest_switch_msr *msrs;
7288
7289 msrs = perf_guest_get_msrs(&nr_msrs);
7290
7291 if (!msrs)
7292 return;
7293
7294 for (i = 0; i < nr_msrs; i++)
7295 if (msrs[i].host == msrs[i].guest)
7296 clear_atomic_switch_msr(vmx, msrs[i].msr);
7297 else
7298 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
7299 msrs[i].host);
7300}
7301
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08007302static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007303{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04007304 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03007305 unsigned long debugctlmsr;
Avi Kivity104f2262010-11-18 13:12:52 +02007306
7307 /* Record the guest's net vcpu time for enforced NMI injections. */
7308 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
7309 vmx->entry_time = ktime_get();
7310
7311 /* Don't enter VMX if guest state is invalid, let the exit handler
7312 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02007313 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02007314 return;
7315
Abel Gordon012f83c2013-04-18 14:39:25 +03007316 if (vmx->nested.sync_shadow_vmcs) {
7317 copy_vmcs12_to_shadow(vmx);
7318 vmx->nested.sync_shadow_vmcs = false;
7319 }
7320
Avi Kivity104f2262010-11-18 13:12:52 +02007321 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
7322 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
7323 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
7324 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
7325
7326 /* When single-stepping over STI and MOV SS, we must clear the
7327 * corresponding interruptibility bits in the guest state. Otherwise
7328 * vmentry fails as it then expects bit 14 (BS) in pending debug
7329 * exceptions being set, but that's not correct for the guest debugging
7330 * case. */
7331 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
7332 vmx_set_interrupt_shadow(vcpu, 0);
7333
Gleb Natapovd7cd9792011-10-05 14:01:23 +02007334 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03007335 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02007336
Nadav Har'Eld462b812011-05-24 15:26:10 +03007337 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02007338 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08007339 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007340 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
7341 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
7342 "push %%" _ASM_CX " \n\t"
7343 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03007344 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03007345 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03007346 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03007347 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03007348 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007349 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
7350 "mov %%cr2, %%" _ASM_DX " \n\t"
7351 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03007352 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03007353 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03007354 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007355 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02007356 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007357 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007358 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
7359 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
7360 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
7361 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
7362 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
7363 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08007364#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02007365 "mov %c[r8](%0), %%r8 \n\t"
7366 "mov %c[r9](%0), %%r9 \n\t"
7367 "mov %c[r10](%0), %%r10 \n\t"
7368 "mov %c[r11](%0), %%r11 \n\t"
7369 "mov %c[r12](%0), %%r12 \n\t"
7370 "mov %c[r13](%0), %%r13 \n\t"
7371 "mov %c[r14](%0), %%r14 \n\t"
7372 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007373#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03007374 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03007375
Avi Kivity6aa8b732006-12-10 02:21:36 -08007376 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03007377 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03007378 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03007379 "jmp 2f \n\t"
7380 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
7381 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08007382 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007383 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02007384 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03007385 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
7386 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
7387 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
7388 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
7389 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
7390 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
7391 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08007392#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02007393 "mov %%r8, %c[r8](%0) \n\t"
7394 "mov %%r9, %c[r9](%0) \n\t"
7395 "mov %%r10, %c[r10](%0) \n\t"
7396 "mov %%r11, %c[r11](%0) \n\t"
7397 "mov %%r12, %c[r12](%0) \n\t"
7398 "mov %%r13, %c[r13](%0) \n\t"
7399 "mov %%r14, %c[r14](%0) \n\t"
7400 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007401#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03007402 "mov %%cr2, %%" _ASM_AX " \n\t"
7403 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03007404
Avi Kivityb188c81f2012-09-16 15:10:58 +03007405 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02007406 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03007407 ".pushsection .rodata \n\t"
7408 ".global vmx_return \n\t"
7409 "vmx_return: " _ASM_PTR " 2b \n\t"
7410 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02007411 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03007412 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02007413 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03007414 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007415 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
7416 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
7417 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
7418 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
7419 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
7420 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
7421 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08007422#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007423 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
7424 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
7425 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
7426 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
7427 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
7428 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
7429 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
7430 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08007431#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02007432 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
7433 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02007434 : "cc", "memory"
7435#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03007436 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02007437 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03007438#else
7439 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02007440#endif
7441 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08007442
Gleb Natapov2a7921b2012-08-12 16:12:29 +03007443 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
7444 if (debugctlmsr)
7445 update_debugctlmsr(debugctlmsr);
7446
Avi Kivityaa67f602012-08-01 16:48:03 +03007447#ifndef CONFIG_X86_64
7448 /*
7449 * The sysexit path does not restore ds/es, so we must set them to
7450 * a reasonable value ourselves.
7451 *
7452 * We can't defer this to vmx_load_host_state() since that function
7453 * may be executed in interrupt context, which saves and restore segments
7454 * around it, nullifying its effect.
7455 */
7456 loadsegment(ds, __USER_DS);
7457 loadsegment(es, __USER_DS);
7458#endif
7459
Avi Kivity6de4f3a2009-05-31 22:58:47 +03007460 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02007461 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivity69c73022011-03-07 15:26:44 +02007462 | (1 << VCPU_EXREG_CPL)
Avi Kivityaff48ba2010-12-05 18:56:11 +02007463 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03007464 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02007465 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007466 vcpu->arch.regs_dirty = 0;
7467
Avi Kivity1155f762007-11-22 11:30:47 +02007468 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
7469
Nadav Har'Eld462b812011-05-24 15:26:10 +03007470 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02007471
Avi Kivity51aa01d2010-07-20 14:31:20 +03007472 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02007473 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
Avi Kivity51aa01d2010-07-20 14:31:20 +03007474
Gleb Natapove0b890d2013-09-25 12:51:33 +03007475 /*
7476 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
7477 * we did not inject a still-pending event to L1 now because of
7478 * nested_run_pending, we need to re-enable this bit.
7479 */
7480 if (vmx->nested.nested_run_pending)
7481 kvm_make_request(KVM_REQ_EVENT, vcpu);
7482
7483 vmx->nested.nested_run_pending = 0;
7484
Avi Kivity51aa01d2010-07-20 14:31:20 +03007485 vmx_complete_atomic_exit(vmx);
7486 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03007487 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007488}
7489
Avi Kivity6aa8b732006-12-10 02:21:36 -08007490static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
7491{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007492 struct vcpu_vmx *vmx = to_vmx(vcpu);
7493
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08007494 free_vpid(vmx);
Nadav Har'Eld462b812011-05-24 15:26:10 +03007495 free_loaded_vmcs(vmx->loaded_vmcs);
Marcelo Tosatti26a865f2014-01-03 17:00:51 -02007496 free_nested(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007497 kfree(vmx->guest_msrs);
7498 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10007499 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007500}
7501
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007502static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007503{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007504 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10007505 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03007506 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007507
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04007508 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007509 return ERR_PTR(-ENOMEM);
7510
Sheng Yang2384d2b2008-01-17 15:14:33 +08007511 allocate_vpid(vmx);
7512
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007513 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
7514 if (err)
7515 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08007516
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04007517 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02007518 err = -ENOMEM;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007519 if (!vmx->guest_msrs) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007520 goto uninit_vcpu;
7521 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08007522
Nadav Har'Eld462b812011-05-24 15:26:10 +03007523 vmx->loaded_vmcs = &vmx->vmcs01;
7524 vmx->loaded_vmcs->vmcs = alloc_vmcs();
7525 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007526 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03007527 if (!vmm_exclusive)
7528 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
7529 loaded_vmcs_init(vmx->loaded_vmcs);
7530 if (!vmm_exclusive)
7531 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04007532
Avi Kivity15ad7142007-07-11 18:17:21 +03007533 cpu = get_cpu();
7534 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10007535 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10007536 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007537 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03007538 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007539 if (err)
7540 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02007541 if (vm_need_virtualize_apic_accesses(kvm)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02007542 err = alloc_apic_access_page(kvm);
7543 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02007544 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02007545 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08007546
Sheng Yangb927a3c2009-07-21 10:42:48 +08007547 if (enable_ept) {
7548 if (!kvm->arch.ept_identity_map_addr)
7549 kvm->arch.ept_identity_map_addr =
7550 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Gleb Natapov93ea5382011-02-21 12:07:59 +02007551 err = -ENOMEM;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08007552 if (alloc_identity_pagetable(kvm) != 0)
7553 goto free_vmcs;
Gleb Natapov93ea5382011-02-21 12:07:59 +02007554 if (!init_rmode_identity_map(kvm))
7555 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08007556 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08007557
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03007558 vmx->nested.current_vmptr = -1ull;
7559 vmx->nested.current_vmcs12 = NULL;
7560
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007561 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08007562
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007563free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08007564 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007565free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007566 kfree(vmx->guest_msrs);
7567uninit_vcpu:
7568 kvm_vcpu_uninit(&vmx->vcpu);
7569free_vcpu:
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08007570 free_vpid(vmx);
Rusty Russella4770342007-08-01 14:46:11 +10007571 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007572 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007573}
7574
Yang, Sheng002c7f72007-07-31 14:23:01 +03007575static void __init vmx_check_processor_compat(void *rtn)
7576{
7577 struct vmcs_config vmcs_conf;
7578
7579 *(int *)rtn = 0;
7580 if (setup_vmcs_config(&vmcs_conf) < 0)
7581 *(int *)rtn = -EIO;
7582 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
7583 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
7584 smp_processor_id());
7585 *(int *)rtn = -EIO;
7586 }
7587}
7588
Sheng Yang67253af2008-04-25 10:20:22 +08007589static int get_ept_level(void)
7590{
7591 return VMX_EPT_DEFAULT_GAW + 1;
7592}
7593
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007594static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08007595{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007596 u64 ret;
7597
Sheng Yang522c68c2009-04-27 20:35:43 +08007598 /* For VT-d and EPT combination
7599 * 1. MMIO: always map as UC
7600 * 2. EPT with VT-d:
7601 * a. VT-d without snooping control feature: can't guarantee the
7602 * result, try to trust guest.
7603 * b. VT-d with snooping control feature: snooping control feature of
7604 * VT-d engine can guarantee the cache correctness. Just set it
7605 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08007606 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08007607 * consistent with host MTRR
7608 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007609 if (is_mmio)
7610 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06007611 else if (kvm_arch_has_noncoherent_dma(vcpu->kvm))
Sheng Yang522c68c2009-04-27 20:35:43 +08007612 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
7613 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007614 else
Sheng Yang522c68c2009-04-27 20:35:43 +08007615 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
Sheng Yanga19a6d12010-02-09 16:41:53 +08007616 | VMX_EPT_IPAT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007617
7618 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08007619}
7620
Sheng Yang17cc3932010-01-05 19:02:27 +08007621static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02007622{
Sheng Yang878403b2010-01-05 19:02:29 +08007623 if (enable_ept && !cpu_has_vmx_ept_1g_page())
7624 return PT_DIRECTORY_LEVEL;
7625 else
7626 /* For shadow and EPT supported 1GB page */
7627 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02007628}
7629
Sheng Yang0e851882009-12-18 16:48:46 +08007630static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
7631{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007632 struct kvm_cpuid_entry2 *best;
7633 struct vcpu_vmx *vmx = to_vmx(vcpu);
7634 u32 exec_control;
7635
7636 vmx->rdtscp_enabled = false;
7637 if (vmx_rdtscp_supported()) {
7638 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7639 if (exec_control & SECONDARY_EXEC_RDTSCP) {
7640 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
7641 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
7642 vmx->rdtscp_enabled = true;
7643 else {
7644 exec_control &= ~SECONDARY_EXEC_RDTSCP;
7645 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
7646 exec_control);
7647 }
7648 }
7649 }
Mao, Junjiead756a12012-07-02 01:18:48 +00007650
Mao, Junjiead756a12012-07-02 01:18:48 +00007651 /* Exposing INVPCID only when PCID is exposed */
7652 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
7653 if (vmx_invpcid_supported() &&
Ren, Yongjie4f977042012-09-07 07:36:59 +00007654 best && (best->ebx & bit(X86_FEATURE_INVPCID)) &&
Mao, Junjiead756a12012-07-02 01:18:48 +00007655 guest_cpuid_has_pcid(vcpu)) {
Takashi Iwai29282fd2012-11-09 15:20:17 +01007656 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Mao, Junjiead756a12012-07-02 01:18:48 +00007657 exec_control |= SECONDARY_EXEC_ENABLE_INVPCID;
7658 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
7659 exec_control);
7660 } else {
Takashi Iwai29282fd2012-11-09 15:20:17 +01007661 if (cpu_has_secondary_exec_ctrls()) {
7662 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7663 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
7664 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
7665 exec_control);
7666 }
Mao, Junjiead756a12012-07-02 01:18:48 +00007667 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00007668 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00007669 }
Sheng Yang0e851882009-12-18 16:48:46 +08007670}
7671
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007672static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
7673{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03007674 if (func == 1 && nested)
7675 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007676}
7677
Yang Zhang25d92082013-08-06 12:00:32 +03007678static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
7679 struct x86_exception *fault)
7680{
Jan Kiszka533558b2014-01-04 18:47:20 +01007681 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7682 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03007683
7684 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01007685 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03007686 else
Jan Kiszka533558b2014-01-04 18:47:20 +01007687 exit_reason = EXIT_REASON_EPT_VIOLATION;
7688 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03007689 vmcs12->guest_physical_address = fault->address;
7690}
7691
Nadav Har'El155a97a2013-08-05 11:07:16 +03007692/* Callbacks for nested_ept_init_mmu_context: */
7693
7694static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
7695{
7696 /* return the page table to be shadowed - in our case, EPT12 */
7697 return get_vmcs12(vcpu)->ept_pointer;
7698}
7699
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02007700static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03007701{
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02007702 kvm_init_shadow_ept_mmu(vcpu, &vcpu->arch.mmu,
Nadav Har'El155a97a2013-08-05 11:07:16 +03007703 nested_vmx_ept_caps & VMX_EPT_EXECUTE_ONLY_BIT);
7704
7705 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
7706 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
7707 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
7708
7709 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03007710}
7711
7712static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
7713{
7714 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
7715}
7716
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03007717static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
7718 struct x86_exception *fault)
7719{
7720 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7721
7722 WARN_ON(!is_guest_mode(vcpu));
7723
7724 /* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */
7725 if (vmcs12->exception_bitmap & (1u << PF_VECTOR))
Jan Kiszka533558b2014-01-04 18:47:20 +01007726 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
7727 vmcs_read32(VM_EXIT_INTR_INFO),
7728 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03007729 else
7730 kvm_inject_page_fault(vcpu, fault);
7731}
7732
Jan Kiszkaf41245002014-03-07 20:03:13 +01007733static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
7734{
7735 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
7736 struct vcpu_vmx *vmx = to_vmx(vcpu);
7737
7738 if (vcpu->arch.virtual_tsc_khz == 0)
7739 return;
7740
7741 /* Make sure short timeouts reliably trigger an immediate vmexit.
7742 * hrtimer_start does not guarantee this. */
7743 if (preemption_timeout <= 1) {
7744 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
7745 return;
7746 }
7747
7748 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
7749 preemption_timeout *= 1000000;
7750 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
7751 hrtimer_start(&vmx->nested.preemption_timer,
7752 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
7753}
7754
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007755/*
7756 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
7757 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
7758 * with L0's requirements for its guest (a.k.a. vmsc01), so we can run the L2
7759 * guest in a way that will both be appropriate to L1's requests, and our
7760 * needs. In addition to modifying the active vmcs (which is vmcs02), this
7761 * function also has additional necessary side-effects, like setting various
7762 * vcpu->arch fields.
7763 */
7764static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7765{
7766 struct vcpu_vmx *vmx = to_vmx(vcpu);
7767 u32 exec_control;
7768
7769 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
7770 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
7771 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
7772 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
7773 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
7774 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
7775 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
7776 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
7777 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
7778 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
7779 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
7780 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
7781 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
7782 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
7783 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
7784 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
7785 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
7786 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
7787 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
7788 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
7789 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
7790 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
7791 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
7792 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
7793 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
7794 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
7795 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
7796 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
7797 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
7798 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
7799 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
7800 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
7801 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
7802 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
7803 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
7804 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
7805
7806 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
7807 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
7808 vmcs12->vm_entry_intr_info_field);
7809 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
7810 vmcs12->vm_entry_exception_error_code);
7811 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
7812 vmcs12->vm_entry_instruction_len);
7813 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
7814 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007815 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Jan Kiszka503cd0c2013-03-03 13:05:44 +01007816 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
Gleb Natapov63fbf592013-07-28 18:31:06 +03007817 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007818 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
7819 vmcs12->guest_pending_dbg_exceptions);
7820 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
7821 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
7822
7823 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7824
Jan Kiszkaf41245002014-03-07 20:03:13 +01007825 exec_control = vmcs12->pin_based_vm_exec_control;
7826 exec_control |= vmcs_config.pin_based_exec_ctrl;
7827 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
7828 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007829
Jan Kiszkaf41245002014-03-07 20:03:13 +01007830 vmx->nested.preemption_timer_expired = false;
7831 if (nested_cpu_has_preemption_timer(vmcs12))
7832 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +01007833
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007834 /*
7835 * Whether page-faults are trapped is determined by a combination of
7836 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
7837 * If enable_ept, L0 doesn't care about page faults and we should
7838 * set all of these to L1's desires. However, if !enable_ept, L0 does
7839 * care about (at least some) page faults, and because it is not easy
7840 * (if at all possible?) to merge L0 and L1's desires, we simply ask
7841 * to exit on each and every L2 page fault. This is done by setting
7842 * MASK=MATCH=0 and (see below) EB.PF=1.
7843 * Note that below we don't need special code to set EB.PF beyond the
7844 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
7845 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
7846 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
7847 *
7848 * A problem with this approach (when !enable_ept) is that L1 may be
7849 * injected with more page faults than it asked for. This could have
7850 * caused problems, but in practice existing hypervisors don't care.
7851 * To fix this, we will need to emulate the PFEC checking (on the L1
7852 * page tables), using walk_addr(), when injecting PFs to L1.
7853 */
7854 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
7855 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
7856 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
7857 enable_ept ? vmcs12->page_fault_error_code_match : 0);
7858
7859 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf41245002014-03-07 20:03:13 +01007860 exec_control = vmx_secondary_exec_control(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007861 if (!vmx->rdtscp_enabled)
7862 exec_control &= ~SECONDARY_EXEC_RDTSCP;
7863 /* Take the following fields only from vmcs12 */
7864 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7865 if (nested_cpu_has(vmcs12,
7866 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
7867 exec_control |= vmcs12->secondary_vm_exec_control;
7868
7869 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
7870 /*
7871 * Translate L1 physical address to host physical
7872 * address for vmcs02. Keep the page pinned, so this
7873 * physical address remains valid. We keep a reference
7874 * to it so we can release it later.
7875 */
7876 if (vmx->nested.apic_access_page) /* shouldn't happen */
7877 nested_release_page(vmx->nested.apic_access_page);
7878 vmx->nested.apic_access_page =
7879 nested_get_page(vcpu, vmcs12->apic_access_addr);
7880 /*
7881 * If translation failed, no matter: This feature asks
7882 * to exit when accessing the given address, and if it
7883 * can never be accessed, this feature won't do
7884 * anything anyway.
7885 */
7886 if (!vmx->nested.apic_access_page)
7887 exec_control &=
7888 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7889 else
7890 vmcs_write64(APIC_ACCESS_ADDR,
7891 page_to_phys(vmx->nested.apic_access_page));
Jan Kiszkaca3f2572013-12-16 12:55:46 +01007892 } else if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) {
7893 exec_control |=
7894 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7895 vmcs_write64(APIC_ACCESS_ADDR,
7896 page_to_phys(vcpu->kvm->arch.apic_access_page));
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007897 }
7898
7899 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
7900 }
7901
7902
7903 /*
7904 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
7905 * Some constant fields are set here by vmx_set_constant_host_state().
7906 * Other fields are different per CPU, and will be set later when
7907 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
7908 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08007909 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007910
7911 /*
7912 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
7913 * entry, but only if the current (host) sp changed from the value
7914 * we wrote last (vmx->host_rsp). This cache is no longer relevant
7915 * if we switch vmcs, and rather than hold a separate cache per vmcs,
7916 * here we just force the write to happen on entry.
7917 */
7918 vmx->host_rsp = 0;
7919
7920 exec_control = vmx_exec_control(vmx); /* L0's desires */
7921 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
7922 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
7923 exec_control &= ~CPU_BASED_TPR_SHADOW;
7924 exec_control |= vmcs12->cpu_based_vm_exec_control;
7925 /*
7926 * Merging of IO and MSR bitmaps not currently supported.
7927 * Rather, exit every time.
7928 */
7929 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
7930 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
7931 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
7932
7933 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
7934
7935 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
7936 * bitwise-or of what L1 wants to trap for L2, and what we want to
7937 * trap. Note that CR0.TS also needs updating - we do this later.
7938 */
7939 update_exception_bitmap(vcpu);
7940 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
7941 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
7942
Nadav Har'El8049d652013-08-05 11:07:06 +03007943 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
7944 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
7945 * bits are further modified by vmx_set_efer() below.
7946 */
Jan Kiszkaf41245002014-03-07 20:03:13 +01007947 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +03007948
7949 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
7950 * emulated by vmx_set_efer(), below.
7951 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02007952 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +03007953 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
7954 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007955 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
7956
Jan Kiszka44811c02013-08-04 17:17:27 +02007957 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007958 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +02007959 vcpu->arch.pat = vmcs12->guest_ia32_pat;
7960 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007961 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
7962
7963
7964 set_cr4_guest_host_mask(vmx);
7965
Paolo Bonzini36be0b92014-02-24 12:30:04 +01007966 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
7967 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
7968
Nadav Har'El27fc51b2011-08-02 15:54:52 +03007969 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
7970 vmcs_write64(TSC_OFFSET,
7971 vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
7972 else
7973 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007974
7975 if (enable_vpid) {
7976 /*
7977 * Trivially support vpid by letting L2s share their parent
7978 * L1's vpid. TODO: move to a more elaborate solution, giving
7979 * each L2 its own vpid and exposing the vpid feature to L1.
7980 */
7981 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
7982 vmx_flush_tlb(vcpu);
7983 }
7984
Nadav Har'El155a97a2013-08-05 11:07:16 +03007985 if (nested_cpu_has_ept(vmcs12)) {
7986 kvm_mmu_unload(vcpu);
7987 nested_ept_init_mmu_context(vcpu);
7988 }
7989
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007990 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
7991 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +02007992 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007993 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
7994 else
7995 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
7996 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
7997 vmx_set_efer(vcpu, vcpu->arch.efer);
7998
7999 /*
8000 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
8001 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
8002 * The CR0_READ_SHADOW is what L2 should have expected to read given
8003 * the specifications by L1; It's not enough to take
8004 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
8005 * have more bits than L1 expected.
8006 */
8007 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
8008 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
8009
8010 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
8011 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
8012
8013 /* shadow page tables on either EPT or shadow page tables */
8014 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
8015 kvm_mmu_reset_context(vcpu);
8016
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03008017 if (!enable_ept)
8018 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
8019
Nadav Har'El3633cfc2013-08-05 11:07:07 +03008020 /*
8021 * L1 may access the L2's PDPTR, so save them to construct vmcs12
8022 */
8023 if (enable_ept) {
8024 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
8025 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
8026 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
8027 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
8028 }
8029
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008030 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
8031 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
8032}
8033
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008034/*
8035 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
8036 * for running an L2 nested guest.
8037 */
8038static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
8039{
8040 struct vmcs12 *vmcs12;
8041 struct vcpu_vmx *vmx = to_vmx(vcpu);
8042 int cpu;
8043 struct loaded_vmcs *vmcs02;
Jan Kiszka384bb782013-04-20 10:52:36 +02008044 bool ia32e;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008045
8046 if (!nested_vmx_check_permission(vcpu) ||
8047 !nested_vmx_check_vmcs12(vcpu))
8048 return 1;
8049
8050 skip_emulated_instruction(vcpu);
8051 vmcs12 = get_vmcs12(vcpu);
8052
Abel Gordon012f83c2013-04-18 14:39:25 +03008053 if (enable_shadow_vmcs)
8054 copy_shadow_to_vmcs12(vmx);
8055
Nadav Har'El7c177932011-05-25 23:12:04 +03008056 /*
8057 * The nested entry process starts with enforcing various prerequisites
8058 * on vmcs12 as required by the Intel SDM, and act appropriately when
8059 * they fail: As the SDM explains, some conditions should cause the
8060 * instruction to fail, while others will cause the instruction to seem
8061 * to succeed, but return an EXIT_REASON_INVALID_STATE.
8062 * To speed up the normal (success) code path, we should avoid checking
8063 * for misconfigurations which will anyway be caught by the processor
8064 * when using the merged vmcs02.
8065 */
8066 if (vmcs12->launch_state == launch) {
8067 nested_vmx_failValid(vcpu,
8068 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
8069 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
8070 return 1;
8071 }
8072
Jan Kiszka6dfacad2013-12-04 08:58:54 +01008073 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
8074 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +02008075 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
8076 return 1;
8077 }
8078
Nadav Har'El7c177932011-05-25 23:12:04 +03008079 if ((vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_MSR_BITMAPS) &&
8080 !IS_ALIGNED(vmcs12->msr_bitmap, PAGE_SIZE)) {
8081 /*TODO: Also verify bits beyond physical address width are 0*/
8082 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
8083 return 1;
8084 }
8085
8086 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
8087 !IS_ALIGNED(vmcs12->apic_access_addr, PAGE_SIZE)) {
8088 /*TODO: Also verify bits beyond physical address width are 0*/
8089 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
8090 return 1;
8091 }
8092
8093 if (vmcs12->vm_entry_msr_load_count > 0 ||
8094 vmcs12->vm_exit_msr_load_count > 0 ||
8095 vmcs12->vm_exit_msr_store_count > 0) {
Jan Kiszkabd801582011-09-12 11:26:22 +02008096 pr_warn_ratelimited("%s: VMCS MSR_{LOAD,STORE} unsupported\n",
8097 __func__);
Nadav Har'El7c177932011-05-25 23:12:04 +03008098 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
8099 return 1;
8100 }
8101
8102 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
8103 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high) ||
8104 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
8105 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high) ||
8106 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
8107 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high) ||
8108 !vmx_control_verify(vmcs12->vm_exit_controls,
8109 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high) ||
8110 !vmx_control_verify(vmcs12->vm_entry_controls,
8111 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high))
8112 {
8113 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
8114 return 1;
8115 }
8116
8117 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
8118 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
8119 nested_vmx_failValid(vcpu,
8120 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
8121 return 1;
8122 }
8123
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02008124 if (!nested_cr0_valid(vmcs12, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03008125 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
8126 nested_vmx_entry_failure(vcpu, vmcs12,
8127 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
8128 return 1;
8129 }
8130 if (vmcs12->vmcs_link_pointer != -1ull) {
8131 nested_vmx_entry_failure(vcpu, vmcs12,
8132 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
8133 return 1;
8134 }
8135
8136 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +02008137 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +02008138 * are performed on the field for the IA32_EFER MSR:
8139 * - Bits reserved in the IA32_EFER MSR must be 0.
8140 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
8141 * the IA-32e mode guest VM-exit control. It must also be identical
8142 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
8143 * CR0.PG) is 1.
8144 */
8145 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
8146 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
8147 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
8148 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
8149 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
8150 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
8151 nested_vmx_entry_failure(vcpu, vmcs12,
8152 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
8153 return 1;
8154 }
8155 }
8156
8157 /*
8158 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
8159 * IA32_EFER MSR must be 0 in the field for that register. In addition,
8160 * the values of the LMA and LME bits in the field must each be that of
8161 * the host address-space size VM-exit control.
8162 */
8163 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
8164 ia32e = (vmcs12->vm_exit_controls &
8165 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
8166 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
8167 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
8168 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
8169 nested_vmx_entry_failure(vcpu, vmcs12,
8170 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
8171 return 1;
8172 }
8173 }
8174
8175 /*
Nadav Har'El7c177932011-05-25 23:12:04 +03008176 * We're finally done with prerequisite checking, and can start with
8177 * the nested entry.
8178 */
8179
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008180 vmcs02 = nested_get_current_vmcs02(vmx);
8181 if (!vmcs02)
8182 return -ENOMEM;
8183
8184 enter_guest_mode(vcpu);
8185
8186 vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
8187
8188 cpu = get_cpu();
8189 vmx->loaded_vmcs = vmcs02;
8190 vmx_vcpu_put(vcpu);
8191 vmx_vcpu_load(vcpu, cpu);
8192 vcpu->cpu = cpu;
8193 put_cpu();
8194
Jan Kiszka36c3cc42013-02-23 22:35:37 +01008195 vmx_segment_cache_clear(vmx);
8196
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008197 vmcs12->launch_state = 1;
8198
8199 prepare_vmcs02(vcpu, vmcs12);
8200
Jan Kiszka6dfacad2013-12-04 08:58:54 +01008201 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
8202 return kvm_emulate_halt(vcpu);
8203
Jan Kiszka7af40ad32014-01-04 18:47:23 +01008204 vmx->nested.nested_run_pending = 1;
8205
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008206 /*
8207 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
8208 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
8209 * returned as far as L1 is concerned. It will only return (and set
8210 * the success flag) when L2 exits (see nested_vmx_vmexit()).
8211 */
8212 return 1;
8213}
8214
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008215/*
8216 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
8217 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
8218 * This function returns the new value we should put in vmcs12.guest_cr0.
8219 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
8220 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
8221 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
8222 * didn't trap the bit, because if L1 did, so would L0).
8223 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
8224 * been modified by L2, and L1 knows it. So just leave the old value of
8225 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
8226 * isn't relevant, because if L0 traps this bit it can set it to anything.
8227 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
8228 * changed these bits, and therefore they need to be updated, but L0
8229 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
8230 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
8231 */
8232static inline unsigned long
8233vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
8234{
8235 return
8236 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
8237 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
8238 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
8239 vcpu->arch.cr0_guest_owned_bits));
8240}
8241
8242static inline unsigned long
8243vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
8244{
8245 return
8246 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
8247 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
8248 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
8249 vcpu->arch.cr4_guest_owned_bits));
8250}
8251
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008252static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
8253 struct vmcs12 *vmcs12)
8254{
8255 u32 idt_vectoring;
8256 unsigned int nr;
8257
Gleb Natapov851eb6672013-09-25 12:51:34 +03008258 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008259 nr = vcpu->arch.exception.nr;
8260 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
8261
8262 if (kvm_exception_is_soft(nr)) {
8263 vmcs12->vm_exit_instruction_len =
8264 vcpu->arch.event_exit_inst_len;
8265 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
8266 } else
8267 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
8268
8269 if (vcpu->arch.exception.has_error_code) {
8270 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
8271 vmcs12->idt_vectoring_error_code =
8272 vcpu->arch.exception.error_code;
8273 }
8274
8275 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +01008276 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008277 vmcs12->idt_vectoring_info_field =
8278 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
8279 } else if (vcpu->arch.interrupt.pending) {
8280 nr = vcpu->arch.interrupt.nr;
8281 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
8282
8283 if (vcpu->arch.interrupt.soft) {
8284 idt_vectoring |= INTR_TYPE_SOFT_INTR;
8285 vmcs12->vm_entry_instruction_len =
8286 vcpu->arch.event_exit_inst_len;
8287 } else
8288 idt_vectoring |= INTR_TYPE_EXT_INTR;
8289
8290 vmcs12->idt_vectoring_info_field = idt_vectoring;
8291 }
8292}
8293
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008294static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
8295{
8296 struct vcpu_vmx *vmx = to_vmx(vcpu);
8297
Jan Kiszkaf41245002014-03-07 20:03:13 +01008298 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
8299 vmx->nested.preemption_timer_expired) {
8300 if (vmx->nested.nested_run_pending)
8301 return -EBUSY;
8302 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
8303 return 0;
8304 }
8305
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008306 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +01008307 if (vmx->nested.nested_run_pending ||
8308 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008309 return -EBUSY;
8310 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
8311 NMI_VECTOR | INTR_TYPE_NMI_INTR |
8312 INTR_INFO_VALID_MASK, 0);
8313 /*
8314 * The NMI-triggered VM exit counts as injection:
8315 * clear this one and block further NMIs.
8316 */
8317 vcpu->arch.nmi_pending = 0;
8318 vmx_set_nmi_mask(vcpu, true);
8319 return 0;
8320 }
8321
8322 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
8323 nested_exit_on_intr(vcpu)) {
8324 if (vmx->nested.nested_run_pending)
8325 return -EBUSY;
8326 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
8327 }
8328
8329 return 0;
8330}
8331
Jan Kiszkaf41245002014-03-07 20:03:13 +01008332static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
8333{
8334 ktime_t remaining =
8335 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
8336 u64 value;
8337
8338 if (ktime_to_ns(remaining) <= 0)
8339 return 0;
8340
8341 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
8342 do_div(value, 1000000);
8343 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
8344}
8345
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008346/*
8347 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
8348 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
8349 * and this function updates it to reflect the changes to the guest state while
8350 * L2 was running (and perhaps made some exits which were handled directly by L0
8351 * without going back to L1), and to reflect the exit reason.
8352 * Note that we do not have to copy here all VMCS fields, just those that
8353 * could have changed by the L2 guest or the exit - i.e., the guest-state and
8354 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
8355 * which already writes to vmcs12 directly.
8356 */
Jan Kiszka533558b2014-01-04 18:47:20 +01008357static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
8358 u32 exit_reason, u32 exit_intr_info,
8359 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008360{
8361 /* update guest state fields: */
8362 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
8363 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
8364
8365 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
8366 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
8367 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
8368 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
8369
8370 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
8371 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
8372 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
8373 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
8374 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
8375 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
8376 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
8377 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
8378 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
8379 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
8380 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
8381 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
8382 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
8383 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
8384 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
8385 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
8386 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
8387 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
8388 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
8389 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
8390 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
8391 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
8392 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
8393 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
8394 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
8395 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
8396 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
8397 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
8398 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
8399 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
8400 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
8401 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
8402 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
8403 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
8404 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
8405 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
8406
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008407 vmcs12->guest_interruptibility_info =
8408 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
8409 vmcs12->guest_pending_dbg_exceptions =
8410 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +01008411 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
8412 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
8413 else
8414 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008415
Jan Kiszkaf41245002014-03-07 20:03:13 +01008416 if (nested_cpu_has_preemption_timer(vmcs12)) {
8417 if (vmcs12->vm_exit_controls &
8418 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
8419 vmcs12->vmx_preemption_timer_value =
8420 vmx_get_preemption_timer_value(vcpu);
8421 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
8422 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +08008423
Nadav Har'El3633cfc2013-08-05 11:07:07 +03008424 /*
8425 * In some cases (usually, nested EPT), L2 is allowed to change its
8426 * own CR3 without exiting. If it has changed it, we must keep it.
8427 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
8428 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
8429 *
8430 * Additionally, restore L2's PDPTR to vmcs12.
8431 */
8432 if (enable_ept) {
8433 vmcs12->guest_cr3 = vmcs_read64(GUEST_CR3);
8434 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
8435 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
8436 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
8437 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
8438 }
8439
Jan Kiszkac18911a2013-03-13 16:06:41 +01008440 vmcs12->vm_entry_controls =
8441 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +02008442 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +01008443
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008444 /* TODO: These cannot have changed unless we have MSR bitmaps and
8445 * the relevant bit asks not to trap the change */
8446 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
Jan Kiszkab8c07d52013-04-06 13:51:21 +02008447 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008448 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +02008449 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
8450 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008451 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
8452 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
8453 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzini36be0b92014-02-24 12:30:04 +01008454 if (vmx_mpx_supported())
8455 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008456
8457 /* update exit information fields: */
8458
Jan Kiszka533558b2014-01-04 18:47:20 +01008459 vmcs12->vm_exit_reason = exit_reason;
8460 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008461
Jan Kiszka533558b2014-01-04 18:47:20 +01008462 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +02008463 if ((vmcs12->vm_exit_intr_info &
8464 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8465 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
8466 vmcs12->vm_exit_intr_error_code =
8467 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008468 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008469 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
8470 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8471
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008472 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
8473 /* vm_entry_intr_info_field is cleared on exit. Emulate this
8474 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008475 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008476
8477 /*
8478 * Transfer the event that L0 or L1 may wanted to inject into
8479 * L2 to IDT_VECTORING_INFO_FIELD.
8480 */
8481 vmcs12_save_pending_event(vcpu, vmcs12);
8482 }
8483
8484 /*
8485 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
8486 * preserved above and would only end up incorrectly in L1.
8487 */
8488 vcpu->arch.nmi_injected = false;
8489 kvm_clear_exception_queue(vcpu);
8490 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008491}
8492
8493/*
8494 * A part of what we need to when the nested L2 guest exits and we want to
8495 * run its L1 parent, is to reset L1's guest state to the host state specified
8496 * in vmcs12.
8497 * This function is to be called not only on normal nested exit, but also on
8498 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
8499 * Failures During or After Loading Guest State").
8500 * This function should be called when the active VMCS is L1's (vmcs01).
8501 */
Jan Kiszka733568f2013-02-23 15:07:47 +01008502static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
8503 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008504{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +08008505 struct kvm_segment seg;
8506
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008507 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
8508 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +02008509 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008510 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
8511 else
8512 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
8513 vmx_set_efer(vcpu, vcpu->arch.efer);
8514
8515 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
8516 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -07008517 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008518 /*
8519 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
8520 * actually changed, because it depends on the current state of
8521 * fpu_active (which may have changed).
8522 * Note that vmx_set_cr0 refers to efer set above.
8523 */
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +02008524 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008525 /*
8526 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
8527 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
8528 * but we also need to update cr0_guest_host_mask and exception_bitmap.
8529 */
8530 update_exception_bitmap(vcpu);
8531 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
8532 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
8533
8534 /*
8535 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
8536 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
8537 */
8538 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
8539 kvm_set_cr4(vcpu, vmcs12->host_cr4);
8540
Jan Kiszka29bf08f2013-12-28 16:31:52 +01008541 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +03008542
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008543 kvm_set_cr3(vcpu, vmcs12->host_cr3);
8544 kvm_mmu_reset_context(vcpu);
8545
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03008546 if (!enable_ept)
8547 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
8548
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008549 if (enable_vpid) {
8550 /*
8551 * Trivially support vpid by letting L2s share their parent
8552 * L1's vpid. TODO: move to a more elaborate solution, giving
8553 * each L2 its own vpid and exposing the vpid feature to L1.
8554 */
8555 vmx_flush_tlb(vcpu);
8556 }
8557
8558
8559 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
8560 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
8561 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
8562 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
8563 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008564
Paolo Bonzini36be0b92014-02-24 12:30:04 +01008565 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
8566 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
8567 vmcs_write64(GUEST_BNDCFGS, 0);
8568
Jan Kiszka44811c02013-08-04 17:17:27 +02008569 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008570 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +02008571 vcpu->arch.pat = vmcs12->host_ia32_pat;
8572 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008573 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
8574 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
8575 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +01008576
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +08008577 /* Set L1 segment info according to Intel SDM
8578 27.5.2 Loading Host Segment and Descriptor-Table Registers */
8579 seg = (struct kvm_segment) {
8580 .base = 0,
8581 .limit = 0xFFFFFFFF,
8582 .selector = vmcs12->host_cs_selector,
8583 .type = 11,
8584 .present = 1,
8585 .s = 1,
8586 .g = 1
8587 };
8588 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
8589 seg.l = 1;
8590 else
8591 seg.db = 1;
8592 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
8593 seg = (struct kvm_segment) {
8594 .base = 0,
8595 .limit = 0xFFFFFFFF,
8596 .type = 3,
8597 .present = 1,
8598 .s = 1,
8599 .db = 1,
8600 .g = 1
8601 };
8602 seg.selector = vmcs12->host_ds_selector;
8603 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
8604 seg.selector = vmcs12->host_es_selector;
8605 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
8606 seg.selector = vmcs12->host_ss_selector;
8607 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
8608 seg.selector = vmcs12->host_fs_selector;
8609 seg.base = vmcs12->host_fs_base;
8610 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
8611 seg.selector = vmcs12->host_gs_selector;
8612 seg.base = vmcs12->host_gs_base;
8613 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
8614 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +03008615 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +08008616 .limit = 0x67,
8617 .selector = vmcs12->host_tr_selector,
8618 .type = 11,
8619 .present = 1
8620 };
8621 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
8622
Jan Kiszka503cd0c2013-03-03 13:05:44 +01008623 kvm_set_dr(vcpu, 7, 0x400);
8624 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008625}
8626
8627/*
8628 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
8629 * and modify vmcs12 to make it see what it would expect to see there if
8630 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
8631 */
Jan Kiszka533558b2014-01-04 18:47:20 +01008632static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
8633 u32 exit_intr_info,
8634 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008635{
8636 struct vcpu_vmx *vmx = to_vmx(vcpu);
8637 int cpu;
8638 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8639
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008640 /* trying to cancel vmlaunch/vmresume is a bug */
8641 WARN_ON_ONCE(vmx->nested.nested_run_pending);
8642
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008643 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +01008644 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
8645 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008646
Bandan Das77b0f5d2014-04-19 18:17:45 -04008647 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
8648 && nested_exit_intr_ack_set(vcpu)) {
8649 int irq = kvm_cpu_get_interrupt(vcpu);
8650 WARN_ON(irq < 0);
8651 vmcs12->vm_exit_intr_info = irq |
8652 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
8653 }
8654
Jan Kiszka542060e2014-01-04 18:47:21 +01008655 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
8656 vmcs12->exit_qualification,
8657 vmcs12->idt_vectoring_info_field,
8658 vmcs12->vm_exit_intr_info,
8659 vmcs12->vm_exit_intr_error_code,
8660 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008661
8662 cpu = get_cpu();
8663 vmx->loaded_vmcs = &vmx->vmcs01;
8664 vmx_vcpu_put(vcpu);
8665 vmx_vcpu_load(vcpu, cpu);
8666 vcpu->cpu = cpu;
8667 put_cpu();
8668
Gleb Natapov2961e8762013-11-25 15:37:13 +02008669 vm_entry_controls_init(vmx, vmcs_read32(VM_ENTRY_CONTROLS));
8670 vm_exit_controls_init(vmx, vmcs_read32(VM_EXIT_CONTROLS));
Jan Kiszka36c3cc42013-02-23 22:35:37 +01008671 vmx_segment_cache_clear(vmx);
8672
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008673 /* if no vmcs02 cache requested, remove the one we used */
8674 if (VMCS02_POOL_SIZE == 0)
8675 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
8676
8677 load_vmcs12_host_state(vcpu, vmcs12);
8678
Nadav Har'El27fc51b2011-08-02 15:54:52 +03008679 /* Update TSC_OFFSET if TSC was changed while L2 ran */
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008680 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
8681
8682 /* This is needed for same reason as it was needed in prepare_vmcs02 */
8683 vmx->host_rsp = 0;
8684
8685 /* Unpin physical memory we referred to in vmcs02 */
8686 if (vmx->nested.apic_access_page) {
8687 nested_release_page(vmx->nested.apic_access_page);
8688 vmx->nested.apic_access_page = 0;
8689 }
8690
8691 /*
8692 * Exiting from L2 to L1, we're now back to L1 which thinks it just
8693 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
8694 * success or failure flag accordingly.
8695 */
8696 if (unlikely(vmx->fail)) {
8697 vmx->fail = 0;
8698 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
8699 } else
8700 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +03008701 if (enable_shadow_vmcs)
8702 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008703
8704 /* in case we halted in L2 */
8705 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008706}
8707
Nadav Har'El7c177932011-05-25 23:12:04 +03008708/*
Jan Kiszka42124922014-01-04 18:47:19 +01008709 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
8710 */
8711static void vmx_leave_nested(struct kvm_vcpu *vcpu)
8712{
8713 if (is_guest_mode(vcpu))
Jan Kiszka533558b2014-01-04 18:47:20 +01008714 nested_vmx_vmexit(vcpu, -1, 0, 0);
Jan Kiszka42124922014-01-04 18:47:19 +01008715 free_nested(to_vmx(vcpu));
8716}
8717
8718/*
Nadav Har'El7c177932011-05-25 23:12:04 +03008719 * L1's failure to enter L2 is a subset of a normal exit, as explained in
8720 * 23.7 "VM-entry failures during or after loading guest state" (this also
8721 * lists the acceptable exit-reason and exit-qualification parameters).
8722 * It should only be called before L2 actually succeeded to run, and when
8723 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
8724 */
8725static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
8726 struct vmcs12 *vmcs12,
8727 u32 reason, unsigned long qualification)
8728{
8729 load_vmcs12_host_state(vcpu, vmcs12);
8730 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
8731 vmcs12->exit_qualification = qualification;
8732 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +03008733 if (enable_shadow_vmcs)
8734 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +03008735}
8736
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02008737static int vmx_check_intercept(struct kvm_vcpu *vcpu,
8738 struct x86_instruction_info *info,
8739 enum x86_intercept_stage stage)
8740{
8741 return X86EMUL_CONTINUE;
8742}
8743
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03008744static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008745 .cpu_has_kvm_support = cpu_has_kvm_support,
8746 .disabled_by_bios = vmx_disabled_by_bios,
8747 .hardware_setup = hardware_setup,
8748 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03008749 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008750 .hardware_enable = hardware_enable,
8751 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08008752 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008753
8754 .vcpu_create = vmx_create_vcpu,
8755 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03008756 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008757
Avi Kivity04d2cc72007-09-10 18:10:54 +03008758 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008759 .vcpu_load = vmx_vcpu_load,
8760 .vcpu_put = vmx_vcpu_put,
8761
Jan Kiszkac8639012012-09-21 05:42:55 +02008762 .update_db_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008763 .get_msr = vmx_get_msr,
8764 .set_msr = vmx_set_msr,
8765 .get_segment_base = vmx_get_segment_base,
8766 .get_segment = vmx_get_segment,
8767 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02008768 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008769 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02008770 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02008771 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03008772 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008773 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008774 .set_cr3 = vmx_set_cr3,
8775 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008776 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008777 .get_idt = vmx_get_idt,
8778 .set_idt = vmx_set_idt,
8779 .get_gdt = vmx_get_gdt,
8780 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01008781 .get_dr6 = vmx_get_dr6,
8782 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03008783 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +01008784 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008785 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008786 .get_rflags = vmx_get_rflags,
8787 .set_rflags = vmx_set_rflags,
Avi Kivityebcbab42010-02-07 11:56:52 +02008788 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02008789 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008790
8791 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008792
Avi Kivity6aa8b732006-12-10 02:21:36 -08008793 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02008794 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008795 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04008796 .set_interrupt_shadow = vmx_set_interrupt_shadow,
8797 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02008798 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03008799 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008800 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02008801 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008802 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02008803 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008804 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01008805 .get_nmi_mask = vmx_get_nmi_mask,
8806 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008807 .enable_nmi_window = enable_nmi_window,
8808 .enable_irq_window = enable_irq_window,
8809 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08008810 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008811 .vm_has_apicv = vmx_vm_has_apicv,
8812 .load_eoi_exitmap = vmx_load_eoi_exitmap,
8813 .hwapic_irr_update = vmx_hwapic_irr_update,
8814 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +08008815 .sync_pir_to_irr = vmx_sync_pir_to_irr,
8816 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008817
Izik Eiduscbc94022007-10-25 00:29:55 +02008818 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08008819 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08008820 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03008821
Avi Kivity586f9602010-11-18 13:09:54 +02008822 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02008823
Sheng Yang17cc3932010-01-05 19:02:27 +08008824 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08008825
8826 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08008827
8828 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00008829 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02008830
8831 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08008832
8833 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10008834
Joerg Roedel4051b182011-03-25 09:44:49 +01008835 .set_tsc_khz = vmx_set_tsc_khz,
Will Auldba904632012-11-29 12:42:50 -08008836 .read_tsc_offset = vmx_read_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -10008837 .write_tsc_offset = vmx_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10008838 .adjust_tsc_offset = vmx_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01008839 .compute_tsc_offset = vmx_compute_tsc_offset,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03008840 .read_l1_tsc = vmx_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02008841
8842 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02008843
8844 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08008845 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008846 .mpx_supported = vmx_mpx_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008847
8848 .check_nested_events = vmx_check_nested_events,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008849};
8850
8851static int __init vmx_init(void)
8852{
Yang Zhang8d146952013-01-25 10:18:50 +08008853 int r, i, msr;
Avi Kivity26bb0982009-09-07 11:14:12 +03008854
8855 rdmsrl_safe(MSR_EFER, &host_efer);
8856
8857 for (i = 0; i < NR_VMX_MSR; ++i)
8858 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03008859
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008860 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03008861 if (!vmx_io_bitmap_a)
8862 return -ENOMEM;
8863
Guo Chao2106a542012-06-15 11:31:56 +08008864 r = -ENOMEM;
8865
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008866 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08008867 if (!vmx_io_bitmap_b)
He, Qingfdef3ad2007-04-30 09:45:24 +03008868 goto out;
He, Qingfdef3ad2007-04-30 09:45:24 +03008869
Avi Kivity58972972009-02-24 22:26:47 +02008870 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08008871 if (!vmx_msr_bitmap_legacy)
Sheng Yang25c5f222008-03-28 13:18:56 +08008872 goto out1;
Guo Chao2106a542012-06-15 11:31:56 +08008873
Yang Zhang8d146952013-01-25 10:18:50 +08008874 vmx_msr_bitmap_legacy_x2apic =
8875 (unsigned long *)__get_free_page(GFP_KERNEL);
8876 if (!vmx_msr_bitmap_legacy_x2apic)
8877 goto out2;
Sheng Yang25c5f222008-03-28 13:18:56 +08008878
Avi Kivity58972972009-02-24 22:26:47 +02008879 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08008880 if (!vmx_msr_bitmap_longmode)
Yang Zhang8d146952013-01-25 10:18:50 +08008881 goto out3;
Guo Chao2106a542012-06-15 11:31:56 +08008882
Yang Zhang8d146952013-01-25 10:18:50 +08008883 vmx_msr_bitmap_longmode_x2apic =
8884 (unsigned long *)__get_free_page(GFP_KERNEL);
8885 if (!vmx_msr_bitmap_longmode_x2apic)
8886 goto out4;
Abel Gordon4607c2d2013-04-18 14:35:55 +03008887 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
8888 if (!vmx_vmread_bitmap)
8889 goto out5;
8890
8891 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
8892 if (!vmx_vmwrite_bitmap)
8893 goto out6;
8894
8895 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
8896 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
8897 /* shadowed read/write fields */
8898 for (i = 0; i < max_shadow_read_write_fields; i++) {
8899 clear_bit(shadow_read_write_fields[i], vmx_vmwrite_bitmap);
8900 clear_bit(shadow_read_write_fields[i], vmx_vmread_bitmap);
8901 }
8902 /* shadowed read only fields */
8903 for (i = 0; i < max_shadow_read_only_fields; i++)
8904 clear_bit(shadow_read_only_fields[i], vmx_vmread_bitmap);
Avi Kivity58972972009-02-24 22:26:47 +02008905
He, Qingfdef3ad2007-04-30 09:45:24 +03008906 /*
8907 * Allow direct access to the PC debug port (it is often used for I/O
8908 * delays, but the vmexits simply slow things down).
8909 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008910 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
8911 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03008912
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008913 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03008914
Avi Kivity58972972009-02-24 22:26:47 +02008915 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
8916 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08008917
Sheng Yang2384d2b2008-01-17 15:14:33 +08008918 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
8919
Avi Kivity0ee75be2010-04-28 15:39:01 +03008920 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
8921 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03008922 if (r)
Abel Gordon4607c2d2013-04-18 14:35:55 +03008923 goto out7;
Sheng Yang25c5f222008-03-28 13:18:56 +08008924
Zhang Yanfei8f536b72012-12-06 23:43:34 +08008925#ifdef CONFIG_KEXEC
8926 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
8927 crash_vmclear_local_loaded_vmcss);
8928#endif
8929
Avi Kivity58972972009-02-24 22:26:47 +02008930 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
8931 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
8932 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
8933 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
8934 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
8935 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008936 vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true);
8937
Yang Zhang8d146952013-01-25 10:18:50 +08008938 memcpy(vmx_msr_bitmap_legacy_x2apic,
8939 vmx_msr_bitmap_legacy, PAGE_SIZE);
8940 memcpy(vmx_msr_bitmap_longmode_x2apic,
8941 vmx_msr_bitmap_longmode, PAGE_SIZE);
8942
Yang Zhang01e439b2013-04-11 19:25:12 +08008943 if (enable_apicv) {
Yang Zhang8d146952013-01-25 10:18:50 +08008944 for (msr = 0x800; msr <= 0x8ff; msr++)
8945 vmx_disable_intercept_msr_read_x2apic(msr);
8946
8947 /* According SDM, in x2apic mode, the whole id reg is used.
8948 * But in KVM, it only use the highest eight bits. Need to
8949 * intercept it */
8950 vmx_enable_intercept_msr_read_x2apic(0x802);
8951 /* TMCCT */
8952 vmx_enable_intercept_msr_read_x2apic(0x839);
8953 /* TPR */
8954 vmx_disable_intercept_msr_write_x2apic(0x808);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008955 /* EOI */
8956 vmx_disable_intercept_msr_write_x2apic(0x80b);
8957 /* SELF-IPI */
8958 vmx_disable_intercept_msr_write_x2apic(0x83f);
Yang Zhang8d146952013-01-25 10:18:50 +08008959 }
He, Qingfdef3ad2007-04-30 09:45:24 +03008960
Avi Kivity089d0342009-03-23 18:26:32 +02008961 if (enable_ept) {
Xudong Hao3f6d8c82012-05-22 11:23:15 +08008962 kvm_mmu_set_mask_ptes(0ull,
8963 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
8964 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
8965 0ull, VMX_EPT_EXECUTABLE_MASK);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08008966 ept_set_mmio_spte_mask();
Sheng Yang5fdbcb92008-07-16 09:25:40 +08008967 kvm_enable_tdp();
8968 } else
8969 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08008970
He, Qingfdef3ad2007-04-30 09:45:24 +03008971 return 0;
8972
Abel Gordon4607c2d2013-04-18 14:35:55 +03008973out7:
8974 free_page((unsigned long)vmx_vmwrite_bitmap);
8975out6:
8976 free_page((unsigned long)vmx_vmread_bitmap);
Yang Zhang458f2122013-04-08 15:26:33 +08008977out5:
8978 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
Yang Zhang8d146952013-01-25 10:18:50 +08008979out4:
Avi Kivity58972972009-02-24 22:26:47 +02008980 free_page((unsigned long)vmx_msr_bitmap_longmode);
Yang Zhang8d146952013-01-25 10:18:50 +08008981out3:
8982 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
Sheng Yang25c5f222008-03-28 13:18:56 +08008983out2:
Avi Kivity58972972009-02-24 22:26:47 +02008984 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03008985out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008986 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03008987out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008988 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03008989 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008990}
8991
8992static void __exit vmx_exit(void)
8993{
Yang Zhang8d146952013-01-25 10:18:50 +08008994 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
8995 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
Avi Kivity58972972009-02-24 22:26:47 +02008996 free_page((unsigned long)vmx_msr_bitmap_legacy);
8997 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008998 free_page((unsigned long)vmx_io_bitmap_b);
8999 free_page((unsigned long)vmx_io_bitmap_a);
Abel Gordon4607c2d2013-04-18 14:35:55 +03009000 free_page((unsigned long)vmx_vmwrite_bitmap);
9001 free_page((unsigned long)vmx_vmread_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03009002
Zhang Yanfei8f536b72012-12-06 23:43:34 +08009003#ifdef CONFIG_KEXEC
9004 rcu_assign_pointer(crash_vmclear_loaded_vmcss, NULL);
9005 synchronize_rcu();
9006#endif
9007
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08009008 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08009009}
9010
9011module_init(vmx_init)
9012module_exit(vmx_exit)