blob: 14976cd4e0c102057a3a9a05fd2f3f330086b0b7 [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
Avi Kivity61d2ef22010-04-28 16:40:38 +0300187
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400188struct vmcs {
189 u32 revision_id;
190 u32 abort;
191 char data[0];
192};
193
Nadav Har'Eld462b812011-05-24 15:26:10 +0300194/*
195 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
196 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
197 * loaded on this CPU (so we can clear them if the CPU goes down).
198 */
199struct loaded_vmcs {
200 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700201 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300202 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200203 bool launched;
204 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200205 unsigned long vmcs_host_cr3; /* May not match real cr3 */
206 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100207 /* Support for vnmi-less CPUs */
208 int soft_vnmi_blocked;
209 ktime_t entry_time;
210 s64 vnmi_blocked_time;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300211 struct list_head loaded_vmcss_on_cpu_link;
212};
213
Avi Kivity26bb0982009-09-07 11:14:12 +0300214struct shared_msr_entry {
215 unsigned index;
216 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200217 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300218};
219
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300220/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300221 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
222 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
223 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
224 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
225 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
226 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattson00647b42017-11-27 17:22:25 -0600227 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300228 * underlying hardware which will be used to run L2.
229 * This structure is packed to ensure that its layout is identical across
230 * machines (necessary for live migration).
231 * If there are changes in this struct, VMCS12_REVISION must be changed.
232 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300233typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300234struct __packed vmcs12 {
235 /* According to the Intel spec, a VMCS region must start with the
236 * following two fields. Then follow implementation-specific data.
237 */
238 u32 revision_id;
239 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300240
Nadav Har'El27d6c862011-05-25 23:06:59 +0300241 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
242 u32 padding[7]; /* room for future expansion */
243
Nadav Har'El22bd0352011-05-25 23:05:57 +0300244 u64 io_bitmap_a;
245 u64 io_bitmap_b;
246 u64 msr_bitmap;
247 u64 vm_exit_msr_store_addr;
248 u64 vm_exit_msr_load_addr;
249 u64 vm_entry_msr_load_addr;
250 u64 tsc_offset;
251 u64 virtual_apic_page_addr;
252 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800253 u64 posted_intr_desc_addr;
Bandan Das27c42a12017-08-03 15:54:42 -0400254 u64 vm_function_control;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300255 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800256 u64 eoi_exit_bitmap0;
257 u64 eoi_exit_bitmap1;
258 u64 eoi_exit_bitmap2;
259 u64 eoi_exit_bitmap3;
Bandan Das41ab9372017-08-03 15:54:43 -0400260 u64 eptp_list_address;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800261 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300262 u64 guest_physical_address;
263 u64 vmcs_link_pointer;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400264 u64 pml_address;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300265 u64 guest_ia32_debugctl;
266 u64 guest_ia32_pat;
267 u64 guest_ia32_efer;
268 u64 guest_ia32_perf_global_ctrl;
269 u64 guest_pdptr0;
270 u64 guest_pdptr1;
271 u64 guest_pdptr2;
272 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100273 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300274 u64 host_ia32_pat;
275 u64 host_ia32_efer;
276 u64 host_ia32_perf_global_ctrl;
277 u64 padding64[8]; /* room for future expansion */
278 /*
279 * To allow migration of L1 (complete with its L2 guests) between
280 * machines of different natural widths (32 or 64 bit), we cannot have
281 * unsigned long fields with no explict size. We use u64 (aliased
282 * natural_width) instead. Luckily, x86 is little-endian.
283 */
284 natural_width cr0_guest_host_mask;
285 natural_width cr4_guest_host_mask;
286 natural_width cr0_read_shadow;
287 natural_width cr4_read_shadow;
288 natural_width cr3_target_value0;
289 natural_width cr3_target_value1;
290 natural_width cr3_target_value2;
291 natural_width cr3_target_value3;
292 natural_width exit_qualification;
293 natural_width guest_linear_address;
294 natural_width guest_cr0;
295 natural_width guest_cr3;
296 natural_width guest_cr4;
297 natural_width guest_es_base;
298 natural_width guest_cs_base;
299 natural_width guest_ss_base;
300 natural_width guest_ds_base;
301 natural_width guest_fs_base;
302 natural_width guest_gs_base;
303 natural_width guest_ldtr_base;
304 natural_width guest_tr_base;
305 natural_width guest_gdtr_base;
306 natural_width guest_idtr_base;
307 natural_width guest_dr7;
308 natural_width guest_rsp;
309 natural_width guest_rip;
310 natural_width guest_rflags;
311 natural_width guest_pending_dbg_exceptions;
312 natural_width guest_sysenter_esp;
313 natural_width guest_sysenter_eip;
314 natural_width host_cr0;
315 natural_width host_cr3;
316 natural_width host_cr4;
317 natural_width host_fs_base;
318 natural_width host_gs_base;
319 natural_width host_tr_base;
320 natural_width host_gdtr_base;
321 natural_width host_idtr_base;
322 natural_width host_ia32_sysenter_esp;
323 natural_width host_ia32_sysenter_eip;
324 natural_width host_rsp;
325 natural_width host_rip;
326 natural_width paddingl[8]; /* room for future expansion */
327 u32 pin_based_vm_exec_control;
328 u32 cpu_based_vm_exec_control;
329 u32 exception_bitmap;
330 u32 page_fault_error_code_mask;
331 u32 page_fault_error_code_match;
332 u32 cr3_target_count;
333 u32 vm_exit_controls;
334 u32 vm_exit_msr_store_count;
335 u32 vm_exit_msr_load_count;
336 u32 vm_entry_controls;
337 u32 vm_entry_msr_load_count;
338 u32 vm_entry_intr_info_field;
339 u32 vm_entry_exception_error_code;
340 u32 vm_entry_instruction_len;
341 u32 tpr_threshold;
342 u32 secondary_vm_exec_control;
343 u32 vm_instruction_error;
344 u32 vm_exit_reason;
345 u32 vm_exit_intr_info;
346 u32 vm_exit_intr_error_code;
347 u32 idt_vectoring_info_field;
348 u32 idt_vectoring_error_code;
349 u32 vm_exit_instruction_len;
350 u32 vmx_instruction_info;
351 u32 guest_es_limit;
352 u32 guest_cs_limit;
353 u32 guest_ss_limit;
354 u32 guest_ds_limit;
355 u32 guest_fs_limit;
356 u32 guest_gs_limit;
357 u32 guest_ldtr_limit;
358 u32 guest_tr_limit;
359 u32 guest_gdtr_limit;
360 u32 guest_idtr_limit;
361 u32 guest_es_ar_bytes;
362 u32 guest_cs_ar_bytes;
363 u32 guest_ss_ar_bytes;
364 u32 guest_ds_ar_bytes;
365 u32 guest_fs_ar_bytes;
366 u32 guest_gs_ar_bytes;
367 u32 guest_ldtr_ar_bytes;
368 u32 guest_tr_ar_bytes;
369 u32 guest_interruptibility_info;
370 u32 guest_activity_state;
371 u32 guest_sysenter_cs;
372 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100373 u32 vmx_preemption_timer_value;
374 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300375 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800376 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300377 u16 guest_es_selector;
378 u16 guest_cs_selector;
379 u16 guest_ss_selector;
380 u16 guest_ds_selector;
381 u16 guest_fs_selector;
382 u16 guest_gs_selector;
383 u16 guest_ldtr_selector;
384 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800385 u16 guest_intr_status;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400386 u16 guest_pml_index;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300387 u16 host_es_selector;
388 u16 host_cs_selector;
389 u16 host_ss_selector;
390 u16 host_ds_selector;
391 u16 host_fs_selector;
392 u16 host_gs_selector;
393 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300394};
395
396/*
397 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
398 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
399 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
400 */
401#define VMCS12_REVISION 0x11e57ed0
402
403/*
404 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
405 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
406 * current implementation, 4K are reserved to avoid future complications.
407 */
408#define VMCS12_SIZE 0x1000
409
410/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300411 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
412 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
413 */
414struct nested_vmx {
415 /* Has the level1 guest done vmxon? */
416 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400417 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400418 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300419
420 /* The guest-physical address of the current VMCS L1 keeps for L2 */
421 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700422 /*
423 * Cache of the guest's VMCS, existing outside of guest memory.
424 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700425 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700426 */
427 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300428 /*
429 * Indicates if the shadow vmcs must be updated with the
430 * data hold by vmcs12
431 */
432 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300433
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200434 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300435 /* L2 must run next, and mustn't decide to exit to L1. */
436 bool nested_run_pending;
Jim Mattson00647b42017-11-27 17:22:25 -0600437
438 struct loaded_vmcs vmcs02;
439
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300440 /*
Jim Mattson00647b42017-11-27 17:22:25 -0600441 * Guest pages referred to in the vmcs02 with host-physical
442 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300443 */
444 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800445 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800446 struct page *pi_desc_page;
447 struct pi_desc *pi_desc;
448 bool pi_pending;
449 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100450
Radim Krčmářd048c092016-08-08 20:16:22 +0200451 unsigned long *msr_bitmap;
452
Jan Kiszkaf41245002014-03-07 20:03:13 +0100453 struct hrtimer preemption_timer;
454 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200455
456 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
457 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800458
Wanpeng Li5c614b32015-10-13 09:18:36 -0700459 u16 vpid02;
460 u16 last_vpid;
461
David Matlack0115f9c2016-11-29 18:14:06 -0800462 /*
463 * We only store the "true" versions of the VMX capability MSRs. We
464 * generate the "non-true" versions by setting the must-be-1 bits
465 * according to the SDM.
466 */
Wincy Vanb9c237b2015-02-03 23:56:30 +0800467 u32 nested_vmx_procbased_ctls_low;
468 u32 nested_vmx_procbased_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800469 u32 nested_vmx_secondary_ctls_low;
470 u32 nested_vmx_secondary_ctls_high;
471 u32 nested_vmx_pinbased_ctls_low;
472 u32 nested_vmx_pinbased_ctls_high;
473 u32 nested_vmx_exit_ctls_low;
474 u32 nested_vmx_exit_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800475 u32 nested_vmx_entry_ctls_low;
476 u32 nested_vmx_entry_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800477 u32 nested_vmx_misc_low;
478 u32 nested_vmx_misc_high;
479 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700480 u32 nested_vmx_vpid_caps;
David Matlack62cc6b9d2016-11-29 18:14:07 -0800481 u64 nested_vmx_basic;
482 u64 nested_vmx_cr0_fixed0;
483 u64 nested_vmx_cr0_fixed1;
484 u64 nested_vmx_cr4_fixed0;
485 u64 nested_vmx_cr4_fixed1;
486 u64 nested_vmx_vmcs_enum;
Bandan Das27c42a12017-08-03 15:54:42 -0400487 u64 nested_vmx_vmfunc_controls;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200488
489 /* SMM related state */
490 struct {
491 /* in VMX operation on SMM entry? */
492 bool vmxon;
493 /* in guest mode on SMM entry? */
494 bool guest_mode;
495 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300496};
497
Yang Zhang01e439b2013-04-11 19:25:12 +0800498#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800499#define POSTED_INTR_SN 1
500
Yang Zhang01e439b2013-04-11 19:25:12 +0800501/* Posted-Interrupt Descriptor */
502struct pi_desc {
503 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800504 union {
505 struct {
506 /* bit 256 - Outstanding Notification */
507 u16 on : 1,
508 /* bit 257 - Suppress Notification */
509 sn : 1,
510 /* bit 271:258 - Reserved */
511 rsvd_1 : 14;
512 /* bit 279:272 - Notification Vector */
513 u8 nv;
514 /* bit 287:280 - Reserved */
515 u8 rsvd_2;
516 /* bit 319:288 - Notification Destination */
517 u32 ndst;
518 };
519 u64 control;
520 };
521 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800522} __aligned(64);
523
Yang Zhanga20ed542013-04-11 19:25:15 +0800524static bool pi_test_and_set_on(struct pi_desc *pi_desc)
525{
526 return test_and_set_bit(POSTED_INTR_ON,
527 (unsigned long *)&pi_desc->control);
528}
529
530static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
531{
532 return test_and_clear_bit(POSTED_INTR_ON,
533 (unsigned long *)&pi_desc->control);
534}
535
536static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
537{
538 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
539}
540
Feng Wuebbfc762015-09-18 22:29:46 +0800541static inline void pi_clear_sn(struct pi_desc *pi_desc)
542{
543 return clear_bit(POSTED_INTR_SN,
544 (unsigned long *)&pi_desc->control);
545}
546
547static inline void pi_set_sn(struct pi_desc *pi_desc)
548{
549 return set_bit(POSTED_INTR_SN,
550 (unsigned long *)&pi_desc->control);
551}
552
Paolo Bonziniad361092016-09-20 16:15:05 +0200553static inline void pi_clear_on(struct pi_desc *pi_desc)
554{
555 clear_bit(POSTED_INTR_ON,
556 (unsigned long *)&pi_desc->control);
557}
558
Feng Wuebbfc762015-09-18 22:29:46 +0800559static inline int pi_test_on(struct pi_desc *pi_desc)
560{
561 return test_bit(POSTED_INTR_ON,
562 (unsigned long *)&pi_desc->control);
563}
564
565static inline int pi_test_sn(struct pi_desc *pi_desc)
566{
567 return test_bit(POSTED_INTR_SN,
568 (unsigned long *)&pi_desc->control);
569}
570
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400571struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000572 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300573 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300574 u8 fail;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300575 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200576 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200577 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300578 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400579 int nmsrs;
580 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800581 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400582#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300583 u64 msr_host_kernel_gs_base;
584 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400585#endif
Gleb Natapov2961e8762013-11-25 15:37:13 +0200586 u32 vm_entry_controls_shadow;
587 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200588 u32 secondary_exec_control;
589
Nadav Har'Eld462b812011-05-24 15:26:10 +0300590 /*
591 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
592 * non-nested (L1) guest, it always points to vmcs01. For a nested
593 * guest (L2), it points to a different VMCS.
594 */
595 struct loaded_vmcs vmcs01;
596 struct loaded_vmcs *loaded_vmcs;
597 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300598 struct msr_autoload {
599 unsigned nr;
600 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
601 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
602 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400603 struct {
604 int loaded;
605 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300606#ifdef CONFIG_X86_64
607 u16 ds_sel, es_sel;
608#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200609 int gs_ldt_reload_needed;
610 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000611 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400612 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200613 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300614 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300615 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300616 struct kvm_segment segs[8];
617 } rmode;
618 struct {
619 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300620 struct kvm_save_segment {
621 u16 selector;
622 unsigned long base;
623 u32 limit;
624 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300625 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300626 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800627 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300628 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200629
Andi Kleena0861c02009-06-08 17:37:09 +0800630 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800631
Yang Zhang01e439b2013-04-11 19:25:12 +0800632 /* Posted interrupt descriptor */
633 struct pi_desc pi_desc;
634
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300635 /* Support for a guest hypervisor (nested VMX) */
636 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200637
638 /* Dynamic PLE window. */
639 int ple_window;
640 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800641
642 /* Support for PML */
643#define PML_ENTITY_NUM 512
644 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800645
Yunhong Jiang64672c92016-06-13 14:19:59 -0700646 /* apic deadline value in host tsc */
647 u64 hv_deadline_tsc;
648
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800649 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800650
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800651 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800652
Wanpeng Li74c55932017-11-29 01:31:20 -0800653 unsigned long host_debugctlmsr;
654
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800655 /*
656 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
657 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
658 * in msr_ia32_feature_control_valid_bits.
659 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800660 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800661 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400662};
663
Avi Kivity2fb92db2011-04-27 19:42:18 +0300664enum segment_cache_field {
665 SEG_FIELD_SEL = 0,
666 SEG_FIELD_BASE = 1,
667 SEG_FIELD_LIMIT = 2,
668 SEG_FIELD_AR = 3,
669
670 SEG_FIELD_NR = 4
671};
672
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400673static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
674{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000675 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400676}
677
Feng Wuefc64402015-09-18 22:29:51 +0800678static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
679{
680 return &(to_vmx(vcpu)->pi_desc);
681}
682
Nadav Har'El22bd0352011-05-25 23:05:57 +0300683#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
684#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
685#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
686 [number##_HIGH] = VMCS12_OFFSET(name)+4
687
Abel Gordon4607c2d2013-04-18 14:35:55 +0300688
Bandan Dasfe2b2012014-04-21 15:20:14 -0400689static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300690 /*
691 * We do NOT shadow fields that are modified when L0
692 * traps and emulates any vmx instruction (e.g. VMPTRLD,
693 * VMXON...) executed by L1.
694 * For example, VM_INSTRUCTION_ERROR is read
695 * by L1 if a vmx instruction fails (part of the error path).
696 * Note the code assumes this logic. If for some reason
697 * we start shadowing these fields then we need to
698 * force a shadow sync when L0 emulates vmx instructions
699 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
700 * by nested_vmx_failValid)
701 */
702 VM_EXIT_REASON,
703 VM_EXIT_INTR_INFO,
704 VM_EXIT_INSTRUCTION_LEN,
705 IDT_VECTORING_INFO_FIELD,
706 IDT_VECTORING_ERROR_CODE,
707 VM_EXIT_INTR_ERROR_CODE,
708 EXIT_QUALIFICATION,
709 GUEST_LINEAR_ADDRESS,
710 GUEST_PHYSICAL_ADDRESS
711};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400712static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300713 ARRAY_SIZE(shadow_read_only_fields);
714
Bandan Dasfe2b2012014-04-21 15:20:14 -0400715static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800716 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300717 GUEST_RIP,
718 GUEST_RSP,
719 GUEST_CR0,
720 GUEST_CR3,
721 GUEST_CR4,
722 GUEST_INTERRUPTIBILITY_INFO,
723 GUEST_RFLAGS,
724 GUEST_CS_SELECTOR,
725 GUEST_CS_AR_BYTES,
726 GUEST_CS_LIMIT,
727 GUEST_CS_BASE,
728 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100729 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300730 CR0_GUEST_HOST_MASK,
731 CR0_READ_SHADOW,
732 CR4_READ_SHADOW,
733 TSC_OFFSET,
734 EXCEPTION_BITMAP,
735 CPU_BASED_VM_EXEC_CONTROL,
736 VM_ENTRY_EXCEPTION_ERROR_CODE,
737 VM_ENTRY_INTR_INFO_FIELD,
738 VM_ENTRY_INSTRUCTION_LEN,
739 VM_ENTRY_EXCEPTION_ERROR_CODE,
740 HOST_FS_BASE,
741 HOST_GS_BASE,
742 HOST_FS_SELECTOR,
743 HOST_GS_SELECTOR
744};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400745static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300746 ARRAY_SIZE(shadow_read_write_fields);
747
Mathias Krause772e0312012-08-30 01:30:19 +0200748static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300749 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800750 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300751 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
752 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
753 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
754 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
755 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
756 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
757 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
758 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800759 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400760 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300761 FIELD(HOST_ES_SELECTOR, host_es_selector),
762 FIELD(HOST_CS_SELECTOR, host_cs_selector),
763 FIELD(HOST_SS_SELECTOR, host_ss_selector),
764 FIELD(HOST_DS_SELECTOR, host_ds_selector),
765 FIELD(HOST_FS_SELECTOR, host_fs_selector),
766 FIELD(HOST_GS_SELECTOR, host_gs_selector),
767 FIELD(HOST_TR_SELECTOR, host_tr_selector),
768 FIELD64(IO_BITMAP_A, io_bitmap_a),
769 FIELD64(IO_BITMAP_B, io_bitmap_b),
770 FIELD64(MSR_BITMAP, msr_bitmap),
771 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
772 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
773 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
774 FIELD64(TSC_OFFSET, tsc_offset),
775 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
776 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800777 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400778 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300779 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800780 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
781 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
782 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
783 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400784 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800785 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300786 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
787 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400788 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300789 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
790 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
791 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
792 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
793 FIELD64(GUEST_PDPTR0, guest_pdptr0),
794 FIELD64(GUEST_PDPTR1, guest_pdptr1),
795 FIELD64(GUEST_PDPTR2, guest_pdptr2),
796 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100797 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300798 FIELD64(HOST_IA32_PAT, host_ia32_pat),
799 FIELD64(HOST_IA32_EFER, host_ia32_efer),
800 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
801 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
802 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
803 FIELD(EXCEPTION_BITMAP, exception_bitmap),
804 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
805 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
806 FIELD(CR3_TARGET_COUNT, cr3_target_count),
807 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
808 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
809 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
810 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
811 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
812 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
813 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
814 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
815 FIELD(TPR_THRESHOLD, tpr_threshold),
816 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
817 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
818 FIELD(VM_EXIT_REASON, vm_exit_reason),
819 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
820 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
821 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
822 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
823 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
824 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
825 FIELD(GUEST_ES_LIMIT, guest_es_limit),
826 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
827 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
828 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
829 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
830 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
831 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
832 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
833 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
834 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
835 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
836 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
837 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
838 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
839 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
840 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
841 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
842 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
843 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
844 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
845 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
846 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100847 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300848 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
849 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
850 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
851 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
852 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
853 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
854 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
855 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
856 FIELD(EXIT_QUALIFICATION, exit_qualification),
857 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
858 FIELD(GUEST_CR0, guest_cr0),
859 FIELD(GUEST_CR3, guest_cr3),
860 FIELD(GUEST_CR4, guest_cr4),
861 FIELD(GUEST_ES_BASE, guest_es_base),
862 FIELD(GUEST_CS_BASE, guest_cs_base),
863 FIELD(GUEST_SS_BASE, guest_ss_base),
864 FIELD(GUEST_DS_BASE, guest_ds_base),
865 FIELD(GUEST_FS_BASE, guest_fs_base),
866 FIELD(GUEST_GS_BASE, guest_gs_base),
867 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
868 FIELD(GUEST_TR_BASE, guest_tr_base),
869 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
870 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
871 FIELD(GUEST_DR7, guest_dr7),
872 FIELD(GUEST_RSP, guest_rsp),
873 FIELD(GUEST_RIP, guest_rip),
874 FIELD(GUEST_RFLAGS, guest_rflags),
875 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
876 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
877 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
878 FIELD(HOST_CR0, host_cr0),
879 FIELD(HOST_CR3, host_cr3),
880 FIELD(HOST_CR4, host_cr4),
881 FIELD(HOST_FS_BASE, host_fs_base),
882 FIELD(HOST_GS_BASE, host_gs_base),
883 FIELD(HOST_TR_BASE, host_tr_base),
884 FIELD(HOST_GDTR_BASE, host_gdtr_base),
885 FIELD(HOST_IDTR_BASE, host_idtr_base),
886 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
887 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
888 FIELD(HOST_RSP, host_rsp),
889 FIELD(HOST_RIP, host_rip),
890};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300891
892static inline short vmcs_field_to_offset(unsigned long field)
893{
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100894 BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX);
895
Andrew Honig75f139a2018-01-10 10:12:03 -0800896 if (field >= ARRAY_SIZE(vmcs_field_to_offset_table))
897 return -ENOENT;
898
899 /*
900 * FIXME: Mitigation for CVE-2017-5753. To be replaced with a
901 * generic mechanism.
902 */
903 asm("lfence");
904
905 if (vmcs_field_to_offset_table[field] == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100906 return -ENOENT;
907
Nadav Har'El22bd0352011-05-25 23:05:57 +0300908 return vmcs_field_to_offset_table[field];
909}
910
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300911static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
912{
David Matlack4f2777b2016-07-13 17:16:37 -0700913 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300914}
915
Peter Feiner995f00a2017-06-30 17:26:32 -0700916static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300917static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -0700918static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800919static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300920static void vmx_set_segment(struct kvm_vcpu *vcpu,
921 struct kvm_segment *var, int seg);
922static void vmx_get_segment(struct kvm_vcpu *vcpu,
923 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200924static bool guest_state_valid(struct kvm_vcpu *vcpu);
925static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +0300926static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +0200927static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
928static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
929static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
930 u16 error_code);
Avi Kivity75880a02007-06-20 11:20:04 +0300931
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932static DEFINE_PER_CPU(struct vmcs *, vmxarea);
933static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300934/*
935 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
936 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
937 */
938static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939
Feng Wubf9f6ac2015-09-18 22:29:55 +0800940/*
941 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
942 * can find which vCPU should be waken up.
943 */
944static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
945static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
946
Radim Krčmář23611332016-09-29 22:41:33 +0200947enum {
Radim Krčmář23611332016-09-29 22:41:33 +0200948 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
Radim Krčmář23611332016-09-29 22:41:33 +0200961#define vmx_msr_bitmap_legacy (vmx_bitmap[VMX_MSR_BITMAP_LEGACY])
962#define vmx_msr_bitmap_longmode (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE])
963#define vmx_msr_bitmap_legacy_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC_APICV])
964#define vmx_msr_bitmap_longmode_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV])
965#define vmx_msr_bitmap_legacy_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC])
966#define vmx_msr_bitmap_longmode_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC])
967#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
968#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +0300969
Avi Kivity110312c2010-12-21 12:54:20 +0200970static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200971static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200972
Sheng Yang2384d2b2008-01-17 15:14:33 +0800973static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
974static DEFINE_SPINLOCK(vmx_vpid_lock);
975
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300976static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977 int size;
978 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +0300979 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300981 u32 pin_based_exec_ctrl;
982 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800983 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300984 u32 vmexit_ctrl;
985 u32 vmentry_ctrl;
986} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987
Hannes Ederefff9e52008-11-28 17:02:06 +0100988static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800989 u32 ept;
990 u32 vpid;
991} vmx_capability;
992
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993#define VMX_SEGMENT_FIELD(seg) \
994 [VCPU_SREG_##seg] = { \
995 .selector = GUEST_##seg##_SELECTOR, \
996 .base = GUEST_##seg##_BASE, \
997 .limit = GUEST_##seg##_LIMIT, \
998 .ar_bytes = GUEST_##seg##_AR_BYTES, \
999 }
1000
Mathias Krause772e0312012-08-30 01:30:19 +02001001static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002 unsigned selector;
1003 unsigned base;
1004 unsigned limit;
1005 unsigned ar_bytes;
1006} kvm_vmx_segment_fields[] = {
1007 VMX_SEGMENT_FIELD(CS),
1008 VMX_SEGMENT_FIELD(DS),
1009 VMX_SEGMENT_FIELD(ES),
1010 VMX_SEGMENT_FIELD(FS),
1011 VMX_SEGMENT_FIELD(GS),
1012 VMX_SEGMENT_FIELD(SS),
1013 VMX_SEGMENT_FIELD(TR),
1014 VMX_SEGMENT_FIELD(LDTR),
1015};
1016
Avi Kivity26bb0982009-09-07 11:14:12 +03001017static u64 host_efer;
1018
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001019static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1020
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001021/*
Brian Gerst8c065852010-07-17 09:03:26 -04001022 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001023 * away by decrementing the array size.
1024 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001026#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001027 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001029 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031
Jan Kiszka5bb16012016-02-09 20:14:21 +01001032static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033{
1034 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1035 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001036 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1037}
1038
Jan Kiszka6f054852016-02-09 20:15:18 +01001039static inline bool is_debug(u32 intr_info)
1040{
1041 return is_exception_n(intr_info, DB_VECTOR);
1042}
1043
1044static inline bool is_breakpoint(u32 intr_info)
1045{
1046 return is_exception_n(intr_info, BP_VECTOR);
1047}
1048
Jan Kiszka5bb16012016-02-09 20:14:21 +01001049static inline bool is_page_fault(u32 intr_info)
1050{
1051 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001052}
1053
Gui Jianfeng31299942010-03-15 17:29:09 +08001054static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001055{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001056 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001057}
1058
Gui Jianfeng31299942010-03-15 17:29:09 +08001059static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001060{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001061 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001062}
1063
Gui Jianfeng31299942010-03-15 17:29:09 +08001064static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065{
1066 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1067 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1068}
1069
Gui Jianfeng31299942010-03-15 17:29:09 +08001070static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001071{
1072 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1073 INTR_INFO_VALID_MASK)) ==
1074 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1075}
1076
Gui Jianfeng31299942010-03-15 17:29:09 +08001077static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001078{
Sheng Yang04547152009-04-01 15:52:31 +08001079 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001080}
1081
Gui Jianfeng31299942010-03-15 17:29:09 +08001082static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001083{
Sheng Yang04547152009-04-01 15:52:31 +08001084 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001085}
1086
Paolo Bonzini35754c92015-07-29 12:05:37 +02001087static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001088{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001089 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001090}
1091
Gui Jianfeng31299942010-03-15 17:29:09 +08001092static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001093{
Sheng Yang04547152009-04-01 15:52:31 +08001094 return vmcs_config.cpu_based_exec_ctrl &
1095 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001096}
1097
Avi Kivity774ead32007-12-26 13:57:04 +02001098static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001099{
Sheng Yang04547152009-04-01 15:52:31 +08001100 return vmcs_config.cpu_based_2nd_exec_ctrl &
1101 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1102}
1103
Yang Zhang8d146952013-01-25 10:18:50 +08001104static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1105{
1106 return vmcs_config.cpu_based_2nd_exec_ctrl &
1107 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1108}
1109
Yang Zhang83d4c282013-01-25 10:18:49 +08001110static inline bool cpu_has_vmx_apic_register_virt(void)
1111{
1112 return vmcs_config.cpu_based_2nd_exec_ctrl &
1113 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1114}
1115
Yang Zhangc7c9c562013-01-25 10:18:51 +08001116static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1117{
1118 return vmcs_config.cpu_based_2nd_exec_ctrl &
1119 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1120}
1121
Yunhong Jiang64672c92016-06-13 14:19:59 -07001122/*
1123 * Comment's format: document - errata name - stepping - processor name.
1124 * Refer from
1125 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1126 */
1127static u32 vmx_preemption_cpu_tfms[] = {
1128/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
11290x000206E6,
1130/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1131/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1132/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
11330x00020652,
1134/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
11350x00020655,
1136/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1137/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1138/*
1139 * 320767.pdf - AAP86 - B1 -
1140 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1141 */
11420x000106E5,
1143/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
11440x000106A0,
1145/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
11460x000106A1,
1147/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
11480x000106A4,
1149 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1150 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1151 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
11520x000106A5,
1153};
1154
1155static inline bool cpu_has_broken_vmx_preemption_timer(void)
1156{
1157 u32 eax = cpuid_eax(0x00000001), i;
1158
1159 /* Clear the reserved bits */
1160 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001161 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001162 if (eax == vmx_preemption_cpu_tfms[i])
1163 return true;
1164
1165 return false;
1166}
1167
1168static inline bool cpu_has_vmx_preemption_timer(void)
1169{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001170 return vmcs_config.pin_based_exec_ctrl &
1171 PIN_BASED_VMX_PREEMPTION_TIMER;
1172}
1173
Yang Zhang01e439b2013-04-11 19:25:12 +08001174static inline bool cpu_has_vmx_posted_intr(void)
1175{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001176 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1177 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001178}
1179
1180static inline bool cpu_has_vmx_apicv(void)
1181{
1182 return cpu_has_vmx_apic_register_virt() &&
1183 cpu_has_vmx_virtual_intr_delivery() &&
1184 cpu_has_vmx_posted_intr();
1185}
1186
Sheng Yang04547152009-04-01 15:52:31 +08001187static inline bool cpu_has_vmx_flexpriority(void)
1188{
1189 return cpu_has_vmx_tpr_shadow() &&
1190 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001191}
1192
Marcelo Tosattie7997942009-06-11 12:07:40 -03001193static inline bool cpu_has_vmx_ept_execute_only(void)
1194{
Gui Jianfeng31299942010-03-15 17:29:09 +08001195 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001196}
1197
Marcelo Tosattie7997942009-06-11 12:07:40 -03001198static inline bool cpu_has_vmx_ept_2m_page(void)
1199{
Gui Jianfeng31299942010-03-15 17:29:09 +08001200 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001201}
1202
Sheng Yang878403b2010-01-05 19:02:29 +08001203static inline bool cpu_has_vmx_ept_1g_page(void)
1204{
Gui Jianfeng31299942010-03-15 17:29:09 +08001205 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001206}
1207
Sheng Yang4bc9b982010-06-02 14:05:24 +08001208static inline bool cpu_has_vmx_ept_4levels(void)
1209{
1210 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1211}
1212
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001213static inline bool cpu_has_vmx_ept_mt_wb(void)
1214{
1215 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1216}
1217
Yu Zhang855feb62017-08-24 20:27:55 +08001218static inline bool cpu_has_vmx_ept_5levels(void)
1219{
1220 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1221}
1222
Xudong Hao83c3a332012-05-28 19:33:35 +08001223static inline bool cpu_has_vmx_ept_ad_bits(void)
1224{
1225 return vmx_capability.ept & VMX_EPT_AD_BIT;
1226}
1227
Gui Jianfeng31299942010-03-15 17:29:09 +08001228static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001229{
Gui Jianfeng31299942010-03-15 17:29:09 +08001230 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001231}
1232
Gui Jianfeng31299942010-03-15 17:29:09 +08001233static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001234{
Gui Jianfeng31299942010-03-15 17:29:09 +08001235 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001236}
1237
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001238static inline bool cpu_has_vmx_invvpid_single(void)
1239{
1240 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1241}
1242
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001243static inline bool cpu_has_vmx_invvpid_global(void)
1244{
1245 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1246}
1247
Wanpeng Li08d839c2017-03-23 05:30:08 -07001248static inline bool cpu_has_vmx_invvpid(void)
1249{
1250 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1251}
1252
Gui Jianfeng31299942010-03-15 17:29:09 +08001253static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001254{
Sheng Yang04547152009-04-01 15:52:31 +08001255 return vmcs_config.cpu_based_2nd_exec_ctrl &
1256 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001257}
1258
Gui Jianfeng31299942010-03-15 17:29:09 +08001259static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001260{
1261 return vmcs_config.cpu_based_2nd_exec_ctrl &
1262 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1263}
1264
Gui Jianfeng31299942010-03-15 17:29:09 +08001265static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001266{
1267 return vmcs_config.cpu_based_2nd_exec_ctrl &
1268 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1269}
1270
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001271static inline bool cpu_has_vmx_basic_inout(void)
1272{
1273 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1274}
1275
Paolo Bonzini35754c92015-07-29 12:05:37 +02001276static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001277{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001278 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001279}
1280
Gui Jianfeng31299942010-03-15 17:29:09 +08001281static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001282{
Sheng Yang04547152009-04-01 15:52:31 +08001283 return vmcs_config.cpu_based_2nd_exec_ctrl &
1284 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001285}
1286
Gui Jianfeng31299942010-03-15 17:29:09 +08001287static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001288{
1289 return vmcs_config.cpu_based_2nd_exec_ctrl &
1290 SECONDARY_EXEC_RDTSCP;
1291}
1292
Mao, Junjiead756a12012-07-02 01:18:48 +00001293static inline bool cpu_has_vmx_invpcid(void)
1294{
1295 return vmcs_config.cpu_based_2nd_exec_ctrl &
1296 SECONDARY_EXEC_ENABLE_INVPCID;
1297}
1298
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001299static inline bool cpu_has_virtual_nmis(void)
1300{
1301 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1302}
1303
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001304static inline bool cpu_has_vmx_wbinvd_exit(void)
1305{
1306 return vmcs_config.cpu_based_2nd_exec_ctrl &
1307 SECONDARY_EXEC_WBINVD_EXITING;
1308}
1309
Abel Gordonabc4fc52013-04-18 14:35:25 +03001310static inline bool cpu_has_vmx_shadow_vmcs(void)
1311{
1312 u64 vmx_msr;
1313 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1314 /* check if the cpu supports writing r/o exit information fields */
1315 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1316 return false;
1317
1318 return vmcs_config.cpu_based_2nd_exec_ctrl &
1319 SECONDARY_EXEC_SHADOW_VMCS;
1320}
1321
Kai Huang843e4332015-01-28 10:54:28 +08001322static inline bool cpu_has_vmx_pml(void)
1323{
1324 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1325}
1326
Haozhong Zhang64903d62015-10-20 15:39:09 +08001327static inline bool cpu_has_vmx_tsc_scaling(void)
1328{
1329 return vmcs_config.cpu_based_2nd_exec_ctrl &
1330 SECONDARY_EXEC_TSC_SCALING;
1331}
1332
Bandan Das2a499e42017-08-03 15:54:41 -04001333static inline bool cpu_has_vmx_vmfunc(void)
1334{
1335 return vmcs_config.cpu_based_2nd_exec_ctrl &
1336 SECONDARY_EXEC_ENABLE_VMFUNC;
1337}
1338
Sheng Yang04547152009-04-01 15:52:31 +08001339static inline bool report_flexpriority(void)
1340{
1341 return flexpriority_enabled;
1342}
1343
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001344static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1345{
1346 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.nested_vmx_misc_low);
1347}
1348
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001349static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1350{
1351 return vmcs12->cpu_based_vm_exec_control & bit;
1352}
1353
1354static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1355{
1356 return (vmcs12->cpu_based_vm_exec_control &
1357 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1358 (vmcs12->secondary_vm_exec_control & bit);
1359}
1360
Jan Kiszkaf41245002014-03-07 20:03:13 +01001361static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1362{
1363 return vmcs12->pin_based_vm_exec_control &
1364 PIN_BASED_VMX_PREEMPTION_TIMER;
1365}
1366
Nadav Har'El155a97a2013-08-05 11:07:16 +03001367static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1368{
1369 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1370}
1371
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001372static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1373{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001374 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001375}
1376
Bandan Dasc5f983f2017-05-05 15:25:14 -04001377static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1378{
1379 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1380}
1381
Wincy Vanf2b93282015-02-03 23:56:03 +08001382static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1383{
1384 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1385}
1386
Wanpeng Li5c614b32015-10-13 09:18:36 -07001387static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1388{
1389 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1390}
1391
Wincy Van82f0dd42015-02-03 23:57:18 +08001392static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1393{
1394 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1395}
1396
Wincy Van608406e2015-02-03 23:57:51 +08001397static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1398{
1399 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1400}
1401
Wincy Van705699a2015-02-03 23:58:17 +08001402static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1403{
1404 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1405}
1406
Bandan Das27c42a12017-08-03 15:54:42 -04001407static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1408{
1409 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1410}
1411
Bandan Das41ab9372017-08-03 15:54:43 -04001412static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1413{
1414 return nested_cpu_has_vmfunc(vmcs12) &&
1415 (vmcs12->vm_function_control &
1416 VMX_VMFUNC_EPTP_SWITCHING);
1417}
1418
Jim Mattsonef85b672016-12-12 11:01:37 -08001419static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001420{
1421 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001422 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001423}
1424
Jan Kiszka533558b2014-01-04 18:47:20 +01001425static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1426 u32 exit_intr_info,
1427 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001428static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1429 struct vmcs12 *vmcs12,
1430 u32 reason, unsigned long qualification);
1431
Rusty Russell8b9cf982007-07-30 16:31:43 +10001432static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001433{
1434 int i;
1435
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001436 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001437 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001438 return i;
1439 return -1;
1440}
1441
Sheng Yang2384d2b2008-01-17 15:14:33 +08001442static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1443{
1444 struct {
1445 u64 vpid : 16;
1446 u64 rsvd : 48;
1447 u64 gva;
1448 } operand = { vpid, 0, gva };
1449
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001450 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001451 /* CF==1 or ZF==1 --> rc = -1 */
1452 "; ja 1f ; ud2 ; 1:"
1453 : : "a"(&operand), "c"(ext) : "cc", "memory");
1454}
1455
Sheng Yang14394422008-04-28 12:24:45 +08001456static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1457{
1458 struct {
1459 u64 eptp, gpa;
1460 } operand = {eptp, gpa};
1461
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001462 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001463 /* CF==1 or ZF==1 --> rc = -1 */
1464 "; ja 1f ; ud2 ; 1:\n"
1465 : : "a" (&operand), "c" (ext) : "cc", "memory");
1466}
1467
Avi Kivity26bb0982009-09-07 11:14:12 +03001468static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001469{
1470 int i;
1471
Rusty Russell8b9cf982007-07-30 16:31:43 +10001472 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001473 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001474 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001475 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001476}
1477
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478static void vmcs_clear(struct vmcs *vmcs)
1479{
1480 u64 phys_addr = __pa(vmcs);
1481 u8 error;
1482
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001483 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001484 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485 : "cc", "memory");
1486 if (error)
1487 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1488 vmcs, phys_addr);
1489}
1490
Nadav Har'Eld462b812011-05-24 15:26:10 +03001491static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1492{
1493 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001494 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1495 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001496 loaded_vmcs->cpu = -1;
1497 loaded_vmcs->launched = 0;
1498}
1499
Dongxiao Xu7725b892010-05-11 18:29:38 +08001500static void vmcs_load(struct vmcs *vmcs)
1501{
1502 u64 phys_addr = __pa(vmcs);
1503 u8 error;
1504
1505 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001506 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001507 : "cc", "memory");
1508 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001509 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001510 vmcs, phys_addr);
1511}
1512
Dave Young2965faa2015-09-09 15:38:55 -07001513#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001514/*
1515 * This bitmap is used to indicate whether the vmclear
1516 * operation is enabled on all cpus. All disabled by
1517 * default.
1518 */
1519static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1520
1521static inline void crash_enable_local_vmclear(int cpu)
1522{
1523 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1524}
1525
1526static inline void crash_disable_local_vmclear(int cpu)
1527{
1528 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1529}
1530
1531static inline int crash_local_vmclear_enabled(int cpu)
1532{
1533 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1534}
1535
1536static void crash_vmclear_local_loaded_vmcss(void)
1537{
1538 int cpu = raw_smp_processor_id();
1539 struct loaded_vmcs *v;
1540
1541 if (!crash_local_vmclear_enabled(cpu))
1542 return;
1543
1544 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1545 loaded_vmcss_on_cpu_link)
1546 vmcs_clear(v->vmcs);
1547}
1548#else
1549static inline void crash_enable_local_vmclear(int cpu) { }
1550static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001551#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001552
Nadav Har'Eld462b812011-05-24 15:26:10 +03001553static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001554{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001555 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001556 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557
Nadav Har'Eld462b812011-05-24 15:26:10 +03001558 if (loaded_vmcs->cpu != cpu)
1559 return; /* vcpu migration can race with cpu offline */
1560 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001561 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001562 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001563 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001564
1565 /*
1566 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1567 * is before setting loaded_vmcs->vcpu to -1 which is done in
1568 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1569 * then adds the vmcs into percpu list before it is deleted.
1570 */
1571 smp_wmb();
1572
Nadav Har'Eld462b812011-05-24 15:26:10 +03001573 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001574 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001575}
1576
Nadav Har'Eld462b812011-05-24 15:26:10 +03001577static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001578{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001579 int cpu = loaded_vmcs->cpu;
1580
1581 if (cpu != -1)
1582 smp_call_function_single(cpu,
1583 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001584}
1585
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001586static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001587{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001588 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001589 return;
1590
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001591 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001592 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001593}
1594
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001595static inline void vpid_sync_vcpu_global(void)
1596{
1597 if (cpu_has_vmx_invvpid_global())
1598 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1599}
1600
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001601static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001602{
1603 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001604 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001605 else
1606 vpid_sync_vcpu_global();
1607}
1608
Sheng Yang14394422008-04-28 12:24:45 +08001609static inline void ept_sync_global(void)
1610{
David Hildenbrandf5f51582017-08-24 20:51:30 +02001611 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08001612}
1613
1614static inline void ept_sync_context(u64 eptp)
1615{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02001616 if (cpu_has_vmx_invept_context())
1617 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1618 else
1619 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08001620}
1621
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001622static __always_inline void vmcs_check16(unsigned long field)
1623{
1624 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1625 "16-bit accessor invalid for 64-bit field");
1626 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1627 "16-bit accessor invalid for 64-bit high field");
1628 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1629 "16-bit accessor invalid for 32-bit high field");
1630 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1631 "16-bit accessor invalid for natural width field");
1632}
1633
1634static __always_inline void vmcs_check32(unsigned long field)
1635{
1636 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1637 "32-bit accessor invalid for 16-bit field");
1638 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1639 "32-bit accessor invalid for natural width field");
1640}
1641
1642static __always_inline void vmcs_check64(unsigned long field)
1643{
1644 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1645 "64-bit accessor invalid for 16-bit field");
1646 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1647 "64-bit accessor invalid for 64-bit high field");
1648 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1649 "64-bit accessor invalid for 32-bit field");
1650 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1651 "64-bit accessor invalid for natural width field");
1652}
1653
1654static __always_inline void vmcs_checkl(unsigned long field)
1655{
1656 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1657 "Natural width accessor invalid for 16-bit field");
1658 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1659 "Natural width accessor invalid for 64-bit field");
1660 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1661 "Natural width accessor invalid for 64-bit high field");
1662 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1663 "Natural width accessor invalid for 32-bit field");
1664}
1665
1666static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667{
Avi Kivity5e520e62011-05-15 10:13:12 -04001668 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669
Avi Kivity5e520e62011-05-15 10:13:12 -04001670 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1671 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001672 return value;
1673}
1674
Avi Kivity96304212011-05-15 10:13:13 -04001675static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001676{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001677 vmcs_check16(field);
1678 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679}
1680
Avi Kivity96304212011-05-15 10:13:13 -04001681static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001682{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001683 vmcs_check32(field);
1684 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001685}
1686
Avi Kivity96304212011-05-15 10:13:13 -04001687static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001689 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001690#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001691 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001693 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001694#endif
1695}
1696
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001697static __always_inline unsigned long vmcs_readl(unsigned long field)
1698{
1699 vmcs_checkl(field);
1700 return __vmcs_readl(field);
1701}
1702
Avi Kivitye52de1b2007-01-05 16:36:56 -08001703static noinline void vmwrite_error(unsigned long field, unsigned long value)
1704{
1705 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1706 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1707 dump_stack();
1708}
1709
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001710static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711{
1712 u8 error;
1713
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001714 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001715 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001716 if (unlikely(error))
1717 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001718}
1719
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001720static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001721{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001722 vmcs_check16(field);
1723 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001724}
1725
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001726static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001727{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001728 vmcs_check32(field);
1729 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001730}
1731
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001732static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001733{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001734 vmcs_check64(field);
1735 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001736#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001737 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001738 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001739#endif
1740}
1741
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001742static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001743{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001744 vmcs_checkl(field);
1745 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001746}
1747
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001748static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001749{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001750 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1751 "vmcs_clear_bits does not support 64-bit fields");
1752 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1753}
1754
1755static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1756{
1757 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1758 "vmcs_set_bits does not support 64-bit fields");
1759 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001760}
1761
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001762static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1763{
1764 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1765}
1766
Gleb Natapov2961e8762013-11-25 15:37:13 +02001767static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1768{
1769 vmcs_write32(VM_ENTRY_CONTROLS, val);
1770 vmx->vm_entry_controls_shadow = val;
1771}
1772
1773static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1774{
1775 if (vmx->vm_entry_controls_shadow != val)
1776 vm_entry_controls_init(vmx, val);
1777}
1778
1779static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1780{
1781 return vmx->vm_entry_controls_shadow;
1782}
1783
1784
1785static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1786{
1787 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1788}
1789
1790static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1791{
1792 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1793}
1794
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001795static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1796{
1797 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1798}
1799
Gleb Natapov2961e8762013-11-25 15:37:13 +02001800static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1801{
1802 vmcs_write32(VM_EXIT_CONTROLS, val);
1803 vmx->vm_exit_controls_shadow = val;
1804}
1805
1806static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1807{
1808 if (vmx->vm_exit_controls_shadow != val)
1809 vm_exit_controls_init(vmx, val);
1810}
1811
1812static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1813{
1814 return vmx->vm_exit_controls_shadow;
1815}
1816
1817
1818static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1819{
1820 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1821}
1822
1823static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1824{
1825 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1826}
1827
Avi Kivity2fb92db2011-04-27 19:42:18 +03001828static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1829{
1830 vmx->segment_cache.bitmask = 0;
1831}
1832
1833static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1834 unsigned field)
1835{
1836 bool ret;
1837 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1838
1839 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1840 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1841 vmx->segment_cache.bitmask = 0;
1842 }
1843 ret = vmx->segment_cache.bitmask & mask;
1844 vmx->segment_cache.bitmask |= mask;
1845 return ret;
1846}
1847
1848static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1849{
1850 u16 *p = &vmx->segment_cache.seg[seg].selector;
1851
1852 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1853 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1854 return *p;
1855}
1856
1857static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1858{
1859 ulong *p = &vmx->segment_cache.seg[seg].base;
1860
1861 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1862 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1863 return *p;
1864}
1865
1866static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1867{
1868 u32 *p = &vmx->segment_cache.seg[seg].limit;
1869
1870 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1871 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1872 return *p;
1873}
1874
1875static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1876{
1877 u32 *p = &vmx->segment_cache.seg[seg].ar;
1878
1879 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1880 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1881 return *p;
1882}
1883
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001884static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1885{
1886 u32 eb;
1887
Liran Alonac9b3052017-11-06 16:15:10 +02001888 eb = (1u << PF_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08001889 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001890 if ((vcpu->guest_debug &
1891 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1892 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1893 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001894 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001895 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001896 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001897 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001898
1899 /* When we are running a nested L2 guest and L1 specified for it a
1900 * certain exception bitmap, we must trap the same exceptions and pass
1901 * them to L1. When running L2, we will only handle the exceptions
1902 * specified above if L1 did not want them.
1903 */
1904 if (is_guest_mode(vcpu))
1905 eb |= get_vmcs12(vcpu)->exception_bitmap;
Liran Alonac9b3052017-11-06 16:15:10 +02001906 else
1907 eb |= 1u << UD_VECTOR;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001908
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001909 vmcs_write32(EXCEPTION_BITMAP, eb);
1910}
1911
Gleb Natapov2961e8762013-11-25 15:37:13 +02001912static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1913 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001914{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001915 vm_entry_controls_clearbit(vmx, entry);
1916 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001917}
1918
Avi Kivity61d2ef22010-04-28 16:40:38 +03001919static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1920{
1921 unsigned i;
1922 struct msr_autoload *m = &vmx->msr_autoload;
1923
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001924 switch (msr) {
1925 case MSR_EFER:
1926 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001927 clear_atomic_switch_msr_special(vmx,
1928 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001929 VM_EXIT_LOAD_IA32_EFER);
1930 return;
1931 }
1932 break;
1933 case MSR_CORE_PERF_GLOBAL_CTRL:
1934 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001935 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001936 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1937 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1938 return;
1939 }
1940 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001941 }
1942
Avi Kivity61d2ef22010-04-28 16:40:38 +03001943 for (i = 0; i < m->nr; ++i)
1944 if (m->guest[i].index == msr)
1945 break;
1946
1947 if (i == m->nr)
1948 return;
1949 --m->nr;
1950 m->guest[i] = m->guest[m->nr];
1951 m->host[i] = m->host[m->nr];
1952 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1953 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1954}
1955
Gleb Natapov2961e8762013-11-25 15:37:13 +02001956static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1957 unsigned long entry, unsigned long exit,
1958 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1959 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001960{
1961 vmcs_write64(guest_val_vmcs, guest_val);
1962 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001963 vm_entry_controls_setbit(vmx, entry);
1964 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001965}
1966
Avi Kivity61d2ef22010-04-28 16:40:38 +03001967static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1968 u64 guest_val, u64 host_val)
1969{
1970 unsigned i;
1971 struct msr_autoload *m = &vmx->msr_autoload;
1972
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001973 switch (msr) {
1974 case MSR_EFER:
1975 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001976 add_atomic_switch_msr_special(vmx,
1977 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001978 VM_EXIT_LOAD_IA32_EFER,
1979 GUEST_IA32_EFER,
1980 HOST_IA32_EFER,
1981 guest_val, host_val);
1982 return;
1983 }
1984 break;
1985 case MSR_CORE_PERF_GLOBAL_CTRL:
1986 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001987 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001988 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1989 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1990 GUEST_IA32_PERF_GLOBAL_CTRL,
1991 HOST_IA32_PERF_GLOBAL_CTRL,
1992 guest_val, host_val);
1993 return;
1994 }
1995 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01001996 case MSR_IA32_PEBS_ENABLE:
1997 /* PEBS needs a quiescent period after being disabled (to write
1998 * a record). Disabling PEBS through VMX MSR swapping doesn't
1999 * provide that period, so a CPU could write host's record into
2000 * guest's memory.
2001 */
2002 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002003 }
2004
Avi Kivity61d2ef22010-04-28 16:40:38 +03002005 for (i = 0; i < m->nr; ++i)
2006 if (m->guest[i].index == msr)
2007 break;
2008
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002009 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002010 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002011 "Can't add msr %x\n", msr);
2012 return;
2013 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002014 ++m->nr;
2015 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2016 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2017 }
2018
2019 m->guest[i].index = msr;
2020 m->guest[i].value = guest_val;
2021 m->host[i].index = msr;
2022 m->host[i].value = host_val;
2023}
2024
Avi Kivity92c0d902009-10-29 11:00:16 +02002025static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002026{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002027 u64 guest_efer = vmx->vcpu.arch.efer;
2028 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002029
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002030 if (!enable_ept) {
2031 /*
2032 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2033 * host CPUID is more efficient than testing guest CPUID
2034 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2035 */
2036 if (boot_cpu_has(X86_FEATURE_SMEP))
2037 guest_efer |= EFER_NX;
2038 else if (!(guest_efer & EFER_NX))
2039 ignore_bits |= EFER_NX;
2040 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002041
Avi Kivity51c6cf62007-08-29 03:48:05 +03002042 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002043 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002044 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002045 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002046#ifdef CONFIG_X86_64
2047 ignore_bits |= EFER_LMA | EFER_LME;
2048 /* SCE is meaningful only in long mode on Intel */
2049 if (guest_efer & EFER_LMA)
2050 ignore_bits &= ~(u64)EFER_SCE;
2051#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002052
2053 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002054
2055 /*
2056 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2057 * On CPUs that support "load IA32_EFER", always switch EFER
2058 * atomically, since it's faster than switching it manually.
2059 */
2060 if (cpu_has_load_ia32_efer ||
2061 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002062 if (!(guest_efer & EFER_LMA))
2063 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002064 if (guest_efer != host_efer)
2065 add_atomic_switch_msr(vmx, MSR_EFER,
2066 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002067 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002068 } else {
2069 guest_efer &= ~ignore_bits;
2070 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002071
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002072 vmx->guest_msrs[efer_offset].data = guest_efer;
2073 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2074
2075 return true;
2076 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002077}
2078
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002079#ifdef CONFIG_X86_32
2080/*
2081 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2082 * VMCS rather than the segment table. KVM uses this helper to figure
2083 * out the current bases to poke them into the VMCS before entry.
2084 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002085static unsigned long segment_base(u16 selector)
2086{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002087 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002088 unsigned long v;
2089
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002090 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002091 return 0;
2092
Thomas Garnier45fc8752017-03-14 10:05:08 -07002093 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002094
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002095 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002096 u16 ldt_selector = kvm_read_ldt();
2097
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002098 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002099 return 0;
2100
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002101 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002102 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002103 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002104 return v;
2105}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002106#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002107
Avi Kivity04d2cc72007-09-10 18:10:54 +03002108static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002109{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002110 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002111 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002112
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002113 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002114 return;
2115
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002116 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002117 /*
2118 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2119 * allow segment selectors with cpl > 0 or ti == 1.
2120 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002121 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002122 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002123 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002124 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002125 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002126 vmx->host_state.fs_reload_needed = 0;
2127 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002128 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002129 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002130 }
Avi Kivity9581d442010-10-19 16:46:55 +02002131 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002132 if (!(vmx->host_state.gs_sel & 7))
2133 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002134 else {
2135 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002136 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002137 }
2138
2139#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002140 savesegment(ds, vmx->host_state.ds_sel);
2141 savesegment(es, vmx->host_state.es_sel);
2142#endif
2143
2144#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002145 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2146 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2147#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002148 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2149 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002150#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002151
2152#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002153 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2154 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002155 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002156#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002157 if (boot_cpu_has(X86_FEATURE_MPX))
2158 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002159 for (i = 0; i < vmx->save_nmsrs; ++i)
2160 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002161 vmx->guest_msrs[i].data,
2162 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002163}
2164
Avi Kivitya9b21b62008-06-24 11:48:49 +03002165static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002166{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002167 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002168 return;
2169
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002170 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002171 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002172#ifdef CONFIG_X86_64
2173 if (is_long_mode(&vmx->vcpu))
2174 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2175#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002176 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002177 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002178#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002179 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002180#else
2181 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002182#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002183 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002184 if (vmx->host_state.fs_reload_needed)
2185 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002186#ifdef CONFIG_X86_64
2187 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2188 loadsegment(ds, vmx->host_state.ds_sel);
2189 loadsegment(es, vmx->host_state.es_sel);
2190 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002191#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002192 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002193#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002194 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002195#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002196 if (vmx->host_state.msr_host_bndcfgs)
2197 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002198 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002199}
2200
Avi Kivitya9b21b62008-06-24 11:48:49 +03002201static void vmx_load_host_state(struct vcpu_vmx *vmx)
2202{
2203 preempt_disable();
2204 __vmx_load_host_state(vmx);
2205 preempt_enable();
2206}
2207
Feng Wu28b835d2015-09-18 22:29:54 +08002208static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2209{
2210 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2211 struct pi_desc old, new;
2212 unsigned int dest;
2213
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002214 /*
2215 * In case of hot-plug or hot-unplug, we may have to undo
2216 * vmx_vcpu_pi_put even if there is no assigned device. And we
2217 * always keep PI.NDST up to date for simplicity: it makes the
2218 * code easier, and CPU migration is not a fast path.
2219 */
2220 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002221 return;
2222
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002223 /*
2224 * First handle the simple case where no cmpxchg is necessary; just
2225 * allow posting non-urgent interrupts.
2226 *
2227 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2228 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2229 * expects the VCPU to be on the blocked_vcpu_list that matches
2230 * PI.NDST.
2231 */
2232 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2233 vcpu->cpu == cpu) {
2234 pi_clear_sn(pi_desc);
2235 return;
2236 }
2237
2238 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002239 do {
2240 old.control = new.control = pi_desc->control;
2241
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002242 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002243
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002244 if (x2apic_enabled())
2245 new.ndst = dest;
2246 else
2247 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002248
Feng Wu28b835d2015-09-18 22:29:54 +08002249 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002250 } while (cmpxchg64(&pi_desc->control, old.control,
2251 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002252}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002253
Peter Feinerc95ba922016-08-17 09:36:47 -07002254static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2255{
2256 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2257 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2258}
2259
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260/*
2261 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2262 * vcpu mutex is already taken.
2263 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002264static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002266 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002267 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002269 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002270 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002271 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002272 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002273
2274 /*
2275 * Read loaded_vmcs->cpu should be before fetching
2276 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2277 * See the comments in __loaded_vmcs_clear().
2278 */
2279 smp_rmb();
2280
Nadav Har'Eld462b812011-05-24 15:26:10 +03002281 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2282 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002283 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002284 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002285 }
2286
2287 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2288 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2289 vmcs_load(vmx->loaded_vmcs->vmcs);
2290 }
2291
2292 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002293 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002294 unsigned long sysenter_esp;
2295
2296 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002297
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298 /*
2299 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002300 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002301 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002302 vmcs_writel(HOST_TR_BASE,
2303 (unsigned long)this_cpu_ptr(&cpu_tss));
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002304 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002306 /*
2307 * VM exits change the host TR limit to 0x67 after a VM
2308 * exit. This is okay, since 0x67 covers everything except
2309 * the IO bitmap and have have code to handle the IO bitmap
2310 * being lost after a VM exit.
2311 */
2312 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2313
Avi Kivity6aa8b732006-12-10 02:21:36 -08002314 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2315 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002316
Nadav Har'Eld462b812011-05-24 15:26:10 +03002317 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318 }
Feng Wu28b835d2015-09-18 22:29:54 +08002319
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002320 /* Setup TSC multiplier */
2321 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002322 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2323 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002324
Feng Wu28b835d2015-09-18 22:29:54 +08002325 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002326 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002327 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002328}
2329
2330static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2331{
2332 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2333
2334 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002335 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2336 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002337 return;
2338
2339 /* Set SN when the vCPU is preempted */
2340 if (vcpu->preempted)
2341 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002342}
2343
2344static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2345{
Feng Wu28b835d2015-09-18 22:29:54 +08002346 vmx_vcpu_pi_put(vcpu);
2347
Avi Kivitya9b21b62008-06-24 11:48:49 +03002348 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349}
2350
Wanpeng Lif244dee2017-07-20 01:11:54 -07002351static bool emulation_required(struct kvm_vcpu *vcpu)
2352{
2353 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2354}
2355
Avi Kivityedcafe32009-12-30 18:07:40 +02002356static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2357
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002358/*
2359 * Return the cr0 value that a nested guest would read. This is a combination
2360 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2361 * its hypervisor (cr0_read_shadow).
2362 */
2363static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2364{
2365 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2366 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2367}
2368static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2369{
2370 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2371 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2372}
2373
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2375{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002376 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002377
Avi Kivity6de12732011-03-07 12:51:22 +02002378 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2379 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2380 rflags = vmcs_readl(GUEST_RFLAGS);
2381 if (to_vmx(vcpu)->rmode.vm86_active) {
2382 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2383 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2384 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2385 }
2386 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002387 }
Avi Kivity6de12732011-03-07 12:51:22 +02002388 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389}
2390
2391static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2392{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002393 unsigned long old_rflags = vmx_get_rflags(vcpu);
2394
Avi Kivity6de12732011-03-07 12:51:22 +02002395 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2396 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002397 if (to_vmx(vcpu)->rmode.vm86_active) {
2398 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002399 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002400 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002401 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002402
2403 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2404 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405}
2406
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002407static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002408{
2409 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2410 int ret = 0;
2411
2412 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002413 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002414 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002415 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002416
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002417 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002418}
2419
2420static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2421{
2422 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2423 u32 interruptibility = interruptibility_old;
2424
2425 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2426
Jan Kiszka48005f62010-02-19 19:38:07 +01002427 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002428 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002429 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002430 interruptibility |= GUEST_INTR_STATE_STI;
2431
2432 if ((interruptibility != interruptibility_old))
2433 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2434}
2435
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2437{
2438 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002439
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002440 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002441 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002442 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002443
Glauber Costa2809f5d2009-05-12 16:21:05 -04002444 /* skipping an emulated instruction also counts */
2445 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002446}
2447
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002448static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2449 unsigned long exit_qual)
2450{
2451 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2452 unsigned int nr = vcpu->arch.exception.nr;
2453 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2454
2455 if (vcpu->arch.exception.has_error_code) {
2456 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2457 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2458 }
2459
2460 if (kvm_exception_is_soft(nr))
2461 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2462 else
2463 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2464
2465 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2466 vmx_get_nmi_mask(vcpu))
2467 intr_info |= INTR_INFO_UNBLOCK_NMI;
2468
2469 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2470}
2471
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002472/*
2473 * KVM wants to inject page-faults which it got to the guest. This function
2474 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002475 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002476static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002477{
2478 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002479 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002480
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002481 if (nr == PF_VECTOR) {
2482 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002483 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002484 return 1;
2485 }
2486 /*
2487 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2488 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2489 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2490 * can be written only when inject_pending_event runs. This should be
2491 * conditional on a new capability---if the capability is disabled,
2492 * kvm_multiple_exception would write the ancillary information to
2493 * CR2 or DR6, for backwards ABI-compatibility.
2494 */
2495 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2496 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002497 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002498 return 1;
2499 }
2500 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002501 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002502 if (nr == DB_VECTOR)
2503 *exit_qual = vcpu->arch.dr6;
2504 else
2505 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002506 return 1;
2507 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002508 }
2509
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002510 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002511}
2512
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002513static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02002514{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002515 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002516 unsigned nr = vcpu->arch.exception.nr;
2517 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002518 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002519 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002520
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002521 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002522 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002523 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2524 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002525
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002526 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002527 int inc_eip = 0;
2528 if (kvm_exception_is_soft(nr))
2529 inc_eip = vcpu->arch.event_exit_inst_len;
2530 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002531 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002532 return;
2533 }
2534
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002535 if (kvm_exception_is_soft(nr)) {
2536 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2537 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002538 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2539 } else
2540 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2541
2542 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002543}
2544
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002545static bool vmx_rdtscp_supported(void)
2546{
2547 return cpu_has_vmx_rdtscp();
2548}
2549
Mao, Junjiead756a12012-07-02 01:18:48 +00002550static bool vmx_invpcid_supported(void)
2551{
2552 return cpu_has_vmx_invpcid() && enable_ept;
2553}
2554
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555/*
Eddie Donga75beee2007-05-17 18:55:15 +03002556 * Swap MSR entry in host/guest MSR entry array.
2557 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002558static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002559{
Avi Kivity26bb0982009-09-07 11:14:12 +03002560 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002561
2562 tmp = vmx->guest_msrs[to];
2563 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2564 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002565}
2566
Yang Zhang8d146952013-01-25 10:18:50 +08002567static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2568{
2569 unsigned long *msr_bitmap;
2570
Wincy Van670125b2015-03-04 14:31:56 +08002571 if (is_guest_mode(vcpu))
Radim Krčmářd048c092016-08-08 20:16:22 +02002572 msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap;
Roman Kagan3ce424e2016-05-18 17:48:20 +03002573 else if (cpu_has_secondary_exec_ctrls() &&
2574 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
2575 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002576 if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) {
2577 if (is_long_mode(vcpu))
Wanpeng Lic63e4562016-09-23 19:17:16 +08002578 msr_bitmap = vmx_msr_bitmap_longmode_x2apic_apicv;
2579 else
2580 msr_bitmap = vmx_msr_bitmap_legacy_x2apic_apicv;
2581 } else {
2582 if (is_long_mode(vcpu))
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002583 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2584 else
2585 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002586 }
Yang Zhang8d146952013-01-25 10:18:50 +08002587 } else {
2588 if (is_long_mode(vcpu))
2589 msr_bitmap = vmx_msr_bitmap_longmode;
2590 else
2591 msr_bitmap = vmx_msr_bitmap_legacy;
2592 }
2593
2594 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2595}
2596
Eddie Donga75beee2007-05-17 18:55:15 +03002597/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002598 * Set up the vmcs to automatically save and restore system
2599 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2600 * mode, as fiddling with msrs is very expensive.
2601 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002602static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002603{
Avi Kivity26bb0982009-09-07 11:14:12 +03002604 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002605
Eddie Donga75beee2007-05-17 18:55:15 +03002606 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002607#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002608 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002609 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002610 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002611 move_msr_up(vmx, index, save_nmsrs++);
2612 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002613 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002614 move_msr_up(vmx, index, save_nmsrs++);
2615 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002616 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002617 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002618 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002619 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002620 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002621 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002622 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002623 * if efer.sce is enabled.
2624 */
Brian Gerst8c065852010-07-17 09:03:26 -04002625 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002626 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002627 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002628 }
Eddie Donga75beee2007-05-17 18:55:15 +03002629#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002630 index = __find_msr_index(vmx, MSR_EFER);
2631 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002632 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002633
Avi Kivity26bb0982009-09-07 11:14:12 +03002634 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002635
Yang Zhang8d146952013-01-25 10:18:50 +08002636 if (cpu_has_vmx_msr_bitmap())
2637 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002638}
2639
2640/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002642 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2643 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002645static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002646{
2647 u64 host_tsc, tsc_offset;
2648
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002649 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002651 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652}
2653
2654/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002655 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002656 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002657static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002658{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002659 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002660 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002661 * We're here if L1 chose not to trap WRMSR to TSC. According
2662 * to the spec, this should set L1's TSC; The offset that L1
2663 * set for L2 remains unchanged, and still needs to be added
2664 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002665 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002666 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002667 /* recalculate vmcs02.TSC_OFFSET: */
2668 vmcs12 = get_vmcs12(vcpu);
2669 vmcs_write64(TSC_OFFSET, offset +
2670 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2671 vmcs12->tsc_offset : 0));
2672 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002673 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2674 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002675 vmcs_write64(TSC_OFFSET, offset);
2676 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677}
2678
Nadav Har'El801d3422011-05-25 23:02:23 +03002679/*
2680 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2681 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2682 * all guests if the "nested" module option is off, and can also be disabled
2683 * for a single guest by disabling its VMX cpuid bit.
2684 */
2685static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2686{
Radim Krčmářd6321d42017-08-05 00:12:49 +02002687 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03002688}
2689
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002691 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2692 * returned for the various VMX controls MSRs when nested VMX is enabled.
2693 * The same values should also be used to verify that vmcs12 control fields are
2694 * valid during nested entry from L1 to L2.
2695 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2696 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2697 * bit in the high half is on if the corresponding bit in the control field
2698 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002699 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002700static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002701{
2702 /*
2703 * Note that as a general rule, the high half of the MSRs (bits in
2704 * the control fields which may be 1) should be initialized by the
2705 * intersection of the underlying hardware's MSR (i.e., features which
2706 * can be supported) and the list of features we want to expose -
2707 * because they are known to be properly supported in our code.
2708 * Also, usually, the low half of the MSRs (bits which must be 1) can
2709 * be set to 0, meaning that L1 may turn off any of these bits. The
2710 * reason is that if one of these bits is necessary, it will appear
2711 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2712 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02002713 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002714 * These rules have exceptions below.
2715 */
2716
2717 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002718 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002719 vmx->nested.nested_vmx_pinbased_ctls_low,
2720 vmx->nested.nested_vmx_pinbased_ctls_high);
2721 vmx->nested.nested_vmx_pinbased_ctls_low |=
2722 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2723 vmx->nested.nested_vmx_pinbased_ctls_high &=
2724 PIN_BASED_EXT_INTR_MASK |
2725 PIN_BASED_NMI_EXITING |
2726 PIN_BASED_VIRTUAL_NMIS;
2727 vmx->nested.nested_vmx_pinbased_ctls_high |=
2728 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002729 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002730 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002731 vmx->nested.nested_vmx_pinbased_ctls_high |=
2732 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002733
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002734 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002735 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002736 vmx->nested.nested_vmx_exit_ctls_low,
2737 vmx->nested.nested_vmx_exit_ctls_high);
2738 vmx->nested.nested_vmx_exit_ctls_low =
2739 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002740
Wincy Vanb9c237b2015-02-03 23:56:30 +08002741 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002742#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002743 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002744#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01002745 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002746 vmx->nested.nested_vmx_exit_ctls_high |=
2747 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002748 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002749 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2750
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002751 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002752 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002753
Jan Kiszka2996fca2014-06-16 13:59:43 +02002754 /* We support free control of debug control saving. */
David Matlack0115f9c2016-11-29 18:14:06 -08002755 vmx->nested.nested_vmx_exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002756
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002757 /* entry controls */
2758 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002759 vmx->nested.nested_vmx_entry_ctls_low,
2760 vmx->nested.nested_vmx_entry_ctls_high);
2761 vmx->nested.nested_vmx_entry_ctls_low =
2762 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2763 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002764#ifdef CONFIG_X86_64
2765 VM_ENTRY_IA32E_MODE |
2766#endif
2767 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002768 vmx->nested.nested_vmx_entry_ctls_high |=
2769 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002770 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002771 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002772
Jan Kiszka2996fca2014-06-16 13:59:43 +02002773 /* We support free control of debug control loading. */
David Matlack0115f9c2016-11-29 18:14:06 -08002774 vmx->nested.nested_vmx_entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002775
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002776 /* cpu-based controls */
2777 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002778 vmx->nested.nested_vmx_procbased_ctls_low,
2779 vmx->nested.nested_vmx_procbased_ctls_high);
2780 vmx->nested.nested_vmx_procbased_ctls_low =
2781 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2782 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002783 CPU_BASED_VIRTUAL_INTR_PENDING |
2784 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002785 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2786 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2787 CPU_BASED_CR3_STORE_EXITING |
2788#ifdef CONFIG_X86_64
2789 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2790#endif
2791 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002792 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2793 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2794 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2795 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002796 /*
2797 * We can allow some features even when not supported by the
2798 * hardware. For example, L1 can specify an MSR bitmap - and we
2799 * can use it to avoid exits to L1 - even when L0 runs L2
2800 * without MSR bitmaps.
2801 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002802 vmx->nested.nested_vmx_procbased_ctls_high |=
2803 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002804 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002805
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002806 /* We support free control of CR3 access interception. */
David Matlack0115f9c2016-11-29 18:14:06 -08002807 vmx->nested.nested_vmx_procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002808 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2809
Paolo Bonzini80154d72017-08-24 13:55:35 +02002810 /*
2811 * secondary cpu-based controls. Do not include those that
2812 * depend on CPUID bits, they are added later by vmx_cpuid_update.
2813 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002814 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002815 vmx->nested.nested_vmx_secondary_ctls_low,
2816 vmx->nested.nested_vmx_secondary_ctls_high);
2817 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2818 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002819 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02002820 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08002821 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08002822 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002823 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02002824 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002825
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002826 if (enable_ept) {
2827 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002828 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002829 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002830 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002831 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002832 if (cpu_has_vmx_ept_execute_only())
2833 vmx->nested.nested_vmx_ept_caps |=
2834 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002835 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04002836 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002837 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
2838 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04002839 if (enable_ept_ad_bits) {
2840 vmx->nested.nested_vmx_secondary_ctls_high |=
2841 SECONDARY_EXEC_ENABLE_PML;
Dan Carpenter7461fbc2017-05-18 10:41:15 +03002842 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04002843 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02002844 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002845
Bandan Das27c42a12017-08-03 15:54:42 -04002846 if (cpu_has_vmx_vmfunc()) {
2847 vmx->nested.nested_vmx_secondary_ctls_high |=
2848 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04002849 /*
2850 * Advertise EPTP switching unconditionally
2851 * since we emulate it
2852 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08002853 if (enable_ept)
2854 vmx->nested.nested_vmx_vmfunc_controls =
2855 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04002856 }
2857
Paolo Bonzinief697a72016-03-18 16:58:38 +01002858 /*
2859 * Old versions of KVM use the single-context version without
2860 * checking for support, so declare that it is supported even
2861 * though it is treated as global context. The alternative is
2862 * not failing the single-context invvpid, and it is worse.
2863 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07002864 if (enable_vpid) {
2865 vmx->nested.nested_vmx_secondary_ctls_high |=
2866 SECONDARY_EXEC_ENABLE_VPID;
Wanpeng Li089d7b62015-10-13 09:18:37 -07002867 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03002868 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02002869 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002870
Radim Krčmář0790ec12015-03-17 14:02:32 +01002871 if (enable_unrestricted_guest)
2872 vmx->nested.nested_vmx_secondary_ctls_high |=
2873 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2874
Jan Kiszkac18911a2013-03-13 16:06:41 +01002875 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002876 rdmsr(MSR_IA32_VMX_MISC,
2877 vmx->nested.nested_vmx_misc_low,
2878 vmx->nested.nested_vmx_misc_high);
2879 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2880 vmx->nested.nested_vmx_misc_low |=
2881 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002882 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002883 vmx->nested.nested_vmx_misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002884
2885 /*
2886 * This MSR reports some information about VMX support. We
2887 * should return information about the VMX we emulate for the
2888 * guest, and the VMCS structure we give it - not about the
2889 * VMX support of the underlying hardware.
2890 */
2891 vmx->nested.nested_vmx_basic =
2892 VMCS12_REVISION |
2893 VMX_BASIC_TRUE_CTLS |
2894 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2895 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2896
2897 if (cpu_has_vmx_basic_inout())
2898 vmx->nested.nested_vmx_basic |= VMX_BASIC_INOUT;
2899
2900 /*
David Matlack8322ebb2016-11-29 18:14:09 -08002901 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08002902 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2903 * We picked the standard core2 setting.
2904 */
2905#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2906#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2907 vmx->nested.nested_vmx_cr0_fixed0 = VMXON_CR0_ALWAYSON;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002908 vmx->nested.nested_vmx_cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08002909
2910 /* These MSRs specify bits which the guest must keep fixed off. */
2911 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, vmx->nested.nested_vmx_cr0_fixed1);
2912 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, vmx->nested.nested_vmx_cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08002913
2914 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
2915 vmx->nested.nested_vmx_vmcs_enum = 0x2e;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002916}
2917
David Matlack38991522016-11-29 18:14:08 -08002918/*
2919 * if fixed0[i] == 1: val[i] must be 1
2920 * if fixed1[i] == 0: val[i] must be 0
2921 */
2922static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
2923{
2924 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002925}
2926
2927static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2928{
David Matlack38991522016-11-29 18:14:08 -08002929 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002930}
2931
2932static inline u64 vmx_control_msr(u32 low, u32 high)
2933{
2934 return low | ((u64)high << 32);
2935}
2936
David Matlack62cc6b9d2016-11-29 18:14:07 -08002937static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
2938{
2939 superset &= mask;
2940 subset &= mask;
2941
2942 return (superset | subset) == superset;
2943}
2944
2945static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
2946{
2947 const u64 feature_and_reserved =
2948 /* feature (except bit 48; see below) */
2949 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
2950 /* reserved */
2951 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
2952 u64 vmx_basic = vmx->nested.nested_vmx_basic;
2953
2954 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
2955 return -EINVAL;
2956
2957 /*
2958 * KVM does not emulate a version of VMX that constrains physical
2959 * addresses of VMX structures (e.g. VMCS) to 32-bits.
2960 */
2961 if (data & BIT_ULL(48))
2962 return -EINVAL;
2963
2964 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
2965 vmx_basic_vmcs_revision_id(data))
2966 return -EINVAL;
2967
2968 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
2969 return -EINVAL;
2970
2971 vmx->nested.nested_vmx_basic = data;
2972 return 0;
2973}
2974
2975static int
2976vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
2977{
2978 u64 supported;
2979 u32 *lowp, *highp;
2980
2981 switch (msr_index) {
2982 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2983 lowp = &vmx->nested.nested_vmx_pinbased_ctls_low;
2984 highp = &vmx->nested.nested_vmx_pinbased_ctls_high;
2985 break;
2986 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2987 lowp = &vmx->nested.nested_vmx_procbased_ctls_low;
2988 highp = &vmx->nested.nested_vmx_procbased_ctls_high;
2989 break;
2990 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2991 lowp = &vmx->nested.nested_vmx_exit_ctls_low;
2992 highp = &vmx->nested.nested_vmx_exit_ctls_high;
2993 break;
2994 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2995 lowp = &vmx->nested.nested_vmx_entry_ctls_low;
2996 highp = &vmx->nested.nested_vmx_entry_ctls_high;
2997 break;
2998 case MSR_IA32_VMX_PROCBASED_CTLS2:
2999 lowp = &vmx->nested.nested_vmx_secondary_ctls_low;
3000 highp = &vmx->nested.nested_vmx_secondary_ctls_high;
3001 break;
3002 default:
3003 BUG();
3004 }
3005
3006 supported = vmx_control_msr(*lowp, *highp);
3007
3008 /* Check must-be-1 bits are still 1. */
3009 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3010 return -EINVAL;
3011
3012 /* Check must-be-0 bits are still 0. */
3013 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3014 return -EINVAL;
3015
3016 *lowp = data;
3017 *highp = data >> 32;
3018 return 0;
3019}
3020
3021static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3022{
3023 const u64 feature_and_reserved_bits =
3024 /* feature */
3025 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3026 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3027 /* reserved */
3028 GENMASK_ULL(13, 9) | BIT_ULL(31);
3029 u64 vmx_misc;
3030
3031 vmx_misc = vmx_control_msr(vmx->nested.nested_vmx_misc_low,
3032 vmx->nested.nested_vmx_misc_high);
3033
3034 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3035 return -EINVAL;
3036
3037 if ((vmx->nested.nested_vmx_pinbased_ctls_high &
3038 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3039 vmx_misc_preemption_timer_rate(data) !=
3040 vmx_misc_preemption_timer_rate(vmx_misc))
3041 return -EINVAL;
3042
3043 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3044 return -EINVAL;
3045
3046 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3047 return -EINVAL;
3048
3049 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3050 return -EINVAL;
3051
3052 vmx->nested.nested_vmx_misc_low = data;
3053 vmx->nested.nested_vmx_misc_high = data >> 32;
3054 return 0;
3055}
3056
3057static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3058{
3059 u64 vmx_ept_vpid_cap;
3060
3061 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.nested_vmx_ept_caps,
3062 vmx->nested.nested_vmx_vpid_caps);
3063
3064 /* Every bit is either reserved or a feature bit. */
3065 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3066 return -EINVAL;
3067
3068 vmx->nested.nested_vmx_ept_caps = data;
3069 vmx->nested.nested_vmx_vpid_caps = data >> 32;
3070 return 0;
3071}
3072
3073static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3074{
3075 u64 *msr;
3076
3077 switch (msr_index) {
3078 case MSR_IA32_VMX_CR0_FIXED0:
3079 msr = &vmx->nested.nested_vmx_cr0_fixed0;
3080 break;
3081 case MSR_IA32_VMX_CR4_FIXED0:
3082 msr = &vmx->nested.nested_vmx_cr4_fixed0;
3083 break;
3084 default:
3085 BUG();
3086 }
3087
3088 /*
3089 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3090 * must be 1 in the restored value.
3091 */
3092 if (!is_bitwise_subset(data, *msr, -1ULL))
3093 return -EINVAL;
3094
3095 *msr = data;
3096 return 0;
3097}
3098
3099/*
3100 * Called when userspace is restoring VMX MSRs.
3101 *
3102 * Returns 0 on success, non-0 otherwise.
3103 */
3104static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3105{
3106 struct vcpu_vmx *vmx = to_vmx(vcpu);
3107
3108 switch (msr_index) {
3109 case MSR_IA32_VMX_BASIC:
3110 return vmx_restore_vmx_basic(vmx, data);
3111 case MSR_IA32_VMX_PINBASED_CTLS:
3112 case MSR_IA32_VMX_PROCBASED_CTLS:
3113 case MSR_IA32_VMX_EXIT_CTLS:
3114 case MSR_IA32_VMX_ENTRY_CTLS:
3115 /*
3116 * The "non-true" VMX capability MSRs are generated from the
3117 * "true" MSRs, so we do not support restoring them directly.
3118 *
3119 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3120 * should restore the "true" MSRs with the must-be-1 bits
3121 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3122 * DEFAULT SETTINGS".
3123 */
3124 return -EINVAL;
3125 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3126 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3127 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3128 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3129 case MSR_IA32_VMX_PROCBASED_CTLS2:
3130 return vmx_restore_control_msr(vmx, msr_index, data);
3131 case MSR_IA32_VMX_MISC:
3132 return vmx_restore_vmx_misc(vmx, data);
3133 case MSR_IA32_VMX_CR0_FIXED0:
3134 case MSR_IA32_VMX_CR4_FIXED0:
3135 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3136 case MSR_IA32_VMX_CR0_FIXED1:
3137 case MSR_IA32_VMX_CR4_FIXED1:
3138 /*
3139 * These MSRs are generated based on the vCPU's CPUID, so we
3140 * do not support restoring them directly.
3141 */
3142 return -EINVAL;
3143 case MSR_IA32_VMX_EPT_VPID_CAP:
3144 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3145 case MSR_IA32_VMX_VMCS_ENUM:
3146 vmx->nested.nested_vmx_vmcs_enum = data;
3147 return 0;
3148 default:
3149 /*
3150 * The rest of the VMX capability MSRs do not support restore.
3151 */
3152 return -EINVAL;
3153 }
3154}
3155
Jan Kiszkacae50132014-01-04 18:47:22 +01003156/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003157static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
3158{
Wincy Vanb9c237b2015-02-03 23:56:30 +08003159 struct vcpu_vmx *vmx = to_vmx(vcpu);
3160
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003161 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003162 case MSR_IA32_VMX_BASIC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003163 *pdata = vmx->nested.nested_vmx_basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003164 break;
3165 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3166 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003167 *pdata = vmx_control_msr(
3168 vmx->nested.nested_vmx_pinbased_ctls_low,
3169 vmx->nested.nested_vmx_pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003170 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3171 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003172 break;
3173 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3174 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003175 *pdata = vmx_control_msr(
3176 vmx->nested.nested_vmx_procbased_ctls_low,
3177 vmx->nested.nested_vmx_procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003178 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3179 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003180 break;
3181 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3182 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003183 *pdata = vmx_control_msr(
3184 vmx->nested.nested_vmx_exit_ctls_low,
3185 vmx->nested.nested_vmx_exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003186 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3187 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003188 break;
3189 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3190 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003191 *pdata = vmx_control_msr(
3192 vmx->nested.nested_vmx_entry_ctls_low,
3193 vmx->nested.nested_vmx_entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003194 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3195 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003196 break;
3197 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003198 *pdata = vmx_control_msr(
3199 vmx->nested.nested_vmx_misc_low,
3200 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003201 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003202 case MSR_IA32_VMX_CR0_FIXED0:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003203 *pdata = vmx->nested.nested_vmx_cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003204 break;
3205 case MSR_IA32_VMX_CR0_FIXED1:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003206 *pdata = vmx->nested.nested_vmx_cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003207 break;
3208 case MSR_IA32_VMX_CR4_FIXED0:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003209 *pdata = vmx->nested.nested_vmx_cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003210 break;
3211 case MSR_IA32_VMX_CR4_FIXED1:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003212 *pdata = vmx->nested.nested_vmx_cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003213 break;
3214 case MSR_IA32_VMX_VMCS_ENUM:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003215 *pdata = vmx->nested.nested_vmx_vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003216 break;
3217 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003218 *pdata = vmx_control_msr(
3219 vmx->nested.nested_vmx_secondary_ctls_low,
3220 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003221 break;
3222 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07003223 *pdata = vmx->nested.nested_vmx_ept_caps |
3224 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003225 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003226 case MSR_IA32_VMX_VMFUNC:
3227 *pdata = vmx->nested.nested_vmx_vmfunc_controls;
3228 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003229 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003230 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003231 }
3232
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003233 return 0;
3234}
3235
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003236static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3237 uint64_t val)
3238{
3239 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3240
3241 return !(val & ~valid_bits);
3242}
3243
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003244/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003245 * Reads an msr value (of 'msr_index') into 'pdata'.
3246 * Returns 0 on success, non-0 otherwise.
3247 * Assumes vcpu_load() was already called.
3248 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003249static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003250{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003251 struct vcpu_vmx *vmx = to_vmx(vcpu);
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:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003263 vmx_load_host_state(vmx);
3264 msr_info->data = vmx->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 &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003290 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003291 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:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003296 msr_info->data = vmx->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:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003313 msr = find_msr_entry(vmx, 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 |
Quan Xu8eb73e22017-12-12 16:44:21 +08003635 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003636 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 |
Paolo Bonzini0367f202016-07-12 10:44:55 +02003665 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00003666 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003667 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003668 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003669 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003670 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003671 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02003672 SECONDARY_EXEC_RDSEED_EXITING |
3673 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003674 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04003675 SECONDARY_EXEC_TSC_SCALING |
3676 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08003677 if (adjust_vmx_controls(min2, opt2,
3678 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003679 &_cpu_based_2nd_exec_control) < 0)
3680 return -EIO;
3681 }
3682#ifndef CONFIG_X86_64
3683 if (!(_cpu_based_2nd_exec_control &
3684 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3685 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3686#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003687
3688 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3689 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003690 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003691 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3692 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003693
Wanpeng Li61f1dd92017-10-18 16:02:19 -07003694 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
3695 &vmx_capability.ept, &vmx_capability.vpid);
3696
Sheng Yangd56f5462008-04-25 10:13:16 +08003697 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003698 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3699 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003700 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3701 CPU_BASED_CR3_STORE_EXITING |
3702 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07003703 } else if (vmx_capability.ept) {
3704 vmx_capability.ept = 0;
3705 pr_warn_once("EPT CAP should not exist if not support "
3706 "1-setting enable EPT VM-execution control\n");
3707 }
3708 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
3709 vmx_capability.vpid) {
3710 vmx_capability.vpid = 0;
3711 pr_warn_once("VPID CAP should not exist if not support "
3712 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08003713 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003714
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003715 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003716#ifdef CONFIG_X86_64
3717 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3718#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003719 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003720 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003721 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3722 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003723 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003724
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01003725 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3726 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3727 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003728 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3729 &_pin_based_exec_control) < 0)
3730 return -EIO;
3731
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003732 if (cpu_has_broken_vmx_preemption_timer())
3733 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003734 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003735 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003736 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3737
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003738 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003739 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003740 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3741 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003742 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003743
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003744 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003745
3746 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3747 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003748 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003749
3750#ifdef CONFIG_X86_64
3751 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3752 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003753 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003754#endif
3755
3756 /* Require Write-Back (WB) memory type for VMCS accesses. */
3757 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003758 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003759
Yang, Sheng002c7f72007-07-31 14:23:01 +03003760 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003761 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003762 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003763 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003764
Yang, Sheng002c7f72007-07-31 14:23:01 +03003765 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3766 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003767 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003768 vmcs_conf->vmexit_ctrl = _vmexit_control;
3769 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003770
Avi Kivity110312c2010-12-21 12:54:20 +02003771 cpu_has_load_ia32_efer =
3772 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3773 VM_ENTRY_LOAD_IA32_EFER)
3774 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3775 VM_EXIT_LOAD_IA32_EFER);
3776
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003777 cpu_has_load_perf_global_ctrl =
3778 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3779 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3780 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3781 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3782
3783 /*
3784 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003785 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003786 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3787 *
3788 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3789 *
3790 * AAK155 (model 26)
3791 * AAP115 (model 30)
3792 * AAT100 (model 37)
3793 * BC86,AAY89,BD102 (model 44)
3794 * BA97 (model 46)
3795 *
3796 */
3797 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3798 switch (boot_cpu_data.x86_model) {
3799 case 26:
3800 case 30:
3801 case 37:
3802 case 44:
3803 case 46:
3804 cpu_has_load_perf_global_ctrl = false;
3805 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3806 "does not work properly. Using workaround\n");
3807 break;
3808 default:
3809 break;
3810 }
3811 }
3812
Borislav Petkov782511b2016-04-04 22:25:03 +02003813 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003814 rdmsrl(MSR_IA32_XSS, host_xss);
3815
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003816 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003817}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003818
3819static struct vmcs *alloc_vmcs_cpu(int cpu)
3820{
3821 int node = cpu_to_node(cpu);
3822 struct page *pages;
3823 struct vmcs *vmcs;
3824
Vlastimil Babka96db8002015-09-08 15:03:50 -07003825 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003826 if (!pages)
3827 return NULL;
3828 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003829 memset(vmcs, 0, vmcs_config.size);
3830 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003831 return vmcs;
3832}
3833
3834static struct vmcs *alloc_vmcs(void)
3835{
Ingo Molnard3b2c332007-01-05 16:36:23 -08003836 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08003837}
3838
3839static void free_vmcs(struct vmcs *vmcs)
3840{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003841 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003842}
3843
Nadav Har'Eld462b812011-05-24 15:26:10 +03003844/*
3845 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3846 */
3847static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3848{
3849 if (!loaded_vmcs->vmcs)
3850 return;
3851 loaded_vmcs_clear(loaded_vmcs);
3852 free_vmcs(loaded_vmcs->vmcs);
3853 loaded_vmcs->vmcs = NULL;
Jim Mattson355f4fb2016-10-28 08:29:39 -07003854 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003855}
3856
Mark Kanda276c7962017-11-27 17:22:26 -06003857static void vmx_nested_free_vmcs02(struct vcpu_vmx *vmx)
3858{
3859 struct loaded_vmcs *loaded_vmcs = &vmx->nested.vmcs02;
3860
3861 /*
3862 * Just leak the VMCS02 if the WARN triggers. Better than
3863 * a use-after-free.
3864 */
3865 if (WARN_ON(vmx->loaded_vmcs == loaded_vmcs))
3866 return;
3867 free_loaded_vmcs(loaded_vmcs);
3868}
3869
Sam Ravnborg39959582007-06-01 00:47:13 -07003870static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003871{
3872 int cpu;
3873
Zachary Amsden3230bb42009-09-29 11:38:37 -10003874 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003875 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003876 per_cpu(vmxarea, cpu) = NULL;
3877 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003878}
3879
Jim Mattson85fd5142017-07-07 12:51:41 -07003880enum vmcs_field_type {
3881 VMCS_FIELD_TYPE_U16 = 0,
3882 VMCS_FIELD_TYPE_U64 = 1,
3883 VMCS_FIELD_TYPE_U32 = 2,
3884 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
3885};
3886
3887static inline int vmcs_field_type(unsigned long field)
3888{
3889 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
3890 return VMCS_FIELD_TYPE_U32;
3891 return (field >> 13) & 0x3 ;
3892}
3893
3894static inline int vmcs_field_readonly(unsigned long field)
3895{
3896 return (((field >> 10) & 0x3) == 1);
3897}
3898
Bandan Dasfe2b2012014-04-21 15:20:14 -04003899static void init_vmcs_shadow_fields(void)
3900{
3901 int i, j;
3902
3903 /* No checks for read only fields yet */
3904
3905 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3906 switch (shadow_read_write_fields[i]) {
3907 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003908 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003909 continue;
3910 break;
3911 default:
3912 break;
3913 }
3914
3915 if (j < i)
3916 shadow_read_write_fields[j] =
3917 shadow_read_write_fields[i];
3918 j++;
3919 }
3920 max_shadow_read_write_fields = j;
3921
3922 /* shadowed fields guest access without vmexit */
3923 for (i = 0; i < max_shadow_read_write_fields; i++) {
Jim Mattson85fd5142017-07-07 12:51:41 -07003924 unsigned long field = shadow_read_write_fields[i];
3925
3926 clear_bit(field, vmx_vmwrite_bitmap);
3927 clear_bit(field, vmx_vmread_bitmap);
3928 if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64) {
3929 clear_bit(field + 1, vmx_vmwrite_bitmap);
3930 clear_bit(field + 1, vmx_vmread_bitmap);
3931 }
Bandan Dasfe2b2012014-04-21 15:20:14 -04003932 }
Jim Mattson85fd5142017-07-07 12:51:41 -07003933 for (i = 0; i < max_shadow_read_only_fields; i++) {
3934 unsigned long field = shadow_read_only_fields[i];
3935
3936 clear_bit(field, vmx_vmread_bitmap);
3937 if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64)
3938 clear_bit(field + 1, vmx_vmread_bitmap);
3939 }
Bandan Dasfe2b2012014-04-21 15:20:14 -04003940}
3941
Avi Kivity6aa8b732006-12-10 02:21:36 -08003942static __init int alloc_kvm_area(void)
3943{
3944 int cpu;
3945
Zachary Amsden3230bb42009-09-29 11:38:37 -10003946 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003947 struct vmcs *vmcs;
3948
3949 vmcs = alloc_vmcs_cpu(cpu);
3950 if (!vmcs) {
3951 free_kvm_area();
3952 return -ENOMEM;
3953 }
3954
3955 per_cpu(vmxarea, cpu) = vmcs;
3956 }
3957 return 0;
3958}
3959
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003960static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003961 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003962{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003963 if (!emulate_invalid_guest_state) {
3964 /*
3965 * CS and SS RPL should be equal during guest entry according
3966 * to VMX spec, but in reality it is not always so. Since vcpu
3967 * is in the middle of the transition from real mode to
3968 * protected mode it is safe to assume that RPL 0 is a good
3969 * default value.
3970 */
3971 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003972 save->selector &= ~SEGMENT_RPL_MASK;
3973 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003974 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003975 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003976 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003977}
3978
3979static void enter_pmode(struct kvm_vcpu *vcpu)
3980{
3981 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003982 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003983
Gleb Natapovd99e4152012-12-20 16:57:45 +02003984 /*
3985 * Update real mode segment cache. It may be not up-to-date if sement
3986 * register was written while vcpu was in a guest mode.
3987 */
3988 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3989 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3990 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3991 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3992 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3993 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3994
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003995 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003996
Avi Kivity2fb92db2011-04-27 19:42:18 +03003997 vmx_segment_cache_clear(vmx);
3998
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003999 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004000
4001 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004002 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4003 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004004 vmcs_writel(GUEST_RFLAGS, flags);
4005
Rusty Russell66aee912007-07-17 23:34:16 +10004006 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4007 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004008
4009 update_exception_bitmap(vcpu);
4010
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004011 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4012 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4013 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4014 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4015 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4016 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004017}
4018
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004019static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004020{
Mathias Krause772e0312012-08-30 01:30:19 +02004021 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004022 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004023
Gleb Natapovd99e4152012-12-20 16:57:45 +02004024 var.dpl = 0x3;
4025 if (seg == VCPU_SREG_CS)
4026 var.type = 0x3;
4027
4028 if (!emulate_invalid_guest_state) {
4029 var.selector = var.base >> 4;
4030 var.base = var.base & 0xffff0;
4031 var.limit = 0xffff;
4032 var.g = 0;
4033 var.db = 0;
4034 var.present = 1;
4035 var.s = 1;
4036 var.l = 0;
4037 var.unusable = 0;
4038 var.type = 0x3;
4039 var.avl = 0;
4040 if (save->base & 0xf)
4041 printk_once(KERN_WARNING "kvm: segment base is not "
4042 "paragraph aligned when entering "
4043 "protected mode (seg=%d)", seg);
4044 }
4045
4046 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004047 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004048 vmcs_write32(sf->limit, var.limit);
4049 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004050}
4051
4052static void enter_rmode(struct kvm_vcpu *vcpu)
4053{
4054 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004055 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004057 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4058 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4059 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4060 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4061 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004062 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4063 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004064
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004065 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004066
Gleb Natapov776e58e2011-03-13 12:34:27 +02004067 /*
4068 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004069 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004070 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004071 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004072 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4073 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004074
Avi Kivity2fb92db2011-04-27 19:42:18 +03004075 vmx_segment_cache_clear(vmx);
4076
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004077 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004078 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004079 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4080
4081 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004082 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004083
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004084 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004085
4086 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004087 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004088 update_exception_bitmap(vcpu);
4089
Gleb Natapovd99e4152012-12-20 16:57:45 +02004090 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4091 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4092 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4093 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4094 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4095 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004096
Eddie Dong8668a3c2007-10-10 14:26:45 +08004097 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004098}
4099
Amit Shah401d10d2009-02-20 22:53:37 +05304100static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4101{
4102 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004103 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4104
4105 if (!msr)
4106 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304107
Avi Kivity44ea2b12009-09-06 15:55:37 +03004108 /*
4109 * Force kernel_gs_base reloading before EFER changes, as control
4110 * of this msr depends on is_long_mode().
4111 */
4112 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004113 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304114 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004115 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304116 msr->data = efer;
4117 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004118 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304119
4120 msr->data = efer & ~EFER_LME;
4121 }
4122 setup_msrs(vmx);
4123}
4124
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004125#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004126
4127static void enter_lmode(struct kvm_vcpu *vcpu)
4128{
4129 u32 guest_tr_ar;
4130
Avi Kivity2fb92db2011-04-27 19:42:18 +03004131 vmx_segment_cache_clear(to_vmx(vcpu));
4132
Avi Kivity6aa8b732006-12-10 02:21:36 -08004133 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004134 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004135 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4136 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004137 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004138 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4139 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004140 }
Avi Kivityda38f432010-07-06 11:30:49 +03004141 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004142}
4143
4144static void exit_lmode(struct kvm_vcpu *vcpu)
4145{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004146 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004147 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004148}
4149
4150#endif
4151
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004152static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4153 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004154{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004155 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004156 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4157 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004158 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004159 } else {
4160 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004161 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004162}
4163
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004164static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004165{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004166 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004167}
4168
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004169static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4170{
4171 if (enable_ept)
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004172 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004173}
4174
Avi Kivitye8467fd2009-12-29 18:43:06 +02004175static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4176{
4177 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4178
4179 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4180 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4181}
4182
Avi Kivityaff48ba2010-12-05 18:56:11 +02004183static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4184{
4185 if (enable_ept && is_paging(vcpu))
4186 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4187 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4188}
4189
Anthony Liguori25c4c272007-04-27 09:29:21 +03004190static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004191{
Avi Kivityfc78f512009-12-07 12:16:48 +02004192 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4193
4194 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4195 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004196}
4197
Sheng Yang14394422008-04-28 12:24:45 +08004198static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4199{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004200 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4201
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004202 if (!test_bit(VCPU_EXREG_PDPTR,
4203 (unsigned long *)&vcpu->arch.regs_dirty))
4204 return;
4205
Sheng Yang14394422008-04-28 12:24:45 +08004206 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004207 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4208 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4209 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4210 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004211 }
4212}
4213
Avi Kivity8f5d5492009-05-31 18:41:29 +03004214static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4215{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004216 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4217
Avi Kivity8f5d5492009-05-31 18:41:29 +03004218 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004219 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4220 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4221 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4222 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004223 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004224
4225 __set_bit(VCPU_EXREG_PDPTR,
4226 (unsigned long *)&vcpu->arch.regs_avail);
4227 __set_bit(VCPU_EXREG_PDPTR,
4228 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004229}
4230
David Matlack38991522016-11-29 18:14:08 -08004231static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4232{
4233 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4234 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4235 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4236
4237 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
4238 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4239 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4240 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4241
4242 return fixed_bits_valid(val, fixed0, fixed1);
4243}
4244
4245static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4246{
4247 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4248 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4249
4250 return fixed_bits_valid(val, fixed0, fixed1);
4251}
4252
4253static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4254{
4255 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0;
4256 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1;
4257
4258 return fixed_bits_valid(val, fixed0, fixed1);
4259}
4260
4261/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4262#define nested_guest_cr4_valid nested_cr4_valid
4263#define nested_host_cr4_valid nested_cr4_valid
4264
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004265static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004266
4267static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4268 unsigned long cr0,
4269 struct kvm_vcpu *vcpu)
4270{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004271 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4272 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004273 if (!(cr0 & X86_CR0_PG)) {
4274 /* From paging/starting to nonpaging */
4275 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004276 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004277 (CPU_BASED_CR3_LOAD_EXITING |
4278 CPU_BASED_CR3_STORE_EXITING));
4279 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004280 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004281 } else if (!is_paging(vcpu)) {
4282 /* From nonpaging to paging */
4283 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004284 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004285 ~(CPU_BASED_CR3_LOAD_EXITING |
4286 CPU_BASED_CR3_STORE_EXITING));
4287 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004288 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004289 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004290
4291 if (!(cr0 & X86_CR0_WP))
4292 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004293}
4294
Avi Kivity6aa8b732006-12-10 02:21:36 -08004295static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4296{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004297 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004298 unsigned long hw_cr0;
4299
Gleb Natapov50378782013-02-04 16:00:28 +02004300 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004301 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004302 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004303 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004304 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004305
Gleb Natapov218e7632013-01-21 15:36:45 +02004306 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4307 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004308
Gleb Natapov218e7632013-01-21 15:36:45 +02004309 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4310 enter_rmode(vcpu);
4311 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004312
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004313#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004314 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004315 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004316 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004317 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004318 exit_lmode(vcpu);
4319 }
4320#endif
4321
Avi Kivity089d0342009-03-23 18:26:32 +02004322 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004323 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4324
Avi Kivity6aa8b732006-12-10 02:21:36 -08004325 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004326 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004327 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004328
4329 /* depends on vcpu->arch.cr0 to be set to a new value */
4330 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004331}
4332
Yu Zhang855feb62017-08-24 20:27:55 +08004333static int get_ept_level(struct kvm_vcpu *vcpu)
4334{
4335 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4336 return 5;
4337 return 4;
4338}
4339
Peter Feiner995f00a2017-06-30 17:26:32 -07004340static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004341{
Yu Zhang855feb62017-08-24 20:27:55 +08004342 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004343
Yu Zhang855feb62017-08-24 20:27:55 +08004344 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004345
Peter Feiner995f00a2017-06-30 17:26:32 -07004346 if (enable_ept_ad_bits &&
4347 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004348 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004349 eptp |= (root_hpa & PAGE_MASK);
4350
4351 return eptp;
4352}
4353
Avi Kivity6aa8b732006-12-10 02:21:36 -08004354static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4355{
Sheng Yang14394422008-04-28 12:24:45 +08004356 unsigned long guest_cr3;
4357 u64 eptp;
4358
4359 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004360 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004361 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004362 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004363 if (is_paging(vcpu) || is_guest_mode(vcpu))
4364 guest_cr3 = kvm_read_cr3(vcpu);
4365 else
4366 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004367 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004368 }
4369
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004370 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08004371 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004372}
4373
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004374static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004375{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004376 /*
4377 * Pass through host's Machine Check Enable value to hw_cr4, which
4378 * is in force while we are in guest mode. Do not let guests control
4379 * this bit, even if host CR4.MCE == 0.
4380 */
4381 unsigned long hw_cr4 =
4382 (cr4_read_shadow() & X86_CR4_MCE) |
4383 (cr4 & ~X86_CR4_MCE) |
4384 (to_vmx(vcpu)->rmode.vm86_active ?
4385 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004386
Paolo Bonzini0367f202016-07-12 10:44:55 +02004387 if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) {
4388 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4389 SECONDARY_EXEC_DESC);
4390 hw_cr4 &= ~X86_CR4_UMIP;
4391 } else
4392 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4393 SECONDARY_EXEC_DESC);
4394
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004395 if (cr4 & X86_CR4_VMXE) {
4396 /*
4397 * To use VMXON (and later other VMX instructions), a guest
4398 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4399 * So basically the check on whether to allow nested VMX
4400 * is here.
4401 */
4402 if (!nested_vmx_allowed(vcpu))
4403 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004404 }
David Matlack38991522016-11-29 18:14:08 -08004405
4406 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004407 return 1;
4408
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004409 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004410 if (enable_ept) {
4411 if (!is_paging(vcpu)) {
4412 hw_cr4 &= ~X86_CR4_PAE;
4413 hw_cr4 |= X86_CR4_PSE;
4414 } else if (!(cr4 & X86_CR4_PAE)) {
4415 hw_cr4 &= ~X86_CR4_PAE;
4416 }
4417 }
Sheng Yang14394422008-04-28 12:24:45 +08004418
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004419 if (!enable_unrestricted_guest && !is_paging(vcpu))
4420 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004421 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4422 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4423 * to be manually disabled when guest switches to non-paging
4424 * mode.
4425 *
4426 * If !enable_unrestricted_guest, the CPU is always running
4427 * with CR0.PG=1 and CR4 needs to be modified.
4428 * If enable_unrestricted_guest, the CPU automatically
4429 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004430 */
Huaitong Handdba2622016-03-22 16:51:15 +08004431 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004432
Sheng Yang14394422008-04-28 12:24:45 +08004433 vmcs_writel(CR4_READ_SHADOW, cr4);
4434 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004435 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004436}
4437
Avi Kivity6aa8b732006-12-10 02:21:36 -08004438static void vmx_get_segment(struct kvm_vcpu *vcpu,
4439 struct kvm_segment *var, int seg)
4440{
Avi Kivitya9179492011-01-03 14:28:52 +02004441 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004442 u32 ar;
4443
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004444 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004445 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004446 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004447 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004448 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004449 var->base = vmx_read_guest_seg_base(vmx, seg);
4450 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4451 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004452 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004453 var->base = vmx_read_guest_seg_base(vmx, seg);
4454 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4455 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4456 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004457 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004458 var->type = ar & 15;
4459 var->s = (ar >> 4) & 1;
4460 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004461 /*
4462 * Some userspaces do not preserve unusable property. Since usable
4463 * segment has to be present according to VMX spec we can use present
4464 * property to amend userspace bug by making unusable segment always
4465 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4466 * segment as unusable.
4467 */
4468 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004469 var->avl = (ar >> 12) & 1;
4470 var->l = (ar >> 13) & 1;
4471 var->db = (ar >> 14) & 1;
4472 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004473}
4474
Avi Kivitya9179492011-01-03 14:28:52 +02004475static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4476{
Avi Kivitya9179492011-01-03 14:28:52 +02004477 struct kvm_segment s;
4478
4479 if (to_vmx(vcpu)->rmode.vm86_active) {
4480 vmx_get_segment(vcpu, &s, seg);
4481 return s.base;
4482 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004483 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004484}
4485
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004486static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004487{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004488 struct vcpu_vmx *vmx = to_vmx(vcpu);
4489
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004490 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004491 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004492 else {
4493 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004494 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004495 }
Avi Kivity69c73022011-03-07 15:26:44 +02004496}
4497
Avi Kivity653e3102007-05-07 10:55:37 +03004498static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004499{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004500 u32 ar;
4501
Avi Kivityf0495f92012-06-07 17:06:10 +03004502 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004503 ar = 1 << 16;
4504 else {
4505 ar = var->type & 15;
4506 ar |= (var->s & 1) << 4;
4507 ar |= (var->dpl & 3) << 5;
4508 ar |= (var->present & 1) << 7;
4509 ar |= (var->avl & 1) << 12;
4510 ar |= (var->l & 1) << 13;
4511 ar |= (var->db & 1) << 14;
4512 ar |= (var->g & 1) << 15;
4513 }
Avi Kivity653e3102007-05-07 10:55:37 +03004514
4515 return ar;
4516}
4517
4518static void vmx_set_segment(struct kvm_vcpu *vcpu,
4519 struct kvm_segment *var, int seg)
4520{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004521 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004522 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004523
Avi Kivity2fb92db2011-04-27 19:42:18 +03004524 vmx_segment_cache_clear(vmx);
4525
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004526 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4527 vmx->rmode.segs[seg] = *var;
4528 if (seg == VCPU_SREG_TR)
4529 vmcs_write16(sf->selector, var->selector);
4530 else if (var->s)
4531 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004532 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004533 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004534
Avi Kivity653e3102007-05-07 10:55:37 +03004535 vmcs_writel(sf->base, var->base);
4536 vmcs_write32(sf->limit, var->limit);
4537 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004538
4539 /*
4540 * Fix the "Accessed" bit in AR field of segment registers for older
4541 * qemu binaries.
4542 * IA32 arch specifies that at the time of processor reset the
4543 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004544 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004545 * state vmexit when "unrestricted guest" mode is turned on.
4546 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4547 * tree. Newer qemu binaries with that qemu fix would not need this
4548 * kvm hack.
4549 */
4550 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004551 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004552
Gleb Natapovf924d662012-12-12 19:10:55 +02004553 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004554
4555out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004556 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004557}
4558
Avi Kivity6aa8b732006-12-10 02:21:36 -08004559static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4560{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004561 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004562
4563 *db = (ar >> 14) & 1;
4564 *l = (ar >> 13) & 1;
4565}
4566
Gleb Natapov89a27f42010-02-16 10:51:48 +02004567static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004568{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004569 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4570 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004571}
4572
Gleb Natapov89a27f42010-02-16 10:51:48 +02004573static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004574{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004575 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4576 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004577}
4578
Gleb Natapov89a27f42010-02-16 10:51:48 +02004579static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004580{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004581 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4582 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004583}
4584
Gleb Natapov89a27f42010-02-16 10:51:48 +02004585static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004586{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004587 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4588 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004589}
4590
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004591static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4592{
4593 struct kvm_segment var;
4594 u32 ar;
4595
4596 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004597 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004598 if (seg == VCPU_SREG_CS)
4599 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004600 ar = vmx_segment_access_rights(&var);
4601
4602 if (var.base != (var.selector << 4))
4603 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004604 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004605 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004606 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004607 return false;
4608
4609 return true;
4610}
4611
4612static bool code_segment_valid(struct kvm_vcpu *vcpu)
4613{
4614 struct kvm_segment cs;
4615 unsigned int cs_rpl;
4616
4617 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004618 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004619
Avi Kivity1872a3f2009-01-04 23:26:52 +02004620 if (cs.unusable)
4621 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004622 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004623 return false;
4624 if (!cs.s)
4625 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004626 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004627 if (cs.dpl > cs_rpl)
4628 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004629 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004630 if (cs.dpl != cs_rpl)
4631 return false;
4632 }
4633 if (!cs.present)
4634 return false;
4635
4636 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4637 return true;
4638}
4639
4640static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4641{
4642 struct kvm_segment ss;
4643 unsigned int ss_rpl;
4644
4645 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004646 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004647
Avi Kivity1872a3f2009-01-04 23:26:52 +02004648 if (ss.unusable)
4649 return true;
4650 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004651 return false;
4652 if (!ss.s)
4653 return false;
4654 if (ss.dpl != ss_rpl) /* DPL != RPL */
4655 return false;
4656 if (!ss.present)
4657 return false;
4658
4659 return true;
4660}
4661
4662static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4663{
4664 struct kvm_segment var;
4665 unsigned int rpl;
4666
4667 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004668 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004669
Avi Kivity1872a3f2009-01-04 23:26:52 +02004670 if (var.unusable)
4671 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004672 if (!var.s)
4673 return false;
4674 if (!var.present)
4675 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004676 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004677 if (var.dpl < rpl) /* DPL < RPL */
4678 return false;
4679 }
4680
4681 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4682 * rights flags
4683 */
4684 return true;
4685}
4686
4687static bool tr_valid(struct kvm_vcpu *vcpu)
4688{
4689 struct kvm_segment tr;
4690
4691 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4692
Avi Kivity1872a3f2009-01-04 23:26:52 +02004693 if (tr.unusable)
4694 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004695 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004696 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004697 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004698 return false;
4699 if (!tr.present)
4700 return false;
4701
4702 return true;
4703}
4704
4705static bool ldtr_valid(struct kvm_vcpu *vcpu)
4706{
4707 struct kvm_segment ldtr;
4708
4709 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4710
Avi Kivity1872a3f2009-01-04 23:26:52 +02004711 if (ldtr.unusable)
4712 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004713 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004714 return false;
4715 if (ldtr.type != 2)
4716 return false;
4717 if (!ldtr.present)
4718 return false;
4719
4720 return true;
4721}
4722
4723static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4724{
4725 struct kvm_segment cs, ss;
4726
4727 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4728 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4729
Nadav Amitb32a9912015-03-29 16:33:04 +03004730 return ((cs.selector & SEGMENT_RPL_MASK) ==
4731 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004732}
4733
4734/*
4735 * Check if guest state is valid. Returns true if valid, false if
4736 * not.
4737 * We assume that registers are always usable
4738 */
4739static bool guest_state_valid(struct kvm_vcpu *vcpu)
4740{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004741 if (enable_unrestricted_guest)
4742 return true;
4743
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004744 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004745 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004746 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4747 return false;
4748 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4749 return false;
4750 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4751 return false;
4752 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4753 return false;
4754 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4755 return false;
4756 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4757 return false;
4758 } else {
4759 /* protected mode guest state checks */
4760 if (!cs_ss_rpl_check(vcpu))
4761 return false;
4762 if (!code_segment_valid(vcpu))
4763 return false;
4764 if (!stack_segment_valid(vcpu))
4765 return false;
4766 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4767 return false;
4768 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4769 return false;
4770 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4771 return false;
4772 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4773 return false;
4774 if (!tr_valid(vcpu))
4775 return false;
4776 if (!ldtr_valid(vcpu))
4777 return false;
4778 }
4779 /* TODO:
4780 * - Add checks on RIP
4781 * - Add checks on RFLAGS
4782 */
4783
4784 return true;
4785}
4786
Jim Mattson5fa99cb2017-07-06 16:33:07 -07004787static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
4788{
4789 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
4790}
4791
Mike Dayd77c26f2007-10-08 09:02:08 -04004792static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004793{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004794 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004795 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004796 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004797
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004798 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004799 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004800 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4801 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004802 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004803 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004804 r = kvm_write_guest_page(kvm, fn++, &data,
4805 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004806 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004807 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004808 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4809 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004810 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004811 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4812 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004813 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004814 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004815 r = kvm_write_guest_page(kvm, fn, &data,
4816 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4817 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004818out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004819 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004820 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004821}
4822
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004823static int init_rmode_identity_map(struct kvm *kvm)
4824{
Tang Chenf51770e2014-09-16 18:41:59 +08004825 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004826 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004827 u32 tmp;
4828
Tang Chena255d472014-09-16 18:41:58 +08004829 /* Protect kvm->arch.ept_identity_pagetable_done. */
4830 mutex_lock(&kvm->slots_lock);
4831
Tang Chenf51770e2014-09-16 18:41:59 +08004832 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004833 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004834
David Hildenbrandd8a6e362017-08-24 20:51:34 +02004835 if (!kvm->arch.ept_identity_map_addr)
4836 kvm->arch.ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004837 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004838
David Hildenbrandd8a6e362017-08-24 20:51:34 +02004839 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4840 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08004841 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004842 goto out2;
4843
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004844 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004845 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4846 if (r < 0)
4847 goto out;
4848 /* Set up identity-mapping pagetable for EPT in real mode */
4849 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4850 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4851 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4852 r = kvm_write_guest_page(kvm, identity_map_pfn,
4853 &tmp, i * sizeof(tmp), sizeof(tmp));
4854 if (r < 0)
4855 goto out;
4856 }
4857 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004858
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004859out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004860 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004861
4862out2:
4863 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004864 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004865}
4866
Avi Kivity6aa8b732006-12-10 02:21:36 -08004867static void seg_setup(int seg)
4868{
Mathias Krause772e0312012-08-30 01:30:19 +02004869 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004870 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004871
4872 vmcs_write16(sf->selector, 0);
4873 vmcs_writel(sf->base, 0);
4874 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004875 ar = 0x93;
4876 if (seg == VCPU_SREG_CS)
4877 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004878
4879 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004880}
4881
Sheng Yangf78e0e22007-10-29 09:40:42 +08004882static int alloc_apic_access_page(struct kvm *kvm)
4883{
Xiao Guangrong44841412012-09-07 14:14:20 +08004884 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004885 int r = 0;
4886
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004887 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004888 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004889 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004890 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4891 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004892 if (r)
4893 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004894
Tang Chen73a6d942014-09-11 13:38:00 +08004895 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004896 if (is_error_page(page)) {
4897 r = -EFAULT;
4898 goto out;
4899 }
4900
Tang Chenc24ae0d2014-09-24 15:57:58 +08004901 /*
4902 * Do not pin the page in memory, so that memory hot-unplug
4903 * is able to migrate it.
4904 */
4905 put_page(page);
4906 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004907out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004908 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004909 return r;
4910}
4911
Wanpeng Li991e7a02015-09-16 17:30:05 +08004912static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004913{
4914 int vpid;
4915
Avi Kivity919818a2009-03-23 18:01:29 +02004916 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004917 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004918 spin_lock(&vmx_vpid_lock);
4919 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004920 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004921 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004922 else
4923 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004924 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004925 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004926}
4927
Wanpeng Li991e7a02015-09-16 17:30:05 +08004928static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004929{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004930 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004931 return;
4932 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004933 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004934 spin_unlock(&vmx_vpid_lock);
4935}
4936
Yang Zhang8d146952013-01-25 10:18:50 +08004937#define MSR_TYPE_R 1
4938#define MSR_TYPE_W 2
4939static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4940 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004941{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004942 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004943
4944 if (!cpu_has_vmx_msr_bitmap())
4945 return;
4946
4947 /*
4948 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4949 * have the write-low and read-high bitmap offsets the wrong way round.
4950 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4951 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004952 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004953 if (type & MSR_TYPE_R)
4954 /* read-low */
4955 __clear_bit(msr, msr_bitmap + 0x000 / f);
4956
4957 if (type & MSR_TYPE_W)
4958 /* write-low */
4959 __clear_bit(msr, msr_bitmap + 0x800 / f);
4960
Sheng Yang25c5f222008-03-28 13:18:56 +08004961 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4962 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004963 if (type & MSR_TYPE_R)
4964 /* read-high */
4965 __clear_bit(msr, msr_bitmap + 0x400 / f);
4966
4967 if (type & MSR_TYPE_W)
4968 /* write-high */
4969 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4970
4971 }
4972}
4973
Wincy Vanf2b93282015-02-03 23:56:03 +08004974/*
4975 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4976 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4977 */
4978static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4979 unsigned long *msr_bitmap_nested,
4980 u32 msr, int type)
4981{
4982 int f = sizeof(unsigned long);
4983
4984 if (!cpu_has_vmx_msr_bitmap()) {
4985 WARN_ON(1);
4986 return;
4987 }
4988
4989 /*
4990 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4991 * have the write-low and read-high bitmap offsets the wrong way round.
4992 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4993 */
4994 if (msr <= 0x1fff) {
4995 if (type & MSR_TYPE_R &&
4996 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4997 /* read-low */
4998 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4999
5000 if (type & MSR_TYPE_W &&
5001 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5002 /* write-low */
5003 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5004
5005 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5006 msr &= 0x1fff;
5007 if (type & MSR_TYPE_R &&
5008 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5009 /* read-high */
5010 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5011
5012 if (type & MSR_TYPE_W &&
5013 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5014 /* write-high */
5015 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5016
5017 }
5018}
5019
Avi Kivity58972972009-02-24 22:26:47 +02005020static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
5021{
5022 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08005023 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
5024 msr, MSR_TYPE_R | MSR_TYPE_W);
5025 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
5026 msr, MSR_TYPE_R | MSR_TYPE_W);
5027}
5028
Radim Krčmář2e69f862016-09-29 22:41:32 +02005029static void vmx_disable_intercept_msr_x2apic(u32 msr, int type, bool apicv_active)
Yang Zhang8d146952013-01-25 10:18:50 +08005030{
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005031 if (apicv_active) {
Wanpeng Lic63e4562016-09-23 19:17:16 +08005032 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv,
Radim Krčmář2e69f862016-09-29 22:41:32 +02005033 msr, type);
Wanpeng Lic63e4562016-09-23 19:17:16 +08005034 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv,
Radim Krčmář2e69f862016-09-29 22:41:32 +02005035 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005036 } else {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005037 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
Radim Krčmář2e69f862016-09-29 22:41:32 +02005038 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005039 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
Radim Krčmář2e69f862016-09-29 22:41:32 +02005040 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005041 }
Avi Kivity58972972009-02-24 22:26:47 +02005042}
5043
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005044static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005045{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005046 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005047}
5048
David Matlackc9f04402017-08-01 14:00:40 -07005049static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5050{
5051 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5052 gfn_t gfn;
5053
5054 /*
5055 * Don't need to mark the APIC access page dirty; it is never
5056 * written to by the CPU during APIC virtualization.
5057 */
5058
5059 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5060 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5061 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5062 }
5063
5064 if (nested_cpu_has_posted_intr(vmcs12)) {
5065 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5066 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5067 }
5068}
5069
5070
David Hildenbrand6342c502017-01-25 11:58:58 +01005071static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005072{
5073 struct vcpu_vmx *vmx = to_vmx(vcpu);
5074 int max_irr;
5075 void *vapic_page;
5076 u16 status;
5077
David Matlackc9f04402017-08-01 14:00:40 -07005078 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5079 return;
Wincy Van705699a2015-02-03 23:58:17 +08005080
David Matlackc9f04402017-08-01 14:00:40 -07005081 vmx->nested.pi_pending = false;
5082 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5083 return;
Wincy Van705699a2015-02-03 23:58:17 +08005084
David Matlackc9f04402017-08-01 14:00:40 -07005085 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5086 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005087 vapic_page = kmap(vmx->nested.virtual_apic_page);
Wincy Van705699a2015-02-03 23:58:17 +08005088 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
5089 kunmap(vmx->nested.virtual_apic_page);
5090
5091 status = vmcs_read16(GUEST_INTR_STATUS);
5092 if ((u8)max_irr > ((u8)status & 0xff)) {
5093 status &= ~0xff;
5094 status |= (u8)max_irr;
5095 vmcs_write16(GUEST_INTR_STATUS, status);
5096 }
5097 }
David Matlackc9f04402017-08-01 14:00:40 -07005098
5099 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005100}
5101
Wincy Van06a55242017-04-28 13:13:59 +08005102static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5103 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005104{
5105#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005106 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5107
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005108 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005109 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005110 * The vector of interrupt to be delivered to vcpu had
5111 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005112 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005113 * Following cases will be reached in this block, and
5114 * we always send a notification event in all cases as
5115 * explained below.
5116 *
5117 * Case 1: vcpu keeps in non-root mode. Sending a
5118 * notification event posts the interrupt to vcpu.
5119 *
5120 * Case 2: vcpu exits to root mode and is still
5121 * runnable. PIR will be synced to vIRR before the
5122 * next vcpu entry. Sending a notification event in
5123 * this case has no effect, as vcpu is not in root
5124 * mode.
5125 *
5126 * Case 3: vcpu exits to root mode and is blocked.
5127 * vcpu_block() has already synced PIR to vIRR and
5128 * never blocks vcpu if vIRR is not cleared. Therefore,
5129 * a blocked vcpu here does not wait for any requested
5130 * interrupts in PIR, and sending a notification event
5131 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005132 */
Feng Wu28b835d2015-09-18 22:29:54 +08005133
Wincy Van06a55242017-04-28 13:13:59 +08005134 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005135 return true;
5136 }
5137#endif
5138 return false;
5139}
5140
Wincy Van705699a2015-02-03 23:58:17 +08005141static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5142 int vector)
5143{
5144 struct vcpu_vmx *vmx = to_vmx(vcpu);
5145
5146 if (is_guest_mode(vcpu) &&
5147 vector == vmx->nested.posted_intr_nv) {
5148 /* the PIR and ON have been set by L1. */
Wincy Van06a55242017-04-28 13:13:59 +08005149 kvm_vcpu_trigger_posted_interrupt(vcpu, true);
Wincy Van705699a2015-02-03 23:58:17 +08005150 /*
5151 * If a posted intr is not recognized by hardware,
5152 * we will accomplish it in the next vmentry.
5153 */
5154 vmx->nested.pi_pending = true;
5155 kvm_make_request(KVM_REQ_EVENT, vcpu);
5156 return 0;
5157 }
5158 return -1;
5159}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005160/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005161 * Send interrupt to vcpu via posted interrupt way.
5162 * 1. If target vcpu is running(non-root mode), send posted interrupt
5163 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5164 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5165 * interrupt from PIR in next vmentry.
5166 */
5167static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5168{
5169 struct vcpu_vmx *vmx = to_vmx(vcpu);
5170 int r;
5171
Wincy Van705699a2015-02-03 23:58:17 +08005172 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5173 if (!r)
5174 return;
5175
Yang Zhanga20ed542013-04-11 19:25:15 +08005176 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5177 return;
5178
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005179 /* If a previous notification has sent the IPI, nothing to do. */
5180 if (pi_test_and_set_on(&vmx->pi_desc))
5181 return;
5182
Wincy Van06a55242017-04-28 13:13:59 +08005183 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005184 kvm_vcpu_kick(vcpu);
5185}
5186
Avi Kivity6aa8b732006-12-10 02:21:36 -08005187/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005188 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5189 * will not change in the lifetime of the guest.
5190 * Note that host-state that does change is set elsewhere. E.g., host-state
5191 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5192 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005193static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005194{
5195 u32 low32, high32;
5196 unsigned long tmpl;
5197 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005198 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005199
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005200 cr0 = read_cr0();
5201 WARN_ON(cr0 & X86_CR0_TS);
5202 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005203
5204 /*
5205 * Save the most likely value for this task's CR3 in the VMCS.
5206 * We can't use __get_current_cr3_fast() because we're not atomic.
5207 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005208 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005209 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005210 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005211
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005212 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005213 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005214 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005215 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005216
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005217 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005218#ifdef CONFIG_X86_64
5219 /*
5220 * Load null selectors, so we can avoid reloading them in
5221 * __vmx_load_host_state(), in case userspace uses the null selectors
5222 * too (the expected case).
5223 */
5224 vmcs_write16(HOST_DS_SELECTOR, 0);
5225 vmcs_write16(HOST_ES_SELECTOR, 0);
5226#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005227 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5228 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005229#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005230 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5231 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5232
Juergen Gross87930012017-09-04 12:25:27 +02005233 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005234 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005235 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005236
Avi Kivity83287ea422012-09-16 15:10:57 +03005237 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005238
5239 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5240 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5241 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5242 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5243
5244 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5245 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5246 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5247 }
5248}
5249
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005250static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5251{
5252 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5253 if (enable_ept)
5254 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005255 if (is_guest_mode(&vmx->vcpu))
5256 vmx->vcpu.arch.cr4_guest_owned_bits &=
5257 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005258 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5259}
5260
Yang Zhang01e439b2013-04-11 19:25:12 +08005261static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5262{
5263 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5264
Andrey Smetanind62caab2015-11-10 15:36:33 +03005265 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005266 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005267
5268 if (!enable_vnmi)
5269 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5270
Yunhong Jiang64672c92016-06-13 14:19:59 -07005271 /* Enable the preemption timer dynamically */
5272 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005273 return pin_based_exec_ctrl;
5274}
5275
Andrey Smetanind62caab2015-11-10 15:36:33 +03005276static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5277{
5278 struct vcpu_vmx *vmx = to_vmx(vcpu);
5279
5280 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005281 if (cpu_has_secondary_exec_ctrls()) {
5282 if (kvm_vcpu_apicv_active(vcpu))
5283 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5284 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5285 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5286 else
5287 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5288 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5289 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5290 }
5291
5292 if (cpu_has_vmx_msr_bitmap())
5293 vmx_set_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005294}
5295
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005296static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5297{
5298 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005299
5300 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5301 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5302
Paolo Bonzini35754c92015-07-29 12:05:37 +02005303 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005304 exec_control &= ~CPU_BASED_TPR_SHADOW;
5305#ifdef CONFIG_X86_64
5306 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5307 CPU_BASED_CR8_LOAD_EXITING;
5308#endif
5309 }
5310 if (!enable_ept)
5311 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5312 CPU_BASED_CR3_LOAD_EXITING |
5313 CPU_BASED_INVLPG_EXITING;
5314 return exec_control;
5315}
5316
Jim Mattson45ec3682017-08-23 16:32:04 -07005317static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005318{
Jim Mattson45ec3682017-08-23 16:32:04 -07005319 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005320 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005321}
5322
Jim Mattson75f4fc82017-08-23 16:32:03 -07005323static bool vmx_rdseed_supported(void)
5324{
5325 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005326 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005327}
5328
Paolo Bonzini80154d72017-08-24 13:55:35 +02005329static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005330{
Paolo Bonzini80154d72017-08-24 13:55:35 +02005331 struct kvm_vcpu *vcpu = &vmx->vcpu;
5332
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005333 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005334
Paolo Bonzini80154d72017-08-24 13:55:35 +02005335 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005336 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5337 if (vmx->vpid == 0)
5338 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5339 if (!enable_ept) {
5340 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5341 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005342 /* Enable INVPCID for non-ept guests may cause performance regression. */
5343 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005344 }
5345 if (!enable_unrestricted_guest)
5346 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5347 if (!ple_gap)
5348 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02005349 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005350 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5351 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005352 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005353
5354 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5355 * in vmx_set_cr4. */
5356 exec_control &= ~SECONDARY_EXEC_DESC;
5357
Abel Gordonabc4fc52013-04-18 14:35:25 +03005358 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5359 (handle_vmptrld).
5360 We can NOT enable shadow_vmcs here because we don't have yet
5361 a current VMCS12
5362 */
5363 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005364
5365 if (!enable_pml)
5366 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005367
Paolo Bonzini3db13482017-08-24 14:48:03 +02005368 if (vmx_xsaves_supported()) {
5369 /* Exposing XSAVES only when XSAVE is exposed */
5370 bool xsaves_enabled =
5371 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5372 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5373
5374 if (!xsaves_enabled)
5375 exec_control &= ~SECONDARY_EXEC_XSAVES;
5376
5377 if (nested) {
5378 if (xsaves_enabled)
5379 vmx->nested.nested_vmx_secondary_ctls_high |=
5380 SECONDARY_EXEC_XSAVES;
5381 else
5382 vmx->nested.nested_vmx_secondary_ctls_high &=
5383 ~SECONDARY_EXEC_XSAVES;
5384 }
5385 }
5386
Paolo Bonzini80154d72017-08-24 13:55:35 +02005387 if (vmx_rdtscp_supported()) {
5388 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5389 if (!rdtscp_enabled)
5390 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5391
5392 if (nested) {
5393 if (rdtscp_enabled)
5394 vmx->nested.nested_vmx_secondary_ctls_high |=
5395 SECONDARY_EXEC_RDTSCP;
5396 else
5397 vmx->nested.nested_vmx_secondary_ctls_high &=
5398 ~SECONDARY_EXEC_RDTSCP;
5399 }
5400 }
5401
5402 if (vmx_invpcid_supported()) {
5403 /* Exposing INVPCID only when PCID is exposed */
5404 bool invpcid_enabled =
5405 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5406 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5407
5408 if (!invpcid_enabled) {
5409 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5410 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5411 }
5412
5413 if (nested) {
5414 if (invpcid_enabled)
5415 vmx->nested.nested_vmx_secondary_ctls_high |=
5416 SECONDARY_EXEC_ENABLE_INVPCID;
5417 else
5418 vmx->nested.nested_vmx_secondary_ctls_high &=
5419 ~SECONDARY_EXEC_ENABLE_INVPCID;
5420 }
5421 }
5422
Jim Mattson45ec3682017-08-23 16:32:04 -07005423 if (vmx_rdrand_supported()) {
5424 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5425 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005426 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005427
5428 if (nested) {
5429 if (rdrand_enabled)
5430 vmx->nested.nested_vmx_secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005431 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005432 else
5433 vmx->nested.nested_vmx_secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005434 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005435 }
5436 }
5437
Jim Mattson75f4fc82017-08-23 16:32:03 -07005438 if (vmx_rdseed_supported()) {
5439 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5440 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005441 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005442
5443 if (nested) {
5444 if (rdseed_enabled)
5445 vmx->nested.nested_vmx_secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005446 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005447 else
5448 vmx->nested.nested_vmx_secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005449 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005450 }
5451 }
5452
Paolo Bonzini80154d72017-08-24 13:55:35 +02005453 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005454}
5455
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005456static void ept_set_mmio_spte_mask(void)
5457{
5458 /*
5459 * EPT Misconfigurations can be generated if the value of bits 2:0
5460 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005461 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07005462 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5463 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005464}
5465
Wanpeng Lif53cd632014-12-02 19:14:58 +08005466#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005467/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005468 * Sets up the vmcs for emulated real mode.
5469 */
David Hildenbrand12d79912017-08-24 20:51:26 +02005470static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005471{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005472#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005473 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005474#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005475 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005476
Abel Gordon4607c2d2013-04-18 14:35:55 +03005477 if (enable_shadow_vmcs) {
5478 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5479 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5480 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005481 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02005482 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08005483
Avi Kivity6aa8b732006-12-10 02:21:36 -08005484 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5485
Avi Kivity6aa8b732006-12-10 02:21:36 -08005486 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005487 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005488 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005489
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005490 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005491
Dan Williamsdfa169b2016-06-02 11:17:24 -07005492 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02005493 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005494 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02005495 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07005496 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005497
Andrey Smetanind62caab2015-11-10 15:36:33 +03005498 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005499 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5500 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5501 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5502 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5503
5504 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005505
Li RongQing0bcf2612015-12-03 13:29:34 +08005506 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005507 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005508 }
5509
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005510 if (ple_gap) {
5511 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005512 vmx->ple_window = ple_window;
5513 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005514 }
5515
Xiao Guangrongc3707952011-07-12 03:28:04 +08005516 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5517 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005518 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5519
Avi Kivity9581d442010-10-19 16:46:55 +02005520 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5521 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005522 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005523#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005524 rdmsrl(MSR_FS_BASE, a);
5525 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5526 rdmsrl(MSR_GS_BASE, a);
5527 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5528#else
5529 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5530 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5531#endif
5532
Bandan Das2a499e42017-08-03 15:54:41 -04005533 if (cpu_has_vmx_vmfunc())
5534 vmcs_write64(VM_FUNCTION_CONTROL, 0);
5535
Eddie Dong2cc51562007-05-21 07:28:09 +03005536 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5537 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005538 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005539 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005540 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005541
Radim Krčmář74545702015-04-27 15:11:25 +02005542 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5543 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005544
Paolo Bonzini03916db2014-07-24 14:21:57 +02005545 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005546 u32 index = vmx_msr_index[i];
5547 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005548 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005549
5550 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5551 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005552 if (wrmsr_safe(index, data_low, data_high) < 0)
5553 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005554 vmx->guest_msrs[j].index = i;
5555 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005556 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005557 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005558 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005559
Gleb Natapov2961e8762013-11-25 15:37:13 +02005560
5561 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005562
5563 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005564 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005565
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005566 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5567 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5568
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005569 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005570
Wanpeng Lif53cd632014-12-02 19:14:58 +08005571 if (vmx_xsaves_supported())
5572 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5573
Peter Feiner4e595162016-07-07 14:49:58 -07005574 if (enable_pml) {
5575 ASSERT(vmx->pml_pg);
5576 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5577 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5578 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005579}
5580
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005581static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005582{
5583 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005584 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005585 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005586
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005587 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005588
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005589 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005590 kvm_set_cr8(vcpu, 0);
5591
5592 if (!init_event) {
5593 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5594 MSR_IA32_APICBASE_ENABLE;
5595 if (kvm_vcpu_is_reset_bsp(vcpu))
5596 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5597 apic_base_msr.host_initiated = true;
5598 kvm_set_apic_base(vcpu, &apic_base_msr);
5599 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005600
Avi Kivity2fb92db2011-04-27 19:42:18 +03005601 vmx_segment_cache_clear(vmx);
5602
Avi Kivity5706be02008-08-20 15:07:31 +03005603 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005604 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005605 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005606
5607 seg_setup(VCPU_SREG_DS);
5608 seg_setup(VCPU_SREG_ES);
5609 seg_setup(VCPU_SREG_FS);
5610 seg_setup(VCPU_SREG_GS);
5611 seg_setup(VCPU_SREG_SS);
5612
5613 vmcs_write16(GUEST_TR_SELECTOR, 0);
5614 vmcs_writel(GUEST_TR_BASE, 0);
5615 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5616 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5617
5618 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5619 vmcs_writel(GUEST_LDTR_BASE, 0);
5620 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5621 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5622
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005623 if (!init_event) {
5624 vmcs_write32(GUEST_SYSENTER_CS, 0);
5625 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5626 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5627 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5628 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005629
Wanpeng Lic37c2872017-11-20 14:52:21 -08005630 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01005631 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005632
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005633 vmcs_writel(GUEST_GDTR_BASE, 0);
5634 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5635
5636 vmcs_writel(GUEST_IDTR_BASE, 0);
5637 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5638
Anthony Liguori443381a2010-12-06 10:53:38 -06005639 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005640 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005641 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07005642 if (kvm_mpx_supported())
5643 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005644
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005645 setup_msrs(vmx);
5646
Avi Kivity6aa8b732006-12-10 02:21:36 -08005647 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5648
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005649 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005650 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005651 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005652 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005653 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005654 vmcs_write32(TPR_THRESHOLD, 0);
5655 }
5656
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005657 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005658
Sheng Yang2384d2b2008-01-17 15:14:33 +08005659 if (vmx->vpid != 0)
5660 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5661
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005662 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005663 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005664 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005665 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005666 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005667
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005668 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005669
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005670 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005671}
5672
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005673/*
5674 * In nested virtualization, check if L1 asked to exit on external interrupts.
5675 * For most existing hypervisors, this will always return true.
5676 */
5677static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5678{
5679 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5680 PIN_BASED_EXT_INTR_MASK;
5681}
5682
Bandan Das77b0f5d2014-04-19 18:17:45 -04005683/*
5684 * In nested virtualization, check if L1 has set
5685 * VM_EXIT_ACK_INTR_ON_EXIT
5686 */
5687static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5688{
5689 return get_vmcs12(vcpu)->vm_exit_controls &
5690 VM_EXIT_ACK_INTR_ON_EXIT;
5691}
5692
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005693static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5694{
5695 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5696 PIN_BASED_NMI_EXITING;
5697}
5698
Jan Kiszkac9a79532014-03-07 20:03:15 +01005699static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005700{
Paolo Bonzini47c01522016-12-19 11:44:07 +01005701 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5702 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005703}
5704
Jan Kiszkac9a79532014-03-07 20:03:15 +01005705static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005706{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005707 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005708 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01005709 enable_irq_window(vcpu);
5710 return;
5711 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005712
Paolo Bonzini47c01522016-12-19 11:44:07 +01005713 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5714 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005715}
5716
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005717static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005718{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005719 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005720 uint32_t intr;
5721 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005722
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005723 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005724
Avi Kivityfa89a812008-09-01 15:57:51 +03005725 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005726 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005727 int inc_eip = 0;
5728 if (vcpu->arch.interrupt.soft)
5729 inc_eip = vcpu->arch.event_exit_inst_len;
5730 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005731 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005732 return;
5733 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005734 intr = irq | INTR_INFO_VALID_MASK;
5735 if (vcpu->arch.interrupt.soft) {
5736 intr |= INTR_TYPE_SOFT_INTR;
5737 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5738 vmx->vcpu.arch.event_exit_inst_len);
5739 } else
5740 intr |= INTR_TYPE_EXT_INTR;
5741 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005742}
5743
Sheng Yangf08864b2008-05-15 18:23:25 +08005744static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5745{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005746 struct vcpu_vmx *vmx = to_vmx(vcpu);
5747
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005748 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005749 /*
5750 * Tracking the NMI-blocked state in software is built upon
5751 * finding the next open IRQ window. This, in turn, depends on
5752 * well-behaving guests: They have to keep IRQs disabled at
5753 * least as long as the NMI handler runs. Otherwise we may
5754 * cause NMI nesting, maybe breaking the guest. But as this is
5755 * highly unlikely, we can live with the residual risk.
5756 */
5757 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
5758 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5759 }
5760
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005761 ++vcpu->stat.nmi_injections;
5762 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005763
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005764 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005765 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005766 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005767 return;
5768 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005769
Sheng Yangf08864b2008-05-15 18:23:25 +08005770 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5771 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005772}
5773
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005774static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5775{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005776 struct vcpu_vmx *vmx = to_vmx(vcpu);
5777 bool masked;
5778
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005779 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005780 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005781 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02005782 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005783 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
5784 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5785 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005786}
5787
5788static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5789{
5790 struct vcpu_vmx *vmx = to_vmx(vcpu);
5791
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005792 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005793 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
5794 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
5795 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5796 }
5797 } else {
5798 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5799 if (masked)
5800 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5801 GUEST_INTR_STATE_NMI);
5802 else
5803 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5804 GUEST_INTR_STATE_NMI);
5805 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005806}
5807
Jan Kiszka2505dc92013-04-14 12:12:47 +02005808static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5809{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005810 if (to_vmx(vcpu)->nested.nested_run_pending)
5811 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005812
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005813 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005814 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
5815 return 0;
5816
Jan Kiszka2505dc92013-04-14 12:12:47 +02005817 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5818 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5819 | GUEST_INTR_STATE_NMI));
5820}
5821
Gleb Natapov78646122009-03-23 12:12:11 +02005822static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5823{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005824 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5825 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005826 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5827 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005828}
5829
Izik Eiduscbc94022007-10-25 00:29:55 +02005830static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5831{
5832 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005833
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005834 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5835 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005836 if (ret)
5837 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005838 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005839 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005840}
5841
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005842static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005843{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005844 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005845 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005846 /*
5847 * Update instruction length as we may reinject the exception
5848 * from user space while in guest debugging mode.
5849 */
5850 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5851 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005852 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005853 return false;
5854 /* fall through */
5855 case DB_VECTOR:
5856 if (vcpu->guest_debug &
5857 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5858 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005859 /* fall through */
5860 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005861 case OF_VECTOR:
5862 case BR_VECTOR:
5863 case UD_VECTOR:
5864 case DF_VECTOR:
5865 case SS_VECTOR:
5866 case GP_VECTOR:
5867 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005868 return true;
5869 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005870 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005871 return false;
5872}
5873
5874static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5875 int vec, u32 err_code)
5876{
5877 /*
5878 * Instruction with address size override prefix opcode 0x67
5879 * Cause the #SS fault with 0 error code in VM86 mode.
5880 */
5881 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5882 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5883 if (vcpu->arch.halt_request) {
5884 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005885 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005886 }
5887 return 1;
5888 }
5889 return 0;
5890 }
5891
5892 /*
5893 * Forward all other exceptions that are valid in real mode.
5894 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5895 * the required debugging infrastructure rework.
5896 */
5897 kvm_queue_exception(vcpu, vec);
5898 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005899}
5900
Andi Kleena0861c02009-06-08 17:37:09 +08005901/*
5902 * Trigger machine check on the host. We assume all the MSRs are already set up
5903 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5904 * We pass a fake environment to the machine check handler because we want
5905 * the guest to be always treated like user space, no matter what context
5906 * it used internally.
5907 */
5908static void kvm_machine_check(void)
5909{
5910#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5911 struct pt_regs regs = {
5912 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5913 .flags = X86_EFLAGS_IF,
5914 };
5915
5916 do_machine_check(&regs, 0);
5917#endif
5918}
5919
Avi Kivity851ba692009-08-24 11:10:17 +03005920static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005921{
5922 /* already handled by vcpu_run */
5923 return 1;
5924}
5925
Avi Kivity851ba692009-08-24 11:10:17 +03005926static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005927{
Avi Kivity1155f762007-11-22 11:30:47 +02005928 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005929 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005930 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005931 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005932 u32 vect_info;
5933 enum emulation_result er;
5934
Avi Kivity1155f762007-11-22 11:30:47 +02005935 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005936 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005937
Andi Kleena0861c02009-06-08 17:37:09 +08005938 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005939 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005940
Jim Mattsonef85b672016-12-12 11:01:37 -08005941 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005942 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005943
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005944 if (is_invalid_opcode(intr_info)) {
Liran Alonac9b3052017-11-06 16:15:10 +02005945 WARN_ON_ONCE(is_guest_mode(vcpu));
Andre Przywara51d8b662010-12-21 11:12:02 +01005946 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alon61cb57c2017-11-05 16:56:32 +02005947 if (er == EMULATE_USER_EXIT)
5948 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005949 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005950 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005951 return 1;
5952 }
5953
Avi Kivity6aa8b732006-12-10 02:21:36 -08005954 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005955 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005956 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005957
5958 /*
5959 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5960 * MMIO, it is better to report an internal error.
5961 * See the comments in vmx_handle_exit.
5962 */
5963 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5964 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5965 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5966 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005967 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005968 vcpu->run->internal.data[0] = vect_info;
5969 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005970 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005971 return 0;
5972 }
5973
Avi Kivity6aa8b732006-12-10 02:21:36 -08005974 if (is_page_fault(intr_info)) {
5975 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005976 /* EPT won't cause page fault directly */
5977 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02005978 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005979 }
5980
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005981 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005982
5983 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5984 return handle_rmode_exception(vcpu, ex_no, error_code);
5985
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005986 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005987 case AC_VECTOR:
5988 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5989 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005990 case DB_VECTOR:
5991 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5992 if (!(vcpu->guest_debug &
5993 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005994 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005995 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01005996 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
5997 skip_emulated_instruction(vcpu);
5998
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005999 kvm_queue_exception(vcpu, DB_VECTOR);
6000 return 1;
6001 }
6002 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6003 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6004 /* fall through */
6005 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006006 /*
6007 * Update instruction length as we may reinject #BP from
6008 * user space while in guest debugging mode. Reading it for
6009 * #DB as well causes no harm, it is not used in that case.
6010 */
6011 vmx->vcpu.arch.event_exit_inst_len =
6012 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006013 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006014 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006015 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6016 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006017 break;
6018 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006019 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6020 kvm_run->ex.exception = ex_no;
6021 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006022 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006023 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006024 return 0;
6025}
6026
Avi Kivity851ba692009-08-24 11:10:17 +03006027static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006028{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006029 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006030 return 1;
6031}
6032
Avi Kivity851ba692009-08-24 11:10:17 +03006033static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006034{
Avi Kivity851ba692009-08-24 11:10:17 +03006035 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006036 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006037 return 0;
6038}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006039
Avi Kivity851ba692009-08-24 11:10:17 +03006040static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006041{
He, Qingbfdaab02007-09-12 14:18:28 +08006042 unsigned long exit_qualification;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006043 int size, in, string, ret;
Avi Kivity039576c2007-03-20 12:46:50 +02006044 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006045
He, Qingbfdaab02007-09-12 14:18:28 +08006046 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006047 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006048 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006049
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006050 ++vcpu->stat.io_exits;
6051
6052 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01006053 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006054
6055 port = exit_qualification >> 16;
6056 size = (exit_qualification & 7) + 1;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006057
Kyle Huey6affcbe2016-11-29 12:40:40 -08006058 ret = kvm_skip_emulated_instruction(vcpu);
6059
6060 /*
6061 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
6062 * KVM_EXIT_DEBUG here.
6063 */
6064 return kvm_fast_pio_out(vcpu, size, port) && ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006065}
6066
Ingo Molnar102d8322007-02-19 14:37:47 +02006067static void
6068vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6069{
6070 /*
6071 * Patch in the VMCALL instruction:
6072 */
6073 hypercall[0] = 0x0f;
6074 hypercall[1] = 0x01;
6075 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006076}
6077
Guo Chao0fa06072012-06-28 15:16:19 +08006078/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006079static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6080{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006081 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006082 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6083 unsigned long orig_val = val;
6084
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006085 /*
6086 * We get here when L2 changed cr0 in a way that did not change
6087 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006088 * but did change L0 shadowed bits. So we first calculate the
6089 * effective cr0 value that L1 would like to write into the
6090 * hardware. It consists of the L2-owned bits from the new
6091 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006092 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006093 val = (val & ~vmcs12->cr0_guest_host_mask) |
6094 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6095
David Matlack38991522016-11-29 18:14:08 -08006096 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006097 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006098
6099 if (kvm_set_cr0(vcpu, val))
6100 return 1;
6101 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006102 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006103 } else {
6104 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006105 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006106 return 1;
David Matlack38991522016-11-29 18:14:08 -08006107
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006108 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006109 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006110}
6111
6112static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6113{
6114 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006115 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6116 unsigned long orig_val = val;
6117
6118 /* analogously to handle_set_cr0 */
6119 val = (val & ~vmcs12->cr4_guest_host_mask) |
6120 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6121 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006122 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006123 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006124 return 0;
6125 } else
6126 return kvm_set_cr4(vcpu, val);
6127}
6128
Paolo Bonzini0367f202016-07-12 10:44:55 +02006129static int handle_desc(struct kvm_vcpu *vcpu)
6130{
6131 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6132 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6133}
6134
Avi Kivity851ba692009-08-24 11:10:17 +03006135static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006136{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006137 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006138 int cr;
6139 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006140 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006141 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006142
He, Qingbfdaab02007-09-12 14:18:28 +08006143 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006144 cr = exit_qualification & 15;
6145 reg = (exit_qualification >> 8) & 15;
6146 switch ((exit_qualification >> 4) & 3) {
6147 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006148 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006149 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006150 switch (cr) {
6151 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006152 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006153 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006154 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006155 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006156 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006157 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006158 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006159 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006160 case 8: {
6161 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006162 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006163 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006164 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006165 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006166 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006167 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006168 return ret;
6169 /*
6170 * TODO: we might be squashing a
6171 * KVM_GUESTDBG_SINGLESTEP-triggered
6172 * KVM_EXIT_DEBUG here.
6173 */
Avi Kivity851ba692009-08-24 11:10:17 +03006174 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006175 return 0;
6176 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006177 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006178 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006179 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006180 WARN_ONCE(1, "Guest should always own CR0.TS");
6181 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006182 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006183 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006184 case 1: /*mov from cr*/
6185 switch (cr) {
6186 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006187 val = kvm_read_cr3(vcpu);
6188 kvm_register_write(vcpu, reg, val);
6189 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006190 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006191 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006192 val = kvm_get_cr8(vcpu);
6193 kvm_register_write(vcpu, reg, val);
6194 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006195 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006196 }
6197 break;
6198 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006199 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006200 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006201 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006202
Kyle Huey6affcbe2016-11-29 12:40:40 -08006203 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006204 default:
6205 break;
6206 }
Avi Kivity851ba692009-08-24 11:10:17 +03006207 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006208 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006209 (int)(exit_qualification >> 4) & 3, cr);
6210 return 0;
6211}
6212
Avi Kivity851ba692009-08-24 11:10:17 +03006213static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006214{
He, Qingbfdaab02007-09-12 14:18:28 +08006215 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006216 int dr, dr7, reg;
6217
6218 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6219 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6220
6221 /* First, if DR does not exist, trigger UD */
6222 if (!kvm_require_dr(vcpu, dr))
6223 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006224
Jan Kiszkaf2483412010-01-20 18:20:20 +01006225 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006226 if (!kvm_require_cpl(vcpu, 0))
6227 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006228 dr7 = vmcs_readl(GUEST_DR7);
6229 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006230 /*
6231 * As the vm-exit takes precedence over the debug trap, we
6232 * need to emulate the latter, either for the host or the
6233 * guest debugging itself.
6234 */
6235 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006236 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006237 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006238 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006239 vcpu->run->debug.arch.exception = DB_VECTOR;
6240 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006241 return 0;
6242 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006243 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006244 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006245 kvm_queue_exception(vcpu, DB_VECTOR);
6246 return 1;
6247 }
6248 }
6249
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006250 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006251 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6252 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006253
6254 /*
6255 * No more DR vmexits; force a reload of the debug registers
6256 * and reenter on this instruction. The next vmexit will
6257 * retrieve the full state of the debug registers.
6258 */
6259 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6260 return 1;
6261 }
6262
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006263 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6264 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006265 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006266
6267 if (kvm_get_dr(vcpu, dr, &val))
6268 return 1;
6269 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006270 } else
Nadav Amit57773922014-06-18 17:19:23 +03006271 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006272 return 1;
6273
Kyle Huey6affcbe2016-11-29 12:40:40 -08006274 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006275}
6276
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006277static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6278{
6279 return vcpu->arch.dr6;
6280}
6281
6282static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6283{
6284}
6285
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006286static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6287{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006288 get_debugreg(vcpu->arch.db[0], 0);
6289 get_debugreg(vcpu->arch.db[1], 1);
6290 get_debugreg(vcpu->arch.db[2], 2);
6291 get_debugreg(vcpu->arch.db[3], 3);
6292 get_debugreg(vcpu->arch.dr6, 6);
6293 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6294
6295 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006296 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006297}
6298
Gleb Natapov020df072010-04-13 10:05:23 +03006299static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6300{
6301 vmcs_writel(GUEST_DR7, val);
6302}
6303
Avi Kivity851ba692009-08-24 11:10:17 +03006304static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006305{
Kyle Huey6a908b62016-11-29 12:40:37 -08006306 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006307}
6308
Avi Kivity851ba692009-08-24 11:10:17 +03006309static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006310{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006311 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006312 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006313
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006314 msr_info.index = ecx;
6315 msr_info.host_initiated = false;
6316 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006317 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006318 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006319 return 1;
6320 }
6321
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006322 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006323
Avi Kivity6aa8b732006-12-10 02:21:36 -08006324 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006325 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6326 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006327 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006328}
6329
Avi Kivity851ba692009-08-24 11:10:17 +03006330static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006331{
Will Auld8fe8ab42012-11-29 12:42:12 -08006332 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006333 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6334 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6335 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006336
Will Auld8fe8ab42012-11-29 12:42:12 -08006337 msr.data = data;
6338 msr.index = ecx;
6339 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006340 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006341 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006342 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006343 return 1;
6344 }
6345
Avi Kivity59200272010-01-25 19:47:02 +02006346 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006347 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006348}
6349
Avi Kivity851ba692009-08-24 11:10:17 +03006350static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006351{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01006352 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006353 return 1;
6354}
6355
Avi Kivity851ba692009-08-24 11:10:17 +03006356static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006357{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006358 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6359 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006360
Avi Kivity3842d132010-07-27 12:30:24 +03006361 kvm_make_request(KVM_REQ_EVENT, vcpu);
6362
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006363 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006364 return 1;
6365}
6366
Avi Kivity851ba692009-08-24 11:10:17 +03006367static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006368{
Avi Kivityd3bef152007-06-05 15:53:05 +03006369 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006370}
6371
Avi Kivity851ba692009-08-24 11:10:17 +03006372static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006373{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006374 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006375}
6376
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006377static int handle_invd(struct kvm_vcpu *vcpu)
6378{
Andre Przywara51d8b662010-12-21 11:12:02 +01006379 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006380}
6381
Avi Kivity851ba692009-08-24 11:10:17 +03006382static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006383{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006384 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006385
6386 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006387 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006388}
6389
Avi Kivityfee84b02011-11-10 14:57:25 +02006390static int handle_rdpmc(struct kvm_vcpu *vcpu)
6391{
6392 int err;
6393
6394 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006395 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02006396}
6397
Avi Kivity851ba692009-08-24 11:10:17 +03006398static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006399{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006400 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006401}
6402
Dexuan Cui2acf9232010-06-10 11:27:12 +08006403static int handle_xsetbv(struct kvm_vcpu *vcpu)
6404{
6405 u64 new_bv = kvm_read_edx_eax(vcpu);
6406 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6407
6408 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006409 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08006410 return 1;
6411}
6412
Wanpeng Lif53cd632014-12-02 19:14:58 +08006413static int handle_xsaves(struct kvm_vcpu *vcpu)
6414{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006415 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006416 WARN(1, "this should never happen\n");
6417 return 1;
6418}
6419
6420static int handle_xrstors(struct kvm_vcpu *vcpu)
6421{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006422 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006423 WARN(1, "this should never happen\n");
6424 return 1;
6425}
6426
Avi Kivity851ba692009-08-24 11:10:17 +03006427static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006428{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006429 if (likely(fasteoi)) {
6430 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6431 int access_type, offset;
6432
6433 access_type = exit_qualification & APIC_ACCESS_TYPE;
6434 offset = exit_qualification & APIC_ACCESS_OFFSET;
6435 /*
6436 * Sane guest uses MOV to write EOI, with written value
6437 * not cared. So make a short-circuit here by avoiding
6438 * heavy instruction emulation.
6439 */
6440 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6441 (offset == APIC_EOI)) {
6442 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006443 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03006444 }
6445 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006446 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006447}
6448
Yang Zhangc7c9c562013-01-25 10:18:51 +08006449static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6450{
6451 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6452 int vector = exit_qualification & 0xff;
6453
6454 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6455 kvm_apic_set_eoi_accelerated(vcpu, vector);
6456 return 1;
6457}
6458
Yang Zhang83d4c282013-01-25 10:18:49 +08006459static int handle_apic_write(struct kvm_vcpu *vcpu)
6460{
6461 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6462 u32 offset = exit_qualification & 0xfff;
6463
6464 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6465 kvm_apic_write_nodecode(vcpu, offset);
6466 return 1;
6467}
6468
Avi Kivity851ba692009-08-24 11:10:17 +03006469static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006470{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006471 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006472 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006473 bool has_error_code = false;
6474 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006475 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006476 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006477
6478 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006479 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006480 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006481
6482 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6483
6484 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006485 if (reason == TASK_SWITCH_GATE && idt_v) {
6486 switch (type) {
6487 case INTR_TYPE_NMI_INTR:
6488 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006489 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006490 break;
6491 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006492 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006493 kvm_clear_interrupt_queue(vcpu);
6494 break;
6495 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006496 if (vmx->idt_vectoring_info &
6497 VECTORING_INFO_DELIVER_CODE_MASK) {
6498 has_error_code = true;
6499 error_code =
6500 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6501 }
6502 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006503 case INTR_TYPE_SOFT_EXCEPTION:
6504 kvm_clear_exception_queue(vcpu);
6505 break;
6506 default:
6507 break;
6508 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006509 }
Izik Eidus37817f22008-03-24 23:14:53 +02006510 tss_selector = exit_qualification;
6511
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006512 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6513 type != INTR_TYPE_EXT_INTR &&
6514 type != INTR_TYPE_NMI_INTR))
6515 skip_emulated_instruction(vcpu);
6516
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006517 if (kvm_task_switch(vcpu, tss_selector,
6518 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6519 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006520 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6521 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6522 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006523 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006524 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006525
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006526 /*
6527 * TODO: What about debug traps on tss switch?
6528 * Are we supposed to inject them and update dr6?
6529 */
6530
6531 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006532}
6533
Avi Kivity851ba692009-08-24 11:10:17 +03006534static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006535{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006536 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006537 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01006538 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006539
Sheng Yangf9c617f2009-03-25 10:08:52 +08006540 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006541
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006542 /*
6543 * EPT violation happened while executing iret from NMI,
6544 * "blocked by NMI" bit has to be set before next VM entry.
6545 * There are errata that may cause this bit to not be set:
6546 * AAK134, BY25.
6547 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006548 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006549 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006550 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006551 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6552
Sheng Yang14394422008-04-28 12:24:45 +08006553 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006554 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006555
Junaid Shahid27959a42016-12-06 16:46:10 -08006556 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006557 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08006558 ? PFERR_USER_MASK : 0;
6559 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006560 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08006561 ? PFERR_WRITE_MASK : 0;
6562 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006563 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08006564 ? PFERR_FETCH_MASK : 0;
6565 /* ept page table entry is present? */
6566 error_code |= (exit_qualification &
6567 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6568 EPT_VIOLATION_EXECUTABLE))
6569 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006570
Paolo Bonzinieebed242016-11-28 14:39:58 +01006571 error_code |= (exit_qualification & 0x100) != 0 ?
6572 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006573
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006574 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006575 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006576}
6577
Avi Kivity851ba692009-08-24 11:10:17 +03006578static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006579{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006580 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006581 gpa_t gpa;
6582
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02006583 /*
6584 * A nested guest cannot optimize MMIO vmexits, because we have an
6585 * nGPA here instead of the required GPA.
6586 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006587 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02006588 if (!is_guest_mode(vcpu) &&
6589 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08006590 trace_kvm_fast_mmio(gpa);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006591 return kvm_skip_emulated_instruction(vcpu);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006592 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006593
Paolo Bonzinie08d26f2017-08-17 18:36:56 +02006594 ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
6595 if (ret >= 0)
6596 return ret;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006597
6598 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006599 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006600
Avi Kivity851ba692009-08-24 11:10:17 +03006601 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6602 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006603
6604 return 0;
6605}
6606
Avi Kivity851ba692009-08-24 11:10:17 +03006607static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006608{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006609 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01006610 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6611 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08006612 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006613 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006614
6615 return 1;
6616}
6617
Mohammed Gamal80ced182009-09-01 12:48:18 +02006618static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006619{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006620 struct vcpu_vmx *vmx = to_vmx(vcpu);
6621 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006622 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006623 u32 cpu_exec_ctrl;
6624 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006625 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006626
6627 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6628 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006629
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006630 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006631 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006632 return handle_interrupt_window(&vmx->vcpu);
6633
Radim Krčmář72875d82017-04-26 22:32:19 +02006634 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03006635 return 1;
6636
Liran Alon9b8ae632017-11-05 16:56:34 +02006637 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006638
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006639 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006640 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006641 ret = 0;
6642 goto out;
6643 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006644
Avi Kivityde5f70e2012-06-12 20:22:28 +03006645 if (err != EMULATE_DONE) {
6646 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6647 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6648 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006649 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03006650 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006651
Gleb Natapov8d76c492013-05-08 18:38:44 +03006652 if (vcpu->arch.halt_request) {
6653 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006654 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006655 goto out;
6656 }
6657
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006658 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006659 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006660 if (need_resched())
6661 schedule();
6662 }
6663
Mohammed Gamal80ced182009-09-01 12:48:18 +02006664out:
6665 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006666}
6667
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006668static int __grow_ple_window(int val)
6669{
6670 if (ple_window_grow < 1)
6671 return ple_window;
6672
6673 val = min(val, ple_window_actual_max);
6674
6675 if (ple_window_grow < ple_window)
6676 val *= ple_window_grow;
6677 else
6678 val += ple_window_grow;
6679
6680 return val;
6681}
6682
6683static int __shrink_ple_window(int val, int modifier, int minimum)
6684{
6685 if (modifier < 1)
6686 return ple_window;
6687
6688 if (modifier < ple_window)
6689 val /= modifier;
6690 else
6691 val -= modifier;
6692
6693 return max(val, minimum);
6694}
6695
6696static void grow_ple_window(struct kvm_vcpu *vcpu)
6697{
6698 struct vcpu_vmx *vmx = to_vmx(vcpu);
6699 int old = vmx->ple_window;
6700
6701 vmx->ple_window = __grow_ple_window(old);
6702
6703 if (vmx->ple_window != old)
6704 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006705
6706 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006707}
6708
6709static void shrink_ple_window(struct kvm_vcpu *vcpu)
6710{
6711 struct vcpu_vmx *vmx = to_vmx(vcpu);
6712 int old = vmx->ple_window;
6713
6714 vmx->ple_window = __shrink_ple_window(old,
6715 ple_window_shrink, ple_window);
6716
6717 if (vmx->ple_window != old)
6718 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006719
6720 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006721}
6722
6723/*
6724 * ple_window_actual_max is computed to be one grow_ple_window() below
6725 * ple_window_max. (See __grow_ple_window for the reason.)
6726 * This prevents overflows, because ple_window_max is int.
6727 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6728 * this process.
6729 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6730 */
6731static void update_ple_window_actual_max(void)
6732{
6733 ple_window_actual_max =
6734 __shrink_ple_window(max(ple_window_max, ple_window),
6735 ple_window_grow, INT_MIN);
6736}
6737
Feng Wubf9f6ac2015-09-18 22:29:55 +08006738/*
6739 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6740 */
6741static void wakeup_handler(void)
6742{
6743 struct kvm_vcpu *vcpu;
6744 int cpu = smp_processor_id();
6745
6746 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6747 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6748 blocked_vcpu_list) {
6749 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6750
6751 if (pi_test_on(pi_desc) == 1)
6752 kvm_vcpu_kick(vcpu);
6753 }
6754 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6755}
6756
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006757void vmx_enable_tdp(void)
6758{
6759 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
6760 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
6761 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
6762 0ull, VMX_EPT_EXECUTABLE_MASK,
6763 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05006764 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006765
6766 ept_set_mmio_spte_mask();
6767 kvm_enable_tdp();
6768}
6769
Tiejun Chenf2c76482014-10-28 10:14:47 +08006770static __init int hardware_setup(void)
6771{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006772 int r = -ENOMEM, i, msr;
6773
6774 rdmsrl_safe(MSR_EFER, &host_efer);
6775
6776 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6777 kvm_define_shared_msr(i, vmx_msr_index[i]);
6778
Radim Krčmář23611332016-09-29 22:41:33 +02006779 for (i = 0; i < VMX_BITMAP_NR; i++) {
6780 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
6781 if (!vmx_bitmap[i])
6782 goto out;
6783 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006784
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006785 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6786 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6787
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006788 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
6789 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
6790
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006791 if (setup_vmcs_config(&vmcs_config) < 0) {
6792 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02006793 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006794 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006795
6796 if (boot_cpu_has(X86_FEATURE_NX))
6797 kvm_enable_efer_bits(EFER_NX);
6798
Wanpeng Li08d839c2017-03-23 05:30:08 -07006799 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6800 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006801 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07006802
Tiejun Chenf2c76482014-10-28 10:14:47 +08006803 if (!cpu_has_vmx_shadow_vmcs())
6804 enable_shadow_vmcs = 0;
6805 if (enable_shadow_vmcs)
6806 init_vmcs_shadow_fields();
6807
6808 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02006809 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02006810 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07006811 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006812 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006813
Wanpeng Lifce6ac42017-05-11 02:58:56 -07006814 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006815 enable_ept_ad_bits = 0;
6816
Wanpeng Li8ad81822017-10-09 15:51:53 -07006817 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006818 enable_unrestricted_guest = 0;
6819
Paolo Bonziniad15a292015-01-30 16:18:49 +01006820 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006821 flexpriority_enabled = 0;
6822
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006823 if (!cpu_has_virtual_nmis())
6824 enable_vnmi = 0;
6825
Paolo Bonziniad15a292015-01-30 16:18:49 +01006826 /*
6827 * set_apic_access_page_addr() is used to reload apic access
6828 * page upon invalidation. No need to do anything if not
6829 * using the APIC_ACCESS_ADDR VMCS field.
6830 */
6831 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006832 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006833
6834 if (!cpu_has_vmx_tpr_shadow())
6835 kvm_x86_ops->update_cr8_intercept = NULL;
6836
6837 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6838 kvm_disable_largepages();
6839
Wanpeng Li0f107682017-09-28 18:06:24 -07006840 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08006841 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07006842 ple_window = 0;
6843 ple_window_grow = 0;
6844 ple_window_max = 0;
6845 ple_window_shrink = 0;
6846 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006847
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006848 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08006849 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006850 kvm_x86_ops->sync_pir_to_irr = NULL;
6851 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006852
Haozhong Zhang64903d62015-10-20 15:39:09 +08006853 if (cpu_has_vmx_tsc_scaling()) {
6854 kvm_has_tsc_control = true;
6855 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6856 kvm_tsc_scaling_ratio_frac_bits = 48;
6857 }
6858
Tiejun Chenbaa03522014-12-23 16:21:11 +08006859 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
6860 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
6861 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
6862 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
6863 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
6864 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006865
Wanpeng Lic63e4562016-09-23 19:17:16 +08006866 memcpy(vmx_msr_bitmap_legacy_x2apic_apicv,
6867 vmx_msr_bitmap_legacy, PAGE_SIZE);
6868 memcpy(vmx_msr_bitmap_longmode_x2apic_apicv,
6869 vmx_msr_bitmap_longmode, PAGE_SIZE);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006870 memcpy(vmx_msr_bitmap_legacy_x2apic,
6871 vmx_msr_bitmap_legacy, PAGE_SIZE);
6872 memcpy(vmx_msr_bitmap_longmode_x2apic,
6873 vmx_msr_bitmap_longmode, PAGE_SIZE);
6874
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006875 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6876
Radim Krčmář40d83382016-09-29 22:41:31 +02006877 for (msr = 0x800; msr <= 0x8ff; msr++) {
6878 if (msr == 0x839 /* TMCCT */)
6879 continue;
Radim Krčmář2e69f862016-09-29 22:41:32 +02006880 vmx_disable_intercept_msr_x2apic(msr, MSR_TYPE_R, true);
Radim Krčmář40d83382016-09-29 22:41:31 +02006881 }
Tiejun Chenbaa03522014-12-23 16:21:11 +08006882
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006883 /*
Radim Krčmář2e69f862016-09-29 22:41:32 +02006884 * TPR reads and writes can be virtualized even if virtual interrupt
6885 * delivery is not in use.
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006886 */
Radim Krčmář2e69f862016-09-29 22:41:32 +02006887 vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_W, true);
6888 vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_R | MSR_TYPE_W, false);
6889
Roman Kagan3ce424e2016-05-18 17:48:20 +03006890 /* EOI */
Radim Krčmář2e69f862016-09-29 22:41:32 +02006891 vmx_disable_intercept_msr_x2apic(0x80b, MSR_TYPE_W, true);
Roman Kagan3ce424e2016-05-18 17:48:20 +03006892 /* SELF-IPI */
Radim Krčmář2e69f862016-09-29 22:41:32 +02006893 vmx_disable_intercept_msr_x2apic(0x83f, MSR_TYPE_W, true);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006894
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006895 if (enable_ept)
6896 vmx_enable_tdp();
6897 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08006898 kvm_disable_tdp();
6899
6900 update_ple_window_actual_max();
6901
Kai Huang843e4332015-01-28 10:54:28 +08006902 /*
6903 * Only enable PML when hardware supports PML feature, and both EPT
6904 * and EPT A/D bit features are enabled -- PML depends on them to work.
6905 */
6906 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6907 enable_pml = 0;
6908
6909 if (!enable_pml) {
6910 kvm_x86_ops->slot_enable_log_dirty = NULL;
6911 kvm_x86_ops->slot_disable_log_dirty = NULL;
6912 kvm_x86_ops->flush_log_dirty = NULL;
6913 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6914 }
6915
Yunhong Jiang64672c92016-06-13 14:19:59 -07006916 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6917 u64 vmx_msr;
6918
6919 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6920 cpu_preemption_timer_multi =
6921 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6922 } else {
6923 kvm_x86_ops->set_hv_timer = NULL;
6924 kvm_x86_ops->cancel_hv_timer = NULL;
6925 }
6926
Feng Wubf9f6ac2015-09-18 22:29:55 +08006927 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6928
Ashok Rajc45dcc72016-06-22 14:59:56 +08006929 kvm_mce_cap_supported |= MCG_LMCE_P;
6930
Tiejun Chenf2c76482014-10-28 10:14:47 +08006931 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006932
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006933out:
Radim Krčmář23611332016-09-29 22:41:33 +02006934 for (i = 0; i < VMX_BITMAP_NR; i++)
6935 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006936
6937 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006938}
6939
6940static __exit void hardware_unsetup(void)
6941{
Radim Krčmář23611332016-09-29 22:41:33 +02006942 int i;
6943
6944 for (i = 0; i < VMX_BITMAP_NR; i++)
6945 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006946
Tiejun Chenf2c76482014-10-28 10:14:47 +08006947 free_kvm_area();
6948}
6949
Avi Kivity6aa8b732006-12-10 02:21:36 -08006950/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006951 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6952 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6953 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006954static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006955{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006956 if (ple_gap)
6957 grow_ple_window(vcpu);
6958
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08006959 /*
6960 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
6961 * VM-execution control is ignored if CPL > 0. OTOH, KVM
6962 * never set PAUSE_EXITING and just set PLE if supported,
6963 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
6964 */
6965 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006966 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006967}
6968
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006969static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006970{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006971 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006972}
6973
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006974static int handle_mwait(struct kvm_vcpu *vcpu)
6975{
6976 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6977 return handle_nop(vcpu);
6978}
6979
Jim Mattson45ec3682017-08-23 16:32:04 -07006980static int handle_invalid_op(struct kvm_vcpu *vcpu)
6981{
6982 kvm_queue_exception(vcpu, UD_VECTOR);
6983 return 1;
6984}
6985
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006986static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6987{
6988 return 1;
6989}
6990
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006991static int handle_monitor(struct kvm_vcpu *vcpu)
6992{
6993 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6994 return handle_nop(vcpu);
6995}
6996
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006997/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006998 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6999 * set the success or error code of an emulated VMX instruction, as specified
7000 * by Vol 2B, VMX Instruction Reference, "Conventions".
7001 */
7002static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7003{
7004 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7005 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7006 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7007}
7008
7009static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7010{
7011 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7012 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7013 X86_EFLAGS_SF | X86_EFLAGS_OF))
7014 | X86_EFLAGS_CF);
7015}
7016
Abel Gordon145c28d2013-04-18 14:36:55 +03007017static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007018 u32 vm_instruction_error)
7019{
7020 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7021 /*
7022 * failValid writes the error number to the current VMCS, which
7023 * can't be done there isn't a current VMCS.
7024 */
7025 nested_vmx_failInvalid(vcpu);
7026 return;
7027 }
7028 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7029 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7030 X86_EFLAGS_SF | X86_EFLAGS_OF))
7031 | X86_EFLAGS_ZF);
7032 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7033 /*
7034 * We don't need to force a shadow sync because
7035 * VM_INSTRUCTION_ERROR is not shadowed
7036 */
7037}
Abel Gordon145c28d2013-04-18 14:36:55 +03007038
Wincy Vanff651cb2014-12-11 08:52:58 +03007039static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7040{
7041 /* TODO: not to reset guest simply here. */
7042 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007043 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007044}
7045
Jan Kiszkaf41245002014-03-07 20:03:13 +01007046static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7047{
7048 struct vcpu_vmx *vmx =
7049 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7050
7051 vmx->nested.preemption_timer_expired = true;
7052 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7053 kvm_vcpu_kick(&vmx->vcpu);
7054
7055 return HRTIMER_NORESTART;
7056}
7057
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007058/*
Bandan Das19677e32014-05-06 02:19:15 -04007059 * Decode the memory-address operand of a vmx instruction, as recorded on an
7060 * exit caused by such an instruction (run by a guest hypervisor).
7061 * On success, returns 0. When the operand is invalid, returns 1 and throws
7062 * #UD or #GP.
7063 */
7064static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7065 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007066 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007067{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007068 gva_t off;
7069 bool exn;
7070 struct kvm_segment s;
7071
Bandan Das19677e32014-05-06 02:19:15 -04007072 /*
7073 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7074 * Execution", on an exit, vmx_instruction_info holds most of the
7075 * addressing components of the operand. Only the displacement part
7076 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7077 * For how an actual address is calculated from all these components,
7078 * refer to Vol. 1, "Operand Addressing".
7079 */
7080 int scaling = vmx_instruction_info & 3;
7081 int addr_size = (vmx_instruction_info >> 7) & 7;
7082 bool is_reg = vmx_instruction_info & (1u << 10);
7083 int seg_reg = (vmx_instruction_info >> 15) & 7;
7084 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7085 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7086 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7087 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7088
7089 if (is_reg) {
7090 kvm_queue_exception(vcpu, UD_VECTOR);
7091 return 1;
7092 }
7093
7094 /* Addr = segment_base + offset */
7095 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007096 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007097 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007098 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007099 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007100 off += kvm_register_read(vcpu, index_reg)<<scaling;
7101 vmx_get_segment(vcpu, &s, seg_reg);
7102 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007103
7104 if (addr_size == 1) /* 32 bit */
7105 *ret &= 0xffffffff;
7106
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007107 /* Checks for #GP/#SS exceptions. */
7108 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007109 if (is_long_mode(vcpu)) {
7110 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7111 * non-canonical form. This is the only check on the memory
7112 * destination for long mode!
7113 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007114 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007115 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007116 /* Protected mode: apply checks for segment validity in the
7117 * following order:
7118 * - segment type check (#GP(0) may be thrown)
7119 * - usability check (#GP(0)/#SS(0))
7120 * - limit check (#GP(0)/#SS(0))
7121 */
7122 if (wr)
7123 /* #GP(0) if the destination operand is located in a
7124 * read-only data segment or any code segment.
7125 */
7126 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7127 else
7128 /* #GP(0) if the source operand is located in an
7129 * execute-only code segment
7130 */
7131 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007132 if (exn) {
7133 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7134 return 1;
7135 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007136 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7137 */
7138 exn = (s.unusable != 0);
7139 /* Protected mode: #GP(0)/#SS(0) if the memory
7140 * operand is outside the segment limit.
7141 */
7142 exn = exn || (off + sizeof(u64) > s.limit);
7143 }
7144 if (exn) {
7145 kvm_queue_exception_e(vcpu,
7146 seg_reg == VCPU_SREG_SS ?
7147 SS_VECTOR : GP_VECTOR,
7148 0);
7149 return 1;
7150 }
7151
Bandan Das19677e32014-05-06 02:19:15 -04007152 return 0;
7153}
7154
Radim Krčmářcbf71272017-05-19 15:48:51 +02007155static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007156{
7157 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007158 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007159
7160 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007161 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007162 return 1;
7163
Radim Krčmářcbf71272017-05-19 15:48:51 +02007164 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7165 sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007166 kvm_inject_page_fault(vcpu, &e);
7167 return 1;
7168 }
7169
Bandan Das3573e222014-05-06 02:19:16 -04007170 return 0;
7171}
7172
Jim Mattsone29acc52016-11-30 12:03:43 -08007173static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7174{
7175 struct vcpu_vmx *vmx = to_vmx(vcpu);
7176 struct vmcs *shadow_vmcs;
7177
Jim Mattson00647b42017-11-27 17:22:25 -06007178 vmx->nested.vmcs02.vmcs = alloc_vmcs();
7179 vmx->nested.vmcs02.shadow_vmcs = NULL;
7180 if (!vmx->nested.vmcs02.vmcs)
7181 goto out_vmcs02;
7182 loaded_vmcs_init(&vmx->nested.vmcs02);
7183
Jim Mattsone29acc52016-11-30 12:03:43 -08007184 if (cpu_has_vmx_msr_bitmap()) {
7185 vmx->nested.msr_bitmap =
7186 (unsigned long *)__get_free_page(GFP_KERNEL);
7187 if (!vmx->nested.msr_bitmap)
7188 goto out_msr_bitmap;
7189 }
7190
7191 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7192 if (!vmx->nested.cached_vmcs12)
7193 goto out_cached_vmcs12;
7194
7195 if (enable_shadow_vmcs) {
7196 shadow_vmcs = alloc_vmcs();
7197 if (!shadow_vmcs)
7198 goto out_shadow_vmcs;
7199 /* mark vmcs as shadow */
7200 shadow_vmcs->revision_id |= (1u << 31);
7201 /* init shadow vmcs */
7202 vmcs_clear(shadow_vmcs);
7203 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7204 }
7205
Jim Mattsone29acc52016-11-30 12:03:43 -08007206 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7207 HRTIMER_MODE_REL_PINNED);
7208 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7209
7210 vmx->nested.vmxon = true;
7211 return 0;
7212
7213out_shadow_vmcs:
7214 kfree(vmx->nested.cached_vmcs12);
7215
7216out_cached_vmcs12:
7217 free_page((unsigned long)vmx->nested.msr_bitmap);
7218
7219out_msr_bitmap:
Mark Kanda276c7962017-11-27 17:22:26 -06007220 vmx_nested_free_vmcs02(vmx);
Jim Mattson00647b42017-11-27 17:22:25 -06007221
7222out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007223 return -ENOMEM;
7224}
7225
Bandan Das3573e222014-05-06 02:19:16 -04007226/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007227 * Emulate the VMXON instruction.
7228 * Currently, we just remember that VMX is active, and do not save or even
7229 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7230 * do not currently need to store anything in that guest-allocated memory
7231 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7232 * argument is different from the VMXON pointer (which the spec says they do).
7233 */
7234static int handle_vmon(struct kvm_vcpu *vcpu)
7235{
Jim Mattsone29acc52016-11-30 12:03:43 -08007236 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007237 gpa_t vmptr;
7238 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007239 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007240 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7241 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007242
Jim Mattson70f3aac2017-04-26 08:53:46 -07007243 /*
7244 * The Intel VMX Instruction Reference lists a bunch of bits that are
7245 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7246 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7247 * Otherwise, we should fail with #UD. But most faulting conditions
7248 * have already been checked by hardware, prior to the VM-exit for
7249 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7250 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007251 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007252 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007253 kvm_queue_exception(vcpu, UD_VECTOR);
7254 return 1;
7255 }
7256
Abel Gordon145c28d2013-04-18 14:36:55 +03007257 if (vmx->nested.vmxon) {
7258 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007259 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007260 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007261
Haozhong Zhang3b840802016-06-22 14:59:54 +08007262 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007263 != VMXON_NEEDED_FEATURES) {
7264 kvm_inject_gp(vcpu, 0);
7265 return 1;
7266 }
7267
Radim Krčmářcbf71272017-05-19 15:48:51 +02007268 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007269 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007270
7271 /*
7272 * SDM 3: 24.11.5
7273 * The first 4 bytes of VMXON region contain the supported
7274 * VMCS revision identifier
7275 *
7276 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7277 * which replaces physical address width with 32
7278 */
7279 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7280 nested_vmx_failInvalid(vcpu);
7281 return kvm_skip_emulated_instruction(vcpu);
7282 }
7283
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007284 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7285 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007286 nested_vmx_failInvalid(vcpu);
7287 return kvm_skip_emulated_instruction(vcpu);
7288 }
7289 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7290 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007291 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007292 nested_vmx_failInvalid(vcpu);
7293 return kvm_skip_emulated_instruction(vcpu);
7294 }
7295 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007296 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007297
7298 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007299 ret = enter_vmx_operation(vcpu);
7300 if (ret)
7301 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007302
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007303 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007304 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007305}
7306
7307/*
7308 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7309 * for running VMX instructions (except VMXON, whose prerequisites are
7310 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07007311 * Note that many of these exceptions have priority over VM exits, so they
7312 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007313 */
7314static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7315{
Jim Mattson70f3aac2017-04-26 08:53:46 -07007316 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007317 kvm_queue_exception(vcpu, UD_VECTOR);
7318 return 0;
7319 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007320 return 1;
7321}
7322
David Matlack8ca44e82017-08-01 14:00:39 -07007323static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7324{
7325 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7326 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7327}
7328
Abel Gordone7953d72013-04-18 14:37:55 +03007329static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7330{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007331 if (vmx->nested.current_vmptr == -1ull)
7332 return;
7333
Abel Gordon012f83c2013-04-18 14:39:25 +03007334 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007335 /* copy to memory all shadowed fields in case
7336 they were modified */
7337 copy_shadow_to_vmcs12(vmx);
7338 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07007339 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03007340 }
Wincy Van705699a2015-02-03 23:58:17 +08007341 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007342
7343 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007344 kvm_vcpu_write_guest_page(&vmx->vcpu,
7345 vmx->nested.current_vmptr >> PAGE_SHIFT,
7346 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07007347
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007348 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03007349}
7350
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007351/*
7352 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7353 * just stops using VMX.
7354 */
7355static void free_nested(struct vcpu_vmx *vmx)
7356{
Wanpeng Lib7455822017-11-22 14:04:00 -08007357 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007358 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007359
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007360 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08007361 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007362 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07007363 vmx->nested.posted_intr_nv = -1;
7364 vmx->nested.current_vmptr = -1ull;
Radim Krčmářd048c092016-08-08 20:16:22 +02007365 if (vmx->nested.msr_bitmap) {
7366 free_page((unsigned long)vmx->nested.msr_bitmap);
7367 vmx->nested.msr_bitmap = NULL;
7368 }
Jim Mattson355f4fb2016-10-28 08:29:39 -07007369 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07007370 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007371 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7372 free_vmcs(vmx->vmcs01.shadow_vmcs);
7373 vmx->vmcs01.shadow_vmcs = NULL;
7374 }
David Matlack4f2777b2016-07-13 17:16:37 -07007375 kfree(vmx->nested.cached_vmcs12);
Jim Mattson00647b42017-11-27 17:22:25 -06007376 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007377 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007378 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007379 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007380 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007381 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007382 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007383 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007384 }
Wincy Van705699a2015-02-03 23:58:17 +08007385 if (vmx->nested.pi_desc_page) {
7386 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007387 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08007388 vmx->nested.pi_desc_page = NULL;
7389 vmx->nested.pi_desc = NULL;
7390 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007391
Mark Kanda276c7962017-11-27 17:22:26 -06007392 vmx_nested_free_vmcs02(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007393}
7394
7395/* Emulate the VMXOFF instruction */
7396static int handle_vmoff(struct kvm_vcpu *vcpu)
7397{
7398 if (!nested_vmx_check_permission(vcpu))
7399 return 1;
7400 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007401 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007402 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007403}
7404
Nadav Har'El27d6c862011-05-25 23:06:59 +03007405/* Emulate the VMCLEAR instruction */
7406static int handle_vmclear(struct kvm_vcpu *vcpu)
7407{
7408 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08007409 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007410 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007411
7412 if (!nested_vmx_check_permission(vcpu))
7413 return 1;
7414
Radim Krčmářcbf71272017-05-19 15:48:51 +02007415 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007416 return 1;
7417
Radim Krčmářcbf71272017-05-19 15:48:51 +02007418 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7419 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7420 return kvm_skip_emulated_instruction(vcpu);
7421 }
7422
7423 if (vmptr == vmx->nested.vmxon_ptr) {
7424 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7425 return kvm_skip_emulated_instruction(vcpu);
7426 }
7427
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007428 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007429 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007430
Jim Mattson587d7e722017-03-02 12:41:48 -08007431 kvm_vcpu_write_guest(vcpu,
7432 vmptr + offsetof(struct vmcs12, launch_state),
7433 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007434
Nadav Har'El27d6c862011-05-25 23:06:59 +03007435 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007436 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007437}
7438
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007439static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7440
7441/* Emulate the VMLAUNCH instruction */
7442static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7443{
7444 return nested_vmx_run(vcpu, true);
7445}
7446
7447/* Emulate the VMRESUME instruction */
7448static int handle_vmresume(struct kvm_vcpu *vcpu)
7449{
7450
7451 return nested_vmx_run(vcpu, false);
7452}
7453
Nadav Har'El49f705c2011-05-25 23:08:30 +03007454/*
7455 * Read a vmcs12 field. Since these can have varying lengths and we return
7456 * one type, we chose the biggest type (u64) and zero-extend the return value
7457 * to that size. Note that the caller, handle_vmread, might need to use only
7458 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7459 * 64-bit fields are to be returned).
7460 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007461static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7462 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007463{
7464 short offset = vmcs_field_to_offset(field);
7465 char *p;
7466
7467 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007468 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007469
7470 p = ((char *)(get_vmcs12(vcpu))) + offset;
7471
7472 switch (vmcs_field_type(field)) {
7473 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7474 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007475 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007476 case VMCS_FIELD_TYPE_U16:
7477 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007478 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007479 case VMCS_FIELD_TYPE_U32:
7480 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007481 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007482 case VMCS_FIELD_TYPE_U64:
7483 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007484 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007485 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007486 WARN_ON(1);
7487 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007488 }
7489}
7490
Abel Gordon20b97fe2013-04-18 14:36:25 +03007491
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007492static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7493 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007494 short offset = vmcs_field_to_offset(field);
7495 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7496 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007497 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007498
7499 switch (vmcs_field_type(field)) {
7500 case VMCS_FIELD_TYPE_U16:
7501 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007502 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007503 case VMCS_FIELD_TYPE_U32:
7504 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007505 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007506 case VMCS_FIELD_TYPE_U64:
7507 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007508 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007509 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7510 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007511 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007512 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007513 WARN_ON(1);
7514 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007515 }
7516
7517}
7518
Abel Gordon16f5b902013-04-18 14:38:25 +03007519static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7520{
7521 int i;
7522 unsigned long field;
7523 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007524 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007525 const unsigned long *fields = shadow_read_write_fields;
7526 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007527
Jan Kiszka282da872014-10-08 18:05:39 +02007528 preempt_disable();
7529
Abel Gordon16f5b902013-04-18 14:38:25 +03007530 vmcs_load(shadow_vmcs);
7531
7532 for (i = 0; i < num_fields; i++) {
7533 field = fields[i];
7534 switch (vmcs_field_type(field)) {
7535 case VMCS_FIELD_TYPE_U16:
7536 field_value = vmcs_read16(field);
7537 break;
7538 case VMCS_FIELD_TYPE_U32:
7539 field_value = vmcs_read32(field);
7540 break;
7541 case VMCS_FIELD_TYPE_U64:
7542 field_value = vmcs_read64(field);
7543 break;
7544 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7545 field_value = vmcs_readl(field);
7546 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007547 default:
7548 WARN_ON(1);
7549 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007550 }
7551 vmcs12_write_any(&vmx->vcpu, field, field_value);
7552 }
7553
7554 vmcs_clear(shadow_vmcs);
7555 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007556
7557 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007558}
7559
Abel Gordonc3114422013-04-18 14:38:55 +03007560static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7561{
Mathias Krausec2bae892013-06-26 20:36:21 +02007562 const unsigned long *fields[] = {
7563 shadow_read_write_fields,
7564 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007565 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007566 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007567 max_shadow_read_write_fields,
7568 max_shadow_read_only_fields
7569 };
7570 int i, q;
7571 unsigned long field;
7572 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007573 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007574
7575 vmcs_load(shadow_vmcs);
7576
Mathias Krausec2bae892013-06-26 20:36:21 +02007577 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007578 for (i = 0; i < max_fields[q]; i++) {
7579 field = fields[q][i];
7580 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7581
7582 switch (vmcs_field_type(field)) {
7583 case VMCS_FIELD_TYPE_U16:
7584 vmcs_write16(field, (u16)field_value);
7585 break;
7586 case VMCS_FIELD_TYPE_U32:
7587 vmcs_write32(field, (u32)field_value);
7588 break;
7589 case VMCS_FIELD_TYPE_U64:
7590 vmcs_write64(field, (u64)field_value);
7591 break;
7592 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7593 vmcs_writel(field, (long)field_value);
7594 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007595 default:
7596 WARN_ON(1);
7597 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007598 }
7599 }
7600 }
7601
7602 vmcs_clear(shadow_vmcs);
7603 vmcs_load(vmx->loaded_vmcs->vmcs);
7604}
7605
Nadav Har'El49f705c2011-05-25 23:08:30 +03007606/*
7607 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7608 * used before) all generate the same failure when it is missing.
7609 */
7610static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7611{
7612 struct vcpu_vmx *vmx = to_vmx(vcpu);
7613 if (vmx->nested.current_vmptr == -1ull) {
7614 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007615 return 0;
7616 }
7617 return 1;
7618}
7619
7620static int handle_vmread(struct kvm_vcpu *vcpu)
7621{
7622 unsigned long field;
7623 u64 field_value;
7624 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7625 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7626 gva_t gva = 0;
7627
Kyle Hueyeb277562016-11-29 12:40:39 -08007628 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007629 return 1;
7630
Kyle Huey6affcbe2016-11-29 12:40:40 -08007631 if (!nested_vmx_check_vmcs12(vcpu))
7632 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007633
Nadav Har'El49f705c2011-05-25 23:08:30 +03007634 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007635 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007636 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007637 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007638 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007639 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007640 }
7641 /*
7642 * Now copy part of this value to register or memory, as requested.
7643 * Note that the number of bits actually copied is 32 or 64 depending
7644 * on the guest's mode (32 or 64 bit), not on the given field's length.
7645 */
7646 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007647 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007648 field_value);
7649 } else {
7650 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007651 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007652 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07007653 /* _system ok, as hardware has verified cpl=0 */
Nadav Har'El49f705c2011-05-25 23:08:30 +03007654 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7655 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7656 }
7657
7658 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007659 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007660}
7661
7662
7663static int handle_vmwrite(struct kvm_vcpu *vcpu)
7664{
7665 unsigned long field;
7666 gva_t gva;
7667 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7668 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007669 /* The value to write might be 32 or 64 bits, depending on L1's long
7670 * mode, and eventually we need to write that into a field of several
7671 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007672 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007673 * bits into the vmcs12 field.
7674 */
7675 u64 field_value = 0;
7676 struct x86_exception e;
7677
Kyle Hueyeb277562016-11-29 12:40:39 -08007678 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007679 return 1;
7680
Kyle Huey6affcbe2016-11-29 12:40:40 -08007681 if (!nested_vmx_check_vmcs12(vcpu))
7682 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007683
Nadav Har'El49f705c2011-05-25 23:08:30 +03007684 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007685 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007686 (((vmx_instruction_info) >> 3) & 0xf));
7687 else {
7688 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007689 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007690 return 1;
7691 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007692 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007693 kvm_inject_page_fault(vcpu, &e);
7694 return 1;
7695 }
7696 }
7697
7698
Nadav Amit27e6fb52014-06-18 17:19:26 +03007699 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007700 if (vmcs_field_readonly(field)) {
7701 nested_vmx_failValid(vcpu,
7702 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007703 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007704 }
7705
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007706 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007707 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007708 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007709 }
7710
7711 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007712 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007713}
7714
Jim Mattsona8bc2842016-11-30 12:03:44 -08007715static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
7716{
7717 vmx->nested.current_vmptr = vmptr;
7718 if (enable_shadow_vmcs) {
7719 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7720 SECONDARY_EXEC_SHADOW_VMCS);
7721 vmcs_write64(VMCS_LINK_POINTER,
7722 __pa(vmx->vmcs01.shadow_vmcs));
7723 vmx->nested.sync_shadow_vmcs = true;
7724 }
7725}
7726
Nadav Har'El63846662011-05-25 23:07:29 +03007727/* Emulate the VMPTRLD instruction */
7728static int handle_vmptrld(struct kvm_vcpu *vcpu)
7729{
7730 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007731 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007732
7733 if (!nested_vmx_check_permission(vcpu))
7734 return 1;
7735
Radim Krčmářcbf71272017-05-19 15:48:51 +02007736 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007737 return 1;
7738
Radim Krčmářcbf71272017-05-19 15:48:51 +02007739 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7740 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
7741 return kvm_skip_emulated_instruction(vcpu);
7742 }
7743
7744 if (vmptr == vmx->nested.vmxon_ptr) {
7745 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
7746 return kvm_skip_emulated_instruction(vcpu);
7747 }
7748
Nadav Har'El63846662011-05-25 23:07:29 +03007749 if (vmx->nested.current_vmptr != vmptr) {
7750 struct vmcs12 *new_vmcs12;
7751 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007752 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7753 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03007754 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007755 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007756 }
7757 new_vmcs12 = kmap(page);
7758 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7759 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007760 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03007761 nested_vmx_failValid(vcpu,
7762 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007763 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007764 }
Nadav Har'El63846662011-05-25 23:07:29 +03007765
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007766 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07007767 /*
7768 * Load VMCS12 from guest memory since it is not already
7769 * cached.
7770 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007771 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
7772 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007773 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007774
Jim Mattsona8bc2842016-11-30 12:03:44 -08007775 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03007776 }
7777
7778 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007779 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007780}
7781
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007782/* Emulate the VMPTRST instruction */
7783static int handle_vmptrst(struct kvm_vcpu *vcpu)
7784{
7785 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7786 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7787 gva_t vmcs_gva;
7788 struct x86_exception e;
7789
7790 if (!nested_vmx_check_permission(vcpu))
7791 return 1;
7792
7793 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007794 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007795 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07007796 /* ok to use *_system, as hardware has verified cpl=0 */
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007797 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7798 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7799 sizeof(u64), &e)) {
7800 kvm_inject_page_fault(vcpu, &e);
7801 return 1;
7802 }
7803 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007804 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007805}
7806
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007807/* Emulate the INVEPT instruction */
7808static int handle_invept(struct kvm_vcpu *vcpu)
7809{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007810 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007811 u32 vmx_instruction_info, types;
7812 unsigned long type;
7813 gva_t gva;
7814 struct x86_exception e;
7815 struct {
7816 u64 eptp, gpa;
7817 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007818
Wincy Vanb9c237b2015-02-03 23:56:30 +08007819 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7820 SECONDARY_EXEC_ENABLE_EPT) ||
7821 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007822 kvm_queue_exception(vcpu, UD_VECTOR);
7823 return 1;
7824 }
7825
7826 if (!nested_vmx_check_permission(vcpu))
7827 return 1;
7828
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007829 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007830 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007831
Wincy Vanb9c237b2015-02-03 23:56:30 +08007832 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007833
Jim Mattson85c856b2016-10-26 08:38:38 -07007834 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007835 nested_vmx_failValid(vcpu,
7836 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007837 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007838 }
7839
7840 /* According to the Intel VMX instruction reference, the memory
7841 * operand is read even if it isn't needed (e.g., for type==global)
7842 */
7843 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007844 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007845 return 1;
7846 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7847 sizeof(operand), &e)) {
7848 kvm_inject_page_fault(vcpu, &e);
7849 return 1;
7850 }
7851
7852 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007853 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007854 /*
7855 * TODO: track mappings and invalidate
7856 * single context requests appropriately
7857 */
7858 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007859 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007860 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007861 nested_vmx_succeed(vcpu);
7862 break;
7863 default:
7864 BUG_ON(1);
7865 break;
7866 }
7867
Kyle Huey6affcbe2016-11-29 12:40:40 -08007868 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007869}
7870
Petr Matouseka642fc32014-09-23 20:22:30 +02007871static int handle_invvpid(struct kvm_vcpu *vcpu)
7872{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007873 struct vcpu_vmx *vmx = to_vmx(vcpu);
7874 u32 vmx_instruction_info;
7875 unsigned long type, types;
7876 gva_t gva;
7877 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07007878 struct {
7879 u64 vpid;
7880 u64 gla;
7881 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007882
7883 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7884 SECONDARY_EXEC_ENABLE_VPID) ||
7885 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7886 kvm_queue_exception(vcpu, UD_VECTOR);
7887 return 1;
7888 }
7889
7890 if (!nested_vmx_check_permission(vcpu))
7891 return 1;
7892
7893 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7894 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7895
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007896 types = (vmx->nested.nested_vmx_vpid_caps &
7897 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007898
Jim Mattson85c856b2016-10-26 08:38:38 -07007899 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007900 nested_vmx_failValid(vcpu,
7901 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007902 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007903 }
7904
7905 /* according to the intel vmx instruction reference, the memory
7906 * operand is read even if it isn't needed (e.g., for type==global)
7907 */
7908 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7909 vmx_instruction_info, false, &gva))
7910 return 1;
Jim Mattson40352602017-06-28 09:37:37 -07007911 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7912 sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007913 kvm_inject_page_fault(vcpu, &e);
7914 return 1;
7915 }
Jim Mattson40352602017-06-28 09:37:37 -07007916 if (operand.vpid >> 16) {
7917 nested_vmx_failValid(vcpu,
7918 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7919 return kvm_skip_emulated_instruction(vcpu);
7920 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007921
7922 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007923 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Yu Zhangfd8cb432017-08-24 20:27:56 +08007924 if (is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07007925 nested_vmx_failValid(vcpu,
7926 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7927 return kvm_skip_emulated_instruction(vcpu);
7928 }
7929 /* fall through */
Paolo Bonzinief697a72016-03-18 16:58:38 +01007930 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007931 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07007932 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007933 nested_vmx_failValid(vcpu,
7934 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007935 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007936 }
7937 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007938 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007939 break;
7940 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007941 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007942 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007943 }
7944
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08007945 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007946 nested_vmx_succeed(vcpu);
7947
Kyle Huey6affcbe2016-11-29 12:40:40 -08007948 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007949}
7950
Kai Huang843e4332015-01-28 10:54:28 +08007951static int handle_pml_full(struct kvm_vcpu *vcpu)
7952{
7953 unsigned long exit_qualification;
7954
7955 trace_kvm_pml_full(vcpu->vcpu_id);
7956
7957 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7958
7959 /*
7960 * PML buffer FULL happened while executing iret from NMI,
7961 * "blocked by NMI" bit has to be set before next VM entry.
7962 */
7963 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007964 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08007965 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7966 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7967 GUEST_INTR_STATE_NMI);
7968
7969 /*
7970 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7971 * here.., and there's no userspace involvement needed for PML.
7972 */
7973 return 1;
7974}
7975
Yunhong Jiang64672c92016-06-13 14:19:59 -07007976static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7977{
7978 kvm_lapic_expired_hv_timer(vcpu);
7979 return 1;
7980}
7981
Bandan Das41ab9372017-08-03 15:54:43 -04007982static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
7983{
7984 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04007985 int maxphyaddr = cpuid_maxphyaddr(vcpu);
7986
7987 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02007988 switch (address & VMX_EPTP_MT_MASK) {
7989 case VMX_EPTP_MT_UC:
Bandan Das41ab9372017-08-03 15:54:43 -04007990 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_UC_BIT))
7991 return false;
7992 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02007993 case VMX_EPTP_MT_WB:
Bandan Das41ab9372017-08-03 15:54:43 -04007994 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_WB_BIT))
7995 return false;
7996 break;
7997 default:
7998 return false;
7999 }
8000
David Hildenbrandbb97a012017-08-10 23:15:28 +02008001 /* only 4 levels page-walk length are valid */
8002 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008003 return false;
8004
8005 /* Reserved bits should not be set */
8006 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8007 return false;
8008
8009 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008010 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Bandan Das41ab9372017-08-03 15:54:43 -04008011 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPT_AD_BIT))
8012 return false;
8013 }
8014
8015 return true;
8016}
8017
8018static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8019 struct vmcs12 *vmcs12)
8020{
8021 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8022 u64 address;
8023 bool accessed_dirty;
8024 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8025
8026 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8027 !nested_cpu_has_ept(vmcs12))
8028 return 1;
8029
8030 if (index >= VMFUNC_EPTP_ENTRIES)
8031 return 1;
8032
8033
8034 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8035 &address, index * 8, 8))
8036 return 1;
8037
David Hildenbrandbb97a012017-08-10 23:15:28 +02008038 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008039
8040 /*
8041 * If the (L2) guest does a vmfunc to the currently
8042 * active ept pointer, we don't have to do anything else
8043 */
8044 if (vmcs12->ept_pointer != address) {
8045 if (!valid_ept_address(vcpu, address))
8046 return 1;
8047
8048 kvm_mmu_unload(vcpu);
8049 mmu->ept_ad = accessed_dirty;
8050 mmu->base_role.ad_disabled = !accessed_dirty;
8051 vmcs12->ept_pointer = address;
8052 /*
8053 * TODO: Check what's the correct approach in case
8054 * mmu reload fails. Currently, we just let the next
8055 * reload potentially fail
8056 */
8057 kvm_mmu_reload(vcpu);
8058 }
8059
8060 return 0;
8061}
8062
Bandan Das2a499e42017-08-03 15:54:41 -04008063static int handle_vmfunc(struct kvm_vcpu *vcpu)
8064{
Bandan Das27c42a12017-08-03 15:54:42 -04008065 struct vcpu_vmx *vmx = to_vmx(vcpu);
8066 struct vmcs12 *vmcs12;
8067 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8068
8069 /*
8070 * VMFUNC is only supported for nested guests, but we always enable the
8071 * secondary control for simplicity; for non-nested mode, fake that we
8072 * didn't by injecting #UD.
8073 */
8074 if (!is_guest_mode(vcpu)) {
8075 kvm_queue_exception(vcpu, UD_VECTOR);
8076 return 1;
8077 }
8078
8079 vmcs12 = get_vmcs12(vcpu);
8080 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8081 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008082
8083 switch (function) {
8084 case 0:
8085 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8086 goto fail;
8087 break;
8088 default:
8089 goto fail;
8090 }
8091 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008092
8093fail:
8094 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8095 vmcs_read32(VM_EXIT_INTR_INFO),
8096 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008097 return 1;
8098}
8099
Nadav Har'El0140cae2011-05-25 23:06:28 +03008100/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008101 * The exit handlers return 1 if the exit was handled fully and guest execution
8102 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8103 * to be done to userspace and return 0.
8104 */
Mathias Krause772e0312012-08-30 01:30:19 +02008105static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008106 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8107 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008108 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008109 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008110 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008111 [EXIT_REASON_CR_ACCESS] = handle_cr,
8112 [EXIT_REASON_DR_ACCESS] = handle_dr,
8113 [EXIT_REASON_CPUID] = handle_cpuid,
8114 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8115 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8116 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8117 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008118 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008119 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008120 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008121 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008122 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008123 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008124 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008125 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008126 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008127 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008128 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008129 [EXIT_REASON_VMOFF] = handle_vmoff,
8130 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008131 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8132 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008133 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008134 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008135 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008136 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008137 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008138 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008139 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8140 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008141 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8142 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008143 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008144 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008145 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008146 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008147 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008148 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008149 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008150 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008151 [EXIT_REASON_XSAVES] = handle_xsaves,
8152 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008153 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008154 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008155 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008156};
8157
8158static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008159 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008160
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008161static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8162 struct vmcs12 *vmcs12)
8163{
8164 unsigned long exit_qualification;
8165 gpa_t bitmap, last_bitmap;
8166 unsigned int port;
8167 int size;
8168 u8 b;
8169
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008170 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008171 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008172
8173 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8174
8175 port = exit_qualification >> 16;
8176 size = (exit_qualification & 7) + 1;
8177
8178 last_bitmap = (gpa_t)-1;
8179 b = -1;
8180
8181 while (size > 0) {
8182 if (port < 0x8000)
8183 bitmap = vmcs12->io_bitmap_a;
8184 else if (port < 0x10000)
8185 bitmap = vmcs12->io_bitmap_b;
8186 else
Joe Perches1d804d02015-03-30 16:46:09 -07008187 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008188 bitmap += (port & 0x7fff) / 8;
8189
8190 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008191 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008192 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008193 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008194 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008195
8196 port++;
8197 size--;
8198 last_bitmap = bitmap;
8199 }
8200
Joe Perches1d804d02015-03-30 16:46:09 -07008201 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008202}
8203
Nadav Har'El644d7112011-05-25 23:12:35 +03008204/*
8205 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8206 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8207 * disinterest in the current event (read or write a specific MSR) by using an
8208 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8209 */
8210static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8211 struct vmcs12 *vmcs12, u32 exit_reason)
8212{
8213 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8214 gpa_t bitmap;
8215
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008216 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008217 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008218
8219 /*
8220 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8221 * for the four combinations of read/write and low/high MSR numbers.
8222 * First we need to figure out which of the four to use:
8223 */
8224 bitmap = vmcs12->msr_bitmap;
8225 if (exit_reason == EXIT_REASON_MSR_WRITE)
8226 bitmap += 2048;
8227 if (msr_index >= 0xc0000000) {
8228 msr_index -= 0xc0000000;
8229 bitmap += 1024;
8230 }
8231
8232 /* Then read the msr_index'th bit from this bitmap: */
8233 if (msr_index < 1024*8) {
8234 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008235 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008236 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008237 return 1 & (b >> (msr_index & 7));
8238 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008239 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008240}
8241
8242/*
8243 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8244 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8245 * intercept (via guest_host_mask etc.) the current event.
8246 */
8247static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8248 struct vmcs12 *vmcs12)
8249{
8250 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8251 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008252 int reg;
8253 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008254
8255 switch ((exit_qualification >> 4) & 3) {
8256 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008257 reg = (exit_qualification >> 8) & 15;
8258 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008259 switch (cr) {
8260 case 0:
8261 if (vmcs12->cr0_guest_host_mask &
8262 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008263 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008264 break;
8265 case 3:
8266 if ((vmcs12->cr3_target_count >= 1 &&
8267 vmcs12->cr3_target_value0 == val) ||
8268 (vmcs12->cr3_target_count >= 2 &&
8269 vmcs12->cr3_target_value1 == val) ||
8270 (vmcs12->cr3_target_count >= 3 &&
8271 vmcs12->cr3_target_value2 == val) ||
8272 (vmcs12->cr3_target_count >= 4 &&
8273 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008274 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008275 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008276 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008277 break;
8278 case 4:
8279 if (vmcs12->cr4_guest_host_mask &
8280 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008281 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008282 break;
8283 case 8:
8284 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008285 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008286 break;
8287 }
8288 break;
8289 case 2: /* clts */
8290 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8291 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008292 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008293 break;
8294 case 1: /* mov from cr */
8295 switch (cr) {
8296 case 3:
8297 if (vmcs12->cpu_based_vm_exec_control &
8298 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008299 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008300 break;
8301 case 8:
8302 if (vmcs12->cpu_based_vm_exec_control &
8303 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008304 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008305 break;
8306 }
8307 break;
8308 case 3: /* lmsw */
8309 /*
8310 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8311 * cr0. Other attempted changes are ignored, with no exit.
8312 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008313 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008314 if (vmcs12->cr0_guest_host_mask & 0xe &
8315 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008316 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008317 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8318 !(vmcs12->cr0_read_shadow & 0x1) &&
8319 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008320 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008321 break;
8322 }
Joe Perches1d804d02015-03-30 16:46:09 -07008323 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008324}
8325
8326/*
8327 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8328 * should handle it ourselves in L0 (and then continue L2). Only call this
8329 * when in is_guest_mode (L2).
8330 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02008331static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03008332{
Nadav Har'El644d7112011-05-25 23:12:35 +03008333 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8334 struct vcpu_vmx *vmx = to_vmx(vcpu);
8335 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8336
Jim Mattson4f350c62017-09-14 16:31:44 -07008337 if (vmx->nested.nested_run_pending)
8338 return false;
8339
8340 if (unlikely(vmx->fail)) {
8341 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8342 vmcs_read32(VM_INSTRUCTION_ERROR));
8343 return true;
8344 }
Jan Kiszka542060e2014-01-04 18:47:21 +01008345
David Matlackc9f04402017-08-01 14:00:40 -07008346 /*
8347 * The host physical addresses of some pages of guest memory
Jim Mattson00647b42017-11-27 17:22:25 -06008348 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8349 * Page). The CPU may write to these pages via their host
8350 * physical address while L2 is running, bypassing any
8351 * address-translation-based dirty tracking (e.g. EPT write
8352 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07008353 *
8354 * Mark them dirty on every exit from L2 to prevent them from
8355 * getting out of sync with dirty tracking.
8356 */
8357 nested_mark_vmcs12_pages_dirty(vcpu);
8358
Jim Mattson4f350c62017-09-14 16:31:44 -07008359 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8360 vmcs_readl(EXIT_QUALIFICATION),
8361 vmx->idt_vectoring_info,
8362 intr_info,
8363 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8364 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03008365
8366 switch (exit_reason) {
8367 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008368 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008369 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008370 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07008371 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008372 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008373 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008374 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008375 else if (is_debug(intr_info) &&
8376 vcpu->guest_debug &
8377 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8378 return false;
8379 else if (is_breakpoint(intr_info) &&
8380 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8381 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008382 return vmcs12->exception_bitmap &
8383 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8384 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008385 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008386 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008387 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008388 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008389 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008390 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008391 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008392 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008393 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008394 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008395 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008396 case EXIT_REASON_HLT:
8397 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8398 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008399 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008400 case EXIT_REASON_INVLPG:
8401 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8402 case EXIT_REASON_RDPMC:
8403 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008404 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008405 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008406 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008407 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008408 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008409 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8410 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8411 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8412 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8413 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8414 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008415 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008416 /*
8417 * VMX instructions trap unconditionally. This allows L1 to
8418 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8419 */
Joe Perches1d804d02015-03-30 16:46:09 -07008420 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008421 case EXIT_REASON_CR_ACCESS:
8422 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8423 case EXIT_REASON_DR_ACCESS:
8424 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8425 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008426 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008427 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8428 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03008429 case EXIT_REASON_MSR_READ:
8430 case EXIT_REASON_MSR_WRITE:
8431 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8432 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008433 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008434 case EXIT_REASON_MWAIT_INSTRUCTION:
8435 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008436 case EXIT_REASON_MONITOR_TRAP_FLAG:
8437 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008438 case EXIT_REASON_MONITOR_INSTRUCTION:
8439 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8440 case EXIT_REASON_PAUSE_INSTRUCTION:
8441 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8442 nested_cpu_has2(vmcs12,
8443 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8444 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008445 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008446 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008447 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008448 case EXIT_REASON_APIC_ACCESS:
8449 return nested_cpu_has2(vmcs12,
8450 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008451 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008452 case EXIT_REASON_EOI_INDUCED:
8453 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008454 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008455 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008456 /*
8457 * L0 always deals with the EPT violation. If nested EPT is
8458 * used, and the nested mmu code discovers that the address is
8459 * missing in the guest EPT table (EPT12), the EPT violation
8460 * will be injected with nested_ept_inject_page_fault()
8461 */
Joe Perches1d804d02015-03-30 16:46:09 -07008462 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008463 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008464 /*
8465 * L2 never uses directly L1's EPT, but rather L0's own EPT
8466 * table (shadow on EPT) or a merged EPT table that L0 built
8467 * (EPT on EPT). So any problems with the structure of the
8468 * table is L0's fault.
8469 */
Joe Perches1d804d02015-03-30 16:46:09 -07008470 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02008471 case EXIT_REASON_INVPCID:
8472 return
8473 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8474 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008475 case EXIT_REASON_WBINVD:
8476 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8477 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008478 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008479 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8480 /*
8481 * This should never happen, since it is not possible to
8482 * set XSS to a non-zero value---neither in L1 nor in L2.
8483 * If if it were, XSS would have to be checked against
8484 * the XSS exit bitmap in vmcs12.
8485 */
8486 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008487 case EXIT_REASON_PREEMPTION_TIMER:
8488 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02008489 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04008490 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02008491 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04008492 case EXIT_REASON_VMFUNC:
8493 /* VM functions are emulated through L2->L0 vmexits. */
8494 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008495 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008496 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008497 }
8498}
8499
Paolo Bonzini7313c692017-07-27 10:31:25 +02008500static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8501{
8502 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8503
8504 /*
8505 * At this point, the exit interruption info in exit_intr_info
8506 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8507 * we need to query the in-kernel LAPIC.
8508 */
8509 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8510 if ((exit_intr_info &
8511 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8512 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8513 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8514 vmcs12->vm_exit_intr_error_code =
8515 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8516 }
8517
8518 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8519 vmcs_readl(EXIT_QUALIFICATION));
8520 return 1;
8521}
8522
Avi Kivity586f9602010-11-18 13:09:54 +02008523static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8524{
8525 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8526 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8527}
8528
Kai Huanga3eaa862015-11-04 13:46:05 +08008529static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008530{
Kai Huanga3eaa862015-11-04 13:46:05 +08008531 if (vmx->pml_pg) {
8532 __free_page(vmx->pml_pg);
8533 vmx->pml_pg = NULL;
8534 }
Kai Huang843e4332015-01-28 10:54:28 +08008535}
8536
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008537static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008538{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008539 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008540 u64 *pml_buf;
8541 u16 pml_idx;
8542
8543 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8544
8545 /* Do nothing if PML buffer is empty */
8546 if (pml_idx == (PML_ENTITY_NUM - 1))
8547 return;
8548
8549 /* PML index always points to next available PML buffer entity */
8550 if (pml_idx >= PML_ENTITY_NUM)
8551 pml_idx = 0;
8552 else
8553 pml_idx++;
8554
8555 pml_buf = page_address(vmx->pml_pg);
8556 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8557 u64 gpa;
8558
8559 gpa = pml_buf[pml_idx];
8560 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008561 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008562 }
8563
8564 /* reset PML index */
8565 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8566}
8567
8568/*
8569 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8570 * Called before reporting dirty_bitmap to userspace.
8571 */
8572static void kvm_flush_pml_buffers(struct kvm *kvm)
8573{
8574 int i;
8575 struct kvm_vcpu *vcpu;
8576 /*
8577 * We only need to kick vcpu out of guest mode here, as PML buffer
8578 * is flushed at beginning of all VMEXITs, and it's obvious that only
8579 * vcpus running in guest are possible to have unflushed GPAs in PML
8580 * buffer.
8581 */
8582 kvm_for_each_vcpu(i, vcpu, kvm)
8583 kvm_vcpu_kick(vcpu);
8584}
8585
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008586static void vmx_dump_sel(char *name, uint32_t sel)
8587{
8588 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05008589 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008590 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8591 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8592 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8593}
8594
8595static void vmx_dump_dtsel(char *name, uint32_t limit)
8596{
8597 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8598 name, vmcs_read32(limit),
8599 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8600}
8601
8602static void dump_vmcs(void)
8603{
8604 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8605 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8606 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8607 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8608 u32 secondary_exec_control = 0;
8609 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008610 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008611 int i, n;
8612
8613 if (cpu_has_secondary_exec_ctrls())
8614 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8615
8616 pr_err("*** Guest State ***\n");
8617 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8618 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8619 vmcs_readl(CR0_GUEST_HOST_MASK));
8620 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8621 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8622 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8623 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8624 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8625 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008626 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8627 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8628 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8629 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008630 }
8631 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8632 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8633 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8634 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8635 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8636 vmcs_readl(GUEST_SYSENTER_ESP),
8637 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8638 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8639 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8640 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8641 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8642 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8643 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8644 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8645 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8646 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8647 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8648 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8649 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008650 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8651 efer, vmcs_read64(GUEST_IA32_PAT));
8652 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8653 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008654 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8655 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008656 pr_err("PerfGlobCtl = 0x%016llx\n",
8657 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008658 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008659 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008660 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8661 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8662 vmcs_read32(GUEST_ACTIVITY_STATE));
8663 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8664 pr_err("InterruptStatus = %04x\n",
8665 vmcs_read16(GUEST_INTR_STATUS));
8666
8667 pr_err("*** Host State ***\n");
8668 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8669 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8670 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8671 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8672 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8673 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8674 vmcs_read16(HOST_TR_SELECTOR));
8675 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8676 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8677 vmcs_readl(HOST_TR_BASE));
8678 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8679 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8680 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8681 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8682 vmcs_readl(HOST_CR4));
8683 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8684 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8685 vmcs_read32(HOST_IA32_SYSENTER_CS),
8686 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8687 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008688 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8689 vmcs_read64(HOST_IA32_EFER),
8690 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008691 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008692 pr_err("PerfGlobCtl = 0x%016llx\n",
8693 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008694
8695 pr_err("*** Control State ***\n");
8696 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8697 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8698 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8699 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8700 vmcs_read32(EXCEPTION_BITMAP),
8701 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8702 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8703 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8704 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8705 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8706 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8707 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8708 vmcs_read32(VM_EXIT_INTR_INFO),
8709 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8710 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8711 pr_err(" reason=%08x qualification=%016lx\n",
8712 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8713 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8714 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8715 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008716 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008717 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008718 pr_err("TSC Multiplier = 0x%016llx\n",
8719 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008720 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8721 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8722 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8723 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8724 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008725 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008726 n = vmcs_read32(CR3_TARGET_COUNT);
8727 for (i = 0; i + 1 < n; i += 4)
8728 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8729 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8730 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8731 if (i < n)
8732 pr_err("CR3 target%u=%016lx\n",
8733 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8734 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8735 pr_err("PLE Gap=%08x Window=%08x\n",
8736 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8737 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8738 pr_err("Virtual processor ID = 0x%04x\n",
8739 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8740}
8741
Avi Kivity6aa8b732006-12-10 02:21:36 -08008742/*
8743 * The guest has exited. See if we can fix it or if we need userspace
8744 * assistance.
8745 */
Avi Kivity851ba692009-08-24 11:10:17 +03008746static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008747{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008748 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008749 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008750 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008751
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008752 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8753
Kai Huang843e4332015-01-28 10:54:28 +08008754 /*
8755 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8756 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8757 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8758 * mode as if vcpus is in root mode, the PML buffer must has been
8759 * flushed already.
8760 */
8761 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008762 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008763
Mohammed Gamal80ced182009-09-01 12:48:18 +02008764 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008765 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008766 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008767
Paolo Bonzini7313c692017-07-27 10:31:25 +02008768 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
8769 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03008770
Mohammed Gamal51207022010-05-31 22:40:54 +03008771 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008772 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008773 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8774 vcpu->run->fail_entry.hardware_entry_failure_reason
8775 = exit_reason;
8776 return 0;
8777 }
8778
Avi Kivity29bd8a72007-09-10 17:27:03 +03008779 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008780 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8781 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008782 = vmcs_read32(VM_INSTRUCTION_ERROR);
8783 return 0;
8784 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008785
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008786 /*
8787 * Note:
8788 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8789 * delivery event since it indicates guest is accessing MMIO.
8790 * The vm-exit can be triggered again after return to guest that
8791 * will cause infinite loop.
8792 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008793 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008794 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008795 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008796 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008797 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8798 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8799 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02008800 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008801 vcpu->run->internal.data[0] = vectoring_info;
8802 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02008803 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
8804 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
8805 vcpu->run->internal.ndata++;
8806 vcpu->run->internal.data[3] =
8807 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
8808 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008809 return 0;
8810 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008811
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008812 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01008813 vmx->loaded_vmcs->soft_vnmi_blocked)) {
8814 if (vmx_interrupt_allowed(vcpu)) {
8815 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8816 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
8817 vcpu->arch.nmi_pending) {
8818 /*
8819 * This CPU don't support us in finding the end of an
8820 * NMI-blocked window if the guest runs with IRQs
8821 * disabled. So we pull the trigger after 1 s of
8822 * futile waiting, but inform the user about this.
8823 */
8824 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8825 "state on VCPU %d after 1 s timeout\n",
8826 __func__, vcpu->vcpu_id);
8827 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8828 }
8829 }
8830
Avi Kivity6aa8b732006-12-10 02:21:36 -08008831 if (exit_reason < kvm_vmx_max_exit_handlers
8832 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008833 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008834 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01008835 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
8836 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008837 kvm_queue_exception(vcpu, UD_VECTOR);
8838 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008839 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008840}
8841
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008842static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008843{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008844 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8845
8846 if (is_guest_mode(vcpu) &&
8847 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8848 return;
8849
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008850 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008851 vmcs_write32(TPR_THRESHOLD, 0);
8852 return;
8853 }
8854
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008855 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008856}
8857
Yang Zhang8d146952013-01-25 10:18:50 +08008858static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8859{
8860 u32 sec_exec_control;
8861
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008862 /* Postpone execution until vmcs01 is the current VMCS. */
8863 if (is_guest_mode(vcpu)) {
8864 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8865 return;
8866 }
8867
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008868 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008869 return;
8870
Paolo Bonzini35754c92015-07-29 12:05:37 +02008871 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008872 return;
8873
8874 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8875
8876 if (set) {
8877 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8878 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8879 } else {
8880 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8881 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008882 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008883 }
8884 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8885
8886 vmx_set_msr_bitmap(vcpu);
8887}
8888
Tang Chen38b99172014-09-24 15:57:54 +08008889static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8890{
8891 struct vcpu_vmx *vmx = to_vmx(vcpu);
8892
8893 /*
8894 * Currently we do not handle the nested case where L2 has an
8895 * APIC access page of its own; that page is still pinned.
8896 * Hence, we skip the case where the VCPU is in guest mode _and_
8897 * L1 prepared an APIC access page for L2.
8898 *
8899 * For the case where L1 and L2 share the same APIC access page
8900 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8901 * in the vmcs12), this function will only update either the vmcs01
8902 * or the vmcs02. If the former, the vmcs02 will be updated by
8903 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8904 * the next L2->L1 exit.
8905 */
8906 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008907 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008908 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08008909 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008910 vmx_flush_tlb_ept_only(vcpu);
8911 }
Tang Chen38b99172014-09-24 15:57:54 +08008912}
8913
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008914static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008915{
8916 u16 status;
8917 u8 old;
8918
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008919 if (max_isr == -1)
8920 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008921
8922 status = vmcs_read16(GUEST_INTR_STATUS);
8923 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008924 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008925 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008926 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008927 vmcs_write16(GUEST_INTR_STATUS, status);
8928 }
8929}
8930
8931static void vmx_set_rvi(int vector)
8932{
8933 u16 status;
8934 u8 old;
8935
Wei Wang4114c272014-11-05 10:53:43 +08008936 if (vector == -1)
8937 vector = 0;
8938
Yang Zhangc7c9c562013-01-25 10:18:51 +08008939 status = vmcs_read16(GUEST_INTR_STATUS);
8940 old = (u8)status & 0xff;
8941 if ((u8)vector != old) {
8942 status &= ~0xff;
8943 status |= (u8)vector;
8944 vmcs_write16(GUEST_INTR_STATUS, status);
8945 }
8946}
8947
8948static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8949{
Wanpeng Li963fee12014-07-17 19:03:00 +08008950 if (!is_guest_mode(vcpu)) {
8951 vmx_set_rvi(max_irr);
8952 return;
8953 }
8954
Wei Wang4114c272014-11-05 10:53:43 +08008955 if (max_irr == -1)
8956 return;
8957
Wanpeng Li963fee12014-07-17 19:03:00 +08008958 /*
Wei Wang4114c272014-11-05 10:53:43 +08008959 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8960 * handles it.
8961 */
8962 if (nested_exit_on_intr(vcpu))
8963 return;
8964
8965 /*
8966 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008967 * is run without virtual interrupt delivery.
8968 */
8969 if (!kvm_event_needs_reinjection(vcpu) &&
8970 vmx_interrupt_allowed(vcpu)) {
8971 kvm_queue_interrupt(vcpu, max_irr, false);
8972 vmx_inject_irq(vcpu);
8973 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008974}
8975
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008976static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008977{
8978 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008979 int max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008980
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008981 WARN_ON(!vcpu->arch.apicv_active);
8982 if (pi_test_on(&vmx->pi_desc)) {
8983 pi_clear_on(&vmx->pi_desc);
8984 /*
8985 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
8986 * But on x86 this is just a compiler barrier anyway.
8987 */
8988 smp_mb__after_atomic();
8989 max_irr = kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
8990 } else {
8991 max_irr = kvm_lapic_find_highest_irr(vcpu);
8992 }
8993 vmx_hwapic_irr_update(vcpu, max_irr);
8994 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008995}
8996
Andrey Smetanin63086302015-11-10 15:36:32 +03008997static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008998{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008999 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009000 return;
9001
Yang Zhangc7c9c562013-01-25 10:18:51 +08009002 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9003 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9004 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9005 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9006}
9007
Paolo Bonzini967235d2016-12-19 14:03:45 +01009008static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9009{
9010 struct vcpu_vmx *vmx = to_vmx(vcpu);
9011
9012 pi_clear_on(&vmx->pi_desc);
9013 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9014}
9015
Avi Kivity51aa01d2010-07-20 14:31:20 +03009016static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009017{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009018 u32 exit_intr_info = 0;
9019 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009020
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009021 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9022 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009023 return;
9024
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009025 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9026 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9027 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009028
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009029 /* if exit due to PF check for async PF */
9030 if (is_page_fault(exit_intr_info))
9031 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9032
Andi Kleena0861c02009-06-08 17:37:09 +08009033 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009034 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9035 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009036 kvm_machine_check();
9037
Gleb Natapov20f65982009-05-11 13:35:55 +03009038 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009039 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009040 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009041 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009042 kvm_after_handle_nmi(&vmx->vcpu);
9043 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009044}
Gleb Natapov20f65982009-05-11 13:35:55 +03009045
Yang Zhanga547c6d2013-04-11 19:25:10 +08009046static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9047{
9048 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9049
Yang Zhanga547c6d2013-04-11 19:25:10 +08009050 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9051 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9052 unsigned int vector;
9053 unsigned long entry;
9054 gate_desc *desc;
9055 struct vcpu_vmx *vmx = to_vmx(vcpu);
9056#ifdef CONFIG_X86_64
9057 unsigned long tmp;
9058#endif
9059
9060 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9061 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009062 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009063 asm volatile(
9064#ifdef CONFIG_X86_64
9065 "mov %%" _ASM_SP ", %[sp]\n\t"
9066 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9067 "push $%c[ss]\n\t"
9068 "push %[sp]\n\t"
9069#endif
9070 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009071 __ASM_SIZE(push) " $%c[cs]\n\t"
9072 "call *%[entry]\n\t"
9073 :
9074#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009075 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009076#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009077 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009078 :
9079 [entry]"r"(entry),
9080 [ss]"i"(__KERNEL_DS),
9081 [cs]"i"(__KERNEL_CS)
9082 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009083 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009084}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009085STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009086
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009087static bool vmx_has_high_real_mode_segbase(void)
9088{
9089 return enable_unrestricted_guest || emulate_invalid_guest_state;
9090}
9091
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009092static bool vmx_mpx_supported(void)
9093{
9094 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9095 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9096}
9097
Wanpeng Li55412b22014-12-02 19:21:30 +08009098static bool vmx_xsaves_supported(void)
9099{
9100 return vmcs_config.cpu_based_2nd_exec_ctrl &
9101 SECONDARY_EXEC_XSAVES;
9102}
9103
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009104static bool vmx_umip_emulated(void)
9105{
Paolo Bonzini0367f202016-07-12 10:44:55 +02009106 return vmcs_config.cpu_based_2nd_exec_ctrl &
9107 SECONDARY_EXEC_DESC;
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009108}
9109
Avi Kivity51aa01d2010-07-20 14:31:20 +03009110static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9111{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009112 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009113 bool unblock_nmi;
9114 u8 vector;
9115 bool idtv_info_valid;
9116
9117 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009118
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009119 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009120 if (vmx->loaded_vmcs->nmi_known_unmasked)
9121 return;
9122 /*
9123 * Can't use vmx->exit_intr_info since we're not sure what
9124 * the exit reason is.
9125 */
9126 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9127 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9128 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9129 /*
9130 * SDM 3: 27.7.1.2 (September 2008)
9131 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9132 * a guest IRET fault.
9133 * SDM 3: 23.2.2 (September 2008)
9134 * Bit 12 is undefined in any of the following cases:
9135 * If the VM exit sets the valid bit in the IDT-vectoring
9136 * information field.
9137 * If the VM exit is due to a double fault.
9138 */
9139 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9140 vector != DF_VECTOR && !idtv_info_valid)
9141 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9142 GUEST_INTR_STATE_NMI);
9143 else
9144 vmx->loaded_vmcs->nmi_known_unmasked =
9145 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9146 & GUEST_INTR_STATE_NMI);
9147 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9148 vmx->loaded_vmcs->vnmi_blocked_time +=
9149 ktime_to_ns(ktime_sub(ktime_get(),
9150 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009151}
9152
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009153static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009154 u32 idt_vectoring_info,
9155 int instr_len_field,
9156 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009157{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009158 u8 vector;
9159 int type;
9160 bool idtv_info_valid;
9161
9162 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009163
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009164 vcpu->arch.nmi_injected = false;
9165 kvm_clear_exception_queue(vcpu);
9166 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009167
9168 if (!idtv_info_valid)
9169 return;
9170
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009171 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009172
Avi Kivity668f6122008-07-02 09:28:55 +03009173 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9174 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009175
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009176 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009177 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009178 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009179 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009180 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009181 * Clear bit "block by NMI" before VM entry if a NMI
9182 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009183 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009184 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009185 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009186 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009187 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009188 /* fall through */
9189 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009190 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009191 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009192 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009193 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009194 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009195 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009196 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009197 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009198 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009199 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009200 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009201 break;
9202 default:
9203 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009204 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009205}
9206
Avi Kivity83422e12010-07-20 14:43:23 +03009207static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9208{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009209 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009210 VM_EXIT_INSTRUCTION_LEN,
9211 IDT_VECTORING_ERROR_CODE);
9212}
9213
Avi Kivityb463a6f2010-07-20 15:06:17 +03009214static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9215{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009216 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009217 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9218 VM_ENTRY_INSTRUCTION_LEN,
9219 VM_ENTRY_EXCEPTION_ERROR_CODE);
9220
9221 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9222}
9223
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009224static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9225{
9226 int i, nr_msrs;
9227 struct perf_guest_switch_msr *msrs;
9228
9229 msrs = perf_guest_get_msrs(&nr_msrs);
9230
9231 if (!msrs)
9232 return;
9233
9234 for (i = 0; i < nr_msrs; i++)
9235 if (msrs[i].host == msrs[i].guest)
9236 clear_atomic_switch_msr(vmx, msrs[i].msr);
9237 else
9238 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9239 msrs[i].host);
9240}
9241
Jiang Biao33365e72016-11-03 15:03:37 +08009242static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009243{
9244 struct vcpu_vmx *vmx = to_vmx(vcpu);
9245 u64 tscl;
9246 u32 delta_tsc;
9247
9248 if (vmx->hv_deadline_tsc == -1)
9249 return;
9250
9251 tscl = rdtsc();
9252 if (vmx->hv_deadline_tsc > tscl)
9253 /* sure to be 32 bit only because checked on set_hv_timer */
9254 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9255 cpu_preemption_timer_multi);
9256 else
9257 delta_tsc = 0;
9258
9259 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9260}
9261
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009262static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009263{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009264 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Li74c55932017-11-29 01:31:20 -08009265 unsigned long cr3, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02009266
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009267 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009268 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009269 vmx->loaded_vmcs->soft_vnmi_blocked))
9270 vmx->loaded_vmcs->entry_time = ktime_get();
9271
Avi Kivity104f2262010-11-18 13:12:52 +02009272 /* Don't enter VMX if guest state is invalid, let the exit handler
9273 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009274 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009275 return;
9276
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009277 if (vmx->ple_window_dirty) {
9278 vmx->ple_window_dirty = false;
9279 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9280 }
9281
Abel Gordon012f83c2013-04-18 14:39:25 +03009282 if (vmx->nested.sync_shadow_vmcs) {
9283 copy_vmcs12_to_shadow(vmx);
9284 vmx->nested.sync_shadow_vmcs = false;
9285 }
9286
Avi Kivity104f2262010-11-18 13:12:52 +02009287 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9288 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9289 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9290 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9291
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009292 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +02009293 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009294 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +02009295 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009296 }
9297
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009298 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +02009299 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009300 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +02009301 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009302 }
9303
Avi Kivity104f2262010-11-18 13:12:52 +02009304 /* When single-stepping over STI and MOV SS, we must clear the
9305 * corresponding interruptibility bits in the guest state. Otherwise
9306 * vmentry fails as it then expects bit 14 (BS) in pending debug
9307 * exceptions being set, but that's not correct for the guest debugging
9308 * case. */
9309 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9310 vmx_set_interrupt_shadow(vcpu, 0);
9311
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009312 if (static_cpu_has(X86_FEATURE_PKU) &&
9313 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9314 vcpu->arch.pkru != vmx->host_pkru)
9315 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009316
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009317 atomic_switch_perf_msrs(vmx);
9318
Yunhong Jiang64672c92016-06-13 14:19:59 -07009319 vmx_arm_hv_timer(vcpu);
9320
Nadav Har'Eld462b812011-05-24 15:26:10 +03009321 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02009322 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009323 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009324 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9325 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9326 "push %%" _ASM_CX " \n\t"
9327 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009328 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009329 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009330 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009331 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009332 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009333 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9334 "mov %%cr2, %%" _ASM_DX " \n\t"
9335 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009336 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009337 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009338 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009339 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009340 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009341 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009342 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9343 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9344 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9345 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9346 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9347 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009348#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009349 "mov %c[r8](%0), %%r8 \n\t"
9350 "mov %c[r9](%0), %%r9 \n\t"
9351 "mov %c[r10](%0), %%r10 \n\t"
9352 "mov %c[r11](%0), %%r11 \n\t"
9353 "mov %c[r12](%0), %%r12 \n\t"
9354 "mov %c[r13](%0), %%r13 \n\t"
9355 "mov %c[r14](%0), %%r14 \n\t"
9356 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009357#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009358 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009359
Avi Kivity6aa8b732006-12-10 02:21:36 -08009360 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009361 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009362 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009363 "jmp 2f \n\t"
9364 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9365 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009366 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009367 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009368 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009369 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9370 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9371 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9372 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9373 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9374 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9375 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009376#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009377 "mov %%r8, %c[r8](%0) \n\t"
9378 "mov %%r9, %c[r9](%0) \n\t"
9379 "mov %%r10, %c[r10](%0) \n\t"
9380 "mov %%r11, %c[r11](%0) \n\t"
9381 "mov %%r12, %c[r12](%0) \n\t"
9382 "mov %%r13, %c[r13](%0) \n\t"
9383 "mov %%r14, %c[r14](%0) \n\t"
9384 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009385#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009386 "mov %%cr2, %%" _ASM_AX " \n\t"
9387 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009388
Avi Kivityb188c81f2012-09-16 15:10:58 +03009389 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02009390 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009391 ".pushsection .rodata \n\t"
9392 ".global vmx_return \n\t"
9393 "vmx_return: " _ASM_PTR " 2b \n\t"
9394 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009395 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009396 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009397 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03009398 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009399 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9400 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9401 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9402 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9403 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9404 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9405 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009406#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009407 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9408 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9409 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9410 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9411 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9412 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9413 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9414 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009415#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009416 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9417 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009418 : "cc", "memory"
9419#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009420 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009421 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009422#else
9423 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009424#endif
9425 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009426
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009427 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08009428 if (vmx->host_debugctlmsr)
9429 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009430
Avi Kivityaa67f602012-08-01 16:48:03 +03009431#ifndef CONFIG_X86_64
9432 /*
9433 * The sysexit path does not restore ds/es, so we must set them to
9434 * a reasonable value ourselves.
9435 *
9436 * We can't defer this to vmx_load_host_state() since that function
9437 * may be executed in interrupt context, which saves and restore segments
9438 * around it, nullifying its effect.
9439 */
9440 loadsegment(ds, __USER_DS);
9441 loadsegment(es, __USER_DS);
9442#endif
9443
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009444 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009445 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009446 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009447 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009448 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009449 vcpu->arch.regs_dirty = 0;
9450
Gleb Natapove0b890d2013-09-25 12:51:33 +03009451 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009452 * eager fpu is enabled if PKEY is supported and CR4 is switched
9453 * back on host, so it is safe to read guest PKRU from current
9454 * XSAVE.
9455 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009456 if (static_cpu_has(X86_FEATURE_PKU) &&
9457 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9458 vcpu->arch.pkru = __read_pkru();
9459 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009460 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009461 }
9462
9463 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009464 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9465 * we did not inject a still-pending event to L1 now because of
9466 * nested_run_pending, we need to re-enable this bit.
9467 */
9468 if (vmx->nested.nested_run_pending)
9469 kvm_make_request(KVM_REQ_EVENT, vcpu);
9470
9471 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07009472 vmx->idt_vectoring_info = 0;
9473
9474 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9475 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9476 return;
9477
9478 vmx->loaded_vmcs->launched = 1;
9479 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03009480
Avi Kivity51aa01d2010-07-20 14:31:20 +03009481 vmx_complete_atomic_exit(vmx);
9482 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009483 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009484}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009485STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009486
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009487static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009488{
9489 struct vcpu_vmx *vmx = to_vmx(vcpu);
9490 int cpu;
9491
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009492 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009493 return;
9494
9495 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009496 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009497 vmx_vcpu_put(vcpu);
9498 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009499 put_cpu();
9500}
9501
Jim Mattson2f1fe812016-07-08 15:36:06 -07009502/*
9503 * Ensure that the current vmcs of the logical processor is the
9504 * vmcs01 of the vcpu before calling free_nested().
9505 */
9506static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9507{
9508 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009509
Christoffer Dallec7660c2017-12-04 21:35:23 +01009510 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009511 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009512 free_nested(vmx);
9513 vcpu_put(vcpu);
9514}
9515
Avi Kivity6aa8b732006-12-10 02:21:36 -08009516static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9517{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009518 struct vcpu_vmx *vmx = to_vmx(vcpu);
9519
Kai Huang843e4332015-01-28 10:54:28 +08009520 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009521 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009522 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009523 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009524 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009525 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009526 kfree(vmx->guest_msrs);
9527 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009528 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009529}
9530
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009531static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009532{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009533 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009534 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03009535 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009536
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009537 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009538 return ERR_PTR(-ENOMEM);
9539
Wanpeng Li991e7a02015-09-16 17:30:05 +08009540 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009541
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009542 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9543 if (err)
9544 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009545
Peter Feiner4e595162016-07-07 14:49:58 -07009546 err = -ENOMEM;
9547
9548 /*
9549 * If PML is turned on, failure on enabling PML just results in failure
9550 * of creating the vcpu, therefore we can simplify PML logic (by
9551 * avoiding dealing with cases, such as enabling PML partially on vcpus
9552 * for the guest, etc.
9553 */
9554 if (enable_pml) {
9555 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9556 if (!vmx->pml_pg)
9557 goto uninit_vcpu;
9558 }
9559
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009560 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009561 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9562 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009563
Peter Feiner4e595162016-07-07 14:49:58 -07009564 if (!vmx->guest_msrs)
9565 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009566
Nadav Har'Eld462b812011-05-24 15:26:10 +03009567 vmx->loaded_vmcs = &vmx->vmcs01;
9568 vmx->loaded_vmcs->vmcs = alloc_vmcs();
Jim Mattson355f4fb2016-10-28 08:29:39 -07009569 vmx->loaded_vmcs->shadow_vmcs = NULL;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009570 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009571 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009572 loaded_vmcs_init(vmx->loaded_vmcs);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009573
Avi Kivity15ad7142007-07-11 18:17:21 +03009574 cpu = get_cpu();
9575 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009576 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +02009577 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009578 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009579 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +02009580 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009581 err = alloc_apic_access_page(kvm);
9582 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009583 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009584 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009585
Sheng Yangb927a3c2009-07-21 10:42:48 +08009586 if (enable_ept) {
Tang Chenf51770e2014-09-16 18:41:59 +08009587 err = init_rmode_identity_map(kvm);
9588 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009589 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009590 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009591
Wanpeng Li5c614b32015-10-13 09:18:36 -07009592 if (nested) {
Wincy Vanb9c237b2015-02-03 23:56:30 +08009593 nested_vmx_setup_ctls_msrs(vmx);
Wanpeng Li5c614b32015-10-13 09:18:36 -07009594 vmx->nested.vpid02 = allocate_vpid();
9595 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009596
Wincy Van705699a2015-02-03 23:58:17 +08009597 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009598 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009599
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009600 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9601
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02009602 /*
9603 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9604 * or POSTED_INTR_WAKEUP_VECTOR.
9605 */
9606 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9607 vmx->pi_desc.sn = 1;
9608
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009609 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009610
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009611free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009612 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009613 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009614free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009615 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009616free_pml:
9617 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009618uninit_vcpu:
9619 kvm_vcpu_uninit(&vmx->vcpu);
9620free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009621 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009622 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009623 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009624}
9625
Yang, Sheng002c7f72007-07-31 14:23:01 +03009626static void __init vmx_check_processor_compat(void *rtn)
9627{
9628 struct vmcs_config vmcs_conf;
9629
9630 *(int *)rtn = 0;
9631 if (setup_vmcs_config(&vmcs_conf) < 0)
9632 *(int *)rtn = -EIO;
9633 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9634 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9635 smp_processor_id());
9636 *(int *)rtn = -EIO;
9637 }
9638}
9639
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009640static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009641{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009642 u8 cache;
9643 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009644
Sheng Yang522c68c2009-04-27 20:35:43 +08009645 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009646 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009647 * 2. EPT with VT-d:
9648 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009649 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009650 * b. VT-d with snooping control feature: snooping control feature of
9651 * VT-d engine can guarantee the cache correctness. Just set it
9652 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009653 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009654 * consistent with host MTRR
9655 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009656 if (is_mmio) {
9657 cache = MTRR_TYPE_UNCACHABLE;
9658 goto exit;
9659 }
9660
9661 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009662 ipat = VMX_EPT_IPAT_BIT;
9663 cache = MTRR_TYPE_WRBACK;
9664 goto exit;
9665 }
9666
9667 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9668 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009669 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009670 cache = MTRR_TYPE_WRBACK;
9671 else
9672 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009673 goto exit;
9674 }
9675
Xiao Guangrongff536042015-06-15 16:55:22 +08009676 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009677
9678exit:
9679 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009680}
9681
Sheng Yang17cc3932010-01-05 19:02:27 +08009682static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009683{
Sheng Yang878403b2010-01-05 19:02:29 +08009684 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9685 return PT_DIRECTORY_LEVEL;
9686 else
9687 /* For shadow and EPT supported 1GB page */
9688 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009689}
9690
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009691static void vmcs_set_secondary_exec_control(u32 new_ctl)
9692{
9693 /*
9694 * These bits in the secondary execution controls field
9695 * are dynamic, the others are mostly based on the hypervisor
9696 * architecture and the guest's CPUID. Do not touch the
9697 * dynamic bits.
9698 */
9699 u32 mask =
9700 SECONDARY_EXEC_SHADOW_VMCS |
9701 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02009702 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9703 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009704
9705 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9706
9707 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9708 (new_ctl & ~mask) | (cur_ctl & mask));
9709}
9710
David Matlack8322ebb2016-11-29 18:14:09 -08009711/*
9712 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
9713 * (indicating "allowed-1") if they are supported in the guest's CPUID.
9714 */
9715static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
9716{
9717 struct vcpu_vmx *vmx = to_vmx(vcpu);
9718 struct kvm_cpuid_entry2 *entry;
9719
9720 vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff;
9721 vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE;
9722
9723#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
9724 if (entry && (entry->_reg & (_cpuid_mask))) \
9725 vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \
9726} while (0)
9727
9728 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
9729 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
9730 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
9731 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
9732 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
9733 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
9734 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
9735 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
9736 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
9737 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
9738 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
9739 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
9740 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
9741 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
9742 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
9743
9744 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9745 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
9746 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
9747 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
9748 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +01009749 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -08009750
9751#undef cr4_fixed1_update
9752}
9753
Sheng Yang0e851882009-12-18 16:48:46 +08009754static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9755{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009756 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009757
Paolo Bonzini80154d72017-08-24 13:55:35 +02009758 if (cpu_has_secondary_exec_ctrls()) {
9759 vmx_compute_secondary_exec_control(vmx);
9760 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009761 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009762
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009763 if (nested_vmx_allowed(vcpu))
9764 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9765 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9766 else
9767 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9768 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -08009769
9770 if (nested_vmx_allowed(vcpu))
9771 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08009772}
9773
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009774static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9775{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009776 if (func == 1 && nested)
9777 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009778}
9779
Yang Zhang25d92082013-08-06 12:00:32 +03009780static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9781 struct x86_exception *fault)
9782{
Jan Kiszka533558b2014-01-04 18:47:20 +01009783 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -04009784 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +01009785 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -04009786 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +03009787
Bandan Dasc5f983f2017-05-05 15:25:14 -04009788 if (vmx->nested.pml_full) {
9789 exit_reason = EXIT_REASON_PML_FULL;
9790 vmx->nested.pml_full = false;
9791 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
9792 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009793 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009794 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009795 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -04009796
9797 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009798 vmcs12->guest_physical_address = fault->address;
9799}
9800
Peter Feiner995f00a2017-06-30 17:26:32 -07009801static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
9802{
David Hildenbrandbb97a012017-08-10 23:15:28 +02009803 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -07009804}
9805
Nadav Har'El155a97a2013-08-05 11:07:16 +03009806/* Callbacks for nested_ept_init_mmu_context: */
9807
9808static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9809{
9810 /* return the page table to be shadowed - in our case, EPT12 */
9811 return get_vmcs12(vcpu)->ept_pointer;
9812}
9813
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009814static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009815{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009816 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +02009817 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009818 return 1;
9819
9820 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +02009821 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009822 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009823 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +02009824 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +03009825 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9826 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9827 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9828
9829 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009830 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009831}
9832
9833static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9834{
9835 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9836}
9837
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009838static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9839 u16 error_code)
9840{
9841 bool inequality, bit;
9842
9843 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9844 inequality =
9845 (error_code & vmcs12->page_fault_error_code_mask) !=
9846 vmcs12->page_fault_error_code_match;
9847 return inequality ^ bit;
9848}
9849
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009850static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9851 struct x86_exception *fault)
9852{
9853 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9854
9855 WARN_ON(!is_guest_mode(vcpu));
9856
Wanpeng Li305d0ab2017-09-28 18:16:44 -07009857 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
9858 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02009859 vmcs12->vm_exit_intr_error_code = fault->error_code;
9860 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
9861 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
9862 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
9863 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +02009864 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009865 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +02009866 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009867}
9868
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009869static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9870 struct vmcs12 *vmcs12);
9871
9872static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009873 struct vmcs12 *vmcs12)
9874{
9875 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009876 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009877 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009878
9879 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009880 /*
9881 * Translate L1 physical address to host physical
9882 * address for vmcs02. Keep the page pinned, so this
9883 * physical address remains valid. We keep a reference
9884 * to it so we can release it later.
9885 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009886 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +02009887 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009888 vmx->nested.apic_access_page = NULL;
9889 }
9890 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009891 /*
9892 * If translation failed, no matter: This feature asks
9893 * to exit when accessing the given address, and if it
9894 * can never be accessed, this feature won't do
9895 * anything anyway.
9896 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009897 if (!is_error_page(page)) {
9898 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009899 hpa = page_to_phys(vmx->nested.apic_access_page);
9900 vmcs_write64(APIC_ACCESS_ADDR, hpa);
9901 } else {
9902 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
9903 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9904 }
9905 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
9906 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
9907 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
9908 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9909 kvm_vcpu_reload_apic_access_page(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009910 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009911
9912 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009913 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +02009914 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009915 vmx->nested.virtual_apic_page = NULL;
9916 }
9917 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009918
9919 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009920 * If translation failed, VM entry will fail because
9921 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
9922 * Failing the vm entry is _not_ what the processor
9923 * does but it's basically the only possibility we
9924 * have. We could still enter the guest if CR8 load
9925 * exits are enabled, CR8 store exits are enabled, and
9926 * virtualize APIC access is disabled; in this case
9927 * the processor would never use the TPR shadow and we
9928 * could simply clear the bit from the execution
9929 * control. But such a configuration is useless, so
9930 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009931 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009932 if (!is_error_page(page)) {
9933 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009934 hpa = page_to_phys(vmx->nested.virtual_apic_page);
9935 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
9936 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009937 }
9938
Wincy Van705699a2015-02-03 23:58:17 +08009939 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +08009940 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9941 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02009942 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009943 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +08009944 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009945 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
9946 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009947 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009948 vmx->nested.pi_desc_page = page;
9949 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08009950 vmx->nested.pi_desc =
9951 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9952 (unsigned long)(vmcs12->posted_intr_desc_addr &
9953 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009954 vmcs_write64(POSTED_INTR_DESC_ADDR,
9955 page_to_phys(vmx->nested.pi_desc_page) +
9956 (unsigned long)(vmcs12->posted_intr_desc_addr &
9957 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +08009958 }
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009959 if (cpu_has_vmx_msr_bitmap() &&
9960 nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS) &&
9961 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
9962 ;
9963 else
9964 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
9965 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009966}
9967
Jan Kiszkaf41245002014-03-07 20:03:13 +01009968static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9969{
9970 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9971 struct vcpu_vmx *vmx = to_vmx(vcpu);
9972
9973 if (vcpu->arch.virtual_tsc_khz == 0)
9974 return;
9975
9976 /* Make sure short timeouts reliably trigger an immediate vmexit.
9977 * hrtimer_start does not guarantee this. */
9978 if (preemption_timeout <= 1) {
9979 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9980 return;
9981 }
9982
9983 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9984 preemption_timeout *= 1000000;
9985 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9986 hrtimer_start(&vmx->nested.preemption_timer,
9987 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9988}
9989
Jim Mattson56a20512017-07-06 16:33:06 -07009990static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
9991 struct vmcs12 *vmcs12)
9992{
9993 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
9994 return 0;
9995
9996 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
9997 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
9998 return -EINVAL;
9999
10000 return 0;
10001}
10002
Wincy Van3af18d92015-02-03 23:49:31 +080010003static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10004 struct vmcs12 *vmcs12)
10005{
Wincy Van3af18d92015-02-03 23:49:31 +080010006 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10007 return 0;
10008
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010009 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010010 return -EINVAL;
10011
10012 return 0;
10013}
10014
Jim Mattson712b12d2017-08-24 13:24:47 -070010015static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10016 struct vmcs12 *vmcs12)
10017{
10018 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10019 return 0;
10020
10021 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10022 return -EINVAL;
10023
10024 return 0;
10025}
10026
Wincy Van3af18d92015-02-03 23:49:31 +080010027/*
10028 * Merge L0's and L1's MSR bitmap, return false to indicate that
10029 * we do not use the hardware.
10030 */
10031static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
10032 struct vmcs12 *vmcs12)
10033{
Wincy Van82f0dd42015-02-03 23:57:18 +080010034 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010035 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010036 unsigned long *msr_bitmap_l1;
10037 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap;
Wincy Vanf2b93282015-02-03 23:56:03 +080010038
Radim Krčmářd048c092016-08-08 20:16:22 +020010039 /* This shortcut is ok because we support only x2APIC MSRs so far. */
Wincy Vanf2b93282015-02-03 23:56:03 +080010040 if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
10041 return false;
10042
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010043 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10044 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010045 return false;
Radim Krčmářd048c092016-08-08 20:16:22 +020010046 msr_bitmap_l1 = (unsigned long *)kmap(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010047
Radim Krčmářd048c092016-08-08 20:16:22 +020010048 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
10049
Wincy Vanf2b93282015-02-03 23:56:03 +080010050 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +080010051 if (nested_cpu_has_apic_reg_virt(vmcs12))
10052 for (msr = 0x800; msr <= 0x8ff; msr++)
10053 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +020010054 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +080010055 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +020010056
10057 nested_vmx_disable_intercept_for_msr(
10058 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +080010059 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
10060 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +020010061
Wincy Van608406e2015-02-03 23:57:51 +080010062 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +080010063 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +020010064 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +080010065 APIC_BASE_MSR + (APIC_EOI >> 4),
10066 MSR_TYPE_W);
10067 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +020010068 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +080010069 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
10070 MSR_TYPE_W);
10071 }
Wincy Van82f0dd42015-02-03 23:57:18 +080010072 }
Wincy Vanf2b93282015-02-03 23:56:03 +080010073 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010074 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010075
10076 return true;
10077}
10078
10079static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10080 struct vmcs12 *vmcs12)
10081{
Wincy Van82f0dd42015-02-03 23:57:18 +080010082 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010083 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010084 !nested_cpu_has_vid(vmcs12) &&
10085 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010086 return 0;
10087
10088 /*
10089 * If virtualize x2apic mode is enabled,
10090 * virtualize apic access must be disabled.
10091 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010092 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10093 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010094 return -EINVAL;
10095
Wincy Van608406e2015-02-03 23:57:51 +080010096 /*
10097 * If virtual interrupt delivery is enabled,
10098 * we must exit on external interrupts.
10099 */
10100 if (nested_cpu_has_vid(vmcs12) &&
10101 !nested_exit_on_intr(vcpu))
10102 return -EINVAL;
10103
Wincy Van705699a2015-02-03 23:58:17 +080010104 /*
10105 * bits 15:8 should be zero in posted_intr_nv,
10106 * the descriptor address has been already checked
10107 * in nested_get_vmcs12_pages.
10108 */
10109 if (nested_cpu_has_posted_intr(vmcs12) &&
10110 (!nested_cpu_has_vid(vmcs12) ||
10111 !nested_exit_intr_ack_set(vcpu) ||
10112 vmcs12->posted_intr_nv & 0xff00))
10113 return -EINVAL;
10114
Wincy Vanf2b93282015-02-03 23:56:03 +080010115 /* tpr shadow is needed by all apicv features. */
10116 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10117 return -EINVAL;
10118
10119 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010120}
10121
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010122static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10123 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010124 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010125{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010126 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010127 u64 count, addr;
10128
10129 if (vmcs12_read_any(vcpu, count_field, &count) ||
10130 vmcs12_read_any(vcpu, addr_field, &addr)) {
10131 WARN_ON(1);
10132 return -EINVAL;
10133 }
10134 if (count == 0)
10135 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010136 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010137 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10138 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010139 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010140 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10141 addr_field, maxphyaddr, count, addr);
10142 return -EINVAL;
10143 }
10144 return 0;
10145}
10146
10147static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10148 struct vmcs12 *vmcs12)
10149{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010150 if (vmcs12->vm_exit_msr_load_count == 0 &&
10151 vmcs12->vm_exit_msr_store_count == 0 &&
10152 vmcs12->vm_entry_msr_load_count == 0)
10153 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010154 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010155 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010156 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010157 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010158 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010159 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010160 return -EINVAL;
10161 return 0;
10162}
10163
Bandan Dasc5f983f2017-05-05 15:25:14 -040010164static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10165 struct vmcs12 *vmcs12)
10166{
10167 u64 address = vmcs12->pml_address;
10168 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10169
10170 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10171 if (!nested_cpu_has_ept(vmcs12) ||
10172 !IS_ALIGNED(address, 4096) ||
10173 address >> maxphyaddr)
10174 return -EINVAL;
10175 }
10176
10177 return 0;
10178}
10179
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010180static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10181 struct vmx_msr_entry *e)
10182{
10183 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010184 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010185 return -EINVAL;
10186 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10187 e->index == MSR_IA32_UCODE_REV)
10188 return -EINVAL;
10189 if (e->reserved != 0)
10190 return -EINVAL;
10191 return 0;
10192}
10193
10194static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10195 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010196{
10197 if (e->index == MSR_FS_BASE ||
10198 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010199 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10200 nested_vmx_msr_check_common(vcpu, e))
10201 return -EINVAL;
10202 return 0;
10203}
10204
10205static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10206 struct vmx_msr_entry *e)
10207{
10208 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10209 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010210 return -EINVAL;
10211 return 0;
10212}
10213
10214/*
10215 * Load guest's/host's msr at nested entry/exit.
10216 * return 0 for success, entry index for failure.
10217 */
10218static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10219{
10220 u32 i;
10221 struct vmx_msr_entry e;
10222 struct msr_data msr;
10223
10224 msr.host_initiated = false;
10225 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010226 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10227 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010228 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010229 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10230 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010231 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010232 }
10233 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010234 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010235 "%s check failed (%u, 0x%x, 0x%x)\n",
10236 __func__, i, e.index, e.reserved);
10237 goto fail;
10238 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010239 msr.index = e.index;
10240 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010241 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010242 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010243 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10244 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010245 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010246 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010247 }
10248 return 0;
10249fail:
10250 return i + 1;
10251}
10252
10253static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10254{
10255 u32 i;
10256 struct vmx_msr_entry e;
10257
10258 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010259 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010260 if (kvm_vcpu_read_guest(vcpu,
10261 gpa + i * sizeof(e),
10262 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010263 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010264 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10265 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010266 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010267 }
10268 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010269 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010270 "%s check failed (%u, 0x%x, 0x%x)\n",
10271 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010272 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010273 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010274 msr_info.host_initiated = false;
10275 msr_info.index = e.index;
10276 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010277 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010278 "%s cannot read MSR (%u, 0x%x)\n",
10279 __func__, i, e.index);
10280 return -EINVAL;
10281 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010282 if (kvm_vcpu_write_guest(vcpu,
10283 gpa + i * sizeof(e) +
10284 offsetof(struct vmx_msr_entry, value),
10285 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010286 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010287 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010288 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010289 return -EINVAL;
10290 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010291 }
10292 return 0;
10293}
10294
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010295static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10296{
10297 unsigned long invalid_mask;
10298
10299 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10300 return (val & invalid_mask) == 0;
10301}
10302
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010303/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010304 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10305 * emulating VM entry into a guest with EPT enabled.
10306 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10307 * is assigned to entry_failure_code on failure.
10308 */
10309static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010310 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010311{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010312 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010313 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010314 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10315 return 1;
10316 }
10317
10318 /*
10319 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10320 * must not be dereferenced.
10321 */
10322 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10323 !nested_ept) {
10324 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10325 *entry_failure_code = ENTRY_FAIL_PDPTE;
10326 return 1;
10327 }
10328 }
10329
10330 vcpu->arch.cr3 = cr3;
10331 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10332 }
10333
10334 kvm_mmu_reset_context(vcpu);
10335 return 0;
10336}
10337
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010338/*
10339 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10340 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010341 * 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 +030010342 * guest in a way that will both be appropriate to L1's requests, and our
10343 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10344 * function also has additional necessary side-effects, like setting various
10345 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010010346 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10347 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010348 */
Ladi Prosekee146c12016-11-30 16:03:09 +010010349static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010350 bool from_vmentry, u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010351{
10352 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040010353 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010354
10355 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10356 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
10357 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10358 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10359 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10360 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10361 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10362 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10363 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10364 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
10365 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10366 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10367 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10368 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10369 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10370 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10371 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10372 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10373 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10374 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
10375 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10376 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10377 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10378 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10379 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10380 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10381 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10382 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
10383 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10384 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10385 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10386 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10387 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10388 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10389 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10390 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10391
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010392 if (from_vmentry &&
10393 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020010394 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10395 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10396 } else {
10397 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10398 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10399 }
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010400 if (from_vmentry) {
10401 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10402 vmcs12->vm_entry_intr_info_field);
10403 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10404 vmcs12->vm_entry_exception_error_code);
10405 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10406 vmcs12->vm_entry_instruction_len);
10407 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10408 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070010409 vmx->loaded_vmcs->nmi_known_unmasked =
10410 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010411 } else {
10412 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10413 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010414 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +030010415 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010416 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10417 vmcs12->guest_pending_dbg_exceptions);
10418 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10419 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10420
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010421 if (nested_cpu_has_xsaves(vmcs12))
10422 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010423 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10424
Jan Kiszkaf41245002014-03-07 20:03:13 +010010425 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010426
Paolo Bonzini9314006db2016-07-06 13:23:51 +020010427 /* Preemption timer setting is only taken from vmcs01. */
10428 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10429 exec_control |= vmcs_config.pin_based_exec_ctrl;
10430 if (vmx->hv_deadline_tsc == -1)
10431 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10432
10433 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010434 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010435 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10436 vmx->nested.pi_pending = false;
Wincy Van06a55242017-04-28 13:13:59 +080010437 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010438 } else {
Wincy Van705699a2015-02-03 23:58:17 +080010439 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010440 }
Wincy Van705699a2015-02-03 23:58:17 +080010441
Jan Kiszkaf41245002014-03-07 20:03:13 +010010442 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010443
Jan Kiszkaf41245002014-03-07 20:03:13 +010010444 vmx->nested.preemption_timer_expired = false;
10445 if (nested_cpu_has_preemption_timer(vmcs12))
10446 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010447
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010448 /*
10449 * Whether page-faults are trapped is determined by a combination of
10450 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10451 * If enable_ept, L0 doesn't care about page faults and we should
10452 * set all of these to L1's desires. However, if !enable_ept, L0 does
10453 * care about (at least some) page faults, and because it is not easy
10454 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10455 * to exit on each and every L2 page fault. This is done by setting
10456 * MASK=MATCH=0 and (see below) EB.PF=1.
10457 * Note that below we don't need special code to set EB.PF beyond the
10458 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10459 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10460 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010461 */
10462 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10463 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10464 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10465 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10466
10467 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020010468 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080010469
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010470 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010471 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020010472 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010473 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020010474 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010475 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040010476 SECONDARY_EXEC_APIC_REGISTER_VIRT |
10477 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010478 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040010479 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
10480 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
10481 ~SECONDARY_EXEC_ENABLE_PML;
10482 exec_control |= vmcs12_exec_ctrl;
10483 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010484
Bandan Das27c42a12017-08-03 15:54:42 -040010485 /* All VMFUNCs are currently emulated through L0 vmexits. */
10486 if (exec_control & SECONDARY_EXEC_ENABLE_VMFUNC)
10487 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10488
Wincy Van608406e2015-02-03 23:57:51 +080010489 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
10490 vmcs_write64(EOI_EXIT_BITMAP0,
10491 vmcs12->eoi_exit_bitmap0);
10492 vmcs_write64(EOI_EXIT_BITMAP1,
10493 vmcs12->eoi_exit_bitmap1);
10494 vmcs_write64(EOI_EXIT_BITMAP2,
10495 vmcs12->eoi_exit_bitmap2);
10496 vmcs_write64(EOI_EXIT_BITMAP3,
10497 vmcs12->eoi_exit_bitmap3);
10498 vmcs_write16(GUEST_INTR_STATUS,
10499 vmcs12->guest_intr_status);
10500 }
10501
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010502 /*
10503 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10504 * nested_get_vmcs12_pages will either fix it up or
10505 * remove the VM execution control.
10506 */
10507 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
10508 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
10509
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010510 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10511 }
10512
10513
10514 /*
10515 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10516 * Some constant fields are set here by vmx_set_constant_host_state().
10517 * Other fields are different per CPU, and will be set later when
10518 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10519 */
Yang Zhanga547c6d2013-04-11 19:25:10 +080010520 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010521
10522 /*
Jim Mattson83bafef2016-10-04 10:48:38 -070010523 * Set the MSR load/store lists to match L0's settings.
10524 */
10525 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10526 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10527 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10528 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10529 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10530
10531 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010532 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10533 * entry, but only if the current (host) sp changed from the value
10534 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10535 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10536 * here we just force the write to happen on entry.
10537 */
10538 vmx->host_rsp = 0;
10539
10540 exec_control = vmx_exec_control(vmx); /* L0's desires */
10541 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10542 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10543 exec_control &= ~CPU_BASED_TPR_SHADOW;
10544 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010545
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010546 /*
10547 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
10548 * nested_get_vmcs12_pages can't fix it up, the illegal value
10549 * will result in a VM entry failure.
10550 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010551 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010552 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010553 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070010554 } else {
10555#ifdef CONFIG_X86_64
10556 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10557 CPU_BASED_CR8_STORE_EXITING;
10558#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010559 }
10560
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010561 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080010562 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
10563 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010564 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010565 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10566 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10567
10568 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10569
10570 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10571 * bitwise-or of what L1 wants to trap for L2, and what we want to
10572 * trap. Note that CR0.TS also needs updating - we do this later.
10573 */
10574 update_exception_bitmap(vcpu);
10575 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10576 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10577
Nadav Har'El8049d652013-08-05 11:07:06 +030010578 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10579 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10580 * bits are further modified by vmx_set_efer() below.
10581 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010010582 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010583
10584 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10585 * emulated by vmx_set_efer(), below.
10586 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010587 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010588 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10589 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010590 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10591
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010592 if (from_vmentry &&
10593 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010594 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010595 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010596 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010597 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010598 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010599
10600 set_cr4_guest_host_mask(vmx);
10601
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010602 if (from_vmentry &&
10603 vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010604 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10605
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010606 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10607 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010608 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010609 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010610 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010611 if (kvm_has_tsc_control)
10612 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010613
10614 if (enable_vpid) {
10615 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010616 * There is no direct mapping between vpid02 and vpid12, the
10617 * vpid02 is per-vCPU for L0 and reused while the value of
10618 * vpid12 is changed w/ one invvpid during nested vmentry.
10619 * The vpid12 is allocated by L1 for L2, so it will not
10620 * influence global bitmap(for vpid01 and vpid02 allocation)
10621 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010622 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010623 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10624 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10625 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10626 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080010627 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070010628 }
10629 } else {
10630 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080010631 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070010632 }
10633
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010634 }
10635
Ladi Prosek1fb883b2017-04-04 14:18:53 +020010636 if (enable_pml) {
10637 /*
10638 * Conceptually we want to copy the PML address and index from
10639 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10640 * since we always flush the log on each vmexit, this happens
10641 * to be equivalent to simply resetting the fields in vmcs02.
10642 */
10643 ASSERT(vmx->pml_pg);
10644 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10645 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10646 }
10647
Nadav Har'El155a97a2013-08-05 11:07:16 +030010648 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010649 if (nested_ept_init_mmu_context(vcpu)) {
10650 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10651 return 1;
10652 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010653 } else if (nested_cpu_has2(vmcs12,
10654 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10655 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010656 }
10657
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010658 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080010659 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
10660 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010661 * The CR0_READ_SHADOW is what L2 should have expected to read given
10662 * the specifications by L1; It's not enough to take
10663 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10664 * have more bits than L1 expected.
10665 */
10666 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10667 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10668
10669 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10670 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10671
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010672 if (from_vmentry &&
10673 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080010674 vcpu->arch.efer = vmcs12->guest_ia32_efer;
10675 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
10676 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10677 else
10678 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10679 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10680 vmx_set_efer(vcpu, vcpu->arch.efer);
10681
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010682 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010010683 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010684 entry_failure_code))
10685 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010010686
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010687 if (!enable_ept)
10688 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10689
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010690 /*
10691 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10692 */
10693 if (enable_ept) {
10694 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10695 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10696 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10697 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10698 }
10699
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010700 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10701 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010010702 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010703}
10704
Jim Mattsonca0bde22016-11-30 12:03:46 -080010705static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10706{
10707 struct vcpu_vmx *vmx = to_vmx(vcpu);
10708
10709 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10710 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
10711 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10712
Jim Mattson56a20512017-07-06 16:33:06 -070010713 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
10714 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10715
Jim Mattsonca0bde22016-11-30 12:03:46 -080010716 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
10717 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10718
Jim Mattson712b12d2017-08-24 13:24:47 -070010719 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
10720 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10721
Jim Mattsonca0bde22016-11-30 12:03:46 -080010722 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
10723 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10724
10725 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
10726 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10727
Bandan Dasc5f983f2017-05-05 15:25:14 -040010728 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
10729 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10730
Jim Mattsonca0bde22016-11-30 12:03:46 -080010731 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
10732 vmx->nested.nested_vmx_procbased_ctls_low,
10733 vmx->nested.nested_vmx_procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070010734 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
10735 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
10736 vmx->nested.nested_vmx_secondary_ctls_low,
10737 vmx->nested.nested_vmx_secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080010738 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
10739 vmx->nested.nested_vmx_pinbased_ctls_low,
10740 vmx->nested.nested_vmx_pinbased_ctls_high) ||
10741 !vmx_control_verify(vmcs12->vm_exit_controls,
10742 vmx->nested.nested_vmx_exit_ctls_low,
10743 vmx->nested.nested_vmx_exit_ctls_high) ||
10744 !vmx_control_verify(vmcs12->vm_entry_controls,
10745 vmx->nested.nested_vmx_entry_ctls_low,
10746 vmx->nested.nested_vmx_entry_ctls_high))
10747 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10748
Bandan Das41ab9372017-08-03 15:54:43 -040010749 if (nested_cpu_has_vmfunc(vmcs12)) {
10750 if (vmcs12->vm_function_control &
10751 ~vmx->nested.nested_vmx_vmfunc_controls)
10752 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10753
10754 if (nested_cpu_has_eptp_switching(vmcs12)) {
10755 if (!nested_cpu_has_ept(vmcs12) ||
10756 !page_address_valid(vcpu, vmcs12->eptp_list_address))
10757 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10758 }
10759 }
Bandan Das27c42a12017-08-03 15:54:42 -040010760
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070010761 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
10762 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10763
Jim Mattsonca0bde22016-11-30 12:03:46 -080010764 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
10765 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
10766 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
10767 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
10768
10769 return 0;
10770}
10771
10772static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10773 u32 *exit_qual)
10774{
10775 bool ia32e;
10776
10777 *exit_qual = ENTRY_FAIL_DEFAULT;
10778
10779 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
10780 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
10781 return 1;
10782
10783 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
10784 vmcs12->vmcs_link_pointer != -1ull) {
10785 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
10786 return 1;
10787 }
10788
10789 /*
10790 * If the load IA32_EFER VM-entry control is 1, the following checks
10791 * are performed on the field for the IA32_EFER MSR:
10792 * - Bits reserved in the IA32_EFER MSR must be 0.
10793 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10794 * the IA-32e mode guest VM-exit control. It must also be identical
10795 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10796 * CR0.PG) is 1.
10797 */
10798 if (to_vmx(vcpu)->nested.nested_run_pending &&
10799 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
10800 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10801 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10802 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10803 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10804 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
10805 return 1;
10806 }
10807
10808 /*
10809 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10810 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10811 * the values of the LMA and LME bits in the field must each be that of
10812 * the host address-space size VM-exit control.
10813 */
10814 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10815 ia32e = (vmcs12->vm_exit_controls &
10816 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10817 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10818 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10819 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
10820 return 1;
10821 }
10822
Wanpeng Lif1b026a2017-11-05 16:54:48 -080010823 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
10824 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
10825 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
10826 return 1;
10827
Jim Mattsonca0bde22016-11-30 12:03:46 -080010828 return 0;
10829}
10830
Jim Mattson858e25c2016-11-30 12:03:47 -080010831static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
10832{
10833 struct vcpu_vmx *vmx = to_vmx(vcpu);
10834 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080010835 u32 msr_entry_idx;
10836 u32 exit_qual;
10837
Jim Mattson858e25c2016-11-30 12:03:47 -080010838 enter_guest_mode(vcpu);
10839
10840 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10841 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10842
Jim Mattson00647b42017-11-27 17:22:25 -060010843 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080010844 vmx_segment_cache_clear(vmx);
10845
10846 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
10847 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010848 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080010849 nested_vmx_entry_failure(vcpu, vmcs12,
10850 EXIT_REASON_INVALID_STATE, exit_qual);
10851 return 1;
10852 }
10853
10854 nested_get_vmcs12_pages(vcpu, vmcs12);
10855
10856 msr_entry_idx = nested_vmx_load_msr(vcpu,
10857 vmcs12->vm_entry_msr_load_addr,
10858 vmcs12->vm_entry_msr_load_count);
10859 if (msr_entry_idx) {
10860 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010861 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080010862 nested_vmx_entry_failure(vcpu, vmcs12,
10863 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10864 return 1;
10865 }
10866
Jim Mattson858e25c2016-11-30 12:03:47 -080010867 /*
10868 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10869 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10870 * returned as far as L1 is concerned. It will only return (and set
10871 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10872 */
10873 return 0;
10874}
10875
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010876/*
10877 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10878 * for running an L2 nested guest.
10879 */
10880static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10881{
10882 struct vmcs12 *vmcs12;
10883 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070010884 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080010885 u32 exit_qual;
10886 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010887
Kyle Hueyeb277562016-11-29 12:40:39 -080010888 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010889 return 1;
10890
Kyle Hueyeb277562016-11-29 12:40:39 -080010891 if (!nested_vmx_check_vmcs12(vcpu))
10892 goto out;
10893
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010894 vmcs12 = get_vmcs12(vcpu);
10895
Abel Gordon012f83c2013-04-18 14:39:25 +030010896 if (enable_shadow_vmcs)
10897 copy_shadow_to_vmcs12(vmx);
10898
Nadav Har'El7c177932011-05-25 23:12:04 +030010899 /*
10900 * The nested entry process starts with enforcing various prerequisites
10901 * on vmcs12 as required by the Intel SDM, and act appropriately when
10902 * they fail: As the SDM explains, some conditions should cause the
10903 * instruction to fail, while others will cause the instruction to seem
10904 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10905 * To speed up the normal (success) code path, we should avoid checking
10906 * for misconfigurations which will anyway be caught by the processor
10907 * when using the merged vmcs02.
10908 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070010909 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
10910 nested_vmx_failValid(vcpu,
10911 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
10912 goto out;
10913 }
10914
Nadav Har'El7c177932011-05-25 23:12:04 +030010915 if (vmcs12->launch_state == launch) {
10916 nested_vmx_failValid(vcpu,
10917 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10918 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080010919 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030010920 }
10921
Jim Mattsonca0bde22016-11-30 12:03:46 -080010922 ret = check_vmentry_prereqs(vcpu, vmcs12);
10923 if (ret) {
10924 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080010925 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010926 }
10927
Nadav Har'El7c177932011-05-25 23:12:04 +030010928 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080010929 * After this point, the trap flag no longer triggers a singlestep trap
10930 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
10931 * This is not 100% correct; for performance reasons, we delegate most
10932 * of the checks on host state to the processor. If those fail,
10933 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020010934 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080010935 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020010936
Jim Mattsonca0bde22016-11-30 12:03:46 -080010937 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
10938 if (ret) {
10939 nested_vmx_entry_failure(vcpu, vmcs12,
10940 EXIT_REASON_INVALID_STATE, exit_qual);
10941 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020010942 }
10943
10944 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010945 * We're finally done with prerequisite checking, and can start with
10946 * the nested entry.
10947 */
10948
Jim Mattson858e25c2016-11-30 12:03:47 -080010949 ret = enter_vmx_non_root_mode(vcpu, true);
10950 if (ret)
10951 return ret;
Wincy Vanff651cb2014-12-11 08:52:58 +030010952
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010953 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010954 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010955
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010956 vmx->nested.nested_run_pending = 1;
10957
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010958 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080010959
10960out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080010961 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010962}
10963
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010964/*
10965 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10966 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10967 * This function returns the new value we should put in vmcs12.guest_cr0.
10968 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10969 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10970 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10971 * didn't trap the bit, because if L1 did, so would L0).
10972 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10973 * been modified by L2, and L1 knows it. So just leave the old value of
10974 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10975 * isn't relevant, because if L0 traps this bit it can set it to anything.
10976 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10977 * changed these bits, and therefore they need to be updated, but L0
10978 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10979 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10980 */
10981static inline unsigned long
10982vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10983{
10984 return
10985 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10986 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10987 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10988 vcpu->arch.cr0_guest_owned_bits));
10989}
10990
10991static inline unsigned long
10992vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10993{
10994 return
10995 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10996 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10997 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10998 vcpu->arch.cr4_guest_owned_bits));
10999}
11000
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011001static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11002 struct vmcs12 *vmcs12)
11003{
11004 u32 idt_vectoring;
11005 unsigned int nr;
11006
Wanpeng Li664f8e22017-08-24 03:35:09 -070011007 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011008 nr = vcpu->arch.exception.nr;
11009 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11010
11011 if (kvm_exception_is_soft(nr)) {
11012 vmcs12->vm_exit_instruction_len =
11013 vcpu->arch.event_exit_inst_len;
11014 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11015 } else
11016 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11017
11018 if (vcpu->arch.exception.has_error_code) {
11019 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11020 vmcs12->idt_vectoring_error_code =
11021 vcpu->arch.exception.error_code;
11022 }
11023
11024 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011025 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011026 vmcs12->idt_vectoring_info_field =
11027 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
11028 } else if (vcpu->arch.interrupt.pending) {
11029 nr = vcpu->arch.interrupt.nr;
11030 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11031
11032 if (vcpu->arch.interrupt.soft) {
11033 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11034 vmcs12->vm_entry_instruction_len =
11035 vcpu->arch.event_exit_inst_len;
11036 } else
11037 idt_vectoring |= INTR_TYPE_EXT_INTR;
11038
11039 vmcs12->idt_vectoring_info_field = idt_vectoring;
11040 }
11041}
11042
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011043static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11044{
11045 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011046 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020011047 bool block_nested_events =
11048 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011049
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011050 if (vcpu->arch.exception.pending &&
11051 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020011052 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011053 return -EBUSY;
11054 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
11055 vcpu->arch.exception.pending = false;
11056 return 0;
11057 }
11058
Jan Kiszkaf41245002014-03-07 20:03:13 +010011059 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11060 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020011061 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010011062 return -EBUSY;
11063 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11064 return 0;
11065 }
11066
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011067 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011068 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011069 return -EBUSY;
11070 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11071 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11072 INTR_INFO_VALID_MASK, 0);
11073 /*
11074 * The NMI-triggered VM exit counts as injection:
11075 * clear this one and block further NMIs.
11076 */
11077 vcpu->arch.nmi_pending = 0;
11078 vmx_set_nmi_mask(vcpu, true);
11079 return 0;
11080 }
11081
11082 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11083 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011084 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011085 return -EBUSY;
11086 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080011087 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011088 }
11089
David Hildenbrand6342c502017-01-25 11:58:58 +010011090 vmx_complete_nested_posted_interrupt(vcpu);
11091 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011092}
11093
Jan Kiszkaf41245002014-03-07 20:03:13 +010011094static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11095{
11096 ktime_t remaining =
11097 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11098 u64 value;
11099
11100 if (ktime_to_ns(remaining) <= 0)
11101 return 0;
11102
11103 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11104 do_div(value, 1000000);
11105 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11106}
11107
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011108/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011109 * Update the guest state fields of vmcs12 to reflect changes that
11110 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11111 * VM-entry controls is also updated, since this is really a guest
11112 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011113 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011114static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011115{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011116 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11117 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11118
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011119 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11120 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11121 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11122
11123 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11124 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11125 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11126 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11127 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11128 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11129 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11130 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11131 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11132 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11133 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11134 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11135 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11136 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11137 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11138 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11139 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11140 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11141 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11142 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11143 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11144 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11145 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11146 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11147 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11148 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11149 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11150 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11151 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11152 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11153 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11154 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11155 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11156 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11157 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11158 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11159
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011160 vmcs12->guest_interruptibility_info =
11161 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11162 vmcs12->guest_pending_dbg_exceptions =
11163 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010011164 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11165 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11166 else
11167 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011168
Jan Kiszkaf41245002014-03-07 20:03:13 +010011169 if (nested_cpu_has_preemption_timer(vmcs12)) {
11170 if (vmcs12->vm_exit_controls &
11171 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11172 vmcs12->vmx_preemption_timer_value =
11173 vmx_get_preemption_timer_value(vcpu);
11174 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11175 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080011176
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011177 /*
11178 * In some cases (usually, nested EPT), L2 is allowed to change its
11179 * own CR3 without exiting. If it has changed it, we must keep it.
11180 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11181 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11182 *
11183 * Additionally, restore L2's PDPTR to vmcs12.
11184 */
11185 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010011186 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011187 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11188 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11189 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11190 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11191 }
11192
Jim Mattsond281e132017-06-01 12:44:46 -070011193 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011194
Wincy Van608406e2015-02-03 23:57:51 +080011195 if (nested_cpu_has_vid(vmcs12))
11196 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11197
Jan Kiszkac18911a2013-03-13 16:06:41 +010011198 vmcs12->vm_entry_controls =
11199 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011200 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011201
Jan Kiszka2996fca2014-06-16 13:59:43 +020011202 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11203 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11204 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11205 }
11206
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011207 /* TODO: These cannot have changed unless we have MSR bitmaps and
11208 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011209 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011210 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011211 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11212 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011213 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11214 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11215 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011216 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011217 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011218}
11219
11220/*
11221 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11222 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11223 * and this function updates it to reflect the changes to the guest state while
11224 * L2 was running (and perhaps made some exits which were handled directly by L0
11225 * without going back to L1), and to reflect the exit reason.
11226 * Note that we do not have to copy here all VMCS fields, just those that
11227 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11228 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11229 * which already writes to vmcs12 directly.
11230 */
11231static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11232 u32 exit_reason, u32 exit_intr_info,
11233 unsigned long exit_qualification)
11234{
11235 /* update guest state fields: */
11236 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011237
11238 /* update exit information fields: */
11239
Jan Kiszka533558b2014-01-04 18:47:20 +010011240 vmcs12->vm_exit_reason = exit_reason;
11241 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010011242 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020011243
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011244 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011245 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11246 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11247
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011248 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070011249 vmcs12->launch_state = 1;
11250
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011251 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11252 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011253 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011254
11255 /*
11256 * Transfer the event that L0 or L1 may wanted to inject into
11257 * L2 to IDT_VECTORING_INFO_FIELD.
11258 */
11259 vmcs12_save_pending_event(vcpu, vmcs12);
11260 }
11261
11262 /*
11263 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11264 * preserved above and would only end up incorrectly in L1.
11265 */
11266 vcpu->arch.nmi_injected = false;
11267 kvm_clear_exception_queue(vcpu);
11268 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011269}
11270
Wanpeng Li5af41572017-11-05 16:54:49 -080011271static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11272 struct vmcs12 *vmcs12)
11273{
11274 u32 entry_failure_code;
11275
11276 nested_ept_uninit_mmu_context(vcpu);
11277
11278 /*
11279 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11280 * couldn't have changed.
11281 */
11282 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11283 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11284
11285 if (!enable_ept)
11286 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11287}
11288
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011289/*
11290 * A part of what we need to when the nested L2 guest exits and we want to
11291 * run its L1 parent, is to reset L1's guest state to the host state specified
11292 * in vmcs12.
11293 * This function is to be called not only on normal nested exit, but also on
11294 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11295 * Failures During or After Loading Guest State").
11296 * This function should be called when the active VMCS is L1's (vmcs01).
11297 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011298static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11299 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011300{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011301 struct kvm_segment seg;
11302
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011303 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11304 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011305 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011306 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11307 else
11308 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11309 vmx_set_efer(vcpu, vcpu->arch.efer);
11310
11311 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11312 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011313 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011314 /*
11315 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011316 * actually changed, because vmx_set_cr0 refers to efer set above.
11317 *
11318 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11319 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011320 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011321 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020011322 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011323
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011324 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011325 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080011326 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011327
Wanpeng Li5af41572017-11-05 16:54:49 -080011328 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011329
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011330 if (enable_vpid) {
11331 /*
11332 * Trivially support vpid by letting L2s share their parent
11333 * L1's vpid. TODO: move to a more elaborate solution, giving
11334 * each L2 its own vpid and exposing the vpid feature to L1.
11335 */
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011336 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011337 }
Wincy Van06a55242017-04-28 13:13:59 +080011338 /* Restore posted intr vector. */
11339 if (nested_cpu_has_posted_intr(vmcs12))
11340 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011341
11342 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11343 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11344 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11345 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11346 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d5512017-10-11 16:54:42 +020011347 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11348 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011349
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011350 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11351 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11352 vmcs_write64(GUEST_BNDCFGS, 0);
11353
Jan Kiszka44811c02013-08-04 17:17:27 +020011354 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011355 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011356 vcpu->arch.pat = vmcs12->host_ia32_pat;
11357 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011358 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11359 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11360 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011361
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011362 /* Set L1 segment info according to Intel SDM
11363 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11364 seg = (struct kvm_segment) {
11365 .base = 0,
11366 .limit = 0xFFFFFFFF,
11367 .selector = vmcs12->host_cs_selector,
11368 .type = 11,
11369 .present = 1,
11370 .s = 1,
11371 .g = 1
11372 };
11373 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11374 seg.l = 1;
11375 else
11376 seg.db = 1;
11377 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11378 seg = (struct kvm_segment) {
11379 .base = 0,
11380 .limit = 0xFFFFFFFF,
11381 .type = 3,
11382 .present = 1,
11383 .s = 1,
11384 .db = 1,
11385 .g = 1
11386 };
11387 seg.selector = vmcs12->host_ds_selector;
11388 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11389 seg.selector = vmcs12->host_es_selector;
11390 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11391 seg.selector = vmcs12->host_ss_selector;
11392 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11393 seg.selector = vmcs12->host_fs_selector;
11394 seg.base = vmcs12->host_fs_base;
11395 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11396 seg.selector = vmcs12->host_gs_selector;
11397 seg.base = vmcs12->host_gs_base;
11398 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11399 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011400 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011401 .limit = 0x67,
11402 .selector = vmcs12->host_tr_selector,
11403 .type = 11,
11404 .present = 1
11405 };
11406 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11407
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011408 kvm_set_dr(vcpu, 7, 0x400);
11409 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011410
Wincy Van3af18d92015-02-03 23:49:31 +080011411 if (cpu_has_vmx_msr_bitmap())
11412 vmx_set_msr_bitmap(vcpu);
11413
Wincy Vanff651cb2014-12-11 08:52:58 +030011414 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11415 vmcs12->vm_exit_msr_load_count))
11416 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011417}
11418
11419/*
11420 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11421 * and modify vmcs12 to make it see what it would expect to see there if
11422 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11423 */
Jan Kiszka533558b2014-01-04 18:47:20 +010011424static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11425 u32 exit_intr_info,
11426 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011427{
11428 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011429 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11430
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011431 /* trying to cancel vmlaunch/vmresume is a bug */
11432 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11433
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011434 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070011435 * The only expected VM-instruction error is "VM entry with
11436 * invalid control field(s)." Anything else indicates a
11437 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011438 */
Jim Mattson4f350c62017-09-14 16:31:44 -070011439 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
11440 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
11441
11442 leave_guest_mode(vcpu);
11443
11444 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011445 if (exit_reason == -1)
11446 sync_vmcs12(vcpu, vmcs12);
11447 else
11448 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11449 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070011450
11451 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11452 vmcs12->vm_exit_msr_store_count))
11453 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040011454 }
11455
Jim Mattson4f350c62017-09-14 16:31:44 -070011456 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020011457 vm_entry_controls_reset_shadow(vmx);
11458 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010011459 vmx_segment_cache_clear(vmx);
11460
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011461 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070011462 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11463 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011464 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011465 if (vmx->hv_deadline_tsc == -1)
11466 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11467 PIN_BASED_VMX_PREEMPTION_TIMER);
11468 else
11469 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11470 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070011471 if (kvm_has_tsc_control)
11472 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011473
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011474 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11475 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11476 vmx_set_virtual_x2apic_mode(vcpu,
11477 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011478 } else if (!nested_cpu_has_ept(vmcs12) &&
11479 nested_cpu_has2(vmcs12,
11480 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11481 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011482 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011483
11484 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11485 vmx->host_rsp = 0;
11486
11487 /* Unpin physical memory we referred to in vmcs02 */
11488 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011489 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011490 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011491 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011492 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011493 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011494 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011495 }
Wincy Van705699a2015-02-03 23:58:17 +080011496 if (vmx->nested.pi_desc_page) {
11497 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011498 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011499 vmx->nested.pi_desc_page = NULL;
11500 vmx->nested.pi_desc = NULL;
11501 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011502
11503 /*
Tang Chen38b99172014-09-24 15:57:54 +080011504 * We are now running in L2, mmu_notifier will force to reload the
11505 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11506 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011507 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011508
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011509 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030011510 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011511
11512 /* in case we halted in L2 */
11513 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070011514
11515 if (likely(!vmx->fail)) {
11516 /*
11517 * TODO: SDM says that with acknowledge interrupt on
11518 * exit, bit 31 of the VM-exit interrupt information
11519 * (valid interrupt) is always set to 1 on
11520 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
11521 * need kvm_cpu_has_interrupt(). See the commit
11522 * message for details.
11523 */
11524 if (nested_exit_intr_ack_set(vcpu) &&
11525 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
11526 kvm_cpu_has_interrupt(vcpu)) {
11527 int irq = kvm_cpu_get_interrupt(vcpu);
11528 WARN_ON(irq < 0);
11529 vmcs12->vm_exit_intr_info = irq |
11530 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11531 }
11532
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011533 if (exit_reason != -1)
11534 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11535 vmcs12->exit_qualification,
11536 vmcs12->idt_vectoring_info_field,
11537 vmcs12->vm_exit_intr_info,
11538 vmcs12->vm_exit_intr_error_code,
11539 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070011540
11541 load_vmcs12_host_state(vcpu, vmcs12);
11542
11543 return;
11544 }
11545
11546 /*
11547 * After an early L2 VM-entry failure, we're now back
11548 * in L1 which thinks it just finished a VMLAUNCH or
11549 * VMRESUME instruction, so we need to set the failure
11550 * flag and the VM-instruction error field of the VMCS
11551 * accordingly.
11552 */
11553 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080011554
11555 load_vmcs12_mmu_host_state(vcpu, vmcs12);
11556
Jim Mattson4f350c62017-09-14 16:31:44 -070011557 /*
11558 * The emulated instruction was already skipped in
11559 * nested_vmx_run, but the updated RIP was never
11560 * written back to the vmcs01.
11561 */
11562 skip_emulated_instruction(vcpu);
11563 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011564}
11565
Nadav Har'El7c177932011-05-25 23:12:04 +030011566/*
Jan Kiszka42124922014-01-04 18:47:19 +010011567 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11568 */
11569static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11570{
Wanpeng Li2f707d92017-03-06 04:03:28 -080011571 if (is_guest_mode(vcpu)) {
11572 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011573 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080011574 }
Jan Kiszka42124922014-01-04 18:47:19 +010011575 free_nested(to_vmx(vcpu));
11576}
11577
11578/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011579 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11580 * 23.7 "VM-entry failures during or after loading guest state" (this also
11581 * lists the acceptable exit-reason and exit-qualification parameters).
11582 * It should only be called before L2 actually succeeded to run, and when
11583 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11584 */
11585static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11586 struct vmcs12 *vmcs12,
11587 u32 reason, unsigned long qualification)
11588{
11589 load_vmcs12_host_state(vcpu, vmcs12);
11590 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11591 vmcs12->exit_qualification = qualification;
11592 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011593 if (enable_shadow_vmcs)
11594 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011595}
11596
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011597static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11598 struct x86_instruction_info *info,
11599 enum x86_intercept_stage stage)
11600{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020011601 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11602 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
11603
11604 /*
11605 * RDPID causes #UD if disabled through secondary execution controls.
11606 * Because it is marked as EmulateOnUD, we need to intercept it here.
11607 */
11608 if (info->intercept == x86_intercept_rdtscp &&
11609 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
11610 ctxt->exception.vector = UD_VECTOR;
11611 ctxt->exception.error_code_valid = false;
11612 return X86EMUL_PROPAGATE_FAULT;
11613 }
11614
11615 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011616 return X86EMUL_CONTINUE;
11617}
11618
Yunhong Jiang64672c92016-06-13 14:19:59 -070011619#ifdef CONFIG_X86_64
11620/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11621static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11622 u64 divisor, u64 *result)
11623{
11624 u64 low = a << shift, high = a >> (64 - shift);
11625
11626 /* To avoid the overflow on divq */
11627 if (high >= divisor)
11628 return 1;
11629
11630 /* Low hold the result, high hold rem which is discarded */
11631 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11632 "rm" (divisor), "0" (low), "1" (high));
11633 *result = low;
11634
11635 return 0;
11636}
11637
11638static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11639{
11640 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011641 u64 tscl = rdtsc();
11642 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11643 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011644
11645 /* Convert to host delta tsc if tsc scaling is enabled */
11646 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11647 u64_shl_div_u64(delta_tsc,
11648 kvm_tsc_scaling_ratio_frac_bits,
11649 vcpu->arch.tsc_scaling_ratio,
11650 &delta_tsc))
11651 return -ERANGE;
11652
11653 /*
11654 * If the delta tsc can't fit in the 32 bit after the multi shift,
11655 * we can't use the preemption timer.
11656 * It's possible that it fits on later vmentries, but checking
11657 * on every vmentry is costly so we just use an hrtimer.
11658 */
11659 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11660 return -ERANGE;
11661
11662 vmx->hv_deadline_tsc = tscl + delta_tsc;
11663 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11664 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070011665
11666 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011667}
11668
11669static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11670{
11671 struct vcpu_vmx *vmx = to_vmx(vcpu);
11672 vmx->hv_deadline_tsc = -1;
11673 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11674 PIN_BASED_VMX_PREEMPTION_TIMER);
11675}
11676#endif
11677
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011678static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011679{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011680 if (ple_gap)
11681 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011682}
11683
Kai Huang843e4332015-01-28 10:54:28 +080011684static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11685 struct kvm_memory_slot *slot)
11686{
11687 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11688 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11689}
11690
11691static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11692 struct kvm_memory_slot *slot)
11693{
11694 kvm_mmu_slot_set_dirty(kvm, slot);
11695}
11696
11697static void vmx_flush_log_dirty(struct kvm *kvm)
11698{
11699 kvm_flush_pml_buffers(kvm);
11700}
11701
Bandan Dasc5f983f2017-05-05 15:25:14 -040011702static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
11703{
11704 struct vmcs12 *vmcs12;
11705 struct vcpu_vmx *vmx = to_vmx(vcpu);
11706 gpa_t gpa;
11707 struct page *page = NULL;
11708 u64 *pml_address;
11709
11710 if (is_guest_mode(vcpu)) {
11711 WARN_ON_ONCE(vmx->nested.pml_full);
11712
11713 /*
11714 * Check if PML is enabled for the nested guest.
11715 * Whether eptp bit 6 is set is already checked
11716 * as part of A/D emulation.
11717 */
11718 vmcs12 = get_vmcs12(vcpu);
11719 if (!nested_cpu_has_pml(vmcs12))
11720 return 0;
11721
Dan Carpenter47698862017-05-10 22:43:17 +030011722 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040011723 vmx->nested.pml_full = true;
11724 return 1;
11725 }
11726
11727 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
11728
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011729 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
11730 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040011731 return 0;
11732
11733 pml_address = kmap(page);
11734 pml_address[vmcs12->guest_pml_index--] = gpa;
11735 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011736 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011737 }
11738
11739 return 0;
11740}
11741
Kai Huang843e4332015-01-28 10:54:28 +080011742static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11743 struct kvm_memory_slot *memslot,
11744 gfn_t offset, unsigned long mask)
11745{
11746 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11747}
11748
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011749static void __pi_post_block(struct kvm_vcpu *vcpu)
11750{
11751 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11752 struct pi_desc old, new;
11753 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011754
11755 do {
11756 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011757 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
11758 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011759
11760 dest = cpu_physical_id(vcpu->cpu);
11761
11762 if (x2apic_enabled())
11763 new.ndst = dest;
11764 else
11765 new.ndst = (dest << 8) & 0xFF00;
11766
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011767 /* set 'NV' to 'notification vector' */
11768 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020011769 } while (cmpxchg64(&pi_desc->control, old.control,
11770 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011771
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011772 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
11773 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011774 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011775 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011776 vcpu->pre_pcpu = -1;
11777 }
11778}
11779
Feng Wuefc64402015-09-18 22:29:51 +080011780/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080011781 * This routine does the following things for vCPU which is going
11782 * to be blocked if VT-d PI is enabled.
11783 * - Store the vCPU to the wakeup list, so when interrupts happen
11784 * we can find the right vCPU to wake up.
11785 * - Change the Posted-interrupt descriptor as below:
11786 * 'NDST' <-- vcpu->pre_pcpu
11787 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11788 * - If 'ON' is set during this process, which means at least one
11789 * interrupt is posted for this vCPU, we cannot block it, in
11790 * this case, return 1, otherwise, return 0.
11791 *
11792 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070011793static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011794{
Feng Wubf9f6ac2015-09-18 22:29:55 +080011795 unsigned int dest;
11796 struct pi_desc old, new;
11797 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11798
11799 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011800 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11801 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011802 return 0;
11803
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011804 WARN_ON(irqs_disabled());
11805 local_irq_disable();
11806 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
11807 vcpu->pre_pcpu = vcpu->cpu;
11808 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11809 list_add_tail(&vcpu->blocked_vcpu_list,
11810 &per_cpu(blocked_vcpu_on_cpu,
11811 vcpu->pre_pcpu));
11812 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11813 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080011814
11815 do {
11816 old.control = new.control = pi_desc->control;
11817
Feng Wubf9f6ac2015-09-18 22:29:55 +080011818 WARN((pi_desc->sn == 1),
11819 "Warning: SN field of posted-interrupts "
11820 "is set before blocking\n");
11821
11822 /*
11823 * Since vCPU can be preempted during this process,
11824 * vcpu->cpu could be different with pre_pcpu, we
11825 * need to set pre_pcpu as the destination of wakeup
11826 * notification event, then we can find the right vCPU
11827 * to wakeup in wakeup handler if interrupts happen
11828 * when the vCPU is in blocked state.
11829 */
11830 dest = cpu_physical_id(vcpu->pre_pcpu);
11831
11832 if (x2apic_enabled())
11833 new.ndst = dest;
11834 else
11835 new.ndst = (dest << 8) & 0xFF00;
11836
11837 /* set 'NV' to 'wakeup vector' */
11838 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020011839 } while (cmpxchg64(&pi_desc->control, old.control,
11840 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011841
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011842 /* We should not block the vCPU if an interrupt is posted for it. */
11843 if (pi_test_on(pi_desc) == 1)
11844 __pi_post_block(vcpu);
11845
11846 local_irq_enable();
11847 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011848}
11849
Yunhong Jiangbc225122016-06-13 14:19:58 -070011850static int vmx_pre_block(struct kvm_vcpu *vcpu)
11851{
11852 if (pi_pre_block(vcpu))
11853 return 1;
11854
Yunhong Jiang64672c92016-06-13 14:19:59 -070011855 if (kvm_lapic_hv_timer_in_use(vcpu))
11856 kvm_lapic_switch_to_sw_timer(vcpu);
11857
Yunhong Jiangbc225122016-06-13 14:19:58 -070011858 return 0;
11859}
11860
11861static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011862{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011863 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011864 return;
11865
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011866 WARN_ON(irqs_disabled());
11867 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011868 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011869 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080011870}
11871
Yunhong Jiangbc225122016-06-13 14:19:58 -070011872static void vmx_post_block(struct kvm_vcpu *vcpu)
11873{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011874 if (kvm_x86_ops->set_hv_timer)
11875 kvm_lapic_switch_to_hv_timer(vcpu);
11876
Yunhong Jiangbc225122016-06-13 14:19:58 -070011877 pi_post_block(vcpu);
11878}
11879
Feng Wubf9f6ac2015-09-18 22:29:55 +080011880/*
Feng Wuefc64402015-09-18 22:29:51 +080011881 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11882 *
11883 * @kvm: kvm
11884 * @host_irq: host irq of the interrupt
11885 * @guest_irq: gsi of the interrupt
11886 * @set: set or unset PI
11887 * returns 0 on success, < 0 on failure
11888 */
11889static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11890 uint32_t guest_irq, bool set)
11891{
11892 struct kvm_kernel_irq_routing_entry *e;
11893 struct kvm_irq_routing_table *irq_rt;
11894 struct kvm_lapic_irq irq;
11895 struct kvm_vcpu *vcpu;
11896 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010011897 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080011898
11899 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011900 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11901 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011902 return 0;
11903
11904 idx = srcu_read_lock(&kvm->irq_srcu);
11905 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010011906 if (guest_irq >= irq_rt->nr_rt_entries ||
11907 hlist_empty(&irq_rt->map[guest_irq])) {
11908 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
11909 guest_irq, irq_rt->nr_rt_entries);
11910 goto out;
11911 }
Feng Wuefc64402015-09-18 22:29:51 +080011912
11913 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11914 if (e->type != KVM_IRQ_ROUTING_MSI)
11915 continue;
11916 /*
11917 * VT-d PI cannot support posting multicast/broadcast
11918 * interrupts to a vCPU, we still use interrupt remapping
11919 * for these kind of interrupts.
11920 *
11921 * For lowest-priority interrupts, we only support
11922 * those with single CPU as the destination, e.g. user
11923 * configures the interrupts via /proc/irq or uses
11924 * irqbalance to make the interrupts single-CPU.
11925 *
11926 * We will support full lowest-priority interrupt later.
11927 */
11928
Radim Krčmář371313132016-07-12 22:09:27 +020011929 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011930 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11931 /*
11932 * Make sure the IRTE is in remapped mode if
11933 * we don't handle it in posted mode.
11934 */
11935 ret = irq_set_vcpu_affinity(host_irq, NULL);
11936 if (ret < 0) {
11937 printk(KERN_INFO
11938 "failed to back to remapped mode, irq: %u\n",
11939 host_irq);
11940 goto out;
11941 }
11942
Feng Wuefc64402015-09-18 22:29:51 +080011943 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011944 }
Feng Wuefc64402015-09-18 22:29:51 +080011945
11946 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11947 vcpu_info.vector = irq.vector;
11948
Feng Wub6ce9782016-01-25 16:53:35 +080011949 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011950 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11951
11952 if (set)
11953 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2eb2017-09-18 09:56:49 +080011954 else
Feng Wuefc64402015-09-18 22:29:51 +080011955 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080011956
11957 if (ret < 0) {
11958 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11959 __func__);
11960 goto out;
11961 }
11962 }
11963
11964 ret = 0;
11965out:
11966 srcu_read_unlock(&kvm->irq_srcu, idx);
11967 return ret;
11968}
11969
Ashok Rajc45dcc72016-06-22 14:59:56 +080011970static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11971{
11972 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11973 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11974 FEATURE_CONTROL_LMCE;
11975 else
11976 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11977 ~FEATURE_CONTROL_LMCE;
11978}
11979
Ladi Prosek72d7b372017-10-11 16:54:41 +020011980static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
11981{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011982 /* we need a nested vmexit to enter SMM, postpone if run is pending */
11983 if (to_vmx(vcpu)->nested.nested_run_pending)
11984 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020011985 return 1;
11986}
11987
Ladi Prosek0234bf82017-10-11 16:54:40 +020011988static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
11989{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011990 struct vcpu_vmx *vmx = to_vmx(vcpu);
11991
11992 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
11993 if (vmx->nested.smm.guest_mode)
11994 nested_vmx_vmexit(vcpu, -1, 0, 0);
11995
11996 vmx->nested.smm.vmxon = vmx->nested.vmxon;
11997 vmx->nested.vmxon = false;
Ladi Prosek0234bf82017-10-11 16:54:40 +020011998 return 0;
11999}
12000
12001static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12002{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012003 struct vcpu_vmx *vmx = to_vmx(vcpu);
12004 int ret;
12005
12006 if (vmx->nested.smm.vmxon) {
12007 vmx->nested.vmxon = true;
12008 vmx->nested.smm.vmxon = false;
12009 }
12010
12011 if (vmx->nested.smm.guest_mode) {
12012 vcpu->arch.hflags &= ~HF_SMM_MASK;
12013 ret = enter_vmx_non_root_mode(vcpu, false);
12014 vcpu->arch.hflags |= HF_SMM_MASK;
12015 if (ret)
12016 return ret;
12017
12018 vmx->nested.smm.guest_mode = false;
12019 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020012020 return 0;
12021}
12022
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012023static int enable_smi_window(struct kvm_vcpu *vcpu)
12024{
12025 return 0;
12026}
12027
Kees Cook404f6aa2016-08-08 16:29:06 -070012028static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080012029 .cpu_has_kvm_support = cpu_has_kvm_support,
12030 .disabled_by_bios = vmx_disabled_by_bios,
12031 .hardware_setup = hardware_setup,
12032 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030012033 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012034 .hardware_enable = hardware_enable,
12035 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080012036 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020012037 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012038
12039 .vcpu_create = vmx_create_vcpu,
12040 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030012041 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012042
Avi Kivity04d2cc72007-09-10 18:10:54 +030012043 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012044 .vcpu_load = vmx_vcpu_load,
12045 .vcpu_put = vmx_vcpu_put,
12046
Paolo Bonzinia96036b2015-11-10 11:55:36 +010012047 .update_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012048 .get_msr = vmx_get_msr,
12049 .set_msr = vmx_set_msr,
12050 .get_segment_base = vmx_get_segment_base,
12051 .get_segment = vmx_get_segment,
12052 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020012053 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012054 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020012055 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020012056 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030012057 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012058 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012059 .set_cr3 = vmx_set_cr3,
12060 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012061 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012062 .get_idt = vmx_get_idt,
12063 .set_idt = vmx_set_idt,
12064 .get_gdt = vmx_get_gdt,
12065 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010012066 .get_dr6 = vmx_get_dr6,
12067 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030012068 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010012069 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030012070 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012071 .get_rflags = vmx_get_rflags,
12072 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080012073
Avi Kivity6aa8b732006-12-10 02:21:36 -080012074 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012075
Avi Kivity6aa8b732006-12-10 02:21:36 -080012076 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020012077 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012078 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040012079 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12080 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020012081 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030012082 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012083 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020012084 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030012085 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020012086 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012087 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010012088 .get_nmi_mask = vmx_get_nmi_mask,
12089 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012090 .enable_nmi_window = enable_nmi_window,
12091 .enable_irq_window = enable_irq_window,
12092 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080012093 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080012094 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030012095 .get_enable_apicv = vmx_get_enable_apicv,
12096 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012097 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010012098 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012099 .hwapic_irr_update = vmx_hwapic_irr_update,
12100 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080012101 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12102 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012103
Izik Eiduscbc94022007-10-25 00:29:55 +020012104 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080012105 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080012106 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030012107
Avi Kivity586f9602010-11-18 13:09:54 +020012108 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020012109
Sheng Yang17cc3932010-01-05 19:02:27 +080012110 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080012111
12112 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080012113
12114 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000012115 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020012116
12117 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080012118
12119 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012120
12121 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020012122
12123 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012124
12125 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080012126 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000012127 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080012128 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020012129 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012130
12131 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012132
12133 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080012134
12135 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12136 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12137 .flush_log_dirty = vmx_flush_log_dirty,
12138 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040012139 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020012140
Feng Wubf9f6ac2015-09-18 22:29:55 +080012141 .pre_block = vmx_pre_block,
12142 .post_block = vmx_post_block,
12143
Wei Huang25462f72015-06-19 15:45:05 +020012144 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080012145
12146 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070012147
12148#ifdef CONFIG_X86_64
12149 .set_hv_timer = vmx_set_hv_timer,
12150 .cancel_hv_timer = vmx_cancel_hv_timer,
12151#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080012152
12153 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012154
Ladi Prosek72d7b372017-10-11 16:54:41 +020012155 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012156 .pre_enter_smm = vmx_pre_enter_smm,
12157 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012158 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012159};
12160
12161static int __init vmx_init(void)
12162{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012163 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
12164 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030012165 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012166 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080012167
Dave Young2965faa2015-09-09 15:38:55 -070012168#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012169 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12170 crash_vmclear_local_loaded_vmcss);
12171#endif
12172
He, Qingfdef3ad2007-04-30 09:45:24 +030012173 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080012174}
12175
12176static void __exit vmx_exit(void)
12177{
Dave Young2965faa2015-09-09 15:38:55 -070012178#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053012179 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012180 synchronize_rcu();
12181#endif
12182
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080012183 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080012184}
12185
12186module_init(vmx_init)
12187module_exit(vmx_exit)