blob: d49bb747ebea53a9d240f9fe19477034d8b37940 [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/*
Jim Mattson5b157062017-12-22 12:11:12 -0800411 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
412 * supported VMCS12 field encoding.
413 */
414#define VMCS12_MAX_FIELD_INDEX 0x17
415
416/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300417 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
418 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
419 */
420struct nested_vmx {
421 /* Has the level1 guest done vmxon? */
422 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400423 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400424 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300425
426 /* The guest-physical address of the current VMCS L1 keeps for L2 */
427 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700428 /*
429 * Cache of the guest's VMCS, existing outside of guest memory.
430 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700431 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700432 */
433 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300434 /*
435 * Indicates if the shadow vmcs must be updated with the
436 * data hold by vmcs12
437 */
438 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300439
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200440 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300441 /* L2 must run next, and mustn't decide to exit to L1. */
442 bool nested_run_pending;
Jim Mattson00647b42017-11-27 17:22:25 -0600443
444 struct loaded_vmcs vmcs02;
445
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300446 /*
Jim Mattson00647b42017-11-27 17:22:25 -0600447 * Guest pages referred to in the vmcs02 with host-physical
448 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300449 */
450 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800451 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800452 struct page *pi_desc_page;
453 struct pi_desc *pi_desc;
454 bool pi_pending;
455 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100456
Radim Krčmářd048c092016-08-08 20:16:22 +0200457 unsigned long *msr_bitmap;
458
Jan Kiszkaf41245002014-03-07 20:03:13 +0100459 struct hrtimer preemption_timer;
460 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200461
462 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
463 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800464
Wanpeng Li5c614b32015-10-13 09:18:36 -0700465 u16 vpid02;
466 u16 last_vpid;
467
David Matlack0115f9c2016-11-29 18:14:06 -0800468 /*
469 * We only store the "true" versions of the VMX capability MSRs. We
470 * generate the "non-true" versions by setting the must-be-1 bits
471 * according to the SDM.
472 */
Wincy Vanb9c237b2015-02-03 23:56:30 +0800473 u32 nested_vmx_procbased_ctls_low;
474 u32 nested_vmx_procbased_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800475 u32 nested_vmx_secondary_ctls_low;
476 u32 nested_vmx_secondary_ctls_high;
477 u32 nested_vmx_pinbased_ctls_low;
478 u32 nested_vmx_pinbased_ctls_high;
479 u32 nested_vmx_exit_ctls_low;
480 u32 nested_vmx_exit_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800481 u32 nested_vmx_entry_ctls_low;
482 u32 nested_vmx_entry_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800483 u32 nested_vmx_misc_low;
484 u32 nested_vmx_misc_high;
485 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700486 u32 nested_vmx_vpid_caps;
David Matlack62cc6b9d2016-11-29 18:14:07 -0800487 u64 nested_vmx_basic;
488 u64 nested_vmx_cr0_fixed0;
489 u64 nested_vmx_cr0_fixed1;
490 u64 nested_vmx_cr4_fixed0;
491 u64 nested_vmx_cr4_fixed1;
492 u64 nested_vmx_vmcs_enum;
Bandan Das27c42a12017-08-03 15:54:42 -0400493 u64 nested_vmx_vmfunc_controls;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200494
495 /* SMM related state */
496 struct {
497 /* in VMX operation on SMM entry? */
498 bool vmxon;
499 /* in guest mode on SMM entry? */
500 bool guest_mode;
501 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300502};
503
Yang Zhang01e439b2013-04-11 19:25:12 +0800504#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800505#define POSTED_INTR_SN 1
506
Yang Zhang01e439b2013-04-11 19:25:12 +0800507/* Posted-Interrupt Descriptor */
508struct pi_desc {
509 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800510 union {
511 struct {
512 /* bit 256 - Outstanding Notification */
513 u16 on : 1,
514 /* bit 257 - Suppress Notification */
515 sn : 1,
516 /* bit 271:258 - Reserved */
517 rsvd_1 : 14;
518 /* bit 279:272 - Notification Vector */
519 u8 nv;
520 /* bit 287:280 - Reserved */
521 u8 rsvd_2;
522 /* bit 319:288 - Notification Destination */
523 u32 ndst;
524 };
525 u64 control;
526 };
527 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800528} __aligned(64);
529
Yang Zhanga20ed542013-04-11 19:25:15 +0800530static bool pi_test_and_set_on(struct pi_desc *pi_desc)
531{
532 return test_and_set_bit(POSTED_INTR_ON,
533 (unsigned long *)&pi_desc->control);
534}
535
536static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
537{
538 return test_and_clear_bit(POSTED_INTR_ON,
539 (unsigned long *)&pi_desc->control);
540}
541
542static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
543{
544 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
545}
546
Feng Wuebbfc762015-09-18 22:29:46 +0800547static inline void pi_clear_sn(struct pi_desc *pi_desc)
548{
549 return clear_bit(POSTED_INTR_SN,
550 (unsigned long *)&pi_desc->control);
551}
552
553static inline void pi_set_sn(struct pi_desc *pi_desc)
554{
555 return set_bit(POSTED_INTR_SN,
556 (unsigned long *)&pi_desc->control);
557}
558
Paolo Bonziniad361092016-09-20 16:15:05 +0200559static inline void pi_clear_on(struct pi_desc *pi_desc)
560{
561 clear_bit(POSTED_INTR_ON,
562 (unsigned long *)&pi_desc->control);
563}
564
Feng Wuebbfc762015-09-18 22:29:46 +0800565static inline int pi_test_on(struct pi_desc *pi_desc)
566{
567 return test_bit(POSTED_INTR_ON,
568 (unsigned long *)&pi_desc->control);
569}
570
571static inline int pi_test_sn(struct pi_desc *pi_desc)
572{
573 return test_bit(POSTED_INTR_SN,
574 (unsigned long *)&pi_desc->control);
575}
576
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400577struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000578 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300579 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300580 u8 fail;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300581 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200582 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200583 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300584 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400585 int nmsrs;
586 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800587 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400588#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300589 u64 msr_host_kernel_gs_base;
590 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400591#endif
Gleb Natapov2961e8762013-11-25 15:37:13 +0200592 u32 vm_entry_controls_shadow;
593 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200594 u32 secondary_exec_control;
595
Nadav Har'Eld462b812011-05-24 15:26:10 +0300596 /*
597 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
598 * non-nested (L1) guest, it always points to vmcs01. For a nested
599 * guest (L2), it points to a different VMCS.
600 */
601 struct loaded_vmcs vmcs01;
602 struct loaded_vmcs *loaded_vmcs;
603 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300604 struct msr_autoload {
605 unsigned nr;
606 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
607 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
608 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400609 struct {
610 int loaded;
611 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300612#ifdef CONFIG_X86_64
613 u16 ds_sel, es_sel;
614#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200615 int gs_ldt_reload_needed;
616 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000617 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400618 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200619 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300620 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300621 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300622 struct kvm_segment segs[8];
623 } rmode;
624 struct {
625 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300626 struct kvm_save_segment {
627 u16 selector;
628 unsigned long base;
629 u32 limit;
630 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300631 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300632 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800633 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300634 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200635
Andi Kleena0861c02009-06-08 17:37:09 +0800636 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800637
Yang Zhang01e439b2013-04-11 19:25:12 +0800638 /* Posted interrupt descriptor */
639 struct pi_desc pi_desc;
640
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300641 /* Support for a guest hypervisor (nested VMX) */
642 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200643
644 /* Dynamic PLE window. */
645 int ple_window;
646 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800647
648 /* Support for PML */
649#define PML_ENTITY_NUM 512
650 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800651
Yunhong Jiang64672c92016-06-13 14:19:59 -0700652 /* apic deadline value in host tsc */
653 u64 hv_deadline_tsc;
654
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800655 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800656
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800657 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800658
Wanpeng Li74c55932017-11-29 01:31:20 -0800659 unsigned long host_debugctlmsr;
660
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800661 /*
662 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
663 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
664 * in msr_ia32_feature_control_valid_bits.
665 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800666 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800667 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400668};
669
Avi Kivity2fb92db2011-04-27 19:42:18 +0300670enum segment_cache_field {
671 SEG_FIELD_SEL = 0,
672 SEG_FIELD_BASE = 1,
673 SEG_FIELD_LIMIT = 2,
674 SEG_FIELD_AR = 3,
675
676 SEG_FIELD_NR = 4
677};
678
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400679static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
680{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000681 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400682}
683
Feng Wuefc64402015-09-18 22:29:51 +0800684static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
685{
686 return &(to_vmx(vcpu)->pi_desc);
687}
688
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800689#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300690#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800691#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
692#define FIELD64(number, name) \
693 FIELD(number, name), \
694 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300695
Abel Gordon4607c2d2013-04-18 14:35:55 +0300696
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100697static u16 shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300698 /*
699 * We do NOT shadow fields that are modified when L0
700 * traps and emulates any vmx instruction (e.g. VMPTRLD,
701 * VMXON...) executed by L1.
702 * For example, VM_INSTRUCTION_ERROR is read
703 * by L1 if a vmx instruction fails (part of the error path).
704 * Note the code assumes this logic. If for some reason
705 * we start shadowing these fields then we need to
706 * force a shadow sync when L0 emulates vmx instructions
707 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
708 * by nested_vmx_failValid)
709 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100710 /* 32-bits */
Abel Gordon4607c2d2013-04-18 14:35:55 +0300711 VM_EXIT_REASON,
712 VM_EXIT_INTR_INFO,
713 VM_EXIT_INSTRUCTION_LEN,
714 IDT_VECTORING_INFO_FIELD,
715 IDT_VECTORING_ERROR_CODE,
716 VM_EXIT_INTR_ERROR_CODE,
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100717
718 /* Natural width */
Abel Gordon4607c2d2013-04-18 14:35:55 +0300719 EXIT_QUALIFICATION,
720 GUEST_LINEAR_ADDRESS,
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100721
722 /* 64-bit */
723 GUEST_PHYSICAL_ADDRESS,
724 GUEST_PHYSICAL_ADDRESS_HIGH,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300725};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400726static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300727 ARRAY_SIZE(shadow_read_only_fields);
728
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100729static u16 shadow_read_write_fields[] = {
730 /* 16-bits */
731 GUEST_CS_SELECTOR,
732 GUEST_INTR_STATUS,
733 GUEST_PML_INDEX,
734 HOST_FS_SELECTOR,
735 HOST_GS_SELECTOR,
736
737 /* 32-bits */
738 CPU_BASED_VM_EXEC_CONTROL,
739 EXCEPTION_BITMAP,
740 VM_ENTRY_EXCEPTION_ERROR_CODE,
741 VM_ENTRY_INTR_INFO_FIELD,
742 VM_ENTRY_INSTRUCTION_LEN,
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800743 TPR_THRESHOLD,
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100744 GUEST_CS_LIMIT,
745 GUEST_CS_AR_BYTES,
746 GUEST_INTERRUPTIBILITY_INFO,
747 VMX_PREEMPTION_TIMER_VALUE,
748
749 /* Natural width */
Abel Gordon4607c2d2013-04-18 14:35:55 +0300750 GUEST_RIP,
751 GUEST_RSP,
752 GUEST_CR0,
753 GUEST_CR3,
754 GUEST_CR4,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300755 GUEST_RFLAGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300756 GUEST_CS_BASE,
757 GUEST_ES_BASE,
758 CR0_GUEST_HOST_MASK,
759 CR0_READ_SHADOW,
760 CR4_READ_SHADOW,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300761 HOST_FS_BASE,
762 HOST_GS_BASE,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300763};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400764static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300765 ARRAY_SIZE(shadow_read_write_fields);
766
Mathias Krause772e0312012-08-30 01:30:19 +0200767static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300768 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800769 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300770 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
771 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
772 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
773 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
774 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
775 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
776 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
777 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800778 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400779 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300780 FIELD(HOST_ES_SELECTOR, host_es_selector),
781 FIELD(HOST_CS_SELECTOR, host_cs_selector),
782 FIELD(HOST_SS_SELECTOR, host_ss_selector),
783 FIELD(HOST_DS_SELECTOR, host_ds_selector),
784 FIELD(HOST_FS_SELECTOR, host_fs_selector),
785 FIELD(HOST_GS_SELECTOR, host_gs_selector),
786 FIELD(HOST_TR_SELECTOR, host_tr_selector),
787 FIELD64(IO_BITMAP_A, io_bitmap_a),
788 FIELD64(IO_BITMAP_B, io_bitmap_b),
789 FIELD64(MSR_BITMAP, msr_bitmap),
790 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
791 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
792 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
793 FIELD64(TSC_OFFSET, tsc_offset),
794 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
795 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800796 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400797 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300798 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800799 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
800 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
801 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
802 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400803 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800804 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300805 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
806 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400807 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300808 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
809 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
810 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
811 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
812 FIELD64(GUEST_PDPTR0, guest_pdptr0),
813 FIELD64(GUEST_PDPTR1, guest_pdptr1),
814 FIELD64(GUEST_PDPTR2, guest_pdptr2),
815 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100816 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300817 FIELD64(HOST_IA32_PAT, host_ia32_pat),
818 FIELD64(HOST_IA32_EFER, host_ia32_efer),
819 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
820 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
821 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
822 FIELD(EXCEPTION_BITMAP, exception_bitmap),
823 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
824 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
825 FIELD(CR3_TARGET_COUNT, cr3_target_count),
826 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
827 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
828 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
829 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
830 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
831 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
832 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
833 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
834 FIELD(TPR_THRESHOLD, tpr_threshold),
835 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
836 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
837 FIELD(VM_EXIT_REASON, vm_exit_reason),
838 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
839 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
840 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
841 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
842 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
843 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
844 FIELD(GUEST_ES_LIMIT, guest_es_limit),
845 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
846 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
847 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
848 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
849 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
850 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
851 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
852 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
853 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
854 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
855 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
856 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
857 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
858 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
859 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
860 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
861 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
862 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
863 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
864 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
865 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100866 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300867 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
868 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
869 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
870 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
871 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
872 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
873 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
874 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
875 FIELD(EXIT_QUALIFICATION, exit_qualification),
876 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
877 FIELD(GUEST_CR0, guest_cr0),
878 FIELD(GUEST_CR3, guest_cr3),
879 FIELD(GUEST_CR4, guest_cr4),
880 FIELD(GUEST_ES_BASE, guest_es_base),
881 FIELD(GUEST_CS_BASE, guest_cs_base),
882 FIELD(GUEST_SS_BASE, guest_ss_base),
883 FIELD(GUEST_DS_BASE, guest_ds_base),
884 FIELD(GUEST_FS_BASE, guest_fs_base),
885 FIELD(GUEST_GS_BASE, guest_gs_base),
886 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
887 FIELD(GUEST_TR_BASE, guest_tr_base),
888 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
889 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
890 FIELD(GUEST_DR7, guest_dr7),
891 FIELD(GUEST_RSP, guest_rsp),
892 FIELD(GUEST_RIP, guest_rip),
893 FIELD(GUEST_RFLAGS, guest_rflags),
894 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
895 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
896 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
897 FIELD(HOST_CR0, host_cr0),
898 FIELD(HOST_CR3, host_cr3),
899 FIELD(HOST_CR4, host_cr4),
900 FIELD(HOST_FS_BASE, host_fs_base),
901 FIELD(HOST_GS_BASE, host_gs_base),
902 FIELD(HOST_TR_BASE, host_tr_base),
903 FIELD(HOST_GDTR_BASE, host_gdtr_base),
904 FIELD(HOST_IDTR_BASE, host_idtr_base),
905 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
906 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
907 FIELD(HOST_RSP, host_rsp),
908 FIELD(HOST_RIP, host_rip),
909};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300910
911static inline short vmcs_field_to_offset(unsigned long field)
912{
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800913 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100914
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800915 if (field >> 15)
916 return -ENOENT;
917
918 index = ROL16(field, 6);
919 if (index >= ARRAY_SIZE(vmcs_field_to_offset_table))
Andrew Honig75f139a2018-01-10 10:12:03 -0800920 return -ENOENT;
921
922 /*
923 * FIXME: Mitigation for CVE-2017-5753. To be replaced with a
924 * generic mechanism.
925 */
926 asm("lfence");
927
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800928 if (vmcs_field_to_offset_table[index] == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100929 return -ENOENT;
930
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800931 return vmcs_field_to_offset_table[index];
Nadav Har'El22bd0352011-05-25 23:05:57 +0300932}
933
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300934static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
935{
David Matlack4f2777b2016-07-13 17:16:37 -0700936 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300937}
938
Peter Feiner995f00a2017-06-30 17:26:32 -0700939static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300940static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -0700941static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800942static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300943static void vmx_set_segment(struct kvm_vcpu *vcpu,
944 struct kvm_segment *var, int seg);
945static void vmx_get_segment(struct kvm_vcpu *vcpu,
946 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200947static bool guest_state_valid(struct kvm_vcpu *vcpu);
948static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +0300949static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +0200950static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
951static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
952static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
953 u16 error_code);
Avi Kivity75880a02007-06-20 11:20:04 +0300954
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955static DEFINE_PER_CPU(struct vmcs *, vmxarea);
956static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300957/*
958 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
959 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
960 */
961static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962
Feng Wubf9f6ac2015-09-18 22:29:55 +0800963/*
964 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
965 * can find which vCPU should be waken up.
966 */
967static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
968static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
969
Radim Krčmář23611332016-09-29 22:41:33 +0200970enum {
Radim Krčmář23611332016-09-29 22:41:33 +0200971 VMX_MSR_BITMAP_LEGACY,
972 VMX_MSR_BITMAP_LONGMODE,
973 VMX_MSR_BITMAP_LEGACY_X2APIC_APICV,
974 VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV,
975 VMX_MSR_BITMAP_LEGACY_X2APIC,
976 VMX_MSR_BITMAP_LONGMODE_X2APIC,
977 VMX_VMREAD_BITMAP,
978 VMX_VMWRITE_BITMAP,
979 VMX_BITMAP_NR
980};
981
982static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
983
Radim Krčmář23611332016-09-29 22:41:33 +0200984#define vmx_msr_bitmap_legacy (vmx_bitmap[VMX_MSR_BITMAP_LEGACY])
985#define vmx_msr_bitmap_longmode (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE])
986#define vmx_msr_bitmap_legacy_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC_APICV])
987#define vmx_msr_bitmap_longmode_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV])
988#define vmx_msr_bitmap_legacy_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC])
989#define vmx_msr_bitmap_longmode_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC])
990#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
991#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +0300992
Avi Kivity110312c2010-12-21 12:54:20 +0200993static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200994static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200995
Sheng Yang2384d2b2008-01-17 15:14:33 +0800996static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
997static DEFINE_SPINLOCK(vmx_vpid_lock);
998
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300999static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000 int size;
1001 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001002 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001004 u32 pin_based_exec_ctrl;
1005 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001006 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001007 u32 vmexit_ctrl;
1008 u32 vmentry_ctrl;
1009} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010
Hannes Ederefff9e52008-11-28 17:02:06 +01001011static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001012 u32 ept;
1013 u32 vpid;
1014} vmx_capability;
1015
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016#define VMX_SEGMENT_FIELD(seg) \
1017 [VCPU_SREG_##seg] = { \
1018 .selector = GUEST_##seg##_SELECTOR, \
1019 .base = GUEST_##seg##_BASE, \
1020 .limit = GUEST_##seg##_LIMIT, \
1021 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1022 }
1023
Mathias Krause772e0312012-08-30 01:30:19 +02001024static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025 unsigned selector;
1026 unsigned base;
1027 unsigned limit;
1028 unsigned ar_bytes;
1029} kvm_vmx_segment_fields[] = {
1030 VMX_SEGMENT_FIELD(CS),
1031 VMX_SEGMENT_FIELD(DS),
1032 VMX_SEGMENT_FIELD(ES),
1033 VMX_SEGMENT_FIELD(FS),
1034 VMX_SEGMENT_FIELD(GS),
1035 VMX_SEGMENT_FIELD(SS),
1036 VMX_SEGMENT_FIELD(TR),
1037 VMX_SEGMENT_FIELD(LDTR),
1038};
1039
Avi Kivity26bb0982009-09-07 11:14:12 +03001040static u64 host_efer;
1041
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001042static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1043
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001044/*
Brian Gerst8c065852010-07-17 09:03:26 -04001045 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001046 * away by decrementing the array size.
1047 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001049#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001050 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001052 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001054
Jan Kiszka5bb16012016-02-09 20:14:21 +01001055static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056{
1057 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1058 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001059 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1060}
1061
Jan Kiszka6f054852016-02-09 20:15:18 +01001062static inline bool is_debug(u32 intr_info)
1063{
1064 return is_exception_n(intr_info, DB_VECTOR);
1065}
1066
1067static inline bool is_breakpoint(u32 intr_info)
1068{
1069 return is_exception_n(intr_info, BP_VECTOR);
1070}
1071
Jan Kiszka5bb16012016-02-09 20:14:21 +01001072static inline bool is_page_fault(u32 intr_info)
1073{
1074 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075}
1076
Gui Jianfeng31299942010-03-15 17:29:09 +08001077static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001078{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001079 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001080}
1081
Gui Jianfeng31299942010-03-15 17:29:09 +08001082static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001083{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001084 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001085}
1086
Gui Jianfeng31299942010-03-15 17:29:09 +08001087static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088{
1089 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1090 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1091}
1092
Gui Jianfeng31299942010-03-15 17:29:09 +08001093static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001094{
1095 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1096 INTR_INFO_VALID_MASK)) ==
1097 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1098}
1099
Gui Jianfeng31299942010-03-15 17:29:09 +08001100static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001101{
Sheng Yang04547152009-04-01 15:52:31 +08001102 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001103}
1104
Gui Jianfeng31299942010-03-15 17:29:09 +08001105static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001106{
Sheng Yang04547152009-04-01 15:52:31 +08001107 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001108}
1109
Paolo Bonzini35754c92015-07-29 12:05:37 +02001110static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001111{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001112 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001113}
1114
Gui Jianfeng31299942010-03-15 17:29:09 +08001115static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001116{
Sheng Yang04547152009-04-01 15:52:31 +08001117 return vmcs_config.cpu_based_exec_ctrl &
1118 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001119}
1120
Avi Kivity774ead32007-12-26 13:57:04 +02001121static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001122{
Sheng Yang04547152009-04-01 15:52:31 +08001123 return vmcs_config.cpu_based_2nd_exec_ctrl &
1124 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1125}
1126
Yang Zhang8d146952013-01-25 10:18:50 +08001127static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1128{
1129 return vmcs_config.cpu_based_2nd_exec_ctrl &
1130 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1131}
1132
Yang Zhang83d4c282013-01-25 10:18:49 +08001133static inline bool cpu_has_vmx_apic_register_virt(void)
1134{
1135 return vmcs_config.cpu_based_2nd_exec_ctrl &
1136 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1137}
1138
Yang Zhangc7c9c562013-01-25 10:18:51 +08001139static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1140{
1141 return vmcs_config.cpu_based_2nd_exec_ctrl &
1142 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1143}
1144
Yunhong Jiang64672c92016-06-13 14:19:59 -07001145/*
1146 * Comment's format: document - errata name - stepping - processor name.
1147 * Refer from
1148 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1149 */
1150static u32 vmx_preemption_cpu_tfms[] = {
1151/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
11520x000206E6,
1153/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1154/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1155/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
11560x00020652,
1157/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
11580x00020655,
1159/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1160/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1161/*
1162 * 320767.pdf - AAP86 - B1 -
1163 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1164 */
11650x000106E5,
1166/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
11670x000106A0,
1168/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
11690x000106A1,
1170/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
11710x000106A4,
1172 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1173 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1174 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
11750x000106A5,
1176};
1177
1178static inline bool cpu_has_broken_vmx_preemption_timer(void)
1179{
1180 u32 eax = cpuid_eax(0x00000001), i;
1181
1182 /* Clear the reserved bits */
1183 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001184 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001185 if (eax == vmx_preemption_cpu_tfms[i])
1186 return true;
1187
1188 return false;
1189}
1190
1191static inline bool cpu_has_vmx_preemption_timer(void)
1192{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001193 return vmcs_config.pin_based_exec_ctrl &
1194 PIN_BASED_VMX_PREEMPTION_TIMER;
1195}
1196
Yang Zhang01e439b2013-04-11 19:25:12 +08001197static inline bool cpu_has_vmx_posted_intr(void)
1198{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001199 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1200 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001201}
1202
1203static inline bool cpu_has_vmx_apicv(void)
1204{
1205 return cpu_has_vmx_apic_register_virt() &&
1206 cpu_has_vmx_virtual_intr_delivery() &&
1207 cpu_has_vmx_posted_intr();
1208}
1209
Sheng Yang04547152009-04-01 15:52:31 +08001210static inline bool cpu_has_vmx_flexpriority(void)
1211{
1212 return cpu_has_vmx_tpr_shadow() &&
1213 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001214}
1215
Marcelo Tosattie7997942009-06-11 12:07:40 -03001216static inline bool cpu_has_vmx_ept_execute_only(void)
1217{
Gui Jianfeng31299942010-03-15 17:29:09 +08001218 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001219}
1220
Marcelo Tosattie7997942009-06-11 12:07:40 -03001221static inline bool cpu_has_vmx_ept_2m_page(void)
1222{
Gui Jianfeng31299942010-03-15 17:29:09 +08001223 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001224}
1225
Sheng Yang878403b2010-01-05 19:02:29 +08001226static inline bool cpu_has_vmx_ept_1g_page(void)
1227{
Gui Jianfeng31299942010-03-15 17:29:09 +08001228 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001229}
1230
Sheng Yang4bc9b982010-06-02 14:05:24 +08001231static inline bool cpu_has_vmx_ept_4levels(void)
1232{
1233 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1234}
1235
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001236static inline bool cpu_has_vmx_ept_mt_wb(void)
1237{
1238 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1239}
1240
Yu Zhang855feb62017-08-24 20:27:55 +08001241static inline bool cpu_has_vmx_ept_5levels(void)
1242{
1243 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1244}
1245
Xudong Hao83c3a332012-05-28 19:33:35 +08001246static inline bool cpu_has_vmx_ept_ad_bits(void)
1247{
1248 return vmx_capability.ept & VMX_EPT_AD_BIT;
1249}
1250
Gui Jianfeng31299942010-03-15 17:29:09 +08001251static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001252{
Gui Jianfeng31299942010-03-15 17:29:09 +08001253 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001254}
1255
Gui Jianfeng31299942010-03-15 17:29:09 +08001256static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001257{
Gui Jianfeng31299942010-03-15 17:29:09 +08001258 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001259}
1260
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001261static inline bool cpu_has_vmx_invvpid_single(void)
1262{
1263 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1264}
1265
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001266static inline bool cpu_has_vmx_invvpid_global(void)
1267{
1268 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1269}
1270
Wanpeng Li08d839c2017-03-23 05:30:08 -07001271static inline bool cpu_has_vmx_invvpid(void)
1272{
1273 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1274}
1275
Gui Jianfeng31299942010-03-15 17:29:09 +08001276static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001277{
Sheng Yang04547152009-04-01 15:52:31 +08001278 return vmcs_config.cpu_based_2nd_exec_ctrl &
1279 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001280}
1281
Gui Jianfeng31299942010-03-15 17:29:09 +08001282static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001283{
1284 return vmcs_config.cpu_based_2nd_exec_ctrl &
1285 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1286}
1287
Gui Jianfeng31299942010-03-15 17:29:09 +08001288static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001289{
1290 return vmcs_config.cpu_based_2nd_exec_ctrl &
1291 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1292}
1293
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001294static inline bool cpu_has_vmx_basic_inout(void)
1295{
1296 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1297}
1298
Paolo Bonzini35754c92015-07-29 12:05:37 +02001299static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001300{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001301 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001302}
1303
Gui Jianfeng31299942010-03-15 17:29:09 +08001304static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001305{
Sheng Yang04547152009-04-01 15:52:31 +08001306 return vmcs_config.cpu_based_2nd_exec_ctrl &
1307 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001308}
1309
Gui Jianfeng31299942010-03-15 17:29:09 +08001310static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001311{
1312 return vmcs_config.cpu_based_2nd_exec_ctrl &
1313 SECONDARY_EXEC_RDTSCP;
1314}
1315
Mao, Junjiead756a12012-07-02 01:18:48 +00001316static inline bool cpu_has_vmx_invpcid(void)
1317{
1318 return vmcs_config.cpu_based_2nd_exec_ctrl &
1319 SECONDARY_EXEC_ENABLE_INVPCID;
1320}
1321
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001322static inline bool cpu_has_virtual_nmis(void)
1323{
1324 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1325}
1326
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001327static inline bool cpu_has_vmx_wbinvd_exit(void)
1328{
1329 return vmcs_config.cpu_based_2nd_exec_ctrl &
1330 SECONDARY_EXEC_WBINVD_EXITING;
1331}
1332
Abel Gordonabc4fc52013-04-18 14:35:25 +03001333static inline bool cpu_has_vmx_shadow_vmcs(void)
1334{
1335 u64 vmx_msr;
1336 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1337 /* check if the cpu supports writing r/o exit information fields */
1338 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1339 return false;
1340
1341 return vmcs_config.cpu_based_2nd_exec_ctrl &
1342 SECONDARY_EXEC_SHADOW_VMCS;
1343}
1344
Kai Huang843e4332015-01-28 10:54:28 +08001345static inline bool cpu_has_vmx_pml(void)
1346{
1347 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1348}
1349
Haozhong Zhang64903d62015-10-20 15:39:09 +08001350static inline bool cpu_has_vmx_tsc_scaling(void)
1351{
1352 return vmcs_config.cpu_based_2nd_exec_ctrl &
1353 SECONDARY_EXEC_TSC_SCALING;
1354}
1355
Bandan Das2a499e42017-08-03 15:54:41 -04001356static inline bool cpu_has_vmx_vmfunc(void)
1357{
1358 return vmcs_config.cpu_based_2nd_exec_ctrl &
1359 SECONDARY_EXEC_ENABLE_VMFUNC;
1360}
1361
Sheng Yang04547152009-04-01 15:52:31 +08001362static inline bool report_flexpriority(void)
1363{
1364 return flexpriority_enabled;
1365}
1366
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001367static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1368{
1369 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.nested_vmx_misc_low);
1370}
1371
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001372static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1373{
1374 return vmcs12->cpu_based_vm_exec_control & bit;
1375}
1376
1377static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1378{
1379 return (vmcs12->cpu_based_vm_exec_control &
1380 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1381 (vmcs12->secondary_vm_exec_control & bit);
1382}
1383
Jan Kiszkaf41245002014-03-07 20:03:13 +01001384static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1385{
1386 return vmcs12->pin_based_vm_exec_control &
1387 PIN_BASED_VMX_PREEMPTION_TIMER;
1388}
1389
Nadav Har'El155a97a2013-08-05 11:07:16 +03001390static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1391{
1392 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1393}
1394
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001395static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1396{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001397 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001398}
1399
Bandan Dasc5f983f2017-05-05 15:25:14 -04001400static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1401{
1402 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1403}
1404
Wincy Vanf2b93282015-02-03 23:56:03 +08001405static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1406{
1407 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1408}
1409
Wanpeng Li5c614b32015-10-13 09:18:36 -07001410static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1411{
1412 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1413}
1414
Wincy Van82f0dd42015-02-03 23:57:18 +08001415static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1416{
1417 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1418}
1419
Wincy Van608406e2015-02-03 23:57:51 +08001420static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1421{
1422 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1423}
1424
Wincy Van705699a2015-02-03 23:58:17 +08001425static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1426{
1427 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1428}
1429
Bandan Das27c42a12017-08-03 15:54:42 -04001430static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1431{
1432 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1433}
1434
Bandan Das41ab9372017-08-03 15:54:43 -04001435static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1436{
1437 return nested_cpu_has_vmfunc(vmcs12) &&
1438 (vmcs12->vm_function_control &
1439 VMX_VMFUNC_EPTP_SWITCHING);
1440}
1441
Jim Mattsonef85b672016-12-12 11:01:37 -08001442static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001443{
1444 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001445 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001446}
1447
Jan Kiszka533558b2014-01-04 18:47:20 +01001448static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1449 u32 exit_intr_info,
1450 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001451static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1452 struct vmcs12 *vmcs12,
1453 u32 reason, unsigned long qualification);
1454
Rusty Russell8b9cf982007-07-30 16:31:43 +10001455static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001456{
1457 int i;
1458
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001459 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001460 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001461 return i;
1462 return -1;
1463}
1464
Sheng Yang2384d2b2008-01-17 15:14:33 +08001465static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1466{
1467 struct {
1468 u64 vpid : 16;
1469 u64 rsvd : 48;
1470 u64 gva;
1471 } operand = { vpid, 0, gva };
1472
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001473 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001474 /* CF==1 or ZF==1 --> rc = -1 */
1475 "; ja 1f ; ud2 ; 1:"
1476 : : "a"(&operand), "c"(ext) : "cc", "memory");
1477}
1478
Sheng Yang14394422008-04-28 12:24:45 +08001479static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1480{
1481 struct {
1482 u64 eptp, gpa;
1483 } operand = {eptp, gpa};
1484
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001485 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001486 /* CF==1 or ZF==1 --> rc = -1 */
1487 "; ja 1f ; ud2 ; 1:\n"
1488 : : "a" (&operand), "c" (ext) : "cc", "memory");
1489}
1490
Avi Kivity26bb0982009-09-07 11:14:12 +03001491static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001492{
1493 int i;
1494
Rusty Russell8b9cf982007-07-30 16:31:43 +10001495 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001496 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001497 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001498 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001499}
1500
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501static void vmcs_clear(struct vmcs *vmcs)
1502{
1503 u64 phys_addr = __pa(vmcs);
1504 u8 error;
1505
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001506 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001507 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508 : "cc", "memory");
1509 if (error)
1510 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1511 vmcs, phys_addr);
1512}
1513
Nadav Har'Eld462b812011-05-24 15:26:10 +03001514static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1515{
1516 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001517 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1518 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001519 loaded_vmcs->cpu = -1;
1520 loaded_vmcs->launched = 0;
1521}
1522
Dongxiao Xu7725b892010-05-11 18:29:38 +08001523static void vmcs_load(struct vmcs *vmcs)
1524{
1525 u64 phys_addr = __pa(vmcs);
1526 u8 error;
1527
1528 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001529 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001530 : "cc", "memory");
1531 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001532 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001533 vmcs, phys_addr);
1534}
1535
Dave Young2965faa2015-09-09 15:38:55 -07001536#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001537/*
1538 * This bitmap is used to indicate whether the vmclear
1539 * operation is enabled on all cpus. All disabled by
1540 * default.
1541 */
1542static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1543
1544static inline void crash_enable_local_vmclear(int cpu)
1545{
1546 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1547}
1548
1549static inline void crash_disable_local_vmclear(int cpu)
1550{
1551 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1552}
1553
1554static inline int crash_local_vmclear_enabled(int cpu)
1555{
1556 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1557}
1558
1559static void crash_vmclear_local_loaded_vmcss(void)
1560{
1561 int cpu = raw_smp_processor_id();
1562 struct loaded_vmcs *v;
1563
1564 if (!crash_local_vmclear_enabled(cpu))
1565 return;
1566
1567 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1568 loaded_vmcss_on_cpu_link)
1569 vmcs_clear(v->vmcs);
1570}
1571#else
1572static inline void crash_enable_local_vmclear(int cpu) { }
1573static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001574#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001575
Nadav Har'Eld462b812011-05-24 15:26:10 +03001576static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001577{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001578 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001579 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001580
Nadav Har'Eld462b812011-05-24 15:26:10 +03001581 if (loaded_vmcs->cpu != cpu)
1582 return; /* vcpu migration can race with cpu offline */
1583 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001584 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001585 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001586 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001587
1588 /*
1589 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1590 * is before setting loaded_vmcs->vcpu to -1 which is done in
1591 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1592 * then adds the vmcs into percpu list before it is deleted.
1593 */
1594 smp_wmb();
1595
Nadav Har'Eld462b812011-05-24 15:26:10 +03001596 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001597 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001598}
1599
Nadav Har'Eld462b812011-05-24 15:26:10 +03001600static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001601{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001602 int cpu = loaded_vmcs->cpu;
1603
1604 if (cpu != -1)
1605 smp_call_function_single(cpu,
1606 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001607}
1608
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001609static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001610{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001611 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001612 return;
1613
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001614 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001615 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001616}
1617
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001618static inline void vpid_sync_vcpu_global(void)
1619{
1620 if (cpu_has_vmx_invvpid_global())
1621 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1622}
1623
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001624static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001625{
1626 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001627 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001628 else
1629 vpid_sync_vcpu_global();
1630}
1631
Sheng Yang14394422008-04-28 12:24:45 +08001632static inline void ept_sync_global(void)
1633{
David Hildenbrandf5f51582017-08-24 20:51:30 +02001634 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08001635}
1636
1637static inline void ept_sync_context(u64 eptp)
1638{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02001639 if (cpu_has_vmx_invept_context())
1640 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1641 else
1642 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08001643}
1644
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001645static __always_inline void vmcs_check16(unsigned long field)
1646{
1647 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1648 "16-bit accessor invalid for 64-bit field");
1649 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1650 "16-bit accessor invalid for 64-bit high field");
1651 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1652 "16-bit accessor invalid for 32-bit high field");
1653 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1654 "16-bit accessor invalid for natural width field");
1655}
1656
1657static __always_inline void vmcs_check32(unsigned long field)
1658{
1659 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1660 "32-bit accessor invalid for 16-bit field");
1661 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1662 "32-bit accessor invalid for natural width field");
1663}
1664
1665static __always_inline void vmcs_check64(unsigned long field)
1666{
1667 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1668 "64-bit accessor invalid for 16-bit field");
1669 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1670 "64-bit accessor invalid for 64-bit high field");
1671 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1672 "64-bit accessor invalid for 32-bit field");
1673 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1674 "64-bit accessor invalid for natural width field");
1675}
1676
1677static __always_inline void vmcs_checkl(unsigned long field)
1678{
1679 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1680 "Natural width accessor invalid for 16-bit field");
1681 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1682 "Natural width accessor invalid for 64-bit field");
1683 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1684 "Natural width accessor invalid for 64-bit high field");
1685 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1686 "Natural width accessor invalid for 32-bit field");
1687}
1688
1689static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690{
Avi Kivity5e520e62011-05-15 10:13:12 -04001691 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692
Avi Kivity5e520e62011-05-15 10:13:12 -04001693 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1694 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001695 return value;
1696}
1697
Avi Kivity96304212011-05-15 10:13:13 -04001698static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001699{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001700 vmcs_check16(field);
1701 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001702}
1703
Avi Kivity96304212011-05-15 10:13:13 -04001704static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001706 vmcs_check32(field);
1707 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001708}
1709
Avi Kivity96304212011-05-15 10:13:13 -04001710static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001712 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001713#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001714 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001715#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001716 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001717#endif
1718}
1719
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001720static __always_inline unsigned long vmcs_readl(unsigned long field)
1721{
1722 vmcs_checkl(field);
1723 return __vmcs_readl(field);
1724}
1725
Avi Kivitye52de1b2007-01-05 16:36:56 -08001726static noinline void vmwrite_error(unsigned long field, unsigned long value)
1727{
1728 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1729 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1730 dump_stack();
1731}
1732
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001733static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001734{
1735 u8 error;
1736
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001737 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001738 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001739 if (unlikely(error))
1740 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001741}
1742
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001743static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001744{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001745 vmcs_check16(field);
1746 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001747}
1748
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001749static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001751 vmcs_check32(field);
1752 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001753}
1754
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001755static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001756{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001757 vmcs_check64(field);
1758 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001759#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001760 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001761 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001762#endif
1763}
1764
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001765static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001766{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001767 vmcs_checkl(field);
1768 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001769}
1770
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001771static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001772{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001773 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1774 "vmcs_clear_bits does not support 64-bit fields");
1775 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1776}
1777
1778static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1779{
1780 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1781 "vmcs_set_bits does not support 64-bit fields");
1782 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001783}
1784
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001785static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1786{
1787 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1788}
1789
Gleb Natapov2961e8762013-11-25 15:37:13 +02001790static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1791{
1792 vmcs_write32(VM_ENTRY_CONTROLS, val);
1793 vmx->vm_entry_controls_shadow = val;
1794}
1795
1796static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1797{
1798 if (vmx->vm_entry_controls_shadow != val)
1799 vm_entry_controls_init(vmx, val);
1800}
1801
1802static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1803{
1804 return vmx->vm_entry_controls_shadow;
1805}
1806
1807
1808static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1809{
1810 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1811}
1812
1813static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1814{
1815 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1816}
1817
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001818static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1819{
1820 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1821}
1822
Gleb Natapov2961e8762013-11-25 15:37:13 +02001823static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1824{
1825 vmcs_write32(VM_EXIT_CONTROLS, val);
1826 vmx->vm_exit_controls_shadow = val;
1827}
1828
1829static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1830{
1831 if (vmx->vm_exit_controls_shadow != val)
1832 vm_exit_controls_init(vmx, val);
1833}
1834
1835static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1836{
1837 return vmx->vm_exit_controls_shadow;
1838}
1839
1840
1841static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1842{
1843 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1844}
1845
1846static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1847{
1848 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1849}
1850
Avi Kivity2fb92db2011-04-27 19:42:18 +03001851static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1852{
1853 vmx->segment_cache.bitmask = 0;
1854}
1855
1856static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1857 unsigned field)
1858{
1859 bool ret;
1860 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1861
1862 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1863 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1864 vmx->segment_cache.bitmask = 0;
1865 }
1866 ret = vmx->segment_cache.bitmask & mask;
1867 vmx->segment_cache.bitmask |= mask;
1868 return ret;
1869}
1870
1871static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1872{
1873 u16 *p = &vmx->segment_cache.seg[seg].selector;
1874
1875 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1876 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1877 return *p;
1878}
1879
1880static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1881{
1882 ulong *p = &vmx->segment_cache.seg[seg].base;
1883
1884 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1885 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1886 return *p;
1887}
1888
1889static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1890{
1891 u32 *p = &vmx->segment_cache.seg[seg].limit;
1892
1893 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1894 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1895 return *p;
1896}
1897
1898static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1899{
1900 u32 *p = &vmx->segment_cache.seg[seg].ar;
1901
1902 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1903 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1904 return *p;
1905}
1906
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001907static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1908{
1909 u32 eb;
1910
Liran Alonac9b3052017-11-06 16:15:10 +02001911 eb = (1u << PF_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08001912 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001913 if ((vcpu->guest_debug &
1914 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1915 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1916 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001917 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001918 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001919 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001920 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001921
1922 /* When we are running a nested L2 guest and L1 specified for it a
1923 * certain exception bitmap, we must trap the same exceptions and pass
1924 * them to L1. When running L2, we will only handle the exceptions
1925 * specified above if L1 did not want them.
1926 */
1927 if (is_guest_mode(vcpu))
1928 eb |= get_vmcs12(vcpu)->exception_bitmap;
Liran Alonac9b3052017-11-06 16:15:10 +02001929 else
1930 eb |= 1u << UD_VECTOR;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001931
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001932 vmcs_write32(EXCEPTION_BITMAP, eb);
1933}
1934
Gleb Natapov2961e8762013-11-25 15:37:13 +02001935static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1936 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001937{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001938 vm_entry_controls_clearbit(vmx, entry);
1939 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001940}
1941
Avi Kivity61d2ef22010-04-28 16:40:38 +03001942static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1943{
1944 unsigned i;
1945 struct msr_autoload *m = &vmx->msr_autoload;
1946
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001947 switch (msr) {
1948 case MSR_EFER:
1949 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001950 clear_atomic_switch_msr_special(vmx,
1951 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001952 VM_EXIT_LOAD_IA32_EFER);
1953 return;
1954 }
1955 break;
1956 case MSR_CORE_PERF_GLOBAL_CTRL:
1957 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001958 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001959 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1960 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1961 return;
1962 }
1963 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001964 }
1965
Avi Kivity61d2ef22010-04-28 16:40:38 +03001966 for (i = 0; i < m->nr; ++i)
1967 if (m->guest[i].index == msr)
1968 break;
1969
1970 if (i == m->nr)
1971 return;
1972 --m->nr;
1973 m->guest[i] = m->guest[m->nr];
1974 m->host[i] = m->host[m->nr];
1975 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1976 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1977}
1978
Gleb Natapov2961e8762013-11-25 15:37:13 +02001979static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1980 unsigned long entry, unsigned long exit,
1981 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1982 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001983{
1984 vmcs_write64(guest_val_vmcs, guest_val);
1985 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001986 vm_entry_controls_setbit(vmx, entry);
1987 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001988}
1989
Avi Kivity61d2ef22010-04-28 16:40:38 +03001990static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1991 u64 guest_val, u64 host_val)
1992{
1993 unsigned i;
1994 struct msr_autoload *m = &vmx->msr_autoload;
1995
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001996 switch (msr) {
1997 case MSR_EFER:
1998 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001999 add_atomic_switch_msr_special(vmx,
2000 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002001 VM_EXIT_LOAD_IA32_EFER,
2002 GUEST_IA32_EFER,
2003 HOST_IA32_EFER,
2004 guest_val, host_val);
2005 return;
2006 }
2007 break;
2008 case MSR_CORE_PERF_GLOBAL_CTRL:
2009 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002010 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002011 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2012 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2013 GUEST_IA32_PERF_GLOBAL_CTRL,
2014 HOST_IA32_PERF_GLOBAL_CTRL,
2015 guest_val, host_val);
2016 return;
2017 }
2018 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002019 case MSR_IA32_PEBS_ENABLE:
2020 /* PEBS needs a quiescent period after being disabled (to write
2021 * a record). Disabling PEBS through VMX MSR swapping doesn't
2022 * provide that period, so a CPU could write host's record into
2023 * guest's memory.
2024 */
2025 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002026 }
2027
Avi Kivity61d2ef22010-04-28 16:40:38 +03002028 for (i = 0; i < m->nr; ++i)
2029 if (m->guest[i].index == msr)
2030 break;
2031
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002032 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002033 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002034 "Can't add msr %x\n", msr);
2035 return;
2036 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002037 ++m->nr;
2038 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2039 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2040 }
2041
2042 m->guest[i].index = msr;
2043 m->guest[i].value = guest_val;
2044 m->host[i].index = msr;
2045 m->host[i].value = host_val;
2046}
2047
Avi Kivity92c0d902009-10-29 11:00:16 +02002048static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002049{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002050 u64 guest_efer = vmx->vcpu.arch.efer;
2051 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002052
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002053 if (!enable_ept) {
2054 /*
2055 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2056 * host CPUID is more efficient than testing guest CPUID
2057 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2058 */
2059 if (boot_cpu_has(X86_FEATURE_SMEP))
2060 guest_efer |= EFER_NX;
2061 else if (!(guest_efer & EFER_NX))
2062 ignore_bits |= EFER_NX;
2063 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002064
Avi Kivity51c6cf62007-08-29 03:48:05 +03002065 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002066 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002067 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002068 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002069#ifdef CONFIG_X86_64
2070 ignore_bits |= EFER_LMA | EFER_LME;
2071 /* SCE is meaningful only in long mode on Intel */
2072 if (guest_efer & EFER_LMA)
2073 ignore_bits &= ~(u64)EFER_SCE;
2074#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002075
2076 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002077
2078 /*
2079 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2080 * On CPUs that support "load IA32_EFER", always switch EFER
2081 * atomically, since it's faster than switching it manually.
2082 */
2083 if (cpu_has_load_ia32_efer ||
2084 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002085 if (!(guest_efer & EFER_LMA))
2086 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002087 if (guest_efer != host_efer)
2088 add_atomic_switch_msr(vmx, MSR_EFER,
2089 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002090 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002091 } else {
2092 guest_efer &= ~ignore_bits;
2093 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002094
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002095 vmx->guest_msrs[efer_offset].data = guest_efer;
2096 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2097
2098 return true;
2099 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002100}
2101
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002102#ifdef CONFIG_X86_32
2103/*
2104 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2105 * VMCS rather than the segment table. KVM uses this helper to figure
2106 * out the current bases to poke them into the VMCS before entry.
2107 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002108static unsigned long segment_base(u16 selector)
2109{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002110 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002111 unsigned long v;
2112
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002113 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002114 return 0;
2115
Thomas Garnier45fc8752017-03-14 10:05:08 -07002116 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002117
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002118 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002119 u16 ldt_selector = kvm_read_ldt();
2120
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002121 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002122 return 0;
2123
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002124 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002125 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002126 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002127 return v;
2128}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002129#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002130
Avi Kivity04d2cc72007-09-10 18:10:54 +03002131static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002132{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002133 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002134 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002135
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002136 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002137 return;
2138
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002139 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002140 /*
2141 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2142 * allow segment selectors with cpl > 0 or ti == 1.
2143 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002144 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002145 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002146 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002147 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002148 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002149 vmx->host_state.fs_reload_needed = 0;
2150 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002151 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002152 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002153 }
Avi Kivity9581d442010-10-19 16:46:55 +02002154 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002155 if (!(vmx->host_state.gs_sel & 7))
2156 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002157 else {
2158 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002159 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002160 }
2161
2162#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002163 savesegment(ds, vmx->host_state.ds_sel);
2164 savesegment(es, vmx->host_state.es_sel);
2165#endif
2166
2167#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002168 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2169 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2170#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002171 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2172 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002173#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002174
2175#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002176 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2177 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002178 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002179#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002180 if (boot_cpu_has(X86_FEATURE_MPX))
2181 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002182 for (i = 0; i < vmx->save_nmsrs; ++i)
2183 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002184 vmx->guest_msrs[i].data,
2185 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002186}
2187
Avi Kivitya9b21b62008-06-24 11:48:49 +03002188static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002189{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002190 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002191 return;
2192
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002193 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002194 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002195#ifdef CONFIG_X86_64
2196 if (is_long_mode(&vmx->vcpu))
2197 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2198#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002199 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002200 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002201#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002202 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002203#else
2204 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002205#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002206 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002207 if (vmx->host_state.fs_reload_needed)
2208 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002209#ifdef CONFIG_X86_64
2210 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2211 loadsegment(ds, vmx->host_state.ds_sel);
2212 loadsegment(es, vmx->host_state.es_sel);
2213 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002214#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002215 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002216#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002217 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002218#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002219 if (vmx->host_state.msr_host_bndcfgs)
2220 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002221 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002222}
2223
Avi Kivitya9b21b62008-06-24 11:48:49 +03002224static void vmx_load_host_state(struct vcpu_vmx *vmx)
2225{
2226 preempt_disable();
2227 __vmx_load_host_state(vmx);
2228 preempt_enable();
2229}
2230
Feng Wu28b835d2015-09-18 22:29:54 +08002231static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2232{
2233 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2234 struct pi_desc old, new;
2235 unsigned int dest;
2236
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002237 /*
2238 * In case of hot-plug or hot-unplug, we may have to undo
2239 * vmx_vcpu_pi_put even if there is no assigned device. And we
2240 * always keep PI.NDST up to date for simplicity: it makes the
2241 * code easier, and CPU migration is not a fast path.
2242 */
2243 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002244 return;
2245
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002246 /*
2247 * First handle the simple case where no cmpxchg is necessary; just
2248 * allow posting non-urgent interrupts.
2249 *
2250 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2251 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2252 * expects the VCPU to be on the blocked_vcpu_list that matches
2253 * PI.NDST.
2254 */
2255 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2256 vcpu->cpu == cpu) {
2257 pi_clear_sn(pi_desc);
2258 return;
2259 }
2260
2261 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002262 do {
2263 old.control = new.control = pi_desc->control;
2264
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002265 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002266
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002267 if (x2apic_enabled())
2268 new.ndst = dest;
2269 else
2270 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002271
Feng Wu28b835d2015-09-18 22:29:54 +08002272 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002273 } while (cmpxchg64(&pi_desc->control, old.control,
2274 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002275}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002276
Peter Feinerc95ba922016-08-17 09:36:47 -07002277static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2278{
2279 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2280 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2281}
2282
Avi Kivity6aa8b732006-12-10 02:21:36 -08002283/*
2284 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2285 * vcpu mutex is already taken.
2286 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002287static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002288{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002289 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002290 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002291
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002292 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002293 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002294 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002295 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002296
2297 /*
2298 * Read loaded_vmcs->cpu should be before fetching
2299 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2300 * See the comments in __loaded_vmcs_clear().
2301 */
2302 smp_rmb();
2303
Nadav Har'Eld462b812011-05-24 15:26:10 +03002304 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2305 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002306 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002307 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002308 }
2309
2310 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2311 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2312 vmcs_load(vmx->loaded_vmcs->vmcs);
2313 }
2314
2315 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002316 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002317 unsigned long sysenter_esp;
2318
2319 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002320
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321 /*
2322 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002323 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002325 vmcs_writel(HOST_TR_BASE,
2326 (unsigned long)this_cpu_ptr(&cpu_tss));
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002327 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002328
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002329 /*
2330 * VM exits change the host TR limit to 0x67 after a VM
2331 * exit. This is okay, since 0x67 covers everything except
2332 * the IO bitmap and have have code to handle the IO bitmap
2333 * being lost after a VM exit.
2334 */
2335 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2336
Avi Kivity6aa8b732006-12-10 02:21:36 -08002337 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2338 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002339
Nadav Har'Eld462b812011-05-24 15:26:10 +03002340 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341 }
Feng Wu28b835d2015-09-18 22:29:54 +08002342
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002343 /* Setup TSC multiplier */
2344 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002345 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2346 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002347
Feng Wu28b835d2015-09-18 22:29:54 +08002348 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002349 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002350 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002351}
2352
2353static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2354{
2355 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2356
2357 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002358 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2359 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002360 return;
2361
2362 /* Set SN when the vCPU is preempted */
2363 if (vcpu->preempted)
2364 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002365}
2366
2367static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2368{
Feng Wu28b835d2015-09-18 22:29:54 +08002369 vmx_vcpu_pi_put(vcpu);
2370
Avi Kivitya9b21b62008-06-24 11:48:49 +03002371 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372}
2373
Wanpeng Lif244dee2017-07-20 01:11:54 -07002374static bool emulation_required(struct kvm_vcpu *vcpu)
2375{
2376 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2377}
2378
Avi Kivityedcafe32009-12-30 18:07:40 +02002379static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2380
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002381/*
2382 * Return the cr0 value that a nested guest would read. This is a combination
2383 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2384 * its hypervisor (cr0_read_shadow).
2385 */
2386static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2387{
2388 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2389 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2390}
2391static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2392{
2393 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2394 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2395}
2396
Avi Kivity6aa8b732006-12-10 02:21:36 -08002397static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2398{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002399 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002400
Avi Kivity6de12732011-03-07 12:51:22 +02002401 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2402 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2403 rflags = vmcs_readl(GUEST_RFLAGS);
2404 if (to_vmx(vcpu)->rmode.vm86_active) {
2405 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2406 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2407 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2408 }
2409 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002410 }
Avi Kivity6de12732011-03-07 12:51:22 +02002411 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002412}
2413
2414static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2415{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002416 unsigned long old_rflags = vmx_get_rflags(vcpu);
2417
Avi Kivity6de12732011-03-07 12:51:22 +02002418 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2419 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002420 if (to_vmx(vcpu)->rmode.vm86_active) {
2421 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002422 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002423 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002424 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002425
2426 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2427 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428}
2429
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002430static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002431{
2432 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2433 int ret = 0;
2434
2435 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002436 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002437 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002438 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002439
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002440 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002441}
2442
2443static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2444{
2445 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2446 u32 interruptibility = interruptibility_old;
2447
2448 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2449
Jan Kiszka48005f62010-02-19 19:38:07 +01002450 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002451 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002452 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002453 interruptibility |= GUEST_INTR_STATE_STI;
2454
2455 if ((interruptibility != interruptibility_old))
2456 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2457}
2458
Avi Kivity6aa8b732006-12-10 02:21:36 -08002459static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2460{
2461 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002462
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002463 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002464 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002465 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466
Glauber Costa2809f5d2009-05-12 16:21:05 -04002467 /* skipping an emulated instruction also counts */
2468 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469}
2470
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002471static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2472 unsigned long exit_qual)
2473{
2474 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2475 unsigned int nr = vcpu->arch.exception.nr;
2476 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2477
2478 if (vcpu->arch.exception.has_error_code) {
2479 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2480 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2481 }
2482
2483 if (kvm_exception_is_soft(nr))
2484 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2485 else
2486 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2487
2488 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2489 vmx_get_nmi_mask(vcpu))
2490 intr_info |= INTR_INFO_UNBLOCK_NMI;
2491
2492 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2493}
2494
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002495/*
2496 * KVM wants to inject page-faults which it got to the guest. This function
2497 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002498 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002499static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002500{
2501 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002502 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002503
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002504 if (nr == PF_VECTOR) {
2505 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002506 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002507 return 1;
2508 }
2509 /*
2510 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2511 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2512 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2513 * can be written only when inject_pending_event runs. This should be
2514 * conditional on a new capability---if the capability is disabled,
2515 * kvm_multiple_exception would write the ancillary information to
2516 * CR2 or DR6, for backwards ABI-compatibility.
2517 */
2518 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2519 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002520 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002521 return 1;
2522 }
2523 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002524 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002525 if (nr == DB_VECTOR)
2526 *exit_qual = vcpu->arch.dr6;
2527 else
2528 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002529 return 1;
2530 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002531 }
2532
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002533 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002534}
2535
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002536static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02002537{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002538 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002539 unsigned nr = vcpu->arch.exception.nr;
2540 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002541 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002542 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002543
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002544 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002545 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002546 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2547 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002548
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002549 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002550 int inc_eip = 0;
2551 if (kvm_exception_is_soft(nr))
2552 inc_eip = vcpu->arch.event_exit_inst_len;
2553 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002554 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002555 return;
2556 }
2557
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002558 if (kvm_exception_is_soft(nr)) {
2559 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2560 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002561 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2562 } else
2563 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2564
2565 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002566}
2567
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002568static bool vmx_rdtscp_supported(void)
2569{
2570 return cpu_has_vmx_rdtscp();
2571}
2572
Mao, Junjiead756a12012-07-02 01:18:48 +00002573static bool vmx_invpcid_supported(void)
2574{
2575 return cpu_has_vmx_invpcid() && enable_ept;
2576}
2577
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578/*
Eddie Donga75beee2007-05-17 18:55:15 +03002579 * Swap MSR entry in host/guest MSR entry array.
2580 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002581static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002582{
Avi Kivity26bb0982009-09-07 11:14:12 +03002583 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002584
2585 tmp = vmx->guest_msrs[to];
2586 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2587 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002588}
2589
Yang Zhang8d146952013-01-25 10:18:50 +08002590static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2591{
2592 unsigned long *msr_bitmap;
2593
Wincy Van670125b2015-03-04 14:31:56 +08002594 if (is_guest_mode(vcpu))
Radim Krčmářd048c092016-08-08 20:16:22 +02002595 msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap;
Roman Kagan3ce424e2016-05-18 17:48:20 +03002596 else if (cpu_has_secondary_exec_ctrls() &&
2597 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
2598 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002599 if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) {
2600 if (is_long_mode(vcpu))
Wanpeng Lic63e4562016-09-23 19:17:16 +08002601 msr_bitmap = vmx_msr_bitmap_longmode_x2apic_apicv;
2602 else
2603 msr_bitmap = vmx_msr_bitmap_legacy_x2apic_apicv;
2604 } else {
2605 if (is_long_mode(vcpu))
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002606 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2607 else
2608 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002609 }
Yang Zhang8d146952013-01-25 10:18:50 +08002610 } else {
2611 if (is_long_mode(vcpu))
2612 msr_bitmap = vmx_msr_bitmap_longmode;
2613 else
2614 msr_bitmap = vmx_msr_bitmap_legacy;
2615 }
2616
2617 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2618}
2619
Eddie Donga75beee2007-05-17 18:55:15 +03002620/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002621 * Set up the vmcs to automatically save and restore system
2622 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2623 * mode, as fiddling with msrs is very expensive.
2624 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002625static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002626{
Avi Kivity26bb0982009-09-07 11:14:12 +03002627 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002628
Eddie Donga75beee2007-05-17 18:55:15 +03002629 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002630#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002631 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002632 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002633 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002634 move_msr_up(vmx, index, save_nmsrs++);
2635 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002636 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002637 move_msr_up(vmx, index, save_nmsrs++);
2638 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002639 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002640 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002641 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002642 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002643 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002644 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002645 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002646 * if efer.sce is enabled.
2647 */
Brian Gerst8c065852010-07-17 09:03:26 -04002648 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002649 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002650 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002651 }
Eddie Donga75beee2007-05-17 18:55:15 +03002652#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002653 index = __find_msr_index(vmx, MSR_EFER);
2654 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002655 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002656
Avi Kivity26bb0982009-09-07 11:14:12 +03002657 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002658
Yang Zhang8d146952013-01-25 10:18:50 +08002659 if (cpu_has_vmx_msr_bitmap())
2660 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002661}
2662
2663/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002664 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002665 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2666 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002667 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002668static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002669{
2670 u64 host_tsc, tsc_offset;
2671
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002672 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002673 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002674 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675}
2676
2677/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002678 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002679 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002680static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002681{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002682 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002683 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002684 * We're here if L1 chose not to trap WRMSR to TSC. According
2685 * to the spec, this should set L1's TSC; The offset that L1
2686 * set for L2 remains unchanged, and still needs to be added
2687 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002688 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002689 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002690 /* recalculate vmcs02.TSC_OFFSET: */
2691 vmcs12 = get_vmcs12(vcpu);
2692 vmcs_write64(TSC_OFFSET, offset +
2693 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2694 vmcs12->tsc_offset : 0));
2695 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002696 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2697 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002698 vmcs_write64(TSC_OFFSET, offset);
2699 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700}
2701
Nadav Har'El801d3422011-05-25 23:02:23 +03002702/*
2703 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2704 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2705 * all guests if the "nested" module option is off, and can also be disabled
2706 * for a single guest by disabling its VMX cpuid bit.
2707 */
2708static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2709{
Radim Krčmářd6321d42017-08-05 00:12:49 +02002710 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03002711}
2712
Avi Kivity6aa8b732006-12-10 02:21:36 -08002713/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002714 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2715 * returned for the various VMX controls MSRs when nested VMX is enabled.
2716 * The same values should also be used to verify that vmcs12 control fields are
2717 * valid during nested entry from L1 to L2.
2718 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2719 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2720 * bit in the high half is on if the corresponding bit in the control field
2721 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002722 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002723static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002724{
2725 /*
2726 * Note that as a general rule, the high half of the MSRs (bits in
2727 * the control fields which may be 1) should be initialized by the
2728 * intersection of the underlying hardware's MSR (i.e., features which
2729 * can be supported) and the list of features we want to expose -
2730 * because they are known to be properly supported in our code.
2731 * Also, usually, the low half of the MSRs (bits which must be 1) can
2732 * be set to 0, meaning that L1 may turn off any of these bits. The
2733 * reason is that if one of these bits is necessary, it will appear
2734 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2735 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02002736 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002737 * These rules have exceptions below.
2738 */
2739
2740 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002741 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002742 vmx->nested.nested_vmx_pinbased_ctls_low,
2743 vmx->nested.nested_vmx_pinbased_ctls_high);
2744 vmx->nested.nested_vmx_pinbased_ctls_low |=
2745 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2746 vmx->nested.nested_vmx_pinbased_ctls_high &=
2747 PIN_BASED_EXT_INTR_MASK |
2748 PIN_BASED_NMI_EXITING |
2749 PIN_BASED_VIRTUAL_NMIS;
2750 vmx->nested.nested_vmx_pinbased_ctls_high |=
2751 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002752 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002753 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002754 vmx->nested.nested_vmx_pinbased_ctls_high |=
2755 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002756
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002757 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002758 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002759 vmx->nested.nested_vmx_exit_ctls_low,
2760 vmx->nested.nested_vmx_exit_ctls_high);
2761 vmx->nested.nested_vmx_exit_ctls_low =
2762 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002763
Wincy Vanb9c237b2015-02-03 23:56:30 +08002764 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002765#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002766 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002767#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01002768 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002769 vmx->nested.nested_vmx_exit_ctls_high |=
2770 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002771 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002772 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2773
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002774 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002775 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002776
Jan Kiszka2996fca2014-06-16 13:59:43 +02002777 /* We support free control of debug control saving. */
David Matlack0115f9c2016-11-29 18:14:06 -08002778 vmx->nested.nested_vmx_exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002779
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002780 /* entry controls */
2781 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002782 vmx->nested.nested_vmx_entry_ctls_low,
2783 vmx->nested.nested_vmx_entry_ctls_high);
2784 vmx->nested.nested_vmx_entry_ctls_low =
2785 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2786 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002787#ifdef CONFIG_X86_64
2788 VM_ENTRY_IA32E_MODE |
2789#endif
2790 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002791 vmx->nested.nested_vmx_entry_ctls_high |=
2792 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002793 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002794 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002795
Jan Kiszka2996fca2014-06-16 13:59:43 +02002796 /* We support free control of debug control loading. */
David Matlack0115f9c2016-11-29 18:14:06 -08002797 vmx->nested.nested_vmx_entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002798
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002799 /* cpu-based controls */
2800 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002801 vmx->nested.nested_vmx_procbased_ctls_low,
2802 vmx->nested.nested_vmx_procbased_ctls_high);
2803 vmx->nested.nested_vmx_procbased_ctls_low =
2804 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2805 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002806 CPU_BASED_VIRTUAL_INTR_PENDING |
2807 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002808 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2809 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2810 CPU_BASED_CR3_STORE_EXITING |
2811#ifdef CONFIG_X86_64
2812 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2813#endif
2814 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002815 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2816 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2817 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2818 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002819 /*
2820 * We can allow some features even when not supported by the
2821 * hardware. For example, L1 can specify an MSR bitmap - and we
2822 * can use it to avoid exits to L1 - even when L0 runs L2
2823 * without MSR bitmaps.
2824 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002825 vmx->nested.nested_vmx_procbased_ctls_high |=
2826 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002827 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002828
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002829 /* We support free control of CR3 access interception. */
David Matlack0115f9c2016-11-29 18:14:06 -08002830 vmx->nested.nested_vmx_procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002831 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2832
Paolo Bonzini80154d72017-08-24 13:55:35 +02002833 /*
2834 * secondary cpu-based controls. Do not include those that
2835 * depend on CPUID bits, they are added later by vmx_cpuid_update.
2836 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002837 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002838 vmx->nested.nested_vmx_secondary_ctls_low,
2839 vmx->nested.nested_vmx_secondary_ctls_high);
2840 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2841 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002842 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02002843 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08002844 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08002845 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002846 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02002847 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002848
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002849 if (enable_ept) {
2850 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002851 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002852 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002853 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002854 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002855 if (cpu_has_vmx_ept_execute_only())
2856 vmx->nested.nested_vmx_ept_caps |=
2857 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002858 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04002859 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002860 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
2861 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04002862 if (enable_ept_ad_bits) {
2863 vmx->nested.nested_vmx_secondary_ctls_high |=
2864 SECONDARY_EXEC_ENABLE_PML;
Dan Carpenter7461fbc2017-05-18 10:41:15 +03002865 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04002866 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02002867 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002868
Bandan Das27c42a12017-08-03 15:54:42 -04002869 if (cpu_has_vmx_vmfunc()) {
2870 vmx->nested.nested_vmx_secondary_ctls_high |=
2871 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04002872 /*
2873 * Advertise EPTP switching unconditionally
2874 * since we emulate it
2875 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08002876 if (enable_ept)
2877 vmx->nested.nested_vmx_vmfunc_controls =
2878 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04002879 }
2880
Paolo Bonzinief697a72016-03-18 16:58:38 +01002881 /*
2882 * Old versions of KVM use the single-context version without
2883 * checking for support, so declare that it is supported even
2884 * though it is treated as global context. The alternative is
2885 * not failing the single-context invvpid, and it is worse.
2886 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07002887 if (enable_vpid) {
2888 vmx->nested.nested_vmx_secondary_ctls_high |=
2889 SECONDARY_EXEC_ENABLE_VPID;
Wanpeng Li089d7b62015-10-13 09:18:37 -07002890 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03002891 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02002892 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002893
Radim Krčmář0790ec12015-03-17 14:02:32 +01002894 if (enable_unrestricted_guest)
2895 vmx->nested.nested_vmx_secondary_ctls_high |=
2896 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2897
Jan Kiszkac18911a2013-03-13 16:06:41 +01002898 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002899 rdmsr(MSR_IA32_VMX_MISC,
2900 vmx->nested.nested_vmx_misc_low,
2901 vmx->nested.nested_vmx_misc_high);
2902 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2903 vmx->nested.nested_vmx_misc_low |=
2904 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002905 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002906 vmx->nested.nested_vmx_misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002907
2908 /*
2909 * This MSR reports some information about VMX support. We
2910 * should return information about the VMX we emulate for the
2911 * guest, and the VMCS structure we give it - not about the
2912 * VMX support of the underlying hardware.
2913 */
2914 vmx->nested.nested_vmx_basic =
2915 VMCS12_REVISION |
2916 VMX_BASIC_TRUE_CTLS |
2917 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2918 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2919
2920 if (cpu_has_vmx_basic_inout())
2921 vmx->nested.nested_vmx_basic |= VMX_BASIC_INOUT;
2922
2923 /*
David Matlack8322ebb2016-11-29 18:14:09 -08002924 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08002925 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2926 * We picked the standard core2 setting.
2927 */
2928#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2929#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2930 vmx->nested.nested_vmx_cr0_fixed0 = VMXON_CR0_ALWAYSON;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002931 vmx->nested.nested_vmx_cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08002932
2933 /* These MSRs specify bits which the guest must keep fixed off. */
2934 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, vmx->nested.nested_vmx_cr0_fixed1);
2935 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, vmx->nested.nested_vmx_cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08002936
2937 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Jim Mattson5b157062017-12-22 12:11:12 -08002938 vmx->nested.nested_vmx_vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002939}
2940
David Matlack38991522016-11-29 18:14:08 -08002941/*
2942 * if fixed0[i] == 1: val[i] must be 1
2943 * if fixed1[i] == 0: val[i] must be 0
2944 */
2945static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
2946{
2947 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002948}
2949
2950static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2951{
David Matlack38991522016-11-29 18:14:08 -08002952 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002953}
2954
2955static inline u64 vmx_control_msr(u32 low, u32 high)
2956{
2957 return low | ((u64)high << 32);
2958}
2959
David Matlack62cc6b9d2016-11-29 18:14:07 -08002960static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
2961{
2962 superset &= mask;
2963 subset &= mask;
2964
2965 return (superset | subset) == superset;
2966}
2967
2968static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
2969{
2970 const u64 feature_and_reserved =
2971 /* feature (except bit 48; see below) */
2972 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
2973 /* reserved */
2974 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
2975 u64 vmx_basic = vmx->nested.nested_vmx_basic;
2976
2977 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
2978 return -EINVAL;
2979
2980 /*
2981 * KVM does not emulate a version of VMX that constrains physical
2982 * addresses of VMX structures (e.g. VMCS) to 32-bits.
2983 */
2984 if (data & BIT_ULL(48))
2985 return -EINVAL;
2986
2987 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
2988 vmx_basic_vmcs_revision_id(data))
2989 return -EINVAL;
2990
2991 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
2992 return -EINVAL;
2993
2994 vmx->nested.nested_vmx_basic = data;
2995 return 0;
2996}
2997
2998static int
2999vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3000{
3001 u64 supported;
3002 u32 *lowp, *highp;
3003
3004 switch (msr_index) {
3005 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3006 lowp = &vmx->nested.nested_vmx_pinbased_ctls_low;
3007 highp = &vmx->nested.nested_vmx_pinbased_ctls_high;
3008 break;
3009 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3010 lowp = &vmx->nested.nested_vmx_procbased_ctls_low;
3011 highp = &vmx->nested.nested_vmx_procbased_ctls_high;
3012 break;
3013 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3014 lowp = &vmx->nested.nested_vmx_exit_ctls_low;
3015 highp = &vmx->nested.nested_vmx_exit_ctls_high;
3016 break;
3017 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3018 lowp = &vmx->nested.nested_vmx_entry_ctls_low;
3019 highp = &vmx->nested.nested_vmx_entry_ctls_high;
3020 break;
3021 case MSR_IA32_VMX_PROCBASED_CTLS2:
3022 lowp = &vmx->nested.nested_vmx_secondary_ctls_low;
3023 highp = &vmx->nested.nested_vmx_secondary_ctls_high;
3024 break;
3025 default:
3026 BUG();
3027 }
3028
3029 supported = vmx_control_msr(*lowp, *highp);
3030
3031 /* Check must-be-1 bits are still 1. */
3032 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3033 return -EINVAL;
3034
3035 /* Check must-be-0 bits are still 0. */
3036 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3037 return -EINVAL;
3038
3039 *lowp = data;
3040 *highp = data >> 32;
3041 return 0;
3042}
3043
3044static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3045{
3046 const u64 feature_and_reserved_bits =
3047 /* feature */
3048 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3049 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3050 /* reserved */
3051 GENMASK_ULL(13, 9) | BIT_ULL(31);
3052 u64 vmx_misc;
3053
3054 vmx_misc = vmx_control_msr(vmx->nested.nested_vmx_misc_low,
3055 vmx->nested.nested_vmx_misc_high);
3056
3057 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3058 return -EINVAL;
3059
3060 if ((vmx->nested.nested_vmx_pinbased_ctls_high &
3061 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3062 vmx_misc_preemption_timer_rate(data) !=
3063 vmx_misc_preemption_timer_rate(vmx_misc))
3064 return -EINVAL;
3065
3066 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3067 return -EINVAL;
3068
3069 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3070 return -EINVAL;
3071
3072 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3073 return -EINVAL;
3074
3075 vmx->nested.nested_vmx_misc_low = data;
3076 vmx->nested.nested_vmx_misc_high = data >> 32;
3077 return 0;
3078}
3079
3080static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3081{
3082 u64 vmx_ept_vpid_cap;
3083
3084 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.nested_vmx_ept_caps,
3085 vmx->nested.nested_vmx_vpid_caps);
3086
3087 /* Every bit is either reserved or a feature bit. */
3088 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3089 return -EINVAL;
3090
3091 vmx->nested.nested_vmx_ept_caps = data;
3092 vmx->nested.nested_vmx_vpid_caps = data >> 32;
3093 return 0;
3094}
3095
3096static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3097{
3098 u64 *msr;
3099
3100 switch (msr_index) {
3101 case MSR_IA32_VMX_CR0_FIXED0:
3102 msr = &vmx->nested.nested_vmx_cr0_fixed0;
3103 break;
3104 case MSR_IA32_VMX_CR4_FIXED0:
3105 msr = &vmx->nested.nested_vmx_cr4_fixed0;
3106 break;
3107 default:
3108 BUG();
3109 }
3110
3111 /*
3112 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3113 * must be 1 in the restored value.
3114 */
3115 if (!is_bitwise_subset(data, *msr, -1ULL))
3116 return -EINVAL;
3117
3118 *msr = data;
3119 return 0;
3120}
3121
3122/*
3123 * Called when userspace is restoring VMX MSRs.
3124 *
3125 * Returns 0 on success, non-0 otherwise.
3126 */
3127static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3128{
3129 struct vcpu_vmx *vmx = to_vmx(vcpu);
3130
3131 switch (msr_index) {
3132 case MSR_IA32_VMX_BASIC:
3133 return vmx_restore_vmx_basic(vmx, data);
3134 case MSR_IA32_VMX_PINBASED_CTLS:
3135 case MSR_IA32_VMX_PROCBASED_CTLS:
3136 case MSR_IA32_VMX_EXIT_CTLS:
3137 case MSR_IA32_VMX_ENTRY_CTLS:
3138 /*
3139 * The "non-true" VMX capability MSRs are generated from the
3140 * "true" MSRs, so we do not support restoring them directly.
3141 *
3142 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3143 * should restore the "true" MSRs with the must-be-1 bits
3144 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3145 * DEFAULT SETTINGS".
3146 */
3147 return -EINVAL;
3148 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3149 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3150 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3151 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3152 case MSR_IA32_VMX_PROCBASED_CTLS2:
3153 return vmx_restore_control_msr(vmx, msr_index, data);
3154 case MSR_IA32_VMX_MISC:
3155 return vmx_restore_vmx_misc(vmx, data);
3156 case MSR_IA32_VMX_CR0_FIXED0:
3157 case MSR_IA32_VMX_CR4_FIXED0:
3158 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3159 case MSR_IA32_VMX_CR0_FIXED1:
3160 case MSR_IA32_VMX_CR4_FIXED1:
3161 /*
3162 * These MSRs are generated based on the vCPU's CPUID, so we
3163 * do not support restoring them directly.
3164 */
3165 return -EINVAL;
3166 case MSR_IA32_VMX_EPT_VPID_CAP:
3167 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3168 case MSR_IA32_VMX_VMCS_ENUM:
3169 vmx->nested.nested_vmx_vmcs_enum = data;
3170 return 0;
3171 default:
3172 /*
3173 * The rest of the VMX capability MSRs do not support restore.
3174 */
3175 return -EINVAL;
3176 }
3177}
3178
Jan Kiszkacae50132014-01-04 18:47:22 +01003179/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003180static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
3181{
Wincy Vanb9c237b2015-02-03 23:56:30 +08003182 struct vcpu_vmx *vmx = to_vmx(vcpu);
3183
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003184 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003185 case MSR_IA32_VMX_BASIC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003186 *pdata = vmx->nested.nested_vmx_basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003187 break;
3188 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3189 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003190 *pdata = vmx_control_msr(
3191 vmx->nested.nested_vmx_pinbased_ctls_low,
3192 vmx->nested.nested_vmx_pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003193 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3194 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003195 break;
3196 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3197 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003198 *pdata = vmx_control_msr(
3199 vmx->nested.nested_vmx_procbased_ctls_low,
3200 vmx->nested.nested_vmx_procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003201 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3202 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003203 break;
3204 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3205 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003206 *pdata = vmx_control_msr(
3207 vmx->nested.nested_vmx_exit_ctls_low,
3208 vmx->nested.nested_vmx_exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003209 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3210 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003211 break;
3212 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3213 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003214 *pdata = vmx_control_msr(
3215 vmx->nested.nested_vmx_entry_ctls_low,
3216 vmx->nested.nested_vmx_entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003217 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3218 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003219 break;
3220 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003221 *pdata = vmx_control_msr(
3222 vmx->nested.nested_vmx_misc_low,
3223 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003224 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003225 case MSR_IA32_VMX_CR0_FIXED0:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003226 *pdata = vmx->nested.nested_vmx_cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003227 break;
3228 case MSR_IA32_VMX_CR0_FIXED1:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003229 *pdata = vmx->nested.nested_vmx_cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003230 break;
3231 case MSR_IA32_VMX_CR4_FIXED0:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003232 *pdata = vmx->nested.nested_vmx_cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003233 break;
3234 case MSR_IA32_VMX_CR4_FIXED1:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003235 *pdata = vmx->nested.nested_vmx_cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003236 break;
3237 case MSR_IA32_VMX_VMCS_ENUM:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003238 *pdata = vmx->nested.nested_vmx_vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003239 break;
3240 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003241 *pdata = vmx_control_msr(
3242 vmx->nested.nested_vmx_secondary_ctls_low,
3243 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003244 break;
3245 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07003246 *pdata = vmx->nested.nested_vmx_ept_caps |
3247 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003248 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003249 case MSR_IA32_VMX_VMFUNC:
3250 *pdata = vmx->nested.nested_vmx_vmfunc_controls;
3251 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003252 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003253 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003254 }
3255
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003256 return 0;
3257}
3258
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003259static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3260 uint64_t val)
3261{
3262 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3263
3264 return !(val & ~valid_bits);
3265}
3266
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003267/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003268 * Reads an msr value (of 'msr_index') into 'pdata'.
3269 * Returns 0 on success, non-0 otherwise.
3270 * Assumes vcpu_load() was already called.
3271 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003272static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003273{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003274 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003275 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003277 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003278#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003279 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003280 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003281 break;
3282 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003283 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003284 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003285 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003286 vmx_load_host_state(vmx);
3287 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003288 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003289#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003290 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003291 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303292 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003293 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003294 break;
3295 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003296 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003297 break;
3298 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003299 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003300 break;
3301 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003302 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003303 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003304 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003305 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003306 (!msr_info->host_initiated &&
3307 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003308 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003309 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003310 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003311 case MSR_IA32_MCG_EXT_CTL:
3312 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003313 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003314 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003315 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003316 msr_info->data = vcpu->arch.mcg_ext_ctl;
3317 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003318 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003319 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003320 break;
3321 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3322 if (!nested_vmx_allowed(vcpu))
3323 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003324 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003325 case MSR_IA32_XSS:
3326 if (!vmx_xsaves_supported())
3327 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003328 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003329 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003330 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003331 if (!msr_info->host_initiated &&
3332 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003333 return 1;
3334 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003335 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003336 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003337 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003338 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003339 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003340 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003341 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003342 }
3343
Avi Kivity6aa8b732006-12-10 02:21:36 -08003344 return 0;
3345}
3346
Jan Kiszkacae50132014-01-04 18:47:22 +01003347static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3348
Avi Kivity6aa8b732006-12-10 02:21:36 -08003349/*
3350 * Writes msr value into into the appropriate "register".
3351 * Returns 0 on success, non-0 otherwise.
3352 * Assumes vcpu_load() was already called.
3353 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003354static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003355{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003356 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003357 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003358 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003359 u32 msr_index = msr_info->index;
3360 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003361
Avi Kivity6aa8b732006-12-10 02:21:36 -08003362 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003363 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003364 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003365 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003366#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003367 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003368 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003369 vmcs_writel(GUEST_FS_BASE, data);
3370 break;
3371 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003372 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003373 vmcs_writel(GUEST_GS_BASE, data);
3374 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003375 case MSR_KERNEL_GS_BASE:
3376 vmx_load_host_state(vmx);
3377 vmx->msr_guest_kernel_gs_base = data;
3378 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003379#endif
3380 case MSR_IA32_SYSENTER_CS:
3381 vmcs_write32(GUEST_SYSENTER_CS, data);
3382 break;
3383 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003384 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003385 break;
3386 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003387 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003388 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003389 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003390 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003391 (!msr_info->host_initiated &&
3392 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003393 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003394 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003395 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003396 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003397 vmcs_write64(GUEST_BNDCFGS, data);
3398 break;
3399 case MSR_IA32_TSC:
3400 kvm_write_tsc(vcpu, msr_info);
3401 break;
3402 case MSR_IA32_CR_PAT:
Will Auld8fe8ab42012-11-29 12:42:12 -08003403 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003404 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3405 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003406 vmcs_write64(GUEST_IA32_PAT, data);
3407 vcpu->arch.pat = data;
3408 break;
3409 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003410 ret = kvm_set_msr_common(vcpu, msr_info);
3411 break;
Will Auldba904632012-11-29 12:42:50 -08003412 case MSR_IA32_TSC_ADJUST:
3413 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003414 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003415 case MSR_IA32_MCG_EXT_CTL:
3416 if ((!msr_info->host_initiated &&
3417 !(to_vmx(vcpu)->msr_ia32_feature_control &
3418 FEATURE_CONTROL_LMCE)) ||
3419 (data & ~MCG_EXT_CTL_LMCE_EN))
3420 return 1;
3421 vcpu->arch.mcg_ext_ctl = data;
3422 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003423 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003424 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003425 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003426 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3427 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003428 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003429 if (msr_info->host_initiated && data == 0)
3430 vmx_leave_nested(vcpu);
3431 break;
3432 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003433 if (!msr_info->host_initiated)
3434 return 1; /* they are read-only */
3435 if (!nested_vmx_allowed(vcpu))
3436 return 1;
3437 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003438 case MSR_IA32_XSS:
3439 if (!vmx_xsaves_supported())
3440 return 1;
3441 /*
3442 * The only supported bit as of Skylake is bit 8, but
3443 * it is not supported on KVM.
3444 */
3445 if (data != 0)
3446 return 1;
3447 vcpu->arch.ia32_xss = data;
3448 if (vcpu->arch.ia32_xss != host_xss)
3449 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3450 vcpu->arch.ia32_xss, host_xss);
3451 else
3452 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3453 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003454 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003455 if (!msr_info->host_initiated &&
3456 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003457 return 1;
3458 /* Check reserved bit, higher 32 bits should be zero */
3459 if ((data >> 32) != 0)
3460 return 1;
3461 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003462 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003463 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003464 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003465 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003466 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003467 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3468 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003469 ret = kvm_set_shared_msr(msr->index, msr->data,
3470 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003471 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003472 if (ret)
3473 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003474 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003475 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003476 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003477 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003478 }
3479
Eddie Dong2cc51562007-05-21 07:28:09 +03003480 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003481}
3482
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003483static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003484{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003485 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3486 switch (reg) {
3487 case VCPU_REGS_RSP:
3488 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3489 break;
3490 case VCPU_REGS_RIP:
3491 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3492 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003493 case VCPU_EXREG_PDPTR:
3494 if (enable_ept)
3495 ept_save_pdptrs(vcpu);
3496 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003497 default:
3498 break;
3499 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003500}
3501
Avi Kivity6aa8b732006-12-10 02:21:36 -08003502static __init int cpu_has_kvm_support(void)
3503{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003504 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003505}
3506
3507static __init int vmx_disabled_by_bios(void)
3508{
3509 u64 msr;
3510
3511 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003512 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003513 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003514 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3515 && tboot_enabled())
3516 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003517 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003518 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003519 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003520 && !tboot_enabled()) {
3521 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003522 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003523 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003524 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003525 /* launched w/o TXT and VMX disabled */
3526 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3527 && !tboot_enabled())
3528 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003529 }
3530
3531 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003532}
3533
Dongxiao Xu7725b892010-05-11 18:29:38 +08003534static void kvm_cpu_vmxon(u64 addr)
3535{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003536 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003537 intel_pt_handle_vmx(1);
3538
Dongxiao Xu7725b892010-05-11 18:29:38 +08003539 asm volatile (ASM_VMX_VMXON_RAX
3540 : : "a"(&addr), "m"(addr)
3541 : "memory", "cc");
3542}
3543
Radim Krčmář13a34e02014-08-28 15:13:03 +02003544static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545{
3546 int cpu = raw_smp_processor_id();
3547 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003548 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003549
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003550 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003551 return -EBUSY;
3552
Nadav Har'Eld462b812011-05-24 15:26:10 +03003553 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003554 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3555 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003556
3557 /*
3558 * Now we can enable the vmclear operation in kdump
3559 * since the loaded_vmcss_on_cpu list on this cpu
3560 * has been initialized.
3561 *
3562 * Though the cpu is not in VMX operation now, there
3563 * is no problem to enable the vmclear operation
3564 * for the loaded_vmcss_on_cpu list is empty!
3565 */
3566 crash_enable_local_vmclear(cpu);
3567
Avi Kivity6aa8b732006-12-10 02:21:36 -08003568 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003569
3570 test_bits = FEATURE_CONTROL_LOCKED;
3571 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3572 if (tboot_enabled())
3573 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3574
3575 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003576 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003577 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3578 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003579 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02003580 if (enable_ept)
3581 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02003582
3583 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003584}
3585
Nadav Har'Eld462b812011-05-24 15:26:10 +03003586static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003587{
3588 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003589 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003590
Nadav Har'Eld462b812011-05-24 15:26:10 +03003591 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3592 loaded_vmcss_on_cpu_link)
3593 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003594}
3595
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003596
3597/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3598 * tricks.
3599 */
3600static void kvm_cpu_vmxoff(void)
3601{
3602 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003603
3604 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003605 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003606}
3607
Radim Krčmář13a34e02014-08-28 15:13:03 +02003608static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003609{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003610 vmclear_local_loaded_vmcss();
3611 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003612}
3613
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003614static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003615 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003616{
3617 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003618 u32 ctl = ctl_min | ctl_opt;
3619
3620 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3621
3622 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3623 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3624
3625 /* Ensure minimum (required) set of control bits are supported. */
3626 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003627 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003628
3629 *result = ctl;
3630 return 0;
3631}
3632
Avi Kivity110312c2010-12-21 12:54:20 +02003633static __init bool allow_1_setting(u32 msr, u32 ctl)
3634{
3635 u32 vmx_msr_low, vmx_msr_high;
3636
3637 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3638 return vmx_msr_high & ctl;
3639}
3640
Yang, Sheng002c7f72007-07-31 14:23:01 +03003641static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003642{
3643 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003644 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003645 u32 _pin_based_exec_control = 0;
3646 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003647 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003648 u32 _vmexit_control = 0;
3649 u32 _vmentry_control = 0;
3650
Raghavendra K T10166742012-02-07 23:19:20 +05303651 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003652#ifdef CONFIG_X86_64
3653 CPU_BASED_CR8_LOAD_EXITING |
3654 CPU_BASED_CR8_STORE_EXITING |
3655#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003656 CPU_BASED_CR3_LOAD_EXITING |
3657 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08003658 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003659 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003660 CPU_BASED_USE_TSC_OFFSETING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003661 CPU_BASED_INVLPG_EXITING |
3662 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003663
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003664 if (!kvm_mwait_in_guest())
3665 min |= CPU_BASED_MWAIT_EXITING |
3666 CPU_BASED_MONITOR_EXITING;
3667
Sheng Yangf78e0e22007-10-29 09:40:42 +08003668 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003669 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003670 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003671 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3672 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003673 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003674#ifdef CONFIG_X86_64
3675 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3676 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3677 ~CPU_BASED_CR8_STORE_EXITING;
3678#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003679 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003680 min2 = 0;
3681 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003682 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003683 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003684 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003685 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003686 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003687 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02003688 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00003689 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003690 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003691 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003692 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003693 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003694 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02003695 SECONDARY_EXEC_RDSEED_EXITING |
3696 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003697 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04003698 SECONDARY_EXEC_TSC_SCALING |
3699 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08003700 if (adjust_vmx_controls(min2, opt2,
3701 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003702 &_cpu_based_2nd_exec_control) < 0)
3703 return -EIO;
3704 }
3705#ifndef CONFIG_X86_64
3706 if (!(_cpu_based_2nd_exec_control &
3707 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3708 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3709#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003710
3711 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3712 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003713 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003714 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3715 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003716
Wanpeng Li61f1dd92017-10-18 16:02:19 -07003717 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
3718 &vmx_capability.ept, &vmx_capability.vpid);
3719
Sheng Yangd56f5462008-04-25 10:13:16 +08003720 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003721 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3722 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003723 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3724 CPU_BASED_CR3_STORE_EXITING |
3725 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07003726 } else if (vmx_capability.ept) {
3727 vmx_capability.ept = 0;
3728 pr_warn_once("EPT CAP should not exist if not support "
3729 "1-setting enable EPT VM-execution control\n");
3730 }
3731 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
3732 vmx_capability.vpid) {
3733 vmx_capability.vpid = 0;
3734 pr_warn_once("VPID CAP should not exist if not support "
3735 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08003736 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003737
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003738 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003739#ifdef CONFIG_X86_64
3740 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3741#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003742 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003743 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003744 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3745 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003746 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003747
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01003748 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3749 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3750 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003751 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3752 &_pin_based_exec_control) < 0)
3753 return -EIO;
3754
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003755 if (cpu_has_broken_vmx_preemption_timer())
3756 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003757 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003758 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003759 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3760
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003761 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003762 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003763 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3764 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003765 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003766
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003767 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003768
3769 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3770 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003771 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003772
3773#ifdef CONFIG_X86_64
3774 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3775 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003776 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003777#endif
3778
3779 /* Require Write-Back (WB) memory type for VMCS accesses. */
3780 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003781 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003782
Yang, Sheng002c7f72007-07-31 14:23:01 +03003783 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003784 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003785 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003786 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003787
Yang, Sheng002c7f72007-07-31 14:23:01 +03003788 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3789 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003790 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003791 vmcs_conf->vmexit_ctrl = _vmexit_control;
3792 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003793
Avi Kivity110312c2010-12-21 12:54:20 +02003794 cpu_has_load_ia32_efer =
3795 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3796 VM_ENTRY_LOAD_IA32_EFER)
3797 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3798 VM_EXIT_LOAD_IA32_EFER);
3799
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003800 cpu_has_load_perf_global_ctrl =
3801 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3802 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3803 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3804 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3805
3806 /*
3807 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003808 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003809 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3810 *
3811 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3812 *
3813 * AAK155 (model 26)
3814 * AAP115 (model 30)
3815 * AAT100 (model 37)
3816 * BC86,AAY89,BD102 (model 44)
3817 * BA97 (model 46)
3818 *
3819 */
3820 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3821 switch (boot_cpu_data.x86_model) {
3822 case 26:
3823 case 30:
3824 case 37:
3825 case 44:
3826 case 46:
3827 cpu_has_load_perf_global_ctrl = false;
3828 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3829 "does not work properly. Using workaround\n");
3830 break;
3831 default:
3832 break;
3833 }
3834 }
3835
Borislav Petkov782511b2016-04-04 22:25:03 +02003836 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003837 rdmsrl(MSR_IA32_XSS, host_xss);
3838
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003839 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003840}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003841
3842static struct vmcs *alloc_vmcs_cpu(int cpu)
3843{
3844 int node = cpu_to_node(cpu);
3845 struct page *pages;
3846 struct vmcs *vmcs;
3847
Vlastimil Babka96db8002015-09-08 15:03:50 -07003848 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003849 if (!pages)
3850 return NULL;
3851 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003852 memset(vmcs, 0, vmcs_config.size);
3853 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003854 return vmcs;
3855}
3856
3857static struct vmcs *alloc_vmcs(void)
3858{
Ingo Molnard3b2c332007-01-05 16:36:23 -08003859 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08003860}
3861
3862static void free_vmcs(struct vmcs *vmcs)
3863{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003864 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003865}
3866
Nadav Har'Eld462b812011-05-24 15:26:10 +03003867/*
3868 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3869 */
3870static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3871{
3872 if (!loaded_vmcs->vmcs)
3873 return;
3874 loaded_vmcs_clear(loaded_vmcs);
3875 free_vmcs(loaded_vmcs->vmcs);
3876 loaded_vmcs->vmcs = NULL;
Jim Mattson355f4fb2016-10-28 08:29:39 -07003877 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003878}
3879
Mark Kanda276c7962017-11-27 17:22:26 -06003880static void vmx_nested_free_vmcs02(struct vcpu_vmx *vmx)
3881{
3882 struct loaded_vmcs *loaded_vmcs = &vmx->nested.vmcs02;
3883
3884 /*
3885 * Just leak the VMCS02 if the WARN triggers. Better than
3886 * a use-after-free.
3887 */
3888 if (WARN_ON(vmx->loaded_vmcs == loaded_vmcs))
3889 return;
3890 free_loaded_vmcs(loaded_vmcs);
3891}
3892
Sam Ravnborg39959582007-06-01 00:47:13 -07003893static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003894{
3895 int cpu;
3896
Zachary Amsden3230bb42009-09-29 11:38:37 -10003897 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003898 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003899 per_cpu(vmxarea, cpu) = NULL;
3900 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003901}
3902
Jim Mattsond37f4262017-12-22 12:12:16 -08003903enum vmcs_field_width {
3904 VMCS_FIELD_WIDTH_U16 = 0,
3905 VMCS_FIELD_WIDTH_U64 = 1,
3906 VMCS_FIELD_WIDTH_U32 = 2,
3907 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07003908};
3909
Jim Mattsond37f4262017-12-22 12:12:16 -08003910static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07003911{
3912 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08003913 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07003914 return (field >> 13) & 0x3 ;
3915}
3916
3917static inline int vmcs_field_readonly(unsigned long field)
3918{
3919 return (((field >> 10) & 0x3) == 1);
3920}
3921
Bandan Dasfe2b2012014-04-21 15:20:14 -04003922static void init_vmcs_shadow_fields(void)
3923{
3924 int i, j;
3925
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003926 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
3927 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08003928 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003929 (i + 1 == max_shadow_read_only_fields ||
3930 shadow_read_only_fields[i + 1] != field + 1))
3931 pr_err("Missing field from shadow_read_only_field %x\n",
3932 field + 1);
3933
3934 clear_bit(field, vmx_vmread_bitmap);
3935#ifdef CONFIG_X86_64
3936 if (field & 1)
3937 continue;
3938#endif
3939 if (j < i)
3940 shadow_read_only_fields[j] = field;
3941 j++;
3942 }
3943 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04003944
3945 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003946 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08003947 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003948 (i + 1 == max_shadow_read_write_fields ||
3949 shadow_read_write_fields[i + 1] != field + 1))
3950 pr_err("Missing field from shadow_read_write_field %x\n",
3951 field + 1);
3952
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01003953 /*
3954 * PML and the preemption timer can be emulated, but the
3955 * processor cannot vmwrite to fields that don't exist
3956 * on bare metal.
3957 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003958 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01003959 case GUEST_PML_INDEX:
3960 if (!cpu_has_vmx_pml())
3961 continue;
3962 break;
3963 case VMX_PREEMPTION_TIMER_VALUE:
3964 if (!cpu_has_vmx_preemption_timer())
3965 continue;
3966 break;
3967 case GUEST_INTR_STATUS:
3968 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003969 continue;
3970 break;
3971 default:
3972 break;
3973 }
3974
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003975 clear_bit(field, vmx_vmwrite_bitmap);
3976 clear_bit(field, vmx_vmread_bitmap);
3977#ifdef CONFIG_X86_64
3978 if (field & 1)
3979 continue;
3980#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04003981 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003982 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04003983 j++;
3984 }
3985 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04003986}
3987
Avi Kivity6aa8b732006-12-10 02:21:36 -08003988static __init int alloc_kvm_area(void)
3989{
3990 int cpu;
3991
Zachary Amsden3230bb42009-09-29 11:38:37 -10003992 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003993 struct vmcs *vmcs;
3994
3995 vmcs = alloc_vmcs_cpu(cpu);
3996 if (!vmcs) {
3997 free_kvm_area();
3998 return -ENOMEM;
3999 }
4000
4001 per_cpu(vmxarea, cpu) = vmcs;
4002 }
4003 return 0;
4004}
4005
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004006static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004007 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004008{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004009 if (!emulate_invalid_guest_state) {
4010 /*
4011 * CS and SS RPL should be equal during guest entry according
4012 * to VMX spec, but in reality it is not always so. Since vcpu
4013 * is in the middle of the transition from real mode to
4014 * protected mode it is safe to assume that RPL 0 is a good
4015 * default value.
4016 */
4017 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004018 save->selector &= ~SEGMENT_RPL_MASK;
4019 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004020 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004021 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004022 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004023}
4024
4025static void enter_pmode(struct kvm_vcpu *vcpu)
4026{
4027 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004028 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004029
Gleb Natapovd99e4152012-12-20 16:57:45 +02004030 /*
4031 * Update real mode segment cache. It may be not up-to-date if sement
4032 * register was written while vcpu was in a guest mode.
4033 */
4034 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4035 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4036 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4037 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4038 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4039 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4040
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004041 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004042
Avi Kivity2fb92db2011-04-27 19:42:18 +03004043 vmx_segment_cache_clear(vmx);
4044
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004045 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004046
4047 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004048 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4049 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004050 vmcs_writel(GUEST_RFLAGS, flags);
4051
Rusty Russell66aee912007-07-17 23:34:16 +10004052 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4053 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004054
4055 update_exception_bitmap(vcpu);
4056
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004057 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4058 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4059 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4060 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4061 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4062 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004063}
4064
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004065static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004066{
Mathias Krause772e0312012-08-30 01:30:19 +02004067 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004068 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004069
Gleb Natapovd99e4152012-12-20 16:57:45 +02004070 var.dpl = 0x3;
4071 if (seg == VCPU_SREG_CS)
4072 var.type = 0x3;
4073
4074 if (!emulate_invalid_guest_state) {
4075 var.selector = var.base >> 4;
4076 var.base = var.base & 0xffff0;
4077 var.limit = 0xffff;
4078 var.g = 0;
4079 var.db = 0;
4080 var.present = 1;
4081 var.s = 1;
4082 var.l = 0;
4083 var.unusable = 0;
4084 var.type = 0x3;
4085 var.avl = 0;
4086 if (save->base & 0xf)
4087 printk_once(KERN_WARNING "kvm: segment base is not "
4088 "paragraph aligned when entering "
4089 "protected mode (seg=%d)", seg);
4090 }
4091
4092 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004093 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004094 vmcs_write32(sf->limit, var.limit);
4095 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004096}
4097
4098static void enter_rmode(struct kvm_vcpu *vcpu)
4099{
4100 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004101 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004102
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004103 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4104 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4105 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4106 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4107 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004108 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4109 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004110
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004111 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004112
Gleb Natapov776e58e2011-03-13 12:34:27 +02004113 /*
4114 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004115 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004116 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004117 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004118 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4119 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004120
Avi Kivity2fb92db2011-04-27 19:42:18 +03004121 vmx_segment_cache_clear(vmx);
4122
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004123 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004124 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004125 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4126
4127 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004128 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004129
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004130 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004131
4132 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004133 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004134 update_exception_bitmap(vcpu);
4135
Gleb Natapovd99e4152012-12-20 16:57:45 +02004136 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4137 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4138 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4139 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4140 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4141 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004142
Eddie Dong8668a3c2007-10-10 14:26:45 +08004143 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004144}
4145
Amit Shah401d10d2009-02-20 22:53:37 +05304146static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4147{
4148 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004149 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4150
4151 if (!msr)
4152 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304153
Avi Kivity44ea2b12009-09-06 15:55:37 +03004154 /*
4155 * Force kernel_gs_base reloading before EFER changes, as control
4156 * of this msr depends on is_long_mode().
4157 */
4158 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004159 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304160 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004161 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304162 msr->data = efer;
4163 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004164 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304165
4166 msr->data = efer & ~EFER_LME;
4167 }
4168 setup_msrs(vmx);
4169}
4170
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004171#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004172
4173static void enter_lmode(struct kvm_vcpu *vcpu)
4174{
4175 u32 guest_tr_ar;
4176
Avi Kivity2fb92db2011-04-27 19:42:18 +03004177 vmx_segment_cache_clear(to_vmx(vcpu));
4178
Avi Kivity6aa8b732006-12-10 02:21:36 -08004179 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004180 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004181 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4182 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004183 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004184 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4185 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004186 }
Avi Kivityda38f432010-07-06 11:30:49 +03004187 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004188}
4189
4190static void exit_lmode(struct kvm_vcpu *vcpu)
4191{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004192 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004193 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004194}
4195
4196#endif
4197
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004198static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4199 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004200{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004201 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004202 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4203 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004204 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004205 } else {
4206 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004207 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004208}
4209
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004210static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004211{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004212 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004213}
4214
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004215static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4216{
4217 if (enable_ept)
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004218 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004219}
4220
Avi Kivitye8467fd2009-12-29 18:43:06 +02004221static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4222{
4223 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4224
4225 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4226 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4227}
4228
Avi Kivityaff48ba2010-12-05 18:56:11 +02004229static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4230{
4231 if (enable_ept && is_paging(vcpu))
4232 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4233 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4234}
4235
Anthony Liguori25c4c272007-04-27 09:29:21 +03004236static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004237{
Avi Kivityfc78f512009-12-07 12:16:48 +02004238 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4239
4240 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4241 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004242}
4243
Sheng Yang14394422008-04-28 12:24:45 +08004244static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4245{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004246 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4247
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004248 if (!test_bit(VCPU_EXREG_PDPTR,
4249 (unsigned long *)&vcpu->arch.regs_dirty))
4250 return;
4251
Sheng Yang14394422008-04-28 12:24:45 +08004252 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004253 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4254 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4255 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4256 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004257 }
4258}
4259
Avi Kivity8f5d5492009-05-31 18:41:29 +03004260static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4261{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004262 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4263
Avi Kivity8f5d5492009-05-31 18:41:29 +03004264 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004265 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4266 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4267 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4268 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004269 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004270
4271 __set_bit(VCPU_EXREG_PDPTR,
4272 (unsigned long *)&vcpu->arch.regs_avail);
4273 __set_bit(VCPU_EXREG_PDPTR,
4274 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004275}
4276
David Matlack38991522016-11-29 18:14:08 -08004277static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4278{
4279 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4280 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4281 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4282
4283 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
4284 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4285 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4286 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4287
4288 return fixed_bits_valid(val, fixed0, fixed1);
4289}
4290
4291static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4292{
4293 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4294 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4295
4296 return fixed_bits_valid(val, fixed0, fixed1);
4297}
4298
4299static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4300{
4301 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0;
4302 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1;
4303
4304 return fixed_bits_valid(val, fixed0, fixed1);
4305}
4306
4307/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4308#define nested_guest_cr4_valid nested_cr4_valid
4309#define nested_host_cr4_valid nested_cr4_valid
4310
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004311static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004312
4313static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4314 unsigned long cr0,
4315 struct kvm_vcpu *vcpu)
4316{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004317 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4318 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004319 if (!(cr0 & X86_CR0_PG)) {
4320 /* From paging/starting to nonpaging */
4321 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004322 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004323 (CPU_BASED_CR3_LOAD_EXITING |
4324 CPU_BASED_CR3_STORE_EXITING));
4325 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004326 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004327 } else if (!is_paging(vcpu)) {
4328 /* From nonpaging to paging */
4329 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004330 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004331 ~(CPU_BASED_CR3_LOAD_EXITING |
4332 CPU_BASED_CR3_STORE_EXITING));
4333 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004334 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004335 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004336
4337 if (!(cr0 & X86_CR0_WP))
4338 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004339}
4340
Avi Kivity6aa8b732006-12-10 02:21:36 -08004341static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4342{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004343 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004344 unsigned long hw_cr0;
4345
Gleb Natapov50378782013-02-04 16:00:28 +02004346 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004347 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004348 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004349 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004350 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004351
Gleb Natapov218e7632013-01-21 15:36:45 +02004352 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4353 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004354
Gleb Natapov218e7632013-01-21 15:36:45 +02004355 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4356 enter_rmode(vcpu);
4357 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004358
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004359#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004360 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004361 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004362 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004363 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004364 exit_lmode(vcpu);
4365 }
4366#endif
4367
Avi Kivity089d0342009-03-23 18:26:32 +02004368 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004369 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4370
Avi Kivity6aa8b732006-12-10 02:21:36 -08004371 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004372 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004373 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004374
4375 /* depends on vcpu->arch.cr0 to be set to a new value */
4376 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004377}
4378
Yu Zhang855feb62017-08-24 20:27:55 +08004379static int get_ept_level(struct kvm_vcpu *vcpu)
4380{
4381 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4382 return 5;
4383 return 4;
4384}
4385
Peter Feiner995f00a2017-06-30 17:26:32 -07004386static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004387{
Yu Zhang855feb62017-08-24 20:27:55 +08004388 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004389
Yu Zhang855feb62017-08-24 20:27:55 +08004390 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004391
Peter Feiner995f00a2017-06-30 17:26:32 -07004392 if (enable_ept_ad_bits &&
4393 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004394 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004395 eptp |= (root_hpa & PAGE_MASK);
4396
4397 return eptp;
4398}
4399
Avi Kivity6aa8b732006-12-10 02:21:36 -08004400static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4401{
Sheng Yang14394422008-04-28 12:24:45 +08004402 unsigned long guest_cr3;
4403 u64 eptp;
4404
4405 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004406 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004407 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004408 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004409 if (is_paging(vcpu) || is_guest_mode(vcpu))
4410 guest_cr3 = kvm_read_cr3(vcpu);
4411 else
4412 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004413 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004414 }
4415
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004416 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08004417 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004418}
4419
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004420static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004421{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004422 /*
4423 * Pass through host's Machine Check Enable value to hw_cr4, which
4424 * is in force while we are in guest mode. Do not let guests control
4425 * this bit, even if host CR4.MCE == 0.
4426 */
4427 unsigned long hw_cr4 =
4428 (cr4_read_shadow() & X86_CR4_MCE) |
4429 (cr4 & ~X86_CR4_MCE) |
4430 (to_vmx(vcpu)->rmode.vm86_active ?
4431 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004432
Paolo Bonzini0367f202016-07-12 10:44:55 +02004433 if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) {
4434 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4435 SECONDARY_EXEC_DESC);
4436 hw_cr4 &= ~X86_CR4_UMIP;
4437 } else
4438 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4439 SECONDARY_EXEC_DESC);
4440
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004441 if (cr4 & X86_CR4_VMXE) {
4442 /*
4443 * To use VMXON (and later other VMX instructions), a guest
4444 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4445 * So basically the check on whether to allow nested VMX
4446 * is here.
4447 */
4448 if (!nested_vmx_allowed(vcpu))
4449 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004450 }
David Matlack38991522016-11-29 18:14:08 -08004451
4452 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004453 return 1;
4454
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004455 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004456 if (enable_ept) {
4457 if (!is_paging(vcpu)) {
4458 hw_cr4 &= ~X86_CR4_PAE;
4459 hw_cr4 |= X86_CR4_PSE;
4460 } else if (!(cr4 & X86_CR4_PAE)) {
4461 hw_cr4 &= ~X86_CR4_PAE;
4462 }
4463 }
Sheng Yang14394422008-04-28 12:24:45 +08004464
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004465 if (!enable_unrestricted_guest && !is_paging(vcpu))
4466 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004467 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4468 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4469 * to be manually disabled when guest switches to non-paging
4470 * mode.
4471 *
4472 * If !enable_unrestricted_guest, the CPU is always running
4473 * with CR0.PG=1 and CR4 needs to be modified.
4474 * If enable_unrestricted_guest, the CPU automatically
4475 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004476 */
Huaitong Handdba2622016-03-22 16:51:15 +08004477 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004478
Sheng Yang14394422008-04-28 12:24:45 +08004479 vmcs_writel(CR4_READ_SHADOW, cr4);
4480 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004481 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004482}
4483
Avi Kivity6aa8b732006-12-10 02:21:36 -08004484static void vmx_get_segment(struct kvm_vcpu *vcpu,
4485 struct kvm_segment *var, int seg)
4486{
Avi Kivitya9179492011-01-03 14:28:52 +02004487 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004488 u32 ar;
4489
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004490 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004491 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004492 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004493 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004494 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004495 var->base = vmx_read_guest_seg_base(vmx, seg);
4496 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4497 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004498 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004499 var->base = vmx_read_guest_seg_base(vmx, seg);
4500 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4501 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4502 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004503 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004504 var->type = ar & 15;
4505 var->s = (ar >> 4) & 1;
4506 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004507 /*
4508 * Some userspaces do not preserve unusable property. Since usable
4509 * segment has to be present according to VMX spec we can use present
4510 * property to amend userspace bug by making unusable segment always
4511 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4512 * segment as unusable.
4513 */
4514 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004515 var->avl = (ar >> 12) & 1;
4516 var->l = (ar >> 13) & 1;
4517 var->db = (ar >> 14) & 1;
4518 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004519}
4520
Avi Kivitya9179492011-01-03 14:28:52 +02004521static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4522{
Avi Kivitya9179492011-01-03 14:28:52 +02004523 struct kvm_segment s;
4524
4525 if (to_vmx(vcpu)->rmode.vm86_active) {
4526 vmx_get_segment(vcpu, &s, seg);
4527 return s.base;
4528 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004529 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004530}
4531
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004532static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004533{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004534 struct vcpu_vmx *vmx = to_vmx(vcpu);
4535
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004536 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004537 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004538 else {
4539 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004540 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004541 }
Avi Kivity69c73022011-03-07 15:26:44 +02004542}
4543
Avi Kivity653e3102007-05-07 10:55:37 +03004544static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004545{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004546 u32 ar;
4547
Avi Kivityf0495f92012-06-07 17:06:10 +03004548 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004549 ar = 1 << 16;
4550 else {
4551 ar = var->type & 15;
4552 ar |= (var->s & 1) << 4;
4553 ar |= (var->dpl & 3) << 5;
4554 ar |= (var->present & 1) << 7;
4555 ar |= (var->avl & 1) << 12;
4556 ar |= (var->l & 1) << 13;
4557 ar |= (var->db & 1) << 14;
4558 ar |= (var->g & 1) << 15;
4559 }
Avi Kivity653e3102007-05-07 10:55:37 +03004560
4561 return ar;
4562}
4563
4564static void vmx_set_segment(struct kvm_vcpu *vcpu,
4565 struct kvm_segment *var, int seg)
4566{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004567 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004568 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004569
Avi Kivity2fb92db2011-04-27 19:42:18 +03004570 vmx_segment_cache_clear(vmx);
4571
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004572 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4573 vmx->rmode.segs[seg] = *var;
4574 if (seg == VCPU_SREG_TR)
4575 vmcs_write16(sf->selector, var->selector);
4576 else if (var->s)
4577 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004578 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004579 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004580
Avi Kivity653e3102007-05-07 10:55:37 +03004581 vmcs_writel(sf->base, var->base);
4582 vmcs_write32(sf->limit, var->limit);
4583 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004584
4585 /*
4586 * Fix the "Accessed" bit in AR field of segment registers for older
4587 * qemu binaries.
4588 * IA32 arch specifies that at the time of processor reset the
4589 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004590 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004591 * state vmexit when "unrestricted guest" mode is turned on.
4592 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4593 * tree. Newer qemu binaries with that qemu fix would not need this
4594 * kvm hack.
4595 */
4596 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004597 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004598
Gleb Natapovf924d662012-12-12 19:10:55 +02004599 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004600
4601out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004602 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004603}
4604
Avi Kivity6aa8b732006-12-10 02:21:36 -08004605static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4606{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004607 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004608
4609 *db = (ar >> 14) & 1;
4610 *l = (ar >> 13) & 1;
4611}
4612
Gleb Natapov89a27f42010-02-16 10:51:48 +02004613static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004614{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004615 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4616 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004617}
4618
Gleb Natapov89a27f42010-02-16 10:51:48 +02004619static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004620{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004621 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4622 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004623}
4624
Gleb Natapov89a27f42010-02-16 10:51:48 +02004625static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004626{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004627 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4628 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004629}
4630
Gleb Natapov89a27f42010-02-16 10:51:48 +02004631static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004632{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004633 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4634 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004635}
4636
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004637static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4638{
4639 struct kvm_segment var;
4640 u32 ar;
4641
4642 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004643 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004644 if (seg == VCPU_SREG_CS)
4645 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004646 ar = vmx_segment_access_rights(&var);
4647
4648 if (var.base != (var.selector << 4))
4649 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004650 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004651 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004652 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004653 return false;
4654
4655 return true;
4656}
4657
4658static bool code_segment_valid(struct kvm_vcpu *vcpu)
4659{
4660 struct kvm_segment cs;
4661 unsigned int cs_rpl;
4662
4663 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004664 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004665
Avi Kivity1872a3f2009-01-04 23:26:52 +02004666 if (cs.unusable)
4667 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004668 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004669 return false;
4670 if (!cs.s)
4671 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004672 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004673 if (cs.dpl > cs_rpl)
4674 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004675 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004676 if (cs.dpl != cs_rpl)
4677 return false;
4678 }
4679 if (!cs.present)
4680 return false;
4681
4682 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4683 return true;
4684}
4685
4686static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4687{
4688 struct kvm_segment ss;
4689 unsigned int ss_rpl;
4690
4691 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004692 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004693
Avi Kivity1872a3f2009-01-04 23:26:52 +02004694 if (ss.unusable)
4695 return true;
4696 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004697 return false;
4698 if (!ss.s)
4699 return false;
4700 if (ss.dpl != ss_rpl) /* DPL != RPL */
4701 return false;
4702 if (!ss.present)
4703 return false;
4704
4705 return true;
4706}
4707
4708static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4709{
4710 struct kvm_segment var;
4711 unsigned int rpl;
4712
4713 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004714 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004715
Avi Kivity1872a3f2009-01-04 23:26:52 +02004716 if (var.unusable)
4717 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004718 if (!var.s)
4719 return false;
4720 if (!var.present)
4721 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004722 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004723 if (var.dpl < rpl) /* DPL < RPL */
4724 return false;
4725 }
4726
4727 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4728 * rights flags
4729 */
4730 return true;
4731}
4732
4733static bool tr_valid(struct kvm_vcpu *vcpu)
4734{
4735 struct kvm_segment tr;
4736
4737 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4738
Avi Kivity1872a3f2009-01-04 23:26:52 +02004739 if (tr.unusable)
4740 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004741 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004742 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004743 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004744 return false;
4745 if (!tr.present)
4746 return false;
4747
4748 return true;
4749}
4750
4751static bool ldtr_valid(struct kvm_vcpu *vcpu)
4752{
4753 struct kvm_segment ldtr;
4754
4755 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4756
Avi Kivity1872a3f2009-01-04 23:26:52 +02004757 if (ldtr.unusable)
4758 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004759 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004760 return false;
4761 if (ldtr.type != 2)
4762 return false;
4763 if (!ldtr.present)
4764 return false;
4765
4766 return true;
4767}
4768
4769static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4770{
4771 struct kvm_segment cs, ss;
4772
4773 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4774 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4775
Nadav Amitb32a9912015-03-29 16:33:04 +03004776 return ((cs.selector & SEGMENT_RPL_MASK) ==
4777 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004778}
4779
4780/*
4781 * Check if guest state is valid. Returns true if valid, false if
4782 * not.
4783 * We assume that registers are always usable
4784 */
4785static bool guest_state_valid(struct kvm_vcpu *vcpu)
4786{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004787 if (enable_unrestricted_guest)
4788 return true;
4789
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004790 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004791 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004792 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4793 return false;
4794 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4795 return false;
4796 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4797 return false;
4798 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4799 return false;
4800 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4801 return false;
4802 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4803 return false;
4804 } else {
4805 /* protected mode guest state checks */
4806 if (!cs_ss_rpl_check(vcpu))
4807 return false;
4808 if (!code_segment_valid(vcpu))
4809 return false;
4810 if (!stack_segment_valid(vcpu))
4811 return false;
4812 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4813 return false;
4814 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4815 return false;
4816 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4817 return false;
4818 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4819 return false;
4820 if (!tr_valid(vcpu))
4821 return false;
4822 if (!ldtr_valid(vcpu))
4823 return false;
4824 }
4825 /* TODO:
4826 * - Add checks on RIP
4827 * - Add checks on RFLAGS
4828 */
4829
4830 return true;
4831}
4832
Jim Mattson5fa99cb2017-07-06 16:33:07 -07004833static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
4834{
4835 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
4836}
4837
Mike Dayd77c26f2007-10-08 09:02:08 -04004838static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004839{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004840 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004841 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004842 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004843
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004844 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004845 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004846 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4847 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004848 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004849 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004850 r = kvm_write_guest_page(kvm, fn++, &data,
4851 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004852 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004853 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004854 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4855 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004856 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004857 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4858 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004859 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004860 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004861 r = kvm_write_guest_page(kvm, fn, &data,
4862 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4863 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004864out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004865 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004866 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004867}
4868
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004869static int init_rmode_identity_map(struct kvm *kvm)
4870{
Tang Chenf51770e2014-09-16 18:41:59 +08004871 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004872 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004873 u32 tmp;
4874
Tang Chena255d472014-09-16 18:41:58 +08004875 /* Protect kvm->arch.ept_identity_pagetable_done. */
4876 mutex_lock(&kvm->slots_lock);
4877
Tang Chenf51770e2014-09-16 18:41:59 +08004878 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004879 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004880
David Hildenbrandd8a6e362017-08-24 20:51:34 +02004881 if (!kvm->arch.ept_identity_map_addr)
4882 kvm->arch.ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004883 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004884
David Hildenbrandd8a6e362017-08-24 20:51:34 +02004885 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4886 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08004887 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004888 goto out2;
4889
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004890 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004891 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4892 if (r < 0)
4893 goto out;
4894 /* Set up identity-mapping pagetable for EPT in real mode */
4895 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4896 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4897 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4898 r = kvm_write_guest_page(kvm, identity_map_pfn,
4899 &tmp, i * sizeof(tmp), sizeof(tmp));
4900 if (r < 0)
4901 goto out;
4902 }
4903 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004904
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004905out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004906 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004907
4908out2:
4909 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004910 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004911}
4912
Avi Kivity6aa8b732006-12-10 02:21:36 -08004913static void seg_setup(int seg)
4914{
Mathias Krause772e0312012-08-30 01:30:19 +02004915 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004916 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004917
4918 vmcs_write16(sf->selector, 0);
4919 vmcs_writel(sf->base, 0);
4920 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004921 ar = 0x93;
4922 if (seg == VCPU_SREG_CS)
4923 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004924
4925 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004926}
4927
Sheng Yangf78e0e22007-10-29 09:40:42 +08004928static int alloc_apic_access_page(struct kvm *kvm)
4929{
Xiao Guangrong44841412012-09-07 14:14:20 +08004930 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004931 int r = 0;
4932
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004933 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004934 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004935 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004936 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4937 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004938 if (r)
4939 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004940
Tang Chen73a6d942014-09-11 13:38:00 +08004941 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004942 if (is_error_page(page)) {
4943 r = -EFAULT;
4944 goto out;
4945 }
4946
Tang Chenc24ae0d2014-09-24 15:57:58 +08004947 /*
4948 * Do not pin the page in memory, so that memory hot-unplug
4949 * is able to migrate it.
4950 */
4951 put_page(page);
4952 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004953out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004954 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004955 return r;
4956}
4957
Wanpeng Li991e7a02015-09-16 17:30:05 +08004958static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004959{
4960 int vpid;
4961
Avi Kivity919818a2009-03-23 18:01:29 +02004962 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004963 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004964 spin_lock(&vmx_vpid_lock);
4965 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004966 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004967 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004968 else
4969 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004970 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004971 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004972}
4973
Wanpeng Li991e7a02015-09-16 17:30:05 +08004974static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004975{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004976 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004977 return;
4978 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004979 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004980 spin_unlock(&vmx_vpid_lock);
4981}
4982
Yang Zhang8d146952013-01-25 10:18:50 +08004983#define MSR_TYPE_R 1
4984#define MSR_TYPE_W 2
4985static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4986 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004987{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004988 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004989
4990 if (!cpu_has_vmx_msr_bitmap())
4991 return;
4992
4993 /*
4994 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4995 * have the write-low and read-high bitmap offsets the wrong way round.
4996 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4997 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004998 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004999 if (type & MSR_TYPE_R)
5000 /* read-low */
5001 __clear_bit(msr, msr_bitmap + 0x000 / f);
5002
5003 if (type & MSR_TYPE_W)
5004 /* write-low */
5005 __clear_bit(msr, msr_bitmap + 0x800 / f);
5006
Sheng Yang25c5f222008-03-28 13:18:56 +08005007 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5008 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005009 if (type & MSR_TYPE_R)
5010 /* read-high */
5011 __clear_bit(msr, msr_bitmap + 0x400 / f);
5012
5013 if (type & MSR_TYPE_W)
5014 /* write-high */
5015 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5016
5017 }
5018}
5019
Wincy Vanf2b93282015-02-03 23:56:03 +08005020/*
5021 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5022 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5023 */
5024static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5025 unsigned long *msr_bitmap_nested,
5026 u32 msr, int type)
5027{
5028 int f = sizeof(unsigned long);
5029
5030 if (!cpu_has_vmx_msr_bitmap()) {
5031 WARN_ON(1);
5032 return;
5033 }
5034
5035 /*
5036 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5037 * have the write-low and read-high bitmap offsets the wrong way round.
5038 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5039 */
5040 if (msr <= 0x1fff) {
5041 if (type & MSR_TYPE_R &&
5042 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5043 /* read-low */
5044 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5045
5046 if (type & MSR_TYPE_W &&
5047 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5048 /* write-low */
5049 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5050
5051 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5052 msr &= 0x1fff;
5053 if (type & MSR_TYPE_R &&
5054 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5055 /* read-high */
5056 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5057
5058 if (type & MSR_TYPE_W &&
5059 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5060 /* write-high */
5061 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5062
5063 }
5064}
5065
Avi Kivity58972972009-02-24 22:26:47 +02005066static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
5067{
5068 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08005069 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
5070 msr, MSR_TYPE_R | MSR_TYPE_W);
5071 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
5072 msr, MSR_TYPE_R | MSR_TYPE_W);
5073}
5074
Radim Krčmář2e69f862016-09-29 22:41:32 +02005075static void vmx_disable_intercept_msr_x2apic(u32 msr, int type, bool apicv_active)
Yang Zhang8d146952013-01-25 10:18:50 +08005076{
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005077 if (apicv_active) {
Wanpeng Lic63e4562016-09-23 19:17:16 +08005078 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv,
Radim Krčmář2e69f862016-09-29 22:41:32 +02005079 msr, type);
Wanpeng Lic63e4562016-09-23 19:17:16 +08005080 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv,
Radim Krčmář2e69f862016-09-29 22:41:32 +02005081 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005082 } else {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005083 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
Radim Krčmář2e69f862016-09-29 22:41:32 +02005084 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005085 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
Radim Krčmář2e69f862016-09-29 22:41:32 +02005086 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005087 }
Avi Kivity58972972009-02-24 22:26:47 +02005088}
5089
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005090static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005091{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005092 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005093}
5094
David Matlackc9f04402017-08-01 14:00:40 -07005095static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5096{
5097 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5098 gfn_t gfn;
5099
5100 /*
5101 * Don't need to mark the APIC access page dirty; it is never
5102 * written to by the CPU during APIC virtualization.
5103 */
5104
5105 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5106 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5107 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5108 }
5109
5110 if (nested_cpu_has_posted_intr(vmcs12)) {
5111 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5112 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5113 }
5114}
5115
5116
David Hildenbrand6342c502017-01-25 11:58:58 +01005117static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005118{
5119 struct vcpu_vmx *vmx = to_vmx(vcpu);
5120 int max_irr;
5121 void *vapic_page;
5122 u16 status;
5123
David Matlackc9f04402017-08-01 14:00:40 -07005124 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5125 return;
Wincy Van705699a2015-02-03 23:58:17 +08005126
David Matlackc9f04402017-08-01 14:00:40 -07005127 vmx->nested.pi_pending = false;
5128 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5129 return;
Wincy Van705699a2015-02-03 23:58:17 +08005130
David Matlackc9f04402017-08-01 14:00:40 -07005131 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5132 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005133 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005134 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5135 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005136 kunmap(vmx->nested.virtual_apic_page);
5137
5138 status = vmcs_read16(GUEST_INTR_STATUS);
5139 if ((u8)max_irr > ((u8)status & 0xff)) {
5140 status &= ~0xff;
5141 status |= (u8)max_irr;
5142 vmcs_write16(GUEST_INTR_STATUS, status);
5143 }
5144 }
David Matlackc9f04402017-08-01 14:00:40 -07005145
5146 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005147}
5148
Wincy Van06a55242017-04-28 13:13:59 +08005149static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5150 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005151{
5152#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005153 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5154
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005155 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005156 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005157 * The vector of interrupt to be delivered to vcpu had
5158 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005159 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005160 * Following cases will be reached in this block, and
5161 * we always send a notification event in all cases as
5162 * explained below.
5163 *
5164 * Case 1: vcpu keeps in non-root mode. Sending a
5165 * notification event posts the interrupt to vcpu.
5166 *
5167 * Case 2: vcpu exits to root mode and is still
5168 * runnable. PIR will be synced to vIRR before the
5169 * next vcpu entry. Sending a notification event in
5170 * this case has no effect, as vcpu is not in root
5171 * mode.
5172 *
5173 * Case 3: vcpu exits to root mode and is blocked.
5174 * vcpu_block() has already synced PIR to vIRR and
5175 * never blocks vcpu if vIRR is not cleared. Therefore,
5176 * a blocked vcpu here does not wait for any requested
5177 * interrupts in PIR, and sending a notification event
5178 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005179 */
Feng Wu28b835d2015-09-18 22:29:54 +08005180
Wincy Van06a55242017-04-28 13:13:59 +08005181 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005182 return true;
5183 }
5184#endif
5185 return false;
5186}
5187
Wincy Van705699a2015-02-03 23:58:17 +08005188static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5189 int vector)
5190{
5191 struct vcpu_vmx *vmx = to_vmx(vcpu);
5192
5193 if (is_guest_mode(vcpu) &&
5194 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005195 /*
5196 * If a posted intr is not recognized by hardware,
5197 * we will accomplish it in the next vmentry.
5198 */
5199 vmx->nested.pi_pending = true;
5200 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005201 /* the PIR and ON have been set by L1. */
5202 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5203 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005204 return 0;
5205 }
5206 return -1;
5207}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005208/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005209 * Send interrupt to vcpu via posted interrupt way.
5210 * 1. If target vcpu is running(non-root mode), send posted interrupt
5211 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5212 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5213 * interrupt from PIR in next vmentry.
5214 */
5215static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5216{
5217 struct vcpu_vmx *vmx = to_vmx(vcpu);
5218 int r;
5219
Wincy Van705699a2015-02-03 23:58:17 +08005220 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5221 if (!r)
5222 return;
5223
Yang Zhanga20ed542013-04-11 19:25:15 +08005224 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5225 return;
5226
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005227 /* If a previous notification has sent the IPI, nothing to do. */
5228 if (pi_test_and_set_on(&vmx->pi_desc))
5229 return;
5230
Wincy Van06a55242017-04-28 13:13:59 +08005231 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005232 kvm_vcpu_kick(vcpu);
5233}
5234
Avi Kivity6aa8b732006-12-10 02:21:36 -08005235/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005236 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5237 * will not change in the lifetime of the guest.
5238 * Note that host-state that does change is set elsewhere. E.g., host-state
5239 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5240 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005241static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005242{
5243 u32 low32, high32;
5244 unsigned long tmpl;
5245 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005246 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005247
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005248 cr0 = read_cr0();
5249 WARN_ON(cr0 & X86_CR0_TS);
5250 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005251
5252 /*
5253 * Save the most likely value for this task's CR3 in the VMCS.
5254 * We can't use __get_current_cr3_fast() because we're not atomic.
5255 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005256 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005257 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005258 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005259
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005260 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005261 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005262 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005263 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005264
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005265 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005266#ifdef CONFIG_X86_64
5267 /*
5268 * Load null selectors, so we can avoid reloading them in
5269 * __vmx_load_host_state(), in case userspace uses the null selectors
5270 * too (the expected case).
5271 */
5272 vmcs_write16(HOST_DS_SELECTOR, 0);
5273 vmcs_write16(HOST_ES_SELECTOR, 0);
5274#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005275 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5276 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005277#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005278 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5279 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5280
Juergen Gross87930012017-09-04 12:25:27 +02005281 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005282 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005283 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005284
Avi Kivity83287ea422012-09-16 15:10:57 +03005285 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005286
5287 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5288 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5289 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5290 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5291
5292 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5293 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5294 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5295 }
5296}
5297
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005298static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5299{
5300 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5301 if (enable_ept)
5302 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005303 if (is_guest_mode(&vmx->vcpu))
5304 vmx->vcpu.arch.cr4_guest_owned_bits &=
5305 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005306 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5307}
5308
Yang Zhang01e439b2013-04-11 19:25:12 +08005309static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5310{
5311 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5312
Andrey Smetanind62caab2015-11-10 15:36:33 +03005313 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005314 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005315
5316 if (!enable_vnmi)
5317 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5318
Yunhong Jiang64672c92016-06-13 14:19:59 -07005319 /* Enable the preemption timer dynamically */
5320 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005321 return pin_based_exec_ctrl;
5322}
5323
Andrey Smetanind62caab2015-11-10 15:36:33 +03005324static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5325{
5326 struct vcpu_vmx *vmx = to_vmx(vcpu);
5327
5328 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005329 if (cpu_has_secondary_exec_ctrls()) {
5330 if (kvm_vcpu_apicv_active(vcpu))
5331 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5332 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5333 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5334 else
5335 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5336 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5337 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5338 }
5339
5340 if (cpu_has_vmx_msr_bitmap())
5341 vmx_set_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005342}
5343
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005344static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5345{
5346 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005347
5348 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5349 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5350
Paolo Bonzini35754c92015-07-29 12:05:37 +02005351 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005352 exec_control &= ~CPU_BASED_TPR_SHADOW;
5353#ifdef CONFIG_X86_64
5354 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5355 CPU_BASED_CR8_LOAD_EXITING;
5356#endif
5357 }
5358 if (!enable_ept)
5359 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5360 CPU_BASED_CR3_LOAD_EXITING |
5361 CPU_BASED_INVLPG_EXITING;
5362 return exec_control;
5363}
5364
Jim Mattson45ec3682017-08-23 16:32:04 -07005365static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005366{
Jim Mattson45ec3682017-08-23 16:32:04 -07005367 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005368 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005369}
5370
Jim Mattson75f4fc82017-08-23 16:32:03 -07005371static bool vmx_rdseed_supported(void)
5372{
5373 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005374 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005375}
5376
Paolo Bonzini80154d72017-08-24 13:55:35 +02005377static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005378{
Paolo Bonzini80154d72017-08-24 13:55:35 +02005379 struct kvm_vcpu *vcpu = &vmx->vcpu;
5380
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005381 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005382
Paolo Bonzini80154d72017-08-24 13:55:35 +02005383 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005384 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5385 if (vmx->vpid == 0)
5386 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5387 if (!enable_ept) {
5388 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5389 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005390 /* Enable INVPCID for non-ept guests may cause performance regression. */
5391 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005392 }
5393 if (!enable_unrestricted_guest)
5394 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5395 if (!ple_gap)
5396 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02005397 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005398 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5399 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005400 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005401
5402 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5403 * in vmx_set_cr4. */
5404 exec_control &= ~SECONDARY_EXEC_DESC;
5405
Abel Gordonabc4fc52013-04-18 14:35:25 +03005406 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5407 (handle_vmptrld).
5408 We can NOT enable shadow_vmcs here because we don't have yet
5409 a current VMCS12
5410 */
5411 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005412
5413 if (!enable_pml)
5414 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005415
Paolo Bonzini3db13482017-08-24 14:48:03 +02005416 if (vmx_xsaves_supported()) {
5417 /* Exposing XSAVES only when XSAVE is exposed */
5418 bool xsaves_enabled =
5419 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5420 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5421
5422 if (!xsaves_enabled)
5423 exec_control &= ~SECONDARY_EXEC_XSAVES;
5424
5425 if (nested) {
5426 if (xsaves_enabled)
5427 vmx->nested.nested_vmx_secondary_ctls_high |=
5428 SECONDARY_EXEC_XSAVES;
5429 else
5430 vmx->nested.nested_vmx_secondary_ctls_high &=
5431 ~SECONDARY_EXEC_XSAVES;
5432 }
5433 }
5434
Paolo Bonzini80154d72017-08-24 13:55:35 +02005435 if (vmx_rdtscp_supported()) {
5436 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5437 if (!rdtscp_enabled)
5438 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5439
5440 if (nested) {
5441 if (rdtscp_enabled)
5442 vmx->nested.nested_vmx_secondary_ctls_high |=
5443 SECONDARY_EXEC_RDTSCP;
5444 else
5445 vmx->nested.nested_vmx_secondary_ctls_high &=
5446 ~SECONDARY_EXEC_RDTSCP;
5447 }
5448 }
5449
5450 if (vmx_invpcid_supported()) {
5451 /* Exposing INVPCID only when PCID is exposed */
5452 bool invpcid_enabled =
5453 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5454 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5455
5456 if (!invpcid_enabled) {
5457 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5458 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5459 }
5460
5461 if (nested) {
5462 if (invpcid_enabled)
5463 vmx->nested.nested_vmx_secondary_ctls_high |=
5464 SECONDARY_EXEC_ENABLE_INVPCID;
5465 else
5466 vmx->nested.nested_vmx_secondary_ctls_high &=
5467 ~SECONDARY_EXEC_ENABLE_INVPCID;
5468 }
5469 }
5470
Jim Mattson45ec3682017-08-23 16:32:04 -07005471 if (vmx_rdrand_supported()) {
5472 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5473 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005474 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005475
5476 if (nested) {
5477 if (rdrand_enabled)
5478 vmx->nested.nested_vmx_secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005479 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005480 else
5481 vmx->nested.nested_vmx_secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005482 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005483 }
5484 }
5485
Jim Mattson75f4fc82017-08-23 16:32:03 -07005486 if (vmx_rdseed_supported()) {
5487 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5488 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005489 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005490
5491 if (nested) {
5492 if (rdseed_enabled)
5493 vmx->nested.nested_vmx_secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005494 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005495 else
5496 vmx->nested.nested_vmx_secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005497 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005498 }
5499 }
5500
Paolo Bonzini80154d72017-08-24 13:55:35 +02005501 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005502}
5503
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005504static void ept_set_mmio_spte_mask(void)
5505{
5506 /*
5507 * EPT Misconfigurations can be generated if the value of bits 2:0
5508 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005509 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07005510 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5511 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005512}
5513
Wanpeng Lif53cd632014-12-02 19:14:58 +08005514#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005515/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005516 * Sets up the vmcs for emulated real mode.
5517 */
David Hildenbrand12d79912017-08-24 20:51:26 +02005518static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005519{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005520#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005521 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005522#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005523 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005524
Abel Gordon4607c2d2013-04-18 14:35:55 +03005525 if (enable_shadow_vmcs) {
5526 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5527 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5528 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005529 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02005530 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08005531
Avi Kivity6aa8b732006-12-10 02:21:36 -08005532 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5533
Avi Kivity6aa8b732006-12-10 02:21:36 -08005534 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005535 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005536 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005537
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005538 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005539
Dan Williamsdfa169b2016-06-02 11:17:24 -07005540 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02005541 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005542 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02005543 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07005544 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005545
Andrey Smetanind62caab2015-11-10 15:36:33 +03005546 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005547 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5548 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5549 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5550 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5551
5552 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005553
Li RongQing0bcf2612015-12-03 13:29:34 +08005554 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005555 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005556 }
5557
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005558 if (ple_gap) {
5559 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005560 vmx->ple_window = ple_window;
5561 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005562 }
5563
Xiao Guangrongc3707952011-07-12 03:28:04 +08005564 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5565 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005566 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5567
Avi Kivity9581d442010-10-19 16:46:55 +02005568 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5569 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005570 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005571#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005572 rdmsrl(MSR_FS_BASE, a);
5573 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5574 rdmsrl(MSR_GS_BASE, a);
5575 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5576#else
5577 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5578 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5579#endif
5580
Bandan Das2a499e42017-08-03 15:54:41 -04005581 if (cpu_has_vmx_vmfunc())
5582 vmcs_write64(VM_FUNCTION_CONTROL, 0);
5583
Eddie Dong2cc51562007-05-21 07:28:09 +03005584 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5585 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005586 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005587 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005588 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005589
Radim Krčmář74545702015-04-27 15:11:25 +02005590 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5591 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005592
Paolo Bonzini03916db2014-07-24 14:21:57 +02005593 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005594 u32 index = vmx_msr_index[i];
5595 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005596 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005597
5598 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5599 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005600 if (wrmsr_safe(index, data_low, data_high) < 0)
5601 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005602 vmx->guest_msrs[j].index = i;
5603 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005604 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005605 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005606 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005607
Gleb Natapov2961e8762013-11-25 15:37:13 +02005608
5609 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005610
5611 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005612 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005613
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005614 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5615 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5616
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005617 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005618
Wanpeng Lif53cd632014-12-02 19:14:58 +08005619 if (vmx_xsaves_supported())
5620 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5621
Peter Feiner4e595162016-07-07 14:49:58 -07005622 if (enable_pml) {
5623 ASSERT(vmx->pml_pg);
5624 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5625 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5626 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005627}
5628
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005629static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005630{
5631 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005632 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005633 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005634
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005635 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005636
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005637 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005638 kvm_set_cr8(vcpu, 0);
5639
5640 if (!init_event) {
5641 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5642 MSR_IA32_APICBASE_ENABLE;
5643 if (kvm_vcpu_is_reset_bsp(vcpu))
5644 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5645 apic_base_msr.host_initiated = true;
5646 kvm_set_apic_base(vcpu, &apic_base_msr);
5647 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005648
Avi Kivity2fb92db2011-04-27 19:42:18 +03005649 vmx_segment_cache_clear(vmx);
5650
Avi Kivity5706be02008-08-20 15:07:31 +03005651 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005652 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005653 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005654
5655 seg_setup(VCPU_SREG_DS);
5656 seg_setup(VCPU_SREG_ES);
5657 seg_setup(VCPU_SREG_FS);
5658 seg_setup(VCPU_SREG_GS);
5659 seg_setup(VCPU_SREG_SS);
5660
5661 vmcs_write16(GUEST_TR_SELECTOR, 0);
5662 vmcs_writel(GUEST_TR_BASE, 0);
5663 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5664 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5665
5666 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5667 vmcs_writel(GUEST_LDTR_BASE, 0);
5668 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5669 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5670
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005671 if (!init_event) {
5672 vmcs_write32(GUEST_SYSENTER_CS, 0);
5673 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5674 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5675 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5676 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005677
Wanpeng Lic37c2872017-11-20 14:52:21 -08005678 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01005679 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005680
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005681 vmcs_writel(GUEST_GDTR_BASE, 0);
5682 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5683
5684 vmcs_writel(GUEST_IDTR_BASE, 0);
5685 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5686
Anthony Liguori443381a2010-12-06 10:53:38 -06005687 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005688 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005689 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07005690 if (kvm_mpx_supported())
5691 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005692
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005693 setup_msrs(vmx);
5694
Avi Kivity6aa8b732006-12-10 02:21:36 -08005695 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5696
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005697 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005698 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005699 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005700 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005701 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005702 vmcs_write32(TPR_THRESHOLD, 0);
5703 }
5704
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005705 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005706
Sheng Yang2384d2b2008-01-17 15:14:33 +08005707 if (vmx->vpid != 0)
5708 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5709
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005710 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005711 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005712 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005713 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005714 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005715
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005716 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005717
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005718 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005719}
5720
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005721/*
5722 * In nested virtualization, check if L1 asked to exit on external interrupts.
5723 * For most existing hypervisors, this will always return true.
5724 */
5725static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5726{
5727 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5728 PIN_BASED_EXT_INTR_MASK;
5729}
5730
Bandan Das77b0f5d2014-04-19 18:17:45 -04005731/*
5732 * In nested virtualization, check if L1 has set
5733 * VM_EXIT_ACK_INTR_ON_EXIT
5734 */
5735static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5736{
5737 return get_vmcs12(vcpu)->vm_exit_controls &
5738 VM_EXIT_ACK_INTR_ON_EXIT;
5739}
5740
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005741static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5742{
5743 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5744 PIN_BASED_NMI_EXITING;
5745}
5746
Jan Kiszkac9a79532014-03-07 20:03:15 +01005747static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005748{
Paolo Bonzini47c01522016-12-19 11:44:07 +01005749 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5750 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005751}
5752
Jan Kiszkac9a79532014-03-07 20:03:15 +01005753static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005754{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005755 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005756 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01005757 enable_irq_window(vcpu);
5758 return;
5759 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005760
Paolo Bonzini47c01522016-12-19 11:44:07 +01005761 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5762 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005763}
5764
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005765static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005766{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005767 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005768 uint32_t intr;
5769 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005770
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005771 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005772
Avi Kivityfa89a812008-09-01 15:57:51 +03005773 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005774 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005775 int inc_eip = 0;
5776 if (vcpu->arch.interrupt.soft)
5777 inc_eip = vcpu->arch.event_exit_inst_len;
5778 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005779 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005780 return;
5781 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005782 intr = irq | INTR_INFO_VALID_MASK;
5783 if (vcpu->arch.interrupt.soft) {
5784 intr |= INTR_TYPE_SOFT_INTR;
5785 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5786 vmx->vcpu.arch.event_exit_inst_len);
5787 } else
5788 intr |= INTR_TYPE_EXT_INTR;
5789 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005790}
5791
Sheng Yangf08864b2008-05-15 18:23:25 +08005792static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5793{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005794 struct vcpu_vmx *vmx = to_vmx(vcpu);
5795
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005796 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005797 /*
5798 * Tracking the NMI-blocked state in software is built upon
5799 * finding the next open IRQ window. This, in turn, depends on
5800 * well-behaving guests: They have to keep IRQs disabled at
5801 * least as long as the NMI handler runs. Otherwise we may
5802 * cause NMI nesting, maybe breaking the guest. But as this is
5803 * highly unlikely, we can live with the residual risk.
5804 */
5805 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
5806 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5807 }
5808
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005809 ++vcpu->stat.nmi_injections;
5810 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005811
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005812 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005813 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005814 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005815 return;
5816 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005817
Sheng Yangf08864b2008-05-15 18:23:25 +08005818 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5819 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005820}
5821
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005822static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5823{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005824 struct vcpu_vmx *vmx = to_vmx(vcpu);
5825 bool masked;
5826
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005827 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005828 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005829 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02005830 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005831 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
5832 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5833 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005834}
5835
5836static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5837{
5838 struct vcpu_vmx *vmx = to_vmx(vcpu);
5839
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005840 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005841 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
5842 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
5843 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5844 }
5845 } else {
5846 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5847 if (masked)
5848 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5849 GUEST_INTR_STATE_NMI);
5850 else
5851 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5852 GUEST_INTR_STATE_NMI);
5853 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005854}
5855
Jan Kiszka2505dc92013-04-14 12:12:47 +02005856static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5857{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005858 if (to_vmx(vcpu)->nested.nested_run_pending)
5859 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005860
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005861 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005862 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
5863 return 0;
5864
Jan Kiszka2505dc92013-04-14 12:12:47 +02005865 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5866 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5867 | GUEST_INTR_STATE_NMI));
5868}
5869
Gleb Natapov78646122009-03-23 12:12:11 +02005870static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5871{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005872 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5873 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005874 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5875 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005876}
5877
Izik Eiduscbc94022007-10-25 00:29:55 +02005878static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5879{
5880 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005881
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005882 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5883 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005884 if (ret)
5885 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005886 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005887 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005888}
5889
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005890static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005891{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005892 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005893 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005894 /*
5895 * Update instruction length as we may reinject the exception
5896 * from user space while in guest debugging mode.
5897 */
5898 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5899 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005900 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005901 return false;
5902 /* fall through */
5903 case DB_VECTOR:
5904 if (vcpu->guest_debug &
5905 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5906 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005907 /* fall through */
5908 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005909 case OF_VECTOR:
5910 case BR_VECTOR:
5911 case UD_VECTOR:
5912 case DF_VECTOR:
5913 case SS_VECTOR:
5914 case GP_VECTOR:
5915 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005916 return true;
5917 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005918 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005919 return false;
5920}
5921
5922static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5923 int vec, u32 err_code)
5924{
5925 /*
5926 * Instruction with address size override prefix opcode 0x67
5927 * Cause the #SS fault with 0 error code in VM86 mode.
5928 */
5929 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5930 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5931 if (vcpu->arch.halt_request) {
5932 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005933 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005934 }
5935 return 1;
5936 }
5937 return 0;
5938 }
5939
5940 /*
5941 * Forward all other exceptions that are valid in real mode.
5942 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5943 * the required debugging infrastructure rework.
5944 */
5945 kvm_queue_exception(vcpu, vec);
5946 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005947}
5948
Andi Kleena0861c02009-06-08 17:37:09 +08005949/*
5950 * Trigger machine check on the host. We assume all the MSRs are already set up
5951 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5952 * We pass a fake environment to the machine check handler because we want
5953 * the guest to be always treated like user space, no matter what context
5954 * it used internally.
5955 */
5956static void kvm_machine_check(void)
5957{
5958#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5959 struct pt_regs regs = {
5960 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5961 .flags = X86_EFLAGS_IF,
5962 };
5963
5964 do_machine_check(&regs, 0);
5965#endif
5966}
5967
Avi Kivity851ba692009-08-24 11:10:17 +03005968static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005969{
5970 /* already handled by vcpu_run */
5971 return 1;
5972}
5973
Avi Kivity851ba692009-08-24 11:10:17 +03005974static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005975{
Avi Kivity1155f762007-11-22 11:30:47 +02005976 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005977 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005978 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005979 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005980 u32 vect_info;
5981 enum emulation_result er;
5982
Avi Kivity1155f762007-11-22 11:30:47 +02005983 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005984 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005985
Andi Kleena0861c02009-06-08 17:37:09 +08005986 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005987 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005988
Jim Mattsonef85b672016-12-12 11:01:37 -08005989 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005990 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005991
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005992 if (is_invalid_opcode(intr_info)) {
Liran Alonac9b3052017-11-06 16:15:10 +02005993 WARN_ON_ONCE(is_guest_mode(vcpu));
Andre Przywara51d8b662010-12-21 11:12:02 +01005994 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alon61cb57c2017-11-05 16:56:32 +02005995 if (er == EMULATE_USER_EXIT)
5996 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005997 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005998 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005999 return 1;
6000 }
6001
Avi Kivity6aa8b732006-12-10 02:21:36 -08006002 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006003 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006004 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006005
6006 /*
6007 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6008 * MMIO, it is better to report an internal error.
6009 * See the comments in vmx_handle_exit.
6010 */
6011 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6012 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6013 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6014 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006015 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006016 vcpu->run->internal.data[0] = vect_info;
6017 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006018 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006019 return 0;
6020 }
6021
Avi Kivity6aa8b732006-12-10 02:21:36 -08006022 if (is_page_fault(intr_info)) {
6023 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006024 /* EPT won't cause page fault directly */
6025 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006026 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006027 }
6028
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006029 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006030
6031 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6032 return handle_rmode_exception(vcpu, ex_no, error_code);
6033
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006034 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006035 case AC_VECTOR:
6036 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6037 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006038 case DB_VECTOR:
6039 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6040 if (!(vcpu->guest_debug &
6041 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006042 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006043 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01006044 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
6045 skip_emulated_instruction(vcpu);
6046
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006047 kvm_queue_exception(vcpu, DB_VECTOR);
6048 return 1;
6049 }
6050 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6051 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6052 /* fall through */
6053 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006054 /*
6055 * Update instruction length as we may reinject #BP from
6056 * user space while in guest debugging mode. Reading it for
6057 * #DB as well causes no harm, it is not used in that case.
6058 */
6059 vmx->vcpu.arch.event_exit_inst_len =
6060 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006061 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006062 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006063 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6064 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006065 break;
6066 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006067 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6068 kvm_run->ex.exception = ex_no;
6069 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006070 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006071 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006072 return 0;
6073}
6074
Avi Kivity851ba692009-08-24 11:10:17 +03006075static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006076{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006077 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006078 return 1;
6079}
6080
Avi Kivity851ba692009-08-24 11:10:17 +03006081static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006082{
Avi Kivity851ba692009-08-24 11:10:17 +03006083 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006084 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006085 return 0;
6086}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006087
Avi Kivity851ba692009-08-24 11:10:17 +03006088static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006089{
He, Qingbfdaab02007-09-12 14:18:28 +08006090 unsigned long exit_qualification;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006091 int size, in, string, ret;
Avi Kivity039576c2007-03-20 12:46:50 +02006092 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006093
He, Qingbfdaab02007-09-12 14:18:28 +08006094 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006095 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006096 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006097
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006098 ++vcpu->stat.io_exits;
6099
6100 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01006101 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006102
6103 port = exit_qualification >> 16;
6104 size = (exit_qualification & 7) + 1;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006105
Kyle Huey6affcbe2016-11-29 12:40:40 -08006106 ret = kvm_skip_emulated_instruction(vcpu);
6107
6108 /*
6109 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
6110 * KVM_EXIT_DEBUG here.
6111 */
6112 return kvm_fast_pio_out(vcpu, size, port) && ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006113}
6114
Ingo Molnar102d8322007-02-19 14:37:47 +02006115static void
6116vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6117{
6118 /*
6119 * Patch in the VMCALL instruction:
6120 */
6121 hypercall[0] = 0x0f;
6122 hypercall[1] = 0x01;
6123 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006124}
6125
Guo Chao0fa06072012-06-28 15:16:19 +08006126/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006127static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6128{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006129 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006130 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6131 unsigned long orig_val = val;
6132
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006133 /*
6134 * We get here when L2 changed cr0 in a way that did not change
6135 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006136 * but did change L0 shadowed bits. So we first calculate the
6137 * effective cr0 value that L1 would like to write into the
6138 * hardware. It consists of the L2-owned bits from the new
6139 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006140 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006141 val = (val & ~vmcs12->cr0_guest_host_mask) |
6142 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6143
David Matlack38991522016-11-29 18:14:08 -08006144 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006145 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006146
6147 if (kvm_set_cr0(vcpu, val))
6148 return 1;
6149 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006150 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006151 } else {
6152 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006153 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006154 return 1;
David Matlack38991522016-11-29 18:14:08 -08006155
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006156 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006157 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006158}
6159
6160static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6161{
6162 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006163 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6164 unsigned long orig_val = val;
6165
6166 /* analogously to handle_set_cr0 */
6167 val = (val & ~vmcs12->cr4_guest_host_mask) |
6168 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6169 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006170 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006171 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006172 return 0;
6173 } else
6174 return kvm_set_cr4(vcpu, val);
6175}
6176
Paolo Bonzini0367f202016-07-12 10:44:55 +02006177static int handle_desc(struct kvm_vcpu *vcpu)
6178{
6179 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6180 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6181}
6182
Avi Kivity851ba692009-08-24 11:10:17 +03006183static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006184{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006185 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006186 int cr;
6187 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006188 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006189 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006190
He, Qingbfdaab02007-09-12 14:18:28 +08006191 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006192 cr = exit_qualification & 15;
6193 reg = (exit_qualification >> 8) & 15;
6194 switch ((exit_qualification >> 4) & 3) {
6195 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006196 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006197 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006198 switch (cr) {
6199 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006200 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006201 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006202 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006203 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006204 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006205 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006206 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006207 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006208 case 8: {
6209 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006210 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006211 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006212 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006213 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006214 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006215 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006216 return ret;
6217 /*
6218 * TODO: we might be squashing a
6219 * KVM_GUESTDBG_SINGLESTEP-triggered
6220 * KVM_EXIT_DEBUG here.
6221 */
Avi Kivity851ba692009-08-24 11:10:17 +03006222 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006223 return 0;
6224 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006225 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006226 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006227 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006228 WARN_ONCE(1, "Guest should always own CR0.TS");
6229 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006230 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006231 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006232 case 1: /*mov from cr*/
6233 switch (cr) {
6234 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006235 val = kvm_read_cr3(vcpu);
6236 kvm_register_write(vcpu, reg, val);
6237 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006238 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006239 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006240 val = kvm_get_cr8(vcpu);
6241 kvm_register_write(vcpu, reg, val);
6242 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006243 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006244 }
6245 break;
6246 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006247 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006248 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006249 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006250
Kyle Huey6affcbe2016-11-29 12:40:40 -08006251 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006252 default:
6253 break;
6254 }
Avi Kivity851ba692009-08-24 11:10:17 +03006255 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006256 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006257 (int)(exit_qualification >> 4) & 3, cr);
6258 return 0;
6259}
6260
Avi Kivity851ba692009-08-24 11:10:17 +03006261static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006262{
He, Qingbfdaab02007-09-12 14:18:28 +08006263 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006264 int dr, dr7, reg;
6265
6266 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6267 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6268
6269 /* First, if DR does not exist, trigger UD */
6270 if (!kvm_require_dr(vcpu, dr))
6271 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006272
Jan Kiszkaf2483412010-01-20 18:20:20 +01006273 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006274 if (!kvm_require_cpl(vcpu, 0))
6275 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006276 dr7 = vmcs_readl(GUEST_DR7);
6277 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006278 /*
6279 * As the vm-exit takes precedence over the debug trap, we
6280 * need to emulate the latter, either for the host or the
6281 * guest debugging itself.
6282 */
6283 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006284 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006285 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006286 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006287 vcpu->run->debug.arch.exception = DB_VECTOR;
6288 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006289 return 0;
6290 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006291 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006292 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006293 kvm_queue_exception(vcpu, DB_VECTOR);
6294 return 1;
6295 }
6296 }
6297
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006298 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006299 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6300 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006301
6302 /*
6303 * No more DR vmexits; force a reload of the debug registers
6304 * and reenter on this instruction. The next vmexit will
6305 * retrieve the full state of the debug registers.
6306 */
6307 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6308 return 1;
6309 }
6310
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006311 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6312 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006313 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006314
6315 if (kvm_get_dr(vcpu, dr, &val))
6316 return 1;
6317 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006318 } else
Nadav Amit57773922014-06-18 17:19:23 +03006319 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006320 return 1;
6321
Kyle Huey6affcbe2016-11-29 12:40:40 -08006322 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006323}
6324
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006325static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6326{
6327 return vcpu->arch.dr6;
6328}
6329
6330static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6331{
6332}
6333
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006334static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6335{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006336 get_debugreg(vcpu->arch.db[0], 0);
6337 get_debugreg(vcpu->arch.db[1], 1);
6338 get_debugreg(vcpu->arch.db[2], 2);
6339 get_debugreg(vcpu->arch.db[3], 3);
6340 get_debugreg(vcpu->arch.dr6, 6);
6341 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6342
6343 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006344 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006345}
6346
Gleb Natapov020df072010-04-13 10:05:23 +03006347static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6348{
6349 vmcs_writel(GUEST_DR7, val);
6350}
6351
Avi Kivity851ba692009-08-24 11:10:17 +03006352static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006353{
Kyle Huey6a908b62016-11-29 12:40:37 -08006354 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006355}
6356
Avi Kivity851ba692009-08-24 11:10:17 +03006357static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006358{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006359 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006360 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006361
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006362 msr_info.index = ecx;
6363 msr_info.host_initiated = false;
6364 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006365 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006366 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006367 return 1;
6368 }
6369
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006370 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006371
Avi Kivity6aa8b732006-12-10 02:21:36 -08006372 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006373 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6374 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006375 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006376}
6377
Avi Kivity851ba692009-08-24 11:10:17 +03006378static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006379{
Will Auld8fe8ab42012-11-29 12:42:12 -08006380 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006381 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6382 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6383 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006384
Will Auld8fe8ab42012-11-29 12:42:12 -08006385 msr.data = data;
6386 msr.index = ecx;
6387 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006388 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006389 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006390 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006391 return 1;
6392 }
6393
Avi Kivity59200272010-01-25 19:47:02 +02006394 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006395 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006396}
6397
Avi Kivity851ba692009-08-24 11:10:17 +03006398static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006399{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01006400 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006401 return 1;
6402}
6403
Avi Kivity851ba692009-08-24 11:10:17 +03006404static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006405{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006406 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6407 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006408
Avi Kivity3842d132010-07-27 12:30:24 +03006409 kvm_make_request(KVM_REQ_EVENT, vcpu);
6410
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006411 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006412 return 1;
6413}
6414
Avi Kivity851ba692009-08-24 11:10:17 +03006415static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006416{
Avi Kivityd3bef152007-06-05 15:53:05 +03006417 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006418}
6419
Avi Kivity851ba692009-08-24 11:10:17 +03006420static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006421{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006422 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006423}
6424
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006425static int handle_invd(struct kvm_vcpu *vcpu)
6426{
Andre Przywara51d8b662010-12-21 11:12:02 +01006427 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006428}
6429
Avi Kivity851ba692009-08-24 11:10:17 +03006430static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006431{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006432 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006433
6434 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006435 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006436}
6437
Avi Kivityfee84b02011-11-10 14:57:25 +02006438static int handle_rdpmc(struct kvm_vcpu *vcpu)
6439{
6440 int err;
6441
6442 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006443 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02006444}
6445
Avi Kivity851ba692009-08-24 11:10:17 +03006446static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006447{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006448 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006449}
6450
Dexuan Cui2acf9232010-06-10 11:27:12 +08006451static int handle_xsetbv(struct kvm_vcpu *vcpu)
6452{
6453 u64 new_bv = kvm_read_edx_eax(vcpu);
6454 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6455
6456 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006457 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08006458 return 1;
6459}
6460
Wanpeng Lif53cd632014-12-02 19:14:58 +08006461static int handle_xsaves(struct kvm_vcpu *vcpu)
6462{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006463 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006464 WARN(1, "this should never happen\n");
6465 return 1;
6466}
6467
6468static int handle_xrstors(struct kvm_vcpu *vcpu)
6469{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006470 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006471 WARN(1, "this should never happen\n");
6472 return 1;
6473}
6474
Avi Kivity851ba692009-08-24 11:10:17 +03006475static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006476{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006477 if (likely(fasteoi)) {
6478 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6479 int access_type, offset;
6480
6481 access_type = exit_qualification & APIC_ACCESS_TYPE;
6482 offset = exit_qualification & APIC_ACCESS_OFFSET;
6483 /*
6484 * Sane guest uses MOV to write EOI, with written value
6485 * not cared. So make a short-circuit here by avoiding
6486 * heavy instruction emulation.
6487 */
6488 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6489 (offset == APIC_EOI)) {
6490 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006491 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03006492 }
6493 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006494 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006495}
6496
Yang Zhangc7c9c562013-01-25 10:18:51 +08006497static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6498{
6499 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6500 int vector = exit_qualification & 0xff;
6501
6502 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6503 kvm_apic_set_eoi_accelerated(vcpu, vector);
6504 return 1;
6505}
6506
Yang Zhang83d4c282013-01-25 10:18:49 +08006507static int handle_apic_write(struct kvm_vcpu *vcpu)
6508{
6509 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6510 u32 offset = exit_qualification & 0xfff;
6511
6512 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6513 kvm_apic_write_nodecode(vcpu, offset);
6514 return 1;
6515}
6516
Avi Kivity851ba692009-08-24 11:10:17 +03006517static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006518{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006519 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006520 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006521 bool has_error_code = false;
6522 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006523 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006524 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006525
6526 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006527 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006528 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006529
6530 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6531
6532 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006533 if (reason == TASK_SWITCH_GATE && idt_v) {
6534 switch (type) {
6535 case INTR_TYPE_NMI_INTR:
6536 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006537 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006538 break;
6539 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006540 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006541 kvm_clear_interrupt_queue(vcpu);
6542 break;
6543 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006544 if (vmx->idt_vectoring_info &
6545 VECTORING_INFO_DELIVER_CODE_MASK) {
6546 has_error_code = true;
6547 error_code =
6548 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6549 }
6550 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006551 case INTR_TYPE_SOFT_EXCEPTION:
6552 kvm_clear_exception_queue(vcpu);
6553 break;
6554 default:
6555 break;
6556 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006557 }
Izik Eidus37817f22008-03-24 23:14:53 +02006558 tss_selector = exit_qualification;
6559
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006560 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6561 type != INTR_TYPE_EXT_INTR &&
6562 type != INTR_TYPE_NMI_INTR))
6563 skip_emulated_instruction(vcpu);
6564
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006565 if (kvm_task_switch(vcpu, tss_selector,
6566 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6567 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006568 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6569 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6570 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006571 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006572 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006573
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006574 /*
6575 * TODO: What about debug traps on tss switch?
6576 * Are we supposed to inject them and update dr6?
6577 */
6578
6579 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006580}
6581
Avi Kivity851ba692009-08-24 11:10:17 +03006582static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006583{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006584 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006585 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01006586 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006587
Sheng Yangf9c617f2009-03-25 10:08:52 +08006588 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006589
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006590 /*
6591 * EPT violation happened while executing iret from NMI,
6592 * "blocked by NMI" bit has to be set before next VM entry.
6593 * There are errata that may cause this bit to not be set:
6594 * AAK134, BY25.
6595 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006596 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006597 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006598 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006599 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6600
Sheng Yang14394422008-04-28 12:24:45 +08006601 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006602 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006603
Junaid Shahid27959a42016-12-06 16:46:10 -08006604 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006605 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08006606 ? PFERR_USER_MASK : 0;
6607 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006608 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08006609 ? PFERR_WRITE_MASK : 0;
6610 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006611 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08006612 ? PFERR_FETCH_MASK : 0;
6613 /* ept page table entry is present? */
6614 error_code |= (exit_qualification &
6615 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6616 EPT_VIOLATION_EXECUTABLE))
6617 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006618
Paolo Bonzinieebed242016-11-28 14:39:58 +01006619 error_code |= (exit_qualification & 0x100) != 0 ?
6620 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006621
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006622 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006623 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006624}
6625
Avi Kivity851ba692009-08-24 11:10:17 +03006626static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006627{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006628 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006629 gpa_t gpa;
6630
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02006631 /*
6632 * A nested guest cannot optimize MMIO vmexits, because we have an
6633 * nGPA here instead of the required GPA.
6634 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006635 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02006636 if (!is_guest_mode(vcpu) &&
6637 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08006638 trace_kvm_fast_mmio(gpa);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006639 return kvm_skip_emulated_instruction(vcpu);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006640 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006641
Paolo Bonzinie08d26f2017-08-17 18:36:56 +02006642 ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
6643 if (ret >= 0)
6644 return ret;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006645
6646 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006647 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006648
Avi Kivity851ba692009-08-24 11:10:17 +03006649 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6650 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006651
6652 return 0;
6653}
6654
Avi Kivity851ba692009-08-24 11:10:17 +03006655static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006656{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006657 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01006658 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6659 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08006660 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006661 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006662
6663 return 1;
6664}
6665
Mohammed Gamal80ced182009-09-01 12:48:18 +02006666static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006667{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006668 struct vcpu_vmx *vmx = to_vmx(vcpu);
6669 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006670 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006671 u32 cpu_exec_ctrl;
6672 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006673 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006674
6675 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6676 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006677
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006678 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006679 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006680 return handle_interrupt_window(&vmx->vcpu);
6681
Radim Krčmář72875d82017-04-26 22:32:19 +02006682 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03006683 return 1;
6684
Liran Alon9b8ae632017-11-05 16:56:34 +02006685 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006686
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006687 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006688 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006689 ret = 0;
6690 goto out;
6691 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006692
Avi Kivityde5f70e2012-06-12 20:22:28 +03006693 if (err != EMULATE_DONE) {
6694 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6695 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6696 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006697 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03006698 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006699
Gleb Natapov8d76c492013-05-08 18:38:44 +03006700 if (vcpu->arch.halt_request) {
6701 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006702 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006703 goto out;
6704 }
6705
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006706 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006707 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006708 if (need_resched())
6709 schedule();
6710 }
6711
Mohammed Gamal80ced182009-09-01 12:48:18 +02006712out:
6713 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006714}
6715
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006716static int __grow_ple_window(int val)
6717{
6718 if (ple_window_grow < 1)
6719 return ple_window;
6720
6721 val = min(val, ple_window_actual_max);
6722
6723 if (ple_window_grow < ple_window)
6724 val *= ple_window_grow;
6725 else
6726 val += ple_window_grow;
6727
6728 return val;
6729}
6730
6731static int __shrink_ple_window(int val, int modifier, int minimum)
6732{
6733 if (modifier < 1)
6734 return ple_window;
6735
6736 if (modifier < ple_window)
6737 val /= modifier;
6738 else
6739 val -= modifier;
6740
6741 return max(val, minimum);
6742}
6743
6744static void grow_ple_window(struct kvm_vcpu *vcpu)
6745{
6746 struct vcpu_vmx *vmx = to_vmx(vcpu);
6747 int old = vmx->ple_window;
6748
6749 vmx->ple_window = __grow_ple_window(old);
6750
6751 if (vmx->ple_window != old)
6752 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006753
6754 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006755}
6756
6757static void shrink_ple_window(struct kvm_vcpu *vcpu)
6758{
6759 struct vcpu_vmx *vmx = to_vmx(vcpu);
6760 int old = vmx->ple_window;
6761
6762 vmx->ple_window = __shrink_ple_window(old,
6763 ple_window_shrink, ple_window);
6764
6765 if (vmx->ple_window != old)
6766 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006767
6768 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006769}
6770
6771/*
6772 * ple_window_actual_max is computed to be one grow_ple_window() below
6773 * ple_window_max. (See __grow_ple_window for the reason.)
6774 * This prevents overflows, because ple_window_max is int.
6775 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6776 * this process.
6777 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6778 */
6779static void update_ple_window_actual_max(void)
6780{
6781 ple_window_actual_max =
6782 __shrink_ple_window(max(ple_window_max, ple_window),
6783 ple_window_grow, INT_MIN);
6784}
6785
Feng Wubf9f6ac2015-09-18 22:29:55 +08006786/*
6787 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6788 */
6789static void wakeup_handler(void)
6790{
6791 struct kvm_vcpu *vcpu;
6792 int cpu = smp_processor_id();
6793
6794 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6795 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6796 blocked_vcpu_list) {
6797 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6798
6799 if (pi_test_on(pi_desc) == 1)
6800 kvm_vcpu_kick(vcpu);
6801 }
6802 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6803}
6804
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006805void vmx_enable_tdp(void)
6806{
6807 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
6808 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
6809 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
6810 0ull, VMX_EPT_EXECUTABLE_MASK,
6811 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05006812 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006813
6814 ept_set_mmio_spte_mask();
6815 kvm_enable_tdp();
6816}
6817
Tiejun Chenf2c76482014-10-28 10:14:47 +08006818static __init int hardware_setup(void)
6819{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006820 int r = -ENOMEM, i, msr;
6821
6822 rdmsrl_safe(MSR_EFER, &host_efer);
6823
6824 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6825 kvm_define_shared_msr(i, vmx_msr_index[i]);
6826
Radim Krčmář23611332016-09-29 22:41:33 +02006827 for (i = 0; i < VMX_BITMAP_NR; i++) {
6828 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
6829 if (!vmx_bitmap[i])
6830 goto out;
6831 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006832
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006833 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6834 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6835
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006836 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
6837 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
6838
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006839 if (setup_vmcs_config(&vmcs_config) < 0) {
6840 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02006841 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006842 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006843
6844 if (boot_cpu_has(X86_FEATURE_NX))
6845 kvm_enable_efer_bits(EFER_NX);
6846
Wanpeng Li08d839c2017-03-23 05:30:08 -07006847 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6848 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006849 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07006850
Tiejun Chenf2c76482014-10-28 10:14:47 +08006851 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02006852 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02006853 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07006854 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006855 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006856
Wanpeng Lifce6ac42017-05-11 02:58:56 -07006857 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006858 enable_ept_ad_bits = 0;
6859
Wanpeng Li8ad81822017-10-09 15:51:53 -07006860 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006861 enable_unrestricted_guest = 0;
6862
Paolo Bonziniad15a292015-01-30 16:18:49 +01006863 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006864 flexpriority_enabled = 0;
6865
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006866 if (!cpu_has_virtual_nmis())
6867 enable_vnmi = 0;
6868
Paolo Bonziniad15a292015-01-30 16:18:49 +01006869 /*
6870 * set_apic_access_page_addr() is used to reload apic access
6871 * page upon invalidation. No need to do anything if not
6872 * using the APIC_ACCESS_ADDR VMCS field.
6873 */
6874 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006875 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006876
6877 if (!cpu_has_vmx_tpr_shadow())
6878 kvm_x86_ops->update_cr8_intercept = NULL;
6879
6880 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6881 kvm_disable_largepages();
6882
Wanpeng Li0f107682017-09-28 18:06:24 -07006883 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08006884 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07006885 ple_window = 0;
6886 ple_window_grow = 0;
6887 ple_window_max = 0;
6888 ple_window_shrink = 0;
6889 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006890
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006891 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08006892 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006893 kvm_x86_ops->sync_pir_to_irr = NULL;
6894 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006895
Haozhong Zhang64903d62015-10-20 15:39:09 +08006896 if (cpu_has_vmx_tsc_scaling()) {
6897 kvm_has_tsc_control = true;
6898 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6899 kvm_tsc_scaling_ratio_frac_bits = 48;
6900 }
6901
Tiejun Chenbaa03522014-12-23 16:21:11 +08006902 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
6903 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
6904 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
6905 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
6906 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
6907 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006908
Wanpeng Lic63e4562016-09-23 19:17:16 +08006909 memcpy(vmx_msr_bitmap_legacy_x2apic_apicv,
6910 vmx_msr_bitmap_legacy, PAGE_SIZE);
6911 memcpy(vmx_msr_bitmap_longmode_x2apic_apicv,
6912 vmx_msr_bitmap_longmode, PAGE_SIZE);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006913 memcpy(vmx_msr_bitmap_legacy_x2apic,
6914 vmx_msr_bitmap_legacy, PAGE_SIZE);
6915 memcpy(vmx_msr_bitmap_longmode_x2apic,
6916 vmx_msr_bitmap_longmode, PAGE_SIZE);
6917
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006918 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6919
Radim Krčmář40d83382016-09-29 22:41:31 +02006920 for (msr = 0x800; msr <= 0x8ff; msr++) {
6921 if (msr == 0x839 /* TMCCT */)
6922 continue;
Radim Krčmář2e69f862016-09-29 22:41:32 +02006923 vmx_disable_intercept_msr_x2apic(msr, MSR_TYPE_R, true);
Radim Krčmář40d83382016-09-29 22:41:31 +02006924 }
Tiejun Chenbaa03522014-12-23 16:21:11 +08006925
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006926 /*
Radim Krčmář2e69f862016-09-29 22:41:32 +02006927 * TPR reads and writes can be virtualized even if virtual interrupt
6928 * delivery is not in use.
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006929 */
Radim Krčmář2e69f862016-09-29 22:41:32 +02006930 vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_W, true);
6931 vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_R | MSR_TYPE_W, false);
6932
Roman Kagan3ce424e2016-05-18 17:48:20 +03006933 /* EOI */
Radim Krčmář2e69f862016-09-29 22:41:32 +02006934 vmx_disable_intercept_msr_x2apic(0x80b, MSR_TYPE_W, true);
Roman Kagan3ce424e2016-05-18 17:48:20 +03006935 /* SELF-IPI */
Radim Krčmář2e69f862016-09-29 22:41:32 +02006936 vmx_disable_intercept_msr_x2apic(0x83f, MSR_TYPE_W, true);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006937
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006938 if (enable_ept)
6939 vmx_enable_tdp();
6940 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08006941 kvm_disable_tdp();
6942
6943 update_ple_window_actual_max();
6944
Kai Huang843e4332015-01-28 10:54:28 +08006945 /*
6946 * Only enable PML when hardware supports PML feature, and both EPT
6947 * and EPT A/D bit features are enabled -- PML depends on them to work.
6948 */
6949 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6950 enable_pml = 0;
6951
6952 if (!enable_pml) {
6953 kvm_x86_ops->slot_enable_log_dirty = NULL;
6954 kvm_x86_ops->slot_disable_log_dirty = NULL;
6955 kvm_x86_ops->flush_log_dirty = NULL;
6956 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6957 }
6958
Yunhong Jiang64672c92016-06-13 14:19:59 -07006959 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6960 u64 vmx_msr;
6961
6962 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6963 cpu_preemption_timer_multi =
6964 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6965 } else {
6966 kvm_x86_ops->set_hv_timer = NULL;
6967 kvm_x86_ops->cancel_hv_timer = NULL;
6968 }
6969
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01006970 if (!cpu_has_vmx_shadow_vmcs())
6971 enable_shadow_vmcs = 0;
6972 if (enable_shadow_vmcs)
6973 init_vmcs_shadow_fields();
6974
Feng Wubf9f6ac2015-09-18 22:29:55 +08006975 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6976
Ashok Rajc45dcc72016-06-22 14:59:56 +08006977 kvm_mce_cap_supported |= MCG_LMCE_P;
6978
Tiejun Chenf2c76482014-10-28 10:14:47 +08006979 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006980
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006981out:
Radim Krčmář23611332016-09-29 22:41:33 +02006982 for (i = 0; i < VMX_BITMAP_NR; i++)
6983 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006984
6985 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006986}
6987
6988static __exit void hardware_unsetup(void)
6989{
Radim Krčmář23611332016-09-29 22:41:33 +02006990 int i;
6991
6992 for (i = 0; i < VMX_BITMAP_NR; i++)
6993 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006994
Tiejun Chenf2c76482014-10-28 10:14:47 +08006995 free_kvm_area();
6996}
6997
Avi Kivity6aa8b732006-12-10 02:21:36 -08006998/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006999 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7000 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7001 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007002static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007003{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007004 if (ple_gap)
7005 grow_ple_window(vcpu);
7006
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007007 /*
7008 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7009 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7010 * never set PAUSE_EXITING and just set PLE if supported,
7011 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7012 */
7013 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007014 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007015}
7016
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007017static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007018{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007019 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007020}
7021
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007022static int handle_mwait(struct kvm_vcpu *vcpu)
7023{
7024 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7025 return handle_nop(vcpu);
7026}
7027
Jim Mattson45ec3682017-08-23 16:32:04 -07007028static int handle_invalid_op(struct kvm_vcpu *vcpu)
7029{
7030 kvm_queue_exception(vcpu, UD_VECTOR);
7031 return 1;
7032}
7033
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007034static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7035{
7036 return 1;
7037}
7038
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007039static int handle_monitor(struct kvm_vcpu *vcpu)
7040{
7041 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7042 return handle_nop(vcpu);
7043}
7044
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007045/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007046 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7047 * set the success or error code of an emulated VMX instruction, as specified
7048 * by Vol 2B, VMX Instruction Reference, "Conventions".
7049 */
7050static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7051{
7052 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7053 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7054 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7055}
7056
7057static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7058{
7059 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7060 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7061 X86_EFLAGS_SF | X86_EFLAGS_OF))
7062 | X86_EFLAGS_CF);
7063}
7064
Abel Gordon145c28d2013-04-18 14:36:55 +03007065static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007066 u32 vm_instruction_error)
7067{
7068 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7069 /*
7070 * failValid writes the error number to the current VMCS, which
7071 * can't be done there isn't a current VMCS.
7072 */
7073 nested_vmx_failInvalid(vcpu);
7074 return;
7075 }
7076 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7077 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7078 X86_EFLAGS_SF | X86_EFLAGS_OF))
7079 | X86_EFLAGS_ZF);
7080 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7081 /*
7082 * We don't need to force a shadow sync because
7083 * VM_INSTRUCTION_ERROR is not shadowed
7084 */
7085}
Abel Gordon145c28d2013-04-18 14:36:55 +03007086
Wincy Vanff651cb2014-12-11 08:52:58 +03007087static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7088{
7089 /* TODO: not to reset guest simply here. */
7090 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007091 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007092}
7093
Jan Kiszkaf41245002014-03-07 20:03:13 +01007094static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7095{
7096 struct vcpu_vmx *vmx =
7097 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7098
7099 vmx->nested.preemption_timer_expired = true;
7100 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7101 kvm_vcpu_kick(&vmx->vcpu);
7102
7103 return HRTIMER_NORESTART;
7104}
7105
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007106/*
Bandan Das19677e32014-05-06 02:19:15 -04007107 * Decode the memory-address operand of a vmx instruction, as recorded on an
7108 * exit caused by such an instruction (run by a guest hypervisor).
7109 * On success, returns 0. When the operand is invalid, returns 1 and throws
7110 * #UD or #GP.
7111 */
7112static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7113 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007114 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007115{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007116 gva_t off;
7117 bool exn;
7118 struct kvm_segment s;
7119
Bandan Das19677e32014-05-06 02:19:15 -04007120 /*
7121 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7122 * Execution", on an exit, vmx_instruction_info holds most of the
7123 * addressing components of the operand. Only the displacement part
7124 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7125 * For how an actual address is calculated from all these components,
7126 * refer to Vol. 1, "Operand Addressing".
7127 */
7128 int scaling = vmx_instruction_info & 3;
7129 int addr_size = (vmx_instruction_info >> 7) & 7;
7130 bool is_reg = vmx_instruction_info & (1u << 10);
7131 int seg_reg = (vmx_instruction_info >> 15) & 7;
7132 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7133 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7134 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7135 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7136
7137 if (is_reg) {
7138 kvm_queue_exception(vcpu, UD_VECTOR);
7139 return 1;
7140 }
7141
7142 /* Addr = segment_base + offset */
7143 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007144 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007145 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007146 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007147 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007148 off += kvm_register_read(vcpu, index_reg)<<scaling;
7149 vmx_get_segment(vcpu, &s, seg_reg);
7150 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007151
7152 if (addr_size == 1) /* 32 bit */
7153 *ret &= 0xffffffff;
7154
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007155 /* Checks for #GP/#SS exceptions. */
7156 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007157 if (is_long_mode(vcpu)) {
7158 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7159 * non-canonical form. This is the only check on the memory
7160 * destination for long mode!
7161 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007162 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007163 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007164 /* Protected mode: apply checks for segment validity in the
7165 * following order:
7166 * - segment type check (#GP(0) may be thrown)
7167 * - usability check (#GP(0)/#SS(0))
7168 * - limit check (#GP(0)/#SS(0))
7169 */
7170 if (wr)
7171 /* #GP(0) if the destination operand is located in a
7172 * read-only data segment or any code segment.
7173 */
7174 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7175 else
7176 /* #GP(0) if the source operand is located in an
7177 * execute-only code segment
7178 */
7179 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007180 if (exn) {
7181 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7182 return 1;
7183 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007184 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7185 */
7186 exn = (s.unusable != 0);
7187 /* Protected mode: #GP(0)/#SS(0) if the memory
7188 * operand is outside the segment limit.
7189 */
7190 exn = exn || (off + sizeof(u64) > s.limit);
7191 }
7192 if (exn) {
7193 kvm_queue_exception_e(vcpu,
7194 seg_reg == VCPU_SREG_SS ?
7195 SS_VECTOR : GP_VECTOR,
7196 0);
7197 return 1;
7198 }
7199
Bandan Das19677e32014-05-06 02:19:15 -04007200 return 0;
7201}
7202
Radim Krčmářcbf71272017-05-19 15:48:51 +02007203static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007204{
7205 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007206 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007207
7208 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007209 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007210 return 1;
7211
Radim Krčmářcbf71272017-05-19 15:48:51 +02007212 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7213 sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007214 kvm_inject_page_fault(vcpu, &e);
7215 return 1;
7216 }
7217
Bandan Das3573e222014-05-06 02:19:16 -04007218 return 0;
7219}
7220
Jim Mattsone29acc52016-11-30 12:03:43 -08007221static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7222{
7223 struct vcpu_vmx *vmx = to_vmx(vcpu);
7224 struct vmcs *shadow_vmcs;
7225
Jim Mattson00647b42017-11-27 17:22:25 -06007226 vmx->nested.vmcs02.vmcs = alloc_vmcs();
7227 vmx->nested.vmcs02.shadow_vmcs = NULL;
7228 if (!vmx->nested.vmcs02.vmcs)
7229 goto out_vmcs02;
7230 loaded_vmcs_init(&vmx->nested.vmcs02);
7231
Jim Mattsone29acc52016-11-30 12:03:43 -08007232 if (cpu_has_vmx_msr_bitmap()) {
7233 vmx->nested.msr_bitmap =
7234 (unsigned long *)__get_free_page(GFP_KERNEL);
7235 if (!vmx->nested.msr_bitmap)
7236 goto out_msr_bitmap;
7237 }
7238
7239 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7240 if (!vmx->nested.cached_vmcs12)
7241 goto out_cached_vmcs12;
7242
7243 if (enable_shadow_vmcs) {
7244 shadow_vmcs = alloc_vmcs();
7245 if (!shadow_vmcs)
7246 goto out_shadow_vmcs;
7247 /* mark vmcs as shadow */
7248 shadow_vmcs->revision_id |= (1u << 31);
7249 /* init shadow vmcs */
7250 vmcs_clear(shadow_vmcs);
7251 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7252 }
7253
Jim Mattsone29acc52016-11-30 12:03:43 -08007254 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7255 HRTIMER_MODE_REL_PINNED);
7256 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7257
7258 vmx->nested.vmxon = true;
7259 return 0;
7260
7261out_shadow_vmcs:
7262 kfree(vmx->nested.cached_vmcs12);
7263
7264out_cached_vmcs12:
7265 free_page((unsigned long)vmx->nested.msr_bitmap);
7266
7267out_msr_bitmap:
Mark Kanda276c7962017-11-27 17:22:26 -06007268 vmx_nested_free_vmcs02(vmx);
Jim Mattson00647b42017-11-27 17:22:25 -06007269
7270out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007271 return -ENOMEM;
7272}
7273
Bandan Das3573e222014-05-06 02:19:16 -04007274/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007275 * Emulate the VMXON instruction.
7276 * Currently, we just remember that VMX is active, and do not save or even
7277 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7278 * do not currently need to store anything in that guest-allocated memory
7279 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7280 * argument is different from the VMXON pointer (which the spec says they do).
7281 */
7282static int handle_vmon(struct kvm_vcpu *vcpu)
7283{
Jim Mattsone29acc52016-11-30 12:03:43 -08007284 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007285 gpa_t vmptr;
7286 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007287 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007288 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7289 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007290
Jim Mattson70f3aac2017-04-26 08:53:46 -07007291 /*
7292 * The Intel VMX Instruction Reference lists a bunch of bits that are
7293 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7294 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7295 * Otherwise, we should fail with #UD. But most faulting conditions
7296 * have already been checked by hardware, prior to the VM-exit for
7297 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7298 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007299 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007300 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007301 kvm_queue_exception(vcpu, UD_VECTOR);
7302 return 1;
7303 }
7304
Abel Gordon145c28d2013-04-18 14:36:55 +03007305 if (vmx->nested.vmxon) {
7306 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007307 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007308 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007309
Haozhong Zhang3b840802016-06-22 14:59:54 +08007310 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007311 != VMXON_NEEDED_FEATURES) {
7312 kvm_inject_gp(vcpu, 0);
7313 return 1;
7314 }
7315
Radim Krčmářcbf71272017-05-19 15:48:51 +02007316 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007317 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007318
7319 /*
7320 * SDM 3: 24.11.5
7321 * The first 4 bytes of VMXON region contain the supported
7322 * VMCS revision identifier
7323 *
7324 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7325 * which replaces physical address width with 32
7326 */
7327 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7328 nested_vmx_failInvalid(vcpu);
7329 return kvm_skip_emulated_instruction(vcpu);
7330 }
7331
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007332 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7333 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007334 nested_vmx_failInvalid(vcpu);
7335 return kvm_skip_emulated_instruction(vcpu);
7336 }
7337 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7338 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007339 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007340 nested_vmx_failInvalid(vcpu);
7341 return kvm_skip_emulated_instruction(vcpu);
7342 }
7343 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007344 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007345
7346 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007347 ret = enter_vmx_operation(vcpu);
7348 if (ret)
7349 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007350
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007351 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007352 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007353}
7354
7355/*
7356 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7357 * for running VMX instructions (except VMXON, whose prerequisites are
7358 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07007359 * Note that many of these exceptions have priority over VM exits, so they
7360 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007361 */
7362static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7363{
Jim Mattson70f3aac2017-04-26 08:53:46 -07007364 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007365 kvm_queue_exception(vcpu, UD_VECTOR);
7366 return 0;
7367 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007368 return 1;
7369}
7370
David Matlack8ca44e82017-08-01 14:00:39 -07007371static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7372{
7373 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7374 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7375}
7376
Abel Gordone7953d72013-04-18 14:37:55 +03007377static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7378{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007379 if (vmx->nested.current_vmptr == -1ull)
7380 return;
7381
Abel Gordon012f83c2013-04-18 14:39:25 +03007382 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007383 /* copy to memory all shadowed fields in case
7384 they were modified */
7385 copy_shadow_to_vmcs12(vmx);
7386 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07007387 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03007388 }
Wincy Van705699a2015-02-03 23:58:17 +08007389 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007390
7391 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007392 kvm_vcpu_write_guest_page(&vmx->vcpu,
7393 vmx->nested.current_vmptr >> PAGE_SHIFT,
7394 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07007395
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007396 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03007397}
7398
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007399/*
7400 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7401 * just stops using VMX.
7402 */
7403static void free_nested(struct vcpu_vmx *vmx)
7404{
Wanpeng Lib7455822017-11-22 14:04:00 -08007405 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007406 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007407
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007408 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08007409 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007410 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07007411 vmx->nested.posted_intr_nv = -1;
7412 vmx->nested.current_vmptr = -1ull;
Radim Krčmářd048c092016-08-08 20:16:22 +02007413 if (vmx->nested.msr_bitmap) {
7414 free_page((unsigned long)vmx->nested.msr_bitmap);
7415 vmx->nested.msr_bitmap = NULL;
7416 }
Jim Mattson355f4fb2016-10-28 08:29:39 -07007417 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07007418 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007419 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7420 free_vmcs(vmx->vmcs01.shadow_vmcs);
7421 vmx->vmcs01.shadow_vmcs = NULL;
7422 }
David Matlack4f2777b2016-07-13 17:16:37 -07007423 kfree(vmx->nested.cached_vmcs12);
Jim Mattson00647b42017-11-27 17:22:25 -06007424 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007425 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007426 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007427 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007428 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007429 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007430 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007431 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007432 }
Wincy Van705699a2015-02-03 23:58:17 +08007433 if (vmx->nested.pi_desc_page) {
7434 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007435 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08007436 vmx->nested.pi_desc_page = NULL;
7437 vmx->nested.pi_desc = NULL;
7438 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007439
Mark Kanda276c7962017-11-27 17:22:26 -06007440 vmx_nested_free_vmcs02(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007441}
7442
7443/* Emulate the VMXOFF instruction */
7444static int handle_vmoff(struct kvm_vcpu *vcpu)
7445{
7446 if (!nested_vmx_check_permission(vcpu))
7447 return 1;
7448 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007449 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007450 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007451}
7452
Nadav Har'El27d6c862011-05-25 23:06:59 +03007453/* Emulate the VMCLEAR instruction */
7454static int handle_vmclear(struct kvm_vcpu *vcpu)
7455{
7456 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08007457 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007458 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007459
7460 if (!nested_vmx_check_permission(vcpu))
7461 return 1;
7462
Radim Krčmářcbf71272017-05-19 15:48:51 +02007463 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007464 return 1;
7465
Radim Krčmářcbf71272017-05-19 15:48:51 +02007466 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7467 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7468 return kvm_skip_emulated_instruction(vcpu);
7469 }
7470
7471 if (vmptr == vmx->nested.vmxon_ptr) {
7472 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7473 return kvm_skip_emulated_instruction(vcpu);
7474 }
7475
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007476 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007477 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007478
Jim Mattson587d7e722017-03-02 12:41:48 -08007479 kvm_vcpu_write_guest(vcpu,
7480 vmptr + offsetof(struct vmcs12, launch_state),
7481 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007482
Nadav Har'El27d6c862011-05-25 23:06:59 +03007483 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007484 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007485}
7486
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007487static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7488
7489/* Emulate the VMLAUNCH instruction */
7490static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7491{
7492 return nested_vmx_run(vcpu, true);
7493}
7494
7495/* Emulate the VMRESUME instruction */
7496static int handle_vmresume(struct kvm_vcpu *vcpu)
7497{
7498
7499 return nested_vmx_run(vcpu, false);
7500}
7501
Nadav Har'El49f705c2011-05-25 23:08:30 +03007502/*
7503 * Read a vmcs12 field. Since these can have varying lengths and we return
7504 * one type, we chose the biggest type (u64) and zero-extend the return value
7505 * to that size. Note that the caller, handle_vmread, might need to use only
7506 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7507 * 64-bit fields are to be returned).
7508 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007509static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7510 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007511{
7512 short offset = vmcs_field_to_offset(field);
7513 char *p;
7514
7515 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007516 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007517
7518 p = ((char *)(get_vmcs12(vcpu))) + offset;
7519
Jim Mattsond37f4262017-12-22 12:12:16 -08007520 switch (vmcs_field_width(field)) {
7521 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007522 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007523 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007524 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007525 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007526 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007527 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007528 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007529 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007530 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007531 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007532 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007533 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007534 WARN_ON(1);
7535 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007536 }
7537}
7538
Abel Gordon20b97fe2013-04-18 14:36:25 +03007539
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007540static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7541 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007542 short offset = vmcs_field_to_offset(field);
7543 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7544 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007545 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007546
Jim Mattsond37f4262017-12-22 12:12:16 -08007547 switch (vmcs_field_width(field)) {
7548 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007549 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007550 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007551 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007552 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007553 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007554 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007555 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007556 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007557 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007558 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007559 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007560 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007561 WARN_ON(1);
7562 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007563 }
7564
7565}
7566
Abel Gordon16f5b902013-04-18 14:38:25 +03007567static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7568{
7569 int i;
7570 unsigned long field;
7571 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007572 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007573 const u16 *fields = shadow_read_write_fields;
Mathias Krausec2bae892013-06-26 20:36:21 +02007574 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007575
Jan Kiszka282da872014-10-08 18:05:39 +02007576 preempt_disable();
7577
Abel Gordon16f5b902013-04-18 14:38:25 +03007578 vmcs_load(shadow_vmcs);
7579
7580 for (i = 0; i < num_fields; i++) {
7581 field = fields[i];
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007582 field_value = __vmcs_readl(field);
Abel Gordon16f5b902013-04-18 14:38:25 +03007583 vmcs12_write_any(&vmx->vcpu, field, field_value);
7584 }
7585
7586 vmcs_clear(shadow_vmcs);
7587 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007588
7589 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007590}
7591
Abel Gordonc3114422013-04-18 14:38:55 +03007592static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7593{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007594 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02007595 shadow_read_write_fields,
7596 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007597 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007598 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007599 max_shadow_read_write_fields,
7600 max_shadow_read_only_fields
7601 };
7602 int i, q;
7603 unsigned long field;
7604 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007605 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007606
7607 vmcs_load(shadow_vmcs);
7608
Mathias Krausec2bae892013-06-26 20:36:21 +02007609 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007610 for (i = 0; i < max_fields[q]; i++) {
7611 field = fields[q][i];
7612 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007613 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03007614 }
7615 }
7616
7617 vmcs_clear(shadow_vmcs);
7618 vmcs_load(vmx->loaded_vmcs->vmcs);
7619}
7620
Nadav Har'El49f705c2011-05-25 23:08:30 +03007621/*
7622 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7623 * used before) all generate the same failure when it is missing.
7624 */
7625static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7626{
7627 struct vcpu_vmx *vmx = to_vmx(vcpu);
7628 if (vmx->nested.current_vmptr == -1ull) {
7629 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007630 return 0;
7631 }
7632 return 1;
7633}
7634
7635static int handle_vmread(struct kvm_vcpu *vcpu)
7636{
7637 unsigned long field;
7638 u64 field_value;
7639 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7640 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7641 gva_t gva = 0;
7642
Kyle Hueyeb277562016-11-29 12:40:39 -08007643 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007644 return 1;
7645
Kyle Huey6affcbe2016-11-29 12:40:40 -08007646 if (!nested_vmx_check_vmcs12(vcpu))
7647 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007648
Nadav Har'El49f705c2011-05-25 23:08:30 +03007649 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007650 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007651 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007652 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007653 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007654 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007655 }
7656 /*
7657 * Now copy part of this value to register or memory, as requested.
7658 * Note that the number of bits actually copied is 32 or 64 depending
7659 * on the guest's mode (32 or 64 bit), not on the given field's length.
7660 */
7661 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007662 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007663 field_value);
7664 } else {
7665 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007666 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007667 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07007668 /* _system ok, as hardware has verified cpl=0 */
Nadav Har'El49f705c2011-05-25 23:08:30 +03007669 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7670 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7671 }
7672
7673 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007674 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007675}
7676
7677
7678static int handle_vmwrite(struct kvm_vcpu *vcpu)
7679{
7680 unsigned long field;
7681 gva_t gva;
7682 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7683 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007684 /* The value to write might be 32 or 64 bits, depending on L1's long
7685 * mode, and eventually we need to write that into a field of several
7686 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007687 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007688 * bits into the vmcs12 field.
7689 */
7690 u64 field_value = 0;
7691 struct x86_exception e;
7692
Kyle Hueyeb277562016-11-29 12:40:39 -08007693 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007694 return 1;
7695
Kyle Huey6affcbe2016-11-29 12:40:40 -08007696 if (!nested_vmx_check_vmcs12(vcpu))
7697 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007698
Nadav Har'El49f705c2011-05-25 23:08:30 +03007699 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007700 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007701 (((vmx_instruction_info) >> 3) & 0xf));
7702 else {
7703 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007704 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007705 return 1;
7706 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007707 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007708 kvm_inject_page_fault(vcpu, &e);
7709 return 1;
7710 }
7711 }
7712
7713
Nadav Amit27e6fb52014-06-18 17:19:26 +03007714 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007715 if (vmcs_field_readonly(field)) {
7716 nested_vmx_failValid(vcpu,
7717 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007718 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007719 }
7720
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007721 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007722 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007723 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007724 }
7725
7726 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007727 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007728}
7729
Jim Mattsona8bc2842016-11-30 12:03:44 -08007730static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
7731{
7732 vmx->nested.current_vmptr = vmptr;
7733 if (enable_shadow_vmcs) {
7734 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7735 SECONDARY_EXEC_SHADOW_VMCS);
7736 vmcs_write64(VMCS_LINK_POINTER,
7737 __pa(vmx->vmcs01.shadow_vmcs));
7738 vmx->nested.sync_shadow_vmcs = true;
7739 }
7740}
7741
Nadav Har'El63846662011-05-25 23:07:29 +03007742/* Emulate the VMPTRLD instruction */
7743static int handle_vmptrld(struct kvm_vcpu *vcpu)
7744{
7745 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007746 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007747
7748 if (!nested_vmx_check_permission(vcpu))
7749 return 1;
7750
Radim Krčmářcbf71272017-05-19 15:48:51 +02007751 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007752 return 1;
7753
Radim Krčmářcbf71272017-05-19 15:48:51 +02007754 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7755 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
7756 return kvm_skip_emulated_instruction(vcpu);
7757 }
7758
7759 if (vmptr == vmx->nested.vmxon_ptr) {
7760 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
7761 return kvm_skip_emulated_instruction(vcpu);
7762 }
7763
Nadav Har'El63846662011-05-25 23:07:29 +03007764 if (vmx->nested.current_vmptr != vmptr) {
7765 struct vmcs12 *new_vmcs12;
7766 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007767 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7768 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03007769 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007770 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007771 }
7772 new_vmcs12 = kmap(page);
7773 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7774 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007775 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03007776 nested_vmx_failValid(vcpu,
7777 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007778 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007779 }
Nadav Har'El63846662011-05-25 23:07:29 +03007780
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007781 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07007782 /*
7783 * Load VMCS12 from guest memory since it is not already
7784 * cached.
7785 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007786 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
7787 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007788 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007789
Jim Mattsona8bc2842016-11-30 12:03:44 -08007790 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03007791 }
7792
7793 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007794 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007795}
7796
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007797/* Emulate the VMPTRST instruction */
7798static int handle_vmptrst(struct kvm_vcpu *vcpu)
7799{
7800 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7801 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7802 gva_t vmcs_gva;
7803 struct x86_exception e;
7804
7805 if (!nested_vmx_check_permission(vcpu))
7806 return 1;
7807
7808 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007809 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007810 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07007811 /* ok to use *_system, as hardware has verified cpl=0 */
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007812 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7813 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7814 sizeof(u64), &e)) {
7815 kvm_inject_page_fault(vcpu, &e);
7816 return 1;
7817 }
7818 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007819 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007820}
7821
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007822/* Emulate the INVEPT instruction */
7823static int handle_invept(struct kvm_vcpu *vcpu)
7824{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007825 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007826 u32 vmx_instruction_info, types;
7827 unsigned long type;
7828 gva_t gva;
7829 struct x86_exception e;
7830 struct {
7831 u64 eptp, gpa;
7832 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007833
Wincy Vanb9c237b2015-02-03 23:56:30 +08007834 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7835 SECONDARY_EXEC_ENABLE_EPT) ||
7836 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007837 kvm_queue_exception(vcpu, UD_VECTOR);
7838 return 1;
7839 }
7840
7841 if (!nested_vmx_check_permission(vcpu))
7842 return 1;
7843
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007844 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007845 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007846
Wincy Vanb9c237b2015-02-03 23:56:30 +08007847 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007848
Jim Mattson85c856b2016-10-26 08:38:38 -07007849 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007850 nested_vmx_failValid(vcpu,
7851 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007852 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007853 }
7854
7855 /* According to the Intel VMX instruction reference, the memory
7856 * operand is read even if it isn't needed (e.g., for type==global)
7857 */
7858 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007859 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007860 return 1;
7861 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7862 sizeof(operand), &e)) {
7863 kvm_inject_page_fault(vcpu, &e);
7864 return 1;
7865 }
7866
7867 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007868 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007869 /*
7870 * TODO: track mappings and invalidate
7871 * single context requests appropriately
7872 */
7873 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007874 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007875 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007876 nested_vmx_succeed(vcpu);
7877 break;
7878 default:
7879 BUG_ON(1);
7880 break;
7881 }
7882
Kyle Huey6affcbe2016-11-29 12:40:40 -08007883 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007884}
7885
Petr Matouseka642fc32014-09-23 20:22:30 +02007886static int handle_invvpid(struct kvm_vcpu *vcpu)
7887{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007888 struct vcpu_vmx *vmx = to_vmx(vcpu);
7889 u32 vmx_instruction_info;
7890 unsigned long type, types;
7891 gva_t gva;
7892 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07007893 struct {
7894 u64 vpid;
7895 u64 gla;
7896 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007897
7898 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7899 SECONDARY_EXEC_ENABLE_VPID) ||
7900 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7901 kvm_queue_exception(vcpu, UD_VECTOR);
7902 return 1;
7903 }
7904
7905 if (!nested_vmx_check_permission(vcpu))
7906 return 1;
7907
7908 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7909 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7910
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007911 types = (vmx->nested.nested_vmx_vpid_caps &
7912 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007913
Jim Mattson85c856b2016-10-26 08:38:38 -07007914 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007915 nested_vmx_failValid(vcpu,
7916 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007917 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007918 }
7919
7920 /* according to the intel vmx instruction reference, the memory
7921 * operand is read even if it isn't needed (e.g., for type==global)
7922 */
7923 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7924 vmx_instruction_info, false, &gva))
7925 return 1;
Jim Mattson40352602017-06-28 09:37:37 -07007926 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7927 sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007928 kvm_inject_page_fault(vcpu, &e);
7929 return 1;
7930 }
Jim Mattson40352602017-06-28 09:37:37 -07007931 if (operand.vpid >> 16) {
7932 nested_vmx_failValid(vcpu,
7933 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7934 return kvm_skip_emulated_instruction(vcpu);
7935 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007936
7937 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007938 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Yu Zhangfd8cb432017-08-24 20:27:56 +08007939 if (is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07007940 nested_vmx_failValid(vcpu,
7941 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7942 return kvm_skip_emulated_instruction(vcpu);
7943 }
7944 /* fall through */
Paolo Bonzinief697a72016-03-18 16:58:38 +01007945 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007946 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07007947 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007948 nested_vmx_failValid(vcpu,
7949 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007950 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007951 }
7952 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007953 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007954 break;
7955 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007956 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007957 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007958 }
7959
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08007960 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007961 nested_vmx_succeed(vcpu);
7962
Kyle Huey6affcbe2016-11-29 12:40:40 -08007963 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007964}
7965
Kai Huang843e4332015-01-28 10:54:28 +08007966static int handle_pml_full(struct kvm_vcpu *vcpu)
7967{
7968 unsigned long exit_qualification;
7969
7970 trace_kvm_pml_full(vcpu->vcpu_id);
7971
7972 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7973
7974 /*
7975 * PML buffer FULL happened while executing iret from NMI,
7976 * "blocked by NMI" bit has to be set before next VM entry.
7977 */
7978 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007979 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08007980 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7981 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7982 GUEST_INTR_STATE_NMI);
7983
7984 /*
7985 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7986 * here.., and there's no userspace involvement needed for PML.
7987 */
7988 return 1;
7989}
7990
Yunhong Jiang64672c92016-06-13 14:19:59 -07007991static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7992{
7993 kvm_lapic_expired_hv_timer(vcpu);
7994 return 1;
7995}
7996
Bandan Das41ab9372017-08-03 15:54:43 -04007997static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
7998{
7999 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008000 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8001
8002 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008003 switch (address & VMX_EPTP_MT_MASK) {
8004 case VMX_EPTP_MT_UC:
Bandan Das41ab9372017-08-03 15:54:43 -04008005 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_UC_BIT))
8006 return false;
8007 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008008 case VMX_EPTP_MT_WB:
Bandan Das41ab9372017-08-03 15:54:43 -04008009 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_WB_BIT))
8010 return false;
8011 break;
8012 default:
8013 return false;
8014 }
8015
David Hildenbrandbb97a012017-08-10 23:15:28 +02008016 /* only 4 levels page-walk length are valid */
8017 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008018 return false;
8019
8020 /* Reserved bits should not be set */
8021 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8022 return false;
8023
8024 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008025 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Bandan Das41ab9372017-08-03 15:54:43 -04008026 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPT_AD_BIT))
8027 return false;
8028 }
8029
8030 return true;
8031}
8032
8033static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8034 struct vmcs12 *vmcs12)
8035{
8036 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8037 u64 address;
8038 bool accessed_dirty;
8039 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8040
8041 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8042 !nested_cpu_has_ept(vmcs12))
8043 return 1;
8044
8045 if (index >= VMFUNC_EPTP_ENTRIES)
8046 return 1;
8047
8048
8049 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8050 &address, index * 8, 8))
8051 return 1;
8052
David Hildenbrandbb97a012017-08-10 23:15:28 +02008053 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008054
8055 /*
8056 * If the (L2) guest does a vmfunc to the currently
8057 * active ept pointer, we don't have to do anything else
8058 */
8059 if (vmcs12->ept_pointer != address) {
8060 if (!valid_ept_address(vcpu, address))
8061 return 1;
8062
8063 kvm_mmu_unload(vcpu);
8064 mmu->ept_ad = accessed_dirty;
8065 mmu->base_role.ad_disabled = !accessed_dirty;
8066 vmcs12->ept_pointer = address;
8067 /*
8068 * TODO: Check what's the correct approach in case
8069 * mmu reload fails. Currently, we just let the next
8070 * reload potentially fail
8071 */
8072 kvm_mmu_reload(vcpu);
8073 }
8074
8075 return 0;
8076}
8077
Bandan Das2a499e42017-08-03 15:54:41 -04008078static int handle_vmfunc(struct kvm_vcpu *vcpu)
8079{
Bandan Das27c42a12017-08-03 15:54:42 -04008080 struct vcpu_vmx *vmx = to_vmx(vcpu);
8081 struct vmcs12 *vmcs12;
8082 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8083
8084 /*
8085 * VMFUNC is only supported for nested guests, but we always enable the
8086 * secondary control for simplicity; for non-nested mode, fake that we
8087 * didn't by injecting #UD.
8088 */
8089 if (!is_guest_mode(vcpu)) {
8090 kvm_queue_exception(vcpu, UD_VECTOR);
8091 return 1;
8092 }
8093
8094 vmcs12 = get_vmcs12(vcpu);
8095 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8096 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008097
8098 switch (function) {
8099 case 0:
8100 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8101 goto fail;
8102 break;
8103 default:
8104 goto fail;
8105 }
8106 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008107
8108fail:
8109 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8110 vmcs_read32(VM_EXIT_INTR_INFO),
8111 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008112 return 1;
8113}
8114
Nadav Har'El0140cae2011-05-25 23:06:28 +03008115/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008116 * The exit handlers return 1 if the exit was handled fully and guest execution
8117 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8118 * to be done to userspace and return 0.
8119 */
Mathias Krause772e0312012-08-30 01:30:19 +02008120static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008121 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8122 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008123 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008124 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008125 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008126 [EXIT_REASON_CR_ACCESS] = handle_cr,
8127 [EXIT_REASON_DR_ACCESS] = handle_dr,
8128 [EXIT_REASON_CPUID] = handle_cpuid,
8129 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8130 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8131 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8132 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008133 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008134 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008135 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008136 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008137 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008138 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008139 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008140 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008141 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008142 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008143 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008144 [EXIT_REASON_VMOFF] = handle_vmoff,
8145 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008146 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8147 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008148 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008149 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008150 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008151 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008152 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008153 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008154 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8155 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008156 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8157 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008158 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008159 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008160 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008161 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008162 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008163 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008164 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008165 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008166 [EXIT_REASON_XSAVES] = handle_xsaves,
8167 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008168 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008169 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008170 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008171};
8172
8173static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008174 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008175
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008176static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8177 struct vmcs12 *vmcs12)
8178{
8179 unsigned long exit_qualification;
8180 gpa_t bitmap, last_bitmap;
8181 unsigned int port;
8182 int size;
8183 u8 b;
8184
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008185 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008186 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008187
8188 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8189
8190 port = exit_qualification >> 16;
8191 size = (exit_qualification & 7) + 1;
8192
8193 last_bitmap = (gpa_t)-1;
8194 b = -1;
8195
8196 while (size > 0) {
8197 if (port < 0x8000)
8198 bitmap = vmcs12->io_bitmap_a;
8199 else if (port < 0x10000)
8200 bitmap = vmcs12->io_bitmap_b;
8201 else
Joe Perches1d804d02015-03-30 16:46:09 -07008202 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008203 bitmap += (port & 0x7fff) / 8;
8204
8205 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008206 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008207 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008208 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008209 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008210
8211 port++;
8212 size--;
8213 last_bitmap = bitmap;
8214 }
8215
Joe Perches1d804d02015-03-30 16:46:09 -07008216 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008217}
8218
Nadav Har'El644d7112011-05-25 23:12:35 +03008219/*
8220 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8221 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8222 * disinterest in the current event (read or write a specific MSR) by using an
8223 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8224 */
8225static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8226 struct vmcs12 *vmcs12, u32 exit_reason)
8227{
8228 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8229 gpa_t bitmap;
8230
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008231 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008232 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008233
8234 /*
8235 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8236 * for the four combinations of read/write and low/high MSR numbers.
8237 * First we need to figure out which of the four to use:
8238 */
8239 bitmap = vmcs12->msr_bitmap;
8240 if (exit_reason == EXIT_REASON_MSR_WRITE)
8241 bitmap += 2048;
8242 if (msr_index >= 0xc0000000) {
8243 msr_index -= 0xc0000000;
8244 bitmap += 1024;
8245 }
8246
8247 /* Then read the msr_index'th bit from this bitmap: */
8248 if (msr_index < 1024*8) {
8249 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008250 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008251 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008252 return 1 & (b >> (msr_index & 7));
8253 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008254 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008255}
8256
8257/*
8258 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8259 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8260 * intercept (via guest_host_mask etc.) the current event.
8261 */
8262static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8263 struct vmcs12 *vmcs12)
8264{
8265 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8266 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008267 int reg;
8268 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008269
8270 switch ((exit_qualification >> 4) & 3) {
8271 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008272 reg = (exit_qualification >> 8) & 15;
8273 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008274 switch (cr) {
8275 case 0:
8276 if (vmcs12->cr0_guest_host_mask &
8277 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008278 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008279 break;
8280 case 3:
8281 if ((vmcs12->cr3_target_count >= 1 &&
8282 vmcs12->cr3_target_value0 == val) ||
8283 (vmcs12->cr3_target_count >= 2 &&
8284 vmcs12->cr3_target_value1 == val) ||
8285 (vmcs12->cr3_target_count >= 3 &&
8286 vmcs12->cr3_target_value2 == val) ||
8287 (vmcs12->cr3_target_count >= 4 &&
8288 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008289 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008290 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008291 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008292 break;
8293 case 4:
8294 if (vmcs12->cr4_guest_host_mask &
8295 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008296 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008297 break;
8298 case 8:
8299 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008300 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008301 break;
8302 }
8303 break;
8304 case 2: /* clts */
8305 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8306 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008307 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008308 break;
8309 case 1: /* mov from cr */
8310 switch (cr) {
8311 case 3:
8312 if (vmcs12->cpu_based_vm_exec_control &
8313 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008314 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008315 break;
8316 case 8:
8317 if (vmcs12->cpu_based_vm_exec_control &
8318 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008319 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008320 break;
8321 }
8322 break;
8323 case 3: /* lmsw */
8324 /*
8325 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8326 * cr0. Other attempted changes are ignored, with no exit.
8327 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008328 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008329 if (vmcs12->cr0_guest_host_mask & 0xe &
8330 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008331 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008332 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8333 !(vmcs12->cr0_read_shadow & 0x1) &&
8334 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008335 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008336 break;
8337 }
Joe Perches1d804d02015-03-30 16:46:09 -07008338 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008339}
8340
8341/*
8342 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8343 * should handle it ourselves in L0 (and then continue L2). Only call this
8344 * when in is_guest_mode (L2).
8345 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02008346static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03008347{
Nadav Har'El644d7112011-05-25 23:12:35 +03008348 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8349 struct vcpu_vmx *vmx = to_vmx(vcpu);
8350 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8351
Jim Mattson4f350c62017-09-14 16:31:44 -07008352 if (vmx->nested.nested_run_pending)
8353 return false;
8354
8355 if (unlikely(vmx->fail)) {
8356 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8357 vmcs_read32(VM_INSTRUCTION_ERROR));
8358 return true;
8359 }
Jan Kiszka542060e2014-01-04 18:47:21 +01008360
David Matlackc9f04402017-08-01 14:00:40 -07008361 /*
8362 * The host physical addresses of some pages of guest memory
Jim Mattson00647b42017-11-27 17:22:25 -06008363 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8364 * Page). The CPU may write to these pages via their host
8365 * physical address while L2 is running, bypassing any
8366 * address-translation-based dirty tracking (e.g. EPT write
8367 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07008368 *
8369 * Mark them dirty on every exit from L2 to prevent them from
8370 * getting out of sync with dirty tracking.
8371 */
8372 nested_mark_vmcs12_pages_dirty(vcpu);
8373
Jim Mattson4f350c62017-09-14 16:31:44 -07008374 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8375 vmcs_readl(EXIT_QUALIFICATION),
8376 vmx->idt_vectoring_info,
8377 intr_info,
8378 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8379 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03008380
8381 switch (exit_reason) {
8382 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008383 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008384 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008385 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07008386 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008387 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008388 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008389 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008390 else if (is_debug(intr_info) &&
8391 vcpu->guest_debug &
8392 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8393 return false;
8394 else if (is_breakpoint(intr_info) &&
8395 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8396 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008397 return vmcs12->exception_bitmap &
8398 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8399 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008400 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008401 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008402 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008403 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008404 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008405 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008406 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008407 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008408 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008409 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008410 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008411 case EXIT_REASON_HLT:
8412 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8413 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008414 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008415 case EXIT_REASON_INVLPG:
8416 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8417 case EXIT_REASON_RDPMC:
8418 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008419 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008420 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008421 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008422 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008423 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008424 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8425 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8426 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8427 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8428 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8429 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008430 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008431 /*
8432 * VMX instructions trap unconditionally. This allows L1 to
8433 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8434 */
Joe Perches1d804d02015-03-30 16:46:09 -07008435 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008436 case EXIT_REASON_CR_ACCESS:
8437 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8438 case EXIT_REASON_DR_ACCESS:
8439 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8440 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008441 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008442 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8443 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03008444 case EXIT_REASON_MSR_READ:
8445 case EXIT_REASON_MSR_WRITE:
8446 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8447 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008448 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008449 case EXIT_REASON_MWAIT_INSTRUCTION:
8450 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008451 case EXIT_REASON_MONITOR_TRAP_FLAG:
8452 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008453 case EXIT_REASON_MONITOR_INSTRUCTION:
8454 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8455 case EXIT_REASON_PAUSE_INSTRUCTION:
8456 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8457 nested_cpu_has2(vmcs12,
8458 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8459 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008460 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008461 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008462 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008463 case EXIT_REASON_APIC_ACCESS:
8464 return nested_cpu_has2(vmcs12,
8465 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008466 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008467 case EXIT_REASON_EOI_INDUCED:
8468 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008469 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008470 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008471 /*
8472 * L0 always deals with the EPT violation. If nested EPT is
8473 * used, and the nested mmu code discovers that the address is
8474 * missing in the guest EPT table (EPT12), the EPT violation
8475 * will be injected with nested_ept_inject_page_fault()
8476 */
Joe Perches1d804d02015-03-30 16:46:09 -07008477 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008478 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008479 /*
8480 * L2 never uses directly L1's EPT, but rather L0's own EPT
8481 * table (shadow on EPT) or a merged EPT table that L0 built
8482 * (EPT on EPT). So any problems with the structure of the
8483 * table is L0's fault.
8484 */
Joe Perches1d804d02015-03-30 16:46:09 -07008485 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02008486 case EXIT_REASON_INVPCID:
8487 return
8488 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8489 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008490 case EXIT_REASON_WBINVD:
8491 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8492 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008493 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008494 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8495 /*
8496 * This should never happen, since it is not possible to
8497 * set XSS to a non-zero value---neither in L1 nor in L2.
8498 * If if it were, XSS would have to be checked against
8499 * the XSS exit bitmap in vmcs12.
8500 */
8501 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008502 case EXIT_REASON_PREEMPTION_TIMER:
8503 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02008504 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04008505 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02008506 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04008507 case EXIT_REASON_VMFUNC:
8508 /* VM functions are emulated through L2->L0 vmexits. */
8509 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008510 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008511 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008512 }
8513}
8514
Paolo Bonzini7313c692017-07-27 10:31:25 +02008515static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8516{
8517 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8518
8519 /*
8520 * At this point, the exit interruption info in exit_intr_info
8521 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8522 * we need to query the in-kernel LAPIC.
8523 */
8524 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8525 if ((exit_intr_info &
8526 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8527 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8528 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8529 vmcs12->vm_exit_intr_error_code =
8530 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8531 }
8532
8533 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8534 vmcs_readl(EXIT_QUALIFICATION));
8535 return 1;
8536}
8537
Avi Kivity586f9602010-11-18 13:09:54 +02008538static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8539{
8540 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8541 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8542}
8543
Kai Huanga3eaa862015-11-04 13:46:05 +08008544static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008545{
Kai Huanga3eaa862015-11-04 13:46:05 +08008546 if (vmx->pml_pg) {
8547 __free_page(vmx->pml_pg);
8548 vmx->pml_pg = NULL;
8549 }
Kai Huang843e4332015-01-28 10:54:28 +08008550}
8551
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008552static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008553{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008554 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008555 u64 *pml_buf;
8556 u16 pml_idx;
8557
8558 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8559
8560 /* Do nothing if PML buffer is empty */
8561 if (pml_idx == (PML_ENTITY_NUM - 1))
8562 return;
8563
8564 /* PML index always points to next available PML buffer entity */
8565 if (pml_idx >= PML_ENTITY_NUM)
8566 pml_idx = 0;
8567 else
8568 pml_idx++;
8569
8570 pml_buf = page_address(vmx->pml_pg);
8571 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8572 u64 gpa;
8573
8574 gpa = pml_buf[pml_idx];
8575 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008576 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008577 }
8578
8579 /* reset PML index */
8580 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8581}
8582
8583/*
8584 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8585 * Called before reporting dirty_bitmap to userspace.
8586 */
8587static void kvm_flush_pml_buffers(struct kvm *kvm)
8588{
8589 int i;
8590 struct kvm_vcpu *vcpu;
8591 /*
8592 * We only need to kick vcpu out of guest mode here, as PML buffer
8593 * is flushed at beginning of all VMEXITs, and it's obvious that only
8594 * vcpus running in guest are possible to have unflushed GPAs in PML
8595 * buffer.
8596 */
8597 kvm_for_each_vcpu(i, vcpu, kvm)
8598 kvm_vcpu_kick(vcpu);
8599}
8600
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008601static void vmx_dump_sel(char *name, uint32_t sel)
8602{
8603 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05008604 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008605 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8606 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8607 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8608}
8609
8610static void vmx_dump_dtsel(char *name, uint32_t limit)
8611{
8612 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8613 name, vmcs_read32(limit),
8614 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8615}
8616
8617static void dump_vmcs(void)
8618{
8619 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8620 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8621 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8622 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8623 u32 secondary_exec_control = 0;
8624 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008625 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008626 int i, n;
8627
8628 if (cpu_has_secondary_exec_ctrls())
8629 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8630
8631 pr_err("*** Guest State ***\n");
8632 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8633 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8634 vmcs_readl(CR0_GUEST_HOST_MASK));
8635 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8636 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8637 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8638 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8639 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8640 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008641 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8642 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8643 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8644 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008645 }
8646 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8647 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8648 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8649 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8650 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8651 vmcs_readl(GUEST_SYSENTER_ESP),
8652 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8653 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8654 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8655 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8656 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8657 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8658 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8659 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8660 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8661 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8662 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8663 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8664 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008665 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8666 efer, vmcs_read64(GUEST_IA32_PAT));
8667 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8668 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008669 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8670 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008671 pr_err("PerfGlobCtl = 0x%016llx\n",
8672 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008673 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008674 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008675 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8676 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8677 vmcs_read32(GUEST_ACTIVITY_STATE));
8678 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8679 pr_err("InterruptStatus = %04x\n",
8680 vmcs_read16(GUEST_INTR_STATUS));
8681
8682 pr_err("*** Host State ***\n");
8683 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8684 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8685 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8686 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8687 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8688 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8689 vmcs_read16(HOST_TR_SELECTOR));
8690 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8691 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8692 vmcs_readl(HOST_TR_BASE));
8693 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8694 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8695 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8696 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8697 vmcs_readl(HOST_CR4));
8698 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8699 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8700 vmcs_read32(HOST_IA32_SYSENTER_CS),
8701 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8702 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008703 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8704 vmcs_read64(HOST_IA32_EFER),
8705 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008706 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008707 pr_err("PerfGlobCtl = 0x%016llx\n",
8708 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008709
8710 pr_err("*** Control State ***\n");
8711 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8712 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8713 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8714 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8715 vmcs_read32(EXCEPTION_BITMAP),
8716 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8717 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8718 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8719 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8720 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8721 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8722 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8723 vmcs_read32(VM_EXIT_INTR_INFO),
8724 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8725 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8726 pr_err(" reason=%08x qualification=%016lx\n",
8727 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8728 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8729 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8730 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008731 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008732 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008733 pr_err("TSC Multiplier = 0x%016llx\n",
8734 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008735 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8736 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8737 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8738 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8739 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008740 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008741 n = vmcs_read32(CR3_TARGET_COUNT);
8742 for (i = 0; i + 1 < n; i += 4)
8743 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8744 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8745 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8746 if (i < n)
8747 pr_err("CR3 target%u=%016lx\n",
8748 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8749 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8750 pr_err("PLE Gap=%08x Window=%08x\n",
8751 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8752 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8753 pr_err("Virtual processor ID = 0x%04x\n",
8754 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8755}
8756
Avi Kivity6aa8b732006-12-10 02:21:36 -08008757/*
8758 * The guest has exited. See if we can fix it or if we need userspace
8759 * assistance.
8760 */
Avi Kivity851ba692009-08-24 11:10:17 +03008761static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008762{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008763 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008764 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008765 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008766
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008767 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8768
Kai Huang843e4332015-01-28 10:54:28 +08008769 /*
8770 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8771 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8772 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8773 * mode as if vcpus is in root mode, the PML buffer must has been
8774 * flushed already.
8775 */
8776 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008777 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008778
Mohammed Gamal80ced182009-09-01 12:48:18 +02008779 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008780 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008781 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008782
Paolo Bonzini7313c692017-07-27 10:31:25 +02008783 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
8784 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03008785
Mohammed Gamal51207022010-05-31 22:40:54 +03008786 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008787 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008788 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8789 vcpu->run->fail_entry.hardware_entry_failure_reason
8790 = exit_reason;
8791 return 0;
8792 }
8793
Avi Kivity29bd8a72007-09-10 17:27:03 +03008794 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008795 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8796 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008797 = vmcs_read32(VM_INSTRUCTION_ERROR);
8798 return 0;
8799 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008800
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008801 /*
8802 * Note:
8803 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8804 * delivery event since it indicates guest is accessing MMIO.
8805 * The vm-exit can be triggered again after return to guest that
8806 * will cause infinite loop.
8807 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008808 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008809 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008810 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008811 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008812 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8813 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8814 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02008815 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008816 vcpu->run->internal.data[0] = vectoring_info;
8817 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02008818 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
8819 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
8820 vcpu->run->internal.ndata++;
8821 vcpu->run->internal.data[3] =
8822 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
8823 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008824 return 0;
8825 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008826
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008827 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01008828 vmx->loaded_vmcs->soft_vnmi_blocked)) {
8829 if (vmx_interrupt_allowed(vcpu)) {
8830 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8831 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
8832 vcpu->arch.nmi_pending) {
8833 /*
8834 * This CPU don't support us in finding the end of an
8835 * NMI-blocked window if the guest runs with IRQs
8836 * disabled. So we pull the trigger after 1 s of
8837 * futile waiting, but inform the user about this.
8838 */
8839 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8840 "state on VCPU %d after 1 s timeout\n",
8841 __func__, vcpu->vcpu_id);
8842 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8843 }
8844 }
8845
Avi Kivity6aa8b732006-12-10 02:21:36 -08008846 if (exit_reason < kvm_vmx_max_exit_handlers
8847 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008848 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008849 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01008850 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
8851 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008852 kvm_queue_exception(vcpu, UD_VECTOR);
8853 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008854 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008855}
8856
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008857static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008858{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008859 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8860
8861 if (is_guest_mode(vcpu) &&
8862 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8863 return;
8864
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008865 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008866 vmcs_write32(TPR_THRESHOLD, 0);
8867 return;
8868 }
8869
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008870 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008871}
8872
Yang Zhang8d146952013-01-25 10:18:50 +08008873static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8874{
8875 u32 sec_exec_control;
8876
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008877 /* Postpone execution until vmcs01 is the current VMCS. */
8878 if (is_guest_mode(vcpu)) {
8879 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8880 return;
8881 }
8882
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008883 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008884 return;
8885
Paolo Bonzini35754c92015-07-29 12:05:37 +02008886 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008887 return;
8888
8889 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8890
8891 if (set) {
8892 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8893 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8894 } else {
8895 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8896 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008897 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008898 }
8899 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8900
8901 vmx_set_msr_bitmap(vcpu);
8902}
8903
Tang Chen38b99172014-09-24 15:57:54 +08008904static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8905{
8906 struct vcpu_vmx *vmx = to_vmx(vcpu);
8907
8908 /*
8909 * Currently we do not handle the nested case where L2 has an
8910 * APIC access page of its own; that page is still pinned.
8911 * Hence, we skip the case where the VCPU is in guest mode _and_
8912 * L1 prepared an APIC access page for L2.
8913 *
8914 * For the case where L1 and L2 share the same APIC access page
8915 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8916 * in the vmcs12), this function will only update either the vmcs01
8917 * or the vmcs02. If the former, the vmcs02 will be updated by
8918 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8919 * the next L2->L1 exit.
8920 */
8921 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008922 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008923 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08008924 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008925 vmx_flush_tlb_ept_only(vcpu);
8926 }
Tang Chen38b99172014-09-24 15:57:54 +08008927}
8928
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008929static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008930{
8931 u16 status;
8932 u8 old;
8933
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008934 if (max_isr == -1)
8935 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008936
8937 status = vmcs_read16(GUEST_INTR_STATUS);
8938 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008939 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008940 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008941 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008942 vmcs_write16(GUEST_INTR_STATUS, status);
8943 }
8944}
8945
8946static void vmx_set_rvi(int vector)
8947{
8948 u16 status;
8949 u8 old;
8950
Wei Wang4114c272014-11-05 10:53:43 +08008951 if (vector == -1)
8952 vector = 0;
8953
Yang Zhangc7c9c562013-01-25 10:18:51 +08008954 status = vmcs_read16(GUEST_INTR_STATUS);
8955 old = (u8)status & 0xff;
8956 if ((u8)vector != old) {
8957 status &= ~0xff;
8958 status |= (u8)vector;
8959 vmcs_write16(GUEST_INTR_STATUS, status);
8960 }
8961}
8962
8963static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8964{
Liran Alon851c1a182017-12-24 18:12:56 +02008965 /*
8966 * When running L2, updating RVI is only relevant when
8967 * vmcs12 virtual-interrupt-delivery enabled.
8968 * However, it can be enabled only when L1 also
8969 * intercepts external-interrupts and in that case
8970 * we should not update vmcs02 RVI but instead intercept
8971 * interrupt. Therefore, do nothing when running L2.
8972 */
8973 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08008974 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008975}
8976
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008977static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008978{
8979 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008980 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02008981 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008982
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008983 WARN_ON(!vcpu->arch.apicv_active);
8984 if (pi_test_on(&vmx->pi_desc)) {
8985 pi_clear_on(&vmx->pi_desc);
8986 /*
8987 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
8988 * But on x86 this is just a compiler barrier anyway.
8989 */
8990 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02008991 max_irr_updated =
8992 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
8993
8994 /*
8995 * If we are running L2 and L1 has a new pending interrupt
8996 * which can be injected, we should re-evaluate
8997 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02008998 * If L1 intercepts external-interrupts, we should
8999 * exit from L2 to L1. Otherwise, interrupt should be
9000 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009001 */
Liran Alon851c1a182017-12-24 18:12:56 +02009002 if (is_guest_mode(vcpu) && max_irr_updated) {
9003 if (nested_exit_on_intr(vcpu))
9004 kvm_vcpu_exiting_guest_mode(vcpu);
9005 else
9006 kvm_make_request(KVM_REQ_EVENT, vcpu);
9007 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009008 } else {
9009 max_irr = kvm_lapic_find_highest_irr(vcpu);
9010 }
9011 vmx_hwapic_irr_update(vcpu, max_irr);
9012 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009013}
9014
Andrey Smetanin63086302015-11-10 15:36:32 +03009015static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009016{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009017 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009018 return;
9019
Yang Zhangc7c9c562013-01-25 10:18:51 +08009020 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9021 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9022 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9023 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9024}
9025
Paolo Bonzini967235d2016-12-19 14:03:45 +01009026static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9027{
9028 struct vcpu_vmx *vmx = to_vmx(vcpu);
9029
9030 pi_clear_on(&vmx->pi_desc);
9031 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9032}
9033
Avi Kivity51aa01d2010-07-20 14:31:20 +03009034static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009035{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009036 u32 exit_intr_info = 0;
9037 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009038
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009039 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9040 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009041 return;
9042
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009043 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9044 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9045 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009046
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009047 /* if exit due to PF check for async PF */
9048 if (is_page_fault(exit_intr_info))
9049 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9050
Andi Kleena0861c02009-06-08 17:37:09 +08009051 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009052 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9053 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009054 kvm_machine_check();
9055
Gleb Natapov20f65982009-05-11 13:35:55 +03009056 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009057 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009058 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009059 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009060 kvm_after_handle_nmi(&vmx->vcpu);
9061 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009062}
Gleb Natapov20f65982009-05-11 13:35:55 +03009063
Yang Zhanga547c6d2013-04-11 19:25:10 +08009064static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9065{
9066 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9067
Yang Zhanga547c6d2013-04-11 19:25:10 +08009068 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9069 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9070 unsigned int vector;
9071 unsigned long entry;
9072 gate_desc *desc;
9073 struct vcpu_vmx *vmx = to_vmx(vcpu);
9074#ifdef CONFIG_X86_64
9075 unsigned long tmp;
9076#endif
9077
9078 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9079 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009080 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009081 asm volatile(
9082#ifdef CONFIG_X86_64
9083 "mov %%" _ASM_SP ", %[sp]\n\t"
9084 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9085 "push $%c[ss]\n\t"
9086 "push %[sp]\n\t"
9087#endif
9088 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009089 __ASM_SIZE(push) " $%c[cs]\n\t"
9090 "call *%[entry]\n\t"
9091 :
9092#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009093 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009094#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009095 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009096 :
9097 [entry]"r"(entry),
9098 [ss]"i"(__KERNEL_DS),
9099 [cs]"i"(__KERNEL_CS)
9100 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009101 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009102}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009103STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009104
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009105static bool vmx_has_high_real_mode_segbase(void)
9106{
9107 return enable_unrestricted_guest || emulate_invalid_guest_state;
9108}
9109
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009110static bool vmx_mpx_supported(void)
9111{
9112 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9113 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9114}
9115
Wanpeng Li55412b22014-12-02 19:21:30 +08009116static bool vmx_xsaves_supported(void)
9117{
9118 return vmcs_config.cpu_based_2nd_exec_ctrl &
9119 SECONDARY_EXEC_XSAVES;
9120}
9121
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009122static bool vmx_umip_emulated(void)
9123{
Paolo Bonzini0367f202016-07-12 10:44:55 +02009124 return vmcs_config.cpu_based_2nd_exec_ctrl &
9125 SECONDARY_EXEC_DESC;
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009126}
9127
Avi Kivity51aa01d2010-07-20 14:31:20 +03009128static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9129{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009130 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009131 bool unblock_nmi;
9132 u8 vector;
9133 bool idtv_info_valid;
9134
9135 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009136
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009137 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009138 if (vmx->loaded_vmcs->nmi_known_unmasked)
9139 return;
9140 /*
9141 * Can't use vmx->exit_intr_info since we're not sure what
9142 * the exit reason is.
9143 */
9144 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9145 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9146 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9147 /*
9148 * SDM 3: 27.7.1.2 (September 2008)
9149 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9150 * a guest IRET fault.
9151 * SDM 3: 23.2.2 (September 2008)
9152 * Bit 12 is undefined in any of the following cases:
9153 * If the VM exit sets the valid bit in the IDT-vectoring
9154 * information field.
9155 * If the VM exit is due to a double fault.
9156 */
9157 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9158 vector != DF_VECTOR && !idtv_info_valid)
9159 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9160 GUEST_INTR_STATE_NMI);
9161 else
9162 vmx->loaded_vmcs->nmi_known_unmasked =
9163 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9164 & GUEST_INTR_STATE_NMI);
9165 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9166 vmx->loaded_vmcs->vnmi_blocked_time +=
9167 ktime_to_ns(ktime_sub(ktime_get(),
9168 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009169}
9170
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009171static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009172 u32 idt_vectoring_info,
9173 int instr_len_field,
9174 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009175{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009176 u8 vector;
9177 int type;
9178 bool idtv_info_valid;
9179
9180 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009181
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009182 vcpu->arch.nmi_injected = false;
9183 kvm_clear_exception_queue(vcpu);
9184 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009185
9186 if (!idtv_info_valid)
9187 return;
9188
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009189 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009190
Avi Kivity668f6122008-07-02 09:28:55 +03009191 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9192 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009193
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009194 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009195 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009196 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009197 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009198 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009199 * Clear bit "block by NMI" before VM entry if a NMI
9200 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009201 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009202 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009203 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009204 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009205 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009206 /* fall through */
9207 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009208 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009209 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009210 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009211 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009212 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009213 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009214 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009215 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009216 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009217 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009218 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009219 break;
9220 default:
9221 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009222 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009223}
9224
Avi Kivity83422e12010-07-20 14:43:23 +03009225static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9226{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009227 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009228 VM_EXIT_INSTRUCTION_LEN,
9229 IDT_VECTORING_ERROR_CODE);
9230}
9231
Avi Kivityb463a6f2010-07-20 15:06:17 +03009232static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9233{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009234 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009235 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9236 VM_ENTRY_INSTRUCTION_LEN,
9237 VM_ENTRY_EXCEPTION_ERROR_CODE);
9238
9239 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9240}
9241
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009242static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9243{
9244 int i, nr_msrs;
9245 struct perf_guest_switch_msr *msrs;
9246
9247 msrs = perf_guest_get_msrs(&nr_msrs);
9248
9249 if (!msrs)
9250 return;
9251
9252 for (i = 0; i < nr_msrs; i++)
9253 if (msrs[i].host == msrs[i].guest)
9254 clear_atomic_switch_msr(vmx, msrs[i].msr);
9255 else
9256 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9257 msrs[i].host);
9258}
9259
Jiang Biao33365e72016-11-03 15:03:37 +08009260static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009261{
9262 struct vcpu_vmx *vmx = to_vmx(vcpu);
9263 u64 tscl;
9264 u32 delta_tsc;
9265
9266 if (vmx->hv_deadline_tsc == -1)
9267 return;
9268
9269 tscl = rdtsc();
9270 if (vmx->hv_deadline_tsc > tscl)
9271 /* sure to be 32 bit only because checked on set_hv_timer */
9272 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9273 cpu_preemption_timer_multi);
9274 else
9275 delta_tsc = 0;
9276
9277 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9278}
9279
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009280static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009281{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009282 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Li74c55932017-11-29 01:31:20 -08009283 unsigned long cr3, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02009284
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009285 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009286 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009287 vmx->loaded_vmcs->soft_vnmi_blocked))
9288 vmx->loaded_vmcs->entry_time = ktime_get();
9289
Avi Kivity104f2262010-11-18 13:12:52 +02009290 /* Don't enter VMX if guest state is invalid, let the exit handler
9291 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009292 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009293 return;
9294
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009295 if (vmx->ple_window_dirty) {
9296 vmx->ple_window_dirty = false;
9297 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9298 }
9299
Abel Gordon012f83c2013-04-18 14:39:25 +03009300 if (vmx->nested.sync_shadow_vmcs) {
9301 copy_vmcs12_to_shadow(vmx);
9302 vmx->nested.sync_shadow_vmcs = false;
9303 }
9304
Avi Kivity104f2262010-11-18 13:12:52 +02009305 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9306 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9307 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9308 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9309
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009310 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +02009311 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009312 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +02009313 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009314 }
9315
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009316 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +02009317 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009318 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +02009319 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009320 }
9321
Avi Kivity104f2262010-11-18 13:12:52 +02009322 /* When single-stepping over STI and MOV SS, we must clear the
9323 * corresponding interruptibility bits in the guest state. Otherwise
9324 * vmentry fails as it then expects bit 14 (BS) in pending debug
9325 * exceptions being set, but that's not correct for the guest debugging
9326 * case. */
9327 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9328 vmx_set_interrupt_shadow(vcpu, 0);
9329
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009330 if (static_cpu_has(X86_FEATURE_PKU) &&
9331 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9332 vcpu->arch.pkru != vmx->host_pkru)
9333 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009334
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009335 atomic_switch_perf_msrs(vmx);
9336
Yunhong Jiang64672c92016-06-13 14:19:59 -07009337 vmx_arm_hv_timer(vcpu);
9338
Nadav Har'Eld462b812011-05-24 15:26:10 +03009339 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02009340 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009341 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009342 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9343 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9344 "push %%" _ASM_CX " \n\t"
9345 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009346 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009347 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009348 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009349 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009350 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009351 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9352 "mov %%cr2, %%" _ASM_DX " \n\t"
9353 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009354 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009355 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009356 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009357 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009358 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009359 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009360 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9361 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9362 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9363 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9364 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9365 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009366#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009367 "mov %c[r8](%0), %%r8 \n\t"
9368 "mov %c[r9](%0), %%r9 \n\t"
9369 "mov %c[r10](%0), %%r10 \n\t"
9370 "mov %c[r11](%0), %%r11 \n\t"
9371 "mov %c[r12](%0), %%r12 \n\t"
9372 "mov %c[r13](%0), %%r13 \n\t"
9373 "mov %c[r14](%0), %%r14 \n\t"
9374 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009375#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009376 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009377
Avi Kivity6aa8b732006-12-10 02:21:36 -08009378 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009379 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009380 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009381 "jmp 2f \n\t"
9382 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9383 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009384 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009385 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009386 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009387 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9388 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9389 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9390 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9391 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9392 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9393 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009394#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009395 "mov %%r8, %c[r8](%0) \n\t"
9396 "mov %%r9, %c[r9](%0) \n\t"
9397 "mov %%r10, %c[r10](%0) \n\t"
9398 "mov %%r11, %c[r11](%0) \n\t"
9399 "mov %%r12, %c[r12](%0) \n\t"
9400 "mov %%r13, %c[r13](%0) \n\t"
9401 "mov %%r14, %c[r14](%0) \n\t"
9402 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009403#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009404 "mov %%cr2, %%" _ASM_AX " \n\t"
9405 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009406
Avi Kivityb188c81f2012-09-16 15:10:58 +03009407 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02009408 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009409 ".pushsection .rodata \n\t"
9410 ".global vmx_return \n\t"
9411 "vmx_return: " _ASM_PTR " 2b \n\t"
9412 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009413 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009414 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009415 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03009416 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009417 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9418 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9419 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9420 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9421 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9422 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9423 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009424#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009425 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9426 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9427 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9428 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9429 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9430 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9431 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9432 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009433#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009434 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9435 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009436 : "cc", "memory"
9437#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009438 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009439 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009440#else
9441 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009442#endif
9443 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009444
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009445 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08009446 if (vmx->host_debugctlmsr)
9447 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009448
Avi Kivityaa67f602012-08-01 16:48:03 +03009449#ifndef CONFIG_X86_64
9450 /*
9451 * The sysexit path does not restore ds/es, so we must set them to
9452 * a reasonable value ourselves.
9453 *
9454 * We can't defer this to vmx_load_host_state() since that function
9455 * may be executed in interrupt context, which saves and restore segments
9456 * around it, nullifying its effect.
9457 */
9458 loadsegment(ds, __USER_DS);
9459 loadsegment(es, __USER_DS);
9460#endif
9461
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009462 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009463 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009464 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009465 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009466 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009467 vcpu->arch.regs_dirty = 0;
9468
Gleb Natapove0b890d2013-09-25 12:51:33 +03009469 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009470 * eager fpu is enabled if PKEY is supported and CR4 is switched
9471 * back on host, so it is safe to read guest PKRU from current
9472 * XSAVE.
9473 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009474 if (static_cpu_has(X86_FEATURE_PKU) &&
9475 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9476 vcpu->arch.pkru = __read_pkru();
9477 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009478 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009479 }
9480
9481 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009482 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9483 * we did not inject a still-pending event to L1 now because of
9484 * nested_run_pending, we need to re-enable this bit.
9485 */
9486 if (vmx->nested.nested_run_pending)
9487 kvm_make_request(KVM_REQ_EVENT, vcpu);
9488
9489 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07009490 vmx->idt_vectoring_info = 0;
9491
9492 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9493 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9494 return;
9495
9496 vmx->loaded_vmcs->launched = 1;
9497 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03009498
Avi Kivity51aa01d2010-07-20 14:31:20 +03009499 vmx_complete_atomic_exit(vmx);
9500 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009501 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009502}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009503STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009504
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009505static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009506{
9507 struct vcpu_vmx *vmx = to_vmx(vcpu);
9508 int cpu;
9509
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009510 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009511 return;
9512
9513 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009514 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009515 vmx_vcpu_put(vcpu);
9516 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009517 put_cpu();
9518}
9519
Jim Mattson2f1fe812016-07-08 15:36:06 -07009520/*
9521 * Ensure that the current vmcs of the logical processor is the
9522 * vmcs01 of the vcpu before calling free_nested().
9523 */
9524static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9525{
9526 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009527
Christoffer Dallec7660c2017-12-04 21:35:23 +01009528 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009529 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009530 free_nested(vmx);
9531 vcpu_put(vcpu);
9532}
9533
Avi Kivity6aa8b732006-12-10 02:21:36 -08009534static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9535{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009536 struct vcpu_vmx *vmx = to_vmx(vcpu);
9537
Kai Huang843e4332015-01-28 10:54:28 +08009538 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009539 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009540 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009541 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009542 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009543 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009544 kfree(vmx->guest_msrs);
9545 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009546 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009547}
9548
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009549static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009550{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009551 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009552 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03009553 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009554
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009555 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009556 return ERR_PTR(-ENOMEM);
9557
Wanpeng Li991e7a02015-09-16 17:30:05 +08009558 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009559
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009560 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9561 if (err)
9562 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009563
Peter Feiner4e595162016-07-07 14:49:58 -07009564 err = -ENOMEM;
9565
9566 /*
9567 * If PML is turned on, failure on enabling PML just results in failure
9568 * of creating the vcpu, therefore we can simplify PML logic (by
9569 * avoiding dealing with cases, such as enabling PML partially on vcpus
9570 * for the guest, etc.
9571 */
9572 if (enable_pml) {
9573 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9574 if (!vmx->pml_pg)
9575 goto uninit_vcpu;
9576 }
9577
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009578 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009579 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9580 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009581
Peter Feiner4e595162016-07-07 14:49:58 -07009582 if (!vmx->guest_msrs)
9583 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009584
Nadav Har'Eld462b812011-05-24 15:26:10 +03009585 vmx->loaded_vmcs = &vmx->vmcs01;
9586 vmx->loaded_vmcs->vmcs = alloc_vmcs();
Jim Mattson355f4fb2016-10-28 08:29:39 -07009587 vmx->loaded_vmcs->shadow_vmcs = NULL;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009588 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009589 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009590 loaded_vmcs_init(vmx->loaded_vmcs);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009591
Avi Kivity15ad7142007-07-11 18:17:21 +03009592 cpu = get_cpu();
9593 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009594 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +02009595 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009596 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009597 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +02009598 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009599 err = alloc_apic_access_page(kvm);
9600 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009601 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009602 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009603
Sheng Yangb927a3c2009-07-21 10:42:48 +08009604 if (enable_ept) {
Tang Chenf51770e2014-09-16 18:41:59 +08009605 err = init_rmode_identity_map(kvm);
9606 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009607 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009608 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009609
Wanpeng Li5c614b32015-10-13 09:18:36 -07009610 if (nested) {
Wincy Vanb9c237b2015-02-03 23:56:30 +08009611 nested_vmx_setup_ctls_msrs(vmx);
Wanpeng Li5c614b32015-10-13 09:18:36 -07009612 vmx->nested.vpid02 = allocate_vpid();
9613 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009614
Wincy Van705699a2015-02-03 23:58:17 +08009615 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009616 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009617
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009618 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9619
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02009620 /*
9621 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9622 * or POSTED_INTR_WAKEUP_VECTOR.
9623 */
9624 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9625 vmx->pi_desc.sn = 1;
9626
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009627 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009628
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009629free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009630 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009631 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009632free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009633 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009634free_pml:
9635 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009636uninit_vcpu:
9637 kvm_vcpu_uninit(&vmx->vcpu);
9638free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009639 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009640 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009641 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009642}
9643
Yang, Sheng002c7f72007-07-31 14:23:01 +03009644static void __init vmx_check_processor_compat(void *rtn)
9645{
9646 struct vmcs_config vmcs_conf;
9647
9648 *(int *)rtn = 0;
9649 if (setup_vmcs_config(&vmcs_conf) < 0)
9650 *(int *)rtn = -EIO;
9651 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9652 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9653 smp_processor_id());
9654 *(int *)rtn = -EIO;
9655 }
9656}
9657
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009658static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009659{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009660 u8 cache;
9661 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009662
Sheng Yang522c68c2009-04-27 20:35:43 +08009663 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009664 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009665 * 2. EPT with VT-d:
9666 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009667 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009668 * b. VT-d with snooping control feature: snooping control feature of
9669 * VT-d engine can guarantee the cache correctness. Just set it
9670 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009671 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009672 * consistent with host MTRR
9673 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009674 if (is_mmio) {
9675 cache = MTRR_TYPE_UNCACHABLE;
9676 goto exit;
9677 }
9678
9679 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009680 ipat = VMX_EPT_IPAT_BIT;
9681 cache = MTRR_TYPE_WRBACK;
9682 goto exit;
9683 }
9684
9685 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9686 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009687 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009688 cache = MTRR_TYPE_WRBACK;
9689 else
9690 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009691 goto exit;
9692 }
9693
Xiao Guangrongff536042015-06-15 16:55:22 +08009694 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009695
9696exit:
9697 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009698}
9699
Sheng Yang17cc3932010-01-05 19:02:27 +08009700static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009701{
Sheng Yang878403b2010-01-05 19:02:29 +08009702 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9703 return PT_DIRECTORY_LEVEL;
9704 else
9705 /* For shadow and EPT supported 1GB page */
9706 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009707}
9708
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009709static void vmcs_set_secondary_exec_control(u32 new_ctl)
9710{
9711 /*
9712 * These bits in the secondary execution controls field
9713 * are dynamic, the others are mostly based on the hypervisor
9714 * architecture and the guest's CPUID. Do not touch the
9715 * dynamic bits.
9716 */
9717 u32 mask =
9718 SECONDARY_EXEC_SHADOW_VMCS |
9719 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02009720 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9721 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009722
9723 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9724
9725 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9726 (new_ctl & ~mask) | (cur_ctl & mask));
9727}
9728
David Matlack8322ebb2016-11-29 18:14:09 -08009729/*
9730 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
9731 * (indicating "allowed-1") if they are supported in the guest's CPUID.
9732 */
9733static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
9734{
9735 struct vcpu_vmx *vmx = to_vmx(vcpu);
9736 struct kvm_cpuid_entry2 *entry;
9737
9738 vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff;
9739 vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE;
9740
9741#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
9742 if (entry && (entry->_reg & (_cpuid_mask))) \
9743 vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \
9744} while (0)
9745
9746 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
9747 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
9748 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
9749 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
9750 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
9751 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
9752 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
9753 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
9754 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
9755 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
9756 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
9757 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
9758 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
9759 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
9760 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
9761
9762 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9763 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
9764 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
9765 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
9766 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +01009767 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -08009768
9769#undef cr4_fixed1_update
9770}
9771
Sheng Yang0e851882009-12-18 16:48:46 +08009772static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9773{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009774 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009775
Paolo Bonzini80154d72017-08-24 13:55:35 +02009776 if (cpu_has_secondary_exec_ctrls()) {
9777 vmx_compute_secondary_exec_control(vmx);
9778 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009779 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009780
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009781 if (nested_vmx_allowed(vcpu))
9782 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9783 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9784 else
9785 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9786 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -08009787
9788 if (nested_vmx_allowed(vcpu))
9789 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08009790}
9791
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009792static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9793{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009794 if (func == 1 && nested)
9795 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009796}
9797
Yang Zhang25d92082013-08-06 12:00:32 +03009798static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9799 struct x86_exception *fault)
9800{
Jan Kiszka533558b2014-01-04 18:47:20 +01009801 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -04009802 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +01009803 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -04009804 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +03009805
Bandan Dasc5f983f2017-05-05 15:25:14 -04009806 if (vmx->nested.pml_full) {
9807 exit_reason = EXIT_REASON_PML_FULL;
9808 vmx->nested.pml_full = false;
9809 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
9810 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009811 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009812 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009813 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -04009814
9815 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009816 vmcs12->guest_physical_address = fault->address;
9817}
9818
Peter Feiner995f00a2017-06-30 17:26:32 -07009819static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
9820{
David Hildenbrandbb97a012017-08-10 23:15:28 +02009821 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -07009822}
9823
Nadav Har'El155a97a2013-08-05 11:07:16 +03009824/* Callbacks for nested_ept_init_mmu_context: */
9825
9826static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9827{
9828 /* return the page table to be shadowed - in our case, EPT12 */
9829 return get_vmcs12(vcpu)->ept_pointer;
9830}
9831
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009832static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009833{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009834 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +02009835 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009836 return 1;
9837
9838 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +02009839 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009840 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009841 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +02009842 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +03009843 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9844 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9845 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9846
9847 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009848 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009849}
9850
9851static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9852{
9853 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9854}
9855
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009856static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9857 u16 error_code)
9858{
9859 bool inequality, bit;
9860
9861 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9862 inequality =
9863 (error_code & vmcs12->page_fault_error_code_mask) !=
9864 vmcs12->page_fault_error_code_match;
9865 return inequality ^ bit;
9866}
9867
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009868static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9869 struct x86_exception *fault)
9870{
9871 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9872
9873 WARN_ON(!is_guest_mode(vcpu));
9874
Wanpeng Li305d0ab2017-09-28 18:16:44 -07009875 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
9876 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02009877 vmcs12->vm_exit_intr_error_code = fault->error_code;
9878 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
9879 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
9880 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
9881 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +02009882 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009883 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +02009884 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009885}
9886
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009887static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9888 struct vmcs12 *vmcs12);
9889
9890static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009891 struct vmcs12 *vmcs12)
9892{
9893 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009894 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009895 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009896
9897 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009898 /*
9899 * Translate L1 physical address to host physical
9900 * address for vmcs02. Keep the page pinned, so this
9901 * physical address remains valid. We keep a reference
9902 * to it so we can release it later.
9903 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009904 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +02009905 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009906 vmx->nested.apic_access_page = NULL;
9907 }
9908 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009909 /*
9910 * If translation failed, no matter: This feature asks
9911 * to exit when accessing the given address, and if it
9912 * can never be accessed, this feature won't do
9913 * anything anyway.
9914 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009915 if (!is_error_page(page)) {
9916 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009917 hpa = page_to_phys(vmx->nested.apic_access_page);
9918 vmcs_write64(APIC_ACCESS_ADDR, hpa);
9919 } else {
9920 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
9921 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9922 }
9923 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
9924 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
9925 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
9926 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9927 kvm_vcpu_reload_apic_access_page(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009928 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009929
9930 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009931 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +02009932 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009933 vmx->nested.virtual_apic_page = NULL;
9934 }
9935 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009936
9937 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009938 * If translation failed, VM entry will fail because
9939 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
9940 * Failing the vm entry is _not_ what the processor
9941 * does but it's basically the only possibility we
9942 * have. We could still enter the guest if CR8 load
9943 * exits are enabled, CR8 store exits are enabled, and
9944 * virtualize APIC access is disabled; in this case
9945 * the processor would never use the TPR shadow and we
9946 * could simply clear the bit from the execution
9947 * control. But such a configuration is useless, so
9948 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009949 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009950 if (!is_error_page(page)) {
9951 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009952 hpa = page_to_phys(vmx->nested.virtual_apic_page);
9953 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
9954 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009955 }
9956
Wincy Van705699a2015-02-03 23:58:17 +08009957 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +08009958 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9959 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02009960 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009961 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +08009962 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009963 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
9964 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009965 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009966 vmx->nested.pi_desc_page = page;
9967 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08009968 vmx->nested.pi_desc =
9969 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9970 (unsigned long)(vmcs12->posted_intr_desc_addr &
9971 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009972 vmcs_write64(POSTED_INTR_DESC_ADDR,
9973 page_to_phys(vmx->nested.pi_desc_page) +
9974 (unsigned long)(vmcs12->posted_intr_desc_addr &
9975 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +08009976 }
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009977 if (cpu_has_vmx_msr_bitmap() &&
9978 nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS) &&
9979 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
9980 ;
9981 else
9982 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
9983 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009984}
9985
Jan Kiszkaf41245002014-03-07 20:03:13 +01009986static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9987{
9988 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9989 struct vcpu_vmx *vmx = to_vmx(vcpu);
9990
9991 if (vcpu->arch.virtual_tsc_khz == 0)
9992 return;
9993
9994 /* Make sure short timeouts reliably trigger an immediate vmexit.
9995 * hrtimer_start does not guarantee this. */
9996 if (preemption_timeout <= 1) {
9997 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9998 return;
9999 }
10000
10001 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10002 preemption_timeout *= 1000000;
10003 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10004 hrtimer_start(&vmx->nested.preemption_timer,
10005 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10006}
10007
Jim Mattson56a20512017-07-06 16:33:06 -070010008static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10009 struct vmcs12 *vmcs12)
10010{
10011 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10012 return 0;
10013
10014 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10015 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10016 return -EINVAL;
10017
10018 return 0;
10019}
10020
Wincy Van3af18d92015-02-03 23:49:31 +080010021static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10022 struct vmcs12 *vmcs12)
10023{
Wincy Van3af18d92015-02-03 23:49:31 +080010024 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10025 return 0;
10026
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010027 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010028 return -EINVAL;
10029
10030 return 0;
10031}
10032
Jim Mattson712b12d2017-08-24 13:24:47 -070010033static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10034 struct vmcs12 *vmcs12)
10035{
10036 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10037 return 0;
10038
10039 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10040 return -EINVAL;
10041
10042 return 0;
10043}
10044
Wincy Van3af18d92015-02-03 23:49:31 +080010045/*
10046 * Merge L0's and L1's MSR bitmap, return false to indicate that
10047 * we do not use the hardware.
10048 */
10049static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
10050 struct vmcs12 *vmcs12)
10051{
Wincy Van82f0dd42015-02-03 23:57:18 +080010052 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010053 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010054 unsigned long *msr_bitmap_l1;
10055 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap;
Wincy Vanf2b93282015-02-03 23:56:03 +080010056
Radim Krčmářd048c092016-08-08 20:16:22 +020010057 /* This shortcut is ok because we support only x2APIC MSRs so far. */
Wincy Vanf2b93282015-02-03 23:56:03 +080010058 if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
10059 return false;
10060
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010061 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10062 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010063 return false;
Radim Krčmářd048c092016-08-08 20:16:22 +020010064 msr_bitmap_l1 = (unsigned long *)kmap(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010065
Radim Krčmářd048c092016-08-08 20:16:22 +020010066 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
10067
Wincy Vanf2b93282015-02-03 23:56:03 +080010068 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +080010069 if (nested_cpu_has_apic_reg_virt(vmcs12))
10070 for (msr = 0x800; msr <= 0x8ff; msr++)
10071 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +020010072 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +080010073 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +020010074
10075 nested_vmx_disable_intercept_for_msr(
10076 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +080010077 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
10078 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +020010079
Wincy Van608406e2015-02-03 23:57:51 +080010080 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +080010081 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +020010082 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +080010083 APIC_BASE_MSR + (APIC_EOI >> 4),
10084 MSR_TYPE_W);
10085 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +020010086 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +080010087 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
10088 MSR_TYPE_W);
10089 }
Wincy Van82f0dd42015-02-03 23:57:18 +080010090 }
Wincy Vanf2b93282015-02-03 23:56:03 +080010091 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010092 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010093
10094 return true;
10095}
10096
10097static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10098 struct vmcs12 *vmcs12)
10099{
Wincy Van82f0dd42015-02-03 23:57:18 +080010100 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010101 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010102 !nested_cpu_has_vid(vmcs12) &&
10103 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010104 return 0;
10105
10106 /*
10107 * If virtualize x2apic mode is enabled,
10108 * virtualize apic access must be disabled.
10109 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010110 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10111 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010112 return -EINVAL;
10113
Wincy Van608406e2015-02-03 23:57:51 +080010114 /*
10115 * If virtual interrupt delivery is enabled,
10116 * we must exit on external interrupts.
10117 */
10118 if (nested_cpu_has_vid(vmcs12) &&
10119 !nested_exit_on_intr(vcpu))
10120 return -EINVAL;
10121
Wincy Van705699a2015-02-03 23:58:17 +080010122 /*
10123 * bits 15:8 should be zero in posted_intr_nv,
10124 * the descriptor address has been already checked
10125 * in nested_get_vmcs12_pages.
10126 */
10127 if (nested_cpu_has_posted_intr(vmcs12) &&
10128 (!nested_cpu_has_vid(vmcs12) ||
10129 !nested_exit_intr_ack_set(vcpu) ||
10130 vmcs12->posted_intr_nv & 0xff00))
10131 return -EINVAL;
10132
Wincy Vanf2b93282015-02-03 23:56:03 +080010133 /* tpr shadow is needed by all apicv features. */
10134 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10135 return -EINVAL;
10136
10137 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010138}
10139
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010140static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10141 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010142 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010143{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010144 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010145 u64 count, addr;
10146
10147 if (vmcs12_read_any(vcpu, count_field, &count) ||
10148 vmcs12_read_any(vcpu, addr_field, &addr)) {
10149 WARN_ON(1);
10150 return -EINVAL;
10151 }
10152 if (count == 0)
10153 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010154 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010155 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10156 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010157 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010158 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10159 addr_field, maxphyaddr, count, addr);
10160 return -EINVAL;
10161 }
10162 return 0;
10163}
10164
10165static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10166 struct vmcs12 *vmcs12)
10167{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010168 if (vmcs12->vm_exit_msr_load_count == 0 &&
10169 vmcs12->vm_exit_msr_store_count == 0 &&
10170 vmcs12->vm_entry_msr_load_count == 0)
10171 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010172 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010173 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010174 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010175 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010176 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010177 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010178 return -EINVAL;
10179 return 0;
10180}
10181
Bandan Dasc5f983f2017-05-05 15:25:14 -040010182static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10183 struct vmcs12 *vmcs12)
10184{
10185 u64 address = vmcs12->pml_address;
10186 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10187
10188 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10189 if (!nested_cpu_has_ept(vmcs12) ||
10190 !IS_ALIGNED(address, 4096) ||
10191 address >> maxphyaddr)
10192 return -EINVAL;
10193 }
10194
10195 return 0;
10196}
10197
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010198static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10199 struct vmx_msr_entry *e)
10200{
10201 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010202 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010203 return -EINVAL;
10204 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10205 e->index == MSR_IA32_UCODE_REV)
10206 return -EINVAL;
10207 if (e->reserved != 0)
10208 return -EINVAL;
10209 return 0;
10210}
10211
10212static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10213 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010214{
10215 if (e->index == MSR_FS_BASE ||
10216 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010217 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10218 nested_vmx_msr_check_common(vcpu, e))
10219 return -EINVAL;
10220 return 0;
10221}
10222
10223static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10224 struct vmx_msr_entry *e)
10225{
10226 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10227 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010228 return -EINVAL;
10229 return 0;
10230}
10231
10232/*
10233 * Load guest's/host's msr at nested entry/exit.
10234 * return 0 for success, entry index for failure.
10235 */
10236static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10237{
10238 u32 i;
10239 struct vmx_msr_entry e;
10240 struct msr_data msr;
10241
10242 msr.host_initiated = false;
10243 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010244 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10245 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010246 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010247 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10248 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010249 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010250 }
10251 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010252 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010253 "%s check failed (%u, 0x%x, 0x%x)\n",
10254 __func__, i, e.index, e.reserved);
10255 goto fail;
10256 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010257 msr.index = e.index;
10258 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010259 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010260 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010261 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10262 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010263 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010264 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010265 }
10266 return 0;
10267fail:
10268 return i + 1;
10269}
10270
10271static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10272{
10273 u32 i;
10274 struct vmx_msr_entry e;
10275
10276 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010277 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010278 if (kvm_vcpu_read_guest(vcpu,
10279 gpa + i * sizeof(e),
10280 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010281 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010282 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10283 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010284 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010285 }
10286 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010287 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010288 "%s check failed (%u, 0x%x, 0x%x)\n",
10289 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010290 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010291 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010292 msr_info.host_initiated = false;
10293 msr_info.index = e.index;
10294 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010295 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010296 "%s cannot read MSR (%u, 0x%x)\n",
10297 __func__, i, e.index);
10298 return -EINVAL;
10299 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010300 if (kvm_vcpu_write_guest(vcpu,
10301 gpa + i * sizeof(e) +
10302 offsetof(struct vmx_msr_entry, value),
10303 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010304 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010305 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010306 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010307 return -EINVAL;
10308 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010309 }
10310 return 0;
10311}
10312
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010313static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10314{
10315 unsigned long invalid_mask;
10316
10317 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10318 return (val & invalid_mask) == 0;
10319}
10320
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010321/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010322 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10323 * emulating VM entry into a guest with EPT enabled.
10324 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10325 * is assigned to entry_failure_code on failure.
10326 */
10327static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010328 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010329{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010330 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010331 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010332 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10333 return 1;
10334 }
10335
10336 /*
10337 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10338 * must not be dereferenced.
10339 */
10340 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10341 !nested_ept) {
10342 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10343 *entry_failure_code = ENTRY_FAIL_PDPTE;
10344 return 1;
10345 }
10346 }
10347
10348 vcpu->arch.cr3 = cr3;
10349 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10350 }
10351
10352 kvm_mmu_reset_context(vcpu);
10353 return 0;
10354}
10355
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010356/*
10357 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10358 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010359 * 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 +030010360 * guest in a way that will both be appropriate to L1's requests, and our
10361 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10362 * function also has additional necessary side-effects, like setting various
10363 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010010364 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10365 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010366 */
Ladi Prosekee146c12016-11-30 16:03:09 +010010367static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010368 bool from_vmentry, u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010369{
10370 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040010371 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010372
10373 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10374 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
10375 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10376 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10377 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10378 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10379 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10380 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10381 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10382 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
10383 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10384 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10385 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10386 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10387 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10388 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10389 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10390 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10391 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10392 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
10393 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10394 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10395 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10396 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10397 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10398 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10399 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10400 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
10401 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10402 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10403 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10404 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10405 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10406 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10407 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10408 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10409
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010410 if (from_vmentry &&
10411 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020010412 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10413 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10414 } else {
10415 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10416 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10417 }
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010418 if (from_vmentry) {
10419 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10420 vmcs12->vm_entry_intr_info_field);
10421 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10422 vmcs12->vm_entry_exception_error_code);
10423 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10424 vmcs12->vm_entry_instruction_len);
10425 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10426 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070010427 vmx->loaded_vmcs->nmi_known_unmasked =
10428 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010429 } else {
10430 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10431 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010432 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +030010433 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010434 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10435 vmcs12->guest_pending_dbg_exceptions);
10436 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10437 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10438
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010439 if (nested_cpu_has_xsaves(vmcs12))
10440 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010441 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10442
Jan Kiszkaf41245002014-03-07 20:03:13 +010010443 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010444
Paolo Bonzini9314006db2016-07-06 13:23:51 +020010445 /* Preemption timer setting is only taken from vmcs01. */
10446 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10447 exec_control |= vmcs_config.pin_based_exec_ctrl;
10448 if (vmx->hv_deadline_tsc == -1)
10449 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10450
10451 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010452 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010453 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10454 vmx->nested.pi_pending = false;
Wincy Van06a55242017-04-28 13:13:59 +080010455 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010456 } else {
Wincy Van705699a2015-02-03 23:58:17 +080010457 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010458 }
Wincy Van705699a2015-02-03 23:58:17 +080010459
Jan Kiszkaf41245002014-03-07 20:03:13 +010010460 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010461
Jan Kiszkaf41245002014-03-07 20:03:13 +010010462 vmx->nested.preemption_timer_expired = false;
10463 if (nested_cpu_has_preemption_timer(vmcs12))
10464 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010465
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010466 /*
10467 * Whether page-faults are trapped is determined by a combination of
10468 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10469 * If enable_ept, L0 doesn't care about page faults and we should
10470 * set all of these to L1's desires. However, if !enable_ept, L0 does
10471 * care about (at least some) page faults, and because it is not easy
10472 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10473 * to exit on each and every L2 page fault. This is done by setting
10474 * MASK=MATCH=0 and (see below) EB.PF=1.
10475 * Note that below we don't need special code to set EB.PF beyond the
10476 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10477 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10478 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010479 */
10480 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10481 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10482 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10483 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10484
10485 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020010486 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080010487
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010488 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010489 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020010490 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010491 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020010492 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010493 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040010494 SECONDARY_EXEC_APIC_REGISTER_VIRT |
10495 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010496 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040010497 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
10498 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
10499 ~SECONDARY_EXEC_ENABLE_PML;
10500 exec_control |= vmcs12_exec_ctrl;
10501 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010502
Bandan Das27c42a12017-08-03 15:54:42 -040010503 /* All VMFUNCs are currently emulated through L0 vmexits. */
10504 if (exec_control & SECONDARY_EXEC_ENABLE_VMFUNC)
10505 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10506
Wincy Van608406e2015-02-03 23:57:51 +080010507 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
10508 vmcs_write64(EOI_EXIT_BITMAP0,
10509 vmcs12->eoi_exit_bitmap0);
10510 vmcs_write64(EOI_EXIT_BITMAP1,
10511 vmcs12->eoi_exit_bitmap1);
10512 vmcs_write64(EOI_EXIT_BITMAP2,
10513 vmcs12->eoi_exit_bitmap2);
10514 vmcs_write64(EOI_EXIT_BITMAP3,
10515 vmcs12->eoi_exit_bitmap3);
10516 vmcs_write16(GUEST_INTR_STATUS,
10517 vmcs12->guest_intr_status);
10518 }
10519
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010520 /*
10521 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10522 * nested_get_vmcs12_pages will either fix it up or
10523 * remove the VM execution control.
10524 */
10525 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
10526 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
10527
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010528 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10529 }
10530
10531
10532 /*
10533 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10534 * Some constant fields are set here by vmx_set_constant_host_state().
10535 * Other fields are different per CPU, and will be set later when
10536 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10537 */
Yang Zhanga547c6d2013-04-11 19:25:10 +080010538 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010539
10540 /*
Jim Mattson83bafef2016-10-04 10:48:38 -070010541 * Set the MSR load/store lists to match L0's settings.
10542 */
10543 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10544 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10545 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10546 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10547 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10548
10549 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010550 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10551 * entry, but only if the current (host) sp changed from the value
10552 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10553 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10554 * here we just force the write to happen on entry.
10555 */
10556 vmx->host_rsp = 0;
10557
10558 exec_control = vmx_exec_control(vmx); /* L0's desires */
10559 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10560 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10561 exec_control &= ~CPU_BASED_TPR_SHADOW;
10562 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010563
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010564 /*
10565 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
10566 * nested_get_vmcs12_pages can't fix it up, the illegal value
10567 * will result in a VM entry failure.
10568 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010569 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010570 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010571 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070010572 } else {
10573#ifdef CONFIG_X86_64
10574 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10575 CPU_BASED_CR8_STORE_EXITING;
10576#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010577 }
10578
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010579 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080010580 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
10581 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010582 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010583 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10584 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10585
10586 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10587
10588 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10589 * bitwise-or of what L1 wants to trap for L2, and what we want to
10590 * trap. Note that CR0.TS also needs updating - we do this later.
10591 */
10592 update_exception_bitmap(vcpu);
10593 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10594 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10595
Nadav Har'El8049d652013-08-05 11:07:06 +030010596 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10597 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10598 * bits are further modified by vmx_set_efer() below.
10599 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010010600 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010601
10602 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10603 * emulated by vmx_set_efer(), below.
10604 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010605 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010606 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10607 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010608 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10609
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010610 if (from_vmentry &&
10611 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010612 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010613 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010614 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010615 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010616 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010617
10618 set_cr4_guest_host_mask(vmx);
10619
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010620 if (from_vmentry &&
10621 vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010622 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10623
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010624 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10625 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010626 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010627 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010628 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010629 if (kvm_has_tsc_control)
10630 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010631
10632 if (enable_vpid) {
10633 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010634 * There is no direct mapping between vpid02 and vpid12, the
10635 * vpid02 is per-vCPU for L0 and reused while the value of
10636 * vpid12 is changed w/ one invvpid during nested vmentry.
10637 * The vpid12 is allocated by L1 for L2, so it will not
10638 * influence global bitmap(for vpid01 and vpid02 allocation)
10639 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010640 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010641 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10642 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10643 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10644 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080010645 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070010646 }
10647 } else {
10648 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080010649 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070010650 }
10651
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010652 }
10653
Ladi Prosek1fb883b2017-04-04 14:18:53 +020010654 if (enable_pml) {
10655 /*
10656 * Conceptually we want to copy the PML address and index from
10657 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10658 * since we always flush the log on each vmexit, this happens
10659 * to be equivalent to simply resetting the fields in vmcs02.
10660 */
10661 ASSERT(vmx->pml_pg);
10662 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10663 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10664 }
10665
Nadav Har'El155a97a2013-08-05 11:07:16 +030010666 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010667 if (nested_ept_init_mmu_context(vcpu)) {
10668 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10669 return 1;
10670 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010671 } else if (nested_cpu_has2(vmcs12,
10672 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10673 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010674 }
10675
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010676 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080010677 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
10678 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010679 * The CR0_READ_SHADOW is what L2 should have expected to read given
10680 * the specifications by L1; It's not enough to take
10681 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10682 * have more bits than L1 expected.
10683 */
10684 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10685 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10686
10687 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10688 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10689
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010690 if (from_vmentry &&
10691 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080010692 vcpu->arch.efer = vmcs12->guest_ia32_efer;
10693 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
10694 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10695 else
10696 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10697 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10698 vmx_set_efer(vcpu, vcpu->arch.efer);
10699
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010700 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010010701 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010702 entry_failure_code))
10703 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010010704
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010705 if (!enable_ept)
10706 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10707
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010708 /*
10709 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10710 */
10711 if (enable_ept) {
10712 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10713 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10714 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10715 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10716 }
10717
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010718 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10719 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010010720 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010721}
10722
Jim Mattsonca0bde22016-11-30 12:03:46 -080010723static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10724{
10725 struct vcpu_vmx *vmx = to_vmx(vcpu);
10726
10727 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10728 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
10729 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10730
Jim Mattson56a20512017-07-06 16:33:06 -070010731 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
10732 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10733
Jim Mattsonca0bde22016-11-30 12:03:46 -080010734 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
10735 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10736
Jim Mattson712b12d2017-08-24 13:24:47 -070010737 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
10738 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10739
Jim Mattsonca0bde22016-11-30 12:03:46 -080010740 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
10741 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10742
10743 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
10744 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10745
Bandan Dasc5f983f2017-05-05 15:25:14 -040010746 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
10747 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10748
Jim Mattsonca0bde22016-11-30 12:03:46 -080010749 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
10750 vmx->nested.nested_vmx_procbased_ctls_low,
10751 vmx->nested.nested_vmx_procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070010752 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
10753 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
10754 vmx->nested.nested_vmx_secondary_ctls_low,
10755 vmx->nested.nested_vmx_secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080010756 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
10757 vmx->nested.nested_vmx_pinbased_ctls_low,
10758 vmx->nested.nested_vmx_pinbased_ctls_high) ||
10759 !vmx_control_verify(vmcs12->vm_exit_controls,
10760 vmx->nested.nested_vmx_exit_ctls_low,
10761 vmx->nested.nested_vmx_exit_ctls_high) ||
10762 !vmx_control_verify(vmcs12->vm_entry_controls,
10763 vmx->nested.nested_vmx_entry_ctls_low,
10764 vmx->nested.nested_vmx_entry_ctls_high))
10765 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10766
Bandan Das41ab9372017-08-03 15:54:43 -040010767 if (nested_cpu_has_vmfunc(vmcs12)) {
10768 if (vmcs12->vm_function_control &
10769 ~vmx->nested.nested_vmx_vmfunc_controls)
10770 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10771
10772 if (nested_cpu_has_eptp_switching(vmcs12)) {
10773 if (!nested_cpu_has_ept(vmcs12) ||
10774 !page_address_valid(vcpu, vmcs12->eptp_list_address))
10775 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10776 }
10777 }
Bandan Das27c42a12017-08-03 15:54:42 -040010778
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070010779 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
10780 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10781
Jim Mattsonca0bde22016-11-30 12:03:46 -080010782 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
10783 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
10784 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
10785 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
10786
10787 return 0;
10788}
10789
10790static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10791 u32 *exit_qual)
10792{
10793 bool ia32e;
10794
10795 *exit_qual = ENTRY_FAIL_DEFAULT;
10796
10797 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
10798 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
10799 return 1;
10800
10801 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
10802 vmcs12->vmcs_link_pointer != -1ull) {
10803 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
10804 return 1;
10805 }
10806
10807 /*
10808 * If the load IA32_EFER VM-entry control is 1, the following checks
10809 * are performed on the field for the IA32_EFER MSR:
10810 * - Bits reserved in the IA32_EFER MSR must be 0.
10811 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10812 * the IA-32e mode guest VM-exit control. It must also be identical
10813 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10814 * CR0.PG) is 1.
10815 */
10816 if (to_vmx(vcpu)->nested.nested_run_pending &&
10817 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
10818 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10819 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10820 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10821 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10822 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
10823 return 1;
10824 }
10825
10826 /*
10827 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10828 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10829 * the values of the LMA and LME bits in the field must each be that of
10830 * the host address-space size VM-exit control.
10831 */
10832 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10833 ia32e = (vmcs12->vm_exit_controls &
10834 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10835 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10836 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10837 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
10838 return 1;
10839 }
10840
Wanpeng Lif1b026a2017-11-05 16:54:48 -080010841 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
10842 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
10843 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
10844 return 1;
10845
Jim Mattsonca0bde22016-11-30 12:03:46 -080010846 return 0;
10847}
10848
Jim Mattson858e25c2016-11-30 12:03:47 -080010849static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
10850{
10851 struct vcpu_vmx *vmx = to_vmx(vcpu);
10852 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080010853 u32 msr_entry_idx;
10854 u32 exit_qual;
10855
Jim Mattson858e25c2016-11-30 12:03:47 -080010856 enter_guest_mode(vcpu);
10857
10858 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10859 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10860
Jim Mattson00647b42017-11-27 17:22:25 -060010861 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080010862 vmx_segment_cache_clear(vmx);
10863
10864 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
10865 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010866 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080010867 nested_vmx_entry_failure(vcpu, vmcs12,
10868 EXIT_REASON_INVALID_STATE, exit_qual);
10869 return 1;
10870 }
10871
10872 nested_get_vmcs12_pages(vcpu, vmcs12);
10873
10874 msr_entry_idx = nested_vmx_load_msr(vcpu,
10875 vmcs12->vm_entry_msr_load_addr,
10876 vmcs12->vm_entry_msr_load_count);
10877 if (msr_entry_idx) {
10878 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010879 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080010880 nested_vmx_entry_failure(vcpu, vmcs12,
10881 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10882 return 1;
10883 }
10884
Jim Mattson858e25c2016-11-30 12:03:47 -080010885 /*
10886 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10887 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10888 * returned as far as L1 is concerned. It will only return (and set
10889 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10890 */
10891 return 0;
10892}
10893
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010894/*
10895 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10896 * for running an L2 nested guest.
10897 */
10898static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10899{
10900 struct vmcs12 *vmcs12;
10901 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070010902 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080010903 u32 exit_qual;
10904 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010905
Kyle Hueyeb277562016-11-29 12:40:39 -080010906 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010907 return 1;
10908
Kyle Hueyeb277562016-11-29 12:40:39 -080010909 if (!nested_vmx_check_vmcs12(vcpu))
10910 goto out;
10911
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010912 vmcs12 = get_vmcs12(vcpu);
10913
Abel Gordon012f83c2013-04-18 14:39:25 +030010914 if (enable_shadow_vmcs)
10915 copy_shadow_to_vmcs12(vmx);
10916
Nadav Har'El7c177932011-05-25 23:12:04 +030010917 /*
10918 * The nested entry process starts with enforcing various prerequisites
10919 * on vmcs12 as required by the Intel SDM, and act appropriately when
10920 * they fail: As the SDM explains, some conditions should cause the
10921 * instruction to fail, while others will cause the instruction to seem
10922 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10923 * To speed up the normal (success) code path, we should avoid checking
10924 * for misconfigurations which will anyway be caught by the processor
10925 * when using the merged vmcs02.
10926 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070010927 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
10928 nested_vmx_failValid(vcpu,
10929 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
10930 goto out;
10931 }
10932
Nadav Har'El7c177932011-05-25 23:12:04 +030010933 if (vmcs12->launch_state == launch) {
10934 nested_vmx_failValid(vcpu,
10935 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10936 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080010937 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030010938 }
10939
Jim Mattsonca0bde22016-11-30 12:03:46 -080010940 ret = check_vmentry_prereqs(vcpu, vmcs12);
10941 if (ret) {
10942 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080010943 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010944 }
10945
Nadav Har'El7c177932011-05-25 23:12:04 +030010946 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080010947 * After this point, the trap flag no longer triggers a singlestep trap
10948 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
10949 * This is not 100% correct; for performance reasons, we delegate most
10950 * of the checks on host state to the processor. If those fail,
10951 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020010952 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080010953 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020010954
Jim Mattsonca0bde22016-11-30 12:03:46 -080010955 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
10956 if (ret) {
10957 nested_vmx_entry_failure(vcpu, vmcs12,
10958 EXIT_REASON_INVALID_STATE, exit_qual);
10959 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020010960 }
10961
10962 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010963 * We're finally done with prerequisite checking, and can start with
10964 * the nested entry.
10965 */
10966
Jim Mattson858e25c2016-11-30 12:03:47 -080010967 ret = enter_vmx_non_root_mode(vcpu, true);
10968 if (ret)
10969 return ret;
Wincy Vanff651cb2014-12-11 08:52:58 +030010970
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010971 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010972 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010973
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010974 vmx->nested.nested_run_pending = 1;
10975
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010976 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080010977
10978out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080010979 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010980}
10981
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010982/*
10983 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10984 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10985 * This function returns the new value we should put in vmcs12.guest_cr0.
10986 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10987 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10988 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10989 * didn't trap the bit, because if L1 did, so would L0).
10990 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10991 * been modified by L2, and L1 knows it. So just leave the old value of
10992 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10993 * isn't relevant, because if L0 traps this bit it can set it to anything.
10994 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10995 * changed these bits, and therefore they need to be updated, but L0
10996 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10997 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10998 */
10999static inline unsigned long
11000vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11001{
11002 return
11003 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11004 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11005 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11006 vcpu->arch.cr0_guest_owned_bits));
11007}
11008
11009static inline unsigned long
11010vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11011{
11012 return
11013 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11014 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11015 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11016 vcpu->arch.cr4_guest_owned_bits));
11017}
11018
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011019static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11020 struct vmcs12 *vmcs12)
11021{
11022 u32 idt_vectoring;
11023 unsigned int nr;
11024
Wanpeng Li664f8e22017-08-24 03:35:09 -070011025 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011026 nr = vcpu->arch.exception.nr;
11027 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11028
11029 if (kvm_exception_is_soft(nr)) {
11030 vmcs12->vm_exit_instruction_len =
11031 vcpu->arch.event_exit_inst_len;
11032 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11033 } else
11034 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11035
11036 if (vcpu->arch.exception.has_error_code) {
11037 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11038 vmcs12->idt_vectoring_error_code =
11039 vcpu->arch.exception.error_code;
11040 }
11041
11042 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011043 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011044 vmcs12->idt_vectoring_info_field =
11045 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
11046 } else if (vcpu->arch.interrupt.pending) {
11047 nr = vcpu->arch.interrupt.nr;
11048 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11049
11050 if (vcpu->arch.interrupt.soft) {
11051 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11052 vmcs12->vm_entry_instruction_len =
11053 vcpu->arch.event_exit_inst_len;
11054 } else
11055 idt_vectoring |= INTR_TYPE_EXT_INTR;
11056
11057 vmcs12->idt_vectoring_info_field = idt_vectoring;
11058 }
11059}
11060
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011061static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11062{
11063 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011064 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020011065 bool block_nested_events =
11066 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011067
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011068 if (vcpu->arch.exception.pending &&
11069 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020011070 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011071 return -EBUSY;
11072 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011073 return 0;
11074 }
11075
Jan Kiszkaf41245002014-03-07 20:03:13 +010011076 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11077 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020011078 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010011079 return -EBUSY;
11080 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11081 return 0;
11082 }
11083
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011084 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011085 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011086 return -EBUSY;
11087 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11088 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11089 INTR_INFO_VALID_MASK, 0);
11090 /*
11091 * The NMI-triggered VM exit counts as injection:
11092 * clear this one and block further NMIs.
11093 */
11094 vcpu->arch.nmi_pending = 0;
11095 vmx_set_nmi_mask(vcpu, true);
11096 return 0;
11097 }
11098
11099 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11100 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011101 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011102 return -EBUSY;
11103 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080011104 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011105 }
11106
David Hildenbrand6342c502017-01-25 11:58:58 +010011107 vmx_complete_nested_posted_interrupt(vcpu);
11108 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011109}
11110
Jan Kiszkaf41245002014-03-07 20:03:13 +010011111static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11112{
11113 ktime_t remaining =
11114 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11115 u64 value;
11116
11117 if (ktime_to_ns(remaining) <= 0)
11118 return 0;
11119
11120 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11121 do_div(value, 1000000);
11122 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11123}
11124
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011125/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011126 * Update the guest state fields of vmcs12 to reflect changes that
11127 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11128 * VM-entry controls is also updated, since this is really a guest
11129 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011130 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011131static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011132{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011133 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11134 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11135
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011136 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11137 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11138 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11139
11140 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11141 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11142 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11143 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11144 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11145 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11146 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11147 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11148 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11149 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11150 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11151 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11152 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11153 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11154 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11155 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11156 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11157 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11158 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11159 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11160 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11161 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11162 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11163 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11164 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11165 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11166 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11167 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11168 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11169 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11170 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11171 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11172 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11173 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11174 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11175 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11176
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011177 vmcs12->guest_interruptibility_info =
11178 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11179 vmcs12->guest_pending_dbg_exceptions =
11180 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010011181 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11182 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11183 else
11184 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011185
Jan Kiszkaf41245002014-03-07 20:03:13 +010011186 if (nested_cpu_has_preemption_timer(vmcs12)) {
11187 if (vmcs12->vm_exit_controls &
11188 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11189 vmcs12->vmx_preemption_timer_value =
11190 vmx_get_preemption_timer_value(vcpu);
11191 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11192 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080011193
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011194 /*
11195 * In some cases (usually, nested EPT), L2 is allowed to change its
11196 * own CR3 without exiting. If it has changed it, we must keep it.
11197 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11198 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11199 *
11200 * Additionally, restore L2's PDPTR to vmcs12.
11201 */
11202 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010011203 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011204 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11205 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11206 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11207 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11208 }
11209
Jim Mattsond281e132017-06-01 12:44:46 -070011210 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011211
Wincy Van608406e2015-02-03 23:57:51 +080011212 if (nested_cpu_has_vid(vmcs12))
11213 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11214
Jan Kiszkac18911a2013-03-13 16:06:41 +010011215 vmcs12->vm_entry_controls =
11216 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011217 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011218
Jan Kiszka2996fca2014-06-16 13:59:43 +020011219 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11220 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11221 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11222 }
11223
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011224 /* TODO: These cannot have changed unless we have MSR bitmaps and
11225 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011226 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011227 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011228 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11229 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011230 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11231 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11232 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011233 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011234 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011235}
11236
11237/*
11238 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11239 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11240 * and this function updates it to reflect the changes to the guest state while
11241 * L2 was running (and perhaps made some exits which were handled directly by L0
11242 * without going back to L1), and to reflect the exit reason.
11243 * Note that we do not have to copy here all VMCS fields, just those that
11244 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11245 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11246 * which already writes to vmcs12 directly.
11247 */
11248static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11249 u32 exit_reason, u32 exit_intr_info,
11250 unsigned long exit_qualification)
11251{
11252 /* update guest state fields: */
11253 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011254
11255 /* update exit information fields: */
11256
Jan Kiszka533558b2014-01-04 18:47:20 +010011257 vmcs12->vm_exit_reason = exit_reason;
11258 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010011259 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020011260
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011261 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011262 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11263 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11264
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011265 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070011266 vmcs12->launch_state = 1;
11267
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011268 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11269 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011270 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011271
11272 /*
11273 * Transfer the event that L0 or L1 may wanted to inject into
11274 * L2 to IDT_VECTORING_INFO_FIELD.
11275 */
11276 vmcs12_save_pending_event(vcpu, vmcs12);
11277 }
11278
11279 /*
11280 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11281 * preserved above and would only end up incorrectly in L1.
11282 */
11283 vcpu->arch.nmi_injected = false;
11284 kvm_clear_exception_queue(vcpu);
11285 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011286}
11287
Wanpeng Li5af41572017-11-05 16:54:49 -080011288static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11289 struct vmcs12 *vmcs12)
11290{
11291 u32 entry_failure_code;
11292
11293 nested_ept_uninit_mmu_context(vcpu);
11294
11295 /*
11296 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11297 * couldn't have changed.
11298 */
11299 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11300 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11301
11302 if (!enable_ept)
11303 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11304}
11305
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011306/*
11307 * A part of what we need to when the nested L2 guest exits and we want to
11308 * run its L1 parent, is to reset L1's guest state to the host state specified
11309 * in vmcs12.
11310 * This function is to be called not only on normal nested exit, but also on
11311 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11312 * Failures During or After Loading Guest State").
11313 * This function should be called when the active VMCS is L1's (vmcs01).
11314 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011315static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11316 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011317{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011318 struct kvm_segment seg;
11319
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011320 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11321 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011322 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011323 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11324 else
11325 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11326 vmx_set_efer(vcpu, vcpu->arch.efer);
11327
11328 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11329 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011330 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011331 /*
11332 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011333 * actually changed, because vmx_set_cr0 refers to efer set above.
11334 *
11335 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11336 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011337 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011338 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020011339 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011340
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011341 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011342 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080011343 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011344
Wanpeng Li5af41572017-11-05 16:54:49 -080011345 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011346
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011347 if (enable_vpid) {
11348 /*
11349 * Trivially support vpid by letting L2s share their parent
11350 * L1's vpid. TODO: move to a more elaborate solution, giving
11351 * each L2 its own vpid and exposing the vpid feature to L1.
11352 */
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011353 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011354 }
Wincy Van06a55242017-04-28 13:13:59 +080011355 /* Restore posted intr vector. */
11356 if (nested_cpu_has_posted_intr(vmcs12))
11357 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011358
11359 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11360 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11361 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11362 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11363 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d5512017-10-11 16:54:42 +020011364 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11365 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011366
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011367 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11368 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11369 vmcs_write64(GUEST_BNDCFGS, 0);
11370
Jan Kiszka44811c02013-08-04 17:17:27 +020011371 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011372 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011373 vcpu->arch.pat = vmcs12->host_ia32_pat;
11374 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011375 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11376 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11377 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011378
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011379 /* Set L1 segment info according to Intel SDM
11380 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11381 seg = (struct kvm_segment) {
11382 .base = 0,
11383 .limit = 0xFFFFFFFF,
11384 .selector = vmcs12->host_cs_selector,
11385 .type = 11,
11386 .present = 1,
11387 .s = 1,
11388 .g = 1
11389 };
11390 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11391 seg.l = 1;
11392 else
11393 seg.db = 1;
11394 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11395 seg = (struct kvm_segment) {
11396 .base = 0,
11397 .limit = 0xFFFFFFFF,
11398 .type = 3,
11399 .present = 1,
11400 .s = 1,
11401 .db = 1,
11402 .g = 1
11403 };
11404 seg.selector = vmcs12->host_ds_selector;
11405 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11406 seg.selector = vmcs12->host_es_selector;
11407 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11408 seg.selector = vmcs12->host_ss_selector;
11409 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11410 seg.selector = vmcs12->host_fs_selector;
11411 seg.base = vmcs12->host_fs_base;
11412 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11413 seg.selector = vmcs12->host_gs_selector;
11414 seg.base = vmcs12->host_gs_base;
11415 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11416 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011417 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011418 .limit = 0x67,
11419 .selector = vmcs12->host_tr_selector,
11420 .type = 11,
11421 .present = 1
11422 };
11423 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11424
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011425 kvm_set_dr(vcpu, 7, 0x400);
11426 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011427
Wincy Van3af18d92015-02-03 23:49:31 +080011428 if (cpu_has_vmx_msr_bitmap())
11429 vmx_set_msr_bitmap(vcpu);
11430
Wincy Vanff651cb2014-12-11 08:52:58 +030011431 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11432 vmcs12->vm_exit_msr_load_count))
11433 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011434}
11435
11436/*
11437 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11438 * and modify vmcs12 to make it see what it would expect to see there if
11439 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11440 */
Jan Kiszka533558b2014-01-04 18:47:20 +010011441static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11442 u32 exit_intr_info,
11443 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011444{
11445 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011446 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11447
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011448 /* trying to cancel vmlaunch/vmresume is a bug */
11449 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11450
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011451 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070011452 * The only expected VM-instruction error is "VM entry with
11453 * invalid control field(s)." Anything else indicates a
11454 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011455 */
Jim Mattson4f350c62017-09-14 16:31:44 -070011456 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
11457 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
11458
11459 leave_guest_mode(vcpu);
11460
11461 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011462 if (exit_reason == -1)
11463 sync_vmcs12(vcpu, vmcs12);
11464 else
11465 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11466 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070011467
11468 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11469 vmcs12->vm_exit_msr_store_count))
11470 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040011471 }
11472
Jim Mattson4f350c62017-09-14 16:31:44 -070011473 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020011474 vm_entry_controls_reset_shadow(vmx);
11475 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010011476 vmx_segment_cache_clear(vmx);
11477
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011478 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070011479 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11480 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011481 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011482 if (vmx->hv_deadline_tsc == -1)
11483 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11484 PIN_BASED_VMX_PREEMPTION_TIMER);
11485 else
11486 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11487 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070011488 if (kvm_has_tsc_control)
11489 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011490
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011491 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11492 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11493 vmx_set_virtual_x2apic_mode(vcpu,
11494 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011495 } else if (!nested_cpu_has_ept(vmcs12) &&
11496 nested_cpu_has2(vmcs12,
11497 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11498 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011499 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011500
11501 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11502 vmx->host_rsp = 0;
11503
11504 /* Unpin physical memory we referred to in vmcs02 */
11505 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011506 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011507 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011508 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011509 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011510 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011511 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011512 }
Wincy Van705699a2015-02-03 23:58:17 +080011513 if (vmx->nested.pi_desc_page) {
11514 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011515 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011516 vmx->nested.pi_desc_page = NULL;
11517 vmx->nested.pi_desc = NULL;
11518 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011519
11520 /*
Tang Chen38b99172014-09-24 15:57:54 +080011521 * We are now running in L2, mmu_notifier will force to reload the
11522 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11523 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011524 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011525
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011526 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030011527 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011528
11529 /* in case we halted in L2 */
11530 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070011531
11532 if (likely(!vmx->fail)) {
11533 /*
11534 * TODO: SDM says that with acknowledge interrupt on
11535 * exit, bit 31 of the VM-exit interrupt information
11536 * (valid interrupt) is always set to 1 on
11537 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
11538 * need kvm_cpu_has_interrupt(). See the commit
11539 * message for details.
11540 */
11541 if (nested_exit_intr_ack_set(vcpu) &&
11542 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
11543 kvm_cpu_has_interrupt(vcpu)) {
11544 int irq = kvm_cpu_get_interrupt(vcpu);
11545 WARN_ON(irq < 0);
11546 vmcs12->vm_exit_intr_info = irq |
11547 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11548 }
11549
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011550 if (exit_reason != -1)
11551 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11552 vmcs12->exit_qualification,
11553 vmcs12->idt_vectoring_info_field,
11554 vmcs12->vm_exit_intr_info,
11555 vmcs12->vm_exit_intr_error_code,
11556 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070011557
11558 load_vmcs12_host_state(vcpu, vmcs12);
11559
11560 return;
11561 }
11562
11563 /*
11564 * After an early L2 VM-entry failure, we're now back
11565 * in L1 which thinks it just finished a VMLAUNCH or
11566 * VMRESUME instruction, so we need to set the failure
11567 * flag and the VM-instruction error field of the VMCS
11568 * accordingly.
11569 */
11570 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080011571
11572 load_vmcs12_mmu_host_state(vcpu, vmcs12);
11573
Jim Mattson4f350c62017-09-14 16:31:44 -070011574 /*
11575 * The emulated instruction was already skipped in
11576 * nested_vmx_run, but the updated RIP was never
11577 * written back to the vmcs01.
11578 */
11579 skip_emulated_instruction(vcpu);
11580 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011581}
11582
Nadav Har'El7c177932011-05-25 23:12:04 +030011583/*
Jan Kiszka42124922014-01-04 18:47:19 +010011584 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11585 */
11586static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11587{
Wanpeng Li2f707d92017-03-06 04:03:28 -080011588 if (is_guest_mode(vcpu)) {
11589 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011590 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080011591 }
Jan Kiszka42124922014-01-04 18:47:19 +010011592 free_nested(to_vmx(vcpu));
11593}
11594
11595/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011596 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11597 * 23.7 "VM-entry failures during or after loading guest state" (this also
11598 * lists the acceptable exit-reason and exit-qualification parameters).
11599 * It should only be called before L2 actually succeeded to run, and when
11600 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11601 */
11602static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11603 struct vmcs12 *vmcs12,
11604 u32 reason, unsigned long qualification)
11605{
11606 load_vmcs12_host_state(vcpu, vmcs12);
11607 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11608 vmcs12->exit_qualification = qualification;
11609 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011610 if (enable_shadow_vmcs)
11611 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011612}
11613
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011614static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11615 struct x86_instruction_info *info,
11616 enum x86_intercept_stage stage)
11617{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020011618 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11619 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
11620
11621 /*
11622 * RDPID causes #UD if disabled through secondary execution controls.
11623 * Because it is marked as EmulateOnUD, we need to intercept it here.
11624 */
11625 if (info->intercept == x86_intercept_rdtscp &&
11626 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
11627 ctxt->exception.vector = UD_VECTOR;
11628 ctxt->exception.error_code_valid = false;
11629 return X86EMUL_PROPAGATE_FAULT;
11630 }
11631
11632 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011633 return X86EMUL_CONTINUE;
11634}
11635
Yunhong Jiang64672c92016-06-13 14:19:59 -070011636#ifdef CONFIG_X86_64
11637/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11638static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11639 u64 divisor, u64 *result)
11640{
11641 u64 low = a << shift, high = a >> (64 - shift);
11642
11643 /* To avoid the overflow on divq */
11644 if (high >= divisor)
11645 return 1;
11646
11647 /* Low hold the result, high hold rem which is discarded */
11648 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11649 "rm" (divisor), "0" (low), "1" (high));
11650 *result = low;
11651
11652 return 0;
11653}
11654
11655static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11656{
11657 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011658 u64 tscl = rdtsc();
11659 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11660 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011661
11662 /* Convert to host delta tsc if tsc scaling is enabled */
11663 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11664 u64_shl_div_u64(delta_tsc,
11665 kvm_tsc_scaling_ratio_frac_bits,
11666 vcpu->arch.tsc_scaling_ratio,
11667 &delta_tsc))
11668 return -ERANGE;
11669
11670 /*
11671 * If the delta tsc can't fit in the 32 bit after the multi shift,
11672 * we can't use the preemption timer.
11673 * It's possible that it fits on later vmentries, but checking
11674 * on every vmentry is costly so we just use an hrtimer.
11675 */
11676 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11677 return -ERANGE;
11678
11679 vmx->hv_deadline_tsc = tscl + delta_tsc;
11680 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11681 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070011682
11683 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011684}
11685
11686static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11687{
11688 struct vcpu_vmx *vmx = to_vmx(vcpu);
11689 vmx->hv_deadline_tsc = -1;
11690 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11691 PIN_BASED_VMX_PREEMPTION_TIMER);
11692}
11693#endif
11694
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011695static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011696{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011697 if (ple_gap)
11698 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011699}
11700
Kai Huang843e4332015-01-28 10:54:28 +080011701static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11702 struct kvm_memory_slot *slot)
11703{
11704 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11705 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11706}
11707
11708static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11709 struct kvm_memory_slot *slot)
11710{
11711 kvm_mmu_slot_set_dirty(kvm, slot);
11712}
11713
11714static void vmx_flush_log_dirty(struct kvm *kvm)
11715{
11716 kvm_flush_pml_buffers(kvm);
11717}
11718
Bandan Dasc5f983f2017-05-05 15:25:14 -040011719static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
11720{
11721 struct vmcs12 *vmcs12;
11722 struct vcpu_vmx *vmx = to_vmx(vcpu);
11723 gpa_t gpa;
11724 struct page *page = NULL;
11725 u64 *pml_address;
11726
11727 if (is_guest_mode(vcpu)) {
11728 WARN_ON_ONCE(vmx->nested.pml_full);
11729
11730 /*
11731 * Check if PML is enabled for the nested guest.
11732 * Whether eptp bit 6 is set is already checked
11733 * as part of A/D emulation.
11734 */
11735 vmcs12 = get_vmcs12(vcpu);
11736 if (!nested_cpu_has_pml(vmcs12))
11737 return 0;
11738
Dan Carpenter47698862017-05-10 22:43:17 +030011739 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040011740 vmx->nested.pml_full = true;
11741 return 1;
11742 }
11743
11744 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
11745
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011746 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
11747 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040011748 return 0;
11749
11750 pml_address = kmap(page);
11751 pml_address[vmcs12->guest_pml_index--] = gpa;
11752 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011753 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011754 }
11755
11756 return 0;
11757}
11758
Kai Huang843e4332015-01-28 10:54:28 +080011759static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11760 struct kvm_memory_slot *memslot,
11761 gfn_t offset, unsigned long mask)
11762{
11763 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11764}
11765
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011766static void __pi_post_block(struct kvm_vcpu *vcpu)
11767{
11768 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11769 struct pi_desc old, new;
11770 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011771
11772 do {
11773 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011774 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
11775 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011776
11777 dest = cpu_physical_id(vcpu->cpu);
11778
11779 if (x2apic_enabled())
11780 new.ndst = dest;
11781 else
11782 new.ndst = (dest << 8) & 0xFF00;
11783
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011784 /* set 'NV' to 'notification vector' */
11785 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020011786 } while (cmpxchg64(&pi_desc->control, old.control,
11787 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011788
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011789 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
11790 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011791 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011792 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011793 vcpu->pre_pcpu = -1;
11794 }
11795}
11796
Feng Wuefc64402015-09-18 22:29:51 +080011797/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080011798 * This routine does the following things for vCPU which is going
11799 * to be blocked if VT-d PI is enabled.
11800 * - Store the vCPU to the wakeup list, so when interrupts happen
11801 * we can find the right vCPU to wake up.
11802 * - Change the Posted-interrupt descriptor as below:
11803 * 'NDST' <-- vcpu->pre_pcpu
11804 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11805 * - If 'ON' is set during this process, which means at least one
11806 * interrupt is posted for this vCPU, we cannot block it, in
11807 * this case, return 1, otherwise, return 0.
11808 *
11809 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070011810static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011811{
Feng Wubf9f6ac2015-09-18 22:29:55 +080011812 unsigned int dest;
11813 struct pi_desc old, new;
11814 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11815
11816 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011817 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11818 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011819 return 0;
11820
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011821 WARN_ON(irqs_disabled());
11822 local_irq_disable();
11823 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
11824 vcpu->pre_pcpu = vcpu->cpu;
11825 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11826 list_add_tail(&vcpu->blocked_vcpu_list,
11827 &per_cpu(blocked_vcpu_on_cpu,
11828 vcpu->pre_pcpu));
11829 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11830 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080011831
11832 do {
11833 old.control = new.control = pi_desc->control;
11834
Feng Wubf9f6ac2015-09-18 22:29:55 +080011835 WARN((pi_desc->sn == 1),
11836 "Warning: SN field of posted-interrupts "
11837 "is set before blocking\n");
11838
11839 /*
11840 * Since vCPU can be preempted during this process,
11841 * vcpu->cpu could be different with pre_pcpu, we
11842 * need to set pre_pcpu as the destination of wakeup
11843 * notification event, then we can find the right vCPU
11844 * to wakeup in wakeup handler if interrupts happen
11845 * when the vCPU is in blocked state.
11846 */
11847 dest = cpu_physical_id(vcpu->pre_pcpu);
11848
11849 if (x2apic_enabled())
11850 new.ndst = dest;
11851 else
11852 new.ndst = (dest << 8) & 0xFF00;
11853
11854 /* set 'NV' to 'wakeup vector' */
11855 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020011856 } while (cmpxchg64(&pi_desc->control, old.control,
11857 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011858
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011859 /* We should not block the vCPU if an interrupt is posted for it. */
11860 if (pi_test_on(pi_desc) == 1)
11861 __pi_post_block(vcpu);
11862
11863 local_irq_enable();
11864 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011865}
11866
Yunhong Jiangbc225122016-06-13 14:19:58 -070011867static int vmx_pre_block(struct kvm_vcpu *vcpu)
11868{
11869 if (pi_pre_block(vcpu))
11870 return 1;
11871
Yunhong Jiang64672c92016-06-13 14:19:59 -070011872 if (kvm_lapic_hv_timer_in_use(vcpu))
11873 kvm_lapic_switch_to_sw_timer(vcpu);
11874
Yunhong Jiangbc225122016-06-13 14:19:58 -070011875 return 0;
11876}
11877
11878static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011879{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011880 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011881 return;
11882
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011883 WARN_ON(irqs_disabled());
11884 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011885 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011886 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080011887}
11888
Yunhong Jiangbc225122016-06-13 14:19:58 -070011889static void vmx_post_block(struct kvm_vcpu *vcpu)
11890{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011891 if (kvm_x86_ops->set_hv_timer)
11892 kvm_lapic_switch_to_hv_timer(vcpu);
11893
Yunhong Jiangbc225122016-06-13 14:19:58 -070011894 pi_post_block(vcpu);
11895}
11896
Feng Wubf9f6ac2015-09-18 22:29:55 +080011897/*
Feng Wuefc64402015-09-18 22:29:51 +080011898 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11899 *
11900 * @kvm: kvm
11901 * @host_irq: host irq of the interrupt
11902 * @guest_irq: gsi of the interrupt
11903 * @set: set or unset PI
11904 * returns 0 on success, < 0 on failure
11905 */
11906static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11907 uint32_t guest_irq, bool set)
11908{
11909 struct kvm_kernel_irq_routing_entry *e;
11910 struct kvm_irq_routing_table *irq_rt;
11911 struct kvm_lapic_irq irq;
11912 struct kvm_vcpu *vcpu;
11913 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010011914 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080011915
11916 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011917 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11918 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011919 return 0;
11920
11921 idx = srcu_read_lock(&kvm->irq_srcu);
11922 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010011923 if (guest_irq >= irq_rt->nr_rt_entries ||
11924 hlist_empty(&irq_rt->map[guest_irq])) {
11925 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
11926 guest_irq, irq_rt->nr_rt_entries);
11927 goto out;
11928 }
Feng Wuefc64402015-09-18 22:29:51 +080011929
11930 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11931 if (e->type != KVM_IRQ_ROUTING_MSI)
11932 continue;
11933 /*
11934 * VT-d PI cannot support posting multicast/broadcast
11935 * interrupts to a vCPU, we still use interrupt remapping
11936 * for these kind of interrupts.
11937 *
11938 * For lowest-priority interrupts, we only support
11939 * those with single CPU as the destination, e.g. user
11940 * configures the interrupts via /proc/irq or uses
11941 * irqbalance to make the interrupts single-CPU.
11942 *
11943 * We will support full lowest-priority interrupt later.
11944 */
11945
Radim Krčmář371313132016-07-12 22:09:27 +020011946 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011947 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11948 /*
11949 * Make sure the IRTE is in remapped mode if
11950 * we don't handle it in posted mode.
11951 */
11952 ret = irq_set_vcpu_affinity(host_irq, NULL);
11953 if (ret < 0) {
11954 printk(KERN_INFO
11955 "failed to back to remapped mode, irq: %u\n",
11956 host_irq);
11957 goto out;
11958 }
11959
Feng Wuefc64402015-09-18 22:29:51 +080011960 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011961 }
Feng Wuefc64402015-09-18 22:29:51 +080011962
11963 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11964 vcpu_info.vector = irq.vector;
11965
Feng Wub6ce9782016-01-25 16:53:35 +080011966 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011967 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11968
11969 if (set)
11970 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2eb2017-09-18 09:56:49 +080011971 else
Feng Wuefc64402015-09-18 22:29:51 +080011972 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080011973
11974 if (ret < 0) {
11975 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11976 __func__);
11977 goto out;
11978 }
11979 }
11980
11981 ret = 0;
11982out:
11983 srcu_read_unlock(&kvm->irq_srcu, idx);
11984 return ret;
11985}
11986
Ashok Rajc45dcc72016-06-22 14:59:56 +080011987static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11988{
11989 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11990 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11991 FEATURE_CONTROL_LMCE;
11992 else
11993 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11994 ~FEATURE_CONTROL_LMCE;
11995}
11996
Ladi Prosek72d7b372017-10-11 16:54:41 +020011997static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
11998{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011999 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12000 if (to_vmx(vcpu)->nested.nested_run_pending)
12001 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012002 return 1;
12003}
12004
Ladi Prosek0234bf82017-10-11 16:54:40 +020012005static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12006{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012007 struct vcpu_vmx *vmx = to_vmx(vcpu);
12008
12009 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12010 if (vmx->nested.smm.guest_mode)
12011 nested_vmx_vmexit(vcpu, -1, 0, 0);
12012
12013 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12014 vmx->nested.vmxon = false;
Ladi Prosek0234bf82017-10-11 16:54:40 +020012015 return 0;
12016}
12017
12018static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12019{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012020 struct vcpu_vmx *vmx = to_vmx(vcpu);
12021 int ret;
12022
12023 if (vmx->nested.smm.vmxon) {
12024 vmx->nested.vmxon = true;
12025 vmx->nested.smm.vmxon = false;
12026 }
12027
12028 if (vmx->nested.smm.guest_mode) {
12029 vcpu->arch.hflags &= ~HF_SMM_MASK;
12030 ret = enter_vmx_non_root_mode(vcpu, false);
12031 vcpu->arch.hflags |= HF_SMM_MASK;
12032 if (ret)
12033 return ret;
12034
12035 vmx->nested.smm.guest_mode = false;
12036 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020012037 return 0;
12038}
12039
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012040static int enable_smi_window(struct kvm_vcpu *vcpu)
12041{
12042 return 0;
12043}
12044
Kees Cook404f6aa2016-08-08 16:29:06 -070012045static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080012046 .cpu_has_kvm_support = cpu_has_kvm_support,
12047 .disabled_by_bios = vmx_disabled_by_bios,
12048 .hardware_setup = hardware_setup,
12049 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030012050 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012051 .hardware_enable = hardware_enable,
12052 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080012053 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020012054 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012055
12056 .vcpu_create = vmx_create_vcpu,
12057 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030012058 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012059
Avi Kivity04d2cc72007-09-10 18:10:54 +030012060 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012061 .vcpu_load = vmx_vcpu_load,
12062 .vcpu_put = vmx_vcpu_put,
12063
Paolo Bonzinia96036b2015-11-10 11:55:36 +010012064 .update_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012065 .get_msr = vmx_get_msr,
12066 .set_msr = vmx_set_msr,
12067 .get_segment_base = vmx_get_segment_base,
12068 .get_segment = vmx_get_segment,
12069 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020012070 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012071 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020012072 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020012073 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030012074 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012075 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012076 .set_cr3 = vmx_set_cr3,
12077 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012078 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012079 .get_idt = vmx_get_idt,
12080 .set_idt = vmx_set_idt,
12081 .get_gdt = vmx_get_gdt,
12082 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010012083 .get_dr6 = vmx_get_dr6,
12084 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030012085 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010012086 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030012087 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012088 .get_rflags = vmx_get_rflags,
12089 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080012090
Avi Kivity6aa8b732006-12-10 02:21:36 -080012091 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012092
Avi Kivity6aa8b732006-12-10 02:21:36 -080012093 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020012094 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012095 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040012096 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12097 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020012098 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030012099 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012100 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020012101 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030012102 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020012103 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012104 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010012105 .get_nmi_mask = vmx_get_nmi_mask,
12106 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012107 .enable_nmi_window = enable_nmi_window,
12108 .enable_irq_window = enable_irq_window,
12109 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080012110 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080012111 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030012112 .get_enable_apicv = vmx_get_enable_apicv,
12113 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012114 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010012115 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012116 .hwapic_irr_update = vmx_hwapic_irr_update,
12117 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080012118 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12119 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012120
Izik Eiduscbc94022007-10-25 00:29:55 +020012121 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080012122 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080012123 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030012124
Avi Kivity586f9602010-11-18 13:09:54 +020012125 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020012126
Sheng Yang17cc3932010-01-05 19:02:27 +080012127 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080012128
12129 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080012130
12131 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000012132 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020012133
12134 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080012135
12136 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012137
12138 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020012139
12140 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012141
12142 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080012143 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000012144 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080012145 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020012146 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012147
12148 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012149
12150 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080012151
12152 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12153 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12154 .flush_log_dirty = vmx_flush_log_dirty,
12155 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040012156 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020012157
Feng Wubf9f6ac2015-09-18 22:29:55 +080012158 .pre_block = vmx_pre_block,
12159 .post_block = vmx_post_block,
12160
Wei Huang25462f72015-06-19 15:45:05 +020012161 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080012162
12163 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070012164
12165#ifdef CONFIG_X86_64
12166 .set_hv_timer = vmx_set_hv_timer,
12167 .cancel_hv_timer = vmx_cancel_hv_timer,
12168#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080012169
12170 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012171
Ladi Prosek72d7b372017-10-11 16:54:41 +020012172 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012173 .pre_enter_smm = vmx_pre_enter_smm,
12174 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012175 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012176};
12177
12178static int __init vmx_init(void)
12179{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012180 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
12181 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030012182 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012183 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080012184
Dave Young2965faa2015-09-09 15:38:55 -070012185#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012186 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12187 crash_vmclear_local_loaded_vmcss);
12188#endif
12189
He, Qingfdef3ad2007-04-30 09:45:24 +030012190 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080012191}
12192
12193static void __exit vmx_exit(void)
12194{
Dave Young2965faa2015-09-09 15:38:55 -070012195#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053012196 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012197 synchronize_rcu();
12198#endif
12199
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080012200 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080012201}
12202
12203module_init(vmx_init)
12204module_exit(vmx_exit)