blob: 10474d26a00020e4bc2518106d4e87205ef39134 [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"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf41245002014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030037#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030038#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040039
Feng Wu28b835d2015-09-18 22:29:54 +080040#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080041#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080042#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020043#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020044#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080045#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020046#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020047#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010048#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080049#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010050#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080051#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070052#include <asm/mmu_context.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080053
Marcelo Tosatti229456f2009-06-17 09:22:14 -030054#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020055#include "pmu.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030056
Avi Kivity4ecac3f2008-05-13 13:23:38 +030057#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040058#define __ex_clear(x, reg) \
59 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030060
Avi Kivity6aa8b732006-12-10 02:21:36 -080061MODULE_AUTHOR("Qumranet");
62MODULE_LICENSE("GPL");
63
Josh Triplette9bda3b2012-03-20 23:33:51 -070064static const struct x86_cpu_id vmx_cpu_id[] = {
65 X86_FEATURE_MATCH(X86_FEATURE_VMX),
66 {}
67};
68MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
69
Rusty Russell476bc002012-01-13 09:32:18 +103070static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020071module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080072
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010073static bool __read_mostly enable_vnmi = 1;
74module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
75
Rusty Russell476bc002012-01-13 09:32:18 +103076static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020077module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020078
Rusty Russell476bc002012-01-13 09:32:18 +103079static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020080module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080081
Rusty Russell476bc002012-01-13 09:32:18 +103082static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070083module_param_named(unrestricted_guest,
84 enable_unrestricted_guest, bool, S_IRUGO);
85
Xudong Hao83c3a332012-05-28 19:33:35 +080086static bool __read_mostly enable_ept_ad_bits = 1;
87module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
88
Avi Kivitya27685c2012-06-12 20:30:18 +030089static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020090module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030091
Rusty Russell476bc002012-01-13 09:32:18 +103092static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030093module_param(fasteoi, bool, S_IRUGO);
94
Yang Zhang5a717852013-04-11 19:25:16 +080095static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080096module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080097
Abel Gordonabc4fc52013-04-18 14:35:25 +030098static bool __read_mostly enable_shadow_vmcs = 1;
99module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300100/*
101 * If nested=1, nested virtualization is supported, i.e., guests may use
102 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
103 * use VMX instructions.
104 */
Rusty Russell476bc002012-01-13 09:32:18 +1030105static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300106module_param(nested, bool, S_IRUGO);
107
Wanpeng Li20300092014-12-02 19:14:59 +0800108static u64 __read_mostly host_xss;
109
Kai Huang843e4332015-01-28 10:54:28 +0800110static bool __read_mostly enable_pml = 1;
111module_param_named(pml, enable_pml, bool, S_IRUGO);
112
Haozhong Zhang64903d62015-10-20 15:39:09 +0800113#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
114
Yunhong Jiang64672c92016-06-13 14:19:59 -0700115/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
116static int __read_mostly cpu_preemption_timer_multi;
117static bool __read_mostly enable_preemption_timer = 1;
118#ifdef CONFIG_X86_64
119module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
120#endif
121
Gleb Natapov50378782013-02-04 16:00:28 +0200122#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
123#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200124#define KVM_VM_CR0_ALWAYS_ON \
125 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200126#define KVM_CR4_GUEST_OWNED_BITS \
127 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800128 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200129
Avi Kivitycdc0e242009-12-06 17:21:14 +0200130#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
131#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
132
Avi Kivity78ac8b42010-04-08 18:19:35 +0300133#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
134
Jan Kiszkaf41245002014-03-07 20:03:13 +0100135#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
136
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800137/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300138 * Hyper-V requires all of these, so mark them as supported even though
139 * they are just treated the same as all-context.
140 */
141#define VMX_VPID_EXTENT_SUPPORTED_MASK \
142 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
143 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
144 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
145 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
146
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800147/*
148 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
149 * ple_gap: upper bound on the amount of time between two successive
150 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500151 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800152 * ple_window: upper bound on the amount of time a guest is allowed to execute
153 * in a PAUSE loop. Tests indicate that most spinlocks are held for
154 * less than 2^12 cycles
155 * Time is measured based on a counter that runs at the same rate as the TSC,
156 * refer SDM volume 3b section 21.6.13 & 22.1.3.
157 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200158#define KVM_VMX_DEFAULT_PLE_GAP 128
159#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
160#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
161#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
162#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
163 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
164
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800165static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
166module_param(ple_gap, int, S_IRUGO);
167
168static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
169module_param(ple_window, int, S_IRUGO);
170
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200171/* Default doubles per-vcpu window every exit. */
172static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
173module_param(ple_window_grow, int, S_IRUGO);
174
175/* Default resets per-vcpu window every exit to ple_window. */
176static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
177module_param(ple_window_shrink, int, S_IRUGO);
178
179/* Default is to compute the maximum so we can never overflow. */
180static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
181static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
182module_param(ple_window_max, int, S_IRUGO);
183
Avi Kivity83287ea422012-09-16 15:10:57 +0300184extern const ulong vmx_return;
185
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200186#define NR_AUTOLOAD_MSRS 8
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300187#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300188
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400189struct vmcs {
190 u32 revision_id;
191 u32 abort;
192 char data[0];
193};
194
Nadav Har'Eld462b812011-05-24 15:26:10 +0300195/*
196 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
197 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
198 * loaded on this CPU (so we can clear them if the CPU goes down).
199 */
200struct loaded_vmcs {
201 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700202 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300203 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200204 bool launched;
205 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200206 unsigned long vmcs_host_cr3; /* May not match real cr3 */
207 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100208 /* Support for vnmi-less CPUs */
209 int soft_vnmi_blocked;
210 ktime_t entry_time;
211 s64 vnmi_blocked_time;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300212 struct list_head loaded_vmcss_on_cpu_link;
213};
214
Avi Kivity26bb0982009-09-07 11:14:12 +0300215struct shared_msr_entry {
216 unsigned index;
217 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200218 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300219};
220
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300221/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300222 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
223 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
224 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
225 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
226 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
227 * More than one of these structures may exist, if L1 runs multiple L2 guests.
228 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
229 * underlying hardware which will be used to run L2.
230 * This structure is packed to ensure that its layout is identical across
231 * machines (necessary for live migration).
232 * If there are changes in this struct, VMCS12_REVISION must be changed.
233 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300234typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300235struct __packed vmcs12 {
236 /* According to the Intel spec, a VMCS region must start with the
237 * following two fields. Then follow implementation-specific data.
238 */
239 u32 revision_id;
240 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300241
Nadav Har'El27d6c862011-05-25 23:06:59 +0300242 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
243 u32 padding[7]; /* room for future expansion */
244
Nadav Har'El22bd0352011-05-25 23:05:57 +0300245 u64 io_bitmap_a;
246 u64 io_bitmap_b;
247 u64 msr_bitmap;
248 u64 vm_exit_msr_store_addr;
249 u64 vm_exit_msr_load_addr;
250 u64 vm_entry_msr_load_addr;
251 u64 tsc_offset;
252 u64 virtual_apic_page_addr;
253 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800254 u64 posted_intr_desc_addr;
Bandan Das27c42a12017-08-03 15:54:42 -0400255 u64 vm_function_control;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300256 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800257 u64 eoi_exit_bitmap0;
258 u64 eoi_exit_bitmap1;
259 u64 eoi_exit_bitmap2;
260 u64 eoi_exit_bitmap3;
Bandan Das41ab9372017-08-03 15:54:43 -0400261 u64 eptp_list_address;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800262 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300263 u64 guest_physical_address;
264 u64 vmcs_link_pointer;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400265 u64 pml_address;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300266 u64 guest_ia32_debugctl;
267 u64 guest_ia32_pat;
268 u64 guest_ia32_efer;
269 u64 guest_ia32_perf_global_ctrl;
270 u64 guest_pdptr0;
271 u64 guest_pdptr1;
272 u64 guest_pdptr2;
273 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100274 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300275 u64 host_ia32_pat;
276 u64 host_ia32_efer;
277 u64 host_ia32_perf_global_ctrl;
278 u64 padding64[8]; /* room for future expansion */
279 /*
280 * To allow migration of L1 (complete with its L2 guests) between
281 * machines of different natural widths (32 or 64 bit), we cannot have
282 * unsigned long fields with no explict size. We use u64 (aliased
283 * natural_width) instead. Luckily, x86 is little-endian.
284 */
285 natural_width cr0_guest_host_mask;
286 natural_width cr4_guest_host_mask;
287 natural_width cr0_read_shadow;
288 natural_width cr4_read_shadow;
289 natural_width cr3_target_value0;
290 natural_width cr3_target_value1;
291 natural_width cr3_target_value2;
292 natural_width cr3_target_value3;
293 natural_width exit_qualification;
294 natural_width guest_linear_address;
295 natural_width guest_cr0;
296 natural_width guest_cr3;
297 natural_width guest_cr4;
298 natural_width guest_es_base;
299 natural_width guest_cs_base;
300 natural_width guest_ss_base;
301 natural_width guest_ds_base;
302 natural_width guest_fs_base;
303 natural_width guest_gs_base;
304 natural_width guest_ldtr_base;
305 natural_width guest_tr_base;
306 natural_width guest_gdtr_base;
307 natural_width guest_idtr_base;
308 natural_width guest_dr7;
309 natural_width guest_rsp;
310 natural_width guest_rip;
311 natural_width guest_rflags;
312 natural_width guest_pending_dbg_exceptions;
313 natural_width guest_sysenter_esp;
314 natural_width guest_sysenter_eip;
315 natural_width host_cr0;
316 natural_width host_cr3;
317 natural_width host_cr4;
318 natural_width host_fs_base;
319 natural_width host_gs_base;
320 natural_width host_tr_base;
321 natural_width host_gdtr_base;
322 natural_width host_idtr_base;
323 natural_width host_ia32_sysenter_esp;
324 natural_width host_ia32_sysenter_eip;
325 natural_width host_rsp;
326 natural_width host_rip;
327 natural_width paddingl[8]; /* room for future expansion */
328 u32 pin_based_vm_exec_control;
329 u32 cpu_based_vm_exec_control;
330 u32 exception_bitmap;
331 u32 page_fault_error_code_mask;
332 u32 page_fault_error_code_match;
333 u32 cr3_target_count;
334 u32 vm_exit_controls;
335 u32 vm_exit_msr_store_count;
336 u32 vm_exit_msr_load_count;
337 u32 vm_entry_controls;
338 u32 vm_entry_msr_load_count;
339 u32 vm_entry_intr_info_field;
340 u32 vm_entry_exception_error_code;
341 u32 vm_entry_instruction_len;
342 u32 tpr_threshold;
343 u32 secondary_vm_exec_control;
344 u32 vm_instruction_error;
345 u32 vm_exit_reason;
346 u32 vm_exit_intr_info;
347 u32 vm_exit_intr_error_code;
348 u32 idt_vectoring_info_field;
349 u32 idt_vectoring_error_code;
350 u32 vm_exit_instruction_len;
351 u32 vmx_instruction_info;
352 u32 guest_es_limit;
353 u32 guest_cs_limit;
354 u32 guest_ss_limit;
355 u32 guest_ds_limit;
356 u32 guest_fs_limit;
357 u32 guest_gs_limit;
358 u32 guest_ldtr_limit;
359 u32 guest_tr_limit;
360 u32 guest_gdtr_limit;
361 u32 guest_idtr_limit;
362 u32 guest_es_ar_bytes;
363 u32 guest_cs_ar_bytes;
364 u32 guest_ss_ar_bytes;
365 u32 guest_ds_ar_bytes;
366 u32 guest_fs_ar_bytes;
367 u32 guest_gs_ar_bytes;
368 u32 guest_ldtr_ar_bytes;
369 u32 guest_tr_ar_bytes;
370 u32 guest_interruptibility_info;
371 u32 guest_activity_state;
372 u32 guest_sysenter_cs;
373 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100374 u32 vmx_preemption_timer_value;
375 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300376 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800377 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300378 u16 guest_es_selector;
379 u16 guest_cs_selector;
380 u16 guest_ss_selector;
381 u16 guest_ds_selector;
382 u16 guest_fs_selector;
383 u16 guest_gs_selector;
384 u16 guest_ldtr_selector;
385 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800386 u16 guest_intr_status;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400387 u16 guest_pml_index;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300388 u16 host_es_selector;
389 u16 host_cs_selector;
390 u16 host_ss_selector;
391 u16 host_ds_selector;
392 u16 host_fs_selector;
393 u16 host_gs_selector;
394 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300395};
396
397/*
398 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
399 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
400 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
401 */
402#define VMCS12_REVISION 0x11e57ed0
403
404/*
405 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
406 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
407 * current implementation, 4K are reserved to avoid future complications.
408 */
409#define VMCS12_SIZE 0x1000
410
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300411/* Used to remember the last vmcs02 used for some recently used vmcs12s */
412struct vmcs02_list {
413 struct list_head list;
414 gpa_t vmptr;
415 struct loaded_vmcs vmcs02;
416};
417
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300418/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300419 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
420 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
421 */
422struct nested_vmx {
423 /* Has the level1 guest done vmxon? */
424 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400425 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400426 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300427
428 /* The guest-physical address of the current VMCS L1 keeps for L2 */
429 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700430 /*
431 * Cache of the guest's VMCS, existing outside of guest memory.
432 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700433 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700434 */
435 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300436 /*
437 * Indicates if the shadow vmcs must be updated with the
438 * data hold by vmcs12
439 */
440 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300441
442 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
443 struct list_head vmcs02_pool;
444 int vmcs02_num;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200445 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300446 /* L2 must run next, and mustn't decide to exit to L1. */
447 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300448 /*
449 * Guest pages referred to in vmcs02 with host-physical pointers, so
450 * we must keep them pinned while L2 runs.
451 */
452 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800453 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800454 struct page *pi_desc_page;
455 struct pi_desc *pi_desc;
456 bool pi_pending;
457 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100458
Radim Krčmářd048c092016-08-08 20:16:22 +0200459 unsigned long *msr_bitmap;
460
Jan Kiszkaf41245002014-03-07 20:03:13 +0100461 struct hrtimer preemption_timer;
462 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200463
464 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
465 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800466
Wanpeng Li5c614b32015-10-13 09:18:36 -0700467 u16 vpid02;
468 u16 last_vpid;
469
David Matlack0115f9c2016-11-29 18:14:06 -0800470 /*
471 * We only store the "true" versions of the VMX capability MSRs. We
472 * generate the "non-true" versions by setting the must-be-1 bits
473 * according to the SDM.
474 */
Wincy Vanb9c237b2015-02-03 23:56:30 +0800475 u32 nested_vmx_procbased_ctls_low;
476 u32 nested_vmx_procbased_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800477 u32 nested_vmx_secondary_ctls_low;
478 u32 nested_vmx_secondary_ctls_high;
479 u32 nested_vmx_pinbased_ctls_low;
480 u32 nested_vmx_pinbased_ctls_high;
481 u32 nested_vmx_exit_ctls_low;
482 u32 nested_vmx_exit_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800483 u32 nested_vmx_entry_ctls_low;
484 u32 nested_vmx_entry_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800485 u32 nested_vmx_misc_low;
486 u32 nested_vmx_misc_high;
487 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700488 u32 nested_vmx_vpid_caps;
David Matlack62cc6b9d2016-11-29 18:14:07 -0800489 u64 nested_vmx_basic;
490 u64 nested_vmx_cr0_fixed0;
491 u64 nested_vmx_cr0_fixed1;
492 u64 nested_vmx_cr4_fixed0;
493 u64 nested_vmx_cr4_fixed1;
494 u64 nested_vmx_vmcs_enum;
Bandan Das27c42a12017-08-03 15:54:42 -0400495 u64 nested_vmx_vmfunc_controls;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200496
497 /* SMM related state */
498 struct {
499 /* in VMX operation on SMM entry? */
500 bool vmxon;
501 /* in guest mode on SMM entry? */
502 bool guest_mode;
503 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300504};
505
Yang Zhang01e439b2013-04-11 19:25:12 +0800506#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800507#define POSTED_INTR_SN 1
508
Yang Zhang01e439b2013-04-11 19:25:12 +0800509/* Posted-Interrupt Descriptor */
510struct pi_desc {
511 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800512 union {
513 struct {
514 /* bit 256 - Outstanding Notification */
515 u16 on : 1,
516 /* bit 257 - Suppress Notification */
517 sn : 1,
518 /* bit 271:258 - Reserved */
519 rsvd_1 : 14;
520 /* bit 279:272 - Notification Vector */
521 u8 nv;
522 /* bit 287:280 - Reserved */
523 u8 rsvd_2;
524 /* bit 319:288 - Notification Destination */
525 u32 ndst;
526 };
527 u64 control;
528 };
529 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800530} __aligned(64);
531
Yang Zhanga20ed542013-04-11 19:25:15 +0800532static bool pi_test_and_set_on(struct pi_desc *pi_desc)
533{
534 return test_and_set_bit(POSTED_INTR_ON,
535 (unsigned long *)&pi_desc->control);
536}
537
538static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
539{
540 return test_and_clear_bit(POSTED_INTR_ON,
541 (unsigned long *)&pi_desc->control);
542}
543
544static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
545{
546 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
547}
548
Feng Wuebbfc762015-09-18 22:29:46 +0800549static inline void pi_clear_sn(struct pi_desc *pi_desc)
550{
551 return clear_bit(POSTED_INTR_SN,
552 (unsigned long *)&pi_desc->control);
553}
554
555static inline void pi_set_sn(struct pi_desc *pi_desc)
556{
557 return set_bit(POSTED_INTR_SN,
558 (unsigned long *)&pi_desc->control);
559}
560
Paolo Bonziniad361092016-09-20 16:15:05 +0200561static inline void pi_clear_on(struct pi_desc *pi_desc)
562{
563 clear_bit(POSTED_INTR_ON,
564 (unsigned long *)&pi_desc->control);
565}
566
Feng Wuebbfc762015-09-18 22:29:46 +0800567static inline int pi_test_on(struct pi_desc *pi_desc)
568{
569 return test_bit(POSTED_INTR_ON,
570 (unsigned long *)&pi_desc->control);
571}
572
573static inline int pi_test_sn(struct pi_desc *pi_desc)
574{
575 return test_bit(POSTED_INTR_SN,
576 (unsigned long *)&pi_desc->control);
577}
578
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400579struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000580 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300581 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300582 u8 fail;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300583 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200584 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200585 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300586 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400587 int nmsrs;
588 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800589 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400590#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300591 u64 msr_host_kernel_gs_base;
592 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400593#endif
Gleb Natapov2961e8762013-11-25 15:37:13 +0200594 u32 vm_entry_controls_shadow;
595 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200596 u32 secondary_exec_control;
597
Nadav Har'Eld462b812011-05-24 15:26:10 +0300598 /*
599 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
600 * non-nested (L1) guest, it always points to vmcs01. For a nested
601 * guest (L2), it points to a different VMCS.
602 */
603 struct loaded_vmcs vmcs01;
604 struct loaded_vmcs *loaded_vmcs;
605 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300606 struct msr_autoload {
607 unsigned nr;
608 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
609 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
610 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400611 struct {
612 int loaded;
613 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300614#ifdef CONFIG_X86_64
615 u16 ds_sel, es_sel;
616#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200617 int gs_ldt_reload_needed;
618 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000619 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400620 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200621 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300622 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300623 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300624 struct kvm_segment segs[8];
625 } rmode;
626 struct {
627 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300628 struct kvm_save_segment {
629 u16 selector;
630 unsigned long base;
631 u32 limit;
632 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300633 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300634 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800635 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300636 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200637
Andi Kleena0861c02009-06-08 17:37:09 +0800638 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800639
Yang Zhang01e439b2013-04-11 19:25:12 +0800640 /* Posted interrupt descriptor */
641 struct pi_desc pi_desc;
642
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300643 /* Support for a guest hypervisor (nested VMX) */
644 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200645
646 /* Dynamic PLE window. */
647 int ple_window;
648 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800649
650 /* Support for PML */
651#define PML_ENTITY_NUM 512
652 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800653
Yunhong Jiang64672c92016-06-13 14:19:59 -0700654 /* apic deadline value in host tsc */
655 u64 hv_deadline_tsc;
656
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800657 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800658
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800659 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800660
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800661 /*
662 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
663 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
664 * in msr_ia32_feature_control_valid_bits.
665 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800666 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800667 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400668};
669
Avi Kivity2fb92db2011-04-27 19:42:18 +0300670enum segment_cache_field {
671 SEG_FIELD_SEL = 0,
672 SEG_FIELD_BASE = 1,
673 SEG_FIELD_LIMIT = 2,
674 SEG_FIELD_AR = 3,
675
676 SEG_FIELD_NR = 4
677};
678
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400679static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
680{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000681 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400682}
683
Feng Wuefc64402015-09-18 22:29:51 +0800684static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
685{
686 return &(to_vmx(vcpu)->pi_desc);
687}
688
Nadav Har'El22bd0352011-05-25 23:05:57 +0300689#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
690#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
691#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
692 [number##_HIGH] = VMCS12_OFFSET(name)+4
693
Abel Gordon4607c2d2013-04-18 14:35:55 +0300694
Bandan Dasfe2b2012014-04-21 15:20:14 -0400695static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300696 /*
697 * We do NOT shadow fields that are modified when L0
698 * traps and emulates any vmx instruction (e.g. VMPTRLD,
699 * VMXON...) executed by L1.
700 * For example, VM_INSTRUCTION_ERROR is read
701 * by L1 if a vmx instruction fails (part of the error path).
702 * Note the code assumes this logic. If for some reason
703 * we start shadowing these fields then we need to
704 * force a shadow sync when L0 emulates vmx instructions
705 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
706 * by nested_vmx_failValid)
707 */
708 VM_EXIT_REASON,
709 VM_EXIT_INTR_INFO,
710 VM_EXIT_INSTRUCTION_LEN,
711 IDT_VECTORING_INFO_FIELD,
712 IDT_VECTORING_ERROR_CODE,
713 VM_EXIT_INTR_ERROR_CODE,
714 EXIT_QUALIFICATION,
715 GUEST_LINEAR_ADDRESS,
716 GUEST_PHYSICAL_ADDRESS
717};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400718static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300719 ARRAY_SIZE(shadow_read_only_fields);
720
Bandan Dasfe2b2012014-04-21 15:20:14 -0400721static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800722 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300723 GUEST_RIP,
724 GUEST_RSP,
725 GUEST_CR0,
726 GUEST_CR3,
727 GUEST_CR4,
728 GUEST_INTERRUPTIBILITY_INFO,
729 GUEST_RFLAGS,
730 GUEST_CS_SELECTOR,
731 GUEST_CS_AR_BYTES,
732 GUEST_CS_LIMIT,
733 GUEST_CS_BASE,
734 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100735 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300736 CR0_GUEST_HOST_MASK,
737 CR0_READ_SHADOW,
738 CR4_READ_SHADOW,
739 TSC_OFFSET,
740 EXCEPTION_BITMAP,
741 CPU_BASED_VM_EXEC_CONTROL,
742 VM_ENTRY_EXCEPTION_ERROR_CODE,
743 VM_ENTRY_INTR_INFO_FIELD,
744 VM_ENTRY_INSTRUCTION_LEN,
745 VM_ENTRY_EXCEPTION_ERROR_CODE,
746 HOST_FS_BASE,
747 HOST_GS_BASE,
748 HOST_FS_SELECTOR,
749 HOST_GS_SELECTOR
750};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400751static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300752 ARRAY_SIZE(shadow_read_write_fields);
753
Mathias Krause772e0312012-08-30 01:30:19 +0200754static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300755 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800756 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300757 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
758 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
759 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
760 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
761 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
762 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
763 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
764 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800765 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400766 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300767 FIELD(HOST_ES_SELECTOR, host_es_selector),
768 FIELD(HOST_CS_SELECTOR, host_cs_selector),
769 FIELD(HOST_SS_SELECTOR, host_ss_selector),
770 FIELD(HOST_DS_SELECTOR, host_ds_selector),
771 FIELD(HOST_FS_SELECTOR, host_fs_selector),
772 FIELD(HOST_GS_SELECTOR, host_gs_selector),
773 FIELD(HOST_TR_SELECTOR, host_tr_selector),
774 FIELD64(IO_BITMAP_A, io_bitmap_a),
775 FIELD64(IO_BITMAP_B, io_bitmap_b),
776 FIELD64(MSR_BITMAP, msr_bitmap),
777 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
778 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
779 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
780 FIELD64(TSC_OFFSET, tsc_offset),
781 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
782 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800783 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400784 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300785 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800786 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
787 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
788 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
789 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400790 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800791 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300792 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
793 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400794 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300795 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
796 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
797 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
798 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
799 FIELD64(GUEST_PDPTR0, guest_pdptr0),
800 FIELD64(GUEST_PDPTR1, guest_pdptr1),
801 FIELD64(GUEST_PDPTR2, guest_pdptr2),
802 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100803 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300804 FIELD64(HOST_IA32_PAT, host_ia32_pat),
805 FIELD64(HOST_IA32_EFER, host_ia32_efer),
806 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
807 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
808 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
809 FIELD(EXCEPTION_BITMAP, exception_bitmap),
810 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
811 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
812 FIELD(CR3_TARGET_COUNT, cr3_target_count),
813 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
814 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
815 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
816 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
817 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
818 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
819 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
820 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
821 FIELD(TPR_THRESHOLD, tpr_threshold),
822 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
823 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
824 FIELD(VM_EXIT_REASON, vm_exit_reason),
825 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
826 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
827 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
828 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
829 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
830 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
831 FIELD(GUEST_ES_LIMIT, guest_es_limit),
832 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
833 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
834 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
835 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
836 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
837 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
838 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
839 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
840 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
841 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
842 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
843 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
844 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
845 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
846 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
847 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
848 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
849 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
850 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
851 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
852 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100853 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300854 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
855 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
856 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
857 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
858 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
859 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
860 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
861 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
862 FIELD(EXIT_QUALIFICATION, exit_qualification),
863 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
864 FIELD(GUEST_CR0, guest_cr0),
865 FIELD(GUEST_CR3, guest_cr3),
866 FIELD(GUEST_CR4, guest_cr4),
867 FIELD(GUEST_ES_BASE, guest_es_base),
868 FIELD(GUEST_CS_BASE, guest_cs_base),
869 FIELD(GUEST_SS_BASE, guest_ss_base),
870 FIELD(GUEST_DS_BASE, guest_ds_base),
871 FIELD(GUEST_FS_BASE, guest_fs_base),
872 FIELD(GUEST_GS_BASE, guest_gs_base),
873 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
874 FIELD(GUEST_TR_BASE, guest_tr_base),
875 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
876 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
877 FIELD(GUEST_DR7, guest_dr7),
878 FIELD(GUEST_RSP, guest_rsp),
879 FIELD(GUEST_RIP, guest_rip),
880 FIELD(GUEST_RFLAGS, guest_rflags),
881 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
882 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
883 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
884 FIELD(HOST_CR0, host_cr0),
885 FIELD(HOST_CR3, host_cr3),
886 FIELD(HOST_CR4, host_cr4),
887 FIELD(HOST_FS_BASE, host_fs_base),
888 FIELD(HOST_GS_BASE, host_gs_base),
889 FIELD(HOST_TR_BASE, host_tr_base),
890 FIELD(HOST_GDTR_BASE, host_gdtr_base),
891 FIELD(HOST_IDTR_BASE, host_idtr_base),
892 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
893 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
894 FIELD(HOST_RSP, host_rsp),
895 FIELD(HOST_RIP, host_rip),
896};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300897
898static inline short vmcs_field_to_offset(unsigned long field)
899{
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100900 BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX);
901
902 if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) ||
903 vmcs_field_to_offset_table[field] == 0)
904 return -ENOENT;
905
Nadav Har'El22bd0352011-05-25 23:05:57 +0300906 return vmcs_field_to_offset_table[field];
907}
908
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300909static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
910{
David Matlack4f2777b2016-07-13 17:16:37 -0700911 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300912}
913
Peter Feiner995f00a2017-06-30 17:26:32 -0700914static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300915static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -0700916static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800917static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300918static void vmx_set_segment(struct kvm_vcpu *vcpu,
919 struct kvm_segment *var, int seg);
920static void vmx_get_segment(struct kvm_vcpu *vcpu,
921 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200922static bool guest_state_valid(struct kvm_vcpu *vcpu);
923static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +0300924static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +0200925static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
926static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
927static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
928 u16 error_code);
Avi Kivity75880a02007-06-20 11:20:04 +0300929
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930static DEFINE_PER_CPU(struct vmcs *, vmxarea);
931static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300932/*
933 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
934 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
935 */
936static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937
Feng Wubf9f6ac2015-09-18 22:29:55 +0800938/*
939 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
940 * can find which vCPU should be waken up.
941 */
942static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
943static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
944
Radim Krčmář23611332016-09-29 22:41:33 +0200945enum {
946 VMX_IO_BITMAP_A,
947 VMX_IO_BITMAP_B,
948 VMX_MSR_BITMAP_LEGACY,
949 VMX_MSR_BITMAP_LONGMODE,
950 VMX_MSR_BITMAP_LEGACY_X2APIC_APICV,
951 VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV,
952 VMX_MSR_BITMAP_LEGACY_X2APIC,
953 VMX_MSR_BITMAP_LONGMODE_X2APIC,
954 VMX_VMREAD_BITMAP,
955 VMX_VMWRITE_BITMAP,
956 VMX_BITMAP_NR
957};
958
959static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
960
961#define vmx_io_bitmap_a (vmx_bitmap[VMX_IO_BITMAP_A])
962#define vmx_io_bitmap_b (vmx_bitmap[VMX_IO_BITMAP_B])
963#define vmx_msr_bitmap_legacy (vmx_bitmap[VMX_MSR_BITMAP_LEGACY])
964#define vmx_msr_bitmap_longmode (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE])
965#define vmx_msr_bitmap_legacy_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC_APICV])
966#define vmx_msr_bitmap_longmode_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV])
967#define vmx_msr_bitmap_legacy_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC])
968#define vmx_msr_bitmap_longmode_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC])
969#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
970#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +0300971
Avi Kivity110312c2010-12-21 12:54:20 +0200972static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200973static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200974
Sheng Yang2384d2b2008-01-17 15:14:33 +0800975static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
976static DEFINE_SPINLOCK(vmx_vpid_lock);
977
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300978static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 int size;
980 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +0300981 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300983 u32 pin_based_exec_ctrl;
984 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800985 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300986 u32 vmexit_ctrl;
987 u32 vmentry_ctrl;
988} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989
Hannes Ederefff9e52008-11-28 17:02:06 +0100990static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800991 u32 ept;
992 u32 vpid;
993} vmx_capability;
994
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995#define VMX_SEGMENT_FIELD(seg) \
996 [VCPU_SREG_##seg] = { \
997 .selector = GUEST_##seg##_SELECTOR, \
998 .base = GUEST_##seg##_BASE, \
999 .limit = GUEST_##seg##_LIMIT, \
1000 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1001 }
1002
Mathias Krause772e0312012-08-30 01:30:19 +02001003static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004 unsigned selector;
1005 unsigned base;
1006 unsigned limit;
1007 unsigned ar_bytes;
1008} kvm_vmx_segment_fields[] = {
1009 VMX_SEGMENT_FIELD(CS),
1010 VMX_SEGMENT_FIELD(DS),
1011 VMX_SEGMENT_FIELD(ES),
1012 VMX_SEGMENT_FIELD(FS),
1013 VMX_SEGMENT_FIELD(GS),
1014 VMX_SEGMENT_FIELD(SS),
1015 VMX_SEGMENT_FIELD(TR),
1016 VMX_SEGMENT_FIELD(LDTR),
1017};
1018
Avi Kivity26bb0982009-09-07 11:14:12 +03001019static u64 host_efer;
1020
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001021static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1022
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001023/*
Brian Gerst8c065852010-07-17 09:03:26 -04001024 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001025 * away by decrementing the array size.
1026 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001028#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001029 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001031 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033
Jan Kiszka5bb16012016-02-09 20:14:21 +01001034static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035{
1036 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1037 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001038 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1039}
1040
Jan Kiszka6f054852016-02-09 20:15:18 +01001041static inline bool is_debug(u32 intr_info)
1042{
1043 return is_exception_n(intr_info, DB_VECTOR);
1044}
1045
1046static inline bool is_breakpoint(u32 intr_info)
1047{
1048 return is_exception_n(intr_info, BP_VECTOR);
1049}
1050
Jan Kiszka5bb16012016-02-09 20:14:21 +01001051static inline bool is_page_fault(u32 intr_info)
1052{
1053 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001054}
1055
Gui Jianfeng31299942010-03-15 17:29:09 +08001056static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001057{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001058 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001059}
1060
Gui Jianfeng31299942010-03-15 17:29:09 +08001061static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001062{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001063 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001064}
1065
Gui Jianfeng31299942010-03-15 17:29:09 +08001066static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067{
1068 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1069 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1070}
1071
Gui Jianfeng31299942010-03-15 17:29:09 +08001072static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001073{
1074 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1075 INTR_INFO_VALID_MASK)) ==
1076 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1077}
1078
Gui Jianfeng31299942010-03-15 17:29:09 +08001079static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001080{
Sheng Yang04547152009-04-01 15:52:31 +08001081 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001082}
1083
Gui Jianfeng31299942010-03-15 17:29:09 +08001084static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001085{
Sheng Yang04547152009-04-01 15:52:31 +08001086 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001087}
1088
Paolo Bonzini35754c92015-07-29 12:05:37 +02001089static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001090{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001091 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001092}
1093
Gui Jianfeng31299942010-03-15 17:29:09 +08001094static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001095{
Sheng Yang04547152009-04-01 15:52:31 +08001096 return vmcs_config.cpu_based_exec_ctrl &
1097 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001098}
1099
Avi Kivity774ead32007-12-26 13:57:04 +02001100static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001101{
Sheng Yang04547152009-04-01 15:52:31 +08001102 return vmcs_config.cpu_based_2nd_exec_ctrl &
1103 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1104}
1105
Yang Zhang8d146952013-01-25 10:18:50 +08001106static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1107{
1108 return vmcs_config.cpu_based_2nd_exec_ctrl &
1109 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1110}
1111
Yang Zhang83d4c282013-01-25 10:18:49 +08001112static inline bool cpu_has_vmx_apic_register_virt(void)
1113{
1114 return vmcs_config.cpu_based_2nd_exec_ctrl &
1115 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1116}
1117
Yang Zhangc7c9c562013-01-25 10:18:51 +08001118static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1119{
1120 return vmcs_config.cpu_based_2nd_exec_ctrl &
1121 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1122}
1123
Yunhong Jiang64672c92016-06-13 14:19:59 -07001124/*
1125 * Comment's format: document - errata name - stepping - processor name.
1126 * Refer from
1127 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1128 */
1129static u32 vmx_preemption_cpu_tfms[] = {
1130/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
11310x000206E6,
1132/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1133/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1134/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
11350x00020652,
1136/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
11370x00020655,
1138/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1139/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1140/*
1141 * 320767.pdf - AAP86 - B1 -
1142 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1143 */
11440x000106E5,
1145/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
11460x000106A0,
1147/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
11480x000106A1,
1149/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
11500x000106A4,
1151 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1152 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1153 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
11540x000106A5,
1155};
1156
1157static inline bool cpu_has_broken_vmx_preemption_timer(void)
1158{
1159 u32 eax = cpuid_eax(0x00000001), i;
1160
1161 /* Clear the reserved bits */
1162 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001163 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001164 if (eax == vmx_preemption_cpu_tfms[i])
1165 return true;
1166
1167 return false;
1168}
1169
1170static inline bool cpu_has_vmx_preemption_timer(void)
1171{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001172 return vmcs_config.pin_based_exec_ctrl &
1173 PIN_BASED_VMX_PREEMPTION_TIMER;
1174}
1175
Yang Zhang01e439b2013-04-11 19:25:12 +08001176static inline bool cpu_has_vmx_posted_intr(void)
1177{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001178 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1179 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001180}
1181
1182static inline bool cpu_has_vmx_apicv(void)
1183{
1184 return cpu_has_vmx_apic_register_virt() &&
1185 cpu_has_vmx_virtual_intr_delivery() &&
1186 cpu_has_vmx_posted_intr();
1187}
1188
Sheng Yang04547152009-04-01 15:52:31 +08001189static inline bool cpu_has_vmx_flexpriority(void)
1190{
1191 return cpu_has_vmx_tpr_shadow() &&
1192 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001193}
1194
Marcelo Tosattie7997942009-06-11 12:07:40 -03001195static inline bool cpu_has_vmx_ept_execute_only(void)
1196{
Gui Jianfeng31299942010-03-15 17:29:09 +08001197 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001198}
1199
Marcelo Tosattie7997942009-06-11 12:07:40 -03001200static inline bool cpu_has_vmx_ept_2m_page(void)
1201{
Gui Jianfeng31299942010-03-15 17:29:09 +08001202 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001203}
1204
Sheng Yang878403b2010-01-05 19:02:29 +08001205static inline bool cpu_has_vmx_ept_1g_page(void)
1206{
Gui Jianfeng31299942010-03-15 17:29:09 +08001207 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001208}
1209
Sheng Yang4bc9b982010-06-02 14:05:24 +08001210static inline bool cpu_has_vmx_ept_4levels(void)
1211{
1212 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1213}
1214
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001215static inline bool cpu_has_vmx_ept_mt_wb(void)
1216{
1217 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1218}
1219
Yu Zhang855feb62017-08-24 20:27:55 +08001220static inline bool cpu_has_vmx_ept_5levels(void)
1221{
1222 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1223}
1224
Xudong Hao83c3a332012-05-28 19:33:35 +08001225static inline bool cpu_has_vmx_ept_ad_bits(void)
1226{
1227 return vmx_capability.ept & VMX_EPT_AD_BIT;
1228}
1229
Gui Jianfeng31299942010-03-15 17:29:09 +08001230static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001231{
Gui Jianfeng31299942010-03-15 17:29:09 +08001232 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001233}
1234
Gui Jianfeng31299942010-03-15 17:29:09 +08001235static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001236{
Gui Jianfeng31299942010-03-15 17:29:09 +08001237 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001238}
1239
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001240static inline bool cpu_has_vmx_invvpid_single(void)
1241{
1242 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1243}
1244
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001245static inline bool cpu_has_vmx_invvpid_global(void)
1246{
1247 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1248}
1249
Wanpeng Li08d839c2017-03-23 05:30:08 -07001250static inline bool cpu_has_vmx_invvpid(void)
1251{
1252 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1253}
1254
Gui Jianfeng31299942010-03-15 17:29:09 +08001255static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001256{
Sheng Yang04547152009-04-01 15:52:31 +08001257 return vmcs_config.cpu_based_2nd_exec_ctrl &
1258 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001259}
1260
Gui Jianfeng31299942010-03-15 17:29:09 +08001261static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001262{
1263 return vmcs_config.cpu_based_2nd_exec_ctrl &
1264 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1265}
1266
Gui Jianfeng31299942010-03-15 17:29:09 +08001267static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001268{
1269 return vmcs_config.cpu_based_2nd_exec_ctrl &
1270 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1271}
1272
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001273static inline bool cpu_has_vmx_basic_inout(void)
1274{
1275 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1276}
1277
Paolo Bonzini35754c92015-07-29 12:05:37 +02001278static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001279{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001280 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001281}
1282
Gui Jianfeng31299942010-03-15 17:29:09 +08001283static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001284{
Sheng Yang04547152009-04-01 15:52:31 +08001285 return vmcs_config.cpu_based_2nd_exec_ctrl &
1286 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001287}
1288
Gui Jianfeng31299942010-03-15 17:29:09 +08001289static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001290{
1291 return vmcs_config.cpu_based_2nd_exec_ctrl &
1292 SECONDARY_EXEC_RDTSCP;
1293}
1294
Mao, Junjiead756a12012-07-02 01:18:48 +00001295static inline bool cpu_has_vmx_invpcid(void)
1296{
1297 return vmcs_config.cpu_based_2nd_exec_ctrl &
1298 SECONDARY_EXEC_ENABLE_INVPCID;
1299}
1300
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001301static inline bool cpu_has_virtual_nmis(void)
1302{
1303 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1304}
1305
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001306static inline bool cpu_has_vmx_wbinvd_exit(void)
1307{
1308 return vmcs_config.cpu_based_2nd_exec_ctrl &
1309 SECONDARY_EXEC_WBINVD_EXITING;
1310}
1311
Abel Gordonabc4fc52013-04-18 14:35:25 +03001312static inline bool cpu_has_vmx_shadow_vmcs(void)
1313{
1314 u64 vmx_msr;
1315 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1316 /* check if the cpu supports writing r/o exit information fields */
1317 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1318 return false;
1319
1320 return vmcs_config.cpu_based_2nd_exec_ctrl &
1321 SECONDARY_EXEC_SHADOW_VMCS;
1322}
1323
Kai Huang843e4332015-01-28 10:54:28 +08001324static inline bool cpu_has_vmx_pml(void)
1325{
1326 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1327}
1328
Haozhong Zhang64903d62015-10-20 15:39:09 +08001329static inline bool cpu_has_vmx_tsc_scaling(void)
1330{
1331 return vmcs_config.cpu_based_2nd_exec_ctrl &
1332 SECONDARY_EXEC_TSC_SCALING;
1333}
1334
Bandan Das2a499e42017-08-03 15:54:41 -04001335static inline bool cpu_has_vmx_vmfunc(void)
1336{
1337 return vmcs_config.cpu_based_2nd_exec_ctrl &
1338 SECONDARY_EXEC_ENABLE_VMFUNC;
1339}
1340
Sheng Yang04547152009-04-01 15:52:31 +08001341static inline bool report_flexpriority(void)
1342{
1343 return flexpriority_enabled;
1344}
1345
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001346static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1347{
1348 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.nested_vmx_misc_low);
1349}
1350
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001351static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1352{
1353 return vmcs12->cpu_based_vm_exec_control & bit;
1354}
1355
1356static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1357{
1358 return (vmcs12->cpu_based_vm_exec_control &
1359 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1360 (vmcs12->secondary_vm_exec_control & bit);
1361}
1362
Jan Kiszkaf41245002014-03-07 20:03:13 +01001363static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1364{
1365 return vmcs12->pin_based_vm_exec_control &
1366 PIN_BASED_VMX_PREEMPTION_TIMER;
1367}
1368
Nadav Har'El155a97a2013-08-05 11:07:16 +03001369static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1370{
1371 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1372}
1373
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001374static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1375{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001376 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001377}
1378
Bandan Dasc5f983f2017-05-05 15:25:14 -04001379static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1380{
1381 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1382}
1383
Wincy Vanf2b93282015-02-03 23:56:03 +08001384static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1385{
1386 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1387}
1388
Wanpeng Li5c614b32015-10-13 09:18:36 -07001389static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1390{
1391 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1392}
1393
Wincy Van82f0dd42015-02-03 23:57:18 +08001394static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1395{
1396 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1397}
1398
Wincy Van608406e2015-02-03 23:57:51 +08001399static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1400{
1401 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1402}
1403
Wincy Van705699a2015-02-03 23:58:17 +08001404static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1405{
1406 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1407}
1408
Bandan Das27c42a12017-08-03 15:54:42 -04001409static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1410{
1411 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1412}
1413
Bandan Das41ab9372017-08-03 15:54:43 -04001414static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1415{
1416 return nested_cpu_has_vmfunc(vmcs12) &&
1417 (vmcs12->vm_function_control &
1418 VMX_VMFUNC_EPTP_SWITCHING);
1419}
1420
Jim Mattsonef85b672016-12-12 11:01:37 -08001421static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001422{
1423 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001424 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001425}
1426
Jan Kiszka533558b2014-01-04 18:47:20 +01001427static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1428 u32 exit_intr_info,
1429 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001430static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1431 struct vmcs12 *vmcs12,
1432 u32 reason, unsigned long qualification);
1433
Rusty Russell8b9cf982007-07-30 16:31:43 +10001434static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001435{
1436 int i;
1437
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001438 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001439 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001440 return i;
1441 return -1;
1442}
1443
Sheng Yang2384d2b2008-01-17 15:14:33 +08001444static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1445{
1446 struct {
1447 u64 vpid : 16;
1448 u64 rsvd : 48;
1449 u64 gva;
1450 } operand = { vpid, 0, gva };
1451
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001452 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001453 /* CF==1 or ZF==1 --> rc = -1 */
1454 "; ja 1f ; ud2 ; 1:"
1455 : : "a"(&operand), "c"(ext) : "cc", "memory");
1456}
1457
Sheng Yang14394422008-04-28 12:24:45 +08001458static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1459{
1460 struct {
1461 u64 eptp, gpa;
1462 } operand = {eptp, gpa};
1463
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001464 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001465 /* CF==1 or ZF==1 --> rc = -1 */
1466 "; ja 1f ; ud2 ; 1:\n"
1467 : : "a" (&operand), "c" (ext) : "cc", "memory");
1468}
1469
Avi Kivity26bb0982009-09-07 11:14:12 +03001470static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001471{
1472 int i;
1473
Rusty Russell8b9cf982007-07-30 16:31:43 +10001474 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001475 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001476 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001477 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001478}
1479
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480static void vmcs_clear(struct vmcs *vmcs)
1481{
1482 u64 phys_addr = __pa(vmcs);
1483 u8 error;
1484
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001485 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001486 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487 : "cc", "memory");
1488 if (error)
1489 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1490 vmcs, phys_addr);
1491}
1492
Nadav Har'Eld462b812011-05-24 15:26:10 +03001493static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1494{
1495 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001496 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1497 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001498 loaded_vmcs->cpu = -1;
1499 loaded_vmcs->launched = 0;
1500}
1501
Dongxiao Xu7725b892010-05-11 18:29:38 +08001502static void vmcs_load(struct vmcs *vmcs)
1503{
1504 u64 phys_addr = __pa(vmcs);
1505 u8 error;
1506
1507 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001508 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001509 : "cc", "memory");
1510 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001511 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001512 vmcs, phys_addr);
1513}
1514
Dave Young2965faa2015-09-09 15:38:55 -07001515#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001516/*
1517 * This bitmap is used to indicate whether the vmclear
1518 * operation is enabled on all cpus. All disabled by
1519 * default.
1520 */
1521static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1522
1523static inline void crash_enable_local_vmclear(int cpu)
1524{
1525 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1526}
1527
1528static inline void crash_disable_local_vmclear(int cpu)
1529{
1530 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1531}
1532
1533static inline int crash_local_vmclear_enabled(int cpu)
1534{
1535 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1536}
1537
1538static void crash_vmclear_local_loaded_vmcss(void)
1539{
1540 int cpu = raw_smp_processor_id();
1541 struct loaded_vmcs *v;
1542
1543 if (!crash_local_vmclear_enabled(cpu))
1544 return;
1545
1546 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1547 loaded_vmcss_on_cpu_link)
1548 vmcs_clear(v->vmcs);
1549}
1550#else
1551static inline void crash_enable_local_vmclear(int cpu) { }
1552static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001553#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001554
Nadav Har'Eld462b812011-05-24 15:26:10 +03001555static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001556{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001557 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001558 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001559
Nadav Har'Eld462b812011-05-24 15:26:10 +03001560 if (loaded_vmcs->cpu != cpu)
1561 return; /* vcpu migration can race with cpu offline */
1562 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001563 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001564 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001565 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001566
1567 /*
1568 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1569 * is before setting loaded_vmcs->vcpu to -1 which is done in
1570 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1571 * then adds the vmcs into percpu list before it is deleted.
1572 */
1573 smp_wmb();
1574
Nadav Har'Eld462b812011-05-24 15:26:10 +03001575 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001576 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001577}
1578
Nadav Har'Eld462b812011-05-24 15:26:10 +03001579static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001580{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001581 int cpu = loaded_vmcs->cpu;
1582
1583 if (cpu != -1)
1584 smp_call_function_single(cpu,
1585 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001586}
1587
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001588static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001589{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001590 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001591 return;
1592
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001593 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001594 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001595}
1596
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001597static inline void vpid_sync_vcpu_global(void)
1598{
1599 if (cpu_has_vmx_invvpid_global())
1600 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1601}
1602
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001603static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001604{
1605 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001606 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001607 else
1608 vpid_sync_vcpu_global();
1609}
1610
Sheng Yang14394422008-04-28 12:24:45 +08001611static inline void ept_sync_global(void)
1612{
David Hildenbrandf5f51582017-08-24 20:51:30 +02001613 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08001614}
1615
1616static inline void ept_sync_context(u64 eptp)
1617{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02001618 if (cpu_has_vmx_invept_context())
1619 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1620 else
1621 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08001622}
1623
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001624static __always_inline void vmcs_check16(unsigned long field)
1625{
1626 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1627 "16-bit accessor invalid for 64-bit field");
1628 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1629 "16-bit accessor invalid for 64-bit high field");
1630 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1631 "16-bit accessor invalid for 32-bit high field");
1632 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1633 "16-bit accessor invalid for natural width field");
1634}
1635
1636static __always_inline void vmcs_check32(unsigned long field)
1637{
1638 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1639 "32-bit accessor invalid for 16-bit field");
1640 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1641 "32-bit accessor invalid for natural width field");
1642}
1643
1644static __always_inline void vmcs_check64(unsigned long field)
1645{
1646 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1647 "64-bit accessor invalid for 16-bit field");
1648 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1649 "64-bit accessor invalid for 64-bit high field");
1650 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1651 "64-bit accessor invalid for 32-bit field");
1652 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1653 "64-bit accessor invalid for natural width field");
1654}
1655
1656static __always_inline void vmcs_checkl(unsigned long field)
1657{
1658 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1659 "Natural width accessor invalid for 16-bit field");
1660 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1661 "Natural width accessor invalid for 64-bit field");
1662 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1663 "Natural width accessor invalid for 64-bit high field");
1664 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1665 "Natural width accessor invalid for 32-bit field");
1666}
1667
1668static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669{
Avi Kivity5e520e62011-05-15 10:13:12 -04001670 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001671
Avi Kivity5e520e62011-05-15 10:13:12 -04001672 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1673 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674 return value;
1675}
1676
Avi Kivity96304212011-05-15 10:13:13 -04001677static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001679 vmcs_check16(field);
1680 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001681}
1682
Avi Kivity96304212011-05-15 10:13:13 -04001683static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001684{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001685 vmcs_check32(field);
1686 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001687}
1688
Avi Kivity96304212011-05-15 10:13:13 -04001689static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001691 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001692#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001693 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001694#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001695 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696#endif
1697}
1698
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001699static __always_inline unsigned long vmcs_readl(unsigned long field)
1700{
1701 vmcs_checkl(field);
1702 return __vmcs_readl(field);
1703}
1704
Avi Kivitye52de1b2007-01-05 16:36:56 -08001705static noinline void vmwrite_error(unsigned long field, unsigned long value)
1706{
1707 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1708 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1709 dump_stack();
1710}
1711
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001712static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001713{
1714 u8 error;
1715
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001716 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001717 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001718 if (unlikely(error))
1719 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720}
1721
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001722static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001723{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001724 vmcs_check16(field);
1725 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726}
1727
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001728static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001730 vmcs_check32(field);
1731 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001732}
1733
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001734static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001735{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001736 vmcs_check64(field);
1737 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001738#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001739 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001740 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001741#endif
1742}
1743
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001744static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001745{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001746 vmcs_checkl(field);
1747 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001748}
1749
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001750static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001751{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001752 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1753 "vmcs_clear_bits does not support 64-bit fields");
1754 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1755}
1756
1757static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1758{
1759 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1760 "vmcs_set_bits does not support 64-bit fields");
1761 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001762}
1763
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001764static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1765{
1766 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1767}
1768
Gleb Natapov2961e8762013-11-25 15:37:13 +02001769static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1770{
1771 vmcs_write32(VM_ENTRY_CONTROLS, val);
1772 vmx->vm_entry_controls_shadow = val;
1773}
1774
1775static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1776{
1777 if (vmx->vm_entry_controls_shadow != val)
1778 vm_entry_controls_init(vmx, val);
1779}
1780
1781static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1782{
1783 return vmx->vm_entry_controls_shadow;
1784}
1785
1786
1787static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1788{
1789 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1790}
1791
1792static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1793{
1794 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1795}
1796
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001797static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1798{
1799 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1800}
1801
Gleb Natapov2961e8762013-11-25 15:37:13 +02001802static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1803{
1804 vmcs_write32(VM_EXIT_CONTROLS, val);
1805 vmx->vm_exit_controls_shadow = val;
1806}
1807
1808static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1809{
1810 if (vmx->vm_exit_controls_shadow != val)
1811 vm_exit_controls_init(vmx, val);
1812}
1813
1814static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1815{
1816 return vmx->vm_exit_controls_shadow;
1817}
1818
1819
1820static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1821{
1822 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1823}
1824
1825static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1826{
1827 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1828}
1829
Avi Kivity2fb92db2011-04-27 19:42:18 +03001830static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1831{
1832 vmx->segment_cache.bitmask = 0;
1833}
1834
1835static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1836 unsigned field)
1837{
1838 bool ret;
1839 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1840
1841 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1842 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1843 vmx->segment_cache.bitmask = 0;
1844 }
1845 ret = vmx->segment_cache.bitmask & mask;
1846 vmx->segment_cache.bitmask |= mask;
1847 return ret;
1848}
1849
1850static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1851{
1852 u16 *p = &vmx->segment_cache.seg[seg].selector;
1853
1854 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1855 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1856 return *p;
1857}
1858
1859static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1860{
1861 ulong *p = &vmx->segment_cache.seg[seg].base;
1862
1863 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1864 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1865 return *p;
1866}
1867
1868static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1869{
1870 u32 *p = &vmx->segment_cache.seg[seg].limit;
1871
1872 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1873 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1874 return *p;
1875}
1876
1877static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1878{
1879 u32 *p = &vmx->segment_cache.seg[seg].ar;
1880
1881 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1882 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1883 return *p;
1884}
1885
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001886static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1887{
1888 u32 eb;
1889
Liran Alonac9b3052017-11-06 16:15:10 +02001890 eb = (1u << PF_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08001891 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001892 if ((vcpu->guest_debug &
1893 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1894 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1895 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001896 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001897 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001898 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001899 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001900
1901 /* When we are running a nested L2 guest and L1 specified for it a
1902 * certain exception bitmap, we must trap the same exceptions and pass
1903 * them to L1. When running L2, we will only handle the exceptions
1904 * specified above if L1 did not want them.
1905 */
1906 if (is_guest_mode(vcpu))
1907 eb |= get_vmcs12(vcpu)->exception_bitmap;
Liran Alonac9b3052017-11-06 16:15:10 +02001908 else
1909 eb |= 1u << UD_VECTOR;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001910
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001911 vmcs_write32(EXCEPTION_BITMAP, eb);
1912}
1913
Gleb Natapov2961e8762013-11-25 15:37:13 +02001914static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1915 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001916{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001917 vm_entry_controls_clearbit(vmx, entry);
1918 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001919}
1920
Avi Kivity61d2ef22010-04-28 16:40:38 +03001921static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1922{
1923 unsigned i;
1924 struct msr_autoload *m = &vmx->msr_autoload;
1925
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001926 switch (msr) {
1927 case MSR_EFER:
1928 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001929 clear_atomic_switch_msr_special(vmx,
1930 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001931 VM_EXIT_LOAD_IA32_EFER);
1932 return;
1933 }
1934 break;
1935 case MSR_CORE_PERF_GLOBAL_CTRL:
1936 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001937 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001938 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1939 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1940 return;
1941 }
1942 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001943 }
1944
Avi Kivity61d2ef22010-04-28 16:40:38 +03001945 for (i = 0; i < m->nr; ++i)
1946 if (m->guest[i].index == msr)
1947 break;
1948
1949 if (i == m->nr)
1950 return;
1951 --m->nr;
1952 m->guest[i] = m->guest[m->nr];
1953 m->host[i] = m->host[m->nr];
1954 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1955 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1956}
1957
Gleb Natapov2961e8762013-11-25 15:37:13 +02001958static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1959 unsigned long entry, unsigned long exit,
1960 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1961 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001962{
1963 vmcs_write64(guest_val_vmcs, guest_val);
1964 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001965 vm_entry_controls_setbit(vmx, entry);
1966 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001967}
1968
Avi Kivity61d2ef22010-04-28 16:40:38 +03001969static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1970 u64 guest_val, u64 host_val)
1971{
1972 unsigned i;
1973 struct msr_autoload *m = &vmx->msr_autoload;
1974
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001975 switch (msr) {
1976 case MSR_EFER:
1977 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001978 add_atomic_switch_msr_special(vmx,
1979 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001980 VM_EXIT_LOAD_IA32_EFER,
1981 GUEST_IA32_EFER,
1982 HOST_IA32_EFER,
1983 guest_val, host_val);
1984 return;
1985 }
1986 break;
1987 case MSR_CORE_PERF_GLOBAL_CTRL:
1988 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001989 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001990 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1991 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1992 GUEST_IA32_PERF_GLOBAL_CTRL,
1993 HOST_IA32_PERF_GLOBAL_CTRL,
1994 guest_val, host_val);
1995 return;
1996 }
1997 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01001998 case MSR_IA32_PEBS_ENABLE:
1999 /* PEBS needs a quiescent period after being disabled (to write
2000 * a record). Disabling PEBS through VMX MSR swapping doesn't
2001 * provide that period, so a CPU could write host's record into
2002 * guest's memory.
2003 */
2004 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002005 }
2006
Avi Kivity61d2ef22010-04-28 16:40:38 +03002007 for (i = 0; i < m->nr; ++i)
2008 if (m->guest[i].index == msr)
2009 break;
2010
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002011 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002012 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002013 "Can't add msr %x\n", msr);
2014 return;
2015 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002016 ++m->nr;
2017 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2018 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2019 }
2020
2021 m->guest[i].index = msr;
2022 m->guest[i].value = guest_val;
2023 m->host[i].index = msr;
2024 m->host[i].value = host_val;
2025}
2026
Avi Kivity92c0d902009-10-29 11:00:16 +02002027static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002028{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002029 u64 guest_efer = vmx->vcpu.arch.efer;
2030 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002031
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002032 if (!enable_ept) {
2033 /*
2034 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2035 * host CPUID is more efficient than testing guest CPUID
2036 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2037 */
2038 if (boot_cpu_has(X86_FEATURE_SMEP))
2039 guest_efer |= EFER_NX;
2040 else if (!(guest_efer & EFER_NX))
2041 ignore_bits |= EFER_NX;
2042 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002043
Avi Kivity51c6cf62007-08-29 03:48:05 +03002044 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002045 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002046 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002047 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002048#ifdef CONFIG_X86_64
2049 ignore_bits |= EFER_LMA | EFER_LME;
2050 /* SCE is meaningful only in long mode on Intel */
2051 if (guest_efer & EFER_LMA)
2052 ignore_bits &= ~(u64)EFER_SCE;
2053#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002054
2055 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002056
2057 /*
2058 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2059 * On CPUs that support "load IA32_EFER", always switch EFER
2060 * atomically, since it's faster than switching it manually.
2061 */
2062 if (cpu_has_load_ia32_efer ||
2063 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002064 if (!(guest_efer & EFER_LMA))
2065 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002066 if (guest_efer != host_efer)
2067 add_atomic_switch_msr(vmx, MSR_EFER,
2068 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002069 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002070 } else {
2071 guest_efer &= ~ignore_bits;
2072 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002073
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002074 vmx->guest_msrs[efer_offset].data = guest_efer;
2075 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2076
2077 return true;
2078 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002079}
2080
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002081#ifdef CONFIG_X86_32
2082/*
2083 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2084 * VMCS rather than the segment table. KVM uses this helper to figure
2085 * out the current bases to poke them into the VMCS before entry.
2086 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002087static unsigned long segment_base(u16 selector)
2088{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002089 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002090 unsigned long v;
2091
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002092 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002093 return 0;
2094
Thomas Garnier45fc8752017-03-14 10:05:08 -07002095 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002096
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002097 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002098 u16 ldt_selector = kvm_read_ldt();
2099
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002100 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002101 return 0;
2102
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002103 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002104 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002105 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002106 return v;
2107}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002108#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002109
Avi Kivity04d2cc72007-09-10 18:10:54 +03002110static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002111{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002112 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002113 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002114
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002115 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002116 return;
2117
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002118 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002119 /*
2120 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2121 * allow segment selectors with cpl > 0 or ti == 1.
2122 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002123 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002124 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002125 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002126 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002127 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002128 vmx->host_state.fs_reload_needed = 0;
2129 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002130 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002131 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002132 }
Avi Kivity9581d442010-10-19 16:46:55 +02002133 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002134 if (!(vmx->host_state.gs_sel & 7))
2135 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002136 else {
2137 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002138 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002139 }
2140
2141#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002142 savesegment(ds, vmx->host_state.ds_sel);
2143 savesegment(es, vmx->host_state.es_sel);
2144#endif
2145
2146#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002147 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2148 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2149#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002150 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2151 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002152#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002153
2154#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002155 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2156 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002157 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002158#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002159 if (boot_cpu_has(X86_FEATURE_MPX))
2160 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002161 for (i = 0; i < vmx->save_nmsrs; ++i)
2162 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002163 vmx->guest_msrs[i].data,
2164 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002165}
2166
Avi Kivitya9b21b62008-06-24 11:48:49 +03002167static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002168{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002169 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002170 return;
2171
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002172 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002173 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002174#ifdef CONFIG_X86_64
2175 if (is_long_mode(&vmx->vcpu))
2176 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2177#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002178 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002179 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002180#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002181 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002182#else
2183 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002184#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002185 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002186 if (vmx->host_state.fs_reload_needed)
2187 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002188#ifdef CONFIG_X86_64
2189 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2190 loadsegment(ds, vmx->host_state.ds_sel);
2191 loadsegment(es, vmx->host_state.es_sel);
2192 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002193#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002194 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002195#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002196 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002197#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002198 if (vmx->host_state.msr_host_bndcfgs)
2199 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002200 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002201}
2202
Avi Kivitya9b21b62008-06-24 11:48:49 +03002203static void vmx_load_host_state(struct vcpu_vmx *vmx)
2204{
2205 preempt_disable();
2206 __vmx_load_host_state(vmx);
2207 preempt_enable();
2208}
2209
Feng Wu28b835d2015-09-18 22:29:54 +08002210static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2211{
2212 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2213 struct pi_desc old, new;
2214 unsigned int dest;
2215
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002216 /*
2217 * In case of hot-plug or hot-unplug, we may have to undo
2218 * vmx_vcpu_pi_put even if there is no assigned device. And we
2219 * always keep PI.NDST up to date for simplicity: it makes the
2220 * code easier, and CPU migration is not a fast path.
2221 */
2222 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002223 return;
2224
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002225 /*
2226 * First handle the simple case where no cmpxchg is necessary; just
2227 * allow posting non-urgent interrupts.
2228 *
2229 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2230 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2231 * expects the VCPU to be on the blocked_vcpu_list that matches
2232 * PI.NDST.
2233 */
2234 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2235 vcpu->cpu == cpu) {
2236 pi_clear_sn(pi_desc);
2237 return;
2238 }
2239
2240 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002241 do {
2242 old.control = new.control = pi_desc->control;
2243
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002244 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002245
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002246 if (x2apic_enabled())
2247 new.ndst = dest;
2248 else
2249 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002250
Feng Wu28b835d2015-09-18 22:29:54 +08002251 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002252 } while (cmpxchg64(&pi_desc->control, old.control,
2253 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002254}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002255
Peter Feinerc95ba922016-08-17 09:36:47 -07002256static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2257{
2258 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2259 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2260}
2261
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262/*
2263 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2264 * vcpu mutex is already taken.
2265 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002266static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002268 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002269 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002271 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002272 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002273 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002274 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002275
2276 /*
2277 * Read loaded_vmcs->cpu should be before fetching
2278 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2279 * See the comments in __loaded_vmcs_clear().
2280 */
2281 smp_rmb();
2282
Nadav Har'Eld462b812011-05-24 15:26:10 +03002283 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2284 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002285 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002286 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002287 }
2288
2289 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2290 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2291 vmcs_load(vmx->loaded_vmcs->vmcs);
2292 }
2293
2294 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002295 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002296 unsigned long sysenter_esp;
2297
2298 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002299
Avi Kivity6aa8b732006-12-10 02:21:36 -08002300 /*
2301 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002302 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002303 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002304 vmcs_writel(HOST_TR_BASE,
2305 (unsigned long)this_cpu_ptr(&cpu_tss));
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002306 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002307
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002308 /*
2309 * VM exits change the host TR limit to 0x67 after a VM
2310 * exit. This is okay, since 0x67 covers everything except
2311 * the IO bitmap and have have code to handle the IO bitmap
2312 * being lost after a VM exit.
2313 */
2314 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2315
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2317 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002318
Nadav Har'Eld462b812011-05-24 15:26:10 +03002319 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002320 }
Feng Wu28b835d2015-09-18 22:29:54 +08002321
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002322 /* Setup TSC multiplier */
2323 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002324 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2325 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002326
Feng Wu28b835d2015-09-18 22:29:54 +08002327 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002328 vmx->host_pkru = read_pkru();
Feng Wu28b835d2015-09-18 22:29:54 +08002329}
2330
2331static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2332{
2333 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2334
2335 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002336 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2337 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002338 return;
2339
2340 /* Set SN when the vCPU is preempted */
2341 if (vcpu->preempted)
2342 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343}
2344
2345static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2346{
Feng Wu28b835d2015-09-18 22:29:54 +08002347 vmx_vcpu_pi_put(vcpu);
2348
Avi Kivitya9b21b62008-06-24 11:48:49 +03002349 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350}
2351
Wanpeng Lif244dee2017-07-20 01:11:54 -07002352static bool emulation_required(struct kvm_vcpu *vcpu)
2353{
2354 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2355}
2356
Avi Kivityedcafe32009-12-30 18:07:40 +02002357static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2358
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002359/*
2360 * Return the cr0 value that a nested guest would read. This is a combination
2361 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2362 * its hypervisor (cr0_read_shadow).
2363 */
2364static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2365{
2366 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2367 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2368}
2369static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2370{
2371 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2372 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2373}
2374
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2376{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002377 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002378
Avi Kivity6de12732011-03-07 12:51:22 +02002379 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2380 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2381 rflags = vmcs_readl(GUEST_RFLAGS);
2382 if (to_vmx(vcpu)->rmode.vm86_active) {
2383 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2384 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2385 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2386 }
2387 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002388 }
Avi Kivity6de12732011-03-07 12:51:22 +02002389 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390}
2391
2392static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2393{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002394 unsigned long old_rflags = vmx_get_rflags(vcpu);
2395
Avi Kivity6de12732011-03-07 12:51:22 +02002396 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2397 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002398 if (to_vmx(vcpu)->rmode.vm86_active) {
2399 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002400 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002401 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002403
2404 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2405 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002406}
2407
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002408static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002409{
2410 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2411 int ret = 0;
2412
2413 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002414 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002415 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002416 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002417
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002418 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002419}
2420
2421static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2422{
2423 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2424 u32 interruptibility = interruptibility_old;
2425
2426 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2427
Jan Kiszka48005f62010-02-19 19:38:07 +01002428 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002429 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002430 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002431 interruptibility |= GUEST_INTR_STATE_STI;
2432
2433 if ((interruptibility != interruptibility_old))
2434 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2435}
2436
Avi Kivity6aa8b732006-12-10 02:21:36 -08002437static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2438{
2439 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002441 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002443 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444
Glauber Costa2809f5d2009-05-12 16:21:05 -04002445 /* skipping an emulated instruction also counts */
2446 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002447}
2448
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002449static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2450 unsigned long exit_qual)
2451{
2452 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2453 unsigned int nr = vcpu->arch.exception.nr;
2454 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2455
2456 if (vcpu->arch.exception.has_error_code) {
2457 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2458 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2459 }
2460
2461 if (kvm_exception_is_soft(nr))
2462 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2463 else
2464 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2465
2466 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2467 vmx_get_nmi_mask(vcpu))
2468 intr_info |= INTR_INFO_UNBLOCK_NMI;
2469
2470 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2471}
2472
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002473/*
2474 * KVM wants to inject page-faults which it got to the guest. This function
2475 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002476 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002477static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002478{
2479 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002480 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002481
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002482 if (nr == PF_VECTOR) {
2483 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002484 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002485 return 1;
2486 }
2487 /*
2488 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2489 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2490 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2491 * can be written only when inject_pending_event runs. This should be
2492 * conditional on a new capability---if the capability is disabled,
2493 * kvm_multiple_exception would write the ancillary information to
2494 * CR2 or DR6, for backwards ABI-compatibility.
2495 */
2496 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2497 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002498 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002499 return 1;
2500 }
2501 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002502 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002503 if (nr == DB_VECTOR)
2504 *exit_qual = vcpu->arch.dr6;
2505 else
2506 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002507 return 1;
2508 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002509 }
2510
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002511 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002512}
2513
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002514static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02002515{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002516 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002517 unsigned nr = vcpu->arch.exception.nr;
2518 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002519 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002520 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002521
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002522 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002523 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002524 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2525 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002526
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002527 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002528 int inc_eip = 0;
2529 if (kvm_exception_is_soft(nr))
2530 inc_eip = vcpu->arch.event_exit_inst_len;
2531 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002532 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002533 return;
2534 }
2535
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002536 if (kvm_exception_is_soft(nr)) {
2537 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2538 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002539 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2540 } else
2541 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2542
2543 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002544}
2545
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002546static bool vmx_rdtscp_supported(void)
2547{
2548 return cpu_has_vmx_rdtscp();
2549}
2550
Mao, Junjiead756a12012-07-02 01:18:48 +00002551static bool vmx_invpcid_supported(void)
2552{
2553 return cpu_has_vmx_invpcid() && enable_ept;
2554}
2555
Avi Kivity6aa8b732006-12-10 02:21:36 -08002556/*
Eddie Donga75beee2007-05-17 18:55:15 +03002557 * Swap MSR entry in host/guest MSR entry array.
2558 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002559static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002560{
Avi Kivity26bb0982009-09-07 11:14:12 +03002561 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002562
2563 tmp = vmx->guest_msrs[to];
2564 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2565 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002566}
2567
Yang Zhang8d146952013-01-25 10:18:50 +08002568static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2569{
2570 unsigned long *msr_bitmap;
2571
Wincy Van670125b2015-03-04 14:31:56 +08002572 if (is_guest_mode(vcpu))
Radim Krčmářd048c092016-08-08 20:16:22 +02002573 msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap;
Roman Kagan3ce424e2016-05-18 17:48:20 +03002574 else if (cpu_has_secondary_exec_ctrls() &&
2575 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
2576 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002577 if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) {
2578 if (is_long_mode(vcpu))
Wanpeng Lic63e4562016-09-23 19:17:16 +08002579 msr_bitmap = vmx_msr_bitmap_longmode_x2apic_apicv;
2580 else
2581 msr_bitmap = vmx_msr_bitmap_legacy_x2apic_apicv;
2582 } else {
2583 if (is_long_mode(vcpu))
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002584 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2585 else
2586 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002587 }
Yang Zhang8d146952013-01-25 10:18:50 +08002588 } else {
2589 if (is_long_mode(vcpu))
2590 msr_bitmap = vmx_msr_bitmap_longmode;
2591 else
2592 msr_bitmap = vmx_msr_bitmap_legacy;
2593 }
2594
2595 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2596}
2597
Eddie Donga75beee2007-05-17 18:55:15 +03002598/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002599 * Set up the vmcs to automatically save and restore system
2600 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2601 * mode, as fiddling with msrs is very expensive.
2602 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002603static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002604{
Avi Kivity26bb0982009-09-07 11:14:12 +03002605 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002606
Eddie Donga75beee2007-05-17 18:55:15 +03002607 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002608#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002609 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002610 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002611 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002612 move_msr_up(vmx, index, save_nmsrs++);
2613 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002614 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002615 move_msr_up(vmx, index, save_nmsrs++);
2616 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002617 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002618 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002619 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002620 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002621 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002622 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002623 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002624 * if efer.sce is enabled.
2625 */
Brian Gerst8c065852010-07-17 09:03:26 -04002626 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002627 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002628 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002629 }
Eddie Donga75beee2007-05-17 18:55:15 +03002630#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002631 index = __find_msr_index(vmx, MSR_EFER);
2632 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002633 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002634
Avi Kivity26bb0982009-09-07 11:14:12 +03002635 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002636
Yang Zhang8d146952013-01-25 10:18:50 +08002637 if (cpu_has_vmx_msr_bitmap())
2638 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002639}
2640
2641/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002643 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2644 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002645 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002646static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647{
2648 u64 host_tsc, tsc_offset;
2649
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002650 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002651 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002652 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002653}
2654
2655/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002656 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002658static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002660 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002661 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002662 * We're here if L1 chose not to trap WRMSR to TSC. According
2663 * to the spec, this should set L1's TSC; The offset that L1
2664 * set for L2 remains unchanged, and still needs to be added
2665 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002666 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002667 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002668 /* recalculate vmcs02.TSC_OFFSET: */
2669 vmcs12 = get_vmcs12(vcpu);
2670 vmcs_write64(TSC_OFFSET, offset +
2671 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2672 vmcs12->tsc_offset : 0));
2673 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002674 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2675 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002676 vmcs_write64(TSC_OFFSET, offset);
2677 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678}
2679
Nadav Har'El801d3422011-05-25 23:02:23 +03002680/*
2681 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2682 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2683 * all guests if the "nested" module option is off, and can also be disabled
2684 * for a single guest by disabling its VMX cpuid bit.
2685 */
2686static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2687{
Radim Krčmářd6321d42017-08-05 00:12:49 +02002688 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03002689}
2690
Avi Kivity6aa8b732006-12-10 02:21:36 -08002691/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002692 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2693 * returned for the various VMX controls MSRs when nested VMX is enabled.
2694 * The same values should also be used to verify that vmcs12 control fields are
2695 * valid during nested entry from L1 to L2.
2696 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2697 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2698 * bit in the high half is on if the corresponding bit in the control field
2699 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002700 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002701static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002702{
2703 /*
2704 * Note that as a general rule, the high half of the MSRs (bits in
2705 * the control fields which may be 1) should be initialized by the
2706 * intersection of the underlying hardware's MSR (i.e., features which
2707 * can be supported) and the list of features we want to expose -
2708 * because they are known to be properly supported in our code.
2709 * Also, usually, the low half of the MSRs (bits which must be 1) can
2710 * be set to 0, meaning that L1 may turn off any of these bits. The
2711 * reason is that if one of these bits is necessary, it will appear
2712 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2713 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02002714 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002715 * These rules have exceptions below.
2716 */
2717
2718 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002719 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002720 vmx->nested.nested_vmx_pinbased_ctls_low,
2721 vmx->nested.nested_vmx_pinbased_ctls_high);
2722 vmx->nested.nested_vmx_pinbased_ctls_low |=
2723 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2724 vmx->nested.nested_vmx_pinbased_ctls_high &=
2725 PIN_BASED_EXT_INTR_MASK |
2726 PIN_BASED_NMI_EXITING |
2727 PIN_BASED_VIRTUAL_NMIS;
2728 vmx->nested.nested_vmx_pinbased_ctls_high |=
2729 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002730 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002731 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002732 vmx->nested.nested_vmx_pinbased_ctls_high |=
2733 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002734
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002735 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002736 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002737 vmx->nested.nested_vmx_exit_ctls_low,
2738 vmx->nested.nested_vmx_exit_ctls_high);
2739 vmx->nested.nested_vmx_exit_ctls_low =
2740 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002741
Wincy Vanb9c237b2015-02-03 23:56:30 +08002742 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002743#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002744 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002745#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01002746 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002747 vmx->nested.nested_vmx_exit_ctls_high |=
2748 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002749 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002750 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2751
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002752 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002753 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002754
Jan Kiszka2996fca2014-06-16 13:59:43 +02002755 /* We support free control of debug control saving. */
David Matlack0115f9c2016-11-29 18:14:06 -08002756 vmx->nested.nested_vmx_exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002757
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002758 /* entry controls */
2759 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002760 vmx->nested.nested_vmx_entry_ctls_low,
2761 vmx->nested.nested_vmx_entry_ctls_high);
2762 vmx->nested.nested_vmx_entry_ctls_low =
2763 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2764 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002765#ifdef CONFIG_X86_64
2766 VM_ENTRY_IA32E_MODE |
2767#endif
2768 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002769 vmx->nested.nested_vmx_entry_ctls_high |=
2770 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002771 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002772 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002773
Jan Kiszka2996fca2014-06-16 13:59:43 +02002774 /* We support free control of debug control loading. */
David Matlack0115f9c2016-11-29 18:14:06 -08002775 vmx->nested.nested_vmx_entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002776
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002777 /* cpu-based controls */
2778 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002779 vmx->nested.nested_vmx_procbased_ctls_low,
2780 vmx->nested.nested_vmx_procbased_ctls_high);
2781 vmx->nested.nested_vmx_procbased_ctls_low =
2782 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2783 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002784 CPU_BASED_VIRTUAL_INTR_PENDING |
2785 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002786 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2787 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2788 CPU_BASED_CR3_STORE_EXITING |
2789#ifdef CONFIG_X86_64
2790 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2791#endif
2792 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002793 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2794 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2795 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2796 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002797 /*
2798 * We can allow some features even when not supported by the
2799 * hardware. For example, L1 can specify an MSR bitmap - and we
2800 * can use it to avoid exits to L1 - even when L0 runs L2
2801 * without MSR bitmaps.
2802 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002803 vmx->nested.nested_vmx_procbased_ctls_high |=
2804 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002805 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002806
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002807 /* We support free control of CR3 access interception. */
David Matlack0115f9c2016-11-29 18:14:06 -08002808 vmx->nested.nested_vmx_procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002809 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2810
Paolo Bonzini80154d72017-08-24 13:55:35 +02002811 /*
2812 * secondary cpu-based controls. Do not include those that
2813 * depend on CPUID bits, they are added later by vmx_cpuid_update.
2814 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002815 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002816 vmx->nested.nested_vmx_secondary_ctls_low,
2817 vmx->nested.nested_vmx_secondary_ctls_high);
2818 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2819 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002820 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02002821 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08002822 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08002823 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002824 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02002825 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002826
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002827 if (enable_ept) {
2828 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002829 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002830 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002831 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002832 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002833 if (cpu_has_vmx_ept_execute_only())
2834 vmx->nested.nested_vmx_ept_caps |=
2835 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002836 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04002837 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002838 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
2839 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04002840 if (enable_ept_ad_bits) {
2841 vmx->nested.nested_vmx_secondary_ctls_high |=
2842 SECONDARY_EXEC_ENABLE_PML;
Dan Carpenter7461fbc2017-05-18 10:41:15 +03002843 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04002844 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02002845 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002846
Bandan Das27c42a12017-08-03 15:54:42 -04002847 if (cpu_has_vmx_vmfunc()) {
2848 vmx->nested.nested_vmx_secondary_ctls_high |=
2849 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04002850 /*
2851 * Advertise EPTP switching unconditionally
2852 * since we emulate it
2853 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08002854 if (enable_ept)
2855 vmx->nested.nested_vmx_vmfunc_controls =
2856 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04002857 }
2858
Paolo Bonzinief697a72016-03-18 16:58:38 +01002859 /*
2860 * Old versions of KVM use the single-context version without
2861 * checking for support, so declare that it is supported even
2862 * though it is treated as global context. The alternative is
2863 * not failing the single-context invvpid, and it is worse.
2864 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07002865 if (enable_vpid) {
2866 vmx->nested.nested_vmx_secondary_ctls_high |=
2867 SECONDARY_EXEC_ENABLE_VPID;
Wanpeng Li089d7b62015-10-13 09:18:37 -07002868 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03002869 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02002870 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002871
Radim Krčmář0790ec12015-03-17 14:02:32 +01002872 if (enable_unrestricted_guest)
2873 vmx->nested.nested_vmx_secondary_ctls_high |=
2874 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2875
Jan Kiszkac18911a2013-03-13 16:06:41 +01002876 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002877 rdmsr(MSR_IA32_VMX_MISC,
2878 vmx->nested.nested_vmx_misc_low,
2879 vmx->nested.nested_vmx_misc_high);
2880 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2881 vmx->nested.nested_vmx_misc_low |=
2882 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002883 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002884 vmx->nested.nested_vmx_misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002885
2886 /*
2887 * This MSR reports some information about VMX support. We
2888 * should return information about the VMX we emulate for the
2889 * guest, and the VMCS structure we give it - not about the
2890 * VMX support of the underlying hardware.
2891 */
2892 vmx->nested.nested_vmx_basic =
2893 VMCS12_REVISION |
2894 VMX_BASIC_TRUE_CTLS |
2895 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2896 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2897
2898 if (cpu_has_vmx_basic_inout())
2899 vmx->nested.nested_vmx_basic |= VMX_BASIC_INOUT;
2900
2901 /*
David Matlack8322ebb2016-11-29 18:14:09 -08002902 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08002903 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2904 * We picked the standard core2 setting.
2905 */
2906#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2907#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2908 vmx->nested.nested_vmx_cr0_fixed0 = VMXON_CR0_ALWAYSON;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002909 vmx->nested.nested_vmx_cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08002910
2911 /* These MSRs specify bits which the guest must keep fixed off. */
2912 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, vmx->nested.nested_vmx_cr0_fixed1);
2913 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, vmx->nested.nested_vmx_cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08002914
2915 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
2916 vmx->nested.nested_vmx_vmcs_enum = 0x2e;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002917}
2918
David Matlack38991522016-11-29 18:14:08 -08002919/*
2920 * if fixed0[i] == 1: val[i] must be 1
2921 * if fixed1[i] == 0: val[i] must be 0
2922 */
2923static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
2924{
2925 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002926}
2927
2928static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2929{
David Matlack38991522016-11-29 18:14:08 -08002930 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002931}
2932
2933static inline u64 vmx_control_msr(u32 low, u32 high)
2934{
2935 return low | ((u64)high << 32);
2936}
2937
David Matlack62cc6b9d2016-11-29 18:14:07 -08002938static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
2939{
2940 superset &= mask;
2941 subset &= mask;
2942
2943 return (superset | subset) == superset;
2944}
2945
2946static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
2947{
2948 const u64 feature_and_reserved =
2949 /* feature (except bit 48; see below) */
2950 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
2951 /* reserved */
2952 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
2953 u64 vmx_basic = vmx->nested.nested_vmx_basic;
2954
2955 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
2956 return -EINVAL;
2957
2958 /*
2959 * KVM does not emulate a version of VMX that constrains physical
2960 * addresses of VMX structures (e.g. VMCS) to 32-bits.
2961 */
2962 if (data & BIT_ULL(48))
2963 return -EINVAL;
2964
2965 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
2966 vmx_basic_vmcs_revision_id(data))
2967 return -EINVAL;
2968
2969 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
2970 return -EINVAL;
2971
2972 vmx->nested.nested_vmx_basic = data;
2973 return 0;
2974}
2975
2976static int
2977vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
2978{
2979 u64 supported;
2980 u32 *lowp, *highp;
2981
2982 switch (msr_index) {
2983 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2984 lowp = &vmx->nested.nested_vmx_pinbased_ctls_low;
2985 highp = &vmx->nested.nested_vmx_pinbased_ctls_high;
2986 break;
2987 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2988 lowp = &vmx->nested.nested_vmx_procbased_ctls_low;
2989 highp = &vmx->nested.nested_vmx_procbased_ctls_high;
2990 break;
2991 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2992 lowp = &vmx->nested.nested_vmx_exit_ctls_low;
2993 highp = &vmx->nested.nested_vmx_exit_ctls_high;
2994 break;
2995 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2996 lowp = &vmx->nested.nested_vmx_entry_ctls_low;
2997 highp = &vmx->nested.nested_vmx_entry_ctls_high;
2998 break;
2999 case MSR_IA32_VMX_PROCBASED_CTLS2:
3000 lowp = &vmx->nested.nested_vmx_secondary_ctls_low;
3001 highp = &vmx->nested.nested_vmx_secondary_ctls_high;
3002 break;
3003 default:
3004 BUG();
3005 }
3006
3007 supported = vmx_control_msr(*lowp, *highp);
3008
3009 /* Check must-be-1 bits are still 1. */
3010 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3011 return -EINVAL;
3012
3013 /* Check must-be-0 bits are still 0. */
3014 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3015 return -EINVAL;
3016
3017 *lowp = data;
3018 *highp = data >> 32;
3019 return 0;
3020}
3021
3022static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3023{
3024 const u64 feature_and_reserved_bits =
3025 /* feature */
3026 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3027 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3028 /* reserved */
3029 GENMASK_ULL(13, 9) | BIT_ULL(31);
3030 u64 vmx_misc;
3031
3032 vmx_misc = vmx_control_msr(vmx->nested.nested_vmx_misc_low,
3033 vmx->nested.nested_vmx_misc_high);
3034
3035 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3036 return -EINVAL;
3037
3038 if ((vmx->nested.nested_vmx_pinbased_ctls_high &
3039 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3040 vmx_misc_preemption_timer_rate(data) !=
3041 vmx_misc_preemption_timer_rate(vmx_misc))
3042 return -EINVAL;
3043
3044 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3045 return -EINVAL;
3046
3047 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3048 return -EINVAL;
3049
3050 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3051 return -EINVAL;
3052
3053 vmx->nested.nested_vmx_misc_low = data;
3054 vmx->nested.nested_vmx_misc_high = data >> 32;
3055 return 0;
3056}
3057
3058static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3059{
3060 u64 vmx_ept_vpid_cap;
3061
3062 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.nested_vmx_ept_caps,
3063 vmx->nested.nested_vmx_vpid_caps);
3064
3065 /* Every bit is either reserved or a feature bit. */
3066 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3067 return -EINVAL;
3068
3069 vmx->nested.nested_vmx_ept_caps = data;
3070 vmx->nested.nested_vmx_vpid_caps = data >> 32;
3071 return 0;
3072}
3073
3074static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3075{
3076 u64 *msr;
3077
3078 switch (msr_index) {
3079 case MSR_IA32_VMX_CR0_FIXED0:
3080 msr = &vmx->nested.nested_vmx_cr0_fixed0;
3081 break;
3082 case MSR_IA32_VMX_CR4_FIXED0:
3083 msr = &vmx->nested.nested_vmx_cr4_fixed0;
3084 break;
3085 default:
3086 BUG();
3087 }
3088
3089 /*
3090 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3091 * must be 1 in the restored value.
3092 */
3093 if (!is_bitwise_subset(data, *msr, -1ULL))
3094 return -EINVAL;
3095
3096 *msr = data;
3097 return 0;
3098}
3099
3100/*
3101 * Called when userspace is restoring VMX MSRs.
3102 *
3103 * Returns 0 on success, non-0 otherwise.
3104 */
3105static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3106{
3107 struct vcpu_vmx *vmx = to_vmx(vcpu);
3108
3109 switch (msr_index) {
3110 case MSR_IA32_VMX_BASIC:
3111 return vmx_restore_vmx_basic(vmx, data);
3112 case MSR_IA32_VMX_PINBASED_CTLS:
3113 case MSR_IA32_VMX_PROCBASED_CTLS:
3114 case MSR_IA32_VMX_EXIT_CTLS:
3115 case MSR_IA32_VMX_ENTRY_CTLS:
3116 /*
3117 * The "non-true" VMX capability MSRs are generated from the
3118 * "true" MSRs, so we do not support restoring them directly.
3119 *
3120 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3121 * should restore the "true" MSRs with the must-be-1 bits
3122 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3123 * DEFAULT SETTINGS".
3124 */
3125 return -EINVAL;
3126 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3127 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3128 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3129 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3130 case MSR_IA32_VMX_PROCBASED_CTLS2:
3131 return vmx_restore_control_msr(vmx, msr_index, data);
3132 case MSR_IA32_VMX_MISC:
3133 return vmx_restore_vmx_misc(vmx, data);
3134 case MSR_IA32_VMX_CR0_FIXED0:
3135 case MSR_IA32_VMX_CR4_FIXED0:
3136 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3137 case MSR_IA32_VMX_CR0_FIXED1:
3138 case MSR_IA32_VMX_CR4_FIXED1:
3139 /*
3140 * These MSRs are generated based on the vCPU's CPUID, so we
3141 * do not support restoring them directly.
3142 */
3143 return -EINVAL;
3144 case MSR_IA32_VMX_EPT_VPID_CAP:
3145 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3146 case MSR_IA32_VMX_VMCS_ENUM:
3147 vmx->nested.nested_vmx_vmcs_enum = data;
3148 return 0;
3149 default:
3150 /*
3151 * The rest of the VMX capability MSRs do not support restore.
3152 */
3153 return -EINVAL;
3154 }
3155}
3156
Jan Kiszkacae50132014-01-04 18:47:22 +01003157/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003158static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
3159{
Wincy Vanb9c237b2015-02-03 23:56:30 +08003160 struct vcpu_vmx *vmx = to_vmx(vcpu);
3161
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003162 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003163 case MSR_IA32_VMX_BASIC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003164 *pdata = vmx->nested.nested_vmx_basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003165 break;
3166 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3167 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003168 *pdata = vmx_control_msr(
3169 vmx->nested.nested_vmx_pinbased_ctls_low,
3170 vmx->nested.nested_vmx_pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003171 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3172 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003173 break;
3174 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3175 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003176 *pdata = vmx_control_msr(
3177 vmx->nested.nested_vmx_procbased_ctls_low,
3178 vmx->nested.nested_vmx_procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003179 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3180 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003181 break;
3182 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3183 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003184 *pdata = vmx_control_msr(
3185 vmx->nested.nested_vmx_exit_ctls_low,
3186 vmx->nested.nested_vmx_exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003187 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3188 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003189 break;
3190 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3191 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003192 *pdata = vmx_control_msr(
3193 vmx->nested.nested_vmx_entry_ctls_low,
3194 vmx->nested.nested_vmx_entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003195 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3196 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003197 break;
3198 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003199 *pdata = vmx_control_msr(
3200 vmx->nested.nested_vmx_misc_low,
3201 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003202 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003203 case MSR_IA32_VMX_CR0_FIXED0:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003204 *pdata = vmx->nested.nested_vmx_cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003205 break;
3206 case MSR_IA32_VMX_CR0_FIXED1:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003207 *pdata = vmx->nested.nested_vmx_cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003208 break;
3209 case MSR_IA32_VMX_CR4_FIXED0:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003210 *pdata = vmx->nested.nested_vmx_cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003211 break;
3212 case MSR_IA32_VMX_CR4_FIXED1:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003213 *pdata = vmx->nested.nested_vmx_cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003214 break;
3215 case MSR_IA32_VMX_VMCS_ENUM:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003216 *pdata = vmx->nested.nested_vmx_vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003217 break;
3218 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003219 *pdata = vmx_control_msr(
3220 vmx->nested.nested_vmx_secondary_ctls_low,
3221 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003222 break;
3223 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07003224 *pdata = vmx->nested.nested_vmx_ept_caps |
3225 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003226 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003227 case MSR_IA32_VMX_VMFUNC:
3228 *pdata = vmx->nested.nested_vmx_vmfunc_controls;
3229 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003230 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003231 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003232 }
3233
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003234 return 0;
3235}
3236
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003237static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3238 uint64_t val)
3239{
3240 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3241
3242 return !(val & ~valid_bits);
3243}
3244
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003245/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246 * Reads an msr value (of 'msr_index') into 'pdata'.
3247 * Returns 0 on success, non-0 otherwise.
3248 * Assumes vcpu_load() was already called.
3249 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003250static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003251{
Avi Kivity26bb0982009-09-07 11:14:12 +03003252 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003253
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003254 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003255#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003256 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003257 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003258 break;
3259 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003260 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003261 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003262 case MSR_KERNEL_GS_BASE:
3263 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003264 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003265 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003266#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003268 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303269 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003270 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003271 break;
3272 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003273 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003274 break;
3275 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003276 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277 break;
3278 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003279 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003280 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003281 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003282 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003283 (!msr_info->host_initiated &&
3284 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003285 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003286 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003287 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003288 case MSR_IA32_MCG_EXT_CTL:
3289 if (!msr_info->host_initiated &&
3290 !(to_vmx(vcpu)->msr_ia32_feature_control &
3291 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003292 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003293 msr_info->data = vcpu->arch.mcg_ext_ctl;
3294 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003295 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003296 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003297 break;
3298 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3299 if (!nested_vmx_allowed(vcpu))
3300 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003301 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003302 case MSR_IA32_XSS:
3303 if (!vmx_xsaves_supported())
3304 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003305 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003306 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003307 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003308 if (!msr_info->host_initiated &&
3309 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003310 return 1;
3311 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003312 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003313 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003314 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003315 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003316 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003317 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003318 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003319 }
3320
Avi Kivity6aa8b732006-12-10 02:21:36 -08003321 return 0;
3322}
3323
Jan Kiszkacae50132014-01-04 18:47:22 +01003324static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3325
Avi Kivity6aa8b732006-12-10 02:21:36 -08003326/*
3327 * Writes msr value into into the appropriate "register".
3328 * Returns 0 on success, non-0 otherwise.
3329 * Assumes vcpu_load() was already called.
3330 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003331static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003332{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003333 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003334 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003335 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003336 u32 msr_index = msr_info->index;
3337 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003338
Avi Kivity6aa8b732006-12-10 02:21:36 -08003339 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003340 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003341 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003342 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003343#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003344 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003345 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003346 vmcs_writel(GUEST_FS_BASE, data);
3347 break;
3348 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003349 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003350 vmcs_writel(GUEST_GS_BASE, data);
3351 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003352 case MSR_KERNEL_GS_BASE:
3353 vmx_load_host_state(vmx);
3354 vmx->msr_guest_kernel_gs_base = data;
3355 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003356#endif
3357 case MSR_IA32_SYSENTER_CS:
3358 vmcs_write32(GUEST_SYSENTER_CS, data);
3359 break;
3360 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003361 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003362 break;
3363 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003364 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003365 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003366 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003367 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003368 (!msr_info->host_initiated &&
3369 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003370 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003371 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003372 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003373 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003374 vmcs_write64(GUEST_BNDCFGS, data);
3375 break;
3376 case MSR_IA32_TSC:
3377 kvm_write_tsc(vcpu, msr_info);
3378 break;
3379 case MSR_IA32_CR_PAT:
Will Auld8fe8ab42012-11-29 12:42:12 -08003380 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003381 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3382 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003383 vmcs_write64(GUEST_IA32_PAT, data);
3384 vcpu->arch.pat = data;
3385 break;
3386 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003387 ret = kvm_set_msr_common(vcpu, msr_info);
3388 break;
Will Auldba904632012-11-29 12:42:50 -08003389 case MSR_IA32_TSC_ADJUST:
3390 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003391 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003392 case MSR_IA32_MCG_EXT_CTL:
3393 if ((!msr_info->host_initiated &&
3394 !(to_vmx(vcpu)->msr_ia32_feature_control &
3395 FEATURE_CONTROL_LMCE)) ||
3396 (data & ~MCG_EXT_CTL_LMCE_EN))
3397 return 1;
3398 vcpu->arch.mcg_ext_ctl = data;
3399 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003400 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003401 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003402 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003403 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3404 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003405 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003406 if (msr_info->host_initiated && data == 0)
3407 vmx_leave_nested(vcpu);
3408 break;
3409 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003410 if (!msr_info->host_initiated)
3411 return 1; /* they are read-only */
3412 if (!nested_vmx_allowed(vcpu))
3413 return 1;
3414 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003415 case MSR_IA32_XSS:
3416 if (!vmx_xsaves_supported())
3417 return 1;
3418 /*
3419 * The only supported bit as of Skylake is bit 8, but
3420 * it is not supported on KVM.
3421 */
3422 if (data != 0)
3423 return 1;
3424 vcpu->arch.ia32_xss = data;
3425 if (vcpu->arch.ia32_xss != host_xss)
3426 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3427 vcpu->arch.ia32_xss, host_xss);
3428 else
3429 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3430 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003431 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003432 if (!msr_info->host_initiated &&
3433 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003434 return 1;
3435 /* Check reserved bit, higher 32 bits should be zero */
3436 if ((data >> 32) != 0)
3437 return 1;
3438 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003439 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003440 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003441 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003442 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003443 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003444 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3445 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003446 ret = kvm_set_shared_msr(msr->index, msr->data,
3447 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003448 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003449 if (ret)
3450 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003451 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003452 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003453 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003454 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003455 }
3456
Eddie Dong2cc51562007-05-21 07:28:09 +03003457 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003458}
3459
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003460static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003461{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003462 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3463 switch (reg) {
3464 case VCPU_REGS_RSP:
3465 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3466 break;
3467 case VCPU_REGS_RIP:
3468 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3469 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003470 case VCPU_EXREG_PDPTR:
3471 if (enable_ept)
3472 ept_save_pdptrs(vcpu);
3473 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003474 default:
3475 break;
3476 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003477}
3478
Avi Kivity6aa8b732006-12-10 02:21:36 -08003479static __init int cpu_has_kvm_support(void)
3480{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003481 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003482}
3483
3484static __init int vmx_disabled_by_bios(void)
3485{
3486 u64 msr;
3487
3488 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003489 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003490 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003491 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3492 && tboot_enabled())
3493 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003494 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003495 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003496 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003497 && !tboot_enabled()) {
3498 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003499 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003500 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003501 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003502 /* launched w/o TXT and VMX disabled */
3503 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3504 && !tboot_enabled())
3505 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003506 }
3507
3508 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003509}
3510
Dongxiao Xu7725b892010-05-11 18:29:38 +08003511static void kvm_cpu_vmxon(u64 addr)
3512{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003513 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003514 intel_pt_handle_vmx(1);
3515
Dongxiao Xu7725b892010-05-11 18:29:38 +08003516 asm volatile (ASM_VMX_VMXON_RAX
3517 : : "a"(&addr), "m"(addr)
3518 : "memory", "cc");
3519}
3520
Radim Krčmář13a34e02014-08-28 15:13:03 +02003521static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003522{
3523 int cpu = raw_smp_processor_id();
3524 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003525 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003526
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003527 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003528 return -EBUSY;
3529
Nadav Har'Eld462b812011-05-24 15:26:10 +03003530 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003531 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3532 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003533
3534 /*
3535 * Now we can enable the vmclear operation in kdump
3536 * since the loaded_vmcss_on_cpu list on this cpu
3537 * has been initialized.
3538 *
3539 * Though the cpu is not in VMX operation now, there
3540 * is no problem to enable the vmclear operation
3541 * for the loaded_vmcss_on_cpu list is empty!
3542 */
3543 crash_enable_local_vmclear(cpu);
3544
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003546
3547 test_bits = FEATURE_CONTROL_LOCKED;
3548 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3549 if (tboot_enabled())
3550 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3551
3552 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003553 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003554 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3555 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003556 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02003557 if (enable_ept)
3558 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02003559
3560 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003561}
3562
Nadav Har'Eld462b812011-05-24 15:26:10 +03003563static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003564{
3565 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003566 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003567
Nadav Har'Eld462b812011-05-24 15:26:10 +03003568 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3569 loaded_vmcss_on_cpu_link)
3570 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003571}
3572
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003573
3574/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3575 * tricks.
3576 */
3577static void kvm_cpu_vmxoff(void)
3578{
3579 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003580
3581 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003582 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003583}
3584
Radim Krčmář13a34e02014-08-28 15:13:03 +02003585static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003586{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003587 vmclear_local_loaded_vmcss();
3588 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003589}
3590
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003591static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003592 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003593{
3594 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003595 u32 ctl = ctl_min | ctl_opt;
3596
3597 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3598
3599 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3600 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3601
3602 /* Ensure minimum (required) set of control bits are supported. */
3603 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003604 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003605
3606 *result = ctl;
3607 return 0;
3608}
3609
Avi Kivity110312c2010-12-21 12:54:20 +02003610static __init bool allow_1_setting(u32 msr, u32 ctl)
3611{
3612 u32 vmx_msr_low, vmx_msr_high;
3613
3614 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3615 return vmx_msr_high & ctl;
3616}
3617
Yang, Sheng002c7f72007-07-31 14:23:01 +03003618static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003619{
3620 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003621 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003622 u32 _pin_based_exec_control = 0;
3623 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003624 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003625 u32 _vmexit_control = 0;
3626 u32 _vmentry_control = 0;
3627
Raghavendra K T10166742012-02-07 23:19:20 +05303628 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003629#ifdef CONFIG_X86_64
3630 CPU_BASED_CR8_LOAD_EXITING |
3631 CPU_BASED_CR8_STORE_EXITING |
3632#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003633 CPU_BASED_CR3_LOAD_EXITING |
3634 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003635 CPU_BASED_USE_IO_BITMAPS |
3636 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003637 CPU_BASED_USE_TSC_OFFSETING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003638 CPU_BASED_INVLPG_EXITING |
3639 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003640
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003641 if (!kvm_mwait_in_guest())
3642 min |= CPU_BASED_MWAIT_EXITING |
3643 CPU_BASED_MONITOR_EXITING;
3644
Sheng Yangf78e0e22007-10-29 09:40:42 +08003645 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003646 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003647 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003648 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3649 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003650 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003651#ifdef CONFIG_X86_64
3652 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3653 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3654 ~CPU_BASED_CR8_STORE_EXITING;
3655#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003656 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003657 min2 = 0;
3658 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003659 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003660 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003661 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003662 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003663 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003664 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003665 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003666 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003667 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003668 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003669 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003670 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02003671 SECONDARY_EXEC_RDSEED_EXITING |
3672 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003673 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04003674 SECONDARY_EXEC_TSC_SCALING |
3675 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08003676 if (adjust_vmx_controls(min2, opt2,
3677 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003678 &_cpu_based_2nd_exec_control) < 0)
3679 return -EIO;
3680 }
3681#ifndef CONFIG_X86_64
3682 if (!(_cpu_based_2nd_exec_control &
3683 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3684 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3685#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003686
3687 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3688 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003689 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003690 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3691 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003692
Wanpeng Li61f1dd92017-10-18 16:02:19 -07003693 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
3694 &vmx_capability.ept, &vmx_capability.vpid);
3695
Sheng Yangd56f5462008-04-25 10:13:16 +08003696 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003697 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3698 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003699 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3700 CPU_BASED_CR3_STORE_EXITING |
3701 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07003702 } else if (vmx_capability.ept) {
3703 vmx_capability.ept = 0;
3704 pr_warn_once("EPT CAP should not exist if not support "
3705 "1-setting enable EPT VM-execution control\n");
3706 }
3707 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
3708 vmx_capability.vpid) {
3709 vmx_capability.vpid = 0;
3710 pr_warn_once("VPID CAP should not exist if not support "
3711 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08003712 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003713
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003714 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003715#ifdef CONFIG_X86_64
3716 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3717#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003718 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003719 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003720 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3721 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003722 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003723
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01003724 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3725 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3726 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003727 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3728 &_pin_based_exec_control) < 0)
3729 return -EIO;
3730
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003731 if (cpu_has_broken_vmx_preemption_timer())
3732 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003733 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003734 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003735 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3736
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003737 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003738 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003739 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3740 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003741 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003742
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003743 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003744
3745 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3746 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003747 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003748
3749#ifdef CONFIG_X86_64
3750 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3751 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003752 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003753#endif
3754
3755 /* Require Write-Back (WB) memory type for VMCS accesses. */
3756 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003757 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003758
Yang, Sheng002c7f72007-07-31 14:23:01 +03003759 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003760 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003761 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003762 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003763
Yang, Sheng002c7f72007-07-31 14:23:01 +03003764 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3765 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003766 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003767 vmcs_conf->vmexit_ctrl = _vmexit_control;
3768 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003769
Avi Kivity110312c2010-12-21 12:54:20 +02003770 cpu_has_load_ia32_efer =
3771 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3772 VM_ENTRY_LOAD_IA32_EFER)
3773 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3774 VM_EXIT_LOAD_IA32_EFER);
3775
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003776 cpu_has_load_perf_global_ctrl =
3777 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3778 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3779 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3780 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3781
3782 /*
3783 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003784 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003785 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3786 *
3787 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3788 *
3789 * AAK155 (model 26)
3790 * AAP115 (model 30)
3791 * AAT100 (model 37)
3792 * BC86,AAY89,BD102 (model 44)
3793 * BA97 (model 46)
3794 *
3795 */
3796 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3797 switch (boot_cpu_data.x86_model) {
3798 case 26:
3799 case 30:
3800 case 37:
3801 case 44:
3802 case 46:
3803 cpu_has_load_perf_global_ctrl = false;
3804 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3805 "does not work properly. Using workaround\n");
3806 break;
3807 default:
3808 break;
3809 }
3810 }
3811
Borislav Petkov782511b2016-04-04 22:25:03 +02003812 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003813 rdmsrl(MSR_IA32_XSS, host_xss);
3814
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003815 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003816}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003817
3818static struct vmcs *alloc_vmcs_cpu(int cpu)
3819{
3820 int node = cpu_to_node(cpu);
3821 struct page *pages;
3822 struct vmcs *vmcs;
3823
Vlastimil Babka96db8002015-09-08 15:03:50 -07003824 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003825 if (!pages)
3826 return NULL;
3827 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003828 memset(vmcs, 0, vmcs_config.size);
3829 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003830 return vmcs;
3831}
3832
3833static struct vmcs *alloc_vmcs(void)
3834{
Ingo Molnard3b2c332007-01-05 16:36:23 -08003835 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08003836}
3837
3838static void free_vmcs(struct vmcs *vmcs)
3839{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003840 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003841}
3842
Nadav Har'Eld462b812011-05-24 15:26:10 +03003843/*
3844 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3845 */
3846static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3847{
3848 if (!loaded_vmcs->vmcs)
3849 return;
3850 loaded_vmcs_clear(loaded_vmcs);
3851 free_vmcs(loaded_vmcs->vmcs);
3852 loaded_vmcs->vmcs = NULL;
Jim Mattson355f4fb2016-10-28 08:29:39 -07003853 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003854}
3855
Sam Ravnborg39959582007-06-01 00:47:13 -07003856static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003857{
3858 int cpu;
3859
Zachary Amsden3230bb42009-09-29 11:38:37 -10003860 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003861 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003862 per_cpu(vmxarea, cpu) = NULL;
3863 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864}
3865
Jim Mattson85fd5142017-07-07 12:51:41 -07003866enum vmcs_field_type {
3867 VMCS_FIELD_TYPE_U16 = 0,
3868 VMCS_FIELD_TYPE_U64 = 1,
3869 VMCS_FIELD_TYPE_U32 = 2,
3870 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
3871};
3872
3873static inline int vmcs_field_type(unsigned long field)
3874{
3875 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
3876 return VMCS_FIELD_TYPE_U32;
3877 return (field >> 13) & 0x3 ;
3878}
3879
3880static inline int vmcs_field_readonly(unsigned long field)
3881{
3882 return (((field >> 10) & 0x3) == 1);
3883}
3884
Bandan Dasfe2b2012014-04-21 15:20:14 -04003885static void init_vmcs_shadow_fields(void)
3886{
3887 int i, j;
3888
3889 /* No checks for read only fields yet */
3890
3891 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3892 switch (shadow_read_write_fields[i]) {
3893 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003894 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003895 continue;
3896 break;
3897 default:
3898 break;
3899 }
3900
3901 if (j < i)
3902 shadow_read_write_fields[j] =
3903 shadow_read_write_fields[i];
3904 j++;
3905 }
3906 max_shadow_read_write_fields = j;
3907
3908 /* shadowed fields guest access without vmexit */
3909 for (i = 0; i < max_shadow_read_write_fields; i++) {
Jim Mattson85fd5142017-07-07 12:51:41 -07003910 unsigned long field = shadow_read_write_fields[i];
3911
3912 clear_bit(field, vmx_vmwrite_bitmap);
3913 clear_bit(field, vmx_vmread_bitmap);
3914 if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64) {
3915 clear_bit(field + 1, vmx_vmwrite_bitmap);
3916 clear_bit(field + 1, vmx_vmread_bitmap);
3917 }
Bandan Dasfe2b2012014-04-21 15:20:14 -04003918 }
Jim Mattson85fd5142017-07-07 12:51:41 -07003919 for (i = 0; i < max_shadow_read_only_fields; i++) {
3920 unsigned long field = shadow_read_only_fields[i];
3921
3922 clear_bit(field, vmx_vmread_bitmap);
3923 if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64)
3924 clear_bit(field + 1, vmx_vmread_bitmap);
3925 }
Bandan Dasfe2b2012014-04-21 15:20:14 -04003926}
3927
Avi Kivity6aa8b732006-12-10 02:21:36 -08003928static __init int alloc_kvm_area(void)
3929{
3930 int cpu;
3931
Zachary Amsden3230bb42009-09-29 11:38:37 -10003932 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003933 struct vmcs *vmcs;
3934
3935 vmcs = alloc_vmcs_cpu(cpu);
3936 if (!vmcs) {
3937 free_kvm_area();
3938 return -ENOMEM;
3939 }
3940
3941 per_cpu(vmxarea, cpu) = vmcs;
3942 }
3943 return 0;
3944}
3945
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003946static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003947 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003948{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003949 if (!emulate_invalid_guest_state) {
3950 /*
3951 * CS and SS RPL should be equal during guest entry according
3952 * to VMX spec, but in reality it is not always so. Since vcpu
3953 * is in the middle of the transition from real mode to
3954 * protected mode it is safe to assume that RPL 0 is a good
3955 * default value.
3956 */
3957 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003958 save->selector &= ~SEGMENT_RPL_MASK;
3959 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003960 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003961 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003962 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003963}
3964
3965static void enter_pmode(struct kvm_vcpu *vcpu)
3966{
3967 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003968 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003969
Gleb Natapovd99e4152012-12-20 16:57:45 +02003970 /*
3971 * Update real mode segment cache. It may be not up-to-date if sement
3972 * register was written while vcpu was in a guest mode.
3973 */
3974 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3975 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3976 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3977 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3978 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3979 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3980
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003981 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003982
Avi Kivity2fb92db2011-04-27 19:42:18 +03003983 vmx_segment_cache_clear(vmx);
3984
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003985 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003986
3987 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003988 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3989 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003990 vmcs_writel(GUEST_RFLAGS, flags);
3991
Rusty Russell66aee912007-07-17 23:34:16 +10003992 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3993 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003994
3995 update_exception_bitmap(vcpu);
3996
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003997 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3998 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3999 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4000 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4001 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4002 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004003}
4004
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004005static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004006{
Mathias Krause772e0312012-08-30 01:30:19 +02004007 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004008 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004009
Gleb Natapovd99e4152012-12-20 16:57:45 +02004010 var.dpl = 0x3;
4011 if (seg == VCPU_SREG_CS)
4012 var.type = 0x3;
4013
4014 if (!emulate_invalid_guest_state) {
4015 var.selector = var.base >> 4;
4016 var.base = var.base & 0xffff0;
4017 var.limit = 0xffff;
4018 var.g = 0;
4019 var.db = 0;
4020 var.present = 1;
4021 var.s = 1;
4022 var.l = 0;
4023 var.unusable = 0;
4024 var.type = 0x3;
4025 var.avl = 0;
4026 if (save->base & 0xf)
4027 printk_once(KERN_WARNING "kvm: segment base is not "
4028 "paragraph aligned when entering "
4029 "protected mode (seg=%d)", seg);
4030 }
4031
4032 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004033 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004034 vmcs_write32(sf->limit, var.limit);
4035 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004036}
4037
4038static void enter_rmode(struct kvm_vcpu *vcpu)
4039{
4040 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004041 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004042
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004043 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4044 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4045 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4046 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4047 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004048 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4049 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004050
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004051 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004052
Gleb Natapov776e58e2011-03-13 12:34:27 +02004053 /*
4054 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004055 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004056 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004057 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004058 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4059 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004060
Avi Kivity2fb92db2011-04-27 19:42:18 +03004061 vmx_segment_cache_clear(vmx);
4062
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004063 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004064 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004065 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4066
4067 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004068 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004069
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004070 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004071
4072 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004073 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004074 update_exception_bitmap(vcpu);
4075
Gleb Natapovd99e4152012-12-20 16:57:45 +02004076 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4077 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4078 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4079 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4080 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4081 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004082
Eddie Dong8668a3c2007-10-10 14:26:45 +08004083 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004084}
4085
Amit Shah401d10d2009-02-20 22:53:37 +05304086static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4087{
4088 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004089 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4090
4091 if (!msr)
4092 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304093
Avi Kivity44ea2b12009-09-06 15:55:37 +03004094 /*
4095 * Force kernel_gs_base reloading before EFER changes, as control
4096 * of this msr depends on is_long_mode().
4097 */
4098 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004099 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304100 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004101 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304102 msr->data = efer;
4103 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004104 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304105
4106 msr->data = efer & ~EFER_LME;
4107 }
4108 setup_msrs(vmx);
4109}
4110
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004111#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004112
4113static void enter_lmode(struct kvm_vcpu *vcpu)
4114{
4115 u32 guest_tr_ar;
4116
Avi Kivity2fb92db2011-04-27 19:42:18 +03004117 vmx_segment_cache_clear(to_vmx(vcpu));
4118
Avi Kivity6aa8b732006-12-10 02:21:36 -08004119 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004120 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004121 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4122 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004123 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004124 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4125 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004126 }
Avi Kivityda38f432010-07-06 11:30:49 +03004127 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004128}
4129
4130static void exit_lmode(struct kvm_vcpu *vcpu)
4131{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004132 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004133 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004134}
4135
4136#endif
4137
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004138static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004139{
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004140 if (enable_ept) {
4141 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4142 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004143 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004144 } else {
4145 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004146 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004147}
4148
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004149static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
4150{
4151 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
4152}
4153
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004154static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4155{
4156 if (enable_ept)
4157 vmx_flush_tlb(vcpu);
4158}
4159
Avi Kivitye8467fd2009-12-29 18:43:06 +02004160static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4161{
4162 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4163
4164 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4165 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4166}
4167
Avi Kivityaff48ba2010-12-05 18:56:11 +02004168static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4169{
4170 if (enable_ept && is_paging(vcpu))
4171 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4172 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4173}
4174
Anthony Liguori25c4c272007-04-27 09:29:21 +03004175static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004176{
Avi Kivityfc78f512009-12-07 12:16:48 +02004177 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4178
4179 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4180 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004181}
4182
Sheng Yang14394422008-04-28 12:24:45 +08004183static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4184{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004185 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4186
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004187 if (!test_bit(VCPU_EXREG_PDPTR,
4188 (unsigned long *)&vcpu->arch.regs_dirty))
4189 return;
4190
Sheng Yang14394422008-04-28 12:24:45 +08004191 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004192 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4193 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4194 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4195 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004196 }
4197}
4198
Avi Kivity8f5d5492009-05-31 18:41:29 +03004199static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4200{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004201 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4202
Avi Kivity8f5d5492009-05-31 18:41:29 +03004203 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004204 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4205 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4206 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4207 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004208 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004209
4210 __set_bit(VCPU_EXREG_PDPTR,
4211 (unsigned long *)&vcpu->arch.regs_avail);
4212 __set_bit(VCPU_EXREG_PDPTR,
4213 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004214}
4215
David Matlack38991522016-11-29 18:14:08 -08004216static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4217{
4218 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4219 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4220 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4221
4222 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
4223 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4224 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4225 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4226
4227 return fixed_bits_valid(val, fixed0, fixed1);
4228}
4229
4230static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4231{
4232 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4233 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4234
4235 return fixed_bits_valid(val, fixed0, fixed1);
4236}
4237
4238static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4239{
4240 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0;
4241 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1;
4242
4243 return fixed_bits_valid(val, fixed0, fixed1);
4244}
4245
4246/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4247#define nested_guest_cr4_valid nested_cr4_valid
4248#define nested_host_cr4_valid nested_cr4_valid
4249
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004250static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004251
4252static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4253 unsigned long cr0,
4254 struct kvm_vcpu *vcpu)
4255{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004256 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4257 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004258 if (!(cr0 & X86_CR0_PG)) {
4259 /* From paging/starting to nonpaging */
4260 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004261 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004262 (CPU_BASED_CR3_LOAD_EXITING |
4263 CPU_BASED_CR3_STORE_EXITING));
4264 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004265 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004266 } else if (!is_paging(vcpu)) {
4267 /* From nonpaging to paging */
4268 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004269 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004270 ~(CPU_BASED_CR3_LOAD_EXITING |
4271 CPU_BASED_CR3_STORE_EXITING));
4272 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004273 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004274 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004275
4276 if (!(cr0 & X86_CR0_WP))
4277 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004278}
4279
Avi Kivity6aa8b732006-12-10 02:21:36 -08004280static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4281{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004282 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004283 unsigned long hw_cr0;
4284
Gleb Natapov50378782013-02-04 16:00:28 +02004285 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004286 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004287 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004288 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004289 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004290
Gleb Natapov218e7632013-01-21 15:36:45 +02004291 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4292 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004293
Gleb Natapov218e7632013-01-21 15:36:45 +02004294 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4295 enter_rmode(vcpu);
4296 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004297
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004298#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004299 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004300 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004301 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004302 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004303 exit_lmode(vcpu);
4304 }
4305#endif
4306
Avi Kivity089d0342009-03-23 18:26:32 +02004307 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004308 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4309
Avi Kivity6aa8b732006-12-10 02:21:36 -08004310 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004311 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004312 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004313
4314 /* depends on vcpu->arch.cr0 to be set to a new value */
4315 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004316}
4317
Yu Zhang855feb62017-08-24 20:27:55 +08004318static int get_ept_level(struct kvm_vcpu *vcpu)
4319{
4320 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4321 return 5;
4322 return 4;
4323}
4324
Peter Feiner995f00a2017-06-30 17:26:32 -07004325static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004326{
Yu Zhang855feb62017-08-24 20:27:55 +08004327 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004328
Yu Zhang855feb62017-08-24 20:27:55 +08004329 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004330
Peter Feiner995f00a2017-06-30 17:26:32 -07004331 if (enable_ept_ad_bits &&
4332 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004333 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004334 eptp |= (root_hpa & PAGE_MASK);
4335
4336 return eptp;
4337}
4338
Avi Kivity6aa8b732006-12-10 02:21:36 -08004339static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4340{
Sheng Yang14394422008-04-28 12:24:45 +08004341 unsigned long guest_cr3;
4342 u64 eptp;
4343
4344 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004345 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004346 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004347 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004348 if (is_paging(vcpu) || is_guest_mode(vcpu))
4349 guest_cr3 = kvm_read_cr3(vcpu);
4350 else
4351 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004352 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004353 }
4354
Sheng Yang2384d2b2008-01-17 15:14:33 +08004355 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004356 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004357}
4358
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004359static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004360{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004361 /*
4362 * Pass through host's Machine Check Enable value to hw_cr4, which
4363 * is in force while we are in guest mode. Do not let guests control
4364 * this bit, even if host CR4.MCE == 0.
4365 */
4366 unsigned long hw_cr4 =
4367 (cr4_read_shadow() & X86_CR4_MCE) |
4368 (cr4 & ~X86_CR4_MCE) |
4369 (to_vmx(vcpu)->rmode.vm86_active ?
4370 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004371
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004372 if (cr4 & X86_CR4_VMXE) {
4373 /*
4374 * To use VMXON (and later other VMX instructions), a guest
4375 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4376 * So basically the check on whether to allow nested VMX
4377 * is here.
4378 */
4379 if (!nested_vmx_allowed(vcpu))
4380 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004381 }
David Matlack38991522016-11-29 18:14:08 -08004382
4383 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004384 return 1;
4385
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004386 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004387 if (enable_ept) {
4388 if (!is_paging(vcpu)) {
4389 hw_cr4 &= ~X86_CR4_PAE;
4390 hw_cr4 |= X86_CR4_PSE;
4391 } else if (!(cr4 & X86_CR4_PAE)) {
4392 hw_cr4 &= ~X86_CR4_PAE;
4393 }
4394 }
Sheng Yang14394422008-04-28 12:24:45 +08004395
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004396 if (!enable_unrestricted_guest && !is_paging(vcpu))
4397 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004398 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4399 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4400 * to be manually disabled when guest switches to non-paging
4401 * mode.
4402 *
4403 * If !enable_unrestricted_guest, the CPU is always running
4404 * with CR0.PG=1 and CR4 needs to be modified.
4405 * If enable_unrestricted_guest, the CPU automatically
4406 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004407 */
Huaitong Handdba2622016-03-22 16:51:15 +08004408 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004409
Sheng Yang14394422008-04-28 12:24:45 +08004410 vmcs_writel(CR4_READ_SHADOW, cr4);
4411 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004412 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004413}
4414
Avi Kivity6aa8b732006-12-10 02:21:36 -08004415static void vmx_get_segment(struct kvm_vcpu *vcpu,
4416 struct kvm_segment *var, int seg)
4417{
Avi Kivitya9179492011-01-03 14:28:52 +02004418 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004419 u32 ar;
4420
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004421 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004422 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004423 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004424 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004425 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004426 var->base = vmx_read_guest_seg_base(vmx, seg);
4427 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4428 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004429 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004430 var->base = vmx_read_guest_seg_base(vmx, seg);
4431 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4432 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4433 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004434 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004435 var->type = ar & 15;
4436 var->s = (ar >> 4) & 1;
4437 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004438 /*
4439 * Some userspaces do not preserve unusable property. Since usable
4440 * segment has to be present according to VMX spec we can use present
4441 * property to amend userspace bug by making unusable segment always
4442 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4443 * segment as unusable.
4444 */
4445 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004446 var->avl = (ar >> 12) & 1;
4447 var->l = (ar >> 13) & 1;
4448 var->db = (ar >> 14) & 1;
4449 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004450}
4451
Avi Kivitya9179492011-01-03 14:28:52 +02004452static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4453{
Avi Kivitya9179492011-01-03 14:28:52 +02004454 struct kvm_segment s;
4455
4456 if (to_vmx(vcpu)->rmode.vm86_active) {
4457 vmx_get_segment(vcpu, &s, seg);
4458 return s.base;
4459 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004460 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004461}
4462
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004463static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004464{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004465 struct vcpu_vmx *vmx = to_vmx(vcpu);
4466
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004467 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004468 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004469 else {
4470 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004471 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004472 }
Avi Kivity69c73022011-03-07 15:26:44 +02004473}
4474
Avi Kivity653e3102007-05-07 10:55:37 +03004475static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004476{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004477 u32 ar;
4478
Avi Kivityf0495f92012-06-07 17:06:10 +03004479 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004480 ar = 1 << 16;
4481 else {
4482 ar = var->type & 15;
4483 ar |= (var->s & 1) << 4;
4484 ar |= (var->dpl & 3) << 5;
4485 ar |= (var->present & 1) << 7;
4486 ar |= (var->avl & 1) << 12;
4487 ar |= (var->l & 1) << 13;
4488 ar |= (var->db & 1) << 14;
4489 ar |= (var->g & 1) << 15;
4490 }
Avi Kivity653e3102007-05-07 10:55:37 +03004491
4492 return ar;
4493}
4494
4495static void vmx_set_segment(struct kvm_vcpu *vcpu,
4496 struct kvm_segment *var, int seg)
4497{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004498 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004499 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004500
Avi Kivity2fb92db2011-04-27 19:42:18 +03004501 vmx_segment_cache_clear(vmx);
4502
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004503 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4504 vmx->rmode.segs[seg] = *var;
4505 if (seg == VCPU_SREG_TR)
4506 vmcs_write16(sf->selector, var->selector);
4507 else if (var->s)
4508 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004509 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004510 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004511
Avi Kivity653e3102007-05-07 10:55:37 +03004512 vmcs_writel(sf->base, var->base);
4513 vmcs_write32(sf->limit, var->limit);
4514 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004515
4516 /*
4517 * Fix the "Accessed" bit in AR field of segment registers for older
4518 * qemu binaries.
4519 * IA32 arch specifies that at the time of processor reset the
4520 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004521 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004522 * state vmexit when "unrestricted guest" mode is turned on.
4523 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4524 * tree. Newer qemu binaries with that qemu fix would not need this
4525 * kvm hack.
4526 */
4527 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004528 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004529
Gleb Natapovf924d662012-12-12 19:10:55 +02004530 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004531
4532out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004533 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004534}
4535
Avi Kivity6aa8b732006-12-10 02:21:36 -08004536static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4537{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004538 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004539
4540 *db = (ar >> 14) & 1;
4541 *l = (ar >> 13) & 1;
4542}
4543
Gleb Natapov89a27f42010-02-16 10:51:48 +02004544static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004545{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004546 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4547 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004548}
4549
Gleb Natapov89a27f42010-02-16 10:51:48 +02004550static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004551{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004552 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4553 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004554}
4555
Gleb Natapov89a27f42010-02-16 10:51:48 +02004556static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004557{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004558 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4559 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004560}
4561
Gleb Natapov89a27f42010-02-16 10:51:48 +02004562static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004563{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004564 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4565 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004566}
4567
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004568static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4569{
4570 struct kvm_segment var;
4571 u32 ar;
4572
4573 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004574 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004575 if (seg == VCPU_SREG_CS)
4576 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004577 ar = vmx_segment_access_rights(&var);
4578
4579 if (var.base != (var.selector << 4))
4580 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004581 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004582 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004583 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004584 return false;
4585
4586 return true;
4587}
4588
4589static bool code_segment_valid(struct kvm_vcpu *vcpu)
4590{
4591 struct kvm_segment cs;
4592 unsigned int cs_rpl;
4593
4594 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004595 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004596
Avi Kivity1872a3f2009-01-04 23:26:52 +02004597 if (cs.unusable)
4598 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004599 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004600 return false;
4601 if (!cs.s)
4602 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004603 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004604 if (cs.dpl > cs_rpl)
4605 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004606 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004607 if (cs.dpl != cs_rpl)
4608 return false;
4609 }
4610 if (!cs.present)
4611 return false;
4612
4613 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4614 return true;
4615}
4616
4617static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4618{
4619 struct kvm_segment ss;
4620 unsigned int ss_rpl;
4621
4622 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004623 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004624
Avi Kivity1872a3f2009-01-04 23:26:52 +02004625 if (ss.unusable)
4626 return true;
4627 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004628 return false;
4629 if (!ss.s)
4630 return false;
4631 if (ss.dpl != ss_rpl) /* DPL != RPL */
4632 return false;
4633 if (!ss.present)
4634 return false;
4635
4636 return true;
4637}
4638
4639static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4640{
4641 struct kvm_segment var;
4642 unsigned int rpl;
4643
4644 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004645 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004646
Avi Kivity1872a3f2009-01-04 23:26:52 +02004647 if (var.unusable)
4648 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004649 if (!var.s)
4650 return false;
4651 if (!var.present)
4652 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004653 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004654 if (var.dpl < rpl) /* DPL < RPL */
4655 return false;
4656 }
4657
4658 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4659 * rights flags
4660 */
4661 return true;
4662}
4663
4664static bool tr_valid(struct kvm_vcpu *vcpu)
4665{
4666 struct kvm_segment tr;
4667
4668 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4669
Avi Kivity1872a3f2009-01-04 23:26:52 +02004670 if (tr.unusable)
4671 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004672 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004673 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004674 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004675 return false;
4676 if (!tr.present)
4677 return false;
4678
4679 return true;
4680}
4681
4682static bool ldtr_valid(struct kvm_vcpu *vcpu)
4683{
4684 struct kvm_segment ldtr;
4685
4686 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4687
Avi Kivity1872a3f2009-01-04 23:26:52 +02004688 if (ldtr.unusable)
4689 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004690 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004691 return false;
4692 if (ldtr.type != 2)
4693 return false;
4694 if (!ldtr.present)
4695 return false;
4696
4697 return true;
4698}
4699
4700static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4701{
4702 struct kvm_segment cs, ss;
4703
4704 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4705 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4706
Nadav Amitb32a9912015-03-29 16:33:04 +03004707 return ((cs.selector & SEGMENT_RPL_MASK) ==
4708 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004709}
4710
4711/*
4712 * Check if guest state is valid. Returns true if valid, false if
4713 * not.
4714 * We assume that registers are always usable
4715 */
4716static bool guest_state_valid(struct kvm_vcpu *vcpu)
4717{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004718 if (enable_unrestricted_guest)
4719 return true;
4720
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004721 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004722 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004723 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4724 return false;
4725 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4726 return false;
4727 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4728 return false;
4729 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4730 return false;
4731 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4732 return false;
4733 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4734 return false;
4735 } else {
4736 /* protected mode guest state checks */
4737 if (!cs_ss_rpl_check(vcpu))
4738 return false;
4739 if (!code_segment_valid(vcpu))
4740 return false;
4741 if (!stack_segment_valid(vcpu))
4742 return false;
4743 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4744 return false;
4745 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4746 return false;
4747 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4748 return false;
4749 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4750 return false;
4751 if (!tr_valid(vcpu))
4752 return false;
4753 if (!ldtr_valid(vcpu))
4754 return false;
4755 }
4756 /* TODO:
4757 * - Add checks on RIP
4758 * - Add checks on RFLAGS
4759 */
4760
4761 return true;
4762}
4763
Jim Mattson5fa99cb2017-07-06 16:33:07 -07004764static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
4765{
4766 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
4767}
4768
Mike Dayd77c26f2007-10-08 09:02:08 -04004769static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004770{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004771 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004772 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004773 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004774
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004775 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004776 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004777 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4778 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004779 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004780 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004781 r = kvm_write_guest_page(kvm, fn++, &data,
4782 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004783 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004784 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004785 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4786 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004787 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004788 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4789 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004790 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004791 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004792 r = kvm_write_guest_page(kvm, fn, &data,
4793 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4794 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004795out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004796 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004797 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004798}
4799
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004800static int init_rmode_identity_map(struct kvm *kvm)
4801{
Tang Chenf51770e2014-09-16 18:41:59 +08004802 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004803 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004804 u32 tmp;
4805
Tang Chena255d472014-09-16 18:41:58 +08004806 /* Protect kvm->arch.ept_identity_pagetable_done. */
4807 mutex_lock(&kvm->slots_lock);
4808
Tang Chenf51770e2014-09-16 18:41:59 +08004809 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004810 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004811
David Hildenbrandd8a6e362017-08-24 20:51:34 +02004812 if (!kvm->arch.ept_identity_map_addr)
4813 kvm->arch.ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004814 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004815
David Hildenbrandd8a6e362017-08-24 20:51:34 +02004816 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4817 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08004818 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004819 goto out2;
4820
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004821 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004822 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4823 if (r < 0)
4824 goto out;
4825 /* Set up identity-mapping pagetable for EPT in real mode */
4826 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4827 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4828 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4829 r = kvm_write_guest_page(kvm, identity_map_pfn,
4830 &tmp, i * sizeof(tmp), sizeof(tmp));
4831 if (r < 0)
4832 goto out;
4833 }
4834 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004835
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004836out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004837 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004838
4839out2:
4840 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004841 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004842}
4843
Avi Kivity6aa8b732006-12-10 02:21:36 -08004844static void seg_setup(int seg)
4845{
Mathias Krause772e0312012-08-30 01:30:19 +02004846 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004847 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004848
4849 vmcs_write16(sf->selector, 0);
4850 vmcs_writel(sf->base, 0);
4851 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004852 ar = 0x93;
4853 if (seg == VCPU_SREG_CS)
4854 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004855
4856 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004857}
4858
Sheng Yangf78e0e22007-10-29 09:40:42 +08004859static int alloc_apic_access_page(struct kvm *kvm)
4860{
Xiao Guangrong44841412012-09-07 14:14:20 +08004861 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004862 int r = 0;
4863
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004864 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004865 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004866 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004867 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4868 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004869 if (r)
4870 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004871
Tang Chen73a6d942014-09-11 13:38:00 +08004872 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004873 if (is_error_page(page)) {
4874 r = -EFAULT;
4875 goto out;
4876 }
4877
Tang Chenc24ae0d2014-09-24 15:57:58 +08004878 /*
4879 * Do not pin the page in memory, so that memory hot-unplug
4880 * is able to migrate it.
4881 */
4882 put_page(page);
4883 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004884out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004885 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004886 return r;
4887}
4888
Wanpeng Li991e7a02015-09-16 17:30:05 +08004889static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004890{
4891 int vpid;
4892
Avi Kivity919818a2009-03-23 18:01:29 +02004893 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004894 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004895 spin_lock(&vmx_vpid_lock);
4896 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004897 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004898 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004899 else
4900 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004901 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004902 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004903}
4904
Wanpeng Li991e7a02015-09-16 17:30:05 +08004905static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004906{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004907 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004908 return;
4909 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004910 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004911 spin_unlock(&vmx_vpid_lock);
4912}
4913
Yang Zhang8d146952013-01-25 10:18:50 +08004914#define MSR_TYPE_R 1
4915#define MSR_TYPE_W 2
4916static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4917 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004918{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004919 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004920
4921 if (!cpu_has_vmx_msr_bitmap())
4922 return;
4923
4924 /*
4925 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4926 * have the write-low and read-high bitmap offsets the wrong way round.
4927 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4928 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004929 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004930 if (type & MSR_TYPE_R)
4931 /* read-low */
4932 __clear_bit(msr, msr_bitmap + 0x000 / f);
4933
4934 if (type & MSR_TYPE_W)
4935 /* write-low */
4936 __clear_bit(msr, msr_bitmap + 0x800 / f);
4937
Sheng Yang25c5f222008-03-28 13:18:56 +08004938 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4939 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004940 if (type & MSR_TYPE_R)
4941 /* read-high */
4942 __clear_bit(msr, msr_bitmap + 0x400 / f);
4943
4944 if (type & MSR_TYPE_W)
4945 /* write-high */
4946 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4947
4948 }
4949}
4950
Wincy Vanf2b93282015-02-03 23:56:03 +08004951/*
4952 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4953 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4954 */
4955static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4956 unsigned long *msr_bitmap_nested,
4957 u32 msr, int type)
4958{
4959 int f = sizeof(unsigned long);
4960
4961 if (!cpu_has_vmx_msr_bitmap()) {
4962 WARN_ON(1);
4963 return;
4964 }
4965
4966 /*
4967 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4968 * have the write-low and read-high bitmap offsets the wrong way round.
4969 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4970 */
4971 if (msr <= 0x1fff) {
4972 if (type & MSR_TYPE_R &&
4973 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4974 /* read-low */
4975 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4976
4977 if (type & MSR_TYPE_W &&
4978 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4979 /* write-low */
4980 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4981
4982 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4983 msr &= 0x1fff;
4984 if (type & MSR_TYPE_R &&
4985 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4986 /* read-high */
4987 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4988
4989 if (type & MSR_TYPE_W &&
4990 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4991 /* write-high */
4992 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4993
4994 }
4995}
4996
Avi Kivity58972972009-02-24 22:26:47 +02004997static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
4998{
4999 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08005000 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
5001 msr, MSR_TYPE_R | MSR_TYPE_W);
5002 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
5003 msr, MSR_TYPE_R | MSR_TYPE_W);
5004}
5005
Radim Krčmář2e69f862016-09-29 22:41:32 +02005006static void vmx_disable_intercept_msr_x2apic(u32 msr, int type, bool apicv_active)
Yang Zhang8d146952013-01-25 10:18:50 +08005007{
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005008 if (apicv_active) {
Wanpeng Lic63e4562016-09-23 19:17:16 +08005009 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv,
Radim Krčmář2e69f862016-09-29 22:41:32 +02005010 msr, type);
Wanpeng Lic63e4562016-09-23 19:17:16 +08005011 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv,
Radim Krčmář2e69f862016-09-29 22:41:32 +02005012 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005013 } else {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005014 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
Radim Krčmář2e69f862016-09-29 22:41:32 +02005015 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005016 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
Radim Krčmář2e69f862016-09-29 22:41:32 +02005017 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005018 }
Avi Kivity58972972009-02-24 22:26:47 +02005019}
5020
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005021static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005022{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005023 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005024}
5025
David Matlackc9f04402017-08-01 14:00:40 -07005026static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5027{
5028 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5029 gfn_t gfn;
5030
5031 /*
5032 * Don't need to mark the APIC access page dirty; it is never
5033 * written to by the CPU during APIC virtualization.
5034 */
5035
5036 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5037 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5038 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5039 }
5040
5041 if (nested_cpu_has_posted_intr(vmcs12)) {
5042 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5043 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5044 }
5045}
5046
5047
David Hildenbrand6342c502017-01-25 11:58:58 +01005048static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005049{
5050 struct vcpu_vmx *vmx = to_vmx(vcpu);
5051 int max_irr;
5052 void *vapic_page;
5053 u16 status;
5054
David Matlackc9f04402017-08-01 14:00:40 -07005055 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5056 return;
Wincy Van705699a2015-02-03 23:58:17 +08005057
David Matlackc9f04402017-08-01 14:00:40 -07005058 vmx->nested.pi_pending = false;
5059 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5060 return;
Wincy Van705699a2015-02-03 23:58:17 +08005061
David Matlackc9f04402017-08-01 14:00:40 -07005062 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5063 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005064 vapic_page = kmap(vmx->nested.virtual_apic_page);
Wincy Van705699a2015-02-03 23:58:17 +08005065 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
5066 kunmap(vmx->nested.virtual_apic_page);
5067
5068 status = vmcs_read16(GUEST_INTR_STATUS);
5069 if ((u8)max_irr > ((u8)status & 0xff)) {
5070 status &= ~0xff;
5071 status |= (u8)max_irr;
5072 vmcs_write16(GUEST_INTR_STATUS, status);
5073 }
5074 }
David Matlackc9f04402017-08-01 14:00:40 -07005075
5076 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005077}
5078
Wincy Van06a55242017-04-28 13:13:59 +08005079static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5080 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005081{
5082#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005083 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5084
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005085 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005086 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005087 * The vector of interrupt to be delivered to vcpu had
5088 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005089 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005090 * Following cases will be reached in this block, and
5091 * we always send a notification event in all cases as
5092 * explained below.
5093 *
5094 * Case 1: vcpu keeps in non-root mode. Sending a
5095 * notification event posts the interrupt to vcpu.
5096 *
5097 * Case 2: vcpu exits to root mode and is still
5098 * runnable. PIR will be synced to vIRR before the
5099 * next vcpu entry. Sending a notification event in
5100 * this case has no effect, as vcpu is not in root
5101 * mode.
5102 *
5103 * Case 3: vcpu exits to root mode and is blocked.
5104 * vcpu_block() has already synced PIR to vIRR and
5105 * never blocks vcpu if vIRR is not cleared. Therefore,
5106 * a blocked vcpu here does not wait for any requested
5107 * interrupts in PIR, and sending a notification event
5108 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005109 */
Feng Wu28b835d2015-09-18 22:29:54 +08005110
Wincy Van06a55242017-04-28 13:13:59 +08005111 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005112 return true;
5113 }
5114#endif
5115 return false;
5116}
5117
Wincy Van705699a2015-02-03 23:58:17 +08005118static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5119 int vector)
5120{
5121 struct vcpu_vmx *vmx = to_vmx(vcpu);
5122
5123 if (is_guest_mode(vcpu) &&
5124 vector == vmx->nested.posted_intr_nv) {
5125 /* the PIR and ON have been set by L1. */
Wincy Van06a55242017-04-28 13:13:59 +08005126 kvm_vcpu_trigger_posted_interrupt(vcpu, true);
Wincy Van705699a2015-02-03 23:58:17 +08005127 /*
5128 * If a posted intr is not recognized by hardware,
5129 * we will accomplish it in the next vmentry.
5130 */
5131 vmx->nested.pi_pending = true;
5132 kvm_make_request(KVM_REQ_EVENT, vcpu);
5133 return 0;
5134 }
5135 return -1;
5136}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005137/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005138 * Send interrupt to vcpu via posted interrupt way.
5139 * 1. If target vcpu is running(non-root mode), send posted interrupt
5140 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5141 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5142 * interrupt from PIR in next vmentry.
5143 */
5144static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5145{
5146 struct vcpu_vmx *vmx = to_vmx(vcpu);
5147 int r;
5148
Wincy Van705699a2015-02-03 23:58:17 +08005149 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5150 if (!r)
5151 return;
5152
Yang Zhanga20ed542013-04-11 19:25:15 +08005153 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5154 return;
5155
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005156 /* If a previous notification has sent the IPI, nothing to do. */
5157 if (pi_test_and_set_on(&vmx->pi_desc))
5158 return;
5159
Wincy Van06a55242017-04-28 13:13:59 +08005160 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005161 kvm_vcpu_kick(vcpu);
5162}
5163
Avi Kivity6aa8b732006-12-10 02:21:36 -08005164/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005165 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5166 * will not change in the lifetime of the guest.
5167 * Note that host-state that does change is set elsewhere. E.g., host-state
5168 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5169 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005170static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005171{
5172 u32 low32, high32;
5173 unsigned long tmpl;
5174 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005175 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005176
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005177 cr0 = read_cr0();
5178 WARN_ON(cr0 & X86_CR0_TS);
5179 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005180
5181 /*
5182 * Save the most likely value for this task's CR3 in the VMCS.
5183 * We can't use __get_current_cr3_fast() because we're not atomic.
5184 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005185 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005186 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005187 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005188
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005189 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005190 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005191 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005192 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005193
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005194 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005195#ifdef CONFIG_X86_64
5196 /*
5197 * Load null selectors, so we can avoid reloading them in
5198 * __vmx_load_host_state(), in case userspace uses the null selectors
5199 * too (the expected case).
5200 */
5201 vmcs_write16(HOST_DS_SELECTOR, 0);
5202 vmcs_write16(HOST_ES_SELECTOR, 0);
5203#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005204 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5205 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005206#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005207 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5208 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5209
Juergen Gross87930012017-09-04 12:25:27 +02005210 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005211 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005212 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005213
Avi Kivity83287ea422012-09-16 15:10:57 +03005214 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005215
5216 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5217 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5218 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5219 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5220
5221 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5222 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5223 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5224 }
5225}
5226
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005227static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5228{
5229 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5230 if (enable_ept)
5231 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005232 if (is_guest_mode(&vmx->vcpu))
5233 vmx->vcpu.arch.cr4_guest_owned_bits &=
5234 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005235 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5236}
5237
Yang Zhang01e439b2013-04-11 19:25:12 +08005238static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5239{
5240 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5241
Andrey Smetanind62caab2015-11-10 15:36:33 +03005242 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005243 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005244
5245 if (!enable_vnmi)
5246 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5247
Yunhong Jiang64672c92016-06-13 14:19:59 -07005248 /* Enable the preemption timer dynamically */
5249 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005250 return pin_based_exec_ctrl;
5251}
5252
Andrey Smetanind62caab2015-11-10 15:36:33 +03005253static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5254{
5255 struct vcpu_vmx *vmx = to_vmx(vcpu);
5256
5257 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005258 if (cpu_has_secondary_exec_ctrls()) {
5259 if (kvm_vcpu_apicv_active(vcpu))
5260 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5261 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5262 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5263 else
5264 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5265 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5266 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5267 }
5268
5269 if (cpu_has_vmx_msr_bitmap())
5270 vmx_set_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005271}
5272
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005273static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5274{
5275 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005276
5277 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5278 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5279
Paolo Bonzini35754c92015-07-29 12:05:37 +02005280 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005281 exec_control &= ~CPU_BASED_TPR_SHADOW;
5282#ifdef CONFIG_X86_64
5283 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5284 CPU_BASED_CR8_LOAD_EXITING;
5285#endif
5286 }
5287 if (!enable_ept)
5288 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5289 CPU_BASED_CR3_LOAD_EXITING |
5290 CPU_BASED_INVLPG_EXITING;
5291 return exec_control;
5292}
5293
Jim Mattson45ec3682017-08-23 16:32:04 -07005294static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005295{
Jim Mattson45ec3682017-08-23 16:32:04 -07005296 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005297 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005298}
5299
Jim Mattson75f4fc82017-08-23 16:32:03 -07005300static bool vmx_rdseed_supported(void)
5301{
5302 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005303 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005304}
5305
Paolo Bonzini80154d72017-08-24 13:55:35 +02005306static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005307{
Paolo Bonzini80154d72017-08-24 13:55:35 +02005308 struct kvm_vcpu *vcpu = &vmx->vcpu;
5309
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005310 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini80154d72017-08-24 13:55:35 +02005311 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005312 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5313 if (vmx->vpid == 0)
5314 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5315 if (!enable_ept) {
5316 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5317 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005318 /* Enable INVPCID for non-ept guests may cause performance regression. */
5319 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005320 }
5321 if (!enable_unrestricted_guest)
5322 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5323 if (!ple_gap)
5324 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02005325 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005326 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5327 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005328 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03005329 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5330 (handle_vmptrld).
5331 We can NOT enable shadow_vmcs here because we don't have yet
5332 a current VMCS12
5333 */
5334 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005335
5336 if (!enable_pml)
5337 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005338
Paolo Bonzini3db13482017-08-24 14:48:03 +02005339 if (vmx_xsaves_supported()) {
5340 /* Exposing XSAVES only when XSAVE is exposed */
5341 bool xsaves_enabled =
5342 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5343 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5344
5345 if (!xsaves_enabled)
5346 exec_control &= ~SECONDARY_EXEC_XSAVES;
5347
5348 if (nested) {
5349 if (xsaves_enabled)
5350 vmx->nested.nested_vmx_secondary_ctls_high |=
5351 SECONDARY_EXEC_XSAVES;
5352 else
5353 vmx->nested.nested_vmx_secondary_ctls_high &=
5354 ~SECONDARY_EXEC_XSAVES;
5355 }
5356 }
5357
Paolo Bonzini80154d72017-08-24 13:55:35 +02005358 if (vmx_rdtscp_supported()) {
5359 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5360 if (!rdtscp_enabled)
5361 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5362
5363 if (nested) {
5364 if (rdtscp_enabled)
5365 vmx->nested.nested_vmx_secondary_ctls_high |=
5366 SECONDARY_EXEC_RDTSCP;
5367 else
5368 vmx->nested.nested_vmx_secondary_ctls_high &=
5369 ~SECONDARY_EXEC_RDTSCP;
5370 }
5371 }
5372
5373 if (vmx_invpcid_supported()) {
5374 /* Exposing INVPCID only when PCID is exposed */
5375 bool invpcid_enabled =
5376 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5377 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5378
5379 if (!invpcid_enabled) {
5380 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5381 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5382 }
5383
5384 if (nested) {
5385 if (invpcid_enabled)
5386 vmx->nested.nested_vmx_secondary_ctls_high |=
5387 SECONDARY_EXEC_ENABLE_INVPCID;
5388 else
5389 vmx->nested.nested_vmx_secondary_ctls_high &=
5390 ~SECONDARY_EXEC_ENABLE_INVPCID;
5391 }
5392 }
5393
Jim Mattson45ec3682017-08-23 16:32:04 -07005394 if (vmx_rdrand_supported()) {
5395 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5396 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005397 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005398
5399 if (nested) {
5400 if (rdrand_enabled)
5401 vmx->nested.nested_vmx_secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005402 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005403 else
5404 vmx->nested.nested_vmx_secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005405 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005406 }
5407 }
5408
Jim Mattson75f4fc82017-08-23 16:32:03 -07005409 if (vmx_rdseed_supported()) {
5410 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5411 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005412 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005413
5414 if (nested) {
5415 if (rdseed_enabled)
5416 vmx->nested.nested_vmx_secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005417 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005418 else
5419 vmx->nested.nested_vmx_secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005420 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005421 }
5422 }
5423
Paolo Bonzini80154d72017-08-24 13:55:35 +02005424 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005425}
5426
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005427static void ept_set_mmio_spte_mask(void)
5428{
5429 /*
5430 * EPT Misconfigurations can be generated if the value of bits 2:0
5431 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005432 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07005433 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5434 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005435}
5436
Wanpeng Lif53cd632014-12-02 19:14:58 +08005437#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005438/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005439 * Sets up the vmcs for emulated real mode.
5440 */
David Hildenbrand12d79912017-08-24 20:51:26 +02005441static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005442{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005443#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005444 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005445#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005446 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005447
Avi Kivity6aa8b732006-12-10 02:21:36 -08005448 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005449 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5450 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005451
Abel Gordon4607c2d2013-04-18 14:35:55 +03005452 if (enable_shadow_vmcs) {
5453 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5454 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5455 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005456 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02005457 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08005458
Avi Kivity6aa8b732006-12-10 02:21:36 -08005459 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5460
Avi Kivity6aa8b732006-12-10 02:21:36 -08005461 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005462 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005463 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005464
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005465 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005466
Dan Williamsdfa169b2016-06-02 11:17:24 -07005467 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02005468 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005469 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02005470 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07005471 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005472
Andrey Smetanind62caab2015-11-10 15:36:33 +03005473 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005474 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5475 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5476 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5477 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5478
5479 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005480
Li RongQing0bcf2612015-12-03 13:29:34 +08005481 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005482 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005483 }
5484
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005485 if (ple_gap) {
5486 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005487 vmx->ple_window = ple_window;
5488 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005489 }
5490
Xiao Guangrongc3707952011-07-12 03:28:04 +08005491 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5492 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005493 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5494
Avi Kivity9581d442010-10-19 16:46:55 +02005495 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5496 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005497 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005498#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005499 rdmsrl(MSR_FS_BASE, a);
5500 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5501 rdmsrl(MSR_GS_BASE, a);
5502 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5503#else
5504 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5505 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5506#endif
5507
Bandan Das2a499e42017-08-03 15:54:41 -04005508 if (cpu_has_vmx_vmfunc())
5509 vmcs_write64(VM_FUNCTION_CONTROL, 0);
5510
Eddie Dong2cc51562007-05-21 07:28:09 +03005511 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5512 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005513 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005514 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005515 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005516
Radim Krčmář74545702015-04-27 15:11:25 +02005517 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5518 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005519
Paolo Bonzini03916db2014-07-24 14:21:57 +02005520 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005521 u32 index = vmx_msr_index[i];
5522 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005523 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005524
5525 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5526 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005527 if (wrmsr_safe(index, data_low, data_high) < 0)
5528 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005529 vmx->guest_msrs[j].index = i;
5530 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005531 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005532 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005533 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005534
Gleb Natapov2961e8762013-11-25 15:37:13 +02005535
5536 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005537
5538 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005539 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005540
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005541 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5542 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5543
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005544 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005545
Wanpeng Lif53cd632014-12-02 19:14:58 +08005546 if (vmx_xsaves_supported())
5547 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5548
Peter Feiner4e595162016-07-07 14:49:58 -07005549 if (enable_pml) {
5550 ASSERT(vmx->pml_pg);
5551 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5552 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5553 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005554}
5555
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005556static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005557{
5558 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005559 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005560 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005561
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005562 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005563
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005564 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005565 kvm_set_cr8(vcpu, 0);
5566
5567 if (!init_event) {
5568 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5569 MSR_IA32_APICBASE_ENABLE;
5570 if (kvm_vcpu_is_reset_bsp(vcpu))
5571 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5572 apic_base_msr.host_initiated = true;
5573 kvm_set_apic_base(vcpu, &apic_base_msr);
5574 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005575
Avi Kivity2fb92db2011-04-27 19:42:18 +03005576 vmx_segment_cache_clear(vmx);
5577
Avi Kivity5706be02008-08-20 15:07:31 +03005578 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005579 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005580 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005581
5582 seg_setup(VCPU_SREG_DS);
5583 seg_setup(VCPU_SREG_ES);
5584 seg_setup(VCPU_SREG_FS);
5585 seg_setup(VCPU_SREG_GS);
5586 seg_setup(VCPU_SREG_SS);
5587
5588 vmcs_write16(GUEST_TR_SELECTOR, 0);
5589 vmcs_writel(GUEST_TR_BASE, 0);
5590 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5591 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5592
5593 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5594 vmcs_writel(GUEST_LDTR_BASE, 0);
5595 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5596 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5597
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005598 if (!init_event) {
5599 vmcs_write32(GUEST_SYSENTER_CS, 0);
5600 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5601 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5602 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5603 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005604
5605 vmcs_writel(GUEST_RFLAGS, 0x02);
Jan Kiszka66450a22013-03-13 12:42:34 +01005606 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005607
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005608 vmcs_writel(GUEST_GDTR_BASE, 0);
5609 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5610
5611 vmcs_writel(GUEST_IDTR_BASE, 0);
5612 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5613
Anthony Liguori443381a2010-12-06 10:53:38 -06005614 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005615 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005616 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07005617 if (kvm_mpx_supported())
5618 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005619
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005620 setup_msrs(vmx);
5621
Avi Kivity6aa8b732006-12-10 02:21:36 -08005622 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5623
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005624 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005625 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005626 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005627 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005628 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005629 vmcs_write32(TPR_THRESHOLD, 0);
5630 }
5631
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005632 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005633
Sheng Yang2384d2b2008-01-17 15:14:33 +08005634 if (vmx->vpid != 0)
5635 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5636
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005637 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005638 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005639 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005640 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005641 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005642
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005643 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005644
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005645 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005646}
5647
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005648/*
5649 * In nested virtualization, check if L1 asked to exit on external interrupts.
5650 * For most existing hypervisors, this will always return true.
5651 */
5652static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5653{
5654 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5655 PIN_BASED_EXT_INTR_MASK;
5656}
5657
Bandan Das77b0f5d2014-04-19 18:17:45 -04005658/*
5659 * In nested virtualization, check if L1 has set
5660 * VM_EXIT_ACK_INTR_ON_EXIT
5661 */
5662static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5663{
5664 return get_vmcs12(vcpu)->vm_exit_controls &
5665 VM_EXIT_ACK_INTR_ON_EXIT;
5666}
5667
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005668static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5669{
5670 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5671 PIN_BASED_NMI_EXITING;
5672}
5673
Jan Kiszkac9a79532014-03-07 20:03:15 +01005674static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005675{
Paolo Bonzini47c01522016-12-19 11:44:07 +01005676 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5677 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005678}
5679
Jan Kiszkac9a79532014-03-07 20:03:15 +01005680static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005681{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005682 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005683 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01005684 enable_irq_window(vcpu);
5685 return;
5686 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005687
Paolo Bonzini47c01522016-12-19 11:44:07 +01005688 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5689 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005690}
5691
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005692static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005693{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005694 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005695 uint32_t intr;
5696 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005697
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005698 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005699
Avi Kivityfa89a812008-09-01 15:57:51 +03005700 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005701 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005702 int inc_eip = 0;
5703 if (vcpu->arch.interrupt.soft)
5704 inc_eip = vcpu->arch.event_exit_inst_len;
5705 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005706 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005707 return;
5708 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005709 intr = irq | INTR_INFO_VALID_MASK;
5710 if (vcpu->arch.interrupt.soft) {
5711 intr |= INTR_TYPE_SOFT_INTR;
5712 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5713 vmx->vcpu.arch.event_exit_inst_len);
5714 } else
5715 intr |= INTR_TYPE_EXT_INTR;
5716 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005717}
5718
Sheng Yangf08864b2008-05-15 18:23:25 +08005719static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5720{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005721 struct vcpu_vmx *vmx = to_vmx(vcpu);
5722
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005723 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005724 /*
5725 * Tracking the NMI-blocked state in software is built upon
5726 * finding the next open IRQ window. This, in turn, depends on
5727 * well-behaving guests: They have to keep IRQs disabled at
5728 * least as long as the NMI handler runs. Otherwise we may
5729 * cause NMI nesting, maybe breaking the guest. But as this is
5730 * highly unlikely, we can live with the residual risk.
5731 */
5732 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
5733 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5734 }
5735
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005736 ++vcpu->stat.nmi_injections;
5737 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005738
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005739 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005740 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005741 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005742 return;
5743 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005744
Sheng Yangf08864b2008-05-15 18:23:25 +08005745 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5746 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005747}
5748
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005749static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5750{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005751 struct vcpu_vmx *vmx = to_vmx(vcpu);
5752 bool masked;
5753
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005754 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005755 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005756 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02005757 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005758 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
5759 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5760 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005761}
5762
5763static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5764{
5765 struct vcpu_vmx *vmx = to_vmx(vcpu);
5766
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005767 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005768 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
5769 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
5770 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5771 }
5772 } else {
5773 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5774 if (masked)
5775 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5776 GUEST_INTR_STATE_NMI);
5777 else
5778 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5779 GUEST_INTR_STATE_NMI);
5780 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005781}
5782
Jan Kiszka2505dc92013-04-14 12:12:47 +02005783static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5784{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005785 if (to_vmx(vcpu)->nested.nested_run_pending)
5786 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005787
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005788 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005789 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
5790 return 0;
5791
Jan Kiszka2505dc92013-04-14 12:12:47 +02005792 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5793 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5794 | GUEST_INTR_STATE_NMI));
5795}
5796
Gleb Natapov78646122009-03-23 12:12:11 +02005797static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5798{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005799 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5800 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005801 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5802 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005803}
5804
Izik Eiduscbc94022007-10-25 00:29:55 +02005805static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5806{
5807 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005808
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005809 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5810 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005811 if (ret)
5812 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005813 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005814 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005815}
5816
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005817static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005818{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005819 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005820 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005821 /*
5822 * Update instruction length as we may reinject the exception
5823 * from user space while in guest debugging mode.
5824 */
5825 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5826 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005827 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005828 return false;
5829 /* fall through */
5830 case DB_VECTOR:
5831 if (vcpu->guest_debug &
5832 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5833 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005834 /* fall through */
5835 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005836 case OF_VECTOR:
5837 case BR_VECTOR:
5838 case UD_VECTOR:
5839 case DF_VECTOR:
5840 case SS_VECTOR:
5841 case GP_VECTOR:
5842 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005843 return true;
5844 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005845 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005846 return false;
5847}
5848
5849static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5850 int vec, u32 err_code)
5851{
5852 /*
5853 * Instruction with address size override prefix opcode 0x67
5854 * Cause the #SS fault with 0 error code in VM86 mode.
5855 */
5856 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5857 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5858 if (vcpu->arch.halt_request) {
5859 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005860 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005861 }
5862 return 1;
5863 }
5864 return 0;
5865 }
5866
5867 /*
5868 * Forward all other exceptions that are valid in real mode.
5869 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5870 * the required debugging infrastructure rework.
5871 */
5872 kvm_queue_exception(vcpu, vec);
5873 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005874}
5875
Andi Kleena0861c02009-06-08 17:37:09 +08005876/*
5877 * Trigger machine check on the host. We assume all the MSRs are already set up
5878 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5879 * We pass a fake environment to the machine check handler because we want
5880 * the guest to be always treated like user space, no matter what context
5881 * it used internally.
5882 */
5883static void kvm_machine_check(void)
5884{
5885#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5886 struct pt_regs regs = {
5887 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5888 .flags = X86_EFLAGS_IF,
5889 };
5890
5891 do_machine_check(&regs, 0);
5892#endif
5893}
5894
Avi Kivity851ba692009-08-24 11:10:17 +03005895static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005896{
5897 /* already handled by vcpu_run */
5898 return 1;
5899}
5900
Avi Kivity851ba692009-08-24 11:10:17 +03005901static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005902{
Avi Kivity1155f762007-11-22 11:30:47 +02005903 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005904 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005905 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005906 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005907 u32 vect_info;
5908 enum emulation_result er;
5909
Avi Kivity1155f762007-11-22 11:30:47 +02005910 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005911 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005912
Andi Kleena0861c02009-06-08 17:37:09 +08005913 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005914 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005915
Jim Mattsonef85b672016-12-12 11:01:37 -08005916 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005917 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005918
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005919 if (is_invalid_opcode(intr_info)) {
Liran Alonac9b3052017-11-06 16:15:10 +02005920 WARN_ON_ONCE(is_guest_mode(vcpu));
Andre Przywara51d8b662010-12-21 11:12:02 +01005921 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alon61cb57c2017-11-05 16:56:32 +02005922 if (er == EMULATE_USER_EXIT)
5923 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005924 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005925 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005926 return 1;
5927 }
5928
Avi Kivity6aa8b732006-12-10 02:21:36 -08005929 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005930 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005931 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005932
5933 /*
5934 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5935 * MMIO, it is better to report an internal error.
5936 * See the comments in vmx_handle_exit.
5937 */
5938 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5939 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5940 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5941 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005942 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005943 vcpu->run->internal.data[0] = vect_info;
5944 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005945 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005946 return 0;
5947 }
5948
Avi Kivity6aa8b732006-12-10 02:21:36 -08005949 if (is_page_fault(intr_info)) {
5950 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005951 /* EPT won't cause page fault directly */
5952 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02005953 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005954 }
5955
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005956 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005957
5958 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5959 return handle_rmode_exception(vcpu, ex_no, error_code);
5960
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005961 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005962 case AC_VECTOR:
5963 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5964 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005965 case DB_VECTOR:
5966 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5967 if (!(vcpu->guest_debug &
5968 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005969 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005970 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01005971 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
5972 skip_emulated_instruction(vcpu);
5973
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005974 kvm_queue_exception(vcpu, DB_VECTOR);
5975 return 1;
5976 }
5977 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5978 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5979 /* fall through */
5980 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005981 /*
5982 * Update instruction length as we may reinject #BP from
5983 * user space while in guest debugging mode. Reading it for
5984 * #DB as well causes no harm, it is not used in that case.
5985 */
5986 vmx->vcpu.arch.event_exit_inst_len =
5987 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005988 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005989 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005990 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5991 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005992 break;
5993 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005994 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5995 kvm_run->ex.exception = ex_no;
5996 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005997 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005998 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005999 return 0;
6000}
6001
Avi Kivity851ba692009-08-24 11:10:17 +03006002static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006003{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006004 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006005 return 1;
6006}
6007
Avi Kivity851ba692009-08-24 11:10:17 +03006008static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006009{
Avi Kivity851ba692009-08-24 11:10:17 +03006010 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006011 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006012 return 0;
6013}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006014
Avi Kivity851ba692009-08-24 11:10:17 +03006015static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006016{
He, Qingbfdaab02007-09-12 14:18:28 +08006017 unsigned long exit_qualification;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006018 int size, in, string, ret;
Avi Kivity039576c2007-03-20 12:46:50 +02006019 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006020
He, Qingbfdaab02007-09-12 14:18:28 +08006021 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006022 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006023 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006024
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006025 ++vcpu->stat.io_exits;
6026
6027 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01006028 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006029
6030 port = exit_qualification >> 16;
6031 size = (exit_qualification & 7) + 1;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006032
Kyle Huey6affcbe2016-11-29 12:40:40 -08006033 ret = kvm_skip_emulated_instruction(vcpu);
6034
6035 /*
6036 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
6037 * KVM_EXIT_DEBUG here.
6038 */
6039 return kvm_fast_pio_out(vcpu, size, port) && ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006040}
6041
Ingo Molnar102d8322007-02-19 14:37:47 +02006042static void
6043vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6044{
6045 /*
6046 * Patch in the VMCALL instruction:
6047 */
6048 hypercall[0] = 0x0f;
6049 hypercall[1] = 0x01;
6050 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006051}
6052
Guo Chao0fa06072012-06-28 15:16:19 +08006053/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006054static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6055{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006056 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006057 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6058 unsigned long orig_val = val;
6059
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006060 /*
6061 * We get here when L2 changed cr0 in a way that did not change
6062 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006063 * but did change L0 shadowed bits. So we first calculate the
6064 * effective cr0 value that L1 would like to write into the
6065 * hardware. It consists of the L2-owned bits from the new
6066 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006067 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006068 val = (val & ~vmcs12->cr0_guest_host_mask) |
6069 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6070
David Matlack38991522016-11-29 18:14:08 -08006071 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006072 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006073
6074 if (kvm_set_cr0(vcpu, val))
6075 return 1;
6076 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006077 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006078 } else {
6079 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006080 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006081 return 1;
David Matlack38991522016-11-29 18:14:08 -08006082
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006083 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006084 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006085}
6086
6087static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6088{
6089 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006090 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6091 unsigned long orig_val = val;
6092
6093 /* analogously to handle_set_cr0 */
6094 val = (val & ~vmcs12->cr4_guest_host_mask) |
6095 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6096 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006097 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006098 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006099 return 0;
6100 } else
6101 return kvm_set_cr4(vcpu, val);
6102}
6103
Avi Kivity851ba692009-08-24 11:10:17 +03006104static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006105{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006106 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006107 int cr;
6108 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006109 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006110 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006111
He, Qingbfdaab02007-09-12 14:18:28 +08006112 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006113 cr = exit_qualification & 15;
6114 reg = (exit_qualification >> 8) & 15;
6115 switch ((exit_qualification >> 4) & 3) {
6116 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006117 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006118 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006119 switch (cr) {
6120 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006121 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006122 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006123 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006124 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006125 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006126 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006127 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006128 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006129 case 8: {
6130 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006131 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006132 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006133 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006134 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006135 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006136 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006137 return ret;
6138 /*
6139 * TODO: we might be squashing a
6140 * KVM_GUESTDBG_SINGLESTEP-triggered
6141 * KVM_EXIT_DEBUG here.
6142 */
Avi Kivity851ba692009-08-24 11:10:17 +03006143 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006144 return 0;
6145 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006146 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006147 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006148 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006149 WARN_ONCE(1, "Guest should always own CR0.TS");
6150 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006151 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006152 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006153 case 1: /*mov from cr*/
6154 switch (cr) {
6155 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006156 val = kvm_read_cr3(vcpu);
6157 kvm_register_write(vcpu, reg, val);
6158 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006159 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006160 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006161 val = kvm_get_cr8(vcpu);
6162 kvm_register_write(vcpu, reg, val);
6163 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006164 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006165 }
6166 break;
6167 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006168 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006169 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006170 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006171
Kyle Huey6affcbe2016-11-29 12:40:40 -08006172 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006173 default:
6174 break;
6175 }
Avi Kivity851ba692009-08-24 11:10:17 +03006176 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006177 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006178 (int)(exit_qualification >> 4) & 3, cr);
6179 return 0;
6180}
6181
Avi Kivity851ba692009-08-24 11:10:17 +03006182static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006183{
He, Qingbfdaab02007-09-12 14:18:28 +08006184 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006185 int dr, dr7, reg;
6186
6187 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6188 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6189
6190 /* First, if DR does not exist, trigger UD */
6191 if (!kvm_require_dr(vcpu, dr))
6192 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006193
Jan Kiszkaf2483412010-01-20 18:20:20 +01006194 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006195 if (!kvm_require_cpl(vcpu, 0))
6196 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006197 dr7 = vmcs_readl(GUEST_DR7);
6198 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006199 /*
6200 * As the vm-exit takes precedence over the debug trap, we
6201 * need to emulate the latter, either for the host or the
6202 * guest debugging itself.
6203 */
6204 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006205 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006206 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006207 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006208 vcpu->run->debug.arch.exception = DB_VECTOR;
6209 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006210 return 0;
6211 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006212 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006213 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006214 kvm_queue_exception(vcpu, DB_VECTOR);
6215 return 1;
6216 }
6217 }
6218
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006219 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006220 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6221 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006222
6223 /*
6224 * No more DR vmexits; force a reload of the debug registers
6225 * and reenter on this instruction. The next vmexit will
6226 * retrieve the full state of the debug registers.
6227 */
6228 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6229 return 1;
6230 }
6231
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006232 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6233 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006234 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006235
6236 if (kvm_get_dr(vcpu, dr, &val))
6237 return 1;
6238 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006239 } else
Nadav Amit57773922014-06-18 17:19:23 +03006240 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006241 return 1;
6242
Kyle Huey6affcbe2016-11-29 12:40:40 -08006243 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006244}
6245
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006246static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6247{
6248 return vcpu->arch.dr6;
6249}
6250
6251static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6252{
6253}
6254
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006255static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6256{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006257 get_debugreg(vcpu->arch.db[0], 0);
6258 get_debugreg(vcpu->arch.db[1], 1);
6259 get_debugreg(vcpu->arch.db[2], 2);
6260 get_debugreg(vcpu->arch.db[3], 3);
6261 get_debugreg(vcpu->arch.dr6, 6);
6262 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6263
6264 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006265 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006266}
6267
Gleb Natapov020df072010-04-13 10:05:23 +03006268static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6269{
6270 vmcs_writel(GUEST_DR7, val);
6271}
6272
Avi Kivity851ba692009-08-24 11:10:17 +03006273static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006274{
Kyle Huey6a908b62016-11-29 12:40:37 -08006275 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006276}
6277
Avi Kivity851ba692009-08-24 11:10:17 +03006278static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006279{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006280 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006281 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006282
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006283 msr_info.index = ecx;
6284 msr_info.host_initiated = false;
6285 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006286 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006287 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006288 return 1;
6289 }
6290
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006291 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006292
Avi Kivity6aa8b732006-12-10 02:21:36 -08006293 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006294 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6295 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006296 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006297}
6298
Avi Kivity851ba692009-08-24 11:10:17 +03006299static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006300{
Will Auld8fe8ab42012-11-29 12:42:12 -08006301 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006302 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6303 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6304 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006305
Will Auld8fe8ab42012-11-29 12:42:12 -08006306 msr.data = data;
6307 msr.index = ecx;
6308 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006309 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006310 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006311 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006312 return 1;
6313 }
6314
Avi Kivity59200272010-01-25 19:47:02 +02006315 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006316 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006317}
6318
Avi Kivity851ba692009-08-24 11:10:17 +03006319static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006320{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01006321 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006322 return 1;
6323}
6324
Avi Kivity851ba692009-08-24 11:10:17 +03006325static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006326{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006327 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6328 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006329
Avi Kivity3842d132010-07-27 12:30:24 +03006330 kvm_make_request(KVM_REQ_EVENT, vcpu);
6331
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006332 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006333 return 1;
6334}
6335
Avi Kivity851ba692009-08-24 11:10:17 +03006336static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006337{
Avi Kivityd3bef152007-06-05 15:53:05 +03006338 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006339}
6340
Avi Kivity851ba692009-08-24 11:10:17 +03006341static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006342{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006343 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006344}
6345
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006346static int handle_invd(struct kvm_vcpu *vcpu)
6347{
Andre Przywara51d8b662010-12-21 11:12:02 +01006348 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006349}
6350
Avi Kivity851ba692009-08-24 11:10:17 +03006351static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006352{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006353 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006354
6355 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006356 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006357}
6358
Avi Kivityfee84b02011-11-10 14:57:25 +02006359static int handle_rdpmc(struct kvm_vcpu *vcpu)
6360{
6361 int err;
6362
6363 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006364 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02006365}
6366
Avi Kivity851ba692009-08-24 11:10:17 +03006367static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006368{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006369 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006370}
6371
Dexuan Cui2acf9232010-06-10 11:27:12 +08006372static int handle_xsetbv(struct kvm_vcpu *vcpu)
6373{
6374 u64 new_bv = kvm_read_edx_eax(vcpu);
6375 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6376
6377 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006378 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08006379 return 1;
6380}
6381
Wanpeng Lif53cd632014-12-02 19:14:58 +08006382static int handle_xsaves(struct kvm_vcpu *vcpu)
6383{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006384 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006385 WARN(1, "this should never happen\n");
6386 return 1;
6387}
6388
6389static int handle_xrstors(struct kvm_vcpu *vcpu)
6390{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006391 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006392 WARN(1, "this should never happen\n");
6393 return 1;
6394}
6395
Avi Kivity851ba692009-08-24 11:10:17 +03006396static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006397{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006398 if (likely(fasteoi)) {
6399 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6400 int access_type, offset;
6401
6402 access_type = exit_qualification & APIC_ACCESS_TYPE;
6403 offset = exit_qualification & APIC_ACCESS_OFFSET;
6404 /*
6405 * Sane guest uses MOV to write EOI, with written value
6406 * not cared. So make a short-circuit here by avoiding
6407 * heavy instruction emulation.
6408 */
6409 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6410 (offset == APIC_EOI)) {
6411 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006412 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03006413 }
6414 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006415 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006416}
6417
Yang Zhangc7c9c562013-01-25 10:18:51 +08006418static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6419{
6420 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6421 int vector = exit_qualification & 0xff;
6422
6423 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6424 kvm_apic_set_eoi_accelerated(vcpu, vector);
6425 return 1;
6426}
6427
Yang Zhang83d4c282013-01-25 10:18:49 +08006428static int handle_apic_write(struct kvm_vcpu *vcpu)
6429{
6430 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6431 u32 offset = exit_qualification & 0xfff;
6432
6433 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6434 kvm_apic_write_nodecode(vcpu, offset);
6435 return 1;
6436}
6437
Avi Kivity851ba692009-08-24 11:10:17 +03006438static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006439{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006440 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006441 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006442 bool has_error_code = false;
6443 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006444 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006445 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006446
6447 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006448 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006449 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006450
6451 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6452
6453 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006454 if (reason == TASK_SWITCH_GATE && idt_v) {
6455 switch (type) {
6456 case INTR_TYPE_NMI_INTR:
6457 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006458 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006459 break;
6460 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006461 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006462 kvm_clear_interrupt_queue(vcpu);
6463 break;
6464 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006465 if (vmx->idt_vectoring_info &
6466 VECTORING_INFO_DELIVER_CODE_MASK) {
6467 has_error_code = true;
6468 error_code =
6469 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6470 }
6471 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006472 case INTR_TYPE_SOFT_EXCEPTION:
6473 kvm_clear_exception_queue(vcpu);
6474 break;
6475 default:
6476 break;
6477 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006478 }
Izik Eidus37817f22008-03-24 23:14:53 +02006479 tss_selector = exit_qualification;
6480
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006481 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6482 type != INTR_TYPE_EXT_INTR &&
6483 type != INTR_TYPE_NMI_INTR))
6484 skip_emulated_instruction(vcpu);
6485
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006486 if (kvm_task_switch(vcpu, tss_selector,
6487 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6488 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006489 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6490 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6491 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006492 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006493 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006494
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006495 /*
6496 * TODO: What about debug traps on tss switch?
6497 * Are we supposed to inject them and update dr6?
6498 */
6499
6500 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006501}
6502
Avi Kivity851ba692009-08-24 11:10:17 +03006503static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006504{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006505 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006506 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01006507 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006508
Sheng Yangf9c617f2009-03-25 10:08:52 +08006509 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006510
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006511 /*
6512 * EPT violation happened while executing iret from NMI,
6513 * "blocked by NMI" bit has to be set before next VM entry.
6514 * There are errata that may cause this bit to not be set:
6515 * AAK134, BY25.
6516 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006517 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006518 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006519 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006520 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6521
Sheng Yang14394422008-04-28 12:24:45 +08006522 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006523 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006524
Junaid Shahid27959a42016-12-06 16:46:10 -08006525 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006526 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08006527 ? PFERR_USER_MASK : 0;
6528 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006529 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08006530 ? PFERR_WRITE_MASK : 0;
6531 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006532 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08006533 ? PFERR_FETCH_MASK : 0;
6534 /* ept page table entry is present? */
6535 error_code |= (exit_qualification &
6536 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6537 EPT_VIOLATION_EXECUTABLE))
6538 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006539
Paolo Bonzinieebed242016-11-28 14:39:58 +01006540 error_code |= (exit_qualification & 0x100) != 0 ?
6541 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006542
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006543 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006544 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006545}
6546
Avi Kivity851ba692009-08-24 11:10:17 +03006547static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006548{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006549 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006550 gpa_t gpa;
6551
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02006552 /*
6553 * A nested guest cannot optimize MMIO vmexits, because we have an
6554 * nGPA here instead of the required GPA.
6555 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006556 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02006557 if (!is_guest_mode(vcpu) &&
6558 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08006559 trace_kvm_fast_mmio(gpa);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006560 return kvm_skip_emulated_instruction(vcpu);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006561 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006562
Paolo Bonzinie08d26f2017-08-17 18:36:56 +02006563 ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
6564 if (ret >= 0)
6565 return ret;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006566
6567 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006568 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006569
Avi Kivity851ba692009-08-24 11:10:17 +03006570 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6571 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006572
6573 return 0;
6574}
6575
Avi Kivity851ba692009-08-24 11:10:17 +03006576static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006577{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006578 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01006579 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6580 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08006581 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006582 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006583
6584 return 1;
6585}
6586
Mohammed Gamal80ced182009-09-01 12:48:18 +02006587static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006588{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006589 struct vcpu_vmx *vmx = to_vmx(vcpu);
6590 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006591 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006592 u32 cpu_exec_ctrl;
6593 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006594 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006595
6596 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6597 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006598
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006599 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006600 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006601 return handle_interrupt_window(&vmx->vcpu);
6602
Radim Krčmář72875d82017-04-26 22:32:19 +02006603 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03006604 return 1;
6605
Liran Alon9b8ae632017-11-05 16:56:34 +02006606 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006607
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006608 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006609 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006610 ret = 0;
6611 goto out;
6612 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006613
Avi Kivityde5f70e2012-06-12 20:22:28 +03006614 if (err != EMULATE_DONE) {
6615 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6616 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6617 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006618 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03006619 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006620
Gleb Natapov8d76c492013-05-08 18:38:44 +03006621 if (vcpu->arch.halt_request) {
6622 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006623 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006624 goto out;
6625 }
6626
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006627 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006628 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006629 if (need_resched())
6630 schedule();
6631 }
6632
Mohammed Gamal80ced182009-09-01 12:48:18 +02006633out:
6634 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006635}
6636
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006637static int __grow_ple_window(int val)
6638{
6639 if (ple_window_grow < 1)
6640 return ple_window;
6641
6642 val = min(val, ple_window_actual_max);
6643
6644 if (ple_window_grow < ple_window)
6645 val *= ple_window_grow;
6646 else
6647 val += ple_window_grow;
6648
6649 return val;
6650}
6651
6652static int __shrink_ple_window(int val, int modifier, int minimum)
6653{
6654 if (modifier < 1)
6655 return ple_window;
6656
6657 if (modifier < ple_window)
6658 val /= modifier;
6659 else
6660 val -= modifier;
6661
6662 return max(val, minimum);
6663}
6664
6665static void grow_ple_window(struct kvm_vcpu *vcpu)
6666{
6667 struct vcpu_vmx *vmx = to_vmx(vcpu);
6668 int old = vmx->ple_window;
6669
6670 vmx->ple_window = __grow_ple_window(old);
6671
6672 if (vmx->ple_window != old)
6673 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006674
6675 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006676}
6677
6678static void shrink_ple_window(struct kvm_vcpu *vcpu)
6679{
6680 struct vcpu_vmx *vmx = to_vmx(vcpu);
6681 int old = vmx->ple_window;
6682
6683 vmx->ple_window = __shrink_ple_window(old,
6684 ple_window_shrink, ple_window);
6685
6686 if (vmx->ple_window != old)
6687 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006688
6689 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006690}
6691
6692/*
6693 * ple_window_actual_max is computed to be one grow_ple_window() below
6694 * ple_window_max. (See __grow_ple_window for the reason.)
6695 * This prevents overflows, because ple_window_max is int.
6696 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6697 * this process.
6698 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6699 */
6700static void update_ple_window_actual_max(void)
6701{
6702 ple_window_actual_max =
6703 __shrink_ple_window(max(ple_window_max, ple_window),
6704 ple_window_grow, INT_MIN);
6705}
6706
Feng Wubf9f6ac2015-09-18 22:29:55 +08006707/*
6708 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6709 */
6710static void wakeup_handler(void)
6711{
6712 struct kvm_vcpu *vcpu;
6713 int cpu = smp_processor_id();
6714
6715 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6716 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6717 blocked_vcpu_list) {
6718 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6719
6720 if (pi_test_on(pi_desc) == 1)
6721 kvm_vcpu_kick(vcpu);
6722 }
6723 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6724}
6725
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006726void vmx_enable_tdp(void)
6727{
6728 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
6729 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
6730 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
6731 0ull, VMX_EPT_EXECUTABLE_MASK,
6732 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05006733 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006734
6735 ept_set_mmio_spte_mask();
6736 kvm_enable_tdp();
6737}
6738
Tiejun Chenf2c76482014-10-28 10:14:47 +08006739static __init int hardware_setup(void)
6740{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006741 int r = -ENOMEM, i, msr;
6742
6743 rdmsrl_safe(MSR_EFER, &host_efer);
6744
6745 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6746 kvm_define_shared_msr(i, vmx_msr_index[i]);
6747
Radim Krčmář23611332016-09-29 22:41:33 +02006748 for (i = 0; i < VMX_BITMAP_NR; i++) {
6749 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
6750 if (!vmx_bitmap[i])
6751 goto out;
6752 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006753
6754 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006755 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6756 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6757
6758 /*
6759 * Allow direct access to the PC debug port (it is often used for I/O
6760 * delays, but the vmexits simply slow things down).
6761 */
6762 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
6763 clear_bit(0x80, vmx_io_bitmap_a);
6764
6765 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6766
6767 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
6768 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
6769
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006770 if (setup_vmcs_config(&vmcs_config) < 0) {
6771 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02006772 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006773 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006774
6775 if (boot_cpu_has(X86_FEATURE_NX))
6776 kvm_enable_efer_bits(EFER_NX);
6777
Wanpeng Li08d839c2017-03-23 05:30:08 -07006778 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6779 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006780 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07006781
Tiejun Chenf2c76482014-10-28 10:14:47 +08006782 if (!cpu_has_vmx_shadow_vmcs())
6783 enable_shadow_vmcs = 0;
6784 if (enable_shadow_vmcs)
6785 init_vmcs_shadow_fields();
6786
6787 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02006788 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02006789 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07006790 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006791 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006792
Wanpeng Lifce6ac42017-05-11 02:58:56 -07006793 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006794 enable_ept_ad_bits = 0;
6795
Wanpeng Li8ad81822017-10-09 15:51:53 -07006796 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006797 enable_unrestricted_guest = 0;
6798
Paolo Bonziniad15a292015-01-30 16:18:49 +01006799 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006800 flexpriority_enabled = 0;
6801
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006802 if (!cpu_has_virtual_nmis())
6803 enable_vnmi = 0;
6804
Paolo Bonziniad15a292015-01-30 16:18:49 +01006805 /*
6806 * set_apic_access_page_addr() is used to reload apic access
6807 * page upon invalidation. No need to do anything if not
6808 * using the APIC_ACCESS_ADDR VMCS field.
6809 */
6810 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006811 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006812
6813 if (!cpu_has_vmx_tpr_shadow())
6814 kvm_x86_ops->update_cr8_intercept = NULL;
6815
6816 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6817 kvm_disable_largepages();
6818
Wanpeng Li0f107682017-09-28 18:06:24 -07006819 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08006820 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07006821 ple_window = 0;
6822 ple_window_grow = 0;
6823 ple_window_max = 0;
6824 ple_window_shrink = 0;
6825 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006826
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006827 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08006828 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006829 kvm_x86_ops->sync_pir_to_irr = NULL;
6830 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006831
Haozhong Zhang64903d62015-10-20 15:39:09 +08006832 if (cpu_has_vmx_tsc_scaling()) {
6833 kvm_has_tsc_control = true;
6834 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6835 kvm_tsc_scaling_ratio_frac_bits = 48;
6836 }
6837
Tiejun Chenbaa03522014-12-23 16:21:11 +08006838 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
6839 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
6840 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
6841 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
6842 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
6843 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006844
Wanpeng Lic63e4562016-09-23 19:17:16 +08006845 memcpy(vmx_msr_bitmap_legacy_x2apic_apicv,
6846 vmx_msr_bitmap_legacy, PAGE_SIZE);
6847 memcpy(vmx_msr_bitmap_longmode_x2apic_apicv,
6848 vmx_msr_bitmap_longmode, PAGE_SIZE);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006849 memcpy(vmx_msr_bitmap_legacy_x2apic,
6850 vmx_msr_bitmap_legacy, PAGE_SIZE);
6851 memcpy(vmx_msr_bitmap_longmode_x2apic,
6852 vmx_msr_bitmap_longmode, PAGE_SIZE);
6853
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006854 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6855
Radim Krčmář40d83382016-09-29 22:41:31 +02006856 for (msr = 0x800; msr <= 0x8ff; msr++) {
6857 if (msr == 0x839 /* TMCCT */)
6858 continue;
Radim Krčmář2e69f862016-09-29 22:41:32 +02006859 vmx_disable_intercept_msr_x2apic(msr, MSR_TYPE_R, true);
Radim Krčmář40d83382016-09-29 22:41:31 +02006860 }
Tiejun Chenbaa03522014-12-23 16:21:11 +08006861
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006862 /*
Radim Krčmář2e69f862016-09-29 22:41:32 +02006863 * TPR reads and writes can be virtualized even if virtual interrupt
6864 * delivery is not in use.
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006865 */
Radim Krčmář2e69f862016-09-29 22:41:32 +02006866 vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_W, true);
6867 vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_R | MSR_TYPE_W, false);
6868
Roman Kagan3ce424e2016-05-18 17:48:20 +03006869 /* EOI */
Radim Krčmář2e69f862016-09-29 22:41:32 +02006870 vmx_disable_intercept_msr_x2apic(0x80b, MSR_TYPE_W, true);
Roman Kagan3ce424e2016-05-18 17:48:20 +03006871 /* SELF-IPI */
Radim Krčmář2e69f862016-09-29 22:41:32 +02006872 vmx_disable_intercept_msr_x2apic(0x83f, MSR_TYPE_W, true);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006873
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006874 if (enable_ept)
6875 vmx_enable_tdp();
6876 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08006877 kvm_disable_tdp();
6878
6879 update_ple_window_actual_max();
6880
Kai Huang843e4332015-01-28 10:54:28 +08006881 /*
6882 * Only enable PML when hardware supports PML feature, and both EPT
6883 * and EPT A/D bit features are enabled -- PML depends on them to work.
6884 */
6885 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6886 enable_pml = 0;
6887
6888 if (!enable_pml) {
6889 kvm_x86_ops->slot_enable_log_dirty = NULL;
6890 kvm_x86_ops->slot_disable_log_dirty = NULL;
6891 kvm_x86_ops->flush_log_dirty = NULL;
6892 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6893 }
6894
Yunhong Jiang64672c92016-06-13 14:19:59 -07006895 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6896 u64 vmx_msr;
6897
6898 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6899 cpu_preemption_timer_multi =
6900 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6901 } else {
6902 kvm_x86_ops->set_hv_timer = NULL;
6903 kvm_x86_ops->cancel_hv_timer = NULL;
6904 }
6905
Feng Wubf9f6ac2015-09-18 22:29:55 +08006906 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6907
Ashok Rajc45dcc72016-06-22 14:59:56 +08006908 kvm_mce_cap_supported |= MCG_LMCE_P;
6909
Tiejun Chenf2c76482014-10-28 10:14:47 +08006910 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006911
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006912out:
Radim Krčmář23611332016-09-29 22:41:33 +02006913 for (i = 0; i < VMX_BITMAP_NR; i++)
6914 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006915
6916 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006917}
6918
6919static __exit void hardware_unsetup(void)
6920{
Radim Krčmář23611332016-09-29 22:41:33 +02006921 int i;
6922
6923 for (i = 0; i < VMX_BITMAP_NR; i++)
6924 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006925
Tiejun Chenf2c76482014-10-28 10:14:47 +08006926 free_kvm_area();
6927}
6928
Avi Kivity6aa8b732006-12-10 02:21:36 -08006929/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006930 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6931 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6932 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006933static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006934{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006935 if (ple_gap)
6936 grow_ple_window(vcpu);
6937
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08006938 /*
6939 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
6940 * VM-execution control is ignored if CPL > 0. OTOH, KVM
6941 * never set PAUSE_EXITING and just set PLE if supported,
6942 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
6943 */
6944 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006945 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006946}
6947
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006948static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006949{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006950 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006951}
6952
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006953static int handle_mwait(struct kvm_vcpu *vcpu)
6954{
6955 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6956 return handle_nop(vcpu);
6957}
6958
Jim Mattson45ec3682017-08-23 16:32:04 -07006959static int handle_invalid_op(struct kvm_vcpu *vcpu)
6960{
6961 kvm_queue_exception(vcpu, UD_VECTOR);
6962 return 1;
6963}
6964
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006965static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6966{
6967 return 1;
6968}
6969
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006970static int handle_monitor(struct kvm_vcpu *vcpu)
6971{
6972 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6973 return handle_nop(vcpu);
6974}
6975
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006976/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006977 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
6978 * We could reuse a single VMCS for all the L2 guests, but we also want the
6979 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
6980 * allows keeping them loaded on the processor, and in the future will allow
6981 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
6982 * every entry if they never change.
6983 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
6984 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
6985 *
6986 * The following functions allocate and free a vmcs02 in this pool.
6987 */
6988
6989/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
6990static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
6991{
6992 struct vmcs02_list *item;
6993 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6994 if (item->vmptr == vmx->nested.current_vmptr) {
6995 list_move(&item->list, &vmx->nested.vmcs02_pool);
6996 return &item->vmcs02;
6997 }
6998
6999 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
7000 /* Recycle the least recently used VMCS. */
Geliang Tangd74c0e62016-01-01 19:47:14 +08007001 item = list_last_entry(&vmx->nested.vmcs02_pool,
7002 struct vmcs02_list, list);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007003 item->vmptr = vmx->nested.current_vmptr;
7004 list_move(&item->list, &vmx->nested.vmcs02_pool);
7005 return &item->vmcs02;
7006 }
7007
7008 /* Create a new VMCS */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01007009 item = kzalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007010 if (!item)
7011 return NULL;
7012 item->vmcs02.vmcs = alloc_vmcs();
Jim Mattson355f4fb2016-10-28 08:29:39 -07007013 item->vmcs02.shadow_vmcs = NULL;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007014 if (!item->vmcs02.vmcs) {
7015 kfree(item);
7016 return NULL;
7017 }
7018 loaded_vmcs_init(&item->vmcs02);
7019 item->vmptr = vmx->nested.current_vmptr;
7020 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
7021 vmx->nested.vmcs02_num++;
7022 return &item->vmcs02;
7023}
7024
7025/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
7026static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
7027{
7028 struct vmcs02_list *item;
7029 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
7030 if (item->vmptr == vmptr) {
7031 free_loaded_vmcs(&item->vmcs02);
7032 list_del(&item->list);
7033 kfree(item);
7034 vmx->nested.vmcs02_num--;
7035 return;
7036 }
7037}
7038
7039/*
7040 * Free all VMCSs saved for this vcpu, except the one pointed by
Paolo Bonzini4fa77342014-07-17 12:25:16 +02007041 * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs
7042 * must be &vmx->vmcs01.
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007043 */
7044static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
7045{
7046 struct vmcs02_list *item, *n;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02007047
7048 WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007049 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
Paolo Bonzini4fa77342014-07-17 12:25:16 +02007050 /*
7051 * Something will leak if the above WARN triggers. Better than
7052 * a use-after-free.
7053 */
7054 if (vmx->loaded_vmcs == &item->vmcs02)
7055 continue;
7056
7057 free_loaded_vmcs(&item->vmcs02);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007058 list_del(&item->list);
7059 kfree(item);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02007060 vmx->nested.vmcs02_num--;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007061 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007062}
7063
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007064/*
7065 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7066 * set the success or error code of an emulated VMX instruction, as specified
7067 * by Vol 2B, VMX Instruction Reference, "Conventions".
7068 */
7069static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7070{
7071 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7072 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7073 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7074}
7075
7076static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7077{
7078 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7079 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7080 X86_EFLAGS_SF | X86_EFLAGS_OF))
7081 | X86_EFLAGS_CF);
7082}
7083
Abel Gordon145c28d2013-04-18 14:36:55 +03007084static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007085 u32 vm_instruction_error)
7086{
7087 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7088 /*
7089 * failValid writes the error number to the current VMCS, which
7090 * can't be done there isn't a current VMCS.
7091 */
7092 nested_vmx_failInvalid(vcpu);
7093 return;
7094 }
7095 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7096 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7097 X86_EFLAGS_SF | X86_EFLAGS_OF))
7098 | X86_EFLAGS_ZF);
7099 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7100 /*
7101 * We don't need to force a shadow sync because
7102 * VM_INSTRUCTION_ERROR is not shadowed
7103 */
7104}
Abel Gordon145c28d2013-04-18 14:36:55 +03007105
Wincy Vanff651cb2014-12-11 08:52:58 +03007106static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7107{
7108 /* TODO: not to reset guest simply here. */
7109 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007110 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007111}
7112
Jan Kiszkaf41245002014-03-07 20:03:13 +01007113static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7114{
7115 struct vcpu_vmx *vmx =
7116 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7117
7118 vmx->nested.preemption_timer_expired = true;
7119 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7120 kvm_vcpu_kick(&vmx->vcpu);
7121
7122 return HRTIMER_NORESTART;
7123}
7124
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007125/*
Bandan Das19677e32014-05-06 02:19:15 -04007126 * Decode the memory-address operand of a vmx instruction, as recorded on an
7127 * exit caused by such an instruction (run by a guest hypervisor).
7128 * On success, returns 0. When the operand is invalid, returns 1 and throws
7129 * #UD or #GP.
7130 */
7131static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7132 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007133 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007134{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007135 gva_t off;
7136 bool exn;
7137 struct kvm_segment s;
7138
Bandan Das19677e32014-05-06 02:19:15 -04007139 /*
7140 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7141 * Execution", on an exit, vmx_instruction_info holds most of the
7142 * addressing components of the operand. Only the displacement part
7143 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7144 * For how an actual address is calculated from all these components,
7145 * refer to Vol. 1, "Operand Addressing".
7146 */
7147 int scaling = vmx_instruction_info & 3;
7148 int addr_size = (vmx_instruction_info >> 7) & 7;
7149 bool is_reg = vmx_instruction_info & (1u << 10);
7150 int seg_reg = (vmx_instruction_info >> 15) & 7;
7151 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7152 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7153 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7154 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7155
7156 if (is_reg) {
7157 kvm_queue_exception(vcpu, UD_VECTOR);
7158 return 1;
7159 }
7160
7161 /* Addr = segment_base + offset */
7162 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007163 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007164 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007165 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007166 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007167 off += kvm_register_read(vcpu, index_reg)<<scaling;
7168 vmx_get_segment(vcpu, &s, seg_reg);
7169 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007170
7171 if (addr_size == 1) /* 32 bit */
7172 *ret &= 0xffffffff;
7173
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007174 /* Checks for #GP/#SS exceptions. */
7175 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007176 if (is_long_mode(vcpu)) {
7177 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7178 * non-canonical form. This is the only check on the memory
7179 * destination for long mode!
7180 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007181 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007182 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007183 /* Protected mode: apply checks for segment validity in the
7184 * following order:
7185 * - segment type check (#GP(0) may be thrown)
7186 * - usability check (#GP(0)/#SS(0))
7187 * - limit check (#GP(0)/#SS(0))
7188 */
7189 if (wr)
7190 /* #GP(0) if the destination operand is located in a
7191 * read-only data segment or any code segment.
7192 */
7193 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7194 else
7195 /* #GP(0) if the source operand is located in an
7196 * execute-only code segment
7197 */
7198 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007199 if (exn) {
7200 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7201 return 1;
7202 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007203 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7204 */
7205 exn = (s.unusable != 0);
7206 /* Protected mode: #GP(0)/#SS(0) if the memory
7207 * operand is outside the segment limit.
7208 */
7209 exn = exn || (off + sizeof(u64) > s.limit);
7210 }
7211 if (exn) {
7212 kvm_queue_exception_e(vcpu,
7213 seg_reg == VCPU_SREG_SS ?
7214 SS_VECTOR : GP_VECTOR,
7215 0);
7216 return 1;
7217 }
7218
Bandan Das19677e32014-05-06 02:19:15 -04007219 return 0;
7220}
7221
Radim Krčmářcbf71272017-05-19 15:48:51 +02007222static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007223{
7224 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007225 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007226
7227 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007228 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007229 return 1;
7230
Radim Krčmářcbf71272017-05-19 15:48:51 +02007231 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7232 sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007233 kvm_inject_page_fault(vcpu, &e);
7234 return 1;
7235 }
7236
Bandan Das3573e222014-05-06 02:19:16 -04007237 return 0;
7238}
7239
Jim Mattsone29acc52016-11-30 12:03:43 -08007240static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7241{
7242 struct vcpu_vmx *vmx = to_vmx(vcpu);
7243 struct vmcs *shadow_vmcs;
7244
7245 if (cpu_has_vmx_msr_bitmap()) {
7246 vmx->nested.msr_bitmap =
7247 (unsigned long *)__get_free_page(GFP_KERNEL);
7248 if (!vmx->nested.msr_bitmap)
7249 goto out_msr_bitmap;
7250 }
7251
7252 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7253 if (!vmx->nested.cached_vmcs12)
7254 goto out_cached_vmcs12;
7255
7256 if (enable_shadow_vmcs) {
7257 shadow_vmcs = alloc_vmcs();
7258 if (!shadow_vmcs)
7259 goto out_shadow_vmcs;
7260 /* mark vmcs as shadow */
7261 shadow_vmcs->revision_id |= (1u << 31);
7262 /* init shadow vmcs */
7263 vmcs_clear(shadow_vmcs);
7264 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7265 }
7266
7267 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
7268 vmx->nested.vmcs02_num = 0;
7269
7270 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7271 HRTIMER_MODE_REL_PINNED);
7272 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7273
7274 vmx->nested.vmxon = true;
7275 return 0;
7276
7277out_shadow_vmcs:
7278 kfree(vmx->nested.cached_vmcs12);
7279
7280out_cached_vmcs12:
7281 free_page((unsigned long)vmx->nested.msr_bitmap);
7282
7283out_msr_bitmap:
7284 return -ENOMEM;
7285}
7286
Bandan Das3573e222014-05-06 02:19:16 -04007287/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007288 * Emulate the VMXON instruction.
7289 * Currently, we just remember that VMX is active, and do not save or even
7290 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7291 * do not currently need to store anything in that guest-allocated memory
7292 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7293 * argument is different from the VMXON pointer (which the spec says they do).
7294 */
7295static int handle_vmon(struct kvm_vcpu *vcpu)
7296{
Jim Mattsone29acc52016-11-30 12:03:43 -08007297 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007298 gpa_t vmptr;
7299 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007300 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007301 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7302 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007303
Jim Mattson70f3aac2017-04-26 08:53:46 -07007304 /*
7305 * The Intel VMX Instruction Reference lists a bunch of bits that are
7306 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7307 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7308 * Otherwise, we should fail with #UD. But most faulting conditions
7309 * have already been checked by hardware, prior to the VM-exit for
7310 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7311 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007312 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007313 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007314 kvm_queue_exception(vcpu, UD_VECTOR);
7315 return 1;
7316 }
7317
Abel Gordon145c28d2013-04-18 14:36:55 +03007318 if (vmx->nested.vmxon) {
7319 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007320 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007321 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007322
Haozhong Zhang3b840802016-06-22 14:59:54 +08007323 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007324 != VMXON_NEEDED_FEATURES) {
7325 kvm_inject_gp(vcpu, 0);
7326 return 1;
7327 }
7328
Radim Krčmářcbf71272017-05-19 15:48:51 +02007329 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007330 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007331
7332 /*
7333 * SDM 3: 24.11.5
7334 * The first 4 bytes of VMXON region contain the supported
7335 * VMCS revision identifier
7336 *
7337 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7338 * which replaces physical address width with 32
7339 */
7340 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7341 nested_vmx_failInvalid(vcpu);
7342 return kvm_skip_emulated_instruction(vcpu);
7343 }
7344
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007345 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7346 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007347 nested_vmx_failInvalid(vcpu);
7348 return kvm_skip_emulated_instruction(vcpu);
7349 }
7350 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7351 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007352 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007353 nested_vmx_failInvalid(vcpu);
7354 return kvm_skip_emulated_instruction(vcpu);
7355 }
7356 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007357 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007358
7359 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007360 ret = enter_vmx_operation(vcpu);
7361 if (ret)
7362 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007363
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007364 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007365 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007366}
7367
7368/*
7369 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7370 * for running VMX instructions (except VMXON, whose prerequisites are
7371 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07007372 * Note that many of these exceptions have priority over VM exits, so they
7373 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007374 */
7375static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7376{
Jim Mattson70f3aac2017-04-26 08:53:46 -07007377 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007378 kvm_queue_exception(vcpu, UD_VECTOR);
7379 return 0;
7380 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007381 return 1;
7382}
7383
David Matlack8ca44e82017-08-01 14:00:39 -07007384static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7385{
7386 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7387 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7388}
7389
Abel Gordone7953d72013-04-18 14:37:55 +03007390static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7391{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007392 if (vmx->nested.current_vmptr == -1ull)
7393 return;
7394
Abel Gordon012f83c2013-04-18 14:39:25 +03007395 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007396 /* copy to memory all shadowed fields in case
7397 they were modified */
7398 copy_shadow_to_vmcs12(vmx);
7399 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07007400 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03007401 }
Wincy Van705699a2015-02-03 23:58:17 +08007402 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007403
7404 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007405 kvm_vcpu_write_guest_page(&vmx->vcpu,
7406 vmx->nested.current_vmptr >> PAGE_SHIFT,
7407 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07007408
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007409 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03007410}
7411
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007412/*
7413 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7414 * just stops using VMX.
7415 */
7416static void free_nested(struct vcpu_vmx *vmx)
7417{
7418 if (!vmx->nested.vmxon)
7419 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007420
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007421 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007422 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07007423 vmx->nested.posted_intr_nv = -1;
7424 vmx->nested.current_vmptr = -1ull;
Radim Krčmářd048c092016-08-08 20:16:22 +02007425 if (vmx->nested.msr_bitmap) {
7426 free_page((unsigned long)vmx->nested.msr_bitmap);
7427 vmx->nested.msr_bitmap = NULL;
7428 }
Jim Mattson355f4fb2016-10-28 08:29:39 -07007429 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07007430 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007431 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7432 free_vmcs(vmx->vmcs01.shadow_vmcs);
7433 vmx->vmcs01.shadow_vmcs = NULL;
7434 }
David Matlack4f2777b2016-07-13 17:16:37 -07007435 kfree(vmx->nested.cached_vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007436 /* Unpin physical memory we referred to in current vmcs02 */
7437 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007438 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007439 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007440 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007441 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007442 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007443 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007444 }
Wincy Van705699a2015-02-03 23:58:17 +08007445 if (vmx->nested.pi_desc_page) {
7446 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007447 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08007448 vmx->nested.pi_desc_page = NULL;
7449 vmx->nested.pi_desc = NULL;
7450 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007451
7452 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007453}
7454
7455/* Emulate the VMXOFF instruction */
7456static int handle_vmoff(struct kvm_vcpu *vcpu)
7457{
7458 if (!nested_vmx_check_permission(vcpu))
7459 return 1;
7460 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007461 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007462 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007463}
7464
Nadav Har'El27d6c862011-05-25 23:06:59 +03007465/* Emulate the VMCLEAR instruction */
7466static int handle_vmclear(struct kvm_vcpu *vcpu)
7467{
7468 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08007469 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007470 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007471
7472 if (!nested_vmx_check_permission(vcpu))
7473 return 1;
7474
Radim Krčmářcbf71272017-05-19 15:48:51 +02007475 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007476 return 1;
7477
Radim Krčmářcbf71272017-05-19 15:48:51 +02007478 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7479 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7480 return kvm_skip_emulated_instruction(vcpu);
7481 }
7482
7483 if (vmptr == vmx->nested.vmxon_ptr) {
7484 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7485 return kvm_skip_emulated_instruction(vcpu);
7486 }
7487
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007488 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007489 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007490
Jim Mattson587d7e722017-03-02 12:41:48 -08007491 kvm_vcpu_write_guest(vcpu,
7492 vmptr + offsetof(struct vmcs12, launch_state),
7493 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007494
7495 nested_free_vmcs02(vmx, vmptr);
7496
Nadav Har'El27d6c862011-05-25 23:06:59 +03007497 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007498 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007499}
7500
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007501static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7502
7503/* Emulate the VMLAUNCH instruction */
7504static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7505{
7506 return nested_vmx_run(vcpu, true);
7507}
7508
7509/* Emulate the VMRESUME instruction */
7510static int handle_vmresume(struct kvm_vcpu *vcpu)
7511{
7512
7513 return nested_vmx_run(vcpu, false);
7514}
7515
Nadav Har'El49f705c2011-05-25 23:08:30 +03007516/*
7517 * Read a vmcs12 field. Since these can have varying lengths and we return
7518 * one type, we chose the biggest type (u64) and zero-extend the return value
7519 * to that size. Note that the caller, handle_vmread, might need to use only
7520 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7521 * 64-bit fields are to be returned).
7522 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007523static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7524 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007525{
7526 short offset = vmcs_field_to_offset(field);
7527 char *p;
7528
7529 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007530 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007531
7532 p = ((char *)(get_vmcs12(vcpu))) + offset;
7533
7534 switch (vmcs_field_type(field)) {
7535 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7536 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007537 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007538 case VMCS_FIELD_TYPE_U16:
7539 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007540 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007541 case VMCS_FIELD_TYPE_U32:
7542 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007543 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007544 case VMCS_FIELD_TYPE_U64:
7545 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007546 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007547 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007548 WARN_ON(1);
7549 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007550 }
7551}
7552
Abel Gordon20b97fe2013-04-18 14:36:25 +03007553
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007554static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7555 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007556 short offset = vmcs_field_to_offset(field);
7557 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7558 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007559 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007560
7561 switch (vmcs_field_type(field)) {
7562 case VMCS_FIELD_TYPE_U16:
7563 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007564 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007565 case VMCS_FIELD_TYPE_U32:
7566 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007567 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007568 case VMCS_FIELD_TYPE_U64:
7569 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007570 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007571 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7572 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007573 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007574 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007575 WARN_ON(1);
7576 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007577 }
7578
7579}
7580
Abel Gordon16f5b902013-04-18 14:38:25 +03007581static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7582{
7583 int i;
7584 unsigned long field;
7585 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007586 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007587 const unsigned long *fields = shadow_read_write_fields;
7588 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007589
Jan Kiszka282da872014-10-08 18:05:39 +02007590 preempt_disable();
7591
Abel Gordon16f5b902013-04-18 14:38:25 +03007592 vmcs_load(shadow_vmcs);
7593
7594 for (i = 0; i < num_fields; i++) {
7595 field = fields[i];
7596 switch (vmcs_field_type(field)) {
7597 case VMCS_FIELD_TYPE_U16:
7598 field_value = vmcs_read16(field);
7599 break;
7600 case VMCS_FIELD_TYPE_U32:
7601 field_value = vmcs_read32(field);
7602 break;
7603 case VMCS_FIELD_TYPE_U64:
7604 field_value = vmcs_read64(field);
7605 break;
7606 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7607 field_value = vmcs_readl(field);
7608 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007609 default:
7610 WARN_ON(1);
7611 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007612 }
7613 vmcs12_write_any(&vmx->vcpu, field, field_value);
7614 }
7615
7616 vmcs_clear(shadow_vmcs);
7617 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007618
7619 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007620}
7621
Abel Gordonc3114422013-04-18 14:38:55 +03007622static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7623{
Mathias Krausec2bae892013-06-26 20:36:21 +02007624 const unsigned long *fields[] = {
7625 shadow_read_write_fields,
7626 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007627 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007628 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007629 max_shadow_read_write_fields,
7630 max_shadow_read_only_fields
7631 };
7632 int i, q;
7633 unsigned long field;
7634 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007635 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007636
7637 vmcs_load(shadow_vmcs);
7638
Mathias Krausec2bae892013-06-26 20:36:21 +02007639 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007640 for (i = 0; i < max_fields[q]; i++) {
7641 field = fields[q][i];
7642 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7643
7644 switch (vmcs_field_type(field)) {
7645 case VMCS_FIELD_TYPE_U16:
7646 vmcs_write16(field, (u16)field_value);
7647 break;
7648 case VMCS_FIELD_TYPE_U32:
7649 vmcs_write32(field, (u32)field_value);
7650 break;
7651 case VMCS_FIELD_TYPE_U64:
7652 vmcs_write64(field, (u64)field_value);
7653 break;
7654 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7655 vmcs_writel(field, (long)field_value);
7656 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007657 default:
7658 WARN_ON(1);
7659 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007660 }
7661 }
7662 }
7663
7664 vmcs_clear(shadow_vmcs);
7665 vmcs_load(vmx->loaded_vmcs->vmcs);
7666}
7667
Nadav Har'El49f705c2011-05-25 23:08:30 +03007668/*
7669 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7670 * used before) all generate the same failure when it is missing.
7671 */
7672static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7673{
7674 struct vcpu_vmx *vmx = to_vmx(vcpu);
7675 if (vmx->nested.current_vmptr == -1ull) {
7676 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007677 return 0;
7678 }
7679 return 1;
7680}
7681
7682static int handle_vmread(struct kvm_vcpu *vcpu)
7683{
7684 unsigned long field;
7685 u64 field_value;
7686 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7687 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7688 gva_t gva = 0;
7689
Kyle Hueyeb277562016-11-29 12:40:39 -08007690 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007691 return 1;
7692
Kyle Huey6affcbe2016-11-29 12:40:40 -08007693 if (!nested_vmx_check_vmcs12(vcpu))
7694 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007695
Nadav Har'El49f705c2011-05-25 23:08:30 +03007696 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007697 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007698 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007699 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007700 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007701 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007702 }
7703 /*
7704 * Now copy part of this value to register or memory, as requested.
7705 * Note that the number of bits actually copied is 32 or 64 depending
7706 * on the guest's mode (32 or 64 bit), not on the given field's length.
7707 */
7708 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007709 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007710 field_value);
7711 } else {
7712 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007713 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007714 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07007715 /* _system ok, as hardware has verified cpl=0 */
Nadav Har'El49f705c2011-05-25 23:08:30 +03007716 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7717 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7718 }
7719
7720 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007721 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007722}
7723
7724
7725static int handle_vmwrite(struct kvm_vcpu *vcpu)
7726{
7727 unsigned long field;
7728 gva_t gva;
7729 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7730 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007731 /* The value to write might be 32 or 64 bits, depending on L1's long
7732 * mode, and eventually we need to write that into a field of several
7733 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007734 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007735 * bits into the vmcs12 field.
7736 */
7737 u64 field_value = 0;
7738 struct x86_exception e;
7739
Kyle Hueyeb277562016-11-29 12:40:39 -08007740 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007741 return 1;
7742
Kyle Huey6affcbe2016-11-29 12:40:40 -08007743 if (!nested_vmx_check_vmcs12(vcpu))
7744 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007745
Nadav Har'El49f705c2011-05-25 23:08:30 +03007746 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007747 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007748 (((vmx_instruction_info) >> 3) & 0xf));
7749 else {
7750 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007751 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007752 return 1;
7753 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007754 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007755 kvm_inject_page_fault(vcpu, &e);
7756 return 1;
7757 }
7758 }
7759
7760
Nadav Amit27e6fb52014-06-18 17:19:26 +03007761 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007762 if (vmcs_field_readonly(field)) {
7763 nested_vmx_failValid(vcpu,
7764 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007765 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007766 }
7767
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007768 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007769 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007770 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007771 }
7772
7773 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007774 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007775}
7776
Jim Mattsona8bc2842016-11-30 12:03:44 -08007777static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
7778{
7779 vmx->nested.current_vmptr = vmptr;
7780 if (enable_shadow_vmcs) {
7781 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7782 SECONDARY_EXEC_SHADOW_VMCS);
7783 vmcs_write64(VMCS_LINK_POINTER,
7784 __pa(vmx->vmcs01.shadow_vmcs));
7785 vmx->nested.sync_shadow_vmcs = true;
7786 }
7787}
7788
Nadav Har'El63846662011-05-25 23:07:29 +03007789/* Emulate the VMPTRLD instruction */
7790static int handle_vmptrld(struct kvm_vcpu *vcpu)
7791{
7792 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007793 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007794
7795 if (!nested_vmx_check_permission(vcpu))
7796 return 1;
7797
Radim Krčmářcbf71272017-05-19 15:48:51 +02007798 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007799 return 1;
7800
Radim Krčmářcbf71272017-05-19 15:48:51 +02007801 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7802 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
7803 return kvm_skip_emulated_instruction(vcpu);
7804 }
7805
7806 if (vmptr == vmx->nested.vmxon_ptr) {
7807 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
7808 return kvm_skip_emulated_instruction(vcpu);
7809 }
7810
Nadav Har'El63846662011-05-25 23:07:29 +03007811 if (vmx->nested.current_vmptr != vmptr) {
7812 struct vmcs12 *new_vmcs12;
7813 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007814 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7815 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03007816 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007817 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007818 }
7819 new_vmcs12 = kmap(page);
7820 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7821 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007822 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03007823 nested_vmx_failValid(vcpu,
7824 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007825 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007826 }
Nadav Har'El63846662011-05-25 23:07:29 +03007827
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007828 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07007829 /*
7830 * Load VMCS12 from guest memory since it is not already
7831 * cached.
7832 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007833 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
7834 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007835 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007836
Jim Mattsona8bc2842016-11-30 12:03:44 -08007837 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03007838 }
7839
7840 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007841 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007842}
7843
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007844/* Emulate the VMPTRST instruction */
7845static int handle_vmptrst(struct kvm_vcpu *vcpu)
7846{
7847 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7848 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7849 gva_t vmcs_gva;
7850 struct x86_exception e;
7851
7852 if (!nested_vmx_check_permission(vcpu))
7853 return 1;
7854
7855 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007856 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007857 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07007858 /* ok to use *_system, as hardware has verified cpl=0 */
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007859 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7860 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7861 sizeof(u64), &e)) {
7862 kvm_inject_page_fault(vcpu, &e);
7863 return 1;
7864 }
7865 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007866 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007867}
7868
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007869/* Emulate the INVEPT instruction */
7870static int handle_invept(struct kvm_vcpu *vcpu)
7871{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007872 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007873 u32 vmx_instruction_info, types;
7874 unsigned long type;
7875 gva_t gva;
7876 struct x86_exception e;
7877 struct {
7878 u64 eptp, gpa;
7879 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007880
Wincy Vanb9c237b2015-02-03 23:56:30 +08007881 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7882 SECONDARY_EXEC_ENABLE_EPT) ||
7883 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007884 kvm_queue_exception(vcpu, UD_VECTOR);
7885 return 1;
7886 }
7887
7888 if (!nested_vmx_check_permission(vcpu))
7889 return 1;
7890
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007891 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007892 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007893
Wincy Vanb9c237b2015-02-03 23:56:30 +08007894 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007895
Jim Mattson85c856b2016-10-26 08:38:38 -07007896 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007897 nested_vmx_failValid(vcpu,
7898 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007899 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007900 }
7901
7902 /* According to the Intel VMX instruction reference, the memory
7903 * operand is read even if it isn't needed (e.g., for type==global)
7904 */
7905 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007906 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007907 return 1;
7908 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7909 sizeof(operand), &e)) {
7910 kvm_inject_page_fault(vcpu, &e);
7911 return 1;
7912 }
7913
7914 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007915 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007916 /*
7917 * TODO: track mappings and invalidate
7918 * single context requests appropriately
7919 */
7920 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007921 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007922 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007923 nested_vmx_succeed(vcpu);
7924 break;
7925 default:
7926 BUG_ON(1);
7927 break;
7928 }
7929
Kyle Huey6affcbe2016-11-29 12:40:40 -08007930 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007931}
7932
Petr Matouseka642fc32014-09-23 20:22:30 +02007933static int handle_invvpid(struct kvm_vcpu *vcpu)
7934{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007935 struct vcpu_vmx *vmx = to_vmx(vcpu);
7936 u32 vmx_instruction_info;
7937 unsigned long type, types;
7938 gva_t gva;
7939 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07007940 struct {
7941 u64 vpid;
7942 u64 gla;
7943 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007944
7945 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7946 SECONDARY_EXEC_ENABLE_VPID) ||
7947 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7948 kvm_queue_exception(vcpu, UD_VECTOR);
7949 return 1;
7950 }
7951
7952 if (!nested_vmx_check_permission(vcpu))
7953 return 1;
7954
7955 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7956 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7957
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007958 types = (vmx->nested.nested_vmx_vpid_caps &
7959 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007960
Jim Mattson85c856b2016-10-26 08:38:38 -07007961 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007962 nested_vmx_failValid(vcpu,
7963 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007964 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007965 }
7966
7967 /* according to the intel vmx instruction reference, the memory
7968 * operand is read even if it isn't needed (e.g., for type==global)
7969 */
7970 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7971 vmx_instruction_info, false, &gva))
7972 return 1;
Jim Mattson40352602017-06-28 09:37:37 -07007973 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7974 sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007975 kvm_inject_page_fault(vcpu, &e);
7976 return 1;
7977 }
Jim Mattson40352602017-06-28 09:37:37 -07007978 if (operand.vpid >> 16) {
7979 nested_vmx_failValid(vcpu,
7980 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7981 return kvm_skip_emulated_instruction(vcpu);
7982 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007983
7984 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007985 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Yu Zhangfd8cb432017-08-24 20:27:56 +08007986 if (is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07007987 nested_vmx_failValid(vcpu,
7988 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7989 return kvm_skip_emulated_instruction(vcpu);
7990 }
7991 /* fall through */
Paolo Bonzinief697a72016-03-18 16:58:38 +01007992 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007993 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07007994 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007995 nested_vmx_failValid(vcpu,
7996 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007997 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007998 }
7999 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008000 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008001 break;
8002 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008003 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008004 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008005 }
8006
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008007 __vmx_flush_tlb(vcpu, vmx->nested.vpid02);
8008 nested_vmx_succeed(vcpu);
8009
Kyle Huey6affcbe2016-11-29 12:40:40 -08008010 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008011}
8012
Kai Huang843e4332015-01-28 10:54:28 +08008013static int handle_pml_full(struct kvm_vcpu *vcpu)
8014{
8015 unsigned long exit_qualification;
8016
8017 trace_kvm_pml_full(vcpu->vcpu_id);
8018
8019 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8020
8021 /*
8022 * PML buffer FULL happened while executing iret from NMI,
8023 * "blocked by NMI" bit has to be set before next VM entry.
8024 */
8025 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008026 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008027 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8028 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8029 GUEST_INTR_STATE_NMI);
8030
8031 /*
8032 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8033 * here.., and there's no userspace involvement needed for PML.
8034 */
8035 return 1;
8036}
8037
Yunhong Jiang64672c92016-06-13 14:19:59 -07008038static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8039{
8040 kvm_lapic_expired_hv_timer(vcpu);
8041 return 1;
8042}
8043
Bandan Das41ab9372017-08-03 15:54:43 -04008044static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8045{
8046 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008047 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8048
8049 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008050 switch (address & VMX_EPTP_MT_MASK) {
8051 case VMX_EPTP_MT_UC:
Bandan Das41ab9372017-08-03 15:54:43 -04008052 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_UC_BIT))
8053 return false;
8054 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008055 case VMX_EPTP_MT_WB:
Bandan Das41ab9372017-08-03 15:54:43 -04008056 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_WB_BIT))
8057 return false;
8058 break;
8059 default:
8060 return false;
8061 }
8062
David Hildenbrandbb97a012017-08-10 23:15:28 +02008063 /* only 4 levels page-walk length are valid */
8064 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008065 return false;
8066
8067 /* Reserved bits should not be set */
8068 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8069 return false;
8070
8071 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008072 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Bandan Das41ab9372017-08-03 15:54:43 -04008073 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPT_AD_BIT))
8074 return false;
8075 }
8076
8077 return true;
8078}
8079
8080static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8081 struct vmcs12 *vmcs12)
8082{
8083 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8084 u64 address;
8085 bool accessed_dirty;
8086 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8087
8088 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8089 !nested_cpu_has_ept(vmcs12))
8090 return 1;
8091
8092 if (index >= VMFUNC_EPTP_ENTRIES)
8093 return 1;
8094
8095
8096 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8097 &address, index * 8, 8))
8098 return 1;
8099
David Hildenbrandbb97a012017-08-10 23:15:28 +02008100 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008101
8102 /*
8103 * If the (L2) guest does a vmfunc to the currently
8104 * active ept pointer, we don't have to do anything else
8105 */
8106 if (vmcs12->ept_pointer != address) {
8107 if (!valid_ept_address(vcpu, address))
8108 return 1;
8109
8110 kvm_mmu_unload(vcpu);
8111 mmu->ept_ad = accessed_dirty;
8112 mmu->base_role.ad_disabled = !accessed_dirty;
8113 vmcs12->ept_pointer = address;
8114 /*
8115 * TODO: Check what's the correct approach in case
8116 * mmu reload fails. Currently, we just let the next
8117 * reload potentially fail
8118 */
8119 kvm_mmu_reload(vcpu);
8120 }
8121
8122 return 0;
8123}
8124
Bandan Das2a499e42017-08-03 15:54:41 -04008125static int handle_vmfunc(struct kvm_vcpu *vcpu)
8126{
Bandan Das27c42a12017-08-03 15:54:42 -04008127 struct vcpu_vmx *vmx = to_vmx(vcpu);
8128 struct vmcs12 *vmcs12;
8129 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8130
8131 /*
8132 * VMFUNC is only supported for nested guests, but we always enable the
8133 * secondary control for simplicity; for non-nested mode, fake that we
8134 * didn't by injecting #UD.
8135 */
8136 if (!is_guest_mode(vcpu)) {
8137 kvm_queue_exception(vcpu, UD_VECTOR);
8138 return 1;
8139 }
8140
8141 vmcs12 = get_vmcs12(vcpu);
8142 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8143 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008144
8145 switch (function) {
8146 case 0:
8147 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8148 goto fail;
8149 break;
8150 default:
8151 goto fail;
8152 }
8153 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008154
8155fail:
8156 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8157 vmcs_read32(VM_EXIT_INTR_INFO),
8158 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008159 return 1;
8160}
8161
Nadav Har'El0140cae2011-05-25 23:06:28 +03008162/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008163 * The exit handlers return 1 if the exit was handled fully and guest execution
8164 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8165 * to be done to userspace and return 0.
8166 */
Mathias Krause772e0312012-08-30 01:30:19 +02008167static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008168 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8169 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008170 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008171 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008172 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008173 [EXIT_REASON_CR_ACCESS] = handle_cr,
8174 [EXIT_REASON_DR_ACCESS] = handle_dr,
8175 [EXIT_REASON_CPUID] = handle_cpuid,
8176 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8177 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8178 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8179 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008180 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008181 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008182 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008183 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008184 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008185 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008186 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008187 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008188 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008189 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008190 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008191 [EXIT_REASON_VMOFF] = handle_vmoff,
8192 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008193 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8194 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008195 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008196 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008197 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008198 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008199 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008200 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008201 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8202 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008203 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008204 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008205 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008206 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008207 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008208 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008209 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008210 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008211 [EXIT_REASON_XSAVES] = handle_xsaves,
8212 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008213 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008214 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008215 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008216};
8217
8218static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008219 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008220
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008221static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8222 struct vmcs12 *vmcs12)
8223{
8224 unsigned long exit_qualification;
8225 gpa_t bitmap, last_bitmap;
8226 unsigned int port;
8227 int size;
8228 u8 b;
8229
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008230 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008231 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008232
8233 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8234
8235 port = exit_qualification >> 16;
8236 size = (exit_qualification & 7) + 1;
8237
8238 last_bitmap = (gpa_t)-1;
8239 b = -1;
8240
8241 while (size > 0) {
8242 if (port < 0x8000)
8243 bitmap = vmcs12->io_bitmap_a;
8244 else if (port < 0x10000)
8245 bitmap = vmcs12->io_bitmap_b;
8246 else
Joe Perches1d804d02015-03-30 16:46:09 -07008247 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008248 bitmap += (port & 0x7fff) / 8;
8249
8250 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008251 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008252 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008253 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008254 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008255
8256 port++;
8257 size--;
8258 last_bitmap = bitmap;
8259 }
8260
Joe Perches1d804d02015-03-30 16:46:09 -07008261 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008262}
8263
Nadav Har'El644d7112011-05-25 23:12:35 +03008264/*
8265 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8266 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8267 * disinterest in the current event (read or write a specific MSR) by using an
8268 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8269 */
8270static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8271 struct vmcs12 *vmcs12, u32 exit_reason)
8272{
8273 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8274 gpa_t bitmap;
8275
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008276 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008277 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008278
8279 /*
8280 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8281 * for the four combinations of read/write and low/high MSR numbers.
8282 * First we need to figure out which of the four to use:
8283 */
8284 bitmap = vmcs12->msr_bitmap;
8285 if (exit_reason == EXIT_REASON_MSR_WRITE)
8286 bitmap += 2048;
8287 if (msr_index >= 0xc0000000) {
8288 msr_index -= 0xc0000000;
8289 bitmap += 1024;
8290 }
8291
8292 /* Then read the msr_index'th bit from this bitmap: */
8293 if (msr_index < 1024*8) {
8294 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008295 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008296 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008297 return 1 & (b >> (msr_index & 7));
8298 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008299 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008300}
8301
8302/*
8303 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8304 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8305 * intercept (via guest_host_mask etc.) the current event.
8306 */
8307static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8308 struct vmcs12 *vmcs12)
8309{
8310 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8311 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008312 int reg;
8313 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008314
8315 switch ((exit_qualification >> 4) & 3) {
8316 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008317 reg = (exit_qualification >> 8) & 15;
8318 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008319 switch (cr) {
8320 case 0:
8321 if (vmcs12->cr0_guest_host_mask &
8322 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008323 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008324 break;
8325 case 3:
8326 if ((vmcs12->cr3_target_count >= 1 &&
8327 vmcs12->cr3_target_value0 == val) ||
8328 (vmcs12->cr3_target_count >= 2 &&
8329 vmcs12->cr3_target_value1 == val) ||
8330 (vmcs12->cr3_target_count >= 3 &&
8331 vmcs12->cr3_target_value2 == val) ||
8332 (vmcs12->cr3_target_count >= 4 &&
8333 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008334 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008335 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008336 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008337 break;
8338 case 4:
8339 if (vmcs12->cr4_guest_host_mask &
8340 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008341 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008342 break;
8343 case 8:
8344 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008345 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008346 break;
8347 }
8348 break;
8349 case 2: /* clts */
8350 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8351 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008352 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008353 break;
8354 case 1: /* mov from cr */
8355 switch (cr) {
8356 case 3:
8357 if (vmcs12->cpu_based_vm_exec_control &
8358 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008359 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008360 break;
8361 case 8:
8362 if (vmcs12->cpu_based_vm_exec_control &
8363 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008364 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008365 break;
8366 }
8367 break;
8368 case 3: /* lmsw */
8369 /*
8370 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8371 * cr0. Other attempted changes are ignored, with no exit.
8372 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008373 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008374 if (vmcs12->cr0_guest_host_mask & 0xe &
8375 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008376 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008377 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8378 !(vmcs12->cr0_read_shadow & 0x1) &&
8379 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008380 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008381 break;
8382 }
Joe Perches1d804d02015-03-30 16:46:09 -07008383 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008384}
8385
8386/*
8387 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8388 * should handle it ourselves in L0 (and then continue L2). Only call this
8389 * when in is_guest_mode (L2).
8390 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02008391static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03008392{
Nadav Har'El644d7112011-05-25 23:12:35 +03008393 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8394 struct vcpu_vmx *vmx = to_vmx(vcpu);
8395 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8396
Jim Mattson4f350c62017-09-14 16:31:44 -07008397 if (vmx->nested.nested_run_pending)
8398 return false;
8399
8400 if (unlikely(vmx->fail)) {
8401 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8402 vmcs_read32(VM_INSTRUCTION_ERROR));
8403 return true;
8404 }
Jan Kiszka542060e2014-01-04 18:47:21 +01008405
David Matlackc9f04402017-08-01 14:00:40 -07008406 /*
8407 * The host physical addresses of some pages of guest memory
8408 * are loaded into VMCS02 (e.g. L1's Virtual APIC Page). The CPU
8409 * may write to these pages via their host physical address while
8410 * L2 is running, bypassing any address-translation-based dirty
8411 * tracking (e.g. EPT write protection).
8412 *
8413 * Mark them dirty on every exit from L2 to prevent them from
8414 * getting out of sync with dirty tracking.
8415 */
8416 nested_mark_vmcs12_pages_dirty(vcpu);
8417
Jim Mattson4f350c62017-09-14 16:31:44 -07008418 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8419 vmcs_readl(EXIT_QUALIFICATION),
8420 vmx->idt_vectoring_info,
8421 intr_info,
8422 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8423 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03008424
8425 switch (exit_reason) {
8426 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008427 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008428 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008429 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07008430 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008431 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008432 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008433 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008434 else if (is_debug(intr_info) &&
8435 vcpu->guest_debug &
8436 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8437 return false;
8438 else if (is_breakpoint(intr_info) &&
8439 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8440 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008441 return vmcs12->exception_bitmap &
8442 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8443 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008444 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008445 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008446 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008447 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008448 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008449 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008450 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008451 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008452 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008453 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008454 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008455 case EXIT_REASON_HLT:
8456 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8457 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008458 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008459 case EXIT_REASON_INVLPG:
8460 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8461 case EXIT_REASON_RDPMC:
8462 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008463 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008464 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008465 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008466 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008467 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008468 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8469 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8470 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8471 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8472 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8473 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008474 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008475 /*
8476 * VMX instructions trap unconditionally. This allows L1 to
8477 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8478 */
Joe Perches1d804d02015-03-30 16:46:09 -07008479 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008480 case EXIT_REASON_CR_ACCESS:
8481 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8482 case EXIT_REASON_DR_ACCESS:
8483 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8484 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008485 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008486 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8487 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03008488 case EXIT_REASON_MSR_READ:
8489 case EXIT_REASON_MSR_WRITE:
8490 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8491 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008492 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008493 case EXIT_REASON_MWAIT_INSTRUCTION:
8494 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008495 case EXIT_REASON_MONITOR_TRAP_FLAG:
8496 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008497 case EXIT_REASON_MONITOR_INSTRUCTION:
8498 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8499 case EXIT_REASON_PAUSE_INSTRUCTION:
8500 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8501 nested_cpu_has2(vmcs12,
8502 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8503 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008504 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008505 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008506 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008507 case EXIT_REASON_APIC_ACCESS:
8508 return nested_cpu_has2(vmcs12,
8509 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008510 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008511 case EXIT_REASON_EOI_INDUCED:
8512 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008513 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008514 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008515 /*
8516 * L0 always deals with the EPT violation. If nested EPT is
8517 * used, and the nested mmu code discovers that the address is
8518 * missing in the guest EPT table (EPT12), the EPT violation
8519 * will be injected with nested_ept_inject_page_fault()
8520 */
Joe Perches1d804d02015-03-30 16:46:09 -07008521 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008522 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008523 /*
8524 * L2 never uses directly L1's EPT, but rather L0's own EPT
8525 * table (shadow on EPT) or a merged EPT table that L0 built
8526 * (EPT on EPT). So any problems with the structure of the
8527 * table is L0's fault.
8528 */
Joe Perches1d804d02015-03-30 16:46:09 -07008529 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02008530 case EXIT_REASON_INVPCID:
8531 return
8532 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8533 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008534 case EXIT_REASON_WBINVD:
8535 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8536 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008537 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008538 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8539 /*
8540 * This should never happen, since it is not possible to
8541 * set XSS to a non-zero value---neither in L1 nor in L2.
8542 * If if it were, XSS would have to be checked against
8543 * the XSS exit bitmap in vmcs12.
8544 */
8545 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008546 case EXIT_REASON_PREEMPTION_TIMER:
8547 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02008548 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04008549 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02008550 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04008551 case EXIT_REASON_VMFUNC:
8552 /* VM functions are emulated through L2->L0 vmexits. */
8553 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008554 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008555 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008556 }
8557}
8558
Paolo Bonzini7313c692017-07-27 10:31:25 +02008559static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8560{
8561 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8562
8563 /*
8564 * At this point, the exit interruption info in exit_intr_info
8565 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8566 * we need to query the in-kernel LAPIC.
8567 */
8568 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8569 if ((exit_intr_info &
8570 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8571 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8572 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8573 vmcs12->vm_exit_intr_error_code =
8574 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8575 }
8576
8577 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8578 vmcs_readl(EXIT_QUALIFICATION));
8579 return 1;
8580}
8581
Avi Kivity586f9602010-11-18 13:09:54 +02008582static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8583{
8584 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8585 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8586}
8587
Kai Huanga3eaa862015-11-04 13:46:05 +08008588static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008589{
Kai Huanga3eaa862015-11-04 13:46:05 +08008590 if (vmx->pml_pg) {
8591 __free_page(vmx->pml_pg);
8592 vmx->pml_pg = NULL;
8593 }
Kai Huang843e4332015-01-28 10:54:28 +08008594}
8595
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008596static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008597{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008598 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008599 u64 *pml_buf;
8600 u16 pml_idx;
8601
8602 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8603
8604 /* Do nothing if PML buffer is empty */
8605 if (pml_idx == (PML_ENTITY_NUM - 1))
8606 return;
8607
8608 /* PML index always points to next available PML buffer entity */
8609 if (pml_idx >= PML_ENTITY_NUM)
8610 pml_idx = 0;
8611 else
8612 pml_idx++;
8613
8614 pml_buf = page_address(vmx->pml_pg);
8615 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8616 u64 gpa;
8617
8618 gpa = pml_buf[pml_idx];
8619 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008620 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008621 }
8622
8623 /* reset PML index */
8624 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8625}
8626
8627/*
8628 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8629 * Called before reporting dirty_bitmap to userspace.
8630 */
8631static void kvm_flush_pml_buffers(struct kvm *kvm)
8632{
8633 int i;
8634 struct kvm_vcpu *vcpu;
8635 /*
8636 * We only need to kick vcpu out of guest mode here, as PML buffer
8637 * is flushed at beginning of all VMEXITs, and it's obvious that only
8638 * vcpus running in guest are possible to have unflushed GPAs in PML
8639 * buffer.
8640 */
8641 kvm_for_each_vcpu(i, vcpu, kvm)
8642 kvm_vcpu_kick(vcpu);
8643}
8644
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008645static void vmx_dump_sel(char *name, uint32_t sel)
8646{
8647 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05008648 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008649 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8650 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8651 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8652}
8653
8654static void vmx_dump_dtsel(char *name, uint32_t limit)
8655{
8656 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8657 name, vmcs_read32(limit),
8658 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8659}
8660
8661static void dump_vmcs(void)
8662{
8663 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8664 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8665 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8666 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8667 u32 secondary_exec_control = 0;
8668 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008669 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008670 int i, n;
8671
8672 if (cpu_has_secondary_exec_ctrls())
8673 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8674
8675 pr_err("*** Guest State ***\n");
8676 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8677 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8678 vmcs_readl(CR0_GUEST_HOST_MASK));
8679 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8680 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8681 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8682 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8683 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8684 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008685 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8686 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8687 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8688 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008689 }
8690 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8691 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8692 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8693 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8694 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8695 vmcs_readl(GUEST_SYSENTER_ESP),
8696 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8697 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8698 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8699 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8700 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8701 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8702 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8703 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8704 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8705 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8706 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8707 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8708 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008709 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8710 efer, vmcs_read64(GUEST_IA32_PAT));
8711 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8712 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008713 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8714 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008715 pr_err("PerfGlobCtl = 0x%016llx\n",
8716 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008717 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008718 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008719 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8720 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8721 vmcs_read32(GUEST_ACTIVITY_STATE));
8722 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8723 pr_err("InterruptStatus = %04x\n",
8724 vmcs_read16(GUEST_INTR_STATUS));
8725
8726 pr_err("*** Host State ***\n");
8727 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8728 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8729 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8730 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8731 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8732 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8733 vmcs_read16(HOST_TR_SELECTOR));
8734 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8735 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8736 vmcs_readl(HOST_TR_BASE));
8737 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8738 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8739 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8740 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8741 vmcs_readl(HOST_CR4));
8742 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8743 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8744 vmcs_read32(HOST_IA32_SYSENTER_CS),
8745 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8746 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008747 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8748 vmcs_read64(HOST_IA32_EFER),
8749 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008750 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008751 pr_err("PerfGlobCtl = 0x%016llx\n",
8752 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008753
8754 pr_err("*** Control State ***\n");
8755 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8756 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8757 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8758 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8759 vmcs_read32(EXCEPTION_BITMAP),
8760 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8761 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8762 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8763 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8764 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8765 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8766 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8767 vmcs_read32(VM_EXIT_INTR_INFO),
8768 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8769 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8770 pr_err(" reason=%08x qualification=%016lx\n",
8771 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8772 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8773 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8774 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008775 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008776 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008777 pr_err("TSC Multiplier = 0x%016llx\n",
8778 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008779 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8780 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8781 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8782 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8783 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008784 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008785 n = vmcs_read32(CR3_TARGET_COUNT);
8786 for (i = 0; i + 1 < n; i += 4)
8787 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8788 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8789 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8790 if (i < n)
8791 pr_err("CR3 target%u=%016lx\n",
8792 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8793 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8794 pr_err("PLE Gap=%08x Window=%08x\n",
8795 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8796 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8797 pr_err("Virtual processor ID = 0x%04x\n",
8798 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8799}
8800
Avi Kivity6aa8b732006-12-10 02:21:36 -08008801/*
8802 * The guest has exited. See if we can fix it or if we need userspace
8803 * assistance.
8804 */
Avi Kivity851ba692009-08-24 11:10:17 +03008805static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008806{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008807 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008808 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008809 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008810
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008811 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8812
Kai Huang843e4332015-01-28 10:54:28 +08008813 /*
8814 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8815 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8816 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8817 * mode as if vcpus is in root mode, the PML buffer must has been
8818 * flushed already.
8819 */
8820 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008821 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008822
Mohammed Gamal80ced182009-09-01 12:48:18 +02008823 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008824 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008825 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008826
Paolo Bonzini7313c692017-07-27 10:31:25 +02008827 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
8828 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03008829
Mohammed Gamal51207022010-05-31 22:40:54 +03008830 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008831 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008832 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8833 vcpu->run->fail_entry.hardware_entry_failure_reason
8834 = exit_reason;
8835 return 0;
8836 }
8837
Avi Kivity29bd8a72007-09-10 17:27:03 +03008838 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008839 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8840 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008841 = vmcs_read32(VM_INSTRUCTION_ERROR);
8842 return 0;
8843 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008844
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008845 /*
8846 * Note:
8847 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8848 * delivery event since it indicates guest is accessing MMIO.
8849 * The vm-exit can be triggered again after return to guest that
8850 * will cause infinite loop.
8851 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008852 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008853 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008854 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008855 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008856 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8857 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8858 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02008859 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008860 vcpu->run->internal.data[0] = vectoring_info;
8861 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02008862 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
8863 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
8864 vcpu->run->internal.ndata++;
8865 vcpu->run->internal.data[3] =
8866 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
8867 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008868 return 0;
8869 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008870
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008871 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01008872 vmx->loaded_vmcs->soft_vnmi_blocked)) {
8873 if (vmx_interrupt_allowed(vcpu)) {
8874 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8875 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
8876 vcpu->arch.nmi_pending) {
8877 /*
8878 * This CPU don't support us in finding the end of an
8879 * NMI-blocked window if the guest runs with IRQs
8880 * disabled. So we pull the trigger after 1 s of
8881 * futile waiting, but inform the user about this.
8882 */
8883 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8884 "state on VCPU %d after 1 s timeout\n",
8885 __func__, vcpu->vcpu_id);
8886 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8887 }
8888 }
8889
Avi Kivity6aa8b732006-12-10 02:21:36 -08008890 if (exit_reason < kvm_vmx_max_exit_handlers
8891 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008892 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008893 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01008894 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
8895 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008896 kvm_queue_exception(vcpu, UD_VECTOR);
8897 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008898 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008899}
8900
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008901static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008902{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008903 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8904
8905 if (is_guest_mode(vcpu) &&
8906 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8907 return;
8908
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008909 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008910 vmcs_write32(TPR_THRESHOLD, 0);
8911 return;
8912 }
8913
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008914 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008915}
8916
Yang Zhang8d146952013-01-25 10:18:50 +08008917static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8918{
8919 u32 sec_exec_control;
8920
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008921 /* Postpone execution until vmcs01 is the current VMCS. */
8922 if (is_guest_mode(vcpu)) {
8923 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8924 return;
8925 }
8926
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008927 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008928 return;
8929
Paolo Bonzini35754c92015-07-29 12:05:37 +02008930 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008931 return;
8932
8933 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8934
8935 if (set) {
8936 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8937 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8938 } else {
8939 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8940 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008941 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008942 }
8943 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8944
8945 vmx_set_msr_bitmap(vcpu);
8946}
8947
Tang Chen38b99172014-09-24 15:57:54 +08008948static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8949{
8950 struct vcpu_vmx *vmx = to_vmx(vcpu);
8951
8952 /*
8953 * Currently we do not handle the nested case where L2 has an
8954 * APIC access page of its own; that page is still pinned.
8955 * Hence, we skip the case where the VCPU is in guest mode _and_
8956 * L1 prepared an APIC access page for L2.
8957 *
8958 * For the case where L1 and L2 share the same APIC access page
8959 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8960 * in the vmcs12), this function will only update either the vmcs01
8961 * or the vmcs02. If the former, the vmcs02 will be updated by
8962 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8963 * the next L2->L1 exit.
8964 */
8965 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008966 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008967 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08008968 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008969 vmx_flush_tlb_ept_only(vcpu);
8970 }
Tang Chen38b99172014-09-24 15:57:54 +08008971}
8972
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008973static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008974{
8975 u16 status;
8976 u8 old;
8977
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008978 if (max_isr == -1)
8979 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008980
8981 status = vmcs_read16(GUEST_INTR_STATUS);
8982 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008983 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008984 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008985 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008986 vmcs_write16(GUEST_INTR_STATUS, status);
8987 }
8988}
8989
8990static void vmx_set_rvi(int vector)
8991{
8992 u16 status;
8993 u8 old;
8994
Wei Wang4114c272014-11-05 10:53:43 +08008995 if (vector == -1)
8996 vector = 0;
8997
Yang Zhangc7c9c562013-01-25 10:18:51 +08008998 status = vmcs_read16(GUEST_INTR_STATUS);
8999 old = (u8)status & 0xff;
9000 if ((u8)vector != old) {
9001 status &= ~0xff;
9002 status |= (u8)vector;
9003 vmcs_write16(GUEST_INTR_STATUS, status);
9004 }
9005}
9006
9007static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9008{
Wanpeng Li963fee12014-07-17 19:03:00 +08009009 if (!is_guest_mode(vcpu)) {
9010 vmx_set_rvi(max_irr);
9011 return;
9012 }
9013
Wei Wang4114c272014-11-05 10:53:43 +08009014 if (max_irr == -1)
9015 return;
9016
Wanpeng Li963fee12014-07-17 19:03:00 +08009017 /*
Wei Wang4114c272014-11-05 10:53:43 +08009018 * In guest mode. If a vmexit is needed, vmx_check_nested_events
9019 * handles it.
9020 */
9021 if (nested_exit_on_intr(vcpu))
9022 return;
9023
9024 /*
9025 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08009026 * is run without virtual interrupt delivery.
9027 */
9028 if (!kvm_event_needs_reinjection(vcpu) &&
9029 vmx_interrupt_allowed(vcpu)) {
9030 kvm_queue_interrupt(vcpu, max_irr, false);
9031 vmx_inject_irq(vcpu);
9032 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08009033}
9034
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009035static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009036{
9037 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009038 int max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009039
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009040 WARN_ON(!vcpu->arch.apicv_active);
9041 if (pi_test_on(&vmx->pi_desc)) {
9042 pi_clear_on(&vmx->pi_desc);
9043 /*
9044 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9045 * But on x86 this is just a compiler barrier anyway.
9046 */
9047 smp_mb__after_atomic();
9048 max_irr = kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
9049 } else {
9050 max_irr = kvm_lapic_find_highest_irr(vcpu);
9051 }
9052 vmx_hwapic_irr_update(vcpu, max_irr);
9053 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009054}
9055
Andrey Smetanin63086302015-11-10 15:36:32 +03009056static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009057{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009058 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009059 return;
9060
Yang Zhangc7c9c562013-01-25 10:18:51 +08009061 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9062 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9063 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9064 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9065}
9066
Paolo Bonzini967235d2016-12-19 14:03:45 +01009067static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9068{
9069 struct vcpu_vmx *vmx = to_vmx(vcpu);
9070
9071 pi_clear_on(&vmx->pi_desc);
9072 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9073}
9074
Avi Kivity51aa01d2010-07-20 14:31:20 +03009075static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009076{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009077 u32 exit_intr_info = 0;
9078 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009079
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009080 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9081 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009082 return;
9083
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009084 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9085 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9086 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009087
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009088 /* if exit due to PF check for async PF */
9089 if (is_page_fault(exit_intr_info))
9090 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9091
Andi Kleena0861c02009-06-08 17:37:09 +08009092 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009093 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9094 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009095 kvm_machine_check();
9096
Gleb Natapov20f65982009-05-11 13:35:55 +03009097 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009098 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009099 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009100 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009101 kvm_after_handle_nmi(&vmx->vcpu);
9102 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009103}
Gleb Natapov20f65982009-05-11 13:35:55 +03009104
Yang Zhanga547c6d2013-04-11 19:25:10 +08009105static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9106{
9107 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9108
Yang Zhanga547c6d2013-04-11 19:25:10 +08009109 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9110 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9111 unsigned int vector;
9112 unsigned long entry;
9113 gate_desc *desc;
9114 struct vcpu_vmx *vmx = to_vmx(vcpu);
9115#ifdef CONFIG_X86_64
9116 unsigned long tmp;
9117#endif
9118
9119 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9120 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009121 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009122 asm volatile(
9123#ifdef CONFIG_X86_64
9124 "mov %%" _ASM_SP ", %[sp]\n\t"
9125 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9126 "push $%c[ss]\n\t"
9127 "push %[sp]\n\t"
9128#endif
9129 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009130 __ASM_SIZE(push) " $%c[cs]\n\t"
9131 "call *%[entry]\n\t"
9132 :
9133#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009134 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009135#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009136 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009137 :
9138 [entry]"r"(entry),
9139 [ss]"i"(__KERNEL_DS),
9140 [cs]"i"(__KERNEL_CS)
9141 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009142 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009143}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009144STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009145
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009146static bool vmx_has_high_real_mode_segbase(void)
9147{
9148 return enable_unrestricted_guest || emulate_invalid_guest_state;
9149}
9150
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009151static bool vmx_mpx_supported(void)
9152{
9153 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9154 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9155}
9156
Wanpeng Li55412b22014-12-02 19:21:30 +08009157static bool vmx_xsaves_supported(void)
9158{
9159 return vmcs_config.cpu_based_2nd_exec_ctrl &
9160 SECONDARY_EXEC_XSAVES;
9161}
9162
Avi Kivity51aa01d2010-07-20 14:31:20 +03009163static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9164{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009165 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009166 bool unblock_nmi;
9167 u8 vector;
9168 bool idtv_info_valid;
9169
9170 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009171
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009172 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009173 if (vmx->loaded_vmcs->nmi_known_unmasked)
9174 return;
9175 /*
9176 * Can't use vmx->exit_intr_info since we're not sure what
9177 * the exit reason is.
9178 */
9179 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9180 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9181 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9182 /*
9183 * SDM 3: 27.7.1.2 (September 2008)
9184 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9185 * a guest IRET fault.
9186 * SDM 3: 23.2.2 (September 2008)
9187 * Bit 12 is undefined in any of the following cases:
9188 * If the VM exit sets the valid bit in the IDT-vectoring
9189 * information field.
9190 * If the VM exit is due to a double fault.
9191 */
9192 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9193 vector != DF_VECTOR && !idtv_info_valid)
9194 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9195 GUEST_INTR_STATE_NMI);
9196 else
9197 vmx->loaded_vmcs->nmi_known_unmasked =
9198 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9199 & GUEST_INTR_STATE_NMI);
9200 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9201 vmx->loaded_vmcs->vnmi_blocked_time +=
9202 ktime_to_ns(ktime_sub(ktime_get(),
9203 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009204}
9205
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009206static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009207 u32 idt_vectoring_info,
9208 int instr_len_field,
9209 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009210{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009211 u8 vector;
9212 int type;
9213 bool idtv_info_valid;
9214
9215 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009216
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009217 vcpu->arch.nmi_injected = false;
9218 kvm_clear_exception_queue(vcpu);
9219 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009220
9221 if (!idtv_info_valid)
9222 return;
9223
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009224 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009225
Avi Kivity668f6122008-07-02 09:28:55 +03009226 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9227 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009228
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009229 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009230 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009231 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009232 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009233 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009234 * Clear bit "block by NMI" before VM entry if a NMI
9235 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009236 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009237 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009238 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009239 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009240 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009241 /* fall through */
9242 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009243 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009244 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009245 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009246 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009247 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009248 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009249 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009250 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009251 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009252 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009253 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009254 break;
9255 default:
9256 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009257 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009258}
9259
Avi Kivity83422e12010-07-20 14:43:23 +03009260static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9261{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009262 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009263 VM_EXIT_INSTRUCTION_LEN,
9264 IDT_VECTORING_ERROR_CODE);
9265}
9266
Avi Kivityb463a6f2010-07-20 15:06:17 +03009267static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9268{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009269 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009270 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9271 VM_ENTRY_INSTRUCTION_LEN,
9272 VM_ENTRY_EXCEPTION_ERROR_CODE);
9273
9274 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9275}
9276
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009277static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9278{
9279 int i, nr_msrs;
9280 struct perf_guest_switch_msr *msrs;
9281
9282 msrs = perf_guest_get_msrs(&nr_msrs);
9283
9284 if (!msrs)
9285 return;
9286
9287 for (i = 0; i < nr_msrs; i++)
9288 if (msrs[i].host == msrs[i].guest)
9289 clear_atomic_switch_msr(vmx, msrs[i].msr);
9290 else
9291 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9292 msrs[i].host);
9293}
9294
Jiang Biao33365e72016-11-03 15:03:37 +08009295static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009296{
9297 struct vcpu_vmx *vmx = to_vmx(vcpu);
9298 u64 tscl;
9299 u32 delta_tsc;
9300
9301 if (vmx->hv_deadline_tsc == -1)
9302 return;
9303
9304 tscl = rdtsc();
9305 if (vmx->hv_deadline_tsc > tscl)
9306 /* sure to be 32 bit only because checked on set_hv_timer */
9307 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9308 cpu_preemption_timer_multi);
9309 else
9310 delta_tsc = 0;
9311
9312 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9313}
9314
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009315static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009316{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009317 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009318 unsigned long debugctlmsr, cr3, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02009319
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009320 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009321 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009322 vmx->loaded_vmcs->soft_vnmi_blocked))
9323 vmx->loaded_vmcs->entry_time = ktime_get();
9324
Avi Kivity104f2262010-11-18 13:12:52 +02009325 /* Don't enter VMX if guest state is invalid, let the exit handler
9326 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009327 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009328 return;
9329
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009330 if (vmx->ple_window_dirty) {
9331 vmx->ple_window_dirty = false;
9332 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9333 }
9334
Abel Gordon012f83c2013-04-18 14:39:25 +03009335 if (vmx->nested.sync_shadow_vmcs) {
9336 copy_vmcs12_to_shadow(vmx);
9337 vmx->nested.sync_shadow_vmcs = false;
9338 }
9339
Avi Kivity104f2262010-11-18 13:12:52 +02009340 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9341 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9342 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9343 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9344
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009345 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +02009346 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009347 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +02009348 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009349 }
9350
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009351 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +02009352 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009353 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +02009354 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009355 }
9356
Avi Kivity104f2262010-11-18 13:12:52 +02009357 /* When single-stepping over STI and MOV SS, we must clear the
9358 * corresponding interruptibility bits in the guest state. Otherwise
9359 * vmentry fails as it then expects bit 14 (BS) in pending debug
9360 * exceptions being set, but that's not correct for the guest debugging
9361 * case. */
9362 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9363 vmx_set_interrupt_shadow(vcpu, 0);
9364
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009365 if (static_cpu_has(X86_FEATURE_PKU) &&
9366 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9367 vcpu->arch.pkru != vmx->host_pkru)
9368 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009369
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009370 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009371 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009372
Yunhong Jiang64672c92016-06-13 14:19:59 -07009373 vmx_arm_hv_timer(vcpu);
9374
Nadav Har'Eld462b812011-05-24 15:26:10 +03009375 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02009376 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009377 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009378 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9379 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9380 "push %%" _ASM_CX " \n\t"
9381 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009382 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009383 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009384 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009385 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009386 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009387 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9388 "mov %%cr2, %%" _ASM_DX " \n\t"
9389 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009390 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009391 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009392 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009393 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009394 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009395 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009396 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9397 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9398 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9399 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9400 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9401 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009402#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009403 "mov %c[r8](%0), %%r8 \n\t"
9404 "mov %c[r9](%0), %%r9 \n\t"
9405 "mov %c[r10](%0), %%r10 \n\t"
9406 "mov %c[r11](%0), %%r11 \n\t"
9407 "mov %c[r12](%0), %%r12 \n\t"
9408 "mov %c[r13](%0), %%r13 \n\t"
9409 "mov %c[r14](%0), %%r14 \n\t"
9410 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009411#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009412 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009413
Avi Kivity6aa8b732006-12-10 02:21:36 -08009414 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009415 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009416 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009417 "jmp 2f \n\t"
9418 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9419 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009420 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009421 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009422 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009423 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9424 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9425 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9426 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9427 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9428 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9429 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009430#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009431 "mov %%r8, %c[r8](%0) \n\t"
9432 "mov %%r9, %c[r9](%0) \n\t"
9433 "mov %%r10, %c[r10](%0) \n\t"
9434 "mov %%r11, %c[r11](%0) \n\t"
9435 "mov %%r12, %c[r12](%0) \n\t"
9436 "mov %%r13, %c[r13](%0) \n\t"
9437 "mov %%r14, %c[r14](%0) \n\t"
9438 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009439#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009440 "mov %%cr2, %%" _ASM_AX " \n\t"
9441 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009442
Avi Kivityb188c81f2012-09-16 15:10:58 +03009443 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02009444 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009445 ".pushsection .rodata \n\t"
9446 ".global vmx_return \n\t"
9447 "vmx_return: " _ASM_PTR " 2b \n\t"
9448 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009449 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009450 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009451 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03009452 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009453 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9454 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9455 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9456 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9457 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9458 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9459 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009460#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009461 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9462 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9463 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9464 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9465 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9466 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9467 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9468 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009469#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009470 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9471 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009472 : "cc", "memory"
9473#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009474 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009475 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009476#else
9477 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009478#endif
9479 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009480
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009481 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9482 if (debugctlmsr)
9483 update_debugctlmsr(debugctlmsr);
9484
Avi Kivityaa67f602012-08-01 16:48:03 +03009485#ifndef CONFIG_X86_64
9486 /*
9487 * The sysexit path does not restore ds/es, so we must set them to
9488 * a reasonable value ourselves.
9489 *
9490 * We can't defer this to vmx_load_host_state() since that function
9491 * may be executed in interrupt context, which saves and restore segments
9492 * around it, nullifying its effect.
9493 */
9494 loadsegment(ds, __USER_DS);
9495 loadsegment(es, __USER_DS);
9496#endif
9497
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009498 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009499 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009500 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009501 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009502 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009503 vcpu->arch.regs_dirty = 0;
9504
Gleb Natapove0b890d2013-09-25 12:51:33 +03009505 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009506 * eager fpu is enabled if PKEY is supported and CR4 is switched
9507 * back on host, so it is safe to read guest PKRU from current
9508 * XSAVE.
9509 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009510 if (static_cpu_has(X86_FEATURE_PKU) &&
9511 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9512 vcpu->arch.pkru = __read_pkru();
9513 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009514 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009515 }
9516
9517 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009518 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9519 * we did not inject a still-pending event to L1 now because of
9520 * nested_run_pending, we need to re-enable this bit.
9521 */
9522 if (vmx->nested.nested_run_pending)
9523 kvm_make_request(KVM_REQ_EVENT, vcpu);
9524
9525 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07009526 vmx->idt_vectoring_info = 0;
9527
9528 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9529 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9530 return;
9531
9532 vmx->loaded_vmcs->launched = 1;
9533 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03009534
Avi Kivity51aa01d2010-07-20 14:31:20 +03009535 vmx_complete_atomic_exit(vmx);
9536 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009537 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009538}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009539STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009540
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009541static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009542{
9543 struct vcpu_vmx *vmx = to_vmx(vcpu);
9544 int cpu;
9545
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009546 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009547 return;
9548
9549 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009550 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009551 vmx_vcpu_put(vcpu);
9552 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009553 put_cpu();
9554}
9555
Jim Mattson2f1fe812016-07-08 15:36:06 -07009556/*
9557 * Ensure that the current vmcs of the logical processor is the
9558 * vmcs01 of the vcpu before calling free_nested().
9559 */
9560static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9561{
9562 struct vcpu_vmx *vmx = to_vmx(vcpu);
9563 int r;
9564
9565 r = vcpu_load(vcpu);
9566 BUG_ON(r);
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009567 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009568 free_nested(vmx);
9569 vcpu_put(vcpu);
9570}
9571
Avi Kivity6aa8b732006-12-10 02:21:36 -08009572static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9573{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009574 struct vcpu_vmx *vmx = to_vmx(vcpu);
9575
Kai Huang843e4332015-01-28 10:54:28 +08009576 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009577 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009578 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009579 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009580 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009581 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009582 kfree(vmx->guest_msrs);
9583 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009584 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009585}
9586
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009587static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009588{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009589 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009590 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03009591 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009592
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009593 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009594 return ERR_PTR(-ENOMEM);
9595
Wanpeng Li991e7a02015-09-16 17:30:05 +08009596 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009597
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009598 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9599 if (err)
9600 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009601
Peter Feiner4e595162016-07-07 14:49:58 -07009602 err = -ENOMEM;
9603
9604 /*
9605 * If PML is turned on, failure on enabling PML just results in failure
9606 * of creating the vcpu, therefore we can simplify PML logic (by
9607 * avoiding dealing with cases, such as enabling PML partially on vcpus
9608 * for the guest, etc.
9609 */
9610 if (enable_pml) {
9611 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9612 if (!vmx->pml_pg)
9613 goto uninit_vcpu;
9614 }
9615
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009616 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009617 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9618 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009619
Peter Feiner4e595162016-07-07 14:49:58 -07009620 if (!vmx->guest_msrs)
9621 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009622
Nadav Har'Eld462b812011-05-24 15:26:10 +03009623 vmx->loaded_vmcs = &vmx->vmcs01;
9624 vmx->loaded_vmcs->vmcs = alloc_vmcs();
Jim Mattson355f4fb2016-10-28 08:29:39 -07009625 vmx->loaded_vmcs->shadow_vmcs = NULL;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009626 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009627 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009628 loaded_vmcs_init(vmx->loaded_vmcs);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009629
Avi Kivity15ad7142007-07-11 18:17:21 +03009630 cpu = get_cpu();
9631 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009632 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +02009633 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009634 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009635 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +02009636 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009637 err = alloc_apic_access_page(kvm);
9638 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009639 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009640 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009641
Sheng Yangb927a3c2009-07-21 10:42:48 +08009642 if (enable_ept) {
Tang Chenf51770e2014-09-16 18:41:59 +08009643 err = init_rmode_identity_map(kvm);
9644 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009645 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009646 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009647
Wanpeng Li5c614b32015-10-13 09:18:36 -07009648 if (nested) {
Wincy Vanb9c237b2015-02-03 23:56:30 +08009649 nested_vmx_setup_ctls_msrs(vmx);
Wanpeng Li5c614b32015-10-13 09:18:36 -07009650 vmx->nested.vpid02 = allocate_vpid();
9651 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009652
Wincy Van705699a2015-02-03 23:58:17 +08009653 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009654 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009655
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009656 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9657
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02009658 /*
9659 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9660 * or POSTED_INTR_WAKEUP_VECTOR.
9661 */
9662 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9663 vmx->pi_desc.sn = 1;
9664
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009665 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009666
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009667free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009668 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009669 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009670free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009671 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009672free_pml:
9673 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009674uninit_vcpu:
9675 kvm_vcpu_uninit(&vmx->vcpu);
9676free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009677 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009678 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009679 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009680}
9681
Yang, Sheng002c7f72007-07-31 14:23:01 +03009682static void __init vmx_check_processor_compat(void *rtn)
9683{
9684 struct vmcs_config vmcs_conf;
9685
9686 *(int *)rtn = 0;
9687 if (setup_vmcs_config(&vmcs_conf) < 0)
9688 *(int *)rtn = -EIO;
9689 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9690 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9691 smp_processor_id());
9692 *(int *)rtn = -EIO;
9693 }
9694}
9695
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009696static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009697{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009698 u8 cache;
9699 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009700
Sheng Yang522c68c2009-04-27 20:35:43 +08009701 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009702 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009703 * 2. EPT with VT-d:
9704 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009705 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009706 * b. VT-d with snooping control feature: snooping control feature of
9707 * VT-d engine can guarantee the cache correctness. Just set it
9708 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009709 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009710 * consistent with host MTRR
9711 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009712 if (is_mmio) {
9713 cache = MTRR_TYPE_UNCACHABLE;
9714 goto exit;
9715 }
9716
9717 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009718 ipat = VMX_EPT_IPAT_BIT;
9719 cache = MTRR_TYPE_WRBACK;
9720 goto exit;
9721 }
9722
9723 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9724 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009725 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009726 cache = MTRR_TYPE_WRBACK;
9727 else
9728 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009729 goto exit;
9730 }
9731
Xiao Guangrongff536042015-06-15 16:55:22 +08009732 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009733
9734exit:
9735 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009736}
9737
Sheng Yang17cc3932010-01-05 19:02:27 +08009738static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009739{
Sheng Yang878403b2010-01-05 19:02:29 +08009740 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9741 return PT_DIRECTORY_LEVEL;
9742 else
9743 /* For shadow and EPT supported 1GB page */
9744 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009745}
9746
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009747static void vmcs_set_secondary_exec_control(u32 new_ctl)
9748{
9749 /*
9750 * These bits in the secondary execution controls field
9751 * are dynamic, the others are mostly based on the hypervisor
9752 * architecture and the guest's CPUID. Do not touch the
9753 * dynamic bits.
9754 */
9755 u32 mask =
9756 SECONDARY_EXEC_SHADOW_VMCS |
9757 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9758 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9759
9760 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9761
9762 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9763 (new_ctl & ~mask) | (cur_ctl & mask));
9764}
9765
David Matlack8322ebb2016-11-29 18:14:09 -08009766/*
9767 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
9768 * (indicating "allowed-1") if they are supported in the guest's CPUID.
9769 */
9770static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
9771{
9772 struct vcpu_vmx *vmx = to_vmx(vcpu);
9773 struct kvm_cpuid_entry2 *entry;
9774
9775 vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff;
9776 vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE;
9777
9778#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
9779 if (entry && (entry->_reg & (_cpuid_mask))) \
9780 vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \
9781} while (0)
9782
9783 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
9784 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
9785 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
9786 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
9787 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
9788 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
9789 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
9790 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
9791 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
9792 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
9793 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
9794 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
9795 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
9796 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
9797 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
9798
9799 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9800 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
9801 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
9802 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
9803 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
9804 /* TODO: Use X86_CR4_UMIP and X86_FEATURE_UMIP macros */
9805 cr4_fixed1_update(bit(11), ecx, bit(2));
9806
9807#undef cr4_fixed1_update
9808}
9809
Sheng Yang0e851882009-12-18 16:48:46 +08009810static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9811{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009812 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009813
Paolo Bonzini80154d72017-08-24 13:55:35 +02009814 if (cpu_has_secondary_exec_ctrls()) {
9815 vmx_compute_secondary_exec_control(vmx);
9816 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009817 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009818
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009819 if (nested_vmx_allowed(vcpu))
9820 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9821 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9822 else
9823 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9824 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -08009825
9826 if (nested_vmx_allowed(vcpu))
9827 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08009828}
9829
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009830static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9831{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009832 if (func == 1 && nested)
9833 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009834}
9835
Yang Zhang25d92082013-08-06 12:00:32 +03009836static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9837 struct x86_exception *fault)
9838{
Jan Kiszka533558b2014-01-04 18:47:20 +01009839 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -04009840 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +01009841 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -04009842 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +03009843
Bandan Dasc5f983f2017-05-05 15:25:14 -04009844 if (vmx->nested.pml_full) {
9845 exit_reason = EXIT_REASON_PML_FULL;
9846 vmx->nested.pml_full = false;
9847 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
9848 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009849 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009850 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009851 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -04009852
9853 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009854 vmcs12->guest_physical_address = fault->address;
9855}
9856
Peter Feiner995f00a2017-06-30 17:26:32 -07009857static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
9858{
David Hildenbrandbb97a012017-08-10 23:15:28 +02009859 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -07009860}
9861
Nadav Har'El155a97a2013-08-05 11:07:16 +03009862/* Callbacks for nested_ept_init_mmu_context: */
9863
9864static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9865{
9866 /* return the page table to be shadowed - in our case, EPT12 */
9867 return get_vmcs12(vcpu)->ept_pointer;
9868}
9869
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009870static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009871{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009872 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +02009873 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009874 return 1;
9875
9876 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +02009877 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009878 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009879 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +02009880 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +03009881 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9882 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9883 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9884
9885 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009886 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009887}
9888
9889static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9890{
9891 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9892}
9893
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009894static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9895 u16 error_code)
9896{
9897 bool inequality, bit;
9898
9899 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9900 inequality =
9901 (error_code & vmcs12->page_fault_error_code_mask) !=
9902 vmcs12->page_fault_error_code_match;
9903 return inequality ^ bit;
9904}
9905
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009906static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9907 struct x86_exception *fault)
9908{
9909 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9910
9911 WARN_ON(!is_guest_mode(vcpu));
9912
Wanpeng Li305d0ab2017-09-28 18:16:44 -07009913 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
9914 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02009915 vmcs12->vm_exit_intr_error_code = fault->error_code;
9916 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
9917 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
9918 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
9919 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +02009920 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009921 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +02009922 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009923}
9924
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009925static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9926 struct vmcs12 *vmcs12);
9927
9928static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009929 struct vmcs12 *vmcs12)
9930{
9931 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009932 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009933 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009934
9935 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009936 /*
9937 * Translate L1 physical address to host physical
9938 * address for vmcs02. Keep the page pinned, so this
9939 * physical address remains valid. We keep a reference
9940 * to it so we can release it later.
9941 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009942 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +02009943 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009944 vmx->nested.apic_access_page = NULL;
9945 }
9946 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009947 /*
9948 * If translation failed, no matter: This feature asks
9949 * to exit when accessing the given address, and if it
9950 * can never be accessed, this feature won't do
9951 * anything anyway.
9952 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009953 if (!is_error_page(page)) {
9954 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009955 hpa = page_to_phys(vmx->nested.apic_access_page);
9956 vmcs_write64(APIC_ACCESS_ADDR, hpa);
9957 } else {
9958 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
9959 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9960 }
9961 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
9962 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
9963 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
9964 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9965 kvm_vcpu_reload_apic_access_page(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009966 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009967
9968 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009969 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +02009970 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009971 vmx->nested.virtual_apic_page = NULL;
9972 }
9973 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009974
9975 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009976 * If translation failed, VM entry will fail because
9977 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
9978 * Failing the vm entry is _not_ what the processor
9979 * does but it's basically the only possibility we
9980 * have. We could still enter the guest if CR8 load
9981 * exits are enabled, CR8 store exits are enabled, and
9982 * virtualize APIC access is disabled; in this case
9983 * the processor would never use the TPR shadow and we
9984 * could simply clear the bit from the execution
9985 * control. But such a configuration is useless, so
9986 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009987 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009988 if (!is_error_page(page)) {
9989 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009990 hpa = page_to_phys(vmx->nested.virtual_apic_page);
9991 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
9992 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009993 }
9994
Wincy Van705699a2015-02-03 23:58:17 +08009995 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +08009996 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9997 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02009998 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009999 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010000 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010001 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10002 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010003 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010004 vmx->nested.pi_desc_page = page;
10005 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010006 vmx->nested.pi_desc =
10007 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10008 (unsigned long)(vmcs12->posted_intr_desc_addr &
10009 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010010 vmcs_write64(POSTED_INTR_DESC_ADDR,
10011 page_to_phys(vmx->nested.pi_desc_page) +
10012 (unsigned long)(vmcs12->posted_intr_desc_addr &
10013 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010014 }
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010015 if (cpu_has_vmx_msr_bitmap() &&
10016 nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS) &&
10017 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
10018 ;
10019 else
10020 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10021 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010022}
10023
Jan Kiszkaf41245002014-03-07 20:03:13 +010010024static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10025{
10026 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10027 struct vcpu_vmx *vmx = to_vmx(vcpu);
10028
10029 if (vcpu->arch.virtual_tsc_khz == 0)
10030 return;
10031
10032 /* Make sure short timeouts reliably trigger an immediate vmexit.
10033 * hrtimer_start does not guarantee this. */
10034 if (preemption_timeout <= 1) {
10035 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10036 return;
10037 }
10038
10039 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10040 preemption_timeout *= 1000000;
10041 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10042 hrtimer_start(&vmx->nested.preemption_timer,
10043 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10044}
10045
Jim Mattson56a20512017-07-06 16:33:06 -070010046static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10047 struct vmcs12 *vmcs12)
10048{
10049 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10050 return 0;
10051
10052 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10053 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10054 return -EINVAL;
10055
10056 return 0;
10057}
10058
Wincy Van3af18d92015-02-03 23:49:31 +080010059static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10060 struct vmcs12 *vmcs12)
10061{
Wincy Van3af18d92015-02-03 23:49:31 +080010062 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10063 return 0;
10064
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010065 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010066 return -EINVAL;
10067
10068 return 0;
10069}
10070
Jim Mattson712b12d2017-08-24 13:24:47 -070010071static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10072 struct vmcs12 *vmcs12)
10073{
10074 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10075 return 0;
10076
10077 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10078 return -EINVAL;
10079
10080 return 0;
10081}
10082
Wincy Van3af18d92015-02-03 23:49:31 +080010083/*
10084 * Merge L0's and L1's MSR bitmap, return false to indicate that
10085 * we do not use the hardware.
10086 */
10087static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
10088 struct vmcs12 *vmcs12)
10089{
Wincy Van82f0dd42015-02-03 23:57:18 +080010090 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010091 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010092 unsigned long *msr_bitmap_l1;
10093 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap;
Wincy Vanf2b93282015-02-03 23:56:03 +080010094
Radim Krčmářd048c092016-08-08 20:16:22 +020010095 /* This shortcut is ok because we support only x2APIC MSRs so far. */
Wincy Vanf2b93282015-02-03 23:56:03 +080010096 if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
10097 return false;
10098
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010099 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10100 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010101 return false;
Radim Krčmářd048c092016-08-08 20:16:22 +020010102 msr_bitmap_l1 = (unsigned long *)kmap(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010103
Radim Krčmářd048c092016-08-08 20:16:22 +020010104 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
10105
Wincy Vanf2b93282015-02-03 23:56:03 +080010106 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +080010107 if (nested_cpu_has_apic_reg_virt(vmcs12))
10108 for (msr = 0x800; msr <= 0x8ff; msr++)
10109 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +020010110 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +080010111 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +020010112
10113 nested_vmx_disable_intercept_for_msr(
10114 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +080010115 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
10116 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +020010117
Wincy Van608406e2015-02-03 23:57:51 +080010118 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +080010119 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +020010120 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +080010121 APIC_BASE_MSR + (APIC_EOI >> 4),
10122 MSR_TYPE_W);
10123 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +020010124 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +080010125 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
10126 MSR_TYPE_W);
10127 }
Wincy Van82f0dd42015-02-03 23:57:18 +080010128 }
Wincy Vanf2b93282015-02-03 23:56:03 +080010129 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010130 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010131
10132 return true;
10133}
10134
10135static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10136 struct vmcs12 *vmcs12)
10137{
Wincy Van82f0dd42015-02-03 23:57:18 +080010138 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010139 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010140 !nested_cpu_has_vid(vmcs12) &&
10141 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010142 return 0;
10143
10144 /*
10145 * If virtualize x2apic mode is enabled,
10146 * virtualize apic access must be disabled.
10147 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010148 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10149 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010150 return -EINVAL;
10151
Wincy Van608406e2015-02-03 23:57:51 +080010152 /*
10153 * If virtual interrupt delivery is enabled,
10154 * we must exit on external interrupts.
10155 */
10156 if (nested_cpu_has_vid(vmcs12) &&
10157 !nested_exit_on_intr(vcpu))
10158 return -EINVAL;
10159
Wincy Van705699a2015-02-03 23:58:17 +080010160 /*
10161 * bits 15:8 should be zero in posted_intr_nv,
10162 * the descriptor address has been already checked
10163 * in nested_get_vmcs12_pages.
10164 */
10165 if (nested_cpu_has_posted_intr(vmcs12) &&
10166 (!nested_cpu_has_vid(vmcs12) ||
10167 !nested_exit_intr_ack_set(vcpu) ||
10168 vmcs12->posted_intr_nv & 0xff00))
10169 return -EINVAL;
10170
Wincy Vanf2b93282015-02-03 23:56:03 +080010171 /* tpr shadow is needed by all apicv features. */
10172 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10173 return -EINVAL;
10174
10175 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010176}
10177
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010178static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10179 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010180 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010181{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010182 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010183 u64 count, addr;
10184
10185 if (vmcs12_read_any(vcpu, count_field, &count) ||
10186 vmcs12_read_any(vcpu, addr_field, &addr)) {
10187 WARN_ON(1);
10188 return -EINVAL;
10189 }
10190 if (count == 0)
10191 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010192 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010193 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10194 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010195 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010196 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10197 addr_field, maxphyaddr, count, addr);
10198 return -EINVAL;
10199 }
10200 return 0;
10201}
10202
10203static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10204 struct vmcs12 *vmcs12)
10205{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010206 if (vmcs12->vm_exit_msr_load_count == 0 &&
10207 vmcs12->vm_exit_msr_store_count == 0 &&
10208 vmcs12->vm_entry_msr_load_count == 0)
10209 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010210 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010211 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010212 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010213 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010214 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010215 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010216 return -EINVAL;
10217 return 0;
10218}
10219
Bandan Dasc5f983f2017-05-05 15:25:14 -040010220static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10221 struct vmcs12 *vmcs12)
10222{
10223 u64 address = vmcs12->pml_address;
10224 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10225
10226 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10227 if (!nested_cpu_has_ept(vmcs12) ||
10228 !IS_ALIGNED(address, 4096) ||
10229 address >> maxphyaddr)
10230 return -EINVAL;
10231 }
10232
10233 return 0;
10234}
10235
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010236static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10237 struct vmx_msr_entry *e)
10238{
10239 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010240 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010241 return -EINVAL;
10242 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10243 e->index == MSR_IA32_UCODE_REV)
10244 return -EINVAL;
10245 if (e->reserved != 0)
10246 return -EINVAL;
10247 return 0;
10248}
10249
10250static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10251 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010252{
10253 if (e->index == MSR_FS_BASE ||
10254 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010255 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10256 nested_vmx_msr_check_common(vcpu, e))
10257 return -EINVAL;
10258 return 0;
10259}
10260
10261static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10262 struct vmx_msr_entry *e)
10263{
10264 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10265 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010266 return -EINVAL;
10267 return 0;
10268}
10269
10270/*
10271 * Load guest's/host's msr at nested entry/exit.
10272 * return 0 for success, entry index for failure.
10273 */
10274static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10275{
10276 u32 i;
10277 struct vmx_msr_entry e;
10278 struct msr_data msr;
10279
10280 msr.host_initiated = false;
10281 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010282 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10283 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010284 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010285 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10286 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010287 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010288 }
10289 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010290 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010291 "%s check failed (%u, 0x%x, 0x%x)\n",
10292 __func__, i, e.index, e.reserved);
10293 goto fail;
10294 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010295 msr.index = e.index;
10296 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010297 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010298 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010299 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10300 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010301 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010302 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010303 }
10304 return 0;
10305fail:
10306 return i + 1;
10307}
10308
10309static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10310{
10311 u32 i;
10312 struct vmx_msr_entry e;
10313
10314 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010315 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010316 if (kvm_vcpu_read_guest(vcpu,
10317 gpa + i * sizeof(e),
10318 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010319 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010320 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10321 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010322 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010323 }
10324 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010325 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010326 "%s check failed (%u, 0x%x, 0x%x)\n",
10327 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010328 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010329 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010330 msr_info.host_initiated = false;
10331 msr_info.index = e.index;
10332 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010333 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010334 "%s cannot read MSR (%u, 0x%x)\n",
10335 __func__, i, e.index);
10336 return -EINVAL;
10337 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010338 if (kvm_vcpu_write_guest(vcpu,
10339 gpa + i * sizeof(e) +
10340 offsetof(struct vmx_msr_entry, value),
10341 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010342 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010343 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010344 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010345 return -EINVAL;
10346 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010347 }
10348 return 0;
10349}
10350
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010351static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10352{
10353 unsigned long invalid_mask;
10354
10355 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10356 return (val & invalid_mask) == 0;
10357}
10358
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010359/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010360 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10361 * emulating VM entry into a guest with EPT enabled.
10362 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10363 * is assigned to entry_failure_code on failure.
10364 */
10365static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010366 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010367{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010368 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010369 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010370 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10371 return 1;
10372 }
10373
10374 /*
10375 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10376 * must not be dereferenced.
10377 */
10378 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10379 !nested_ept) {
10380 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10381 *entry_failure_code = ENTRY_FAIL_PDPTE;
10382 return 1;
10383 }
10384 }
10385
10386 vcpu->arch.cr3 = cr3;
10387 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10388 }
10389
10390 kvm_mmu_reset_context(vcpu);
10391 return 0;
10392}
10393
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010394/*
10395 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10396 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010397 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010398 * guest in a way that will both be appropriate to L1's requests, and our
10399 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10400 * function also has additional necessary side-effects, like setting various
10401 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010010402 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10403 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010404 */
Ladi Prosekee146c12016-11-30 16:03:09 +010010405static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010406 bool from_vmentry, u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010407{
10408 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040010409 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010410
10411 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10412 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
10413 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10414 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10415 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10416 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10417 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10418 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10419 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10420 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
10421 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10422 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10423 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10424 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10425 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10426 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10427 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10428 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10429 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10430 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
10431 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10432 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10433 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10434 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10435 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10436 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10437 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10438 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
10439 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10440 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10441 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10442 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10443 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10444 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10445 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10446 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10447
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010448 if (from_vmentry &&
10449 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020010450 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10451 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10452 } else {
10453 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10454 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10455 }
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010456 if (from_vmentry) {
10457 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10458 vmcs12->vm_entry_intr_info_field);
10459 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10460 vmcs12->vm_entry_exception_error_code);
10461 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10462 vmcs12->vm_entry_instruction_len);
10463 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10464 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070010465 vmx->loaded_vmcs->nmi_known_unmasked =
10466 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010467 } else {
10468 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10469 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010470 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +030010471 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010472 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10473 vmcs12->guest_pending_dbg_exceptions);
10474 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10475 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10476
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010477 if (nested_cpu_has_xsaves(vmcs12))
10478 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010479 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10480
Jan Kiszkaf41245002014-03-07 20:03:13 +010010481 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010482
Paolo Bonzini9314006db2016-07-06 13:23:51 +020010483 /* Preemption timer setting is only taken from vmcs01. */
10484 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10485 exec_control |= vmcs_config.pin_based_exec_ctrl;
10486 if (vmx->hv_deadline_tsc == -1)
10487 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10488
10489 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010490 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010491 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10492 vmx->nested.pi_pending = false;
Wincy Van06a55242017-04-28 13:13:59 +080010493 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010494 } else {
Wincy Van705699a2015-02-03 23:58:17 +080010495 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010496 }
Wincy Van705699a2015-02-03 23:58:17 +080010497
Jan Kiszkaf41245002014-03-07 20:03:13 +010010498 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010499
Jan Kiszkaf41245002014-03-07 20:03:13 +010010500 vmx->nested.preemption_timer_expired = false;
10501 if (nested_cpu_has_preemption_timer(vmcs12))
10502 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010503
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010504 /*
10505 * Whether page-faults are trapped is determined by a combination of
10506 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10507 * If enable_ept, L0 doesn't care about page faults and we should
10508 * set all of these to L1's desires. However, if !enable_ept, L0 does
10509 * care about (at least some) page faults, and because it is not easy
10510 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10511 * to exit on each and every L2 page fault. This is done by setting
10512 * MASK=MATCH=0 and (see below) EB.PF=1.
10513 * Note that below we don't need special code to set EB.PF beyond the
10514 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10515 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10516 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010517 */
10518 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10519 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10520 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10521 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10522
10523 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020010524 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080010525
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010526 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010527 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020010528 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010529 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020010530 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010531 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040010532 SECONDARY_EXEC_APIC_REGISTER_VIRT |
10533 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010534 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040010535 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
10536 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
10537 ~SECONDARY_EXEC_ENABLE_PML;
10538 exec_control |= vmcs12_exec_ctrl;
10539 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010540
Bandan Das27c42a12017-08-03 15:54:42 -040010541 /* All VMFUNCs are currently emulated through L0 vmexits. */
10542 if (exec_control & SECONDARY_EXEC_ENABLE_VMFUNC)
10543 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10544
Wincy Van608406e2015-02-03 23:57:51 +080010545 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
10546 vmcs_write64(EOI_EXIT_BITMAP0,
10547 vmcs12->eoi_exit_bitmap0);
10548 vmcs_write64(EOI_EXIT_BITMAP1,
10549 vmcs12->eoi_exit_bitmap1);
10550 vmcs_write64(EOI_EXIT_BITMAP2,
10551 vmcs12->eoi_exit_bitmap2);
10552 vmcs_write64(EOI_EXIT_BITMAP3,
10553 vmcs12->eoi_exit_bitmap3);
10554 vmcs_write16(GUEST_INTR_STATUS,
10555 vmcs12->guest_intr_status);
10556 }
10557
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010558 /*
10559 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10560 * nested_get_vmcs12_pages will either fix it up or
10561 * remove the VM execution control.
10562 */
10563 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
10564 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
10565
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010566 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10567 }
10568
10569
10570 /*
10571 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10572 * Some constant fields are set here by vmx_set_constant_host_state().
10573 * Other fields are different per CPU, and will be set later when
10574 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10575 */
Yang Zhanga547c6d2013-04-11 19:25:10 +080010576 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010577
10578 /*
Jim Mattson83bafef2016-10-04 10:48:38 -070010579 * Set the MSR load/store lists to match L0's settings.
10580 */
10581 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10582 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10583 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10584 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10585 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10586
10587 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010588 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10589 * entry, but only if the current (host) sp changed from the value
10590 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10591 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10592 * here we just force the write to happen on entry.
10593 */
10594 vmx->host_rsp = 0;
10595
10596 exec_control = vmx_exec_control(vmx); /* L0's desires */
10597 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10598 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10599 exec_control &= ~CPU_BASED_TPR_SHADOW;
10600 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010601
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010602 /*
10603 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
10604 * nested_get_vmcs12_pages can't fix it up, the illegal value
10605 * will result in a VM entry failure.
10606 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010607 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010608 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010609 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070010610 } else {
10611#ifdef CONFIG_X86_64
10612 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10613 CPU_BASED_CR8_STORE_EXITING;
10614#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010615 }
10616
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010617 /*
Wincy Van3af18d92015-02-03 23:49:31 +080010618 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010619 * Rather, exit every time.
10620 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010621 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10622 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10623
10624 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10625
10626 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10627 * bitwise-or of what L1 wants to trap for L2, and what we want to
10628 * trap. Note that CR0.TS also needs updating - we do this later.
10629 */
10630 update_exception_bitmap(vcpu);
10631 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10632 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10633
Nadav Har'El8049d652013-08-05 11:07:06 +030010634 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10635 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10636 * bits are further modified by vmx_set_efer() below.
10637 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010010638 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010639
10640 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10641 * emulated by vmx_set_efer(), below.
10642 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010643 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010644 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10645 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010646 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10647
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010648 if (from_vmentry &&
10649 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010650 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010651 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010652 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010653 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010654 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010655
10656 set_cr4_guest_host_mask(vmx);
10657
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010658 if (from_vmentry &&
10659 vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010660 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10661
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010662 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10663 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010664 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010665 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010666 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010667 if (kvm_has_tsc_control)
10668 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010669
10670 if (enable_vpid) {
10671 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010672 * There is no direct mapping between vpid02 and vpid12, the
10673 * vpid02 is per-vCPU for L0 and reused while the value of
10674 * vpid12 is changed w/ one invvpid during nested vmentry.
10675 * The vpid12 is allocated by L1 for L2, so it will not
10676 * influence global bitmap(for vpid01 and vpid02 allocation)
10677 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010678 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010679 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10680 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10681 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10682 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10683 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10684 }
10685 } else {
10686 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10687 vmx_flush_tlb(vcpu);
10688 }
10689
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010690 }
10691
Ladi Prosek1fb883b2017-04-04 14:18:53 +020010692 if (enable_pml) {
10693 /*
10694 * Conceptually we want to copy the PML address and index from
10695 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10696 * since we always flush the log on each vmexit, this happens
10697 * to be equivalent to simply resetting the fields in vmcs02.
10698 */
10699 ASSERT(vmx->pml_pg);
10700 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10701 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10702 }
10703
Nadav Har'El155a97a2013-08-05 11:07:16 +030010704 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010705 if (nested_ept_init_mmu_context(vcpu)) {
10706 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10707 return 1;
10708 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010709 } else if (nested_cpu_has2(vmcs12,
10710 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10711 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010712 }
10713
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010714 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080010715 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
10716 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010717 * The CR0_READ_SHADOW is what L2 should have expected to read given
10718 * the specifications by L1; It's not enough to take
10719 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10720 * have more bits than L1 expected.
10721 */
10722 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10723 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10724
10725 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10726 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10727
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010728 if (from_vmentry &&
10729 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080010730 vcpu->arch.efer = vmcs12->guest_ia32_efer;
10731 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
10732 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10733 else
10734 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10735 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10736 vmx_set_efer(vcpu, vcpu->arch.efer);
10737
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010738 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010010739 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010740 entry_failure_code))
10741 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010010742
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010743 if (!enable_ept)
10744 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10745
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010746 /*
10747 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10748 */
10749 if (enable_ept) {
10750 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10751 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10752 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10753 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10754 }
10755
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010756 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10757 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010010758 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010759}
10760
Jim Mattsonca0bde22016-11-30 12:03:46 -080010761static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10762{
10763 struct vcpu_vmx *vmx = to_vmx(vcpu);
10764
10765 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10766 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
10767 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10768
Jim Mattson56a20512017-07-06 16:33:06 -070010769 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
10770 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10771
Jim Mattsonca0bde22016-11-30 12:03:46 -080010772 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
10773 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10774
Jim Mattson712b12d2017-08-24 13:24:47 -070010775 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
10776 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10777
Jim Mattsonca0bde22016-11-30 12:03:46 -080010778 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
10779 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10780
10781 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
10782 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10783
Bandan Dasc5f983f2017-05-05 15:25:14 -040010784 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
10785 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10786
Jim Mattsonca0bde22016-11-30 12:03:46 -080010787 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
10788 vmx->nested.nested_vmx_procbased_ctls_low,
10789 vmx->nested.nested_vmx_procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070010790 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
10791 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
10792 vmx->nested.nested_vmx_secondary_ctls_low,
10793 vmx->nested.nested_vmx_secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080010794 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
10795 vmx->nested.nested_vmx_pinbased_ctls_low,
10796 vmx->nested.nested_vmx_pinbased_ctls_high) ||
10797 !vmx_control_verify(vmcs12->vm_exit_controls,
10798 vmx->nested.nested_vmx_exit_ctls_low,
10799 vmx->nested.nested_vmx_exit_ctls_high) ||
10800 !vmx_control_verify(vmcs12->vm_entry_controls,
10801 vmx->nested.nested_vmx_entry_ctls_low,
10802 vmx->nested.nested_vmx_entry_ctls_high))
10803 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10804
Bandan Das41ab9372017-08-03 15:54:43 -040010805 if (nested_cpu_has_vmfunc(vmcs12)) {
10806 if (vmcs12->vm_function_control &
10807 ~vmx->nested.nested_vmx_vmfunc_controls)
10808 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10809
10810 if (nested_cpu_has_eptp_switching(vmcs12)) {
10811 if (!nested_cpu_has_ept(vmcs12) ||
10812 !page_address_valid(vcpu, vmcs12->eptp_list_address))
10813 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10814 }
10815 }
Bandan Das27c42a12017-08-03 15:54:42 -040010816
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070010817 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
10818 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10819
Jim Mattsonca0bde22016-11-30 12:03:46 -080010820 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
10821 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
10822 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
10823 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
10824
10825 return 0;
10826}
10827
10828static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10829 u32 *exit_qual)
10830{
10831 bool ia32e;
10832
10833 *exit_qual = ENTRY_FAIL_DEFAULT;
10834
10835 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
10836 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
10837 return 1;
10838
10839 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
10840 vmcs12->vmcs_link_pointer != -1ull) {
10841 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
10842 return 1;
10843 }
10844
10845 /*
10846 * If the load IA32_EFER VM-entry control is 1, the following checks
10847 * are performed on the field for the IA32_EFER MSR:
10848 * - Bits reserved in the IA32_EFER MSR must be 0.
10849 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10850 * the IA-32e mode guest VM-exit control. It must also be identical
10851 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10852 * CR0.PG) is 1.
10853 */
10854 if (to_vmx(vcpu)->nested.nested_run_pending &&
10855 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
10856 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10857 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10858 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10859 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10860 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
10861 return 1;
10862 }
10863
10864 /*
10865 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10866 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10867 * the values of the LMA and LME bits in the field must each be that of
10868 * the host address-space size VM-exit control.
10869 */
10870 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10871 ia32e = (vmcs12->vm_exit_controls &
10872 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10873 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10874 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10875 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
10876 return 1;
10877 }
10878
Wanpeng Lif1b026a2017-11-05 16:54:48 -080010879 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
10880 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
10881 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
10882 return 1;
10883
Jim Mattsonca0bde22016-11-30 12:03:46 -080010884 return 0;
10885}
10886
Jim Mattson858e25c2016-11-30 12:03:47 -080010887static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
10888{
10889 struct vcpu_vmx *vmx = to_vmx(vcpu);
10890 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10891 struct loaded_vmcs *vmcs02;
Jim Mattson858e25c2016-11-30 12:03:47 -080010892 u32 msr_entry_idx;
10893 u32 exit_qual;
10894
10895 vmcs02 = nested_get_current_vmcs02(vmx);
10896 if (!vmcs02)
10897 return -ENOMEM;
10898
10899 enter_guest_mode(vcpu);
10900
10901 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10902 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10903
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010904 vmx_switch_vmcs(vcpu, vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080010905 vmx_segment_cache_clear(vmx);
10906
10907 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
10908 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010909 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080010910 nested_vmx_entry_failure(vcpu, vmcs12,
10911 EXIT_REASON_INVALID_STATE, exit_qual);
10912 return 1;
10913 }
10914
10915 nested_get_vmcs12_pages(vcpu, vmcs12);
10916
10917 msr_entry_idx = nested_vmx_load_msr(vcpu,
10918 vmcs12->vm_entry_msr_load_addr,
10919 vmcs12->vm_entry_msr_load_count);
10920 if (msr_entry_idx) {
10921 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010922 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080010923 nested_vmx_entry_failure(vcpu, vmcs12,
10924 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10925 return 1;
10926 }
10927
Jim Mattson858e25c2016-11-30 12:03:47 -080010928 /*
10929 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10930 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10931 * returned as far as L1 is concerned. It will only return (and set
10932 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10933 */
10934 return 0;
10935}
10936
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010937/*
10938 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10939 * for running an L2 nested guest.
10940 */
10941static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10942{
10943 struct vmcs12 *vmcs12;
10944 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070010945 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080010946 u32 exit_qual;
10947 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010948
Kyle Hueyeb277562016-11-29 12:40:39 -080010949 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010950 return 1;
10951
Kyle Hueyeb277562016-11-29 12:40:39 -080010952 if (!nested_vmx_check_vmcs12(vcpu))
10953 goto out;
10954
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010955 vmcs12 = get_vmcs12(vcpu);
10956
Abel Gordon012f83c2013-04-18 14:39:25 +030010957 if (enable_shadow_vmcs)
10958 copy_shadow_to_vmcs12(vmx);
10959
Nadav Har'El7c177932011-05-25 23:12:04 +030010960 /*
10961 * The nested entry process starts with enforcing various prerequisites
10962 * on vmcs12 as required by the Intel SDM, and act appropriately when
10963 * they fail: As the SDM explains, some conditions should cause the
10964 * instruction to fail, while others will cause the instruction to seem
10965 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10966 * To speed up the normal (success) code path, we should avoid checking
10967 * for misconfigurations which will anyway be caught by the processor
10968 * when using the merged vmcs02.
10969 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070010970 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
10971 nested_vmx_failValid(vcpu,
10972 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
10973 goto out;
10974 }
10975
Nadav Har'El7c177932011-05-25 23:12:04 +030010976 if (vmcs12->launch_state == launch) {
10977 nested_vmx_failValid(vcpu,
10978 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10979 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080010980 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030010981 }
10982
Jim Mattsonca0bde22016-11-30 12:03:46 -080010983 ret = check_vmentry_prereqs(vcpu, vmcs12);
10984 if (ret) {
10985 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080010986 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010987 }
10988
Nadav Har'El7c177932011-05-25 23:12:04 +030010989 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080010990 * After this point, the trap flag no longer triggers a singlestep trap
10991 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
10992 * This is not 100% correct; for performance reasons, we delegate most
10993 * of the checks on host state to the processor. If those fail,
10994 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020010995 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080010996 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020010997
Jim Mattsonca0bde22016-11-30 12:03:46 -080010998 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
10999 if (ret) {
11000 nested_vmx_entry_failure(vcpu, vmcs12,
11001 EXIT_REASON_INVALID_STATE, exit_qual);
11002 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011003 }
11004
11005 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011006 * We're finally done with prerequisite checking, and can start with
11007 * the nested entry.
11008 */
11009
Jim Mattson858e25c2016-11-30 12:03:47 -080011010 ret = enter_vmx_non_root_mode(vcpu, true);
11011 if (ret)
11012 return ret;
Wincy Vanff651cb2014-12-11 08:52:58 +030011013
Jan Kiszka6dfacad2013-12-04 08:58:54 +010011014 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060011015 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010011016
Jan Kiszka7af40ad32014-01-04 18:47:23 +010011017 vmx->nested.nested_run_pending = 1;
11018
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011019 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011020
11021out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011022 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011023}
11024
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011025/*
11026 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11027 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11028 * This function returns the new value we should put in vmcs12.guest_cr0.
11029 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11030 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11031 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11032 * didn't trap the bit, because if L1 did, so would L0).
11033 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11034 * been modified by L2, and L1 knows it. So just leave the old value of
11035 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11036 * isn't relevant, because if L0 traps this bit it can set it to anything.
11037 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11038 * changed these bits, and therefore they need to be updated, but L0
11039 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11040 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11041 */
11042static inline unsigned long
11043vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11044{
11045 return
11046 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11047 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11048 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11049 vcpu->arch.cr0_guest_owned_bits));
11050}
11051
11052static inline unsigned long
11053vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11054{
11055 return
11056 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11057 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11058 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11059 vcpu->arch.cr4_guest_owned_bits));
11060}
11061
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011062static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11063 struct vmcs12 *vmcs12)
11064{
11065 u32 idt_vectoring;
11066 unsigned int nr;
11067
Wanpeng Li664f8e22017-08-24 03:35:09 -070011068 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011069 nr = vcpu->arch.exception.nr;
11070 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11071
11072 if (kvm_exception_is_soft(nr)) {
11073 vmcs12->vm_exit_instruction_len =
11074 vcpu->arch.event_exit_inst_len;
11075 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11076 } else
11077 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11078
11079 if (vcpu->arch.exception.has_error_code) {
11080 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11081 vmcs12->idt_vectoring_error_code =
11082 vcpu->arch.exception.error_code;
11083 }
11084
11085 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011086 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011087 vmcs12->idt_vectoring_info_field =
11088 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
11089 } else if (vcpu->arch.interrupt.pending) {
11090 nr = vcpu->arch.interrupt.nr;
11091 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11092
11093 if (vcpu->arch.interrupt.soft) {
11094 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11095 vmcs12->vm_entry_instruction_len =
11096 vcpu->arch.event_exit_inst_len;
11097 } else
11098 idt_vectoring |= INTR_TYPE_EXT_INTR;
11099
11100 vmcs12->idt_vectoring_info_field = idt_vectoring;
11101 }
11102}
11103
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011104static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11105{
11106 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011107 unsigned long exit_qual;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011108
Wanpeng Li274bba52017-08-24 03:35:08 -070011109 if (kvm_event_needs_reinjection(vcpu))
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011110 return -EBUSY;
11111
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011112 if (vcpu->arch.exception.pending &&
11113 nested_vmx_check_exception(vcpu, &exit_qual)) {
11114 if (vmx->nested.nested_run_pending)
11115 return -EBUSY;
11116 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
11117 vcpu->arch.exception.pending = false;
11118 return 0;
11119 }
11120
Jan Kiszkaf41245002014-03-07 20:03:13 +010011121 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11122 vmx->nested.preemption_timer_expired) {
11123 if (vmx->nested.nested_run_pending)
11124 return -EBUSY;
11125 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11126 return 0;
11127 }
11128
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011129 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011130 if (vmx->nested.nested_run_pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011131 return -EBUSY;
11132 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11133 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11134 INTR_INFO_VALID_MASK, 0);
11135 /*
11136 * The NMI-triggered VM exit counts as injection:
11137 * clear this one and block further NMIs.
11138 */
11139 vcpu->arch.nmi_pending = 0;
11140 vmx_set_nmi_mask(vcpu, true);
11141 return 0;
11142 }
11143
11144 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11145 nested_exit_on_intr(vcpu)) {
11146 if (vmx->nested.nested_run_pending)
11147 return -EBUSY;
11148 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080011149 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011150 }
11151
David Hildenbrand6342c502017-01-25 11:58:58 +010011152 vmx_complete_nested_posted_interrupt(vcpu);
11153 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011154}
11155
Jan Kiszkaf41245002014-03-07 20:03:13 +010011156static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11157{
11158 ktime_t remaining =
11159 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11160 u64 value;
11161
11162 if (ktime_to_ns(remaining) <= 0)
11163 return 0;
11164
11165 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11166 do_div(value, 1000000);
11167 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11168}
11169
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011170/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011171 * Update the guest state fields of vmcs12 to reflect changes that
11172 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11173 * VM-entry controls is also updated, since this is really a guest
11174 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011175 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011176static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011177{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011178 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11179 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11180
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011181 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11182 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11183 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11184
11185 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11186 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11187 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11188 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11189 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11190 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11191 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11192 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11193 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11194 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11195 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11196 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11197 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11198 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11199 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11200 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11201 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11202 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11203 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11204 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11205 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11206 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11207 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11208 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11209 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11210 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11211 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11212 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11213 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11214 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11215 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11216 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11217 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11218 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11219 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11220 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11221
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011222 vmcs12->guest_interruptibility_info =
11223 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11224 vmcs12->guest_pending_dbg_exceptions =
11225 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010011226 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11227 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11228 else
11229 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011230
Jan Kiszkaf41245002014-03-07 20:03:13 +010011231 if (nested_cpu_has_preemption_timer(vmcs12)) {
11232 if (vmcs12->vm_exit_controls &
11233 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11234 vmcs12->vmx_preemption_timer_value =
11235 vmx_get_preemption_timer_value(vcpu);
11236 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11237 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080011238
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011239 /*
11240 * In some cases (usually, nested EPT), L2 is allowed to change its
11241 * own CR3 without exiting. If it has changed it, we must keep it.
11242 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11243 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11244 *
11245 * Additionally, restore L2's PDPTR to vmcs12.
11246 */
11247 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010011248 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011249 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11250 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11251 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11252 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11253 }
11254
Jim Mattsond281e132017-06-01 12:44:46 -070011255 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011256
Wincy Van608406e2015-02-03 23:57:51 +080011257 if (nested_cpu_has_vid(vmcs12))
11258 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11259
Jan Kiszkac18911a2013-03-13 16:06:41 +010011260 vmcs12->vm_entry_controls =
11261 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011262 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011263
Jan Kiszka2996fca2014-06-16 13:59:43 +020011264 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11265 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11266 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11267 }
11268
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011269 /* TODO: These cannot have changed unless we have MSR bitmaps and
11270 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011271 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011272 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011273 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11274 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011275 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11276 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11277 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011278 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011279 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011280}
11281
11282/*
11283 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11284 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11285 * and this function updates it to reflect the changes to the guest state while
11286 * L2 was running (and perhaps made some exits which were handled directly by L0
11287 * without going back to L1), and to reflect the exit reason.
11288 * Note that we do not have to copy here all VMCS fields, just those that
11289 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11290 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11291 * which already writes to vmcs12 directly.
11292 */
11293static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11294 u32 exit_reason, u32 exit_intr_info,
11295 unsigned long exit_qualification)
11296{
11297 /* update guest state fields: */
11298 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011299
11300 /* update exit information fields: */
11301
Jan Kiszka533558b2014-01-04 18:47:20 +010011302 vmcs12->vm_exit_reason = exit_reason;
11303 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010011304 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020011305
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011306 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011307 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11308 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11309
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011310 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070011311 vmcs12->launch_state = 1;
11312
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011313 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11314 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011315 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011316
11317 /*
11318 * Transfer the event that L0 or L1 may wanted to inject into
11319 * L2 to IDT_VECTORING_INFO_FIELD.
11320 */
11321 vmcs12_save_pending_event(vcpu, vmcs12);
11322 }
11323
11324 /*
11325 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11326 * preserved above and would only end up incorrectly in L1.
11327 */
11328 vcpu->arch.nmi_injected = false;
11329 kvm_clear_exception_queue(vcpu);
11330 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011331}
11332
Wanpeng Li5af41572017-11-05 16:54:49 -080011333static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11334 struct vmcs12 *vmcs12)
11335{
11336 u32 entry_failure_code;
11337
11338 nested_ept_uninit_mmu_context(vcpu);
11339
11340 /*
11341 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11342 * couldn't have changed.
11343 */
11344 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11345 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11346
11347 if (!enable_ept)
11348 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11349}
11350
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011351/*
11352 * A part of what we need to when the nested L2 guest exits and we want to
11353 * run its L1 parent, is to reset L1's guest state to the host state specified
11354 * in vmcs12.
11355 * This function is to be called not only on normal nested exit, but also on
11356 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11357 * Failures During or After Loading Guest State").
11358 * This function should be called when the active VMCS is L1's (vmcs01).
11359 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011360static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11361 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011362{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011363 struct kvm_segment seg;
11364
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011365 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11366 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011367 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011368 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11369 else
11370 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11371 vmx_set_efer(vcpu, vcpu->arch.efer);
11372
11373 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11374 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011375 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011376 /*
11377 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011378 * actually changed, because vmx_set_cr0 refers to efer set above.
11379 *
11380 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11381 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011382 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011383 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020011384 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011385
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011386 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011387 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080011388 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011389
Wanpeng Li5af41572017-11-05 16:54:49 -080011390 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011391
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011392 if (enable_vpid) {
11393 /*
11394 * Trivially support vpid by letting L2s share their parent
11395 * L1's vpid. TODO: move to a more elaborate solution, giving
11396 * each L2 its own vpid and exposing the vpid feature to L1.
11397 */
11398 vmx_flush_tlb(vcpu);
11399 }
Wincy Van06a55242017-04-28 13:13:59 +080011400 /* Restore posted intr vector. */
11401 if (nested_cpu_has_posted_intr(vmcs12))
11402 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011403
11404 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11405 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11406 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11407 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11408 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d5512017-10-11 16:54:42 +020011409 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11410 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011411
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011412 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11413 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11414 vmcs_write64(GUEST_BNDCFGS, 0);
11415
Jan Kiszka44811c02013-08-04 17:17:27 +020011416 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011417 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011418 vcpu->arch.pat = vmcs12->host_ia32_pat;
11419 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011420 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11421 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11422 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011423
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011424 /* Set L1 segment info according to Intel SDM
11425 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11426 seg = (struct kvm_segment) {
11427 .base = 0,
11428 .limit = 0xFFFFFFFF,
11429 .selector = vmcs12->host_cs_selector,
11430 .type = 11,
11431 .present = 1,
11432 .s = 1,
11433 .g = 1
11434 };
11435 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11436 seg.l = 1;
11437 else
11438 seg.db = 1;
11439 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11440 seg = (struct kvm_segment) {
11441 .base = 0,
11442 .limit = 0xFFFFFFFF,
11443 .type = 3,
11444 .present = 1,
11445 .s = 1,
11446 .db = 1,
11447 .g = 1
11448 };
11449 seg.selector = vmcs12->host_ds_selector;
11450 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11451 seg.selector = vmcs12->host_es_selector;
11452 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11453 seg.selector = vmcs12->host_ss_selector;
11454 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11455 seg.selector = vmcs12->host_fs_selector;
11456 seg.base = vmcs12->host_fs_base;
11457 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11458 seg.selector = vmcs12->host_gs_selector;
11459 seg.base = vmcs12->host_gs_base;
11460 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11461 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011462 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011463 .limit = 0x67,
11464 .selector = vmcs12->host_tr_selector,
11465 .type = 11,
11466 .present = 1
11467 };
11468 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11469
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011470 kvm_set_dr(vcpu, 7, 0x400);
11471 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011472
Wincy Van3af18d92015-02-03 23:49:31 +080011473 if (cpu_has_vmx_msr_bitmap())
11474 vmx_set_msr_bitmap(vcpu);
11475
Wincy Vanff651cb2014-12-11 08:52:58 +030011476 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11477 vmcs12->vm_exit_msr_load_count))
11478 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011479}
11480
11481/*
11482 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11483 * and modify vmcs12 to make it see what it would expect to see there if
11484 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11485 */
Jan Kiszka533558b2014-01-04 18:47:20 +010011486static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11487 u32 exit_intr_info,
11488 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011489{
11490 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011491 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11492
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011493 /* trying to cancel vmlaunch/vmresume is a bug */
11494 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11495
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011496 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070011497 * The only expected VM-instruction error is "VM entry with
11498 * invalid control field(s)." Anything else indicates a
11499 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011500 */
Jim Mattson4f350c62017-09-14 16:31:44 -070011501 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
11502 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
11503
11504 leave_guest_mode(vcpu);
11505
11506 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011507 if (exit_reason == -1)
11508 sync_vmcs12(vcpu, vmcs12);
11509 else
11510 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11511 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070011512
11513 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11514 vmcs12->vm_exit_msr_store_count))
11515 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040011516 }
11517
Jim Mattson4f350c62017-09-14 16:31:44 -070011518 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020011519 vm_entry_controls_reset_shadow(vmx);
11520 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010011521 vmx_segment_cache_clear(vmx);
11522
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011523 /* if no vmcs02 cache requested, remove the one we used */
11524 if (VMCS02_POOL_SIZE == 0)
11525 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
11526
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011527 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070011528 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11529 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011530 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011531 if (vmx->hv_deadline_tsc == -1)
11532 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11533 PIN_BASED_VMX_PREEMPTION_TIMER);
11534 else
11535 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11536 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070011537 if (kvm_has_tsc_control)
11538 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011539
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011540 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11541 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11542 vmx_set_virtual_x2apic_mode(vcpu,
11543 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011544 } else if (!nested_cpu_has_ept(vmcs12) &&
11545 nested_cpu_has2(vmcs12,
11546 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11547 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011548 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011549
11550 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11551 vmx->host_rsp = 0;
11552
11553 /* Unpin physical memory we referred to in vmcs02 */
11554 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011555 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011556 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011557 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011558 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011559 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011560 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011561 }
Wincy Van705699a2015-02-03 23:58:17 +080011562 if (vmx->nested.pi_desc_page) {
11563 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011564 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011565 vmx->nested.pi_desc_page = NULL;
11566 vmx->nested.pi_desc = NULL;
11567 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011568
11569 /*
Tang Chen38b99172014-09-24 15:57:54 +080011570 * We are now running in L2, mmu_notifier will force to reload the
11571 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11572 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011573 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011574
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011575 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030011576 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011577
11578 /* in case we halted in L2 */
11579 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070011580
11581 if (likely(!vmx->fail)) {
11582 /*
11583 * TODO: SDM says that with acknowledge interrupt on
11584 * exit, bit 31 of the VM-exit interrupt information
11585 * (valid interrupt) is always set to 1 on
11586 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
11587 * need kvm_cpu_has_interrupt(). See the commit
11588 * message for details.
11589 */
11590 if (nested_exit_intr_ack_set(vcpu) &&
11591 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
11592 kvm_cpu_has_interrupt(vcpu)) {
11593 int irq = kvm_cpu_get_interrupt(vcpu);
11594 WARN_ON(irq < 0);
11595 vmcs12->vm_exit_intr_info = irq |
11596 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11597 }
11598
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011599 if (exit_reason != -1)
11600 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11601 vmcs12->exit_qualification,
11602 vmcs12->idt_vectoring_info_field,
11603 vmcs12->vm_exit_intr_info,
11604 vmcs12->vm_exit_intr_error_code,
11605 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070011606
11607 load_vmcs12_host_state(vcpu, vmcs12);
11608
11609 return;
11610 }
11611
11612 /*
11613 * After an early L2 VM-entry failure, we're now back
11614 * in L1 which thinks it just finished a VMLAUNCH or
11615 * VMRESUME instruction, so we need to set the failure
11616 * flag and the VM-instruction error field of the VMCS
11617 * accordingly.
11618 */
11619 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080011620
11621 load_vmcs12_mmu_host_state(vcpu, vmcs12);
11622
Jim Mattson4f350c62017-09-14 16:31:44 -070011623 /*
11624 * The emulated instruction was already skipped in
11625 * nested_vmx_run, but the updated RIP was never
11626 * written back to the vmcs01.
11627 */
11628 skip_emulated_instruction(vcpu);
11629 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011630}
11631
Nadav Har'El7c177932011-05-25 23:12:04 +030011632/*
Jan Kiszka42124922014-01-04 18:47:19 +010011633 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11634 */
11635static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11636{
Wanpeng Li2f707d92017-03-06 04:03:28 -080011637 if (is_guest_mode(vcpu)) {
11638 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011639 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080011640 }
Jan Kiszka42124922014-01-04 18:47:19 +010011641 free_nested(to_vmx(vcpu));
11642}
11643
11644/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011645 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11646 * 23.7 "VM-entry failures during or after loading guest state" (this also
11647 * lists the acceptable exit-reason and exit-qualification parameters).
11648 * It should only be called before L2 actually succeeded to run, and when
11649 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11650 */
11651static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11652 struct vmcs12 *vmcs12,
11653 u32 reason, unsigned long qualification)
11654{
11655 load_vmcs12_host_state(vcpu, vmcs12);
11656 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11657 vmcs12->exit_qualification = qualification;
11658 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011659 if (enable_shadow_vmcs)
11660 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011661}
11662
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011663static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11664 struct x86_instruction_info *info,
11665 enum x86_intercept_stage stage)
11666{
11667 return X86EMUL_CONTINUE;
11668}
11669
Yunhong Jiang64672c92016-06-13 14:19:59 -070011670#ifdef CONFIG_X86_64
11671/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11672static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11673 u64 divisor, u64 *result)
11674{
11675 u64 low = a << shift, high = a >> (64 - shift);
11676
11677 /* To avoid the overflow on divq */
11678 if (high >= divisor)
11679 return 1;
11680
11681 /* Low hold the result, high hold rem which is discarded */
11682 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11683 "rm" (divisor), "0" (low), "1" (high));
11684 *result = low;
11685
11686 return 0;
11687}
11688
11689static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11690{
11691 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011692 u64 tscl = rdtsc();
11693 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11694 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011695
11696 /* Convert to host delta tsc if tsc scaling is enabled */
11697 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11698 u64_shl_div_u64(delta_tsc,
11699 kvm_tsc_scaling_ratio_frac_bits,
11700 vcpu->arch.tsc_scaling_ratio,
11701 &delta_tsc))
11702 return -ERANGE;
11703
11704 /*
11705 * If the delta tsc can't fit in the 32 bit after the multi shift,
11706 * we can't use the preemption timer.
11707 * It's possible that it fits on later vmentries, but checking
11708 * on every vmentry is costly so we just use an hrtimer.
11709 */
11710 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11711 return -ERANGE;
11712
11713 vmx->hv_deadline_tsc = tscl + delta_tsc;
11714 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11715 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070011716
11717 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011718}
11719
11720static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11721{
11722 struct vcpu_vmx *vmx = to_vmx(vcpu);
11723 vmx->hv_deadline_tsc = -1;
11724 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11725 PIN_BASED_VMX_PREEMPTION_TIMER);
11726}
11727#endif
11728
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011729static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011730{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011731 if (ple_gap)
11732 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011733}
11734
Kai Huang843e4332015-01-28 10:54:28 +080011735static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11736 struct kvm_memory_slot *slot)
11737{
11738 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11739 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11740}
11741
11742static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11743 struct kvm_memory_slot *slot)
11744{
11745 kvm_mmu_slot_set_dirty(kvm, slot);
11746}
11747
11748static void vmx_flush_log_dirty(struct kvm *kvm)
11749{
11750 kvm_flush_pml_buffers(kvm);
11751}
11752
Bandan Dasc5f983f2017-05-05 15:25:14 -040011753static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
11754{
11755 struct vmcs12 *vmcs12;
11756 struct vcpu_vmx *vmx = to_vmx(vcpu);
11757 gpa_t gpa;
11758 struct page *page = NULL;
11759 u64 *pml_address;
11760
11761 if (is_guest_mode(vcpu)) {
11762 WARN_ON_ONCE(vmx->nested.pml_full);
11763
11764 /*
11765 * Check if PML is enabled for the nested guest.
11766 * Whether eptp bit 6 is set is already checked
11767 * as part of A/D emulation.
11768 */
11769 vmcs12 = get_vmcs12(vcpu);
11770 if (!nested_cpu_has_pml(vmcs12))
11771 return 0;
11772
Dan Carpenter47698862017-05-10 22:43:17 +030011773 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040011774 vmx->nested.pml_full = true;
11775 return 1;
11776 }
11777
11778 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
11779
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011780 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
11781 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040011782 return 0;
11783
11784 pml_address = kmap(page);
11785 pml_address[vmcs12->guest_pml_index--] = gpa;
11786 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011787 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011788 }
11789
11790 return 0;
11791}
11792
Kai Huang843e4332015-01-28 10:54:28 +080011793static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11794 struct kvm_memory_slot *memslot,
11795 gfn_t offset, unsigned long mask)
11796{
11797 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11798}
11799
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011800static void __pi_post_block(struct kvm_vcpu *vcpu)
11801{
11802 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11803 struct pi_desc old, new;
11804 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011805
11806 do {
11807 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011808 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
11809 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011810
11811 dest = cpu_physical_id(vcpu->cpu);
11812
11813 if (x2apic_enabled())
11814 new.ndst = dest;
11815 else
11816 new.ndst = (dest << 8) & 0xFF00;
11817
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011818 /* set 'NV' to 'notification vector' */
11819 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020011820 } while (cmpxchg64(&pi_desc->control, old.control,
11821 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011822
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011823 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
11824 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011825 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011826 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011827 vcpu->pre_pcpu = -1;
11828 }
11829}
11830
Feng Wuefc64402015-09-18 22:29:51 +080011831/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080011832 * This routine does the following things for vCPU which is going
11833 * to be blocked if VT-d PI is enabled.
11834 * - Store the vCPU to the wakeup list, so when interrupts happen
11835 * we can find the right vCPU to wake up.
11836 * - Change the Posted-interrupt descriptor as below:
11837 * 'NDST' <-- vcpu->pre_pcpu
11838 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11839 * - If 'ON' is set during this process, which means at least one
11840 * interrupt is posted for this vCPU, we cannot block it, in
11841 * this case, return 1, otherwise, return 0.
11842 *
11843 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070011844static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011845{
Feng Wubf9f6ac2015-09-18 22:29:55 +080011846 unsigned int dest;
11847 struct pi_desc old, new;
11848 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11849
11850 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011851 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11852 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011853 return 0;
11854
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011855 WARN_ON(irqs_disabled());
11856 local_irq_disable();
11857 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
11858 vcpu->pre_pcpu = vcpu->cpu;
11859 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11860 list_add_tail(&vcpu->blocked_vcpu_list,
11861 &per_cpu(blocked_vcpu_on_cpu,
11862 vcpu->pre_pcpu));
11863 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11864 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080011865
11866 do {
11867 old.control = new.control = pi_desc->control;
11868
Feng Wubf9f6ac2015-09-18 22:29:55 +080011869 WARN((pi_desc->sn == 1),
11870 "Warning: SN field of posted-interrupts "
11871 "is set before blocking\n");
11872
11873 /*
11874 * Since vCPU can be preempted during this process,
11875 * vcpu->cpu could be different with pre_pcpu, we
11876 * need to set pre_pcpu as the destination of wakeup
11877 * notification event, then we can find the right vCPU
11878 * to wakeup in wakeup handler if interrupts happen
11879 * when the vCPU is in blocked state.
11880 */
11881 dest = cpu_physical_id(vcpu->pre_pcpu);
11882
11883 if (x2apic_enabled())
11884 new.ndst = dest;
11885 else
11886 new.ndst = (dest << 8) & 0xFF00;
11887
11888 /* set 'NV' to 'wakeup vector' */
11889 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020011890 } while (cmpxchg64(&pi_desc->control, old.control,
11891 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011892
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011893 /* We should not block the vCPU if an interrupt is posted for it. */
11894 if (pi_test_on(pi_desc) == 1)
11895 __pi_post_block(vcpu);
11896
11897 local_irq_enable();
11898 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011899}
11900
Yunhong Jiangbc225122016-06-13 14:19:58 -070011901static int vmx_pre_block(struct kvm_vcpu *vcpu)
11902{
11903 if (pi_pre_block(vcpu))
11904 return 1;
11905
Yunhong Jiang64672c92016-06-13 14:19:59 -070011906 if (kvm_lapic_hv_timer_in_use(vcpu))
11907 kvm_lapic_switch_to_sw_timer(vcpu);
11908
Yunhong Jiangbc225122016-06-13 14:19:58 -070011909 return 0;
11910}
11911
11912static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011913{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011914 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011915 return;
11916
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011917 WARN_ON(irqs_disabled());
11918 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011919 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011920 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080011921}
11922
Yunhong Jiangbc225122016-06-13 14:19:58 -070011923static void vmx_post_block(struct kvm_vcpu *vcpu)
11924{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011925 if (kvm_x86_ops->set_hv_timer)
11926 kvm_lapic_switch_to_hv_timer(vcpu);
11927
Yunhong Jiangbc225122016-06-13 14:19:58 -070011928 pi_post_block(vcpu);
11929}
11930
Feng Wubf9f6ac2015-09-18 22:29:55 +080011931/*
Feng Wuefc64402015-09-18 22:29:51 +080011932 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11933 *
11934 * @kvm: kvm
11935 * @host_irq: host irq of the interrupt
11936 * @guest_irq: gsi of the interrupt
11937 * @set: set or unset PI
11938 * returns 0 on success, < 0 on failure
11939 */
11940static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11941 uint32_t guest_irq, bool set)
11942{
11943 struct kvm_kernel_irq_routing_entry *e;
11944 struct kvm_irq_routing_table *irq_rt;
11945 struct kvm_lapic_irq irq;
11946 struct kvm_vcpu *vcpu;
11947 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010011948 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080011949
11950 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011951 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11952 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011953 return 0;
11954
11955 idx = srcu_read_lock(&kvm->irq_srcu);
11956 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010011957 if (guest_irq >= irq_rt->nr_rt_entries ||
11958 hlist_empty(&irq_rt->map[guest_irq])) {
11959 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
11960 guest_irq, irq_rt->nr_rt_entries);
11961 goto out;
11962 }
Feng Wuefc64402015-09-18 22:29:51 +080011963
11964 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11965 if (e->type != KVM_IRQ_ROUTING_MSI)
11966 continue;
11967 /*
11968 * VT-d PI cannot support posting multicast/broadcast
11969 * interrupts to a vCPU, we still use interrupt remapping
11970 * for these kind of interrupts.
11971 *
11972 * For lowest-priority interrupts, we only support
11973 * those with single CPU as the destination, e.g. user
11974 * configures the interrupts via /proc/irq or uses
11975 * irqbalance to make the interrupts single-CPU.
11976 *
11977 * We will support full lowest-priority interrupt later.
11978 */
11979
Radim Krčmář371313132016-07-12 22:09:27 +020011980 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011981 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11982 /*
11983 * Make sure the IRTE is in remapped mode if
11984 * we don't handle it in posted mode.
11985 */
11986 ret = irq_set_vcpu_affinity(host_irq, NULL);
11987 if (ret < 0) {
11988 printk(KERN_INFO
11989 "failed to back to remapped mode, irq: %u\n",
11990 host_irq);
11991 goto out;
11992 }
11993
Feng Wuefc64402015-09-18 22:29:51 +080011994 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011995 }
Feng Wuefc64402015-09-18 22:29:51 +080011996
11997 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11998 vcpu_info.vector = irq.vector;
11999
Feng Wub6ce9782016-01-25 16:53:35 +080012000 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012001 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12002
12003 if (set)
12004 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2eb2017-09-18 09:56:49 +080012005 else
Feng Wuefc64402015-09-18 22:29:51 +080012006 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012007
12008 if (ret < 0) {
12009 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12010 __func__);
12011 goto out;
12012 }
12013 }
12014
12015 ret = 0;
12016out:
12017 srcu_read_unlock(&kvm->irq_srcu, idx);
12018 return ret;
12019}
12020
Ashok Rajc45dcc72016-06-22 14:59:56 +080012021static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12022{
12023 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12024 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12025 FEATURE_CONTROL_LMCE;
12026 else
12027 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12028 ~FEATURE_CONTROL_LMCE;
12029}
12030
Ladi Prosek72d7b372017-10-11 16:54:41 +020012031static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12032{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012033 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12034 if (to_vmx(vcpu)->nested.nested_run_pending)
12035 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012036 return 1;
12037}
12038
Ladi Prosek0234bf82017-10-11 16:54:40 +020012039static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12040{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012041 struct vcpu_vmx *vmx = to_vmx(vcpu);
12042
12043 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12044 if (vmx->nested.smm.guest_mode)
12045 nested_vmx_vmexit(vcpu, -1, 0, 0);
12046
12047 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12048 vmx->nested.vmxon = false;
Ladi Prosek0234bf82017-10-11 16:54:40 +020012049 return 0;
12050}
12051
12052static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12053{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012054 struct vcpu_vmx *vmx = to_vmx(vcpu);
12055 int ret;
12056
12057 if (vmx->nested.smm.vmxon) {
12058 vmx->nested.vmxon = true;
12059 vmx->nested.smm.vmxon = false;
12060 }
12061
12062 if (vmx->nested.smm.guest_mode) {
12063 vcpu->arch.hflags &= ~HF_SMM_MASK;
12064 ret = enter_vmx_non_root_mode(vcpu, false);
12065 vcpu->arch.hflags |= HF_SMM_MASK;
12066 if (ret)
12067 return ret;
12068
12069 vmx->nested.smm.guest_mode = false;
12070 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020012071 return 0;
12072}
12073
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012074static int enable_smi_window(struct kvm_vcpu *vcpu)
12075{
12076 return 0;
12077}
12078
Kees Cook404f6aa2016-08-08 16:29:06 -070012079static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080012080 .cpu_has_kvm_support = cpu_has_kvm_support,
12081 .disabled_by_bios = vmx_disabled_by_bios,
12082 .hardware_setup = hardware_setup,
12083 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030012084 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012085 .hardware_enable = hardware_enable,
12086 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080012087 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020012088 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012089
12090 .vcpu_create = vmx_create_vcpu,
12091 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030012092 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012093
Avi Kivity04d2cc72007-09-10 18:10:54 +030012094 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012095 .vcpu_load = vmx_vcpu_load,
12096 .vcpu_put = vmx_vcpu_put,
12097
Paolo Bonzinia96036b2015-11-10 11:55:36 +010012098 .update_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012099 .get_msr = vmx_get_msr,
12100 .set_msr = vmx_set_msr,
12101 .get_segment_base = vmx_get_segment_base,
12102 .get_segment = vmx_get_segment,
12103 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020012104 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012105 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020012106 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020012107 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030012108 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012109 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012110 .set_cr3 = vmx_set_cr3,
12111 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012112 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012113 .get_idt = vmx_get_idt,
12114 .set_idt = vmx_set_idt,
12115 .get_gdt = vmx_get_gdt,
12116 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010012117 .get_dr6 = vmx_get_dr6,
12118 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030012119 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010012120 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030012121 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012122 .get_rflags = vmx_get_rflags,
12123 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080012124
Avi Kivity6aa8b732006-12-10 02:21:36 -080012125 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012126
Avi Kivity6aa8b732006-12-10 02:21:36 -080012127 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020012128 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012129 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040012130 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12131 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020012132 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030012133 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012134 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020012135 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030012136 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020012137 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012138 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010012139 .get_nmi_mask = vmx_get_nmi_mask,
12140 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012141 .enable_nmi_window = enable_nmi_window,
12142 .enable_irq_window = enable_irq_window,
12143 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080012144 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080012145 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030012146 .get_enable_apicv = vmx_get_enable_apicv,
12147 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012148 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010012149 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012150 .hwapic_irr_update = vmx_hwapic_irr_update,
12151 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080012152 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12153 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012154
Izik Eiduscbc94022007-10-25 00:29:55 +020012155 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080012156 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080012157 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030012158
Avi Kivity586f9602010-11-18 13:09:54 +020012159 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020012160
Sheng Yang17cc3932010-01-05 19:02:27 +080012161 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080012162
12163 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080012164
12165 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000012166 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020012167
12168 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080012169
12170 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012171
12172 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020012173
12174 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012175
12176 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080012177 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000012178 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080012179 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012180
12181 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012182
12183 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080012184
12185 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12186 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12187 .flush_log_dirty = vmx_flush_log_dirty,
12188 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040012189 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020012190
Feng Wubf9f6ac2015-09-18 22:29:55 +080012191 .pre_block = vmx_pre_block,
12192 .post_block = vmx_post_block,
12193
Wei Huang25462f72015-06-19 15:45:05 +020012194 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080012195
12196 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070012197
12198#ifdef CONFIG_X86_64
12199 .set_hv_timer = vmx_set_hv_timer,
12200 .cancel_hv_timer = vmx_cancel_hv_timer,
12201#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080012202
12203 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012204
Ladi Prosek72d7b372017-10-11 16:54:41 +020012205 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012206 .pre_enter_smm = vmx_pre_enter_smm,
12207 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012208 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012209};
12210
12211static int __init vmx_init(void)
12212{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012213 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
12214 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030012215 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012216 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080012217
Dave Young2965faa2015-09-09 15:38:55 -070012218#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012219 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12220 crash_vmclear_local_loaded_vmcss);
12221#endif
12222
He, Qingfdef3ad2007-04-30 09:45:24 +030012223 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080012224}
12225
12226static void __exit vmx_exit(void)
12227{
Dave Young2965faa2015-09-09 15:38:55 -070012228#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053012229 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012230 synchronize_rcu();
12231#endif
12232
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080012233 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080012234}
12235
12236module_init(vmx_init)
12237module_exit(vmx_exit)