blob: fafc1f6d8987c26d631ce08ec12beafa385bb19b [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 Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Feng Wu28b835d2015-09-18 22:29:54 +080041#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080043#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020044#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020045#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080046#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020047#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020048#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080050#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010051#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080052#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070053#include <asm/mmu_context.h>
David Woodhouse117cc7a2018-01-12 11:11:27 +000054#include <asm/nospec-branch.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080055
Marcelo Tosatti229456f2009-06-17 09:22:14 -030056#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020057#include "pmu.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058
Avi Kivity4ecac3f2008-05-13 13:23:38 +030059#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040060#define __ex_clear(x, reg) \
61 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030062
Avi Kivity6aa8b732006-12-10 02:21:36 -080063MODULE_AUTHOR("Qumranet");
64MODULE_LICENSE("GPL");
65
Josh Triplette9bda3b2012-03-20 23:33:51 -070066static const struct x86_cpu_id vmx_cpu_id[] = {
67 X86_FEATURE_MATCH(X86_FEATURE_VMX),
68 {}
69};
70MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
71
Rusty Russell476bc002012-01-13 09:32:18 +103072static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020073module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080074
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010075static bool __read_mostly enable_vnmi = 1;
76module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
77
Rusty Russell476bc002012-01-13 09:32:18 +103078static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020079module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020080
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020082module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080083
Rusty Russell476bc002012-01-13 09:32:18 +103084static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070085module_param_named(unrestricted_guest,
86 enable_unrestricted_guest, bool, S_IRUGO);
87
Xudong Hao83c3a332012-05-28 19:33:35 +080088static bool __read_mostly enable_ept_ad_bits = 1;
89module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
90
Avi Kivitya27685c2012-06-12 20:30:18 +030091static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020092module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030093
Rusty Russell476bc002012-01-13 09:32:18 +103094static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030095module_param(fasteoi, bool, S_IRUGO);
96
Yang Zhang5a717852013-04-11 19:25:16 +080097static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080098module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080099
Abel Gordonabc4fc52013-04-18 14:35:25 +0300100static bool __read_mostly enable_shadow_vmcs = 1;
101module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300102/*
103 * If nested=1, nested virtualization is supported, i.e., guests may use
104 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
105 * use VMX instructions.
106 */
Rusty Russell476bc002012-01-13 09:32:18 +1030107static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300108module_param(nested, bool, S_IRUGO);
109
Wanpeng Li20300092014-12-02 19:14:59 +0800110static u64 __read_mostly host_xss;
111
Kai Huang843e4332015-01-28 10:54:28 +0800112static bool __read_mostly enable_pml = 1;
113module_param_named(pml, enable_pml, bool, S_IRUGO);
114
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100115#define MSR_TYPE_R 1
116#define MSR_TYPE_W 2
117#define MSR_TYPE_RW 3
118
119#define MSR_BITMAP_MODE_X2APIC 1
120#define MSR_BITMAP_MODE_X2APIC_APICV 2
121#define MSR_BITMAP_MODE_LM 4
122
Haozhong Zhang64903d62015-10-20 15:39:09 +0800123#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
124
Yunhong Jiang64672c92016-06-13 14:19:59 -0700125/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
126static int __read_mostly cpu_preemption_timer_multi;
127static bool __read_mostly enable_preemption_timer = 1;
128#ifdef CONFIG_X86_64
129module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
130#endif
131
Gleb Natapov50378782013-02-04 16:00:28 +0200132#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
133#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200134#define KVM_VM_CR0_ALWAYS_ON \
135 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200136#define KVM_CR4_GUEST_OWNED_BITS \
137 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800138 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200139
Avi Kivitycdc0e242009-12-06 17:21:14 +0200140#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
141#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
142
Avi Kivity78ac8b42010-04-08 18:19:35 +0300143#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
144
Jan Kiszkaf4124502014-03-07 20:03:13 +0100145#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
146
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800147/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300148 * Hyper-V requires all of these, so mark them as supported even though
149 * they are just treated the same as all-context.
150 */
151#define VMX_VPID_EXTENT_SUPPORTED_MASK \
152 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
153 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
154 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
155 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
156
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800157/*
158 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
159 * ple_gap: upper bound on the amount of time between two successive
160 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500161 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800162 * ple_window: upper bound on the amount of time a guest is allowed to execute
163 * in a PAUSE loop. Tests indicate that most spinlocks are held for
164 * less than 2^12 cycles
165 * Time is measured based on a counter that runs at the same rate as the TSC,
166 * refer SDM volume 3b section 21.6.13 & 22.1.3.
167 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200168#define KVM_VMX_DEFAULT_PLE_GAP 128
169#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
170#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
171#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
172#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
173 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
174
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800175static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
176module_param(ple_gap, int, S_IRUGO);
177
178static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
179module_param(ple_window, int, S_IRUGO);
180
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200181/* Default doubles per-vcpu window every exit. */
182static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
183module_param(ple_window_grow, int, S_IRUGO);
184
185/* Default resets per-vcpu window every exit to ple_window. */
186static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
187module_param(ple_window_shrink, int, S_IRUGO);
188
189/* Default is to compute the maximum so we can never overflow. */
190static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
191static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
192module_param(ple_window_max, int, S_IRUGO);
193
Avi Kivity83287ea422012-09-16 15:10:57 +0300194extern const ulong vmx_return;
195
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200196#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300197
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400198struct vmcs {
199 u32 revision_id;
200 u32 abort;
201 char data[0];
202};
203
Nadav Har'Eld462b812011-05-24 15:26:10 +0300204/*
205 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
206 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
207 * loaded on this CPU (so we can clear them if the CPU goes down).
208 */
209struct loaded_vmcs {
210 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700211 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300212 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200213 bool launched;
214 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200215 unsigned long vmcs_host_cr3; /* May not match real cr3 */
216 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100217 /* Support for vnmi-less CPUs */
218 int soft_vnmi_blocked;
219 ktime_t entry_time;
220 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100221 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300222 struct list_head loaded_vmcss_on_cpu_link;
223};
224
Avi Kivity26bb0982009-09-07 11:14:12 +0300225struct shared_msr_entry {
226 unsigned index;
227 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200228 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300229};
230
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300231/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300232 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
233 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
234 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
235 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
236 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
237 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600238 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300239 * underlying hardware which will be used to run L2.
240 * This structure is packed to ensure that its layout is identical across
241 * machines (necessary for live migration).
242 * If there are changes in this struct, VMCS12_REVISION must be changed.
243 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300244typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300245struct __packed vmcs12 {
246 /* According to the Intel spec, a VMCS region must start with the
247 * following two fields. Then follow implementation-specific data.
248 */
249 u32 revision_id;
250 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300251
Nadav Har'El27d6c862011-05-25 23:06:59 +0300252 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
253 u32 padding[7]; /* room for future expansion */
254
Nadav Har'El22bd0352011-05-25 23:05:57 +0300255 u64 io_bitmap_a;
256 u64 io_bitmap_b;
257 u64 msr_bitmap;
258 u64 vm_exit_msr_store_addr;
259 u64 vm_exit_msr_load_addr;
260 u64 vm_entry_msr_load_addr;
261 u64 tsc_offset;
262 u64 virtual_apic_page_addr;
263 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800264 u64 posted_intr_desc_addr;
Bandan Das27c42a12017-08-03 15:54:42 -0400265 u64 vm_function_control;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300266 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800267 u64 eoi_exit_bitmap0;
268 u64 eoi_exit_bitmap1;
269 u64 eoi_exit_bitmap2;
270 u64 eoi_exit_bitmap3;
Bandan Das41ab9372017-08-03 15:54:43 -0400271 u64 eptp_list_address;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800272 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300273 u64 guest_physical_address;
274 u64 vmcs_link_pointer;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400275 u64 pml_address;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300276 u64 guest_ia32_debugctl;
277 u64 guest_ia32_pat;
278 u64 guest_ia32_efer;
279 u64 guest_ia32_perf_global_ctrl;
280 u64 guest_pdptr0;
281 u64 guest_pdptr1;
282 u64 guest_pdptr2;
283 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100284 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300285 u64 host_ia32_pat;
286 u64 host_ia32_efer;
287 u64 host_ia32_perf_global_ctrl;
288 u64 padding64[8]; /* room for future expansion */
289 /*
290 * To allow migration of L1 (complete with its L2 guests) between
291 * machines of different natural widths (32 or 64 bit), we cannot have
292 * unsigned long fields with no explict size. We use u64 (aliased
293 * natural_width) instead. Luckily, x86 is little-endian.
294 */
295 natural_width cr0_guest_host_mask;
296 natural_width cr4_guest_host_mask;
297 natural_width cr0_read_shadow;
298 natural_width cr4_read_shadow;
299 natural_width cr3_target_value0;
300 natural_width cr3_target_value1;
301 natural_width cr3_target_value2;
302 natural_width cr3_target_value3;
303 natural_width exit_qualification;
304 natural_width guest_linear_address;
305 natural_width guest_cr0;
306 natural_width guest_cr3;
307 natural_width guest_cr4;
308 natural_width guest_es_base;
309 natural_width guest_cs_base;
310 natural_width guest_ss_base;
311 natural_width guest_ds_base;
312 natural_width guest_fs_base;
313 natural_width guest_gs_base;
314 natural_width guest_ldtr_base;
315 natural_width guest_tr_base;
316 natural_width guest_gdtr_base;
317 natural_width guest_idtr_base;
318 natural_width guest_dr7;
319 natural_width guest_rsp;
320 natural_width guest_rip;
321 natural_width guest_rflags;
322 natural_width guest_pending_dbg_exceptions;
323 natural_width guest_sysenter_esp;
324 natural_width guest_sysenter_eip;
325 natural_width host_cr0;
326 natural_width host_cr3;
327 natural_width host_cr4;
328 natural_width host_fs_base;
329 natural_width host_gs_base;
330 natural_width host_tr_base;
331 natural_width host_gdtr_base;
332 natural_width host_idtr_base;
333 natural_width host_ia32_sysenter_esp;
334 natural_width host_ia32_sysenter_eip;
335 natural_width host_rsp;
336 natural_width host_rip;
337 natural_width paddingl[8]; /* room for future expansion */
338 u32 pin_based_vm_exec_control;
339 u32 cpu_based_vm_exec_control;
340 u32 exception_bitmap;
341 u32 page_fault_error_code_mask;
342 u32 page_fault_error_code_match;
343 u32 cr3_target_count;
344 u32 vm_exit_controls;
345 u32 vm_exit_msr_store_count;
346 u32 vm_exit_msr_load_count;
347 u32 vm_entry_controls;
348 u32 vm_entry_msr_load_count;
349 u32 vm_entry_intr_info_field;
350 u32 vm_entry_exception_error_code;
351 u32 vm_entry_instruction_len;
352 u32 tpr_threshold;
353 u32 secondary_vm_exec_control;
354 u32 vm_instruction_error;
355 u32 vm_exit_reason;
356 u32 vm_exit_intr_info;
357 u32 vm_exit_intr_error_code;
358 u32 idt_vectoring_info_field;
359 u32 idt_vectoring_error_code;
360 u32 vm_exit_instruction_len;
361 u32 vmx_instruction_info;
362 u32 guest_es_limit;
363 u32 guest_cs_limit;
364 u32 guest_ss_limit;
365 u32 guest_ds_limit;
366 u32 guest_fs_limit;
367 u32 guest_gs_limit;
368 u32 guest_ldtr_limit;
369 u32 guest_tr_limit;
370 u32 guest_gdtr_limit;
371 u32 guest_idtr_limit;
372 u32 guest_es_ar_bytes;
373 u32 guest_cs_ar_bytes;
374 u32 guest_ss_ar_bytes;
375 u32 guest_ds_ar_bytes;
376 u32 guest_fs_ar_bytes;
377 u32 guest_gs_ar_bytes;
378 u32 guest_ldtr_ar_bytes;
379 u32 guest_tr_ar_bytes;
380 u32 guest_interruptibility_info;
381 u32 guest_activity_state;
382 u32 guest_sysenter_cs;
383 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100384 u32 vmx_preemption_timer_value;
385 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300386 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800387 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300388 u16 guest_es_selector;
389 u16 guest_cs_selector;
390 u16 guest_ss_selector;
391 u16 guest_ds_selector;
392 u16 guest_fs_selector;
393 u16 guest_gs_selector;
394 u16 guest_ldtr_selector;
395 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800396 u16 guest_intr_status;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400397 u16 guest_pml_index;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300398 u16 host_es_selector;
399 u16 host_cs_selector;
400 u16 host_ss_selector;
401 u16 host_ds_selector;
402 u16 host_fs_selector;
403 u16 host_gs_selector;
404 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300405};
406
407/*
408 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
409 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
410 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
411 */
412#define VMCS12_REVISION 0x11e57ed0
413
414/*
415 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
416 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
417 * current implementation, 4K are reserved to avoid future complications.
418 */
419#define VMCS12_SIZE 0x1000
420
421/*
Jim Mattson5b157062017-12-22 12:11:12 -0800422 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
423 * supported VMCS12 field encoding.
424 */
425#define VMCS12_MAX_FIELD_INDEX 0x17
426
427/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300428 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
429 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
430 */
431struct nested_vmx {
432 /* Has the level1 guest done vmxon? */
433 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400434 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400435 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300436
437 /* The guest-physical address of the current VMCS L1 keeps for L2 */
438 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700439 /*
440 * Cache of the guest's VMCS, existing outside of guest memory.
441 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700442 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700443 */
444 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300445 /*
446 * Indicates if the shadow vmcs must be updated with the
447 * data hold by vmcs12
448 */
449 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100450 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300451
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200452 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300453 /* L2 must run next, and mustn't decide to exit to L1. */
454 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600455
456 struct loaded_vmcs vmcs02;
457
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300458 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600459 * Guest pages referred to in the vmcs02 with host-physical
460 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300461 */
462 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800463 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800464 struct page *pi_desc_page;
465 struct pi_desc *pi_desc;
466 bool pi_pending;
467 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100468
469 struct hrtimer preemption_timer;
470 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200471
472 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
473 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800474
Wanpeng Li5c614b32015-10-13 09:18:36 -0700475 u16 vpid02;
476 u16 last_vpid;
477
David Matlack0115f9c2016-11-29 18:14:06 -0800478 /*
479 * We only store the "true" versions of the VMX capability MSRs. We
480 * generate the "non-true" versions by setting the must-be-1 bits
481 * according to the SDM.
482 */
Wincy Vanb9c237b2015-02-03 23:56:30 +0800483 u32 nested_vmx_procbased_ctls_low;
484 u32 nested_vmx_procbased_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800485 u32 nested_vmx_secondary_ctls_low;
486 u32 nested_vmx_secondary_ctls_high;
487 u32 nested_vmx_pinbased_ctls_low;
488 u32 nested_vmx_pinbased_ctls_high;
489 u32 nested_vmx_exit_ctls_low;
490 u32 nested_vmx_exit_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800491 u32 nested_vmx_entry_ctls_low;
492 u32 nested_vmx_entry_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800493 u32 nested_vmx_misc_low;
494 u32 nested_vmx_misc_high;
495 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700496 u32 nested_vmx_vpid_caps;
David Matlack62cc6b9d2016-11-29 18:14:07 -0800497 u64 nested_vmx_basic;
498 u64 nested_vmx_cr0_fixed0;
499 u64 nested_vmx_cr0_fixed1;
500 u64 nested_vmx_cr4_fixed0;
501 u64 nested_vmx_cr4_fixed1;
502 u64 nested_vmx_vmcs_enum;
Bandan Das27c42a12017-08-03 15:54:42 -0400503 u64 nested_vmx_vmfunc_controls;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200504
505 /* SMM related state */
506 struct {
507 /* in VMX operation on SMM entry? */
508 bool vmxon;
509 /* in guest mode on SMM entry? */
510 bool guest_mode;
511 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300512};
513
Yang Zhang01e439b2013-04-11 19:25:12 +0800514#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800515#define POSTED_INTR_SN 1
516
Yang Zhang01e439b2013-04-11 19:25:12 +0800517/* Posted-Interrupt Descriptor */
518struct pi_desc {
519 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800520 union {
521 struct {
522 /* bit 256 - Outstanding Notification */
523 u16 on : 1,
524 /* bit 257 - Suppress Notification */
525 sn : 1,
526 /* bit 271:258 - Reserved */
527 rsvd_1 : 14;
528 /* bit 279:272 - Notification Vector */
529 u8 nv;
530 /* bit 287:280 - Reserved */
531 u8 rsvd_2;
532 /* bit 319:288 - Notification Destination */
533 u32 ndst;
534 };
535 u64 control;
536 };
537 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800538} __aligned(64);
539
Yang Zhanga20ed542013-04-11 19:25:15 +0800540static bool pi_test_and_set_on(struct pi_desc *pi_desc)
541{
542 return test_and_set_bit(POSTED_INTR_ON,
543 (unsigned long *)&pi_desc->control);
544}
545
546static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
547{
548 return test_and_clear_bit(POSTED_INTR_ON,
549 (unsigned long *)&pi_desc->control);
550}
551
552static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
553{
554 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
555}
556
Feng Wuebbfc762015-09-18 22:29:46 +0800557static inline void pi_clear_sn(struct pi_desc *pi_desc)
558{
559 return clear_bit(POSTED_INTR_SN,
560 (unsigned long *)&pi_desc->control);
561}
562
563static inline void pi_set_sn(struct pi_desc *pi_desc)
564{
565 return set_bit(POSTED_INTR_SN,
566 (unsigned long *)&pi_desc->control);
567}
568
Paolo Bonziniad361092016-09-20 16:15:05 +0200569static inline void pi_clear_on(struct pi_desc *pi_desc)
570{
571 clear_bit(POSTED_INTR_ON,
572 (unsigned long *)&pi_desc->control);
573}
574
Feng Wuebbfc762015-09-18 22:29:46 +0800575static inline int pi_test_on(struct pi_desc *pi_desc)
576{
577 return test_bit(POSTED_INTR_ON,
578 (unsigned long *)&pi_desc->control);
579}
580
581static inline int pi_test_sn(struct pi_desc *pi_desc)
582{
583 return test_bit(POSTED_INTR_SN,
584 (unsigned long *)&pi_desc->control);
585}
586
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400587struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000588 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300589 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300590 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100591 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300592 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200593 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200594 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300595 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400596 int nmsrs;
597 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800598 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400599#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300600 u64 msr_host_kernel_gs_base;
601 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400602#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100603
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100604 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100605 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100606
Gleb Natapov2961e8762013-11-25 15:37:13 +0200607 u32 vm_entry_controls_shadow;
608 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200609 u32 secondary_exec_control;
610
Nadav Har'Eld462b812011-05-24 15:26:10 +0300611 /*
612 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
613 * non-nested (L1) guest, it always points to vmcs01. For a nested
614 * guest (L2), it points to a different VMCS.
615 */
616 struct loaded_vmcs vmcs01;
617 struct loaded_vmcs *loaded_vmcs;
618 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300619 struct msr_autoload {
620 unsigned nr;
621 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
622 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
623 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400624 struct {
625 int loaded;
626 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300627#ifdef CONFIG_X86_64
628 u16 ds_sel, es_sel;
629#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200630 int gs_ldt_reload_needed;
631 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000632 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400633 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200634 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300635 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300636 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300637 struct kvm_segment segs[8];
638 } rmode;
639 struct {
640 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300641 struct kvm_save_segment {
642 u16 selector;
643 unsigned long base;
644 u32 limit;
645 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300646 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300647 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800648 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300649 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200650
Andi Kleena0861c02009-06-08 17:37:09 +0800651 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800652
Yang Zhang01e439b2013-04-11 19:25:12 +0800653 /* Posted interrupt descriptor */
654 struct pi_desc pi_desc;
655
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300656 /* Support for a guest hypervisor (nested VMX) */
657 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200658
659 /* Dynamic PLE window. */
660 int ple_window;
661 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800662
663 /* Support for PML */
664#define PML_ENTITY_NUM 512
665 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800666
Yunhong Jiang64672c92016-06-13 14:19:59 -0700667 /* apic deadline value in host tsc */
668 u64 hv_deadline_tsc;
669
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800670 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800671
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800672 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800673
Wanpeng Li74c55932017-11-29 01:31:20 -0800674 unsigned long host_debugctlmsr;
675
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800676 /*
677 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
678 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
679 * in msr_ia32_feature_control_valid_bits.
680 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800681 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800682 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400683};
684
Avi Kivity2fb92db2011-04-27 19:42:18 +0300685enum segment_cache_field {
686 SEG_FIELD_SEL = 0,
687 SEG_FIELD_BASE = 1,
688 SEG_FIELD_LIMIT = 2,
689 SEG_FIELD_AR = 3,
690
691 SEG_FIELD_NR = 4
692};
693
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400694static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
695{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000696 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400697}
698
Feng Wuefc64402015-09-18 22:29:51 +0800699static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
700{
701 return &(to_vmx(vcpu)->pi_desc);
702}
703
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800704#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300705#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800706#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
707#define FIELD64(number, name) \
708 FIELD(number, name), \
709 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300710
Abel Gordon4607c2d2013-04-18 14:35:55 +0300711
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100712static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100713#define SHADOW_FIELD_RO(x) x,
714#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300715};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400716static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300717 ARRAY_SIZE(shadow_read_only_fields);
718
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100719static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100720#define SHADOW_FIELD_RW(x) x,
721#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300722};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400723static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300724 ARRAY_SIZE(shadow_read_write_fields);
725
Mathias Krause772e0312012-08-30 01:30:19 +0200726static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300727 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800728 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300729 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
730 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
731 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
732 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
733 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
734 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
735 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
736 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800737 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400738 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300739 FIELD(HOST_ES_SELECTOR, host_es_selector),
740 FIELD(HOST_CS_SELECTOR, host_cs_selector),
741 FIELD(HOST_SS_SELECTOR, host_ss_selector),
742 FIELD(HOST_DS_SELECTOR, host_ds_selector),
743 FIELD(HOST_FS_SELECTOR, host_fs_selector),
744 FIELD(HOST_GS_SELECTOR, host_gs_selector),
745 FIELD(HOST_TR_SELECTOR, host_tr_selector),
746 FIELD64(IO_BITMAP_A, io_bitmap_a),
747 FIELD64(IO_BITMAP_B, io_bitmap_b),
748 FIELD64(MSR_BITMAP, msr_bitmap),
749 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
750 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
751 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
752 FIELD64(TSC_OFFSET, tsc_offset),
753 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
754 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800755 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400756 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300757 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800758 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
759 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
760 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
761 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400762 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800763 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300764 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
765 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400766 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300767 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
768 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
769 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
770 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
771 FIELD64(GUEST_PDPTR0, guest_pdptr0),
772 FIELD64(GUEST_PDPTR1, guest_pdptr1),
773 FIELD64(GUEST_PDPTR2, guest_pdptr2),
774 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100775 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300776 FIELD64(HOST_IA32_PAT, host_ia32_pat),
777 FIELD64(HOST_IA32_EFER, host_ia32_efer),
778 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
779 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
780 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
781 FIELD(EXCEPTION_BITMAP, exception_bitmap),
782 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
783 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
784 FIELD(CR3_TARGET_COUNT, cr3_target_count),
785 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
786 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
787 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
788 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
789 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
790 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
791 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
792 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
793 FIELD(TPR_THRESHOLD, tpr_threshold),
794 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
795 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
796 FIELD(VM_EXIT_REASON, vm_exit_reason),
797 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
798 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
799 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
800 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
801 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
802 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
803 FIELD(GUEST_ES_LIMIT, guest_es_limit),
804 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
805 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
806 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
807 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
808 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
809 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
810 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
811 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
812 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
813 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
814 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
815 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
816 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
817 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
818 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
819 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
820 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
821 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
822 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
823 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
824 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100825 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300826 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
827 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
828 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
829 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
830 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
831 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
832 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
833 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
834 FIELD(EXIT_QUALIFICATION, exit_qualification),
835 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
836 FIELD(GUEST_CR0, guest_cr0),
837 FIELD(GUEST_CR3, guest_cr3),
838 FIELD(GUEST_CR4, guest_cr4),
839 FIELD(GUEST_ES_BASE, guest_es_base),
840 FIELD(GUEST_CS_BASE, guest_cs_base),
841 FIELD(GUEST_SS_BASE, guest_ss_base),
842 FIELD(GUEST_DS_BASE, guest_ds_base),
843 FIELD(GUEST_FS_BASE, guest_fs_base),
844 FIELD(GUEST_GS_BASE, guest_gs_base),
845 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
846 FIELD(GUEST_TR_BASE, guest_tr_base),
847 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
848 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
849 FIELD(GUEST_DR7, guest_dr7),
850 FIELD(GUEST_RSP, guest_rsp),
851 FIELD(GUEST_RIP, guest_rip),
852 FIELD(GUEST_RFLAGS, guest_rflags),
853 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
854 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
855 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
856 FIELD(HOST_CR0, host_cr0),
857 FIELD(HOST_CR3, host_cr3),
858 FIELD(HOST_CR4, host_cr4),
859 FIELD(HOST_FS_BASE, host_fs_base),
860 FIELD(HOST_GS_BASE, host_gs_base),
861 FIELD(HOST_TR_BASE, host_tr_base),
862 FIELD(HOST_GDTR_BASE, host_gdtr_base),
863 FIELD(HOST_IDTR_BASE, host_idtr_base),
864 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
865 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
866 FIELD(HOST_RSP, host_rsp),
867 FIELD(HOST_RIP, host_rip),
868};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300869
870static inline short vmcs_field_to_offset(unsigned long field)
871{
Dan Williams085331d2018-01-31 17:47:03 -0800872 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
873 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800874 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100875
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800876 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -0800877 return -ENOENT;
878
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800879 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -0800880 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -0800881 return -ENOENT;
882
Linus Torvalds15303ba2018-02-10 13:16:35 -0800883 index = array_index_nospec(index, size);
884 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -0800885 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100886 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -0800887 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300888}
889
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300890static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
891{
David Matlack4f2777b2016-07-13 17:16:37 -0700892 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300893}
894
Peter Feiner995f00a2017-06-30 17:26:32 -0700895static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300896static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -0700897static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800898static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300899static void vmx_set_segment(struct kvm_vcpu *vcpu,
900 struct kvm_segment *var, int seg);
901static void vmx_get_segment(struct kvm_vcpu *vcpu,
902 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200903static bool guest_state_valid(struct kvm_vcpu *vcpu);
904static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +0300905static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +0200906static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
907static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
908static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
909 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100910static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +0100911static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
912 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300913
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914static DEFINE_PER_CPU(struct vmcs *, vmxarea);
915static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300916/*
917 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
918 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
919 */
920static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921
Feng Wubf9f6ac2015-09-18 22:29:55 +0800922/*
923 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
924 * can find which vCPU should be waken up.
925 */
926static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
927static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
928
Radim Krčmář23611332016-09-29 22:41:33 +0200929enum {
Radim Krčmář23611332016-09-29 22:41:33 +0200930 VMX_VMREAD_BITMAP,
931 VMX_VMWRITE_BITMAP,
932 VMX_BITMAP_NR
933};
934
935static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
936
Radim Krčmář23611332016-09-29 22:41:33 +0200937#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
938#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +0300939
Avi Kivity110312c2010-12-21 12:54:20 +0200940static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200941static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200942
Sheng Yang2384d2b2008-01-17 15:14:33 +0800943static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
944static DEFINE_SPINLOCK(vmx_vpid_lock);
945
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300946static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947 int size;
948 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +0300949 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300951 u32 pin_based_exec_ctrl;
952 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800953 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300954 u32 vmexit_ctrl;
955 u32 vmentry_ctrl;
956} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957
Hannes Ederefff9e52008-11-28 17:02:06 +0100958static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800959 u32 ept;
960 u32 vpid;
961} vmx_capability;
962
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963#define VMX_SEGMENT_FIELD(seg) \
964 [VCPU_SREG_##seg] = { \
965 .selector = GUEST_##seg##_SELECTOR, \
966 .base = GUEST_##seg##_BASE, \
967 .limit = GUEST_##seg##_LIMIT, \
968 .ar_bytes = GUEST_##seg##_AR_BYTES, \
969 }
970
Mathias Krause772e0312012-08-30 01:30:19 +0200971static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 unsigned selector;
973 unsigned base;
974 unsigned limit;
975 unsigned ar_bytes;
976} kvm_vmx_segment_fields[] = {
977 VMX_SEGMENT_FIELD(CS),
978 VMX_SEGMENT_FIELD(DS),
979 VMX_SEGMENT_FIELD(ES),
980 VMX_SEGMENT_FIELD(FS),
981 VMX_SEGMENT_FIELD(GS),
982 VMX_SEGMENT_FIELD(SS),
983 VMX_SEGMENT_FIELD(TR),
984 VMX_SEGMENT_FIELD(LDTR),
985};
986
Avi Kivity26bb0982009-09-07 11:14:12 +0300987static u64 host_efer;
988
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300989static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
990
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300991/*
Brian Gerst8c065852010-07-17 09:03:26 -0400992 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300993 * away by decrementing the array size.
994 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800996#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300997 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400999 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001
Jan Kiszka5bb16012016-02-09 20:14:21 +01001002static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003{
1004 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1005 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001006 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1007}
1008
Jan Kiszka6f054852016-02-09 20:15:18 +01001009static inline bool is_debug(u32 intr_info)
1010{
1011 return is_exception_n(intr_info, DB_VECTOR);
1012}
1013
1014static inline bool is_breakpoint(u32 intr_info)
1015{
1016 return is_exception_n(intr_info, BP_VECTOR);
1017}
1018
Jan Kiszka5bb16012016-02-09 20:14:21 +01001019static inline bool is_page_fault(u32 intr_info)
1020{
1021 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022}
1023
Gui Jianfeng31299942010-03-15 17:29:09 +08001024static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001025{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001026 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001027}
1028
Gui Jianfeng31299942010-03-15 17:29:09 +08001029static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001030{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001031 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001032}
1033
Gui Jianfeng31299942010-03-15 17:29:09 +08001034static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035{
1036 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1037 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1038}
1039
Gui Jianfeng31299942010-03-15 17:29:09 +08001040static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001041{
1042 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1043 INTR_INFO_VALID_MASK)) ==
1044 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1045}
1046
Gui Jianfeng31299942010-03-15 17:29:09 +08001047static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001048{
Sheng Yang04547152009-04-01 15:52:31 +08001049 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001050}
1051
Gui Jianfeng31299942010-03-15 17:29:09 +08001052static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001053{
Sheng Yang04547152009-04-01 15:52:31 +08001054 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001055}
1056
Paolo Bonzini35754c92015-07-29 12:05:37 +02001057static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001058{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001059 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001060}
1061
Gui Jianfeng31299942010-03-15 17:29:09 +08001062static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001063{
Sheng Yang04547152009-04-01 15:52:31 +08001064 return vmcs_config.cpu_based_exec_ctrl &
1065 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001066}
1067
Avi Kivity774ead32007-12-26 13:57:04 +02001068static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001069{
Sheng Yang04547152009-04-01 15:52:31 +08001070 return vmcs_config.cpu_based_2nd_exec_ctrl &
1071 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1072}
1073
Yang Zhang8d146952013-01-25 10:18:50 +08001074static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1075{
1076 return vmcs_config.cpu_based_2nd_exec_ctrl &
1077 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1078}
1079
Yang Zhang83d4c282013-01-25 10:18:49 +08001080static inline bool cpu_has_vmx_apic_register_virt(void)
1081{
1082 return vmcs_config.cpu_based_2nd_exec_ctrl &
1083 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1084}
1085
Yang Zhangc7c9c562013-01-25 10:18:51 +08001086static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1087{
1088 return vmcs_config.cpu_based_2nd_exec_ctrl &
1089 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1090}
1091
Yunhong Jiang64672c92016-06-13 14:19:59 -07001092/*
1093 * Comment's format: document - errata name - stepping - processor name.
1094 * Refer from
1095 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1096 */
1097static u32 vmx_preemption_cpu_tfms[] = {
1098/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
10990x000206E6,
1100/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1101/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1102/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
11030x00020652,
1104/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
11050x00020655,
1106/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1107/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1108/*
1109 * 320767.pdf - AAP86 - B1 -
1110 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1111 */
11120x000106E5,
1113/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
11140x000106A0,
1115/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
11160x000106A1,
1117/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
11180x000106A4,
1119 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1120 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1121 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
11220x000106A5,
1123};
1124
1125static inline bool cpu_has_broken_vmx_preemption_timer(void)
1126{
1127 u32 eax = cpuid_eax(0x00000001), i;
1128
1129 /* Clear the reserved bits */
1130 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001131 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001132 if (eax == vmx_preemption_cpu_tfms[i])
1133 return true;
1134
1135 return false;
1136}
1137
1138static inline bool cpu_has_vmx_preemption_timer(void)
1139{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001140 return vmcs_config.pin_based_exec_ctrl &
1141 PIN_BASED_VMX_PREEMPTION_TIMER;
1142}
1143
Yang Zhang01e439b2013-04-11 19:25:12 +08001144static inline bool cpu_has_vmx_posted_intr(void)
1145{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001146 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1147 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001148}
1149
1150static inline bool cpu_has_vmx_apicv(void)
1151{
1152 return cpu_has_vmx_apic_register_virt() &&
1153 cpu_has_vmx_virtual_intr_delivery() &&
1154 cpu_has_vmx_posted_intr();
1155}
1156
Sheng Yang04547152009-04-01 15:52:31 +08001157static inline bool cpu_has_vmx_flexpriority(void)
1158{
1159 return cpu_has_vmx_tpr_shadow() &&
1160 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001161}
1162
Marcelo Tosattie7997942009-06-11 12:07:40 -03001163static inline bool cpu_has_vmx_ept_execute_only(void)
1164{
Gui Jianfeng31299942010-03-15 17:29:09 +08001165 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001166}
1167
Marcelo Tosattie7997942009-06-11 12:07:40 -03001168static inline bool cpu_has_vmx_ept_2m_page(void)
1169{
Gui Jianfeng31299942010-03-15 17:29:09 +08001170 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001171}
1172
Sheng Yang878403b2010-01-05 19:02:29 +08001173static inline bool cpu_has_vmx_ept_1g_page(void)
1174{
Gui Jianfeng31299942010-03-15 17:29:09 +08001175 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001176}
1177
Sheng Yang4bc9b982010-06-02 14:05:24 +08001178static inline bool cpu_has_vmx_ept_4levels(void)
1179{
1180 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1181}
1182
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001183static inline bool cpu_has_vmx_ept_mt_wb(void)
1184{
1185 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1186}
1187
Yu Zhang855feb62017-08-24 20:27:55 +08001188static inline bool cpu_has_vmx_ept_5levels(void)
1189{
1190 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1191}
1192
Xudong Hao83c3a332012-05-28 19:33:35 +08001193static inline bool cpu_has_vmx_ept_ad_bits(void)
1194{
1195 return vmx_capability.ept & VMX_EPT_AD_BIT;
1196}
1197
Gui Jianfeng31299942010-03-15 17:29:09 +08001198static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001199{
Gui Jianfeng31299942010-03-15 17:29:09 +08001200 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001201}
1202
Gui Jianfeng31299942010-03-15 17:29:09 +08001203static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001204{
Gui Jianfeng31299942010-03-15 17:29:09 +08001205 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001206}
1207
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001208static inline bool cpu_has_vmx_invvpid_single(void)
1209{
1210 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1211}
1212
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001213static inline bool cpu_has_vmx_invvpid_global(void)
1214{
1215 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1216}
1217
Wanpeng Li08d839c2017-03-23 05:30:08 -07001218static inline bool cpu_has_vmx_invvpid(void)
1219{
1220 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1221}
1222
Gui Jianfeng31299942010-03-15 17:29:09 +08001223static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001224{
Sheng Yang04547152009-04-01 15:52:31 +08001225 return vmcs_config.cpu_based_2nd_exec_ctrl &
1226 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001227}
1228
Gui Jianfeng31299942010-03-15 17:29:09 +08001229static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001230{
1231 return vmcs_config.cpu_based_2nd_exec_ctrl &
1232 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1233}
1234
Gui Jianfeng31299942010-03-15 17:29:09 +08001235static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001236{
1237 return vmcs_config.cpu_based_2nd_exec_ctrl &
1238 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1239}
1240
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001241static inline bool cpu_has_vmx_basic_inout(void)
1242{
1243 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1244}
1245
Paolo Bonzini35754c92015-07-29 12:05:37 +02001246static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001247{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001248 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001249}
1250
Gui Jianfeng31299942010-03-15 17:29:09 +08001251static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001252{
Sheng Yang04547152009-04-01 15:52:31 +08001253 return vmcs_config.cpu_based_2nd_exec_ctrl &
1254 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001255}
1256
Gui Jianfeng31299942010-03-15 17:29:09 +08001257static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001258{
1259 return vmcs_config.cpu_based_2nd_exec_ctrl &
1260 SECONDARY_EXEC_RDTSCP;
1261}
1262
Mao, Junjiead756a12012-07-02 01:18:48 +00001263static inline bool cpu_has_vmx_invpcid(void)
1264{
1265 return vmcs_config.cpu_based_2nd_exec_ctrl &
1266 SECONDARY_EXEC_ENABLE_INVPCID;
1267}
1268
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001269static inline bool cpu_has_virtual_nmis(void)
1270{
1271 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1272}
1273
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001274static inline bool cpu_has_vmx_wbinvd_exit(void)
1275{
1276 return vmcs_config.cpu_based_2nd_exec_ctrl &
1277 SECONDARY_EXEC_WBINVD_EXITING;
1278}
1279
Abel Gordonabc4fc52013-04-18 14:35:25 +03001280static inline bool cpu_has_vmx_shadow_vmcs(void)
1281{
1282 u64 vmx_msr;
1283 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1284 /* check if the cpu supports writing r/o exit information fields */
1285 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1286 return false;
1287
1288 return vmcs_config.cpu_based_2nd_exec_ctrl &
1289 SECONDARY_EXEC_SHADOW_VMCS;
1290}
1291
Kai Huang843e4332015-01-28 10:54:28 +08001292static inline bool cpu_has_vmx_pml(void)
1293{
1294 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1295}
1296
Haozhong Zhang64903d62015-10-20 15:39:09 +08001297static inline bool cpu_has_vmx_tsc_scaling(void)
1298{
1299 return vmcs_config.cpu_based_2nd_exec_ctrl &
1300 SECONDARY_EXEC_TSC_SCALING;
1301}
1302
Bandan Das2a499e42017-08-03 15:54:41 -04001303static inline bool cpu_has_vmx_vmfunc(void)
1304{
1305 return vmcs_config.cpu_based_2nd_exec_ctrl &
1306 SECONDARY_EXEC_ENABLE_VMFUNC;
1307}
1308
Sheng Yang04547152009-04-01 15:52:31 +08001309static inline bool report_flexpriority(void)
1310{
1311 return flexpriority_enabled;
1312}
1313
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001314static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1315{
1316 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.nested_vmx_misc_low);
1317}
1318
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001319static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1320{
1321 return vmcs12->cpu_based_vm_exec_control & bit;
1322}
1323
1324static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1325{
1326 return (vmcs12->cpu_based_vm_exec_control &
1327 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1328 (vmcs12->secondary_vm_exec_control & bit);
1329}
1330
Jan Kiszkaf4124502014-03-07 20:03:13 +01001331static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1332{
1333 return vmcs12->pin_based_vm_exec_control &
1334 PIN_BASED_VMX_PREEMPTION_TIMER;
1335}
1336
Nadav Har'El155a97a2013-08-05 11:07:16 +03001337static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1338{
1339 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1340}
1341
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001342static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1343{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001344 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001345}
1346
Bandan Dasc5f983f2017-05-05 15:25:14 -04001347static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1348{
1349 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1350}
1351
Wincy Vanf2b93282015-02-03 23:56:03 +08001352static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1353{
1354 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1355}
1356
Wanpeng Li5c614b32015-10-13 09:18:36 -07001357static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1358{
1359 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1360}
1361
Wincy Van82f0dd42015-02-03 23:57:18 +08001362static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1363{
1364 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1365}
1366
Wincy Van608406e2015-02-03 23:57:51 +08001367static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1368{
1369 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1370}
1371
Wincy Van705699a2015-02-03 23:58:17 +08001372static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1373{
1374 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1375}
1376
Bandan Das27c42a12017-08-03 15:54:42 -04001377static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1378{
1379 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1380}
1381
Bandan Das41ab9372017-08-03 15:54:43 -04001382static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1383{
1384 return nested_cpu_has_vmfunc(vmcs12) &&
1385 (vmcs12->vm_function_control &
1386 VMX_VMFUNC_EPTP_SWITCHING);
1387}
1388
Jim Mattsonef85b672016-12-12 11:01:37 -08001389static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001390{
1391 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001392 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001393}
1394
Jan Kiszka533558b2014-01-04 18:47:20 +01001395static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1396 u32 exit_intr_info,
1397 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001398static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1399 struct vmcs12 *vmcs12,
1400 u32 reason, unsigned long qualification);
1401
Rusty Russell8b9cf982007-07-30 16:31:43 +10001402static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001403{
1404 int i;
1405
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001406 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001407 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001408 return i;
1409 return -1;
1410}
1411
Sheng Yang2384d2b2008-01-17 15:14:33 +08001412static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1413{
1414 struct {
1415 u64 vpid : 16;
1416 u64 rsvd : 48;
1417 u64 gva;
1418 } operand = { vpid, 0, gva };
1419
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001420 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001421 /* CF==1 or ZF==1 --> rc = -1 */
1422 "; ja 1f ; ud2 ; 1:"
1423 : : "a"(&operand), "c"(ext) : "cc", "memory");
1424}
1425
Sheng Yang14394422008-04-28 12:24:45 +08001426static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1427{
1428 struct {
1429 u64 eptp, gpa;
1430 } operand = {eptp, gpa};
1431
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001432 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001433 /* CF==1 or ZF==1 --> rc = -1 */
1434 "; ja 1f ; ud2 ; 1:\n"
1435 : : "a" (&operand), "c" (ext) : "cc", "memory");
1436}
1437
Avi Kivity26bb0982009-09-07 11:14:12 +03001438static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001439{
1440 int i;
1441
Rusty Russell8b9cf982007-07-30 16:31:43 +10001442 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001443 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001444 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001445 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001446}
1447
Avi Kivity6aa8b732006-12-10 02:21:36 -08001448static void vmcs_clear(struct vmcs *vmcs)
1449{
1450 u64 phys_addr = __pa(vmcs);
1451 u8 error;
1452
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001453 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001454 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455 : "cc", "memory");
1456 if (error)
1457 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1458 vmcs, phys_addr);
1459}
1460
Nadav Har'Eld462b812011-05-24 15:26:10 +03001461static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1462{
1463 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001464 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1465 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001466 loaded_vmcs->cpu = -1;
1467 loaded_vmcs->launched = 0;
1468}
1469
Dongxiao Xu7725b892010-05-11 18:29:38 +08001470static void vmcs_load(struct vmcs *vmcs)
1471{
1472 u64 phys_addr = __pa(vmcs);
1473 u8 error;
1474
1475 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001476 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001477 : "cc", "memory");
1478 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001479 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001480 vmcs, phys_addr);
1481}
1482
Dave Young2965faa2015-09-09 15:38:55 -07001483#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001484/*
1485 * This bitmap is used to indicate whether the vmclear
1486 * operation is enabled on all cpus. All disabled by
1487 * default.
1488 */
1489static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1490
1491static inline void crash_enable_local_vmclear(int cpu)
1492{
1493 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1494}
1495
1496static inline void crash_disable_local_vmclear(int cpu)
1497{
1498 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1499}
1500
1501static inline int crash_local_vmclear_enabled(int cpu)
1502{
1503 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1504}
1505
1506static void crash_vmclear_local_loaded_vmcss(void)
1507{
1508 int cpu = raw_smp_processor_id();
1509 struct loaded_vmcs *v;
1510
1511 if (!crash_local_vmclear_enabled(cpu))
1512 return;
1513
1514 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1515 loaded_vmcss_on_cpu_link)
1516 vmcs_clear(v->vmcs);
1517}
1518#else
1519static inline void crash_enable_local_vmclear(int cpu) { }
1520static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001521#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001522
Nadav Har'Eld462b812011-05-24 15:26:10 +03001523static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001525 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001526 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527
Nadav Har'Eld462b812011-05-24 15:26:10 +03001528 if (loaded_vmcs->cpu != cpu)
1529 return; /* vcpu migration can race with cpu offline */
1530 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001531 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001532 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001533 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001534
1535 /*
1536 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1537 * is before setting loaded_vmcs->vcpu to -1 which is done in
1538 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1539 * then adds the vmcs into percpu list before it is deleted.
1540 */
1541 smp_wmb();
1542
Nadav Har'Eld462b812011-05-24 15:26:10 +03001543 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001544 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001545}
1546
Nadav Har'Eld462b812011-05-24 15:26:10 +03001547static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001548{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001549 int cpu = loaded_vmcs->cpu;
1550
1551 if (cpu != -1)
1552 smp_call_function_single(cpu,
1553 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001554}
1555
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001556static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001557{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001558 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001559 return;
1560
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001561 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001562 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001563}
1564
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001565static inline void vpid_sync_vcpu_global(void)
1566{
1567 if (cpu_has_vmx_invvpid_global())
1568 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1569}
1570
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001571static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001572{
1573 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001574 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001575 else
1576 vpid_sync_vcpu_global();
1577}
1578
Sheng Yang14394422008-04-28 12:24:45 +08001579static inline void ept_sync_global(void)
1580{
David Hildenbrandf5f51582017-08-24 20:51:30 +02001581 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08001582}
1583
1584static inline void ept_sync_context(u64 eptp)
1585{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02001586 if (cpu_has_vmx_invept_context())
1587 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1588 else
1589 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08001590}
1591
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001592static __always_inline void vmcs_check16(unsigned long field)
1593{
1594 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1595 "16-bit accessor invalid for 64-bit field");
1596 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1597 "16-bit accessor invalid for 64-bit high field");
1598 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1599 "16-bit accessor invalid for 32-bit high field");
1600 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1601 "16-bit accessor invalid for natural width field");
1602}
1603
1604static __always_inline void vmcs_check32(unsigned long field)
1605{
1606 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1607 "32-bit accessor invalid for 16-bit field");
1608 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1609 "32-bit accessor invalid for natural width field");
1610}
1611
1612static __always_inline void vmcs_check64(unsigned long field)
1613{
1614 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1615 "64-bit accessor invalid for 16-bit field");
1616 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1617 "64-bit accessor invalid for 64-bit high field");
1618 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1619 "64-bit accessor invalid for 32-bit field");
1620 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1621 "64-bit accessor invalid for natural width field");
1622}
1623
1624static __always_inline void vmcs_checkl(unsigned long field)
1625{
1626 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1627 "Natural width accessor invalid for 16-bit field");
1628 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1629 "Natural width accessor invalid for 64-bit field");
1630 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1631 "Natural width accessor invalid for 64-bit high field");
1632 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1633 "Natural width accessor invalid for 32-bit field");
1634}
1635
1636static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001637{
Avi Kivity5e520e62011-05-15 10:13:12 -04001638 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639
Avi Kivity5e520e62011-05-15 10:13:12 -04001640 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1641 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642 return value;
1643}
1644
Avi Kivity96304212011-05-15 10:13:13 -04001645static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001647 vmcs_check16(field);
1648 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649}
1650
Avi Kivity96304212011-05-15 10:13:13 -04001651static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001653 vmcs_check32(field);
1654 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655}
1656
Avi Kivity96304212011-05-15 10:13:13 -04001657static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001659 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001660#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001661 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001663 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001664#endif
1665}
1666
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001667static __always_inline unsigned long vmcs_readl(unsigned long field)
1668{
1669 vmcs_checkl(field);
1670 return __vmcs_readl(field);
1671}
1672
Avi Kivitye52de1b2007-01-05 16:36:56 -08001673static noinline void vmwrite_error(unsigned long field, unsigned long value)
1674{
1675 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1676 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1677 dump_stack();
1678}
1679
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001680static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001681{
1682 u8 error;
1683
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001684 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001685 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001686 if (unlikely(error))
1687 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688}
1689
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001690static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001692 vmcs_check16(field);
1693 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001694}
1695
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001696static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001697{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001698 vmcs_check32(field);
1699 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001700}
1701
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001702static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001703{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001704 vmcs_check64(field);
1705 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001706#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001707 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001708 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709#endif
1710}
1711
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001712static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001713{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001714 vmcs_checkl(field);
1715 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001716}
1717
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001718static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001719{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001720 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1721 "vmcs_clear_bits does not support 64-bit fields");
1722 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1723}
1724
1725static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1726{
1727 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1728 "vmcs_set_bits does not support 64-bit fields");
1729 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001730}
1731
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001732static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1733{
1734 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1735}
1736
Gleb Natapov2961e8762013-11-25 15:37:13 +02001737static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1738{
1739 vmcs_write32(VM_ENTRY_CONTROLS, val);
1740 vmx->vm_entry_controls_shadow = val;
1741}
1742
1743static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1744{
1745 if (vmx->vm_entry_controls_shadow != val)
1746 vm_entry_controls_init(vmx, val);
1747}
1748
1749static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1750{
1751 return vmx->vm_entry_controls_shadow;
1752}
1753
1754
1755static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1756{
1757 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1758}
1759
1760static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1761{
1762 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1763}
1764
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001765static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1766{
1767 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1768}
1769
Gleb Natapov2961e8762013-11-25 15:37:13 +02001770static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1771{
1772 vmcs_write32(VM_EXIT_CONTROLS, val);
1773 vmx->vm_exit_controls_shadow = val;
1774}
1775
1776static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1777{
1778 if (vmx->vm_exit_controls_shadow != val)
1779 vm_exit_controls_init(vmx, val);
1780}
1781
1782static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1783{
1784 return vmx->vm_exit_controls_shadow;
1785}
1786
1787
1788static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1789{
1790 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1791}
1792
1793static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1794{
1795 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1796}
1797
Avi Kivity2fb92db2011-04-27 19:42:18 +03001798static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1799{
1800 vmx->segment_cache.bitmask = 0;
1801}
1802
1803static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1804 unsigned field)
1805{
1806 bool ret;
1807 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1808
1809 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1810 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1811 vmx->segment_cache.bitmask = 0;
1812 }
1813 ret = vmx->segment_cache.bitmask & mask;
1814 vmx->segment_cache.bitmask |= mask;
1815 return ret;
1816}
1817
1818static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1819{
1820 u16 *p = &vmx->segment_cache.seg[seg].selector;
1821
1822 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1823 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1824 return *p;
1825}
1826
1827static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1828{
1829 ulong *p = &vmx->segment_cache.seg[seg].base;
1830
1831 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1832 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1833 return *p;
1834}
1835
1836static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1837{
1838 u32 *p = &vmx->segment_cache.seg[seg].limit;
1839
1840 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1841 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1842 return *p;
1843}
1844
1845static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1846{
1847 u32 *p = &vmx->segment_cache.seg[seg].ar;
1848
1849 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1850 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1851 return *p;
1852}
1853
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001854static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1855{
1856 u32 eb;
1857
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001858 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08001859 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001860 if ((vcpu->guest_debug &
1861 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1862 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1863 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001864 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001865 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001866 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001867 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001868
1869 /* When we are running a nested L2 guest and L1 specified for it a
1870 * certain exception bitmap, we must trap the same exceptions and pass
1871 * them to L1. When running L2, we will only handle the exceptions
1872 * specified above if L1 did not want them.
1873 */
1874 if (is_guest_mode(vcpu))
1875 eb |= get_vmcs12(vcpu)->exception_bitmap;
1876
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001877 vmcs_write32(EXCEPTION_BITMAP, eb);
1878}
1879
Ashok Raj15d45072018-02-01 22:59:43 +01001880/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001881 * Check if MSR is intercepted for currently loaded MSR bitmap.
1882 */
1883static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
1884{
1885 unsigned long *msr_bitmap;
1886 int f = sizeof(unsigned long);
1887
1888 if (!cpu_has_vmx_msr_bitmap())
1889 return true;
1890
1891 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
1892
1893 if (msr <= 0x1fff) {
1894 return !!test_bit(msr, msr_bitmap + 0x800 / f);
1895 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
1896 msr &= 0x1fff;
1897 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
1898 }
1899
1900 return true;
1901}
1902
1903/*
Ashok Raj15d45072018-02-01 22:59:43 +01001904 * Check if MSR is intercepted for L01 MSR bitmap.
1905 */
1906static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
1907{
1908 unsigned long *msr_bitmap;
1909 int f = sizeof(unsigned long);
1910
1911 if (!cpu_has_vmx_msr_bitmap())
1912 return true;
1913
1914 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
1915
1916 if (msr <= 0x1fff) {
1917 return !!test_bit(msr, msr_bitmap + 0x800 / f);
1918 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
1919 msr &= 0x1fff;
1920 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
1921 }
1922
1923 return true;
1924}
1925
Gleb Natapov2961e8762013-11-25 15:37:13 +02001926static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1927 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001928{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001929 vm_entry_controls_clearbit(vmx, entry);
1930 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001931}
1932
Avi Kivity61d2ef22010-04-28 16:40:38 +03001933static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1934{
1935 unsigned i;
1936 struct msr_autoload *m = &vmx->msr_autoload;
1937
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001938 switch (msr) {
1939 case MSR_EFER:
1940 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001941 clear_atomic_switch_msr_special(vmx,
1942 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001943 VM_EXIT_LOAD_IA32_EFER);
1944 return;
1945 }
1946 break;
1947 case MSR_CORE_PERF_GLOBAL_CTRL:
1948 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001949 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001950 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1951 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1952 return;
1953 }
1954 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001955 }
1956
Avi Kivity61d2ef22010-04-28 16:40:38 +03001957 for (i = 0; i < m->nr; ++i)
1958 if (m->guest[i].index == msr)
1959 break;
1960
1961 if (i == m->nr)
1962 return;
1963 --m->nr;
1964 m->guest[i] = m->guest[m->nr];
1965 m->host[i] = m->host[m->nr];
1966 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1967 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1968}
1969
Gleb Natapov2961e8762013-11-25 15:37:13 +02001970static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1971 unsigned long entry, unsigned long exit,
1972 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1973 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001974{
1975 vmcs_write64(guest_val_vmcs, guest_val);
1976 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001977 vm_entry_controls_setbit(vmx, entry);
1978 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001979}
1980
Avi Kivity61d2ef22010-04-28 16:40:38 +03001981static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1982 u64 guest_val, u64 host_val)
1983{
1984 unsigned i;
1985 struct msr_autoload *m = &vmx->msr_autoload;
1986
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001987 switch (msr) {
1988 case MSR_EFER:
1989 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001990 add_atomic_switch_msr_special(vmx,
1991 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001992 VM_EXIT_LOAD_IA32_EFER,
1993 GUEST_IA32_EFER,
1994 HOST_IA32_EFER,
1995 guest_val, host_val);
1996 return;
1997 }
1998 break;
1999 case MSR_CORE_PERF_GLOBAL_CTRL:
2000 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002001 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002002 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2003 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2004 GUEST_IA32_PERF_GLOBAL_CTRL,
2005 HOST_IA32_PERF_GLOBAL_CTRL,
2006 guest_val, host_val);
2007 return;
2008 }
2009 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002010 case MSR_IA32_PEBS_ENABLE:
2011 /* PEBS needs a quiescent period after being disabled (to write
2012 * a record). Disabling PEBS through VMX MSR swapping doesn't
2013 * provide that period, so a CPU could write host's record into
2014 * guest's memory.
2015 */
2016 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002017 }
2018
Avi Kivity61d2ef22010-04-28 16:40:38 +03002019 for (i = 0; i < m->nr; ++i)
2020 if (m->guest[i].index == msr)
2021 break;
2022
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002023 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002024 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002025 "Can't add msr %x\n", msr);
2026 return;
2027 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002028 ++m->nr;
2029 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2030 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2031 }
2032
2033 m->guest[i].index = msr;
2034 m->guest[i].value = guest_val;
2035 m->host[i].index = msr;
2036 m->host[i].value = host_val;
2037}
2038
Avi Kivity92c0d902009-10-29 11:00:16 +02002039static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002040{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002041 u64 guest_efer = vmx->vcpu.arch.efer;
2042 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002043
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002044 if (!enable_ept) {
2045 /*
2046 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2047 * host CPUID is more efficient than testing guest CPUID
2048 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2049 */
2050 if (boot_cpu_has(X86_FEATURE_SMEP))
2051 guest_efer |= EFER_NX;
2052 else if (!(guest_efer & EFER_NX))
2053 ignore_bits |= EFER_NX;
2054 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002055
Avi Kivity51c6cf62007-08-29 03:48:05 +03002056 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002057 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002058 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002059 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002060#ifdef CONFIG_X86_64
2061 ignore_bits |= EFER_LMA | EFER_LME;
2062 /* SCE is meaningful only in long mode on Intel */
2063 if (guest_efer & EFER_LMA)
2064 ignore_bits &= ~(u64)EFER_SCE;
2065#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002066
2067 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002068
2069 /*
2070 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2071 * On CPUs that support "load IA32_EFER", always switch EFER
2072 * atomically, since it's faster than switching it manually.
2073 */
2074 if (cpu_has_load_ia32_efer ||
2075 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002076 if (!(guest_efer & EFER_LMA))
2077 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002078 if (guest_efer != host_efer)
2079 add_atomic_switch_msr(vmx, MSR_EFER,
2080 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002081 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002082 } else {
2083 guest_efer &= ~ignore_bits;
2084 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002085
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002086 vmx->guest_msrs[efer_offset].data = guest_efer;
2087 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2088
2089 return true;
2090 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002091}
2092
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002093#ifdef CONFIG_X86_32
2094/*
2095 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2096 * VMCS rather than the segment table. KVM uses this helper to figure
2097 * out the current bases to poke them into the VMCS before entry.
2098 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002099static unsigned long segment_base(u16 selector)
2100{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002101 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002102 unsigned long v;
2103
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002104 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002105 return 0;
2106
Thomas Garnier45fc8752017-03-14 10:05:08 -07002107 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002108
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002109 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002110 u16 ldt_selector = kvm_read_ldt();
2111
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002112 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002113 return 0;
2114
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002115 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002116 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002117 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002118 return v;
2119}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002120#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002121
Avi Kivity04d2cc72007-09-10 18:10:54 +03002122static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002123{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002124 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002125 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002126
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002127 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002128 return;
2129
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002130 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002131 /*
2132 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2133 * allow segment selectors with cpl > 0 or ti == 1.
2134 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002135 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002136 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002137 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002138 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002139 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002140 vmx->host_state.fs_reload_needed = 0;
2141 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002142 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002143 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002144 }
Avi Kivity9581d442010-10-19 16:46:55 +02002145 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002146 if (!(vmx->host_state.gs_sel & 7))
2147 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002148 else {
2149 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002150 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002151 }
2152
2153#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002154 savesegment(ds, vmx->host_state.ds_sel);
2155 savesegment(es, vmx->host_state.es_sel);
2156#endif
2157
2158#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002159 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2160 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2161#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002162 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2163 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002164#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002165
2166#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002167 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2168 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002169 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002170#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002171 if (boot_cpu_has(X86_FEATURE_MPX))
2172 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002173 for (i = 0; i < vmx->save_nmsrs; ++i)
2174 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002175 vmx->guest_msrs[i].data,
2176 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002177}
2178
Avi Kivitya9b21b62008-06-24 11:48:49 +03002179static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002180{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002181 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002182 return;
2183
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002184 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002185 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002186#ifdef CONFIG_X86_64
2187 if (is_long_mode(&vmx->vcpu))
2188 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2189#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002190 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002191 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002192#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002193 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002194#else
2195 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002196#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002197 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002198 if (vmx->host_state.fs_reload_needed)
2199 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002200#ifdef CONFIG_X86_64
2201 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2202 loadsegment(ds, vmx->host_state.ds_sel);
2203 loadsegment(es, vmx->host_state.es_sel);
2204 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002205#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002206 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002207#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002208 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002209#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002210 if (vmx->host_state.msr_host_bndcfgs)
2211 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002212 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002213}
2214
Avi Kivitya9b21b62008-06-24 11:48:49 +03002215static void vmx_load_host_state(struct vcpu_vmx *vmx)
2216{
2217 preempt_disable();
2218 __vmx_load_host_state(vmx);
2219 preempt_enable();
2220}
2221
Feng Wu28b835d2015-09-18 22:29:54 +08002222static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2223{
2224 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2225 struct pi_desc old, new;
2226 unsigned int dest;
2227
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002228 /*
2229 * In case of hot-plug or hot-unplug, we may have to undo
2230 * vmx_vcpu_pi_put even if there is no assigned device. And we
2231 * always keep PI.NDST up to date for simplicity: it makes the
2232 * code easier, and CPU migration is not a fast path.
2233 */
2234 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002235 return;
2236
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002237 /*
2238 * First handle the simple case where no cmpxchg is necessary; just
2239 * allow posting non-urgent interrupts.
2240 *
2241 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2242 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2243 * expects the VCPU to be on the blocked_vcpu_list that matches
2244 * PI.NDST.
2245 */
2246 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2247 vcpu->cpu == cpu) {
2248 pi_clear_sn(pi_desc);
2249 return;
2250 }
2251
2252 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002253 do {
2254 old.control = new.control = pi_desc->control;
2255
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002256 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002257
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002258 if (x2apic_enabled())
2259 new.ndst = dest;
2260 else
2261 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002262
Feng Wu28b835d2015-09-18 22:29:54 +08002263 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002264 } while (cmpxchg64(&pi_desc->control, old.control,
2265 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002266}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002267
Peter Feinerc95ba922016-08-17 09:36:47 -07002268static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2269{
2270 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2271 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2272}
2273
Avi Kivity6aa8b732006-12-10 02:21:36 -08002274/*
2275 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2276 * vcpu mutex is already taken.
2277 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002278static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002280 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002281 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002282
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002283 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002284 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002285 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002286 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002287
2288 /*
2289 * Read loaded_vmcs->cpu should be before fetching
2290 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2291 * See the comments in __loaded_vmcs_clear().
2292 */
2293 smp_rmb();
2294
Nadav Har'Eld462b812011-05-24 15:26:10 +03002295 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2296 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002297 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002298 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002299 }
2300
2301 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2302 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2303 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002304 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002305 }
2306
2307 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002308 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002309 unsigned long sysenter_esp;
2310
2311 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002312
Avi Kivity6aa8b732006-12-10 02:21:36 -08002313 /*
2314 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002315 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002317 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002318 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002319 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002320
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002321 /*
2322 * VM exits change the host TR limit to 0x67 after a VM
2323 * exit. This is okay, since 0x67 covers everything except
2324 * the IO bitmap and have have code to handle the IO bitmap
2325 * being lost after a VM exit.
2326 */
2327 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2328
Avi Kivity6aa8b732006-12-10 02:21:36 -08002329 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2330 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002331
Nadav Har'Eld462b812011-05-24 15:26:10 +03002332 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333 }
Feng Wu28b835d2015-09-18 22:29:54 +08002334
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002335 /* Setup TSC multiplier */
2336 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002337 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2338 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002339
Feng Wu28b835d2015-09-18 22:29:54 +08002340 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002341 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002342 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002343}
2344
2345static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2346{
2347 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2348
2349 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002350 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2351 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002352 return;
2353
2354 /* Set SN when the vCPU is preempted */
2355 if (vcpu->preempted)
2356 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002357}
2358
2359static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2360{
Feng Wu28b835d2015-09-18 22:29:54 +08002361 vmx_vcpu_pi_put(vcpu);
2362
Avi Kivitya9b21b62008-06-24 11:48:49 +03002363 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364}
2365
Wanpeng Lif244dee2017-07-20 01:11:54 -07002366static bool emulation_required(struct kvm_vcpu *vcpu)
2367{
2368 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2369}
2370
Avi Kivityedcafe32009-12-30 18:07:40 +02002371static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2372
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002373/*
2374 * Return the cr0 value that a nested guest would read. This is a combination
2375 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2376 * its hypervisor (cr0_read_shadow).
2377 */
2378static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2379{
2380 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2381 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2382}
2383static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2384{
2385 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2386 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2387}
2388
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2390{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002391 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002392
Avi Kivity6de12732011-03-07 12:51:22 +02002393 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2394 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2395 rflags = vmcs_readl(GUEST_RFLAGS);
2396 if (to_vmx(vcpu)->rmode.vm86_active) {
2397 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2398 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2399 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2400 }
2401 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002402 }
Avi Kivity6de12732011-03-07 12:51:22 +02002403 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002404}
2405
2406static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2407{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002408 unsigned long old_rflags = vmx_get_rflags(vcpu);
2409
Avi Kivity6de12732011-03-07 12:51:22 +02002410 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2411 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002412 if (to_vmx(vcpu)->rmode.vm86_active) {
2413 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002414 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002415 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002416 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002417
2418 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2419 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002420}
2421
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002422static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002423{
2424 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2425 int ret = 0;
2426
2427 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002428 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002429 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002430 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002431
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002432 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002433}
2434
2435static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2436{
2437 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2438 u32 interruptibility = interruptibility_old;
2439
2440 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2441
Jan Kiszka48005f62010-02-19 19:38:07 +01002442 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002443 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002444 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002445 interruptibility |= GUEST_INTR_STATE_STI;
2446
2447 if ((interruptibility != interruptibility_old))
2448 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2449}
2450
Avi Kivity6aa8b732006-12-10 02:21:36 -08002451static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2452{
2453 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002455 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002456 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002457 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458
Glauber Costa2809f5d2009-05-12 16:21:05 -04002459 /* skipping an emulated instruction also counts */
2460 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002461}
2462
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002463static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2464 unsigned long exit_qual)
2465{
2466 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2467 unsigned int nr = vcpu->arch.exception.nr;
2468 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2469
2470 if (vcpu->arch.exception.has_error_code) {
2471 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2472 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2473 }
2474
2475 if (kvm_exception_is_soft(nr))
2476 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2477 else
2478 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2479
2480 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2481 vmx_get_nmi_mask(vcpu))
2482 intr_info |= INTR_INFO_UNBLOCK_NMI;
2483
2484 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2485}
2486
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002487/*
2488 * KVM wants to inject page-faults which it got to the guest. This function
2489 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002490 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002491static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002492{
2493 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002494 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002495
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002496 if (nr == PF_VECTOR) {
2497 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002498 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002499 return 1;
2500 }
2501 /*
2502 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2503 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2504 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2505 * can be written only when inject_pending_event runs. This should be
2506 * conditional on a new capability---if the capability is disabled,
2507 * kvm_multiple_exception would write the ancillary information to
2508 * CR2 or DR6, for backwards ABI-compatibility.
2509 */
2510 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2511 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002512 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002513 return 1;
2514 }
2515 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002516 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002517 if (nr == DB_VECTOR)
2518 *exit_qual = vcpu->arch.dr6;
2519 else
2520 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002521 return 1;
2522 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002523 }
2524
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002525 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002526}
2527
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002528static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02002529{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002530 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002531 unsigned nr = vcpu->arch.exception.nr;
2532 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002533 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002534 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002535
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002536 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002537 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002538 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2539 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002540
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002541 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002542 int inc_eip = 0;
2543 if (kvm_exception_is_soft(nr))
2544 inc_eip = vcpu->arch.event_exit_inst_len;
2545 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002546 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002547 return;
2548 }
2549
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002550 if (kvm_exception_is_soft(nr)) {
2551 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2552 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002553 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2554 } else
2555 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2556
2557 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002558}
2559
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002560static bool vmx_rdtscp_supported(void)
2561{
2562 return cpu_has_vmx_rdtscp();
2563}
2564
Mao, Junjiead756a12012-07-02 01:18:48 +00002565static bool vmx_invpcid_supported(void)
2566{
2567 return cpu_has_vmx_invpcid() && enable_ept;
2568}
2569
Avi Kivity6aa8b732006-12-10 02:21:36 -08002570/*
Eddie Donga75beee2007-05-17 18:55:15 +03002571 * Swap MSR entry in host/guest MSR entry array.
2572 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002573static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002574{
Avi Kivity26bb0982009-09-07 11:14:12 +03002575 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002576
2577 tmp = vmx->guest_msrs[to];
2578 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2579 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002580}
2581
2582/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002583 * Set up the vmcs to automatically save and restore system
2584 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2585 * mode, as fiddling with msrs is very expensive.
2586 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002587static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002588{
Avi Kivity26bb0982009-09-07 11:14:12 +03002589 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002590
Eddie Donga75beee2007-05-17 18:55:15 +03002591 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002592#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002593 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002594 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002595 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002596 move_msr_up(vmx, index, save_nmsrs++);
2597 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002598 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002599 move_msr_up(vmx, index, save_nmsrs++);
2600 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002601 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002602 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002603 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002604 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002605 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002606 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002607 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002608 * if efer.sce is enabled.
2609 */
Brian Gerst8c065852010-07-17 09:03:26 -04002610 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002611 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002612 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002613 }
Eddie Donga75beee2007-05-17 18:55:15 +03002614#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002615 index = __find_msr_index(vmx, MSR_EFER);
2616 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002617 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002618
Avi Kivity26bb0982009-09-07 11:14:12 +03002619 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002620
Yang Zhang8d146952013-01-25 10:18:50 +08002621 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002622 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002623}
2624
2625/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002627 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2628 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002629 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002630static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002631{
2632 u64 host_tsc, tsc_offset;
2633
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002634 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002636 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637}
2638
2639/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002640 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002642static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002643{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002644 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002645 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002646 * We're here if L1 chose not to trap WRMSR to TSC. According
2647 * to the spec, this should set L1's TSC; The offset that L1
2648 * set for L2 remains unchanged, and still needs to be added
2649 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002650 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002651 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002652 /* recalculate vmcs02.TSC_OFFSET: */
2653 vmcs12 = get_vmcs12(vcpu);
2654 vmcs_write64(TSC_OFFSET, offset +
2655 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2656 vmcs12->tsc_offset : 0));
2657 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002658 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2659 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002660 vmcs_write64(TSC_OFFSET, offset);
2661 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662}
2663
Nadav Har'El801d3422011-05-25 23:02:23 +03002664/*
2665 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2666 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2667 * all guests if the "nested" module option is off, and can also be disabled
2668 * for a single guest by disabling its VMX cpuid bit.
2669 */
2670static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2671{
Radim Krčmářd6321d42017-08-05 00:12:49 +02002672 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03002673}
2674
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002676 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2677 * returned for the various VMX controls MSRs when nested VMX is enabled.
2678 * The same values should also be used to verify that vmcs12 control fields are
2679 * valid during nested entry from L1 to L2.
2680 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2681 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2682 * bit in the high half is on if the corresponding bit in the control field
2683 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002684 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002685static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002686{
2687 /*
2688 * Note that as a general rule, the high half of the MSRs (bits in
2689 * the control fields which may be 1) should be initialized by the
2690 * intersection of the underlying hardware's MSR (i.e., features which
2691 * can be supported) and the list of features we want to expose -
2692 * because they are known to be properly supported in our code.
2693 * Also, usually, the low half of the MSRs (bits which must be 1) can
2694 * be set to 0, meaning that L1 may turn off any of these bits. The
2695 * reason is that if one of these bits is necessary, it will appear
2696 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2697 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02002698 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002699 * These rules have exceptions below.
2700 */
2701
2702 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002703 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002704 vmx->nested.nested_vmx_pinbased_ctls_low,
2705 vmx->nested.nested_vmx_pinbased_ctls_high);
2706 vmx->nested.nested_vmx_pinbased_ctls_low |=
2707 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2708 vmx->nested.nested_vmx_pinbased_ctls_high &=
2709 PIN_BASED_EXT_INTR_MASK |
2710 PIN_BASED_NMI_EXITING |
2711 PIN_BASED_VIRTUAL_NMIS;
2712 vmx->nested.nested_vmx_pinbased_ctls_high |=
2713 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002714 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002715 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002716 vmx->nested.nested_vmx_pinbased_ctls_high |=
2717 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002718
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002719 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002720 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002721 vmx->nested.nested_vmx_exit_ctls_low,
2722 vmx->nested.nested_vmx_exit_ctls_high);
2723 vmx->nested.nested_vmx_exit_ctls_low =
2724 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002725
Wincy Vanb9c237b2015-02-03 23:56:30 +08002726 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002727#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002728 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002729#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01002730 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002731 vmx->nested.nested_vmx_exit_ctls_high |=
2732 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002733 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002734 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2735
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002736 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002737 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002738
Jan Kiszka2996fca2014-06-16 13:59:43 +02002739 /* We support free control of debug control saving. */
David Matlack0115f9c2016-11-29 18:14:06 -08002740 vmx->nested.nested_vmx_exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002741
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002742 /* entry controls */
2743 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002744 vmx->nested.nested_vmx_entry_ctls_low,
2745 vmx->nested.nested_vmx_entry_ctls_high);
2746 vmx->nested.nested_vmx_entry_ctls_low =
2747 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2748 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002749#ifdef CONFIG_X86_64
2750 VM_ENTRY_IA32E_MODE |
2751#endif
2752 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002753 vmx->nested.nested_vmx_entry_ctls_high |=
2754 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002755 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002756 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002757
Jan Kiszka2996fca2014-06-16 13:59:43 +02002758 /* We support free control of debug control loading. */
David Matlack0115f9c2016-11-29 18:14:06 -08002759 vmx->nested.nested_vmx_entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002760
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002761 /* cpu-based controls */
2762 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002763 vmx->nested.nested_vmx_procbased_ctls_low,
2764 vmx->nested.nested_vmx_procbased_ctls_high);
2765 vmx->nested.nested_vmx_procbased_ctls_low =
2766 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2767 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002768 CPU_BASED_VIRTUAL_INTR_PENDING |
2769 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002770 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2771 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2772 CPU_BASED_CR3_STORE_EXITING |
2773#ifdef CONFIG_X86_64
2774 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2775#endif
2776 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002777 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2778 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2779 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2780 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002781 /*
2782 * We can allow some features even when not supported by the
2783 * hardware. For example, L1 can specify an MSR bitmap - and we
2784 * can use it to avoid exits to L1 - even when L0 runs L2
2785 * without MSR bitmaps.
2786 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002787 vmx->nested.nested_vmx_procbased_ctls_high |=
2788 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002789 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002790
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002791 /* We support free control of CR3 access interception. */
David Matlack0115f9c2016-11-29 18:14:06 -08002792 vmx->nested.nested_vmx_procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002793 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2794
Paolo Bonzini80154d72017-08-24 13:55:35 +02002795 /*
2796 * secondary cpu-based controls. Do not include those that
2797 * depend on CPUID bits, they are added later by vmx_cpuid_update.
2798 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002799 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002800 vmx->nested.nested_vmx_secondary_ctls_low,
2801 vmx->nested.nested_vmx_secondary_ctls_high);
2802 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2803 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002804 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02002805 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08002806 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08002807 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002808 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02002809 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002810
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002811 if (enable_ept) {
2812 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002813 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002814 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002815 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002816 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002817 if (cpu_has_vmx_ept_execute_only())
2818 vmx->nested.nested_vmx_ept_caps |=
2819 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002820 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04002821 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002822 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
2823 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04002824 if (enable_ept_ad_bits) {
2825 vmx->nested.nested_vmx_secondary_ctls_high |=
2826 SECONDARY_EXEC_ENABLE_PML;
Dan Carpenter7461fbc2017-05-18 10:41:15 +03002827 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04002828 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02002829 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002830
Bandan Das27c42a12017-08-03 15:54:42 -04002831 if (cpu_has_vmx_vmfunc()) {
2832 vmx->nested.nested_vmx_secondary_ctls_high |=
2833 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04002834 /*
2835 * Advertise EPTP switching unconditionally
2836 * since we emulate it
2837 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08002838 if (enable_ept)
2839 vmx->nested.nested_vmx_vmfunc_controls =
2840 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04002841 }
2842
Paolo Bonzinief697a72016-03-18 16:58:38 +01002843 /*
2844 * Old versions of KVM use the single-context version without
2845 * checking for support, so declare that it is supported even
2846 * though it is treated as global context. The alternative is
2847 * not failing the single-context invvpid, and it is worse.
2848 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07002849 if (enable_vpid) {
2850 vmx->nested.nested_vmx_secondary_ctls_high |=
2851 SECONDARY_EXEC_ENABLE_VPID;
Wanpeng Li089d7b62015-10-13 09:18:37 -07002852 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03002853 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02002854 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002855
Radim Krčmář0790ec12015-03-17 14:02:32 +01002856 if (enable_unrestricted_guest)
2857 vmx->nested.nested_vmx_secondary_ctls_high |=
2858 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2859
Jan Kiszkac18911a2013-03-13 16:06:41 +01002860 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002861 rdmsr(MSR_IA32_VMX_MISC,
2862 vmx->nested.nested_vmx_misc_low,
2863 vmx->nested.nested_vmx_misc_high);
2864 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2865 vmx->nested.nested_vmx_misc_low |=
2866 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002867 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002868 vmx->nested.nested_vmx_misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002869
2870 /*
2871 * This MSR reports some information about VMX support. We
2872 * should return information about the VMX we emulate for the
2873 * guest, and the VMCS structure we give it - not about the
2874 * VMX support of the underlying hardware.
2875 */
2876 vmx->nested.nested_vmx_basic =
2877 VMCS12_REVISION |
2878 VMX_BASIC_TRUE_CTLS |
2879 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2880 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2881
2882 if (cpu_has_vmx_basic_inout())
2883 vmx->nested.nested_vmx_basic |= VMX_BASIC_INOUT;
2884
2885 /*
David Matlack8322ebb2016-11-29 18:14:09 -08002886 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08002887 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2888 * We picked the standard core2 setting.
2889 */
2890#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2891#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2892 vmx->nested.nested_vmx_cr0_fixed0 = VMXON_CR0_ALWAYSON;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002893 vmx->nested.nested_vmx_cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08002894
2895 /* These MSRs specify bits which the guest must keep fixed off. */
2896 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, vmx->nested.nested_vmx_cr0_fixed1);
2897 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, vmx->nested.nested_vmx_cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08002898
2899 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Jim Mattson5b157062017-12-22 12:11:12 -08002900 vmx->nested.nested_vmx_vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002901}
2902
David Matlack38991522016-11-29 18:14:08 -08002903/*
2904 * if fixed0[i] == 1: val[i] must be 1
2905 * if fixed1[i] == 0: val[i] must be 0
2906 */
2907static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
2908{
2909 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002910}
2911
2912static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2913{
David Matlack38991522016-11-29 18:14:08 -08002914 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002915}
2916
2917static inline u64 vmx_control_msr(u32 low, u32 high)
2918{
2919 return low | ((u64)high << 32);
2920}
2921
David Matlack62cc6b9d2016-11-29 18:14:07 -08002922static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
2923{
2924 superset &= mask;
2925 subset &= mask;
2926
2927 return (superset | subset) == superset;
2928}
2929
2930static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
2931{
2932 const u64 feature_and_reserved =
2933 /* feature (except bit 48; see below) */
2934 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
2935 /* reserved */
2936 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
2937 u64 vmx_basic = vmx->nested.nested_vmx_basic;
2938
2939 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
2940 return -EINVAL;
2941
2942 /*
2943 * KVM does not emulate a version of VMX that constrains physical
2944 * addresses of VMX structures (e.g. VMCS) to 32-bits.
2945 */
2946 if (data & BIT_ULL(48))
2947 return -EINVAL;
2948
2949 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
2950 vmx_basic_vmcs_revision_id(data))
2951 return -EINVAL;
2952
2953 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
2954 return -EINVAL;
2955
2956 vmx->nested.nested_vmx_basic = data;
2957 return 0;
2958}
2959
2960static int
2961vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
2962{
2963 u64 supported;
2964 u32 *lowp, *highp;
2965
2966 switch (msr_index) {
2967 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2968 lowp = &vmx->nested.nested_vmx_pinbased_ctls_low;
2969 highp = &vmx->nested.nested_vmx_pinbased_ctls_high;
2970 break;
2971 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2972 lowp = &vmx->nested.nested_vmx_procbased_ctls_low;
2973 highp = &vmx->nested.nested_vmx_procbased_ctls_high;
2974 break;
2975 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2976 lowp = &vmx->nested.nested_vmx_exit_ctls_low;
2977 highp = &vmx->nested.nested_vmx_exit_ctls_high;
2978 break;
2979 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2980 lowp = &vmx->nested.nested_vmx_entry_ctls_low;
2981 highp = &vmx->nested.nested_vmx_entry_ctls_high;
2982 break;
2983 case MSR_IA32_VMX_PROCBASED_CTLS2:
2984 lowp = &vmx->nested.nested_vmx_secondary_ctls_low;
2985 highp = &vmx->nested.nested_vmx_secondary_ctls_high;
2986 break;
2987 default:
2988 BUG();
2989 }
2990
2991 supported = vmx_control_msr(*lowp, *highp);
2992
2993 /* Check must-be-1 bits are still 1. */
2994 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
2995 return -EINVAL;
2996
2997 /* Check must-be-0 bits are still 0. */
2998 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
2999 return -EINVAL;
3000
3001 *lowp = data;
3002 *highp = data >> 32;
3003 return 0;
3004}
3005
3006static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3007{
3008 const u64 feature_and_reserved_bits =
3009 /* feature */
3010 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3011 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3012 /* reserved */
3013 GENMASK_ULL(13, 9) | BIT_ULL(31);
3014 u64 vmx_misc;
3015
3016 vmx_misc = vmx_control_msr(vmx->nested.nested_vmx_misc_low,
3017 vmx->nested.nested_vmx_misc_high);
3018
3019 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3020 return -EINVAL;
3021
3022 if ((vmx->nested.nested_vmx_pinbased_ctls_high &
3023 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3024 vmx_misc_preemption_timer_rate(data) !=
3025 vmx_misc_preemption_timer_rate(vmx_misc))
3026 return -EINVAL;
3027
3028 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3029 return -EINVAL;
3030
3031 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3032 return -EINVAL;
3033
3034 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3035 return -EINVAL;
3036
3037 vmx->nested.nested_vmx_misc_low = data;
3038 vmx->nested.nested_vmx_misc_high = data >> 32;
3039 return 0;
3040}
3041
3042static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3043{
3044 u64 vmx_ept_vpid_cap;
3045
3046 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.nested_vmx_ept_caps,
3047 vmx->nested.nested_vmx_vpid_caps);
3048
3049 /* Every bit is either reserved or a feature bit. */
3050 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3051 return -EINVAL;
3052
3053 vmx->nested.nested_vmx_ept_caps = data;
3054 vmx->nested.nested_vmx_vpid_caps = data >> 32;
3055 return 0;
3056}
3057
3058static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3059{
3060 u64 *msr;
3061
3062 switch (msr_index) {
3063 case MSR_IA32_VMX_CR0_FIXED0:
3064 msr = &vmx->nested.nested_vmx_cr0_fixed0;
3065 break;
3066 case MSR_IA32_VMX_CR4_FIXED0:
3067 msr = &vmx->nested.nested_vmx_cr4_fixed0;
3068 break;
3069 default:
3070 BUG();
3071 }
3072
3073 /*
3074 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3075 * must be 1 in the restored value.
3076 */
3077 if (!is_bitwise_subset(data, *msr, -1ULL))
3078 return -EINVAL;
3079
3080 *msr = data;
3081 return 0;
3082}
3083
3084/*
3085 * Called when userspace is restoring VMX MSRs.
3086 *
3087 * Returns 0 on success, non-0 otherwise.
3088 */
3089static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3090{
3091 struct vcpu_vmx *vmx = to_vmx(vcpu);
3092
3093 switch (msr_index) {
3094 case MSR_IA32_VMX_BASIC:
3095 return vmx_restore_vmx_basic(vmx, data);
3096 case MSR_IA32_VMX_PINBASED_CTLS:
3097 case MSR_IA32_VMX_PROCBASED_CTLS:
3098 case MSR_IA32_VMX_EXIT_CTLS:
3099 case MSR_IA32_VMX_ENTRY_CTLS:
3100 /*
3101 * The "non-true" VMX capability MSRs are generated from the
3102 * "true" MSRs, so we do not support restoring them directly.
3103 *
3104 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3105 * should restore the "true" MSRs with the must-be-1 bits
3106 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3107 * DEFAULT SETTINGS".
3108 */
3109 return -EINVAL;
3110 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3111 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3112 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3113 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3114 case MSR_IA32_VMX_PROCBASED_CTLS2:
3115 return vmx_restore_control_msr(vmx, msr_index, data);
3116 case MSR_IA32_VMX_MISC:
3117 return vmx_restore_vmx_misc(vmx, data);
3118 case MSR_IA32_VMX_CR0_FIXED0:
3119 case MSR_IA32_VMX_CR4_FIXED0:
3120 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3121 case MSR_IA32_VMX_CR0_FIXED1:
3122 case MSR_IA32_VMX_CR4_FIXED1:
3123 /*
3124 * These MSRs are generated based on the vCPU's CPUID, so we
3125 * do not support restoring them directly.
3126 */
3127 return -EINVAL;
3128 case MSR_IA32_VMX_EPT_VPID_CAP:
3129 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3130 case MSR_IA32_VMX_VMCS_ENUM:
3131 vmx->nested.nested_vmx_vmcs_enum = data;
3132 return 0;
3133 default:
3134 /*
3135 * The rest of the VMX capability MSRs do not support restore.
3136 */
3137 return -EINVAL;
3138 }
3139}
3140
Jan Kiszkacae50132014-01-04 18:47:22 +01003141/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003142static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
3143{
Wincy Vanb9c237b2015-02-03 23:56:30 +08003144 struct vcpu_vmx *vmx = to_vmx(vcpu);
3145
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003146 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003147 case MSR_IA32_VMX_BASIC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003148 *pdata = vmx->nested.nested_vmx_basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003149 break;
3150 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3151 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003152 *pdata = vmx_control_msr(
3153 vmx->nested.nested_vmx_pinbased_ctls_low,
3154 vmx->nested.nested_vmx_pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003155 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3156 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003157 break;
3158 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3159 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003160 *pdata = vmx_control_msr(
3161 vmx->nested.nested_vmx_procbased_ctls_low,
3162 vmx->nested.nested_vmx_procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003163 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3164 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003165 break;
3166 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3167 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003168 *pdata = vmx_control_msr(
3169 vmx->nested.nested_vmx_exit_ctls_low,
3170 vmx->nested.nested_vmx_exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003171 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3172 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003173 break;
3174 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3175 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003176 *pdata = vmx_control_msr(
3177 vmx->nested.nested_vmx_entry_ctls_low,
3178 vmx->nested.nested_vmx_entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003179 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3180 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003181 break;
3182 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003183 *pdata = vmx_control_msr(
3184 vmx->nested.nested_vmx_misc_low,
3185 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003186 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003187 case MSR_IA32_VMX_CR0_FIXED0:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003188 *pdata = vmx->nested.nested_vmx_cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003189 break;
3190 case MSR_IA32_VMX_CR0_FIXED1:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003191 *pdata = vmx->nested.nested_vmx_cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003192 break;
3193 case MSR_IA32_VMX_CR4_FIXED0:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003194 *pdata = vmx->nested.nested_vmx_cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003195 break;
3196 case MSR_IA32_VMX_CR4_FIXED1:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003197 *pdata = vmx->nested.nested_vmx_cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003198 break;
3199 case MSR_IA32_VMX_VMCS_ENUM:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003200 *pdata = vmx->nested.nested_vmx_vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003201 break;
3202 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003203 *pdata = vmx_control_msr(
3204 vmx->nested.nested_vmx_secondary_ctls_low,
3205 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003206 break;
3207 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07003208 *pdata = vmx->nested.nested_vmx_ept_caps |
3209 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003210 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003211 case MSR_IA32_VMX_VMFUNC:
3212 *pdata = vmx->nested.nested_vmx_vmfunc_controls;
3213 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003214 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003215 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003216 }
3217
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003218 return 0;
3219}
3220
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003221static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3222 uint64_t val)
3223{
3224 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3225
3226 return !(val & ~valid_bits);
3227}
3228
Tom Lendacky801e4592018-02-21 13:39:51 -06003229static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3230{
3231 return 1;
3232}
3233
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003234/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235 * Reads an msr value (of 'msr_index') into 'pdata'.
3236 * Returns 0 on success, non-0 otherwise.
3237 * Assumes vcpu_load() was already called.
3238 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003239static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003240{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003241 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003242 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003243
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003244 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003245#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003247 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003248 break;
3249 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003250 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003251 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003252 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003253 vmx_load_host_state(vmx);
3254 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003255 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003256#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003257 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003258 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303259 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003260 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003261 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003262 case MSR_IA32_SPEC_CTRL:
3263 if (!msr_info->host_initiated &&
3264 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3265 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3266 return 1;
3267
3268 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3269 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003270 case MSR_IA32_ARCH_CAPABILITIES:
3271 if (!msr_info->host_initiated &&
3272 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3273 return 1;
3274 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3275 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003277 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003278 break;
3279 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003280 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003281 break;
3282 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003283 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003284 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003285 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003286 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003287 (!msr_info->host_initiated &&
3288 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003289 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003290 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003291 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003292 case MSR_IA32_MCG_EXT_CTL:
3293 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003294 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003295 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003296 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003297 msr_info->data = vcpu->arch.mcg_ext_ctl;
3298 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003299 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003300 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003301 break;
3302 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3303 if (!nested_vmx_allowed(vcpu))
3304 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003305 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003306 case MSR_IA32_XSS:
3307 if (!vmx_xsaves_supported())
3308 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003309 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003310 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003311 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003312 if (!msr_info->host_initiated &&
3313 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003314 return 1;
3315 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003316 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003317 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003318 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003319 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003320 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003321 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003322 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003323 }
3324
Avi Kivity6aa8b732006-12-10 02:21:36 -08003325 return 0;
3326}
3327
Jan Kiszkacae50132014-01-04 18:47:22 +01003328static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3329
Avi Kivity6aa8b732006-12-10 02:21:36 -08003330/*
3331 * Writes msr value into into the appropriate "register".
3332 * Returns 0 on success, non-0 otherwise.
3333 * Assumes vcpu_load() was already called.
3334 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003335static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003336{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003337 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003338 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003339 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003340 u32 msr_index = msr_info->index;
3341 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003342
Avi Kivity6aa8b732006-12-10 02:21:36 -08003343 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003344 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003345 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003346 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003347#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003348 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003349 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003350 vmcs_writel(GUEST_FS_BASE, data);
3351 break;
3352 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003353 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003354 vmcs_writel(GUEST_GS_BASE, data);
3355 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003356 case MSR_KERNEL_GS_BASE:
3357 vmx_load_host_state(vmx);
3358 vmx->msr_guest_kernel_gs_base = data;
3359 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003360#endif
3361 case MSR_IA32_SYSENTER_CS:
3362 vmcs_write32(GUEST_SYSENTER_CS, data);
3363 break;
3364 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003365 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003366 break;
3367 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003368 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003369 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003370 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003371 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003372 (!msr_info->host_initiated &&
3373 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003374 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003375 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003376 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003377 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003378 vmcs_write64(GUEST_BNDCFGS, data);
3379 break;
3380 case MSR_IA32_TSC:
3381 kvm_write_tsc(vcpu, msr_info);
3382 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003383 case MSR_IA32_SPEC_CTRL:
3384 if (!msr_info->host_initiated &&
3385 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3386 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3387 return 1;
3388
3389 /* The STIBP bit doesn't fault even if it's not advertised */
3390 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
3391 return 1;
3392
3393 vmx->spec_ctrl = data;
3394
3395 if (!data)
3396 break;
3397
3398 /*
3399 * For non-nested:
3400 * When it's written (to non-zero) for the first time, pass
3401 * it through.
3402 *
3403 * For nested:
3404 * The handling of the MSR bitmap for L2 guests is done in
3405 * nested_vmx_merge_msr_bitmap. We should not touch the
3406 * vmcs02.msr_bitmap here since it gets completely overwritten
3407 * in the merging. We update the vmcs01 here for L1 as well
3408 * since it will end up touching the MSR anyway now.
3409 */
3410 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3411 MSR_IA32_SPEC_CTRL,
3412 MSR_TYPE_RW);
3413 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003414 case MSR_IA32_PRED_CMD:
3415 if (!msr_info->host_initiated &&
3416 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB) &&
3417 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3418 return 1;
3419
3420 if (data & ~PRED_CMD_IBPB)
3421 return 1;
3422
3423 if (!data)
3424 break;
3425
3426 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3427
3428 /*
3429 * For non-nested:
3430 * When it's written (to non-zero) for the first time, pass
3431 * it through.
3432 *
3433 * For nested:
3434 * The handling of the MSR bitmap for L2 guests is done in
3435 * nested_vmx_merge_msr_bitmap. We should not touch the
3436 * vmcs02.msr_bitmap here since it gets completely overwritten
3437 * in the merging.
3438 */
3439 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3440 MSR_TYPE_W);
3441 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003442 case MSR_IA32_ARCH_CAPABILITIES:
3443 if (!msr_info->host_initiated)
3444 return 1;
3445 vmx->arch_capabilities = data;
3446 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003447 case MSR_IA32_CR_PAT:
3448 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003449 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3450 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003451 vmcs_write64(GUEST_IA32_PAT, data);
3452 vcpu->arch.pat = data;
3453 break;
3454 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003455 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003456 break;
Will Auldba904632012-11-29 12:42:50 -08003457 case MSR_IA32_TSC_ADJUST:
3458 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003459 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003460 case MSR_IA32_MCG_EXT_CTL:
3461 if ((!msr_info->host_initiated &&
3462 !(to_vmx(vcpu)->msr_ia32_feature_control &
3463 FEATURE_CONTROL_LMCE)) ||
3464 (data & ~MCG_EXT_CTL_LMCE_EN))
3465 return 1;
3466 vcpu->arch.mcg_ext_ctl = data;
3467 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003468 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003469 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003470 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003471 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3472 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003473 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003474 if (msr_info->host_initiated && data == 0)
3475 vmx_leave_nested(vcpu);
3476 break;
3477 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003478 if (!msr_info->host_initiated)
3479 return 1; /* they are read-only */
3480 if (!nested_vmx_allowed(vcpu))
3481 return 1;
3482 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003483 case MSR_IA32_XSS:
3484 if (!vmx_xsaves_supported())
3485 return 1;
3486 /*
3487 * The only supported bit as of Skylake is bit 8, but
3488 * it is not supported on KVM.
3489 */
3490 if (data != 0)
3491 return 1;
3492 vcpu->arch.ia32_xss = data;
3493 if (vcpu->arch.ia32_xss != host_xss)
3494 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3495 vcpu->arch.ia32_xss, host_xss);
3496 else
3497 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3498 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003499 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003500 if (!msr_info->host_initiated &&
3501 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003502 return 1;
3503 /* Check reserved bit, higher 32 bits should be zero */
3504 if ((data >> 32) != 0)
3505 return 1;
3506 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003507 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003508 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003509 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003510 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003511 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003512 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3513 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003514 ret = kvm_set_shared_msr(msr->index, msr->data,
3515 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003516 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003517 if (ret)
3518 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003519 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003520 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003521 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003522 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003523 }
3524
Eddie Dong2cc51562007-05-21 07:28:09 +03003525 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003526}
3527
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003528static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003529{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003530 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3531 switch (reg) {
3532 case VCPU_REGS_RSP:
3533 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3534 break;
3535 case VCPU_REGS_RIP:
3536 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3537 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003538 case VCPU_EXREG_PDPTR:
3539 if (enable_ept)
3540 ept_save_pdptrs(vcpu);
3541 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003542 default:
3543 break;
3544 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545}
3546
Avi Kivity6aa8b732006-12-10 02:21:36 -08003547static __init int cpu_has_kvm_support(void)
3548{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003549 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003550}
3551
3552static __init int vmx_disabled_by_bios(void)
3553{
3554 u64 msr;
3555
3556 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003557 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003558 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003559 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3560 && tboot_enabled())
3561 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003562 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003563 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003564 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003565 && !tboot_enabled()) {
3566 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003567 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003568 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003569 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003570 /* launched w/o TXT and VMX disabled */
3571 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3572 && !tboot_enabled())
3573 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003574 }
3575
3576 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003577}
3578
Dongxiao Xu7725b892010-05-11 18:29:38 +08003579static void kvm_cpu_vmxon(u64 addr)
3580{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003581 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003582 intel_pt_handle_vmx(1);
3583
Dongxiao Xu7725b892010-05-11 18:29:38 +08003584 asm volatile (ASM_VMX_VMXON_RAX
3585 : : "a"(&addr), "m"(addr)
3586 : "memory", "cc");
3587}
3588
Radim Krčmář13a34e02014-08-28 15:13:03 +02003589static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003590{
3591 int cpu = raw_smp_processor_id();
3592 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003593 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003594
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003595 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003596 return -EBUSY;
3597
Nadav Har'Eld462b812011-05-24 15:26:10 +03003598 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003599 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3600 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003601
3602 /*
3603 * Now we can enable the vmclear operation in kdump
3604 * since the loaded_vmcss_on_cpu list on this cpu
3605 * has been initialized.
3606 *
3607 * Though the cpu is not in VMX operation now, there
3608 * is no problem to enable the vmclear operation
3609 * for the loaded_vmcss_on_cpu list is empty!
3610 */
3611 crash_enable_local_vmclear(cpu);
3612
Avi Kivity6aa8b732006-12-10 02:21:36 -08003613 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003614
3615 test_bits = FEATURE_CONTROL_LOCKED;
3616 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3617 if (tboot_enabled())
3618 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3619
3620 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003621 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003622 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3623 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003624 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02003625 if (enable_ept)
3626 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02003627
3628 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003629}
3630
Nadav Har'Eld462b812011-05-24 15:26:10 +03003631static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003632{
3633 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003634 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003635
Nadav Har'Eld462b812011-05-24 15:26:10 +03003636 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3637 loaded_vmcss_on_cpu_link)
3638 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003639}
3640
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003641
3642/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3643 * tricks.
3644 */
3645static void kvm_cpu_vmxoff(void)
3646{
3647 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003648
3649 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003650 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003651}
3652
Radim Krčmář13a34e02014-08-28 15:13:03 +02003653static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003654{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003655 vmclear_local_loaded_vmcss();
3656 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003657}
3658
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003659static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003660 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003661{
3662 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003663 u32 ctl = ctl_min | ctl_opt;
3664
3665 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3666
3667 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3668 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3669
3670 /* Ensure minimum (required) set of control bits are supported. */
3671 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003672 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003673
3674 *result = ctl;
3675 return 0;
3676}
3677
Avi Kivity110312c2010-12-21 12:54:20 +02003678static __init bool allow_1_setting(u32 msr, u32 ctl)
3679{
3680 u32 vmx_msr_low, vmx_msr_high;
3681
3682 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3683 return vmx_msr_high & ctl;
3684}
3685
Yang, Sheng002c7f72007-07-31 14:23:01 +03003686static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003687{
3688 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003689 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003690 u32 _pin_based_exec_control = 0;
3691 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003692 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003693 u32 _vmexit_control = 0;
3694 u32 _vmentry_control = 0;
3695
Raghavendra K T10166742012-02-07 23:19:20 +05303696 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003697#ifdef CONFIG_X86_64
3698 CPU_BASED_CR8_LOAD_EXITING |
3699 CPU_BASED_CR8_STORE_EXITING |
3700#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003701 CPU_BASED_CR3_LOAD_EXITING |
3702 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08003703 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003704 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003705 CPU_BASED_USE_TSC_OFFSETING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003706 CPU_BASED_INVLPG_EXITING |
3707 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003708
Michael S. Tsirkin668fffa32017-04-21 12:27:17 +02003709 if (!kvm_mwait_in_guest())
3710 min |= CPU_BASED_MWAIT_EXITING |
3711 CPU_BASED_MONITOR_EXITING;
3712
Sheng Yangf78e0e22007-10-29 09:40:42 +08003713 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003714 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003715 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003716 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3717 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003718 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003719#ifdef CONFIG_X86_64
3720 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3721 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3722 ~CPU_BASED_CR8_STORE_EXITING;
3723#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003724 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003725 min2 = 0;
3726 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003727 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003728 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003729 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003730 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003731 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003732 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02003733 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00003734 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003735 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003736 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003737 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003738 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003739 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02003740 SECONDARY_EXEC_RDSEED_EXITING |
3741 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003742 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04003743 SECONDARY_EXEC_TSC_SCALING |
3744 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08003745 if (adjust_vmx_controls(min2, opt2,
3746 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003747 &_cpu_based_2nd_exec_control) < 0)
3748 return -EIO;
3749 }
3750#ifndef CONFIG_X86_64
3751 if (!(_cpu_based_2nd_exec_control &
3752 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3753 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3754#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003755
3756 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3757 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003758 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003759 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3760 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003761
Wanpeng Li61f1dd92017-10-18 16:02:19 -07003762 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
3763 &vmx_capability.ept, &vmx_capability.vpid);
3764
Sheng Yangd56f5462008-04-25 10:13:16 +08003765 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003766 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3767 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003768 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3769 CPU_BASED_CR3_STORE_EXITING |
3770 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07003771 } else if (vmx_capability.ept) {
3772 vmx_capability.ept = 0;
3773 pr_warn_once("EPT CAP should not exist if not support "
3774 "1-setting enable EPT VM-execution control\n");
3775 }
3776 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
3777 vmx_capability.vpid) {
3778 vmx_capability.vpid = 0;
3779 pr_warn_once("VPID CAP should not exist if not support "
3780 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08003781 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003782
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003783 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003784#ifdef CONFIG_X86_64
3785 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3786#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003787 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003788 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003789 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3790 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003791 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003792
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01003793 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3794 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3795 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003796 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3797 &_pin_based_exec_control) < 0)
3798 return -EIO;
3799
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003800 if (cpu_has_broken_vmx_preemption_timer())
3801 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003802 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003803 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003804 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3805
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003806 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003807 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003808 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3809 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003810 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003811
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003812 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003813
3814 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3815 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003816 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003817
3818#ifdef CONFIG_X86_64
3819 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3820 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003821 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003822#endif
3823
3824 /* Require Write-Back (WB) memory type for VMCS accesses. */
3825 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003826 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003827
Yang, Sheng002c7f72007-07-31 14:23:01 +03003828 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003829 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003830 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003831 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003832
Yang, Sheng002c7f72007-07-31 14:23:01 +03003833 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3834 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003835 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003836 vmcs_conf->vmexit_ctrl = _vmexit_control;
3837 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003838
Avi Kivity110312c2010-12-21 12:54:20 +02003839 cpu_has_load_ia32_efer =
3840 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3841 VM_ENTRY_LOAD_IA32_EFER)
3842 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3843 VM_EXIT_LOAD_IA32_EFER);
3844
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003845 cpu_has_load_perf_global_ctrl =
3846 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3847 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3848 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3849 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3850
3851 /*
3852 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003853 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003854 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3855 *
3856 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3857 *
3858 * AAK155 (model 26)
3859 * AAP115 (model 30)
3860 * AAT100 (model 37)
3861 * BC86,AAY89,BD102 (model 44)
3862 * BA97 (model 46)
3863 *
3864 */
3865 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3866 switch (boot_cpu_data.x86_model) {
3867 case 26:
3868 case 30:
3869 case 37:
3870 case 44:
3871 case 46:
3872 cpu_has_load_perf_global_ctrl = false;
3873 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3874 "does not work properly. Using workaround\n");
3875 break;
3876 default:
3877 break;
3878 }
3879 }
3880
Borislav Petkov782511b2016-04-04 22:25:03 +02003881 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003882 rdmsrl(MSR_IA32_XSS, host_xss);
3883
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003884 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003885}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003886
3887static struct vmcs *alloc_vmcs_cpu(int cpu)
3888{
3889 int node = cpu_to_node(cpu);
3890 struct page *pages;
3891 struct vmcs *vmcs;
3892
Vlastimil Babka96db8002015-09-08 15:03:50 -07003893 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003894 if (!pages)
3895 return NULL;
3896 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003897 memset(vmcs, 0, vmcs_config.size);
3898 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003899 return vmcs;
3900}
3901
Avi Kivity6aa8b732006-12-10 02:21:36 -08003902static void free_vmcs(struct vmcs *vmcs)
3903{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003904 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003905}
3906
Nadav Har'Eld462b812011-05-24 15:26:10 +03003907/*
3908 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3909 */
3910static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3911{
3912 if (!loaded_vmcs->vmcs)
3913 return;
3914 loaded_vmcs_clear(loaded_vmcs);
3915 free_vmcs(loaded_vmcs->vmcs);
3916 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003917 if (loaded_vmcs->msr_bitmap)
3918 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07003919 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003920}
3921
Paolo Bonzinif21f1652018-01-11 12:16:15 +01003922static struct vmcs *alloc_vmcs(void)
3923{
3924 return alloc_vmcs_cpu(raw_smp_processor_id());
3925}
3926
3927static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3928{
3929 loaded_vmcs->vmcs = alloc_vmcs();
3930 if (!loaded_vmcs->vmcs)
3931 return -ENOMEM;
3932
3933 loaded_vmcs->shadow_vmcs = NULL;
3934 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003935
3936 if (cpu_has_vmx_msr_bitmap()) {
3937 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
3938 if (!loaded_vmcs->msr_bitmap)
3939 goto out_vmcs;
3940 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
3941 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01003942 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003943
3944out_vmcs:
3945 free_loaded_vmcs(loaded_vmcs);
3946 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01003947}
3948
Sam Ravnborg39959582007-06-01 00:47:13 -07003949static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003950{
3951 int cpu;
3952
Zachary Amsden3230bb42009-09-29 11:38:37 -10003953 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003954 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003955 per_cpu(vmxarea, cpu) = NULL;
3956 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003957}
3958
Jim Mattsond37f4262017-12-22 12:12:16 -08003959enum vmcs_field_width {
3960 VMCS_FIELD_WIDTH_U16 = 0,
3961 VMCS_FIELD_WIDTH_U64 = 1,
3962 VMCS_FIELD_WIDTH_U32 = 2,
3963 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07003964};
3965
Jim Mattsond37f4262017-12-22 12:12:16 -08003966static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07003967{
3968 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08003969 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07003970 return (field >> 13) & 0x3 ;
3971}
3972
3973static inline int vmcs_field_readonly(unsigned long field)
3974{
3975 return (((field >> 10) & 0x3) == 1);
3976}
3977
Bandan Dasfe2b2012014-04-21 15:20:14 -04003978static void init_vmcs_shadow_fields(void)
3979{
3980 int i, j;
3981
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003982 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
3983 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08003984 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003985 (i + 1 == max_shadow_read_only_fields ||
3986 shadow_read_only_fields[i + 1] != field + 1))
3987 pr_err("Missing field from shadow_read_only_field %x\n",
3988 field + 1);
3989
3990 clear_bit(field, vmx_vmread_bitmap);
3991#ifdef CONFIG_X86_64
3992 if (field & 1)
3993 continue;
3994#endif
3995 if (j < i)
3996 shadow_read_only_fields[j] = field;
3997 j++;
3998 }
3999 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004000
4001 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004002 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004003 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004004 (i + 1 == max_shadow_read_write_fields ||
4005 shadow_read_write_fields[i + 1] != field + 1))
4006 pr_err("Missing field from shadow_read_write_field %x\n",
4007 field + 1);
4008
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004009 /*
4010 * PML and the preemption timer can be emulated, but the
4011 * processor cannot vmwrite to fields that don't exist
4012 * on bare metal.
4013 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004014 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004015 case GUEST_PML_INDEX:
4016 if (!cpu_has_vmx_pml())
4017 continue;
4018 break;
4019 case VMX_PREEMPTION_TIMER_VALUE:
4020 if (!cpu_has_vmx_preemption_timer())
4021 continue;
4022 break;
4023 case GUEST_INTR_STATUS:
4024 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004025 continue;
4026 break;
4027 default:
4028 break;
4029 }
4030
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004031 clear_bit(field, vmx_vmwrite_bitmap);
4032 clear_bit(field, vmx_vmread_bitmap);
4033#ifdef CONFIG_X86_64
4034 if (field & 1)
4035 continue;
4036#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004037 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004038 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004039 j++;
4040 }
4041 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004042}
4043
Avi Kivity6aa8b732006-12-10 02:21:36 -08004044static __init int alloc_kvm_area(void)
4045{
4046 int cpu;
4047
Zachary Amsden3230bb42009-09-29 11:38:37 -10004048 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004049 struct vmcs *vmcs;
4050
4051 vmcs = alloc_vmcs_cpu(cpu);
4052 if (!vmcs) {
4053 free_kvm_area();
4054 return -ENOMEM;
4055 }
4056
4057 per_cpu(vmxarea, cpu) = vmcs;
4058 }
4059 return 0;
4060}
4061
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004062static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004063 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004064{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004065 if (!emulate_invalid_guest_state) {
4066 /*
4067 * CS and SS RPL should be equal during guest entry according
4068 * to VMX spec, but in reality it is not always so. Since vcpu
4069 * is in the middle of the transition from real mode to
4070 * protected mode it is safe to assume that RPL 0 is a good
4071 * default value.
4072 */
4073 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004074 save->selector &= ~SEGMENT_RPL_MASK;
4075 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004076 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004077 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004078 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004079}
4080
4081static void enter_pmode(struct kvm_vcpu *vcpu)
4082{
4083 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004084 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004085
Gleb Natapovd99e4152012-12-20 16:57:45 +02004086 /*
4087 * Update real mode segment cache. It may be not up-to-date if sement
4088 * register was written while vcpu was in a guest mode.
4089 */
4090 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4091 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4092 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4093 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4094 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4095 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4096
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004097 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004098
Avi Kivity2fb92db2011-04-27 19:42:18 +03004099 vmx_segment_cache_clear(vmx);
4100
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004101 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004102
4103 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004104 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4105 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004106 vmcs_writel(GUEST_RFLAGS, flags);
4107
Rusty Russell66aee912007-07-17 23:34:16 +10004108 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4109 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004110
4111 update_exception_bitmap(vcpu);
4112
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004113 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4114 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4115 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4116 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4117 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4118 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004119}
4120
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004121static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004122{
Mathias Krause772e0312012-08-30 01:30:19 +02004123 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004124 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004125
Gleb Natapovd99e4152012-12-20 16:57:45 +02004126 var.dpl = 0x3;
4127 if (seg == VCPU_SREG_CS)
4128 var.type = 0x3;
4129
4130 if (!emulate_invalid_guest_state) {
4131 var.selector = var.base >> 4;
4132 var.base = var.base & 0xffff0;
4133 var.limit = 0xffff;
4134 var.g = 0;
4135 var.db = 0;
4136 var.present = 1;
4137 var.s = 1;
4138 var.l = 0;
4139 var.unusable = 0;
4140 var.type = 0x3;
4141 var.avl = 0;
4142 if (save->base & 0xf)
4143 printk_once(KERN_WARNING "kvm: segment base is not "
4144 "paragraph aligned when entering "
4145 "protected mode (seg=%d)", seg);
4146 }
4147
4148 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004149 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004150 vmcs_write32(sf->limit, var.limit);
4151 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004152}
4153
4154static void enter_rmode(struct kvm_vcpu *vcpu)
4155{
4156 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004157 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004158
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004159 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4160 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4161 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4162 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4163 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004164 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4165 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004166
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004167 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004168
Gleb Natapov776e58e2011-03-13 12:34:27 +02004169 /*
4170 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004171 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004172 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004173 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004174 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4175 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004176
Avi Kivity2fb92db2011-04-27 19:42:18 +03004177 vmx_segment_cache_clear(vmx);
4178
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004179 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004180 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4182
4183 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004184 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004185
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004186 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004187
4188 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004189 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004190 update_exception_bitmap(vcpu);
4191
Gleb Natapovd99e4152012-12-20 16:57:45 +02004192 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4193 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4194 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4195 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4196 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4197 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004198
Eddie Dong8668a3c2007-10-10 14:26:45 +08004199 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004200}
4201
Amit Shah401d10d2009-02-20 22:53:37 +05304202static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4203{
4204 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004205 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4206
4207 if (!msr)
4208 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304209
Avi Kivity44ea2b12009-09-06 15:55:37 +03004210 /*
4211 * Force kernel_gs_base reloading before EFER changes, as control
4212 * of this msr depends on is_long_mode().
4213 */
4214 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004215 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304216 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004217 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304218 msr->data = efer;
4219 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004220 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304221
4222 msr->data = efer & ~EFER_LME;
4223 }
4224 setup_msrs(vmx);
4225}
4226
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004227#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004228
4229static void enter_lmode(struct kvm_vcpu *vcpu)
4230{
4231 u32 guest_tr_ar;
4232
Avi Kivity2fb92db2011-04-27 19:42:18 +03004233 vmx_segment_cache_clear(to_vmx(vcpu));
4234
Avi Kivity6aa8b732006-12-10 02:21:36 -08004235 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004236 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004237 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4238 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004239 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004240 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4241 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004242 }
Avi Kivityda38f432010-07-06 11:30:49 +03004243 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004244}
4245
4246static void exit_lmode(struct kvm_vcpu *vcpu)
4247{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004248 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004249 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004250}
4251
4252#endif
4253
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004254static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4255 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004256{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004257 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004258 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4259 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004260 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004261 } else {
4262 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004263 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004264}
4265
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004266static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004267{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004268 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004269}
4270
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004271static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4272{
4273 if (enable_ept)
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004274 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004275}
4276
Avi Kivitye8467fd2009-12-29 18:43:06 +02004277static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4278{
4279 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4280
4281 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4282 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4283}
4284
Avi Kivityaff48ba2010-12-05 18:56:11 +02004285static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4286{
4287 if (enable_ept && is_paging(vcpu))
4288 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4289 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4290}
4291
Anthony Liguori25c4c272007-04-27 09:29:21 +03004292static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004293{
Avi Kivityfc78f512009-12-07 12:16:48 +02004294 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4295
4296 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4297 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004298}
4299
Sheng Yang14394422008-04-28 12:24:45 +08004300static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4301{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004302 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4303
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004304 if (!test_bit(VCPU_EXREG_PDPTR,
4305 (unsigned long *)&vcpu->arch.regs_dirty))
4306 return;
4307
Sheng Yang14394422008-04-28 12:24:45 +08004308 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004309 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4310 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4311 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4312 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004313 }
4314}
4315
Avi Kivity8f5d5492009-05-31 18:41:29 +03004316static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4317{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004318 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4319
Avi Kivity8f5d5492009-05-31 18:41:29 +03004320 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004321 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4322 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4323 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4324 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004325 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004326
4327 __set_bit(VCPU_EXREG_PDPTR,
4328 (unsigned long *)&vcpu->arch.regs_avail);
4329 __set_bit(VCPU_EXREG_PDPTR,
4330 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004331}
4332
David Matlack38991522016-11-29 18:14:08 -08004333static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4334{
4335 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4336 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4337 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4338
4339 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
4340 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4341 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4342 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4343
4344 return fixed_bits_valid(val, fixed0, fixed1);
4345}
4346
4347static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4348{
4349 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4350 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4351
4352 return fixed_bits_valid(val, fixed0, fixed1);
4353}
4354
4355static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4356{
4357 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0;
4358 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1;
4359
4360 return fixed_bits_valid(val, fixed0, fixed1);
4361}
4362
4363/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4364#define nested_guest_cr4_valid nested_cr4_valid
4365#define nested_host_cr4_valid nested_cr4_valid
4366
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004367static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004368
4369static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4370 unsigned long cr0,
4371 struct kvm_vcpu *vcpu)
4372{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004373 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4374 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004375 if (!(cr0 & X86_CR0_PG)) {
4376 /* From paging/starting to nonpaging */
4377 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004378 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004379 (CPU_BASED_CR3_LOAD_EXITING |
4380 CPU_BASED_CR3_STORE_EXITING));
4381 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004382 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004383 } else if (!is_paging(vcpu)) {
4384 /* From nonpaging to paging */
4385 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004386 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004387 ~(CPU_BASED_CR3_LOAD_EXITING |
4388 CPU_BASED_CR3_STORE_EXITING));
4389 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004390 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004391 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004392
4393 if (!(cr0 & X86_CR0_WP))
4394 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004395}
4396
Avi Kivity6aa8b732006-12-10 02:21:36 -08004397static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4398{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004399 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004400 unsigned long hw_cr0;
4401
Gleb Natapov50378782013-02-04 16:00:28 +02004402 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004403 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004404 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004405 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004406 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004407
Gleb Natapov218e7632013-01-21 15:36:45 +02004408 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4409 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004410
Gleb Natapov218e7632013-01-21 15:36:45 +02004411 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4412 enter_rmode(vcpu);
4413 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004414
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004415#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004416 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004417 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004418 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004419 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004420 exit_lmode(vcpu);
4421 }
4422#endif
4423
Avi Kivity089d0342009-03-23 18:26:32 +02004424 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004425 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4426
Avi Kivity6aa8b732006-12-10 02:21:36 -08004427 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004428 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004429 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004430
4431 /* depends on vcpu->arch.cr0 to be set to a new value */
4432 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004433}
4434
Yu Zhang855feb62017-08-24 20:27:55 +08004435static int get_ept_level(struct kvm_vcpu *vcpu)
4436{
4437 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4438 return 5;
4439 return 4;
4440}
4441
Peter Feiner995f00a2017-06-30 17:26:32 -07004442static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004443{
Yu Zhang855feb62017-08-24 20:27:55 +08004444 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004445
Yu Zhang855feb62017-08-24 20:27:55 +08004446 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004447
Peter Feiner995f00a2017-06-30 17:26:32 -07004448 if (enable_ept_ad_bits &&
4449 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004450 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004451 eptp |= (root_hpa & PAGE_MASK);
4452
4453 return eptp;
4454}
4455
Avi Kivity6aa8b732006-12-10 02:21:36 -08004456static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4457{
Sheng Yang14394422008-04-28 12:24:45 +08004458 unsigned long guest_cr3;
4459 u64 eptp;
4460
4461 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004462 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004463 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004464 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004465 if (is_paging(vcpu) || is_guest_mode(vcpu))
4466 guest_cr3 = kvm_read_cr3(vcpu);
4467 else
4468 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004469 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004470 }
4471
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004472 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08004473 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004474}
4475
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004476static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004477{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004478 /*
4479 * Pass through host's Machine Check Enable value to hw_cr4, which
4480 * is in force while we are in guest mode. Do not let guests control
4481 * this bit, even if host CR4.MCE == 0.
4482 */
4483 unsigned long hw_cr4 =
4484 (cr4_read_shadow() & X86_CR4_MCE) |
4485 (cr4 & ~X86_CR4_MCE) |
4486 (to_vmx(vcpu)->rmode.vm86_active ?
4487 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004488
Paolo Bonzini0367f202016-07-12 10:44:55 +02004489 if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) {
4490 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4491 SECONDARY_EXEC_DESC);
4492 hw_cr4 &= ~X86_CR4_UMIP;
Radim Krčmář99158242018-01-31 18:12:50 +01004493 } else if (!is_guest_mode(vcpu) ||
4494 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
Paolo Bonzini0367f202016-07-12 10:44:55 +02004495 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4496 SECONDARY_EXEC_DESC);
4497
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004498 if (cr4 & X86_CR4_VMXE) {
4499 /*
4500 * To use VMXON (and later other VMX instructions), a guest
4501 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4502 * So basically the check on whether to allow nested VMX
4503 * is here.
4504 */
4505 if (!nested_vmx_allowed(vcpu))
4506 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004507 }
David Matlack38991522016-11-29 18:14:08 -08004508
4509 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004510 return 1;
4511
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004512 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004513 if (enable_ept) {
4514 if (!is_paging(vcpu)) {
4515 hw_cr4 &= ~X86_CR4_PAE;
4516 hw_cr4 |= X86_CR4_PSE;
4517 } else if (!(cr4 & X86_CR4_PAE)) {
4518 hw_cr4 &= ~X86_CR4_PAE;
4519 }
4520 }
Sheng Yang14394422008-04-28 12:24:45 +08004521
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004522 if (!enable_unrestricted_guest && !is_paging(vcpu))
4523 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004524 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4525 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4526 * to be manually disabled when guest switches to non-paging
4527 * mode.
4528 *
4529 * If !enable_unrestricted_guest, the CPU is always running
4530 * with CR0.PG=1 and CR4 needs to be modified.
4531 * If enable_unrestricted_guest, the CPU automatically
4532 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004533 */
Huaitong Handdba2622016-03-22 16:51:15 +08004534 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004535
Sheng Yang14394422008-04-28 12:24:45 +08004536 vmcs_writel(CR4_READ_SHADOW, cr4);
4537 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004538 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004539}
4540
Avi Kivity6aa8b732006-12-10 02:21:36 -08004541static void vmx_get_segment(struct kvm_vcpu *vcpu,
4542 struct kvm_segment *var, int seg)
4543{
Avi Kivitya9179492011-01-03 14:28:52 +02004544 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004545 u32 ar;
4546
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004547 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004548 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004549 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004550 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004551 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004552 var->base = vmx_read_guest_seg_base(vmx, seg);
4553 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4554 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004555 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004556 var->base = vmx_read_guest_seg_base(vmx, seg);
4557 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4558 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4559 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004560 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004561 var->type = ar & 15;
4562 var->s = (ar >> 4) & 1;
4563 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004564 /*
4565 * Some userspaces do not preserve unusable property. Since usable
4566 * segment has to be present according to VMX spec we can use present
4567 * property to amend userspace bug by making unusable segment always
4568 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4569 * segment as unusable.
4570 */
4571 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004572 var->avl = (ar >> 12) & 1;
4573 var->l = (ar >> 13) & 1;
4574 var->db = (ar >> 14) & 1;
4575 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004576}
4577
Avi Kivitya9179492011-01-03 14:28:52 +02004578static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4579{
Avi Kivitya9179492011-01-03 14:28:52 +02004580 struct kvm_segment s;
4581
4582 if (to_vmx(vcpu)->rmode.vm86_active) {
4583 vmx_get_segment(vcpu, &s, seg);
4584 return s.base;
4585 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004586 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004587}
4588
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004589static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004590{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004591 struct vcpu_vmx *vmx = to_vmx(vcpu);
4592
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004593 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004594 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004595 else {
4596 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004597 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004598 }
Avi Kivity69c73022011-03-07 15:26:44 +02004599}
4600
Avi Kivity653e3102007-05-07 10:55:37 +03004601static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004602{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004603 u32 ar;
4604
Avi Kivityf0495f92012-06-07 17:06:10 +03004605 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004606 ar = 1 << 16;
4607 else {
4608 ar = var->type & 15;
4609 ar |= (var->s & 1) << 4;
4610 ar |= (var->dpl & 3) << 5;
4611 ar |= (var->present & 1) << 7;
4612 ar |= (var->avl & 1) << 12;
4613 ar |= (var->l & 1) << 13;
4614 ar |= (var->db & 1) << 14;
4615 ar |= (var->g & 1) << 15;
4616 }
Avi Kivity653e3102007-05-07 10:55:37 +03004617
4618 return ar;
4619}
4620
4621static void vmx_set_segment(struct kvm_vcpu *vcpu,
4622 struct kvm_segment *var, int seg)
4623{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004624 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004625 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004626
Avi Kivity2fb92db2011-04-27 19:42:18 +03004627 vmx_segment_cache_clear(vmx);
4628
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004629 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4630 vmx->rmode.segs[seg] = *var;
4631 if (seg == VCPU_SREG_TR)
4632 vmcs_write16(sf->selector, var->selector);
4633 else if (var->s)
4634 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004635 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004636 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004637
Avi Kivity653e3102007-05-07 10:55:37 +03004638 vmcs_writel(sf->base, var->base);
4639 vmcs_write32(sf->limit, var->limit);
4640 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004641
4642 /*
4643 * Fix the "Accessed" bit in AR field of segment registers for older
4644 * qemu binaries.
4645 * IA32 arch specifies that at the time of processor reset the
4646 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004647 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004648 * state vmexit when "unrestricted guest" mode is turned on.
4649 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4650 * tree. Newer qemu binaries with that qemu fix would not need this
4651 * kvm hack.
4652 */
4653 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004654 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004655
Gleb Natapovf924d662012-12-12 19:10:55 +02004656 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004657
4658out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004659 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004660}
4661
Avi Kivity6aa8b732006-12-10 02:21:36 -08004662static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4663{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004664 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004665
4666 *db = (ar >> 14) & 1;
4667 *l = (ar >> 13) & 1;
4668}
4669
Gleb Natapov89a27f42010-02-16 10:51:48 +02004670static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004671{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004672 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4673 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004674}
4675
Gleb Natapov89a27f42010-02-16 10:51:48 +02004676static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004677{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004678 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4679 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004680}
4681
Gleb Natapov89a27f42010-02-16 10:51:48 +02004682static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004683{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004684 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4685 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004686}
4687
Gleb Natapov89a27f42010-02-16 10:51:48 +02004688static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004689{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004690 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4691 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004692}
4693
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004694static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4695{
4696 struct kvm_segment var;
4697 u32 ar;
4698
4699 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004700 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004701 if (seg == VCPU_SREG_CS)
4702 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004703 ar = vmx_segment_access_rights(&var);
4704
4705 if (var.base != (var.selector << 4))
4706 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004707 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004708 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004709 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004710 return false;
4711
4712 return true;
4713}
4714
4715static bool code_segment_valid(struct kvm_vcpu *vcpu)
4716{
4717 struct kvm_segment cs;
4718 unsigned int cs_rpl;
4719
4720 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004721 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004722
Avi Kivity1872a3f2009-01-04 23:26:52 +02004723 if (cs.unusable)
4724 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004725 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004726 return false;
4727 if (!cs.s)
4728 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004729 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004730 if (cs.dpl > cs_rpl)
4731 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004732 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004733 if (cs.dpl != cs_rpl)
4734 return false;
4735 }
4736 if (!cs.present)
4737 return false;
4738
4739 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4740 return true;
4741}
4742
4743static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4744{
4745 struct kvm_segment ss;
4746 unsigned int ss_rpl;
4747
4748 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004749 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004750
Avi Kivity1872a3f2009-01-04 23:26:52 +02004751 if (ss.unusable)
4752 return true;
4753 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004754 return false;
4755 if (!ss.s)
4756 return false;
4757 if (ss.dpl != ss_rpl) /* DPL != RPL */
4758 return false;
4759 if (!ss.present)
4760 return false;
4761
4762 return true;
4763}
4764
4765static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4766{
4767 struct kvm_segment var;
4768 unsigned int rpl;
4769
4770 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004771 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004772
Avi Kivity1872a3f2009-01-04 23:26:52 +02004773 if (var.unusable)
4774 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004775 if (!var.s)
4776 return false;
4777 if (!var.present)
4778 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004779 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004780 if (var.dpl < rpl) /* DPL < RPL */
4781 return false;
4782 }
4783
4784 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4785 * rights flags
4786 */
4787 return true;
4788}
4789
4790static bool tr_valid(struct kvm_vcpu *vcpu)
4791{
4792 struct kvm_segment tr;
4793
4794 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4795
Avi Kivity1872a3f2009-01-04 23:26:52 +02004796 if (tr.unusable)
4797 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004798 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004799 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004800 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004801 return false;
4802 if (!tr.present)
4803 return false;
4804
4805 return true;
4806}
4807
4808static bool ldtr_valid(struct kvm_vcpu *vcpu)
4809{
4810 struct kvm_segment ldtr;
4811
4812 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4813
Avi Kivity1872a3f2009-01-04 23:26:52 +02004814 if (ldtr.unusable)
4815 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004816 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004817 return false;
4818 if (ldtr.type != 2)
4819 return false;
4820 if (!ldtr.present)
4821 return false;
4822
4823 return true;
4824}
4825
4826static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4827{
4828 struct kvm_segment cs, ss;
4829
4830 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4831 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4832
Nadav Amitb32a9912015-03-29 16:33:04 +03004833 return ((cs.selector & SEGMENT_RPL_MASK) ==
4834 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004835}
4836
4837/*
4838 * Check if guest state is valid. Returns true if valid, false if
4839 * not.
4840 * We assume that registers are always usable
4841 */
4842static bool guest_state_valid(struct kvm_vcpu *vcpu)
4843{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004844 if (enable_unrestricted_guest)
4845 return true;
4846
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004847 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004848 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004849 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4850 return false;
4851 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4852 return false;
4853 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4854 return false;
4855 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4856 return false;
4857 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4858 return false;
4859 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4860 return false;
4861 } else {
4862 /* protected mode guest state checks */
4863 if (!cs_ss_rpl_check(vcpu))
4864 return false;
4865 if (!code_segment_valid(vcpu))
4866 return false;
4867 if (!stack_segment_valid(vcpu))
4868 return false;
4869 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4870 return false;
4871 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4872 return false;
4873 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4874 return false;
4875 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4876 return false;
4877 if (!tr_valid(vcpu))
4878 return false;
4879 if (!ldtr_valid(vcpu))
4880 return false;
4881 }
4882 /* TODO:
4883 * - Add checks on RIP
4884 * - Add checks on RFLAGS
4885 */
4886
4887 return true;
4888}
4889
Jim Mattson5fa99cb2017-07-06 16:33:07 -07004890static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
4891{
4892 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
4893}
4894
Mike Dayd77c26f2007-10-08 09:02:08 -04004895static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004896{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004897 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004898 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004899 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004900
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004901 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004902 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004903 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4904 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004905 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004906 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004907 r = kvm_write_guest_page(kvm, fn++, &data,
4908 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004909 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004910 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004911 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4912 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004913 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004914 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4915 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004916 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004917 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004918 r = kvm_write_guest_page(kvm, fn, &data,
4919 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4920 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004921out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004922 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004923 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004924}
4925
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004926static int init_rmode_identity_map(struct kvm *kvm)
4927{
Tang Chenf51770e2014-09-16 18:41:59 +08004928 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004929 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004930 u32 tmp;
4931
Tang Chena255d472014-09-16 18:41:58 +08004932 /* Protect kvm->arch.ept_identity_pagetable_done. */
4933 mutex_lock(&kvm->slots_lock);
4934
Tang Chenf51770e2014-09-16 18:41:59 +08004935 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004936 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004937
David Hildenbrandd8a6e362017-08-24 20:51:34 +02004938 if (!kvm->arch.ept_identity_map_addr)
4939 kvm->arch.ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004940 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004941
David Hildenbrandd8a6e362017-08-24 20:51:34 +02004942 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4943 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08004944 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004945 goto out2;
4946
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004947 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004948 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4949 if (r < 0)
4950 goto out;
4951 /* Set up identity-mapping pagetable for EPT in real mode */
4952 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4953 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4954 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4955 r = kvm_write_guest_page(kvm, identity_map_pfn,
4956 &tmp, i * sizeof(tmp), sizeof(tmp));
4957 if (r < 0)
4958 goto out;
4959 }
4960 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004961
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004962out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004963 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004964
4965out2:
4966 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004967 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004968}
4969
Avi Kivity6aa8b732006-12-10 02:21:36 -08004970static void seg_setup(int seg)
4971{
Mathias Krause772e0312012-08-30 01:30:19 +02004972 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004973 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004974
4975 vmcs_write16(sf->selector, 0);
4976 vmcs_writel(sf->base, 0);
4977 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004978 ar = 0x93;
4979 if (seg == VCPU_SREG_CS)
4980 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004981
4982 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004983}
4984
Sheng Yangf78e0e22007-10-29 09:40:42 +08004985static int alloc_apic_access_page(struct kvm *kvm)
4986{
Xiao Guangrong44841412012-09-07 14:14:20 +08004987 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004988 int r = 0;
4989
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004990 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004991 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004992 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004993 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4994 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004995 if (r)
4996 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004997
Tang Chen73a6d942014-09-11 13:38:00 +08004998 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004999 if (is_error_page(page)) {
5000 r = -EFAULT;
5001 goto out;
5002 }
5003
Tang Chenc24ae0d2014-09-24 15:57:58 +08005004 /*
5005 * Do not pin the page in memory, so that memory hot-unplug
5006 * is able to migrate it.
5007 */
5008 put_page(page);
5009 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005010out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005011 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005012 return r;
5013}
5014
Wanpeng Li991e7a02015-09-16 17:30:05 +08005015static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005016{
5017 int vpid;
5018
Avi Kivity919818a2009-03-23 18:01:29 +02005019 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005020 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005021 spin_lock(&vmx_vpid_lock);
5022 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005023 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005024 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005025 else
5026 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005027 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005028 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005029}
5030
Wanpeng Li991e7a02015-09-16 17:30:05 +08005031static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005032{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005033 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005034 return;
5035 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005036 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005037 spin_unlock(&vmx_vpid_lock);
5038}
5039
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005040static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5041 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005042{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005043 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005044
5045 if (!cpu_has_vmx_msr_bitmap())
5046 return;
5047
5048 /*
5049 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5050 * have the write-low and read-high bitmap offsets the wrong way round.
5051 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5052 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005053 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005054 if (type & MSR_TYPE_R)
5055 /* read-low */
5056 __clear_bit(msr, msr_bitmap + 0x000 / f);
5057
5058 if (type & MSR_TYPE_W)
5059 /* write-low */
5060 __clear_bit(msr, msr_bitmap + 0x800 / f);
5061
Sheng Yang25c5f222008-03-28 13:18:56 +08005062 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5063 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005064 if (type & MSR_TYPE_R)
5065 /* read-high */
5066 __clear_bit(msr, msr_bitmap + 0x400 / f);
5067
5068 if (type & MSR_TYPE_W)
5069 /* write-high */
5070 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5071
5072 }
5073}
5074
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005075static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5076 u32 msr, int type)
5077{
5078 int f = sizeof(unsigned long);
5079
5080 if (!cpu_has_vmx_msr_bitmap())
5081 return;
5082
5083 /*
5084 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5085 * have the write-low and read-high bitmap offsets the wrong way round.
5086 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5087 */
5088 if (msr <= 0x1fff) {
5089 if (type & MSR_TYPE_R)
5090 /* read-low */
5091 __set_bit(msr, msr_bitmap + 0x000 / f);
5092
5093 if (type & MSR_TYPE_W)
5094 /* write-low */
5095 __set_bit(msr, msr_bitmap + 0x800 / f);
5096
5097 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5098 msr &= 0x1fff;
5099 if (type & MSR_TYPE_R)
5100 /* read-high */
5101 __set_bit(msr, msr_bitmap + 0x400 / f);
5102
5103 if (type & MSR_TYPE_W)
5104 /* write-high */
5105 __set_bit(msr, msr_bitmap + 0xc00 / f);
5106
5107 }
5108}
5109
5110static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5111 u32 msr, int type, bool value)
5112{
5113 if (value)
5114 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5115 else
5116 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5117}
5118
Wincy Vanf2b93282015-02-03 23:56:03 +08005119/*
5120 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5121 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5122 */
5123static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5124 unsigned long *msr_bitmap_nested,
5125 u32 msr, int type)
5126{
5127 int f = sizeof(unsigned long);
5128
Wincy Vanf2b93282015-02-03 23:56:03 +08005129 /*
5130 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5131 * have the write-low and read-high bitmap offsets the wrong way round.
5132 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5133 */
5134 if (msr <= 0x1fff) {
5135 if (type & MSR_TYPE_R &&
5136 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5137 /* read-low */
5138 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5139
5140 if (type & MSR_TYPE_W &&
5141 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5142 /* write-low */
5143 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5144
5145 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5146 msr &= 0x1fff;
5147 if (type & MSR_TYPE_R &&
5148 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5149 /* read-high */
5150 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5151
5152 if (type & MSR_TYPE_W &&
5153 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5154 /* write-high */
5155 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5156
5157 }
5158}
5159
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005160static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005161{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005162 u8 mode = 0;
5163
5164 if (cpu_has_secondary_exec_ctrls() &&
5165 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5166 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5167 mode |= MSR_BITMAP_MODE_X2APIC;
5168 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5169 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5170 }
5171
5172 if (is_long_mode(vcpu))
5173 mode |= MSR_BITMAP_MODE_LM;
5174
5175 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005176}
5177
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005178#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5179
5180static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5181 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005182{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005183 int msr;
5184
5185 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5186 unsigned word = msr / BITS_PER_LONG;
5187 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5188 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005189 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005190
5191 if (mode & MSR_BITMAP_MODE_X2APIC) {
5192 /*
5193 * TPR reads and writes can be virtualized even if virtual interrupt
5194 * delivery is not in use.
5195 */
5196 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5197 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5198 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5199 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5200 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5201 }
5202 }
5203}
5204
5205static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5206{
5207 struct vcpu_vmx *vmx = to_vmx(vcpu);
5208 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5209 u8 mode = vmx_msr_bitmap_mode(vcpu);
5210 u8 changed = mode ^ vmx->msr_bitmap_mode;
5211
5212 if (!changed)
5213 return;
5214
5215 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5216 !(mode & MSR_BITMAP_MODE_LM));
5217
5218 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5219 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5220
5221 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005222}
5223
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005224static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005225{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005226 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005227}
5228
David Matlackc9f04402017-08-01 14:00:40 -07005229static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5230{
5231 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5232 gfn_t gfn;
5233
5234 /*
5235 * Don't need to mark the APIC access page dirty; it is never
5236 * written to by the CPU during APIC virtualization.
5237 */
5238
5239 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5240 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5241 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5242 }
5243
5244 if (nested_cpu_has_posted_intr(vmcs12)) {
5245 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5246 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5247 }
5248}
5249
5250
David Hildenbrand6342c502017-01-25 11:58:58 +01005251static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005252{
5253 struct vcpu_vmx *vmx = to_vmx(vcpu);
5254 int max_irr;
5255 void *vapic_page;
5256 u16 status;
5257
David Matlackc9f04402017-08-01 14:00:40 -07005258 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5259 return;
Wincy Van705699a2015-02-03 23:58:17 +08005260
David Matlackc9f04402017-08-01 14:00:40 -07005261 vmx->nested.pi_pending = false;
5262 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5263 return;
Wincy Van705699a2015-02-03 23:58:17 +08005264
David Matlackc9f04402017-08-01 14:00:40 -07005265 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5266 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005267 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005268 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5269 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005270 kunmap(vmx->nested.virtual_apic_page);
5271
5272 status = vmcs_read16(GUEST_INTR_STATUS);
5273 if ((u8)max_irr > ((u8)status & 0xff)) {
5274 status &= ~0xff;
5275 status |= (u8)max_irr;
5276 vmcs_write16(GUEST_INTR_STATUS, status);
5277 }
5278 }
David Matlackc9f04402017-08-01 14:00:40 -07005279
5280 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005281}
5282
Wincy Van06a55242017-04-28 13:13:59 +08005283static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5284 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005285{
5286#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005287 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5288
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005289 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005290 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005291 * The vector of interrupt to be delivered to vcpu had
5292 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005293 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005294 * Following cases will be reached in this block, and
5295 * we always send a notification event in all cases as
5296 * explained below.
5297 *
5298 * Case 1: vcpu keeps in non-root mode. Sending a
5299 * notification event posts the interrupt to vcpu.
5300 *
5301 * Case 2: vcpu exits to root mode and is still
5302 * runnable. PIR will be synced to vIRR before the
5303 * next vcpu entry. Sending a notification event in
5304 * this case has no effect, as vcpu is not in root
5305 * mode.
5306 *
5307 * Case 3: vcpu exits to root mode and is blocked.
5308 * vcpu_block() has already synced PIR to vIRR and
5309 * never blocks vcpu if vIRR is not cleared. Therefore,
5310 * a blocked vcpu here does not wait for any requested
5311 * interrupts in PIR, and sending a notification event
5312 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005313 */
Feng Wu28b835d2015-09-18 22:29:54 +08005314
Wincy Van06a55242017-04-28 13:13:59 +08005315 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005316 return true;
5317 }
5318#endif
5319 return false;
5320}
5321
Wincy Van705699a2015-02-03 23:58:17 +08005322static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5323 int vector)
5324{
5325 struct vcpu_vmx *vmx = to_vmx(vcpu);
5326
5327 if (is_guest_mode(vcpu) &&
5328 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005329 /*
5330 * If a posted intr is not recognized by hardware,
5331 * we will accomplish it in the next vmentry.
5332 */
5333 vmx->nested.pi_pending = true;
5334 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005335 /* the PIR and ON have been set by L1. */
5336 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5337 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005338 return 0;
5339 }
5340 return -1;
5341}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005342/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005343 * Send interrupt to vcpu via posted interrupt way.
5344 * 1. If target vcpu is running(non-root mode), send posted interrupt
5345 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5346 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5347 * interrupt from PIR in next vmentry.
5348 */
5349static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5350{
5351 struct vcpu_vmx *vmx = to_vmx(vcpu);
5352 int r;
5353
Wincy Van705699a2015-02-03 23:58:17 +08005354 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5355 if (!r)
5356 return;
5357
Yang Zhanga20ed542013-04-11 19:25:15 +08005358 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5359 return;
5360
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005361 /* If a previous notification has sent the IPI, nothing to do. */
5362 if (pi_test_and_set_on(&vmx->pi_desc))
5363 return;
5364
Wincy Van06a55242017-04-28 13:13:59 +08005365 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005366 kvm_vcpu_kick(vcpu);
5367}
5368
Avi Kivity6aa8b732006-12-10 02:21:36 -08005369/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005370 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5371 * will not change in the lifetime of the guest.
5372 * Note that host-state that does change is set elsewhere. E.g., host-state
5373 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5374 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005375static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005376{
5377 u32 low32, high32;
5378 unsigned long tmpl;
5379 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005380 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005381
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005382 cr0 = read_cr0();
5383 WARN_ON(cr0 & X86_CR0_TS);
5384 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005385
5386 /*
5387 * Save the most likely value for this task's CR3 in the VMCS.
5388 * We can't use __get_current_cr3_fast() because we're not atomic.
5389 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005390 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005391 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005392 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005393
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005394 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005395 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005396 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005397 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005398
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005399 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005400#ifdef CONFIG_X86_64
5401 /*
5402 * Load null selectors, so we can avoid reloading them in
5403 * __vmx_load_host_state(), in case userspace uses the null selectors
5404 * too (the expected case).
5405 */
5406 vmcs_write16(HOST_DS_SELECTOR, 0);
5407 vmcs_write16(HOST_ES_SELECTOR, 0);
5408#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005409 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5410 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005411#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005412 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5413 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5414
Juergen Gross87930012017-09-04 12:25:27 +02005415 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005416 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005417 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005418
Avi Kivity83287ea422012-09-16 15:10:57 +03005419 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005420
5421 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5422 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5423 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5424 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5425
5426 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5427 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5428 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5429 }
5430}
5431
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005432static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5433{
5434 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5435 if (enable_ept)
5436 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005437 if (is_guest_mode(&vmx->vcpu))
5438 vmx->vcpu.arch.cr4_guest_owned_bits &=
5439 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005440 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5441}
5442
Yang Zhang01e439b2013-04-11 19:25:12 +08005443static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5444{
5445 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5446
Andrey Smetanind62caab2015-11-10 15:36:33 +03005447 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005448 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005449
5450 if (!enable_vnmi)
5451 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5452
Yunhong Jiang64672c92016-06-13 14:19:59 -07005453 /* Enable the preemption timer dynamically */
5454 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005455 return pin_based_exec_ctrl;
5456}
5457
Andrey Smetanind62caab2015-11-10 15:36:33 +03005458static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5459{
5460 struct vcpu_vmx *vmx = to_vmx(vcpu);
5461
5462 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005463 if (cpu_has_secondary_exec_ctrls()) {
5464 if (kvm_vcpu_apicv_active(vcpu))
5465 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5466 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5467 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5468 else
5469 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5470 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5471 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5472 }
5473
5474 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005475 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005476}
5477
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005478static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5479{
5480 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005481
5482 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5483 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5484
Paolo Bonzini35754c92015-07-29 12:05:37 +02005485 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005486 exec_control &= ~CPU_BASED_TPR_SHADOW;
5487#ifdef CONFIG_X86_64
5488 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5489 CPU_BASED_CR8_LOAD_EXITING;
5490#endif
5491 }
5492 if (!enable_ept)
5493 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5494 CPU_BASED_CR3_LOAD_EXITING |
5495 CPU_BASED_INVLPG_EXITING;
5496 return exec_control;
5497}
5498
Jim Mattson45ec3682017-08-23 16:32:04 -07005499static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005500{
Jim Mattson45ec3682017-08-23 16:32:04 -07005501 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005502 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005503}
5504
Jim Mattson75f4fc82017-08-23 16:32:03 -07005505static bool vmx_rdseed_supported(void)
5506{
5507 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005508 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005509}
5510
Paolo Bonzini80154d72017-08-24 13:55:35 +02005511static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005512{
Paolo Bonzini80154d72017-08-24 13:55:35 +02005513 struct kvm_vcpu *vcpu = &vmx->vcpu;
5514
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005515 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005516
Paolo Bonzini80154d72017-08-24 13:55:35 +02005517 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005518 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5519 if (vmx->vpid == 0)
5520 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5521 if (!enable_ept) {
5522 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5523 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005524 /* Enable INVPCID for non-ept guests may cause performance regression. */
5525 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005526 }
5527 if (!enable_unrestricted_guest)
5528 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5529 if (!ple_gap)
5530 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02005531 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005532 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5533 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005534 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005535
5536 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5537 * in vmx_set_cr4. */
5538 exec_control &= ~SECONDARY_EXEC_DESC;
5539
Abel Gordonabc4fc52013-04-18 14:35:25 +03005540 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5541 (handle_vmptrld).
5542 We can NOT enable shadow_vmcs here because we don't have yet
5543 a current VMCS12
5544 */
5545 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005546
5547 if (!enable_pml)
5548 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005549
Paolo Bonzini3db13482017-08-24 14:48:03 +02005550 if (vmx_xsaves_supported()) {
5551 /* Exposing XSAVES only when XSAVE is exposed */
5552 bool xsaves_enabled =
5553 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5554 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5555
5556 if (!xsaves_enabled)
5557 exec_control &= ~SECONDARY_EXEC_XSAVES;
5558
5559 if (nested) {
5560 if (xsaves_enabled)
5561 vmx->nested.nested_vmx_secondary_ctls_high |=
5562 SECONDARY_EXEC_XSAVES;
5563 else
5564 vmx->nested.nested_vmx_secondary_ctls_high &=
5565 ~SECONDARY_EXEC_XSAVES;
5566 }
5567 }
5568
Paolo Bonzini80154d72017-08-24 13:55:35 +02005569 if (vmx_rdtscp_supported()) {
5570 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5571 if (!rdtscp_enabled)
5572 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5573
5574 if (nested) {
5575 if (rdtscp_enabled)
5576 vmx->nested.nested_vmx_secondary_ctls_high |=
5577 SECONDARY_EXEC_RDTSCP;
5578 else
5579 vmx->nested.nested_vmx_secondary_ctls_high &=
5580 ~SECONDARY_EXEC_RDTSCP;
5581 }
5582 }
5583
5584 if (vmx_invpcid_supported()) {
5585 /* Exposing INVPCID only when PCID is exposed */
5586 bool invpcid_enabled =
5587 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5588 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5589
5590 if (!invpcid_enabled) {
5591 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5592 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5593 }
5594
5595 if (nested) {
5596 if (invpcid_enabled)
5597 vmx->nested.nested_vmx_secondary_ctls_high |=
5598 SECONDARY_EXEC_ENABLE_INVPCID;
5599 else
5600 vmx->nested.nested_vmx_secondary_ctls_high &=
5601 ~SECONDARY_EXEC_ENABLE_INVPCID;
5602 }
5603 }
5604
Jim Mattson45ec3682017-08-23 16:32:04 -07005605 if (vmx_rdrand_supported()) {
5606 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5607 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005608 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005609
5610 if (nested) {
5611 if (rdrand_enabled)
5612 vmx->nested.nested_vmx_secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005613 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005614 else
5615 vmx->nested.nested_vmx_secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005616 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005617 }
5618 }
5619
Jim Mattson75f4fc82017-08-23 16:32:03 -07005620 if (vmx_rdseed_supported()) {
5621 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5622 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005623 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005624
5625 if (nested) {
5626 if (rdseed_enabled)
5627 vmx->nested.nested_vmx_secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005628 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005629 else
5630 vmx->nested.nested_vmx_secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005631 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005632 }
5633 }
5634
Paolo Bonzini80154d72017-08-24 13:55:35 +02005635 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005636}
5637
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005638static void ept_set_mmio_spte_mask(void)
5639{
5640 /*
5641 * EPT Misconfigurations can be generated if the value of bits 2:0
5642 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005643 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07005644 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5645 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005646}
5647
Wanpeng Lif53cd632014-12-02 19:14:58 +08005648#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005649/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005650 * Sets up the vmcs for emulated real mode.
5651 */
David Hildenbrand12d79912017-08-24 20:51:26 +02005652static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005653{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005654#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005655 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005656#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005657 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005658
Abel Gordon4607c2d2013-04-18 14:35:55 +03005659 if (enable_shadow_vmcs) {
5660 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5661 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5662 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005663 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005664 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005665
Avi Kivity6aa8b732006-12-10 02:21:36 -08005666 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5667
Avi Kivity6aa8b732006-12-10 02:21:36 -08005668 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005669 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005670 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005671
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005672 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005673
Dan Williamsdfa169b2016-06-02 11:17:24 -07005674 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02005675 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005676 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02005677 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07005678 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005679
Andrey Smetanind62caab2015-11-10 15:36:33 +03005680 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005681 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5682 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5683 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5684 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5685
5686 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005687
Li RongQing0bcf2612015-12-03 13:29:34 +08005688 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005689 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005690 }
5691
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005692 if (ple_gap) {
5693 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005694 vmx->ple_window = ple_window;
5695 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005696 }
5697
Xiao Guangrongc3707952011-07-12 03:28:04 +08005698 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5699 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005700 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5701
Avi Kivity9581d442010-10-19 16:46:55 +02005702 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5703 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005704 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005705#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005706 rdmsrl(MSR_FS_BASE, a);
5707 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5708 rdmsrl(MSR_GS_BASE, a);
5709 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5710#else
5711 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5712 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5713#endif
5714
Bandan Das2a499e42017-08-03 15:54:41 -04005715 if (cpu_has_vmx_vmfunc())
5716 vmcs_write64(VM_FUNCTION_CONTROL, 0);
5717
Eddie Dong2cc51562007-05-21 07:28:09 +03005718 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5719 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005720 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005721 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005722 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005723
Radim Krčmář74545702015-04-27 15:11:25 +02005724 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5725 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005726
Paolo Bonzini03916db2014-07-24 14:21:57 +02005727 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005728 u32 index = vmx_msr_index[i];
5729 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005730 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005731
5732 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5733 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005734 if (wrmsr_safe(index, data_low, data_high) < 0)
5735 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005736 vmx->guest_msrs[j].index = i;
5737 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005738 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005739 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005740 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005741
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01005742 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
5743 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02005744
5745 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005746
5747 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005748 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005749
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005750 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5751 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5752
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005753 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005754
Wanpeng Lif53cd632014-12-02 19:14:58 +08005755 if (vmx_xsaves_supported())
5756 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5757
Peter Feiner4e595162016-07-07 14:49:58 -07005758 if (enable_pml) {
5759 ASSERT(vmx->pml_pg);
5760 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5761 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5762 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005763}
5764
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005765static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005766{
5767 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005768 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005769 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005770
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005771 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01005772 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005773
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005774 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005775 kvm_set_cr8(vcpu, 0);
5776
5777 if (!init_event) {
5778 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5779 MSR_IA32_APICBASE_ENABLE;
5780 if (kvm_vcpu_is_reset_bsp(vcpu))
5781 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5782 apic_base_msr.host_initiated = true;
5783 kvm_set_apic_base(vcpu, &apic_base_msr);
5784 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005785
Avi Kivity2fb92db2011-04-27 19:42:18 +03005786 vmx_segment_cache_clear(vmx);
5787
Avi Kivity5706be02008-08-20 15:07:31 +03005788 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005789 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005790 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005791
5792 seg_setup(VCPU_SREG_DS);
5793 seg_setup(VCPU_SREG_ES);
5794 seg_setup(VCPU_SREG_FS);
5795 seg_setup(VCPU_SREG_GS);
5796 seg_setup(VCPU_SREG_SS);
5797
5798 vmcs_write16(GUEST_TR_SELECTOR, 0);
5799 vmcs_writel(GUEST_TR_BASE, 0);
5800 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5801 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5802
5803 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5804 vmcs_writel(GUEST_LDTR_BASE, 0);
5805 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5806 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5807
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005808 if (!init_event) {
5809 vmcs_write32(GUEST_SYSENTER_CS, 0);
5810 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5811 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5812 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5813 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005814
Wanpeng Lic37c2872017-11-20 14:52:21 -08005815 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01005816 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005817
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005818 vmcs_writel(GUEST_GDTR_BASE, 0);
5819 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5820
5821 vmcs_writel(GUEST_IDTR_BASE, 0);
5822 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5823
Anthony Liguori443381a2010-12-06 10:53:38 -06005824 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005825 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005826 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07005827 if (kvm_mpx_supported())
5828 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005829
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005830 setup_msrs(vmx);
5831
Avi Kivity6aa8b732006-12-10 02:21:36 -08005832 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5833
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005834 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005835 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005836 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005837 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005838 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005839 vmcs_write32(TPR_THRESHOLD, 0);
5840 }
5841
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005842 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005843
Sheng Yang2384d2b2008-01-17 15:14:33 +08005844 if (vmx->vpid != 0)
5845 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5846
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005847 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005848 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005849 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005850 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005851 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005852
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005853 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005854
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005855 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005856}
5857
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005858/*
5859 * In nested virtualization, check if L1 asked to exit on external interrupts.
5860 * For most existing hypervisors, this will always return true.
5861 */
5862static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5863{
5864 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5865 PIN_BASED_EXT_INTR_MASK;
5866}
5867
Bandan Das77b0f5d2014-04-19 18:17:45 -04005868/*
5869 * In nested virtualization, check if L1 has set
5870 * VM_EXIT_ACK_INTR_ON_EXIT
5871 */
5872static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5873{
5874 return get_vmcs12(vcpu)->vm_exit_controls &
5875 VM_EXIT_ACK_INTR_ON_EXIT;
5876}
5877
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005878static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5879{
5880 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5881 PIN_BASED_NMI_EXITING;
5882}
5883
Jan Kiszkac9a79532014-03-07 20:03:15 +01005884static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005885{
Paolo Bonzini47c01522016-12-19 11:44:07 +01005886 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5887 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005888}
5889
Jan Kiszkac9a79532014-03-07 20:03:15 +01005890static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005891{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005892 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005893 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01005894 enable_irq_window(vcpu);
5895 return;
5896 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005897
Paolo Bonzini47c01522016-12-19 11:44:07 +01005898 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5899 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005900}
5901
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005902static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005903{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005904 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005905 uint32_t intr;
5906 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005907
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005908 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005909
Avi Kivityfa89a812008-09-01 15:57:51 +03005910 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005911 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005912 int inc_eip = 0;
5913 if (vcpu->arch.interrupt.soft)
5914 inc_eip = vcpu->arch.event_exit_inst_len;
5915 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005916 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005917 return;
5918 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005919 intr = irq | INTR_INFO_VALID_MASK;
5920 if (vcpu->arch.interrupt.soft) {
5921 intr |= INTR_TYPE_SOFT_INTR;
5922 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5923 vmx->vcpu.arch.event_exit_inst_len);
5924 } else
5925 intr |= INTR_TYPE_EXT_INTR;
5926 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005927}
5928
Sheng Yangf08864b2008-05-15 18:23:25 +08005929static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5930{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005931 struct vcpu_vmx *vmx = to_vmx(vcpu);
5932
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005933 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005934 /*
5935 * Tracking the NMI-blocked state in software is built upon
5936 * finding the next open IRQ window. This, in turn, depends on
5937 * well-behaving guests: They have to keep IRQs disabled at
5938 * least as long as the NMI handler runs. Otherwise we may
5939 * cause NMI nesting, maybe breaking the guest. But as this is
5940 * highly unlikely, we can live with the residual risk.
5941 */
5942 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
5943 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5944 }
5945
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005946 ++vcpu->stat.nmi_injections;
5947 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005948
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005949 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005950 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005951 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005952 return;
5953 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005954
Sheng Yangf08864b2008-05-15 18:23:25 +08005955 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5956 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005957}
5958
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005959static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5960{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005961 struct vcpu_vmx *vmx = to_vmx(vcpu);
5962 bool masked;
5963
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005964 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005965 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005966 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02005967 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005968 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
5969 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5970 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005971}
5972
5973static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5974{
5975 struct vcpu_vmx *vmx = to_vmx(vcpu);
5976
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005977 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005978 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
5979 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
5980 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5981 }
5982 } else {
5983 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5984 if (masked)
5985 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5986 GUEST_INTR_STATE_NMI);
5987 else
5988 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5989 GUEST_INTR_STATE_NMI);
5990 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005991}
5992
Jan Kiszka2505dc92013-04-14 12:12:47 +02005993static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5994{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005995 if (to_vmx(vcpu)->nested.nested_run_pending)
5996 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005997
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005998 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005999 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6000 return 0;
6001
Jan Kiszka2505dc92013-04-14 12:12:47 +02006002 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6003 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6004 | GUEST_INTR_STATE_NMI));
6005}
6006
Gleb Natapov78646122009-03-23 12:12:11 +02006007static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6008{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006009 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6010 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006011 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6012 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006013}
6014
Izik Eiduscbc94022007-10-25 00:29:55 +02006015static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6016{
6017 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006018
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006019 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6020 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006021 if (ret)
6022 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08006023 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006024 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006025}
6026
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006027static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006028{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006029 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006030 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006031 /*
6032 * Update instruction length as we may reinject the exception
6033 * from user space while in guest debugging mode.
6034 */
6035 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6036 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006037 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006038 return false;
6039 /* fall through */
6040 case DB_VECTOR:
6041 if (vcpu->guest_debug &
6042 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6043 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006044 /* fall through */
6045 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006046 case OF_VECTOR:
6047 case BR_VECTOR:
6048 case UD_VECTOR:
6049 case DF_VECTOR:
6050 case SS_VECTOR:
6051 case GP_VECTOR:
6052 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006053 return true;
6054 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006055 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006056 return false;
6057}
6058
6059static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6060 int vec, u32 err_code)
6061{
6062 /*
6063 * Instruction with address size override prefix opcode 0x67
6064 * Cause the #SS fault with 0 error code in VM86 mode.
6065 */
6066 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6067 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6068 if (vcpu->arch.halt_request) {
6069 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006070 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006071 }
6072 return 1;
6073 }
6074 return 0;
6075 }
6076
6077 /*
6078 * Forward all other exceptions that are valid in real mode.
6079 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6080 * the required debugging infrastructure rework.
6081 */
6082 kvm_queue_exception(vcpu, vec);
6083 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006084}
6085
Andi Kleena0861c02009-06-08 17:37:09 +08006086/*
6087 * Trigger machine check on the host. We assume all the MSRs are already set up
6088 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6089 * We pass a fake environment to the machine check handler because we want
6090 * the guest to be always treated like user space, no matter what context
6091 * it used internally.
6092 */
6093static void kvm_machine_check(void)
6094{
6095#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6096 struct pt_regs regs = {
6097 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6098 .flags = X86_EFLAGS_IF,
6099 };
6100
6101 do_machine_check(&regs, 0);
6102#endif
6103}
6104
Avi Kivity851ba692009-08-24 11:10:17 +03006105static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006106{
6107 /* already handled by vcpu_run */
6108 return 1;
6109}
6110
Avi Kivity851ba692009-08-24 11:10:17 +03006111static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006112{
Avi Kivity1155f762007-11-22 11:30:47 +02006113 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006114 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006115 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006116 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006117 u32 vect_info;
6118 enum emulation_result er;
6119
Avi Kivity1155f762007-11-22 11:30:47 +02006120 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006121 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006122
Andi Kleena0861c02009-06-08 17:37:09 +08006123 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006124 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006125
Jim Mattsonef85b672016-12-12 11:01:37 -08006126 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006127 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006128
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006129 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01006130 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alon61cb57c2017-11-05 16:56:32 +02006131 if (er == EMULATE_USER_EXIT)
6132 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006133 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02006134 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006135 return 1;
6136 }
6137
Avi Kivity6aa8b732006-12-10 02:21:36 -08006138 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006139 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006140 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006141
6142 /*
6143 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6144 * MMIO, it is better to report an internal error.
6145 * See the comments in vmx_handle_exit.
6146 */
6147 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6148 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6149 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6150 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006151 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006152 vcpu->run->internal.data[0] = vect_info;
6153 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006154 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006155 return 0;
6156 }
6157
Avi Kivity6aa8b732006-12-10 02:21:36 -08006158 if (is_page_fault(intr_info)) {
6159 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006160 /* EPT won't cause page fault directly */
6161 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006162 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006163 }
6164
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006165 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006166
6167 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6168 return handle_rmode_exception(vcpu, ex_no, error_code);
6169
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006170 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006171 case AC_VECTOR:
6172 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6173 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006174 case DB_VECTOR:
6175 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6176 if (!(vcpu->guest_debug &
6177 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006178 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006179 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01006180 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
6181 skip_emulated_instruction(vcpu);
6182
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006183 kvm_queue_exception(vcpu, DB_VECTOR);
6184 return 1;
6185 }
6186 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6187 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6188 /* fall through */
6189 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006190 /*
6191 * Update instruction length as we may reinject #BP from
6192 * user space while in guest debugging mode. Reading it for
6193 * #DB as well causes no harm, it is not used in that case.
6194 */
6195 vmx->vcpu.arch.event_exit_inst_len =
6196 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006197 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006198 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006199 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6200 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006201 break;
6202 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006203 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6204 kvm_run->ex.exception = ex_no;
6205 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006206 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006207 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006208 return 0;
6209}
6210
Avi Kivity851ba692009-08-24 11:10:17 +03006211static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006212{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006213 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006214 return 1;
6215}
6216
Avi Kivity851ba692009-08-24 11:10:17 +03006217static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006218{
Avi Kivity851ba692009-08-24 11:10:17 +03006219 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006220 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006221 return 0;
6222}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006223
Avi Kivity851ba692009-08-24 11:10:17 +03006224static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006225{
He, Qingbfdaab02007-09-12 14:18:28 +08006226 unsigned long exit_qualification;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006227 int size, in, string, ret;
Avi Kivity039576c2007-03-20 12:46:50 +02006228 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006229
He, Qingbfdaab02007-09-12 14:18:28 +08006230 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006231 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006232 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006233
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006234 ++vcpu->stat.io_exits;
6235
6236 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01006237 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006238
6239 port = exit_qualification >> 16;
6240 size = (exit_qualification & 7) + 1;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006241
Kyle Huey6affcbe2016-11-29 12:40:40 -08006242 ret = kvm_skip_emulated_instruction(vcpu);
6243
6244 /*
6245 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
6246 * KVM_EXIT_DEBUG here.
6247 */
6248 return kvm_fast_pio_out(vcpu, size, port) && ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006249}
6250
Ingo Molnar102d8322007-02-19 14:37:47 +02006251static void
6252vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6253{
6254 /*
6255 * Patch in the VMCALL instruction:
6256 */
6257 hypercall[0] = 0x0f;
6258 hypercall[1] = 0x01;
6259 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006260}
6261
Guo Chao0fa06072012-06-28 15:16:19 +08006262/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006263static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6264{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006265 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006266 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6267 unsigned long orig_val = val;
6268
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006269 /*
6270 * We get here when L2 changed cr0 in a way that did not change
6271 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006272 * but did change L0 shadowed bits. So we first calculate the
6273 * effective cr0 value that L1 would like to write into the
6274 * hardware. It consists of the L2-owned bits from the new
6275 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006276 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006277 val = (val & ~vmcs12->cr0_guest_host_mask) |
6278 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6279
David Matlack38991522016-11-29 18:14:08 -08006280 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006281 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006282
6283 if (kvm_set_cr0(vcpu, val))
6284 return 1;
6285 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006286 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006287 } else {
6288 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006289 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006290 return 1;
David Matlack38991522016-11-29 18:14:08 -08006291
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006292 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006293 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006294}
6295
6296static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6297{
6298 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006299 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6300 unsigned long orig_val = val;
6301
6302 /* analogously to handle_set_cr0 */
6303 val = (val & ~vmcs12->cr4_guest_host_mask) |
6304 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6305 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006306 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006307 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006308 return 0;
6309 } else
6310 return kvm_set_cr4(vcpu, val);
6311}
6312
Paolo Bonzini0367f202016-07-12 10:44:55 +02006313static int handle_desc(struct kvm_vcpu *vcpu)
6314{
6315 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6316 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6317}
6318
Avi Kivity851ba692009-08-24 11:10:17 +03006319static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006320{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006321 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006322 int cr;
6323 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006324 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006325 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006326
He, Qingbfdaab02007-09-12 14:18:28 +08006327 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006328 cr = exit_qualification & 15;
6329 reg = (exit_qualification >> 8) & 15;
6330 switch ((exit_qualification >> 4) & 3) {
6331 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006332 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006333 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006334 switch (cr) {
6335 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006336 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006337 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006338 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006339 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006340 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006341 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006342 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006343 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006344 case 8: {
6345 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006346 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006347 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006348 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006349 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006350 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006351 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006352 return ret;
6353 /*
6354 * TODO: we might be squashing a
6355 * KVM_GUESTDBG_SINGLESTEP-triggered
6356 * KVM_EXIT_DEBUG here.
6357 */
Avi Kivity851ba692009-08-24 11:10:17 +03006358 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006359 return 0;
6360 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006361 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006362 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006363 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006364 WARN_ONCE(1, "Guest should always own CR0.TS");
6365 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006366 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006367 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006368 case 1: /*mov from cr*/
6369 switch (cr) {
6370 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006371 val = kvm_read_cr3(vcpu);
6372 kvm_register_write(vcpu, reg, val);
6373 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006374 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006375 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006376 val = kvm_get_cr8(vcpu);
6377 kvm_register_write(vcpu, reg, val);
6378 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006379 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006380 }
6381 break;
6382 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006383 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006384 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006385 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006386
Kyle Huey6affcbe2016-11-29 12:40:40 -08006387 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006388 default:
6389 break;
6390 }
Avi Kivity851ba692009-08-24 11:10:17 +03006391 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006392 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006393 (int)(exit_qualification >> 4) & 3, cr);
6394 return 0;
6395}
6396
Avi Kivity851ba692009-08-24 11:10:17 +03006397static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006398{
He, Qingbfdaab02007-09-12 14:18:28 +08006399 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006400 int dr, dr7, reg;
6401
6402 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6403 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6404
6405 /* First, if DR does not exist, trigger UD */
6406 if (!kvm_require_dr(vcpu, dr))
6407 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006408
Jan Kiszkaf2483412010-01-20 18:20:20 +01006409 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006410 if (!kvm_require_cpl(vcpu, 0))
6411 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006412 dr7 = vmcs_readl(GUEST_DR7);
6413 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006414 /*
6415 * As the vm-exit takes precedence over the debug trap, we
6416 * need to emulate the latter, either for the host or the
6417 * guest debugging itself.
6418 */
6419 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006420 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006421 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006422 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006423 vcpu->run->debug.arch.exception = DB_VECTOR;
6424 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006425 return 0;
6426 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006427 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006428 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006429 kvm_queue_exception(vcpu, DB_VECTOR);
6430 return 1;
6431 }
6432 }
6433
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006434 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006435 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6436 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006437
6438 /*
6439 * No more DR vmexits; force a reload of the debug registers
6440 * and reenter on this instruction. The next vmexit will
6441 * retrieve the full state of the debug registers.
6442 */
6443 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6444 return 1;
6445 }
6446
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006447 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6448 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006449 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006450
6451 if (kvm_get_dr(vcpu, dr, &val))
6452 return 1;
6453 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006454 } else
Nadav Amit57773922014-06-18 17:19:23 +03006455 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006456 return 1;
6457
Kyle Huey6affcbe2016-11-29 12:40:40 -08006458 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006459}
6460
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006461static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6462{
6463 return vcpu->arch.dr6;
6464}
6465
6466static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6467{
6468}
6469
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006470static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6471{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006472 get_debugreg(vcpu->arch.db[0], 0);
6473 get_debugreg(vcpu->arch.db[1], 1);
6474 get_debugreg(vcpu->arch.db[2], 2);
6475 get_debugreg(vcpu->arch.db[3], 3);
6476 get_debugreg(vcpu->arch.dr6, 6);
6477 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6478
6479 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006480 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006481}
6482
Gleb Natapov020df072010-04-13 10:05:23 +03006483static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6484{
6485 vmcs_writel(GUEST_DR7, val);
6486}
6487
Avi Kivity851ba692009-08-24 11:10:17 +03006488static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006489{
Kyle Huey6a908b62016-11-29 12:40:37 -08006490 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006491}
6492
Avi Kivity851ba692009-08-24 11:10:17 +03006493static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006494{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006495 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006496 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006497
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006498 msr_info.index = ecx;
6499 msr_info.host_initiated = false;
6500 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006501 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006502 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006503 return 1;
6504 }
6505
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006506 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006507
Avi Kivity6aa8b732006-12-10 02:21:36 -08006508 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006509 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6510 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006511 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006512}
6513
Avi Kivity851ba692009-08-24 11:10:17 +03006514static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006515{
Will Auld8fe8ab42012-11-29 12:42:12 -08006516 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006517 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6518 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6519 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006520
Will Auld8fe8ab42012-11-29 12:42:12 -08006521 msr.data = data;
6522 msr.index = ecx;
6523 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006524 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006525 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006526 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006527 return 1;
6528 }
6529
Avi Kivity59200272010-01-25 19:47:02 +02006530 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006531 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006532}
6533
Avi Kivity851ba692009-08-24 11:10:17 +03006534static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006535{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01006536 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006537 return 1;
6538}
6539
Avi Kivity851ba692009-08-24 11:10:17 +03006540static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006541{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006542 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6543 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006544
Avi Kivity3842d132010-07-27 12:30:24 +03006545 kvm_make_request(KVM_REQ_EVENT, vcpu);
6546
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006547 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006548 return 1;
6549}
6550
Avi Kivity851ba692009-08-24 11:10:17 +03006551static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006552{
Avi Kivityd3bef152007-06-05 15:53:05 +03006553 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006554}
6555
Avi Kivity851ba692009-08-24 11:10:17 +03006556static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006557{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006558 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006559}
6560
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006561static int handle_invd(struct kvm_vcpu *vcpu)
6562{
Andre Przywara51d8b662010-12-21 11:12:02 +01006563 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006564}
6565
Avi Kivity851ba692009-08-24 11:10:17 +03006566static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006567{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006568 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006569
6570 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006571 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006572}
6573
Avi Kivityfee84b02011-11-10 14:57:25 +02006574static int handle_rdpmc(struct kvm_vcpu *vcpu)
6575{
6576 int err;
6577
6578 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006579 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02006580}
6581
Avi Kivity851ba692009-08-24 11:10:17 +03006582static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006583{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006584 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006585}
6586
Dexuan Cui2acf9232010-06-10 11:27:12 +08006587static int handle_xsetbv(struct kvm_vcpu *vcpu)
6588{
6589 u64 new_bv = kvm_read_edx_eax(vcpu);
6590 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6591
6592 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006593 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08006594 return 1;
6595}
6596
Wanpeng Lif53cd632014-12-02 19:14:58 +08006597static int handle_xsaves(struct kvm_vcpu *vcpu)
6598{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006599 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006600 WARN(1, "this should never happen\n");
6601 return 1;
6602}
6603
6604static int handle_xrstors(struct kvm_vcpu *vcpu)
6605{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006606 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006607 WARN(1, "this should never happen\n");
6608 return 1;
6609}
6610
Avi Kivity851ba692009-08-24 11:10:17 +03006611static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006612{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006613 if (likely(fasteoi)) {
6614 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6615 int access_type, offset;
6616
6617 access_type = exit_qualification & APIC_ACCESS_TYPE;
6618 offset = exit_qualification & APIC_ACCESS_OFFSET;
6619 /*
6620 * Sane guest uses MOV to write EOI, with written value
6621 * not cared. So make a short-circuit here by avoiding
6622 * heavy instruction emulation.
6623 */
6624 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6625 (offset == APIC_EOI)) {
6626 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006627 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03006628 }
6629 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006630 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006631}
6632
Yang Zhangc7c9c562013-01-25 10:18:51 +08006633static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6634{
6635 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6636 int vector = exit_qualification & 0xff;
6637
6638 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6639 kvm_apic_set_eoi_accelerated(vcpu, vector);
6640 return 1;
6641}
6642
Yang Zhang83d4c282013-01-25 10:18:49 +08006643static int handle_apic_write(struct kvm_vcpu *vcpu)
6644{
6645 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6646 u32 offset = exit_qualification & 0xfff;
6647
6648 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6649 kvm_apic_write_nodecode(vcpu, offset);
6650 return 1;
6651}
6652
Avi Kivity851ba692009-08-24 11:10:17 +03006653static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006654{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006655 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006656 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006657 bool has_error_code = false;
6658 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006659 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006660 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006661
6662 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006663 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006664 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006665
6666 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6667
6668 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006669 if (reason == TASK_SWITCH_GATE && idt_v) {
6670 switch (type) {
6671 case INTR_TYPE_NMI_INTR:
6672 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006673 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006674 break;
6675 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006676 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006677 kvm_clear_interrupt_queue(vcpu);
6678 break;
6679 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006680 if (vmx->idt_vectoring_info &
6681 VECTORING_INFO_DELIVER_CODE_MASK) {
6682 has_error_code = true;
6683 error_code =
6684 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6685 }
6686 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006687 case INTR_TYPE_SOFT_EXCEPTION:
6688 kvm_clear_exception_queue(vcpu);
6689 break;
6690 default:
6691 break;
6692 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006693 }
Izik Eidus37817f22008-03-24 23:14:53 +02006694 tss_selector = exit_qualification;
6695
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006696 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6697 type != INTR_TYPE_EXT_INTR &&
6698 type != INTR_TYPE_NMI_INTR))
6699 skip_emulated_instruction(vcpu);
6700
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006701 if (kvm_task_switch(vcpu, tss_selector,
6702 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6703 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006704 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6705 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6706 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006707 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006708 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006709
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006710 /*
6711 * TODO: What about debug traps on tss switch?
6712 * Are we supposed to inject them and update dr6?
6713 */
6714
6715 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006716}
6717
Avi Kivity851ba692009-08-24 11:10:17 +03006718static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006719{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006720 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006721 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01006722 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006723
Sheng Yangf9c617f2009-03-25 10:08:52 +08006724 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006725
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006726 /*
6727 * EPT violation happened while executing iret from NMI,
6728 * "blocked by NMI" bit has to be set before next VM entry.
6729 * There are errata that may cause this bit to not be set:
6730 * AAK134, BY25.
6731 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006732 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006733 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006734 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006735 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6736
Sheng Yang14394422008-04-28 12:24:45 +08006737 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006738 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006739
Junaid Shahid27959a42016-12-06 16:46:10 -08006740 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006741 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08006742 ? PFERR_USER_MASK : 0;
6743 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006744 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08006745 ? PFERR_WRITE_MASK : 0;
6746 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006747 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08006748 ? PFERR_FETCH_MASK : 0;
6749 /* ept page table entry is present? */
6750 error_code |= (exit_qualification &
6751 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6752 EPT_VIOLATION_EXECUTABLE))
6753 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006754
Paolo Bonzinieebed242016-11-28 14:39:58 +01006755 error_code |= (exit_qualification & 0x100) != 0 ?
6756 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006757
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006758 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006759 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006760}
6761
Avi Kivity851ba692009-08-24 11:10:17 +03006762static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006763{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006764 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006765 gpa_t gpa;
6766
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02006767 /*
6768 * A nested guest cannot optimize MMIO vmexits, because we have an
6769 * nGPA here instead of the required GPA.
6770 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006771 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02006772 if (!is_guest_mode(vcpu) &&
6773 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08006774 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006775 /*
6776 * Doing kvm_skip_emulated_instruction() depends on undefined
6777 * behavior: Intel's manual doesn't mandate
6778 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
6779 * occurs and while on real hardware it was observed to be set,
6780 * other hypervisors (namely Hyper-V) don't set it, we end up
6781 * advancing IP with some random value. Disable fast mmio when
6782 * running nested and keep it for real hardware in hope that
6783 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
6784 */
6785 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
6786 return kvm_skip_emulated_instruction(vcpu);
6787 else
6788 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
6789 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006790 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006791
Paolo Bonzinie08d26f2017-08-17 18:36:56 +02006792 ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
6793 if (ret >= 0)
6794 return ret;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006795
6796 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006797 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006798
Avi Kivity851ba692009-08-24 11:10:17 +03006799 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6800 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006801
6802 return 0;
6803}
6804
Avi Kivity851ba692009-08-24 11:10:17 +03006805static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006806{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006807 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01006808 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6809 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08006810 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006811 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006812
6813 return 1;
6814}
6815
Mohammed Gamal80ced182009-09-01 12:48:18 +02006816static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006817{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006818 struct vcpu_vmx *vmx = to_vmx(vcpu);
6819 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006820 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006821 u32 cpu_exec_ctrl;
6822 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006823 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006824
6825 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6826 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006827
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006828 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006829 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006830 return handle_interrupt_window(&vmx->vcpu);
6831
Radim Krčmář72875d82017-04-26 22:32:19 +02006832 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03006833 return 1;
6834
Liran Alon9b8ae632017-11-05 16:56:34 +02006835 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006836
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006837 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006838 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006839 ret = 0;
6840 goto out;
6841 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006842
Avi Kivityde5f70e2012-06-12 20:22:28 +03006843 if (err != EMULATE_DONE) {
6844 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6845 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6846 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006847 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03006848 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006849
Gleb Natapov8d76c492013-05-08 18:38:44 +03006850 if (vcpu->arch.halt_request) {
6851 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006852 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006853 goto out;
6854 }
6855
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006856 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006857 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006858 if (need_resched())
6859 schedule();
6860 }
6861
Mohammed Gamal80ced182009-09-01 12:48:18 +02006862out:
6863 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006864}
6865
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006866static int __grow_ple_window(int val)
6867{
6868 if (ple_window_grow < 1)
6869 return ple_window;
6870
6871 val = min(val, ple_window_actual_max);
6872
6873 if (ple_window_grow < ple_window)
6874 val *= ple_window_grow;
6875 else
6876 val += ple_window_grow;
6877
6878 return val;
6879}
6880
6881static int __shrink_ple_window(int val, int modifier, int minimum)
6882{
6883 if (modifier < 1)
6884 return ple_window;
6885
6886 if (modifier < ple_window)
6887 val /= modifier;
6888 else
6889 val -= modifier;
6890
6891 return max(val, minimum);
6892}
6893
6894static void grow_ple_window(struct kvm_vcpu *vcpu)
6895{
6896 struct vcpu_vmx *vmx = to_vmx(vcpu);
6897 int old = vmx->ple_window;
6898
6899 vmx->ple_window = __grow_ple_window(old);
6900
6901 if (vmx->ple_window != old)
6902 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006903
6904 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006905}
6906
6907static void shrink_ple_window(struct kvm_vcpu *vcpu)
6908{
6909 struct vcpu_vmx *vmx = to_vmx(vcpu);
6910 int old = vmx->ple_window;
6911
6912 vmx->ple_window = __shrink_ple_window(old,
6913 ple_window_shrink, ple_window);
6914
6915 if (vmx->ple_window != old)
6916 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006917
6918 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006919}
6920
6921/*
6922 * ple_window_actual_max is computed to be one grow_ple_window() below
6923 * ple_window_max. (See __grow_ple_window for the reason.)
6924 * This prevents overflows, because ple_window_max is int.
6925 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6926 * this process.
6927 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6928 */
6929static void update_ple_window_actual_max(void)
6930{
6931 ple_window_actual_max =
6932 __shrink_ple_window(max(ple_window_max, ple_window),
6933 ple_window_grow, INT_MIN);
6934}
6935
Feng Wubf9f6ac2015-09-18 22:29:55 +08006936/*
6937 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6938 */
6939static void wakeup_handler(void)
6940{
6941 struct kvm_vcpu *vcpu;
6942 int cpu = smp_processor_id();
6943
6944 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6945 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6946 blocked_vcpu_list) {
6947 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6948
6949 if (pi_test_on(pi_desc) == 1)
6950 kvm_vcpu_kick(vcpu);
6951 }
6952 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6953}
6954
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006955void vmx_enable_tdp(void)
6956{
6957 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
6958 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
6959 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
6960 0ull, VMX_EPT_EXECUTABLE_MASK,
6961 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05006962 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006963
6964 ept_set_mmio_spte_mask();
6965 kvm_enable_tdp();
6966}
6967
Tiejun Chenf2c76482014-10-28 10:14:47 +08006968static __init int hardware_setup(void)
6969{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006970 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006971
6972 rdmsrl_safe(MSR_EFER, &host_efer);
6973
6974 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6975 kvm_define_shared_msr(i, vmx_msr_index[i]);
6976
Radim Krčmář23611332016-09-29 22:41:33 +02006977 for (i = 0; i < VMX_BITMAP_NR; i++) {
6978 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
6979 if (!vmx_bitmap[i])
6980 goto out;
6981 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006982
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006983 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6984 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6985
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006986 if (setup_vmcs_config(&vmcs_config) < 0) {
6987 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02006988 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006989 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006990
6991 if (boot_cpu_has(X86_FEATURE_NX))
6992 kvm_enable_efer_bits(EFER_NX);
6993
Wanpeng Li08d839c2017-03-23 05:30:08 -07006994 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6995 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006996 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07006997
Tiejun Chenf2c76482014-10-28 10:14:47 +08006998 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02006999 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007000 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007001 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007002 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007003
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007004 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007005 enable_ept_ad_bits = 0;
7006
Wanpeng Li8ad81822017-10-09 15:51:53 -07007007 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007008 enable_unrestricted_guest = 0;
7009
Paolo Bonziniad15a292015-01-30 16:18:49 +01007010 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007011 flexpriority_enabled = 0;
7012
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007013 if (!cpu_has_virtual_nmis())
7014 enable_vnmi = 0;
7015
Paolo Bonziniad15a292015-01-30 16:18:49 +01007016 /*
7017 * set_apic_access_page_addr() is used to reload apic access
7018 * page upon invalidation. No need to do anything if not
7019 * using the APIC_ACCESS_ADDR VMCS field.
7020 */
7021 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007022 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007023
7024 if (!cpu_has_vmx_tpr_shadow())
7025 kvm_x86_ops->update_cr8_intercept = NULL;
7026
7027 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7028 kvm_disable_largepages();
7029
Wanpeng Li0f107682017-09-28 18:06:24 -07007030 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007031 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007032 ple_window = 0;
7033 ple_window_grow = 0;
7034 ple_window_max = 0;
7035 ple_window_shrink = 0;
7036 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007037
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007038 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007039 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007040 kvm_x86_ops->sync_pir_to_irr = NULL;
7041 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007042
Haozhong Zhang64903d62015-10-20 15:39:09 +08007043 if (cpu_has_vmx_tsc_scaling()) {
7044 kvm_has_tsc_control = true;
7045 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7046 kvm_tsc_scaling_ratio_frac_bits = 48;
7047 }
7048
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007049 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7050
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007051 if (enable_ept)
7052 vmx_enable_tdp();
7053 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007054 kvm_disable_tdp();
7055
7056 update_ple_window_actual_max();
7057
Kai Huang843e4332015-01-28 10:54:28 +08007058 /*
7059 * Only enable PML when hardware supports PML feature, and both EPT
7060 * and EPT A/D bit features are enabled -- PML depends on them to work.
7061 */
7062 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7063 enable_pml = 0;
7064
7065 if (!enable_pml) {
7066 kvm_x86_ops->slot_enable_log_dirty = NULL;
7067 kvm_x86_ops->slot_disable_log_dirty = NULL;
7068 kvm_x86_ops->flush_log_dirty = NULL;
7069 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7070 }
7071
Yunhong Jiang64672c92016-06-13 14:19:59 -07007072 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7073 u64 vmx_msr;
7074
7075 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7076 cpu_preemption_timer_multi =
7077 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7078 } else {
7079 kvm_x86_ops->set_hv_timer = NULL;
7080 kvm_x86_ops->cancel_hv_timer = NULL;
7081 }
7082
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007083 if (!cpu_has_vmx_shadow_vmcs())
7084 enable_shadow_vmcs = 0;
7085 if (enable_shadow_vmcs)
7086 init_vmcs_shadow_fields();
7087
Feng Wubf9f6ac2015-09-18 22:29:55 +08007088 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
7089
Ashok Rajc45dcc72016-06-22 14:59:56 +08007090 kvm_mce_cap_supported |= MCG_LMCE_P;
7091
Tiejun Chenf2c76482014-10-28 10:14:47 +08007092 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007093
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007094out:
Radim Krčmář23611332016-09-29 22:41:33 +02007095 for (i = 0; i < VMX_BITMAP_NR; i++)
7096 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007097
7098 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007099}
7100
7101static __exit void hardware_unsetup(void)
7102{
Radim Krčmář23611332016-09-29 22:41:33 +02007103 int i;
7104
7105 for (i = 0; i < VMX_BITMAP_NR; i++)
7106 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007107
Tiejun Chenf2c76482014-10-28 10:14:47 +08007108 free_kvm_area();
7109}
7110
Avi Kivity6aa8b732006-12-10 02:21:36 -08007111/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007112 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7113 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7114 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007115static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007116{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007117 if (ple_gap)
7118 grow_ple_window(vcpu);
7119
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007120 /*
7121 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7122 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7123 * never set PAUSE_EXITING and just set PLE if supported,
7124 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7125 */
7126 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007127 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007128}
7129
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007130static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007131{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007132 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007133}
7134
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007135static int handle_mwait(struct kvm_vcpu *vcpu)
7136{
7137 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7138 return handle_nop(vcpu);
7139}
7140
Jim Mattson45ec3682017-08-23 16:32:04 -07007141static int handle_invalid_op(struct kvm_vcpu *vcpu)
7142{
7143 kvm_queue_exception(vcpu, UD_VECTOR);
7144 return 1;
7145}
7146
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007147static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7148{
7149 return 1;
7150}
7151
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007152static int handle_monitor(struct kvm_vcpu *vcpu)
7153{
7154 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7155 return handle_nop(vcpu);
7156}
7157
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007158/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007159 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7160 * set the success or error code of an emulated VMX instruction, as specified
7161 * by Vol 2B, VMX Instruction Reference, "Conventions".
7162 */
7163static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7164{
7165 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7166 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7167 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7168}
7169
7170static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7171{
7172 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7173 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7174 X86_EFLAGS_SF | X86_EFLAGS_OF))
7175 | X86_EFLAGS_CF);
7176}
7177
Abel Gordon145c28d2013-04-18 14:36:55 +03007178static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007179 u32 vm_instruction_error)
7180{
7181 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7182 /*
7183 * failValid writes the error number to the current VMCS, which
7184 * can't be done there isn't a current VMCS.
7185 */
7186 nested_vmx_failInvalid(vcpu);
7187 return;
7188 }
7189 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7190 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7191 X86_EFLAGS_SF | X86_EFLAGS_OF))
7192 | X86_EFLAGS_ZF);
7193 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7194 /*
7195 * We don't need to force a shadow sync because
7196 * VM_INSTRUCTION_ERROR is not shadowed
7197 */
7198}
Abel Gordon145c28d2013-04-18 14:36:55 +03007199
Wincy Vanff651cb2014-12-11 08:52:58 +03007200static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7201{
7202 /* TODO: not to reset guest simply here. */
7203 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007204 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007205}
7206
Jan Kiszkaf4124502014-03-07 20:03:13 +01007207static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7208{
7209 struct vcpu_vmx *vmx =
7210 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7211
7212 vmx->nested.preemption_timer_expired = true;
7213 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7214 kvm_vcpu_kick(&vmx->vcpu);
7215
7216 return HRTIMER_NORESTART;
7217}
7218
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007219/*
Bandan Das19677e32014-05-06 02:19:15 -04007220 * Decode the memory-address operand of a vmx instruction, as recorded on an
7221 * exit caused by such an instruction (run by a guest hypervisor).
7222 * On success, returns 0. When the operand is invalid, returns 1 and throws
7223 * #UD or #GP.
7224 */
7225static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7226 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007227 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007228{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007229 gva_t off;
7230 bool exn;
7231 struct kvm_segment s;
7232
Bandan Das19677e32014-05-06 02:19:15 -04007233 /*
7234 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7235 * Execution", on an exit, vmx_instruction_info holds most of the
7236 * addressing components of the operand. Only the displacement part
7237 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7238 * For how an actual address is calculated from all these components,
7239 * refer to Vol. 1, "Operand Addressing".
7240 */
7241 int scaling = vmx_instruction_info & 3;
7242 int addr_size = (vmx_instruction_info >> 7) & 7;
7243 bool is_reg = vmx_instruction_info & (1u << 10);
7244 int seg_reg = (vmx_instruction_info >> 15) & 7;
7245 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7246 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7247 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7248 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7249
7250 if (is_reg) {
7251 kvm_queue_exception(vcpu, UD_VECTOR);
7252 return 1;
7253 }
7254
7255 /* Addr = segment_base + offset */
7256 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007257 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007258 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007259 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007260 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007261 off += kvm_register_read(vcpu, index_reg)<<scaling;
7262 vmx_get_segment(vcpu, &s, seg_reg);
7263 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007264
7265 if (addr_size == 1) /* 32 bit */
7266 *ret &= 0xffffffff;
7267
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007268 /* Checks for #GP/#SS exceptions. */
7269 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007270 if (is_long_mode(vcpu)) {
7271 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7272 * non-canonical form. This is the only check on the memory
7273 * destination for long mode!
7274 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007275 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007276 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007277 /* Protected mode: apply checks for segment validity in the
7278 * following order:
7279 * - segment type check (#GP(0) may be thrown)
7280 * - usability check (#GP(0)/#SS(0))
7281 * - limit check (#GP(0)/#SS(0))
7282 */
7283 if (wr)
7284 /* #GP(0) if the destination operand is located in a
7285 * read-only data segment or any code segment.
7286 */
7287 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7288 else
7289 /* #GP(0) if the source operand is located in an
7290 * execute-only code segment
7291 */
7292 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007293 if (exn) {
7294 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7295 return 1;
7296 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007297 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7298 */
7299 exn = (s.unusable != 0);
7300 /* Protected mode: #GP(0)/#SS(0) if the memory
7301 * operand is outside the segment limit.
7302 */
7303 exn = exn || (off + sizeof(u64) > s.limit);
7304 }
7305 if (exn) {
7306 kvm_queue_exception_e(vcpu,
7307 seg_reg == VCPU_SREG_SS ?
7308 SS_VECTOR : GP_VECTOR,
7309 0);
7310 return 1;
7311 }
7312
Bandan Das19677e32014-05-06 02:19:15 -04007313 return 0;
7314}
7315
Radim Krčmářcbf71272017-05-19 15:48:51 +02007316static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007317{
7318 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007319 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007320
7321 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007322 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007323 return 1;
7324
Radim Krčmářcbf71272017-05-19 15:48:51 +02007325 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7326 sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007327 kvm_inject_page_fault(vcpu, &e);
7328 return 1;
7329 }
7330
Bandan Das3573e222014-05-06 02:19:16 -04007331 return 0;
7332}
7333
Jim Mattsone29acc52016-11-30 12:03:43 -08007334static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7335{
7336 struct vcpu_vmx *vmx = to_vmx(vcpu);
7337 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007338 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007339
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007340 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7341 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007342 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007343
7344 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7345 if (!vmx->nested.cached_vmcs12)
7346 goto out_cached_vmcs12;
7347
7348 if (enable_shadow_vmcs) {
7349 shadow_vmcs = alloc_vmcs();
7350 if (!shadow_vmcs)
7351 goto out_shadow_vmcs;
7352 /* mark vmcs as shadow */
7353 shadow_vmcs->revision_id |= (1u << 31);
7354 /* init shadow vmcs */
7355 vmcs_clear(shadow_vmcs);
7356 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7357 }
7358
Jim Mattsone29acc52016-11-30 12:03:43 -08007359 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7360 HRTIMER_MODE_REL_PINNED);
7361 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7362
7363 vmx->nested.vmxon = true;
7364 return 0;
7365
7366out_shadow_vmcs:
7367 kfree(vmx->nested.cached_vmcs12);
7368
7369out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007370 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007371
Jim Mattsonde3a0022017-11-27 17:22:25 -06007372out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007373 return -ENOMEM;
7374}
7375
Bandan Das3573e222014-05-06 02:19:16 -04007376/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007377 * Emulate the VMXON instruction.
7378 * Currently, we just remember that VMX is active, and do not save or even
7379 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7380 * do not currently need to store anything in that guest-allocated memory
7381 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7382 * argument is different from the VMXON pointer (which the spec says they do).
7383 */
7384static int handle_vmon(struct kvm_vcpu *vcpu)
7385{
Jim Mattsone29acc52016-11-30 12:03:43 -08007386 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007387 gpa_t vmptr;
7388 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007389 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007390 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7391 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007392
Jim Mattson70f3aac2017-04-26 08:53:46 -07007393 /*
7394 * The Intel VMX Instruction Reference lists a bunch of bits that are
7395 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7396 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7397 * Otherwise, we should fail with #UD. But most faulting conditions
7398 * have already been checked by hardware, prior to the VM-exit for
7399 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7400 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007401 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007402 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007403 kvm_queue_exception(vcpu, UD_VECTOR);
7404 return 1;
7405 }
7406
Abel Gordon145c28d2013-04-18 14:36:55 +03007407 if (vmx->nested.vmxon) {
7408 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007409 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007410 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007411
Haozhong Zhang3b840802016-06-22 14:59:54 +08007412 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007413 != VMXON_NEEDED_FEATURES) {
7414 kvm_inject_gp(vcpu, 0);
7415 return 1;
7416 }
7417
Radim Krčmářcbf71272017-05-19 15:48:51 +02007418 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007419 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007420
7421 /*
7422 * SDM 3: 24.11.5
7423 * The first 4 bytes of VMXON region contain the supported
7424 * VMCS revision identifier
7425 *
7426 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7427 * which replaces physical address width with 32
7428 */
7429 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7430 nested_vmx_failInvalid(vcpu);
7431 return kvm_skip_emulated_instruction(vcpu);
7432 }
7433
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007434 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7435 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007436 nested_vmx_failInvalid(vcpu);
7437 return kvm_skip_emulated_instruction(vcpu);
7438 }
7439 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7440 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007441 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007442 nested_vmx_failInvalid(vcpu);
7443 return kvm_skip_emulated_instruction(vcpu);
7444 }
7445 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007446 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007447
7448 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007449 ret = enter_vmx_operation(vcpu);
7450 if (ret)
7451 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007452
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007453 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007454 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007455}
7456
7457/*
7458 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7459 * for running VMX instructions (except VMXON, whose prerequisites are
7460 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07007461 * Note that many of these exceptions have priority over VM exits, so they
7462 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007463 */
7464static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7465{
Jim Mattson70f3aac2017-04-26 08:53:46 -07007466 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007467 kvm_queue_exception(vcpu, UD_VECTOR);
7468 return 0;
7469 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007470 return 1;
7471}
7472
David Matlack8ca44e82017-08-01 14:00:39 -07007473static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7474{
7475 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7476 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7477}
7478
Abel Gordone7953d72013-04-18 14:37:55 +03007479static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7480{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007481 if (vmx->nested.current_vmptr == -1ull)
7482 return;
7483
Abel Gordon012f83c2013-04-18 14:39:25 +03007484 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007485 /* copy to memory all shadowed fields in case
7486 they were modified */
7487 copy_shadow_to_vmcs12(vmx);
7488 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07007489 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03007490 }
Wincy Van705699a2015-02-03 23:58:17 +08007491 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007492
7493 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007494 kvm_vcpu_write_guest_page(&vmx->vcpu,
7495 vmx->nested.current_vmptr >> PAGE_SHIFT,
7496 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07007497
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007498 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03007499}
7500
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007501/*
7502 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7503 * just stops using VMX.
7504 */
7505static void free_nested(struct vcpu_vmx *vmx)
7506{
Wanpeng Lib7455822017-11-22 14:04:00 -08007507 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007508 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007509
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007510 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08007511 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007512 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07007513 vmx->nested.posted_intr_nv = -1;
7514 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007515 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07007516 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007517 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7518 free_vmcs(vmx->vmcs01.shadow_vmcs);
7519 vmx->vmcs01.shadow_vmcs = NULL;
7520 }
David Matlack4f2777b2016-07-13 17:16:37 -07007521 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06007522 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007523 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007524 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007525 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007526 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007527 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007528 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007529 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007530 }
Wincy Van705699a2015-02-03 23:58:17 +08007531 if (vmx->nested.pi_desc_page) {
7532 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007533 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08007534 vmx->nested.pi_desc_page = NULL;
7535 vmx->nested.pi_desc = NULL;
7536 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007537
Jim Mattsonde3a0022017-11-27 17:22:25 -06007538 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007539}
7540
7541/* Emulate the VMXOFF instruction */
7542static int handle_vmoff(struct kvm_vcpu *vcpu)
7543{
7544 if (!nested_vmx_check_permission(vcpu))
7545 return 1;
7546 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007547 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007548 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007549}
7550
Nadav Har'El27d6c862011-05-25 23:06:59 +03007551/* Emulate the VMCLEAR instruction */
7552static int handle_vmclear(struct kvm_vcpu *vcpu)
7553{
7554 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08007555 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007556 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007557
7558 if (!nested_vmx_check_permission(vcpu))
7559 return 1;
7560
Radim Krčmářcbf71272017-05-19 15:48:51 +02007561 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007562 return 1;
7563
Radim Krčmářcbf71272017-05-19 15:48:51 +02007564 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7565 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7566 return kvm_skip_emulated_instruction(vcpu);
7567 }
7568
7569 if (vmptr == vmx->nested.vmxon_ptr) {
7570 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7571 return kvm_skip_emulated_instruction(vcpu);
7572 }
7573
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007574 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007575 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007576
Jim Mattson587d7e722017-03-02 12:41:48 -08007577 kvm_vcpu_write_guest(vcpu,
7578 vmptr + offsetof(struct vmcs12, launch_state),
7579 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007580
Nadav Har'El27d6c862011-05-25 23:06:59 +03007581 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007582 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007583}
7584
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007585static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7586
7587/* Emulate the VMLAUNCH instruction */
7588static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7589{
7590 return nested_vmx_run(vcpu, true);
7591}
7592
7593/* Emulate the VMRESUME instruction */
7594static int handle_vmresume(struct kvm_vcpu *vcpu)
7595{
7596
7597 return nested_vmx_run(vcpu, false);
7598}
7599
Nadav Har'El49f705c2011-05-25 23:08:30 +03007600/*
7601 * Read a vmcs12 field. Since these can have varying lengths and we return
7602 * one type, we chose the biggest type (u64) and zero-extend the return value
7603 * to that size. Note that the caller, handle_vmread, might need to use only
7604 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7605 * 64-bit fields are to be returned).
7606 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007607static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7608 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007609{
7610 short offset = vmcs_field_to_offset(field);
7611 char *p;
7612
7613 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007614 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007615
7616 p = ((char *)(get_vmcs12(vcpu))) + offset;
7617
Jim Mattsond37f4262017-12-22 12:12:16 -08007618 switch (vmcs_field_width(field)) {
7619 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007620 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007621 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007622 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007623 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007624 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007625 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007626 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007627 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007628 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007629 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007630 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007631 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007632 WARN_ON(1);
7633 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007634 }
7635}
7636
Abel Gordon20b97fe2013-04-18 14:36:25 +03007637
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007638static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7639 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007640 short offset = vmcs_field_to_offset(field);
7641 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7642 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007643 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007644
Jim Mattsond37f4262017-12-22 12:12:16 -08007645 switch (vmcs_field_width(field)) {
7646 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007647 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007648 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007649 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007650 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007651 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007652 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007653 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007654 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007655 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007656 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007657 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007658 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007659 WARN_ON(1);
7660 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007661 }
7662
7663}
7664
Abel Gordon16f5b902013-04-18 14:38:25 +03007665static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7666{
7667 int i;
7668 unsigned long field;
7669 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007670 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007671 const u16 *fields = shadow_read_write_fields;
Mathias Krausec2bae892013-06-26 20:36:21 +02007672 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007673
Jan Kiszka282da872014-10-08 18:05:39 +02007674 preempt_disable();
7675
Abel Gordon16f5b902013-04-18 14:38:25 +03007676 vmcs_load(shadow_vmcs);
7677
7678 for (i = 0; i < num_fields; i++) {
7679 field = fields[i];
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007680 field_value = __vmcs_readl(field);
Abel Gordon16f5b902013-04-18 14:38:25 +03007681 vmcs12_write_any(&vmx->vcpu, field, field_value);
7682 }
7683
7684 vmcs_clear(shadow_vmcs);
7685 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007686
7687 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007688}
7689
Abel Gordonc3114422013-04-18 14:38:55 +03007690static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7691{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007692 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02007693 shadow_read_write_fields,
7694 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007695 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007696 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007697 max_shadow_read_write_fields,
7698 max_shadow_read_only_fields
7699 };
7700 int i, q;
7701 unsigned long field;
7702 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007703 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007704
7705 vmcs_load(shadow_vmcs);
7706
Mathias Krausec2bae892013-06-26 20:36:21 +02007707 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007708 for (i = 0; i < max_fields[q]; i++) {
7709 field = fields[q][i];
7710 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007711 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03007712 }
7713 }
7714
7715 vmcs_clear(shadow_vmcs);
7716 vmcs_load(vmx->loaded_vmcs->vmcs);
7717}
7718
Nadav Har'El49f705c2011-05-25 23:08:30 +03007719/*
7720 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7721 * used before) all generate the same failure when it is missing.
7722 */
7723static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7724{
7725 struct vcpu_vmx *vmx = to_vmx(vcpu);
7726 if (vmx->nested.current_vmptr == -1ull) {
7727 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007728 return 0;
7729 }
7730 return 1;
7731}
7732
7733static int handle_vmread(struct kvm_vcpu *vcpu)
7734{
7735 unsigned long field;
7736 u64 field_value;
7737 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7738 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7739 gva_t gva = 0;
7740
Kyle Hueyeb277562016-11-29 12:40:39 -08007741 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007742 return 1;
7743
Kyle Huey6affcbe2016-11-29 12:40:40 -08007744 if (!nested_vmx_check_vmcs12(vcpu))
7745 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007746
Nadav Har'El49f705c2011-05-25 23:08:30 +03007747 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007748 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007749 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007750 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007751 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007752 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007753 }
7754 /*
7755 * Now copy part of this value to register or memory, as requested.
7756 * Note that the number of bits actually copied is 32 or 64 depending
7757 * on the guest's mode (32 or 64 bit), not on the given field's length.
7758 */
7759 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007760 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007761 field_value);
7762 } else {
7763 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007764 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007765 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07007766 /* _system ok, as hardware has verified cpl=0 */
Nadav Har'El49f705c2011-05-25 23:08:30 +03007767 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7768 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7769 }
7770
7771 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007772 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007773}
7774
7775
7776static int handle_vmwrite(struct kvm_vcpu *vcpu)
7777{
7778 unsigned long field;
7779 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007780 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007781 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7782 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007783
Nadav Har'El49f705c2011-05-25 23:08:30 +03007784 /* The value to write might be 32 or 64 bits, depending on L1's long
7785 * mode, and eventually we need to write that into a field of several
7786 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007787 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007788 * bits into the vmcs12 field.
7789 */
7790 u64 field_value = 0;
7791 struct x86_exception e;
7792
Kyle Hueyeb277562016-11-29 12:40:39 -08007793 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007794 return 1;
7795
Kyle Huey6affcbe2016-11-29 12:40:40 -08007796 if (!nested_vmx_check_vmcs12(vcpu))
7797 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007798
Nadav Har'El49f705c2011-05-25 23:08:30 +03007799 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007800 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007801 (((vmx_instruction_info) >> 3) & 0xf));
7802 else {
7803 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007804 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007805 return 1;
7806 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007807 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007808 kvm_inject_page_fault(vcpu, &e);
7809 return 1;
7810 }
7811 }
7812
7813
Nadav Amit27e6fb52014-06-18 17:19:26 +03007814 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007815 if (vmcs_field_readonly(field)) {
7816 nested_vmx_failValid(vcpu,
7817 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007818 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007819 }
7820
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007821 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007822 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007823 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007824 }
7825
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007826 switch (field) {
7827#define SHADOW_FIELD_RW(x) case x:
7828#include "vmx_shadow_fields.h"
7829 /*
7830 * The fields that can be updated by L1 without a vmexit are
7831 * always updated in the vmcs02, the others go down the slow
7832 * path of prepare_vmcs02.
7833 */
7834 break;
7835 default:
7836 vmx->nested.dirty_vmcs12 = true;
7837 break;
7838 }
7839
Nadav Har'El49f705c2011-05-25 23:08:30 +03007840 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007841 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007842}
7843
Jim Mattsona8bc2842016-11-30 12:03:44 -08007844static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
7845{
7846 vmx->nested.current_vmptr = vmptr;
7847 if (enable_shadow_vmcs) {
7848 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7849 SECONDARY_EXEC_SHADOW_VMCS);
7850 vmcs_write64(VMCS_LINK_POINTER,
7851 __pa(vmx->vmcs01.shadow_vmcs));
7852 vmx->nested.sync_shadow_vmcs = true;
7853 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007854 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08007855}
7856
Nadav Har'El63846662011-05-25 23:07:29 +03007857/* Emulate the VMPTRLD instruction */
7858static int handle_vmptrld(struct kvm_vcpu *vcpu)
7859{
7860 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007861 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007862
7863 if (!nested_vmx_check_permission(vcpu))
7864 return 1;
7865
Radim Krčmářcbf71272017-05-19 15:48:51 +02007866 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007867 return 1;
7868
Radim Krčmářcbf71272017-05-19 15:48:51 +02007869 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7870 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
7871 return kvm_skip_emulated_instruction(vcpu);
7872 }
7873
7874 if (vmptr == vmx->nested.vmxon_ptr) {
7875 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
7876 return kvm_skip_emulated_instruction(vcpu);
7877 }
7878
Nadav Har'El63846662011-05-25 23:07:29 +03007879 if (vmx->nested.current_vmptr != vmptr) {
7880 struct vmcs12 *new_vmcs12;
7881 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007882 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7883 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03007884 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007885 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007886 }
7887 new_vmcs12 = kmap(page);
7888 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7889 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007890 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03007891 nested_vmx_failValid(vcpu,
7892 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007893 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007894 }
Nadav Har'El63846662011-05-25 23:07:29 +03007895
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007896 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07007897 /*
7898 * Load VMCS12 from guest memory since it is not already
7899 * cached.
7900 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007901 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
7902 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007903 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007904
Jim Mattsona8bc2842016-11-30 12:03:44 -08007905 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03007906 }
7907
7908 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007909 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007910}
7911
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007912/* Emulate the VMPTRST instruction */
7913static int handle_vmptrst(struct kvm_vcpu *vcpu)
7914{
7915 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7916 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7917 gva_t vmcs_gva;
7918 struct x86_exception e;
7919
7920 if (!nested_vmx_check_permission(vcpu))
7921 return 1;
7922
7923 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007924 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007925 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07007926 /* ok to use *_system, as hardware has verified cpl=0 */
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007927 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7928 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7929 sizeof(u64), &e)) {
7930 kvm_inject_page_fault(vcpu, &e);
7931 return 1;
7932 }
7933 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007934 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007935}
7936
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007937/* Emulate the INVEPT instruction */
7938static int handle_invept(struct kvm_vcpu *vcpu)
7939{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007940 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007941 u32 vmx_instruction_info, types;
7942 unsigned long type;
7943 gva_t gva;
7944 struct x86_exception e;
7945 struct {
7946 u64 eptp, gpa;
7947 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007948
Wincy Vanb9c237b2015-02-03 23:56:30 +08007949 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7950 SECONDARY_EXEC_ENABLE_EPT) ||
7951 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007952 kvm_queue_exception(vcpu, UD_VECTOR);
7953 return 1;
7954 }
7955
7956 if (!nested_vmx_check_permission(vcpu))
7957 return 1;
7958
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007959 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007960 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007961
Wincy Vanb9c237b2015-02-03 23:56:30 +08007962 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007963
Jim Mattson85c856b2016-10-26 08:38:38 -07007964 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007965 nested_vmx_failValid(vcpu,
7966 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007967 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007968 }
7969
7970 /* According to the Intel VMX instruction reference, the memory
7971 * operand is read even if it isn't needed (e.g., for type==global)
7972 */
7973 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007974 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007975 return 1;
7976 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7977 sizeof(operand), &e)) {
7978 kvm_inject_page_fault(vcpu, &e);
7979 return 1;
7980 }
7981
7982 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007983 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007984 /*
7985 * TODO: track mappings and invalidate
7986 * single context requests appropriately
7987 */
7988 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007989 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007990 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007991 nested_vmx_succeed(vcpu);
7992 break;
7993 default:
7994 BUG_ON(1);
7995 break;
7996 }
7997
Kyle Huey6affcbe2016-11-29 12:40:40 -08007998 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007999}
8000
Petr Matouseka642fc32014-09-23 20:22:30 +02008001static int handle_invvpid(struct kvm_vcpu *vcpu)
8002{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008003 struct vcpu_vmx *vmx = to_vmx(vcpu);
8004 u32 vmx_instruction_info;
8005 unsigned long type, types;
8006 gva_t gva;
8007 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008008 struct {
8009 u64 vpid;
8010 u64 gla;
8011 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008012
8013 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
8014 SECONDARY_EXEC_ENABLE_VPID) ||
8015 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
8016 kvm_queue_exception(vcpu, UD_VECTOR);
8017 return 1;
8018 }
8019
8020 if (!nested_vmx_check_permission(vcpu))
8021 return 1;
8022
8023 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8024 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8025
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008026 types = (vmx->nested.nested_vmx_vpid_caps &
8027 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008028
Jim Mattson85c856b2016-10-26 08:38:38 -07008029 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008030 nested_vmx_failValid(vcpu,
8031 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008032 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008033 }
8034
8035 /* according to the intel vmx instruction reference, the memory
8036 * operand is read even if it isn't needed (e.g., for type==global)
8037 */
8038 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8039 vmx_instruction_info, false, &gva))
8040 return 1;
Jim Mattson40352602017-06-28 09:37:37 -07008041 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8042 sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008043 kvm_inject_page_fault(vcpu, &e);
8044 return 1;
8045 }
Jim Mattson40352602017-06-28 09:37:37 -07008046 if (operand.vpid >> 16) {
8047 nested_vmx_failValid(vcpu,
8048 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8049 return kvm_skip_emulated_instruction(vcpu);
8050 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008051
8052 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008053 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Yu Zhangfd8cb432017-08-24 20:27:56 +08008054 if (is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008055 nested_vmx_failValid(vcpu,
8056 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8057 return kvm_skip_emulated_instruction(vcpu);
8058 }
8059 /* fall through */
Paolo Bonzinief697a72016-03-18 16:58:38 +01008060 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008061 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008062 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008063 nested_vmx_failValid(vcpu,
8064 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008065 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008066 }
8067 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008068 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008069 break;
8070 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008071 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008072 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008073 }
8074
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008075 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008076 nested_vmx_succeed(vcpu);
8077
Kyle Huey6affcbe2016-11-29 12:40:40 -08008078 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008079}
8080
Kai Huang843e4332015-01-28 10:54:28 +08008081static int handle_pml_full(struct kvm_vcpu *vcpu)
8082{
8083 unsigned long exit_qualification;
8084
8085 trace_kvm_pml_full(vcpu->vcpu_id);
8086
8087 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8088
8089 /*
8090 * PML buffer FULL happened while executing iret from NMI,
8091 * "blocked by NMI" bit has to be set before next VM entry.
8092 */
8093 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008094 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008095 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8096 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8097 GUEST_INTR_STATE_NMI);
8098
8099 /*
8100 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8101 * here.., and there's no userspace involvement needed for PML.
8102 */
8103 return 1;
8104}
8105
Yunhong Jiang64672c92016-06-13 14:19:59 -07008106static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8107{
8108 kvm_lapic_expired_hv_timer(vcpu);
8109 return 1;
8110}
8111
Bandan Das41ab9372017-08-03 15:54:43 -04008112static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8113{
8114 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008115 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8116
8117 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008118 switch (address & VMX_EPTP_MT_MASK) {
8119 case VMX_EPTP_MT_UC:
Bandan Das41ab9372017-08-03 15:54:43 -04008120 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_UC_BIT))
8121 return false;
8122 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008123 case VMX_EPTP_MT_WB:
Bandan Das41ab9372017-08-03 15:54:43 -04008124 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_WB_BIT))
8125 return false;
8126 break;
8127 default:
8128 return false;
8129 }
8130
David Hildenbrandbb97a012017-08-10 23:15:28 +02008131 /* only 4 levels page-walk length are valid */
8132 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008133 return false;
8134
8135 /* Reserved bits should not be set */
8136 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8137 return false;
8138
8139 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008140 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Bandan Das41ab9372017-08-03 15:54:43 -04008141 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPT_AD_BIT))
8142 return false;
8143 }
8144
8145 return true;
8146}
8147
8148static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8149 struct vmcs12 *vmcs12)
8150{
8151 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8152 u64 address;
8153 bool accessed_dirty;
8154 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8155
8156 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8157 !nested_cpu_has_ept(vmcs12))
8158 return 1;
8159
8160 if (index >= VMFUNC_EPTP_ENTRIES)
8161 return 1;
8162
8163
8164 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8165 &address, index * 8, 8))
8166 return 1;
8167
David Hildenbrandbb97a012017-08-10 23:15:28 +02008168 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008169
8170 /*
8171 * If the (L2) guest does a vmfunc to the currently
8172 * active ept pointer, we don't have to do anything else
8173 */
8174 if (vmcs12->ept_pointer != address) {
8175 if (!valid_ept_address(vcpu, address))
8176 return 1;
8177
8178 kvm_mmu_unload(vcpu);
8179 mmu->ept_ad = accessed_dirty;
8180 mmu->base_role.ad_disabled = !accessed_dirty;
8181 vmcs12->ept_pointer = address;
8182 /*
8183 * TODO: Check what's the correct approach in case
8184 * mmu reload fails. Currently, we just let the next
8185 * reload potentially fail
8186 */
8187 kvm_mmu_reload(vcpu);
8188 }
8189
8190 return 0;
8191}
8192
Bandan Das2a499e42017-08-03 15:54:41 -04008193static int handle_vmfunc(struct kvm_vcpu *vcpu)
8194{
Bandan Das27c42a12017-08-03 15:54:42 -04008195 struct vcpu_vmx *vmx = to_vmx(vcpu);
8196 struct vmcs12 *vmcs12;
8197 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8198
8199 /*
8200 * VMFUNC is only supported for nested guests, but we always enable the
8201 * secondary control for simplicity; for non-nested mode, fake that we
8202 * didn't by injecting #UD.
8203 */
8204 if (!is_guest_mode(vcpu)) {
8205 kvm_queue_exception(vcpu, UD_VECTOR);
8206 return 1;
8207 }
8208
8209 vmcs12 = get_vmcs12(vcpu);
8210 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8211 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008212
8213 switch (function) {
8214 case 0:
8215 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8216 goto fail;
8217 break;
8218 default:
8219 goto fail;
8220 }
8221 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008222
8223fail:
8224 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8225 vmcs_read32(VM_EXIT_INTR_INFO),
8226 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008227 return 1;
8228}
8229
Nadav Har'El0140cae2011-05-25 23:06:28 +03008230/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008231 * The exit handlers return 1 if the exit was handled fully and guest execution
8232 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8233 * to be done to userspace and return 0.
8234 */
Mathias Krause772e0312012-08-30 01:30:19 +02008235static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008236 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8237 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008238 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008239 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008240 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008241 [EXIT_REASON_CR_ACCESS] = handle_cr,
8242 [EXIT_REASON_DR_ACCESS] = handle_dr,
8243 [EXIT_REASON_CPUID] = handle_cpuid,
8244 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8245 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8246 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8247 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008248 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008249 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008250 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008251 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008252 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008253 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008254 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008255 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008256 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008257 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008258 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008259 [EXIT_REASON_VMOFF] = handle_vmoff,
8260 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008261 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8262 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008263 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008264 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008265 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008266 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008267 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008268 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008269 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8270 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008271 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8272 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008273 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008274 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008275 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008276 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008277 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008278 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008279 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008280 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008281 [EXIT_REASON_XSAVES] = handle_xsaves,
8282 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008283 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008284 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008285 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008286};
8287
8288static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008289 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008290
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008291static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8292 struct vmcs12 *vmcs12)
8293{
8294 unsigned long exit_qualification;
8295 gpa_t bitmap, last_bitmap;
8296 unsigned int port;
8297 int size;
8298 u8 b;
8299
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008300 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008301 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008302
8303 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8304
8305 port = exit_qualification >> 16;
8306 size = (exit_qualification & 7) + 1;
8307
8308 last_bitmap = (gpa_t)-1;
8309 b = -1;
8310
8311 while (size > 0) {
8312 if (port < 0x8000)
8313 bitmap = vmcs12->io_bitmap_a;
8314 else if (port < 0x10000)
8315 bitmap = vmcs12->io_bitmap_b;
8316 else
Joe Perches1d804d02015-03-30 16:46:09 -07008317 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008318 bitmap += (port & 0x7fff) / 8;
8319
8320 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008321 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008322 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008323 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008324 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008325
8326 port++;
8327 size--;
8328 last_bitmap = bitmap;
8329 }
8330
Joe Perches1d804d02015-03-30 16:46:09 -07008331 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008332}
8333
Nadav Har'El644d7112011-05-25 23:12:35 +03008334/*
8335 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8336 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8337 * disinterest in the current event (read or write a specific MSR) by using an
8338 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8339 */
8340static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8341 struct vmcs12 *vmcs12, u32 exit_reason)
8342{
8343 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8344 gpa_t bitmap;
8345
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008346 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008347 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008348
8349 /*
8350 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8351 * for the four combinations of read/write and low/high MSR numbers.
8352 * First we need to figure out which of the four to use:
8353 */
8354 bitmap = vmcs12->msr_bitmap;
8355 if (exit_reason == EXIT_REASON_MSR_WRITE)
8356 bitmap += 2048;
8357 if (msr_index >= 0xc0000000) {
8358 msr_index -= 0xc0000000;
8359 bitmap += 1024;
8360 }
8361
8362 /* Then read the msr_index'th bit from this bitmap: */
8363 if (msr_index < 1024*8) {
8364 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008365 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008366 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008367 return 1 & (b >> (msr_index & 7));
8368 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008369 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008370}
8371
8372/*
8373 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8374 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8375 * intercept (via guest_host_mask etc.) the current event.
8376 */
8377static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8378 struct vmcs12 *vmcs12)
8379{
8380 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8381 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008382 int reg;
8383 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008384
8385 switch ((exit_qualification >> 4) & 3) {
8386 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008387 reg = (exit_qualification >> 8) & 15;
8388 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008389 switch (cr) {
8390 case 0:
8391 if (vmcs12->cr0_guest_host_mask &
8392 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008393 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008394 break;
8395 case 3:
8396 if ((vmcs12->cr3_target_count >= 1 &&
8397 vmcs12->cr3_target_value0 == val) ||
8398 (vmcs12->cr3_target_count >= 2 &&
8399 vmcs12->cr3_target_value1 == val) ||
8400 (vmcs12->cr3_target_count >= 3 &&
8401 vmcs12->cr3_target_value2 == val) ||
8402 (vmcs12->cr3_target_count >= 4 &&
8403 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008404 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008405 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008406 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008407 break;
8408 case 4:
8409 if (vmcs12->cr4_guest_host_mask &
8410 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008411 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008412 break;
8413 case 8:
8414 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008415 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008416 break;
8417 }
8418 break;
8419 case 2: /* clts */
8420 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8421 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008422 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008423 break;
8424 case 1: /* mov from cr */
8425 switch (cr) {
8426 case 3:
8427 if (vmcs12->cpu_based_vm_exec_control &
8428 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008429 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008430 break;
8431 case 8:
8432 if (vmcs12->cpu_based_vm_exec_control &
8433 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008434 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008435 break;
8436 }
8437 break;
8438 case 3: /* lmsw */
8439 /*
8440 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8441 * cr0. Other attempted changes are ignored, with no exit.
8442 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008443 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008444 if (vmcs12->cr0_guest_host_mask & 0xe &
8445 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008446 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008447 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8448 !(vmcs12->cr0_read_shadow & 0x1) &&
8449 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008450 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008451 break;
8452 }
Joe Perches1d804d02015-03-30 16:46:09 -07008453 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008454}
8455
8456/*
8457 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8458 * should handle it ourselves in L0 (and then continue L2). Only call this
8459 * when in is_guest_mode (L2).
8460 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02008461static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03008462{
Nadav Har'El644d7112011-05-25 23:12:35 +03008463 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8464 struct vcpu_vmx *vmx = to_vmx(vcpu);
8465 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8466
Jim Mattson4f350c62017-09-14 16:31:44 -07008467 if (vmx->nested.nested_run_pending)
8468 return false;
8469
8470 if (unlikely(vmx->fail)) {
8471 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8472 vmcs_read32(VM_INSTRUCTION_ERROR));
8473 return true;
8474 }
Jan Kiszka542060e2014-01-04 18:47:21 +01008475
David Matlackc9f04402017-08-01 14:00:40 -07008476 /*
8477 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06008478 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8479 * Page). The CPU may write to these pages via their host
8480 * physical address while L2 is running, bypassing any
8481 * address-translation-based dirty tracking (e.g. EPT write
8482 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07008483 *
8484 * Mark them dirty on every exit from L2 to prevent them from
8485 * getting out of sync with dirty tracking.
8486 */
8487 nested_mark_vmcs12_pages_dirty(vcpu);
8488
Jim Mattson4f350c62017-09-14 16:31:44 -07008489 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8490 vmcs_readl(EXIT_QUALIFICATION),
8491 vmx->idt_vectoring_info,
8492 intr_info,
8493 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8494 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03008495
8496 switch (exit_reason) {
8497 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008498 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008499 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008500 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07008501 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008502 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008503 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008504 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008505 else if (is_debug(intr_info) &&
8506 vcpu->guest_debug &
8507 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8508 return false;
8509 else if (is_breakpoint(intr_info) &&
8510 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8511 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008512 return vmcs12->exception_bitmap &
8513 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8514 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008515 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008516 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008517 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008518 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008519 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008520 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008521 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008522 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008523 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008524 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008525 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008526 case EXIT_REASON_HLT:
8527 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8528 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008529 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008530 case EXIT_REASON_INVLPG:
8531 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8532 case EXIT_REASON_RDPMC:
8533 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008534 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008535 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008536 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008537 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008538 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008539 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8540 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8541 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8542 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8543 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8544 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008545 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008546 /*
8547 * VMX instructions trap unconditionally. This allows L1 to
8548 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8549 */
Joe Perches1d804d02015-03-30 16:46:09 -07008550 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008551 case EXIT_REASON_CR_ACCESS:
8552 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8553 case EXIT_REASON_DR_ACCESS:
8554 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8555 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008556 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008557 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8558 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03008559 case EXIT_REASON_MSR_READ:
8560 case EXIT_REASON_MSR_WRITE:
8561 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8562 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008563 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008564 case EXIT_REASON_MWAIT_INSTRUCTION:
8565 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008566 case EXIT_REASON_MONITOR_TRAP_FLAG:
8567 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008568 case EXIT_REASON_MONITOR_INSTRUCTION:
8569 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8570 case EXIT_REASON_PAUSE_INSTRUCTION:
8571 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8572 nested_cpu_has2(vmcs12,
8573 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8574 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008575 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008576 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008577 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008578 case EXIT_REASON_APIC_ACCESS:
8579 return nested_cpu_has2(vmcs12,
8580 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008581 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008582 case EXIT_REASON_EOI_INDUCED:
8583 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008584 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008585 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008586 /*
8587 * L0 always deals with the EPT violation. If nested EPT is
8588 * used, and the nested mmu code discovers that the address is
8589 * missing in the guest EPT table (EPT12), the EPT violation
8590 * will be injected with nested_ept_inject_page_fault()
8591 */
Joe Perches1d804d02015-03-30 16:46:09 -07008592 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008593 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008594 /*
8595 * L2 never uses directly L1's EPT, but rather L0's own EPT
8596 * table (shadow on EPT) or a merged EPT table that L0 built
8597 * (EPT on EPT). So any problems with the structure of the
8598 * table is L0's fault.
8599 */
Joe Perches1d804d02015-03-30 16:46:09 -07008600 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02008601 case EXIT_REASON_INVPCID:
8602 return
8603 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8604 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008605 case EXIT_REASON_WBINVD:
8606 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8607 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008608 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008609 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8610 /*
8611 * This should never happen, since it is not possible to
8612 * set XSS to a non-zero value---neither in L1 nor in L2.
8613 * If if it were, XSS would have to be checked against
8614 * the XSS exit bitmap in vmcs12.
8615 */
8616 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008617 case EXIT_REASON_PREEMPTION_TIMER:
8618 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02008619 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04008620 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02008621 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04008622 case EXIT_REASON_VMFUNC:
8623 /* VM functions are emulated through L2->L0 vmexits. */
8624 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008625 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008626 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008627 }
8628}
8629
Paolo Bonzini7313c692017-07-27 10:31:25 +02008630static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8631{
8632 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8633
8634 /*
8635 * At this point, the exit interruption info in exit_intr_info
8636 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8637 * we need to query the in-kernel LAPIC.
8638 */
8639 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8640 if ((exit_intr_info &
8641 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8642 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8643 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8644 vmcs12->vm_exit_intr_error_code =
8645 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8646 }
8647
8648 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8649 vmcs_readl(EXIT_QUALIFICATION));
8650 return 1;
8651}
8652
Avi Kivity586f9602010-11-18 13:09:54 +02008653static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8654{
8655 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8656 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8657}
8658
Kai Huanga3eaa862015-11-04 13:46:05 +08008659static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008660{
Kai Huanga3eaa862015-11-04 13:46:05 +08008661 if (vmx->pml_pg) {
8662 __free_page(vmx->pml_pg);
8663 vmx->pml_pg = NULL;
8664 }
Kai Huang843e4332015-01-28 10:54:28 +08008665}
8666
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008667static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008668{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008669 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008670 u64 *pml_buf;
8671 u16 pml_idx;
8672
8673 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8674
8675 /* Do nothing if PML buffer is empty */
8676 if (pml_idx == (PML_ENTITY_NUM - 1))
8677 return;
8678
8679 /* PML index always points to next available PML buffer entity */
8680 if (pml_idx >= PML_ENTITY_NUM)
8681 pml_idx = 0;
8682 else
8683 pml_idx++;
8684
8685 pml_buf = page_address(vmx->pml_pg);
8686 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8687 u64 gpa;
8688
8689 gpa = pml_buf[pml_idx];
8690 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008691 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008692 }
8693
8694 /* reset PML index */
8695 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8696}
8697
8698/*
8699 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8700 * Called before reporting dirty_bitmap to userspace.
8701 */
8702static void kvm_flush_pml_buffers(struct kvm *kvm)
8703{
8704 int i;
8705 struct kvm_vcpu *vcpu;
8706 /*
8707 * We only need to kick vcpu out of guest mode here, as PML buffer
8708 * is flushed at beginning of all VMEXITs, and it's obvious that only
8709 * vcpus running in guest are possible to have unflushed GPAs in PML
8710 * buffer.
8711 */
8712 kvm_for_each_vcpu(i, vcpu, kvm)
8713 kvm_vcpu_kick(vcpu);
8714}
8715
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008716static void vmx_dump_sel(char *name, uint32_t sel)
8717{
8718 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05008719 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008720 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8721 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8722 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8723}
8724
8725static void vmx_dump_dtsel(char *name, uint32_t limit)
8726{
8727 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8728 name, vmcs_read32(limit),
8729 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8730}
8731
8732static void dump_vmcs(void)
8733{
8734 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8735 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8736 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8737 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8738 u32 secondary_exec_control = 0;
8739 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008740 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008741 int i, n;
8742
8743 if (cpu_has_secondary_exec_ctrls())
8744 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8745
8746 pr_err("*** Guest State ***\n");
8747 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8748 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8749 vmcs_readl(CR0_GUEST_HOST_MASK));
8750 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8751 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8752 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8753 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8754 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8755 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008756 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8757 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8758 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8759 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008760 }
8761 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8762 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8763 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8764 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8765 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8766 vmcs_readl(GUEST_SYSENTER_ESP),
8767 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8768 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8769 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8770 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8771 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8772 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8773 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8774 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8775 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8776 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8777 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8778 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8779 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008780 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8781 efer, vmcs_read64(GUEST_IA32_PAT));
8782 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8783 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008784 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8785 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008786 pr_err("PerfGlobCtl = 0x%016llx\n",
8787 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008788 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008789 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008790 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8791 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8792 vmcs_read32(GUEST_ACTIVITY_STATE));
8793 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8794 pr_err("InterruptStatus = %04x\n",
8795 vmcs_read16(GUEST_INTR_STATUS));
8796
8797 pr_err("*** Host State ***\n");
8798 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8799 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8800 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8801 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8802 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8803 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8804 vmcs_read16(HOST_TR_SELECTOR));
8805 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8806 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8807 vmcs_readl(HOST_TR_BASE));
8808 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8809 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8810 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8811 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8812 vmcs_readl(HOST_CR4));
8813 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8814 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8815 vmcs_read32(HOST_IA32_SYSENTER_CS),
8816 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8817 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008818 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8819 vmcs_read64(HOST_IA32_EFER),
8820 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008821 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008822 pr_err("PerfGlobCtl = 0x%016llx\n",
8823 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008824
8825 pr_err("*** Control State ***\n");
8826 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8827 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8828 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8829 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8830 vmcs_read32(EXCEPTION_BITMAP),
8831 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8832 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8833 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8834 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8835 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8836 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8837 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8838 vmcs_read32(VM_EXIT_INTR_INFO),
8839 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8840 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8841 pr_err(" reason=%08x qualification=%016lx\n",
8842 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8843 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8844 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8845 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008846 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008847 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008848 pr_err("TSC Multiplier = 0x%016llx\n",
8849 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008850 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8851 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8852 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8853 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8854 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008855 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008856 n = vmcs_read32(CR3_TARGET_COUNT);
8857 for (i = 0; i + 1 < n; i += 4)
8858 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8859 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8860 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8861 if (i < n)
8862 pr_err("CR3 target%u=%016lx\n",
8863 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8864 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8865 pr_err("PLE Gap=%08x Window=%08x\n",
8866 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8867 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8868 pr_err("Virtual processor ID = 0x%04x\n",
8869 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8870}
8871
Avi Kivity6aa8b732006-12-10 02:21:36 -08008872/*
8873 * The guest has exited. See if we can fix it or if we need userspace
8874 * assistance.
8875 */
Avi Kivity851ba692009-08-24 11:10:17 +03008876static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008877{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008878 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008879 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008880 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008881
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008882 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8883
Kai Huang843e4332015-01-28 10:54:28 +08008884 /*
8885 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8886 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8887 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8888 * mode as if vcpus is in root mode, the PML buffer must has been
8889 * flushed already.
8890 */
8891 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008892 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008893
Mohammed Gamal80ced182009-09-01 12:48:18 +02008894 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008895 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008896 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008897
Paolo Bonzini7313c692017-07-27 10:31:25 +02008898 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
8899 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03008900
Mohammed Gamal51207022010-05-31 22:40:54 +03008901 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008902 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008903 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8904 vcpu->run->fail_entry.hardware_entry_failure_reason
8905 = exit_reason;
8906 return 0;
8907 }
8908
Avi Kivity29bd8a72007-09-10 17:27:03 +03008909 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008910 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8911 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008912 = vmcs_read32(VM_INSTRUCTION_ERROR);
8913 return 0;
8914 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008915
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008916 /*
8917 * Note:
8918 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8919 * delivery event since it indicates guest is accessing MMIO.
8920 * The vm-exit can be triggered again after return to guest that
8921 * will cause infinite loop.
8922 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008923 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008924 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008925 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008926 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008927 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8928 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8929 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02008930 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008931 vcpu->run->internal.data[0] = vectoring_info;
8932 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02008933 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
8934 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
8935 vcpu->run->internal.ndata++;
8936 vcpu->run->internal.data[3] =
8937 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
8938 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008939 return 0;
8940 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008941
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008942 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01008943 vmx->loaded_vmcs->soft_vnmi_blocked)) {
8944 if (vmx_interrupt_allowed(vcpu)) {
8945 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8946 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
8947 vcpu->arch.nmi_pending) {
8948 /*
8949 * This CPU don't support us in finding the end of an
8950 * NMI-blocked window if the guest runs with IRQs
8951 * disabled. So we pull the trigger after 1 s of
8952 * futile waiting, but inform the user about this.
8953 */
8954 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8955 "state on VCPU %d after 1 s timeout\n",
8956 __func__, vcpu->vcpu_id);
8957 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8958 }
8959 }
8960
Avi Kivity6aa8b732006-12-10 02:21:36 -08008961 if (exit_reason < kvm_vmx_max_exit_handlers
8962 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008963 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008964 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01008965 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
8966 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008967 kvm_queue_exception(vcpu, UD_VECTOR);
8968 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008969 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008970}
8971
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008972static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008973{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008974 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8975
8976 if (is_guest_mode(vcpu) &&
8977 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8978 return;
8979
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008980 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008981 vmcs_write32(TPR_THRESHOLD, 0);
8982 return;
8983 }
8984
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008985 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008986}
8987
Yang Zhang8d146952013-01-25 10:18:50 +08008988static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8989{
8990 u32 sec_exec_control;
8991
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008992 /* Postpone execution until vmcs01 is the current VMCS. */
8993 if (is_guest_mode(vcpu)) {
8994 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8995 return;
8996 }
8997
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008998 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008999 return;
9000
Paolo Bonzini35754c92015-07-29 12:05:37 +02009001 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009002 return;
9003
9004 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9005
9006 if (set) {
9007 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9008 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9009 } else {
9010 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9011 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009012 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009013 }
9014 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9015
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009016 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009017}
9018
Tang Chen38b99172014-09-24 15:57:54 +08009019static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9020{
9021 struct vcpu_vmx *vmx = to_vmx(vcpu);
9022
9023 /*
9024 * Currently we do not handle the nested case where L2 has an
9025 * APIC access page of its own; that page is still pinned.
9026 * Hence, we skip the case where the VCPU is in guest mode _and_
9027 * L1 prepared an APIC access page for L2.
9028 *
9029 * For the case where L1 and L2 share the same APIC access page
9030 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
9031 * in the vmcs12), this function will only update either the vmcs01
9032 * or the vmcs02. If the former, the vmcs02 will be updated by
9033 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
9034 * the next L2->L1 exit.
9035 */
9036 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07009037 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009038 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08009039 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009040 vmx_flush_tlb_ept_only(vcpu);
9041 }
Tang Chen38b99172014-09-24 15:57:54 +08009042}
9043
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009044static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009045{
9046 u16 status;
9047 u8 old;
9048
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009049 if (max_isr == -1)
9050 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009051
9052 status = vmcs_read16(GUEST_INTR_STATUS);
9053 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009054 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009055 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009056 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009057 vmcs_write16(GUEST_INTR_STATUS, status);
9058 }
9059}
9060
9061static void vmx_set_rvi(int vector)
9062{
9063 u16 status;
9064 u8 old;
9065
Wei Wang4114c272014-11-05 10:53:43 +08009066 if (vector == -1)
9067 vector = 0;
9068
Yang Zhangc7c9c562013-01-25 10:18:51 +08009069 status = vmcs_read16(GUEST_INTR_STATUS);
9070 old = (u8)status & 0xff;
9071 if ((u8)vector != old) {
9072 status &= ~0xff;
9073 status |= (u8)vector;
9074 vmcs_write16(GUEST_INTR_STATUS, status);
9075 }
9076}
9077
9078static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9079{
Liran Alon851c1a182017-12-24 18:12:56 +02009080 /*
9081 * When running L2, updating RVI is only relevant when
9082 * vmcs12 virtual-interrupt-delivery enabled.
9083 * However, it can be enabled only when L1 also
9084 * intercepts external-interrupts and in that case
9085 * we should not update vmcs02 RVI but instead intercept
9086 * interrupt. Therefore, do nothing when running L2.
9087 */
9088 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009089 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009090}
9091
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009092static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009093{
9094 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009095 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009096 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009097
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009098 WARN_ON(!vcpu->arch.apicv_active);
9099 if (pi_test_on(&vmx->pi_desc)) {
9100 pi_clear_on(&vmx->pi_desc);
9101 /*
9102 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9103 * But on x86 this is just a compiler barrier anyway.
9104 */
9105 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009106 max_irr_updated =
9107 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9108
9109 /*
9110 * If we are running L2 and L1 has a new pending interrupt
9111 * which can be injected, we should re-evaluate
9112 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009113 * If L1 intercepts external-interrupts, we should
9114 * exit from L2 to L1. Otherwise, interrupt should be
9115 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009116 */
Liran Alon851c1a182017-12-24 18:12:56 +02009117 if (is_guest_mode(vcpu) && max_irr_updated) {
9118 if (nested_exit_on_intr(vcpu))
9119 kvm_vcpu_exiting_guest_mode(vcpu);
9120 else
9121 kvm_make_request(KVM_REQ_EVENT, vcpu);
9122 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009123 } else {
9124 max_irr = kvm_lapic_find_highest_irr(vcpu);
9125 }
9126 vmx_hwapic_irr_update(vcpu, max_irr);
9127 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009128}
9129
Andrey Smetanin63086302015-11-10 15:36:32 +03009130static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009131{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009132 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009133 return;
9134
Yang Zhangc7c9c562013-01-25 10:18:51 +08009135 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9136 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9137 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9138 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9139}
9140
Paolo Bonzini967235d2016-12-19 14:03:45 +01009141static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9142{
9143 struct vcpu_vmx *vmx = to_vmx(vcpu);
9144
9145 pi_clear_on(&vmx->pi_desc);
9146 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9147}
9148
Avi Kivity51aa01d2010-07-20 14:31:20 +03009149static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009150{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009151 u32 exit_intr_info = 0;
9152 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009153
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009154 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9155 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009156 return;
9157
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009158 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9159 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9160 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009161
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009162 /* if exit due to PF check for async PF */
9163 if (is_page_fault(exit_intr_info))
9164 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9165
Andi Kleena0861c02009-06-08 17:37:09 +08009166 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009167 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9168 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009169 kvm_machine_check();
9170
Gleb Natapov20f65982009-05-11 13:35:55 +03009171 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009172 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009173 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009174 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009175 kvm_after_handle_nmi(&vmx->vcpu);
9176 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009177}
Gleb Natapov20f65982009-05-11 13:35:55 +03009178
Yang Zhanga547c6d2013-04-11 19:25:10 +08009179static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9180{
9181 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9182
Yang Zhanga547c6d2013-04-11 19:25:10 +08009183 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9184 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9185 unsigned int vector;
9186 unsigned long entry;
9187 gate_desc *desc;
9188 struct vcpu_vmx *vmx = to_vmx(vcpu);
9189#ifdef CONFIG_X86_64
9190 unsigned long tmp;
9191#endif
9192
9193 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9194 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009195 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009196 asm volatile(
9197#ifdef CONFIG_X86_64
9198 "mov %%" _ASM_SP ", %[sp]\n\t"
9199 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9200 "push $%c[ss]\n\t"
9201 "push %[sp]\n\t"
9202#endif
9203 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009204 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009205 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009206 :
9207#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009208 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009209#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009210 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009211 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009212 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009213 [ss]"i"(__KERNEL_DS),
9214 [cs]"i"(__KERNEL_CS)
9215 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009216 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009217}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009218STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009219
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009220static bool vmx_has_high_real_mode_segbase(void)
9221{
9222 return enable_unrestricted_guest || emulate_invalid_guest_state;
9223}
9224
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009225static bool vmx_mpx_supported(void)
9226{
9227 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9228 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9229}
9230
Wanpeng Li55412b22014-12-02 19:21:30 +08009231static bool vmx_xsaves_supported(void)
9232{
9233 return vmcs_config.cpu_based_2nd_exec_ctrl &
9234 SECONDARY_EXEC_XSAVES;
9235}
9236
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009237static bool vmx_umip_emulated(void)
9238{
Paolo Bonzini0367f202016-07-12 10:44:55 +02009239 return vmcs_config.cpu_based_2nd_exec_ctrl &
9240 SECONDARY_EXEC_DESC;
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009241}
9242
Avi Kivity51aa01d2010-07-20 14:31:20 +03009243static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9244{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009245 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009246 bool unblock_nmi;
9247 u8 vector;
9248 bool idtv_info_valid;
9249
9250 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009251
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009252 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009253 if (vmx->loaded_vmcs->nmi_known_unmasked)
9254 return;
9255 /*
9256 * Can't use vmx->exit_intr_info since we're not sure what
9257 * the exit reason is.
9258 */
9259 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9260 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9261 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9262 /*
9263 * SDM 3: 27.7.1.2 (September 2008)
9264 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9265 * a guest IRET fault.
9266 * SDM 3: 23.2.2 (September 2008)
9267 * Bit 12 is undefined in any of the following cases:
9268 * If the VM exit sets the valid bit in the IDT-vectoring
9269 * information field.
9270 * If the VM exit is due to a double fault.
9271 */
9272 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9273 vector != DF_VECTOR && !idtv_info_valid)
9274 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9275 GUEST_INTR_STATE_NMI);
9276 else
9277 vmx->loaded_vmcs->nmi_known_unmasked =
9278 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9279 & GUEST_INTR_STATE_NMI);
9280 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9281 vmx->loaded_vmcs->vnmi_blocked_time +=
9282 ktime_to_ns(ktime_sub(ktime_get(),
9283 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009284}
9285
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009286static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009287 u32 idt_vectoring_info,
9288 int instr_len_field,
9289 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009290{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009291 u8 vector;
9292 int type;
9293 bool idtv_info_valid;
9294
9295 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009296
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009297 vcpu->arch.nmi_injected = false;
9298 kvm_clear_exception_queue(vcpu);
9299 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009300
9301 if (!idtv_info_valid)
9302 return;
9303
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009304 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009305
Avi Kivity668f6122008-07-02 09:28:55 +03009306 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9307 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009308
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009309 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009310 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009311 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009312 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009313 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009314 * Clear bit "block by NMI" before VM entry if a NMI
9315 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009316 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009317 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009318 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009319 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009320 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009321 /* fall through */
9322 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009323 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009324 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009325 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009326 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009327 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009328 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009329 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009330 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009331 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009332 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009333 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009334 break;
9335 default:
9336 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009337 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009338}
9339
Avi Kivity83422e12010-07-20 14:43:23 +03009340static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9341{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009342 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009343 VM_EXIT_INSTRUCTION_LEN,
9344 IDT_VECTORING_ERROR_CODE);
9345}
9346
Avi Kivityb463a6f2010-07-20 15:06:17 +03009347static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9348{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009349 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009350 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9351 VM_ENTRY_INSTRUCTION_LEN,
9352 VM_ENTRY_EXCEPTION_ERROR_CODE);
9353
9354 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9355}
9356
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009357static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9358{
9359 int i, nr_msrs;
9360 struct perf_guest_switch_msr *msrs;
9361
9362 msrs = perf_guest_get_msrs(&nr_msrs);
9363
9364 if (!msrs)
9365 return;
9366
9367 for (i = 0; i < nr_msrs; i++)
9368 if (msrs[i].host == msrs[i].guest)
9369 clear_atomic_switch_msr(vmx, msrs[i].msr);
9370 else
9371 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9372 msrs[i].host);
9373}
9374
Jiang Biao33365e72016-11-03 15:03:37 +08009375static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009376{
9377 struct vcpu_vmx *vmx = to_vmx(vcpu);
9378 u64 tscl;
9379 u32 delta_tsc;
9380
9381 if (vmx->hv_deadline_tsc == -1)
9382 return;
9383
9384 tscl = rdtsc();
9385 if (vmx->hv_deadline_tsc > tscl)
9386 /* sure to be 32 bit only because checked on set_hv_timer */
9387 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9388 cpu_preemption_timer_multi);
9389 else
9390 delta_tsc = 0;
9391
9392 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9393}
9394
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009395static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009396{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009397 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Li74c55932017-11-29 01:31:20 -08009398 unsigned long cr3, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02009399
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009400 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009401 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009402 vmx->loaded_vmcs->soft_vnmi_blocked))
9403 vmx->loaded_vmcs->entry_time = ktime_get();
9404
Avi Kivity104f2262010-11-18 13:12:52 +02009405 /* Don't enter VMX if guest state is invalid, let the exit handler
9406 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009407 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009408 return;
9409
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009410 if (vmx->ple_window_dirty) {
9411 vmx->ple_window_dirty = false;
9412 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9413 }
9414
Abel Gordon012f83c2013-04-18 14:39:25 +03009415 if (vmx->nested.sync_shadow_vmcs) {
9416 copy_vmcs12_to_shadow(vmx);
9417 vmx->nested.sync_shadow_vmcs = false;
9418 }
9419
Avi Kivity104f2262010-11-18 13:12:52 +02009420 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9421 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9422 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9423 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9424
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009425 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +02009426 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009427 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +02009428 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009429 }
9430
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009431 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +02009432 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009433 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +02009434 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009435 }
9436
Avi Kivity104f2262010-11-18 13:12:52 +02009437 /* When single-stepping over STI and MOV SS, we must clear the
9438 * corresponding interruptibility bits in the guest state. Otherwise
9439 * vmentry fails as it then expects bit 14 (BS) in pending debug
9440 * exceptions being set, but that's not correct for the guest debugging
9441 * case. */
9442 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9443 vmx_set_interrupt_shadow(vcpu, 0);
9444
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009445 if (static_cpu_has(X86_FEATURE_PKU) &&
9446 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9447 vcpu->arch.pkru != vmx->host_pkru)
9448 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009449
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009450 atomic_switch_perf_msrs(vmx);
9451
Yunhong Jiang64672c92016-06-13 14:19:59 -07009452 vmx_arm_hv_timer(vcpu);
9453
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009454 /*
9455 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9456 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9457 * is no need to worry about the conditional branch over the wrmsr
9458 * being speculatively taken.
9459 */
9460 if (vmx->spec_ctrl)
9461 wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
9462
Nadav Har'Eld462b812011-05-24 15:26:10 +03009463 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02009464 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009465 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009466 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9467 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9468 "push %%" _ASM_CX " \n\t"
9469 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009470 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009471 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009472 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009473 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009474 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009475 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9476 "mov %%cr2, %%" _ASM_DX " \n\t"
9477 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009478 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009479 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009480 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009481 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009482 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009483 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009484 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9485 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9486 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9487 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9488 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9489 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009490#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009491 "mov %c[r8](%0), %%r8 \n\t"
9492 "mov %c[r9](%0), %%r9 \n\t"
9493 "mov %c[r10](%0), %%r10 \n\t"
9494 "mov %c[r11](%0), %%r11 \n\t"
9495 "mov %c[r12](%0), %%r12 \n\t"
9496 "mov %c[r13](%0), %%r13 \n\t"
9497 "mov %c[r14](%0), %%r14 \n\t"
9498 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009499#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009500 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009501
Avi Kivity6aa8b732006-12-10 02:21:36 -08009502 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009503 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009504 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009505 "jmp 2f \n\t"
9506 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9507 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009508 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009509 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009510 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009511 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009512 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9513 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9514 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9515 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9516 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9517 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9518 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009519#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009520 "mov %%r8, %c[r8](%0) \n\t"
9521 "mov %%r9, %c[r9](%0) \n\t"
9522 "mov %%r10, %c[r10](%0) \n\t"
9523 "mov %%r11, %c[r11](%0) \n\t"
9524 "mov %%r12, %c[r12](%0) \n\t"
9525 "mov %%r13, %c[r13](%0) \n\t"
9526 "mov %%r14, %c[r14](%0) \n\t"
9527 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009528 "xor %%r8d, %%r8d \n\t"
9529 "xor %%r9d, %%r9d \n\t"
9530 "xor %%r10d, %%r10d \n\t"
9531 "xor %%r11d, %%r11d \n\t"
9532 "xor %%r12d, %%r12d \n\t"
9533 "xor %%r13d, %%r13d \n\t"
9534 "xor %%r14d, %%r14d \n\t"
9535 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009536#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009537 "mov %%cr2, %%" _ASM_AX " \n\t"
9538 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009539
Jim Mattson0cb5b302018-01-03 14:31:38 -08009540 "xor %%eax, %%eax \n\t"
9541 "xor %%ebx, %%ebx \n\t"
9542 "xor %%esi, %%esi \n\t"
9543 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009544 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009545 ".pushsection .rodata \n\t"
9546 ".global vmx_return \n\t"
9547 "vmx_return: " _ASM_PTR " 2b \n\t"
9548 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009549 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009550 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009551 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03009552 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009553 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9554 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9555 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9556 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9557 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9558 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9559 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009560#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009561 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9562 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9563 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9564 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9565 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9566 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9567 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9568 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009569#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009570 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9571 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009572 : "cc", "memory"
9573#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009574 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009575 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009576#else
9577 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009578#endif
9579 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009580
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009581 /*
9582 * We do not use IBRS in the kernel. If this vCPU has used the
9583 * SPEC_CTRL MSR it may have left it on; save the value and
9584 * turn it off. This is much more efficient than blindly adding
9585 * it to the atomic save/restore list. Especially as the former
9586 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9587 *
9588 * For non-nested case:
9589 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9590 * save it.
9591 *
9592 * For nested case:
9593 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9594 * save it.
9595 */
9596 if (!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))
9597 rdmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
9598
9599 if (vmx->spec_ctrl)
9600 wrmsrl(MSR_IA32_SPEC_CTRL, 0);
9601
David Woodhouse117cc7a2018-01-12 11:11:27 +00009602 /* Eliminate branch target predictions from guest mode */
9603 vmexit_fill_RSB();
9604
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009605 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08009606 if (vmx->host_debugctlmsr)
9607 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009608
Avi Kivityaa67f602012-08-01 16:48:03 +03009609#ifndef CONFIG_X86_64
9610 /*
9611 * The sysexit path does not restore ds/es, so we must set them to
9612 * a reasonable value ourselves.
9613 *
9614 * We can't defer this to vmx_load_host_state() since that function
9615 * may be executed in interrupt context, which saves and restore segments
9616 * around it, nullifying its effect.
9617 */
9618 loadsegment(ds, __USER_DS);
9619 loadsegment(es, __USER_DS);
9620#endif
9621
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009622 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009623 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009624 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009625 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009626 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009627 vcpu->arch.regs_dirty = 0;
9628
Gleb Natapove0b890d2013-09-25 12:51:33 +03009629 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009630 * eager fpu is enabled if PKEY is supported and CR4 is switched
9631 * back on host, so it is safe to read guest PKRU from current
9632 * XSAVE.
9633 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009634 if (static_cpu_has(X86_FEATURE_PKU) &&
9635 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9636 vcpu->arch.pkru = __read_pkru();
9637 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009638 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009639 }
9640
9641 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009642 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9643 * we did not inject a still-pending event to L1 now because of
9644 * nested_run_pending, we need to re-enable this bit.
9645 */
9646 if (vmx->nested.nested_run_pending)
9647 kvm_make_request(KVM_REQ_EVENT, vcpu);
9648
9649 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07009650 vmx->idt_vectoring_info = 0;
9651
9652 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9653 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9654 return;
9655
9656 vmx->loaded_vmcs->launched = 1;
9657 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03009658
Avi Kivity51aa01d2010-07-20 14:31:20 +03009659 vmx_complete_atomic_exit(vmx);
9660 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009661 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009662}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009663STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009664
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009665static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009666{
9667 struct vcpu_vmx *vmx = to_vmx(vcpu);
9668 int cpu;
9669
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009670 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009671 return;
9672
9673 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009674 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009675 vmx_vcpu_put(vcpu);
9676 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009677 put_cpu();
9678}
9679
Jim Mattson2f1fe812016-07-08 15:36:06 -07009680/*
9681 * Ensure that the current vmcs of the logical processor is the
9682 * vmcs01 of the vcpu before calling free_nested().
9683 */
9684static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9685{
9686 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009687
Christoffer Dallec7660c2017-12-04 21:35:23 +01009688 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009689 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009690 free_nested(vmx);
9691 vcpu_put(vcpu);
9692}
9693
Avi Kivity6aa8b732006-12-10 02:21:36 -08009694static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9695{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009696 struct vcpu_vmx *vmx = to_vmx(vcpu);
9697
Kai Huang843e4332015-01-28 10:54:28 +08009698 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009699 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009700 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009701 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009702 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009703 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009704 kfree(vmx->guest_msrs);
9705 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009706 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009707}
9708
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009709static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009710{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009711 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009712 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009713 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03009714 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009715
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009716 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009717 return ERR_PTR(-ENOMEM);
9718
Wanpeng Li991e7a02015-09-16 17:30:05 +08009719 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009720
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009721 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9722 if (err)
9723 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009724
Peter Feiner4e595162016-07-07 14:49:58 -07009725 err = -ENOMEM;
9726
9727 /*
9728 * If PML is turned on, failure on enabling PML just results in failure
9729 * of creating the vcpu, therefore we can simplify PML logic (by
9730 * avoiding dealing with cases, such as enabling PML partially on vcpus
9731 * for the guest, etc.
9732 */
9733 if (enable_pml) {
9734 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9735 if (!vmx->pml_pg)
9736 goto uninit_vcpu;
9737 }
9738
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009739 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009740 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9741 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009742
Peter Feiner4e595162016-07-07 14:49:58 -07009743 if (!vmx->guest_msrs)
9744 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009745
Paolo Bonzinif21f1652018-01-11 12:16:15 +01009746 err = alloc_loaded_vmcs(&vmx->vmcs01);
9747 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009748 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009749
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009750 msr_bitmap = vmx->vmcs01.msr_bitmap;
9751 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
9752 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
9753 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
9754 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
9755 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
9756 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
9757 vmx->msr_bitmap_mode = 0;
9758
Paolo Bonzinif21f1652018-01-11 12:16:15 +01009759 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03009760 cpu = get_cpu();
9761 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009762 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +02009763 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009764 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009765 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +02009766 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009767 err = alloc_apic_access_page(kvm);
9768 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009769 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009770 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009771
Sheng Yangb927a3c2009-07-21 10:42:48 +08009772 if (enable_ept) {
Tang Chenf51770e2014-09-16 18:41:59 +08009773 err = init_rmode_identity_map(kvm);
9774 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009775 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009776 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009777
Wanpeng Li5c614b32015-10-13 09:18:36 -07009778 if (nested) {
Wincy Vanb9c237b2015-02-03 23:56:30 +08009779 nested_vmx_setup_ctls_msrs(vmx);
Wanpeng Li5c614b32015-10-13 09:18:36 -07009780 vmx->nested.vpid02 = allocate_vpid();
9781 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009782
Wincy Van705699a2015-02-03 23:58:17 +08009783 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009784 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009785
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009786 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9787
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02009788 /*
9789 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9790 * or POSTED_INTR_WAKEUP_VECTOR.
9791 */
9792 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9793 vmx->pi_desc.sn = 1;
9794
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009795 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009796
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009797free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009798 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009799 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009800free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009801 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009802free_pml:
9803 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009804uninit_vcpu:
9805 kvm_vcpu_uninit(&vmx->vcpu);
9806free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009807 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009808 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009809 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009810}
9811
Yang, Sheng002c7f72007-07-31 14:23:01 +03009812static void __init vmx_check_processor_compat(void *rtn)
9813{
9814 struct vmcs_config vmcs_conf;
9815
9816 *(int *)rtn = 0;
9817 if (setup_vmcs_config(&vmcs_conf) < 0)
9818 *(int *)rtn = -EIO;
9819 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9820 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9821 smp_processor_id());
9822 *(int *)rtn = -EIO;
9823 }
9824}
9825
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009826static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009827{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009828 u8 cache;
9829 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009830
Sheng Yang522c68c2009-04-27 20:35:43 +08009831 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009832 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009833 * 2. EPT with VT-d:
9834 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009835 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009836 * b. VT-d with snooping control feature: snooping control feature of
9837 * VT-d engine can guarantee the cache correctness. Just set it
9838 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009839 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009840 * consistent with host MTRR
9841 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009842 if (is_mmio) {
9843 cache = MTRR_TYPE_UNCACHABLE;
9844 goto exit;
9845 }
9846
9847 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009848 ipat = VMX_EPT_IPAT_BIT;
9849 cache = MTRR_TYPE_WRBACK;
9850 goto exit;
9851 }
9852
9853 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9854 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009855 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009856 cache = MTRR_TYPE_WRBACK;
9857 else
9858 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009859 goto exit;
9860 }
9861
Xiao Guangrongff536042015-06-15 16:55:22 +08009862 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009863
9864exit:
9865 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009866}
9867
Sheng Yang17cc3932010-01-05 19:02:27 +08009868static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009869{
Sheng Yang878403b2010-01-05 19:02:29 +08009870 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9871 return PT_DIRECTORY_LEVEL;
9872 else
9873 /* For shadow and EPT supported 1GB page */
9874 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009875}
9876
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009877static void vmcs_set_secondary_exec_control(u32 new_ctl)
9878{
9879 /*
9880 * These bits in the secondary execution controls field
9881 * are dynamic, the others are mostly based on the hypervisor
9882 * architecture and the guest's CPUID. Do not touch the
9883 * dynamic bits.
9884 */
9885 u32 mask =
9886 SECONDARY_EXEC_SHADOW_VMCS |
9887 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02009888 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9889 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009890
9891 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9892
9893 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9894 (new_ctl & ~mask) | (cur_ctl & mask));
9895}
9896
David Matlack8322ebb2016-11-29 18:14:09 -08009897/*
9898 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
9899 * (indicating "allowed-1") if they are supported in the guest's CPUID.
9900 */
9901static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
9902{
9903 struct vcpu_vmx *vmx = to_vmx(vcpu);
9904 struct kvm_cpuid_entry2 *entry;
9905
9906 vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff;
9907 vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE;
9908
9909#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
9910 if (entry && (entry->_reg & (_cpuid_mask))) \
9911 vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \
9912} while (0)
9913
9914 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
9915 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
9916 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
9917 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
9918 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
9919 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
9920 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
9921 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
9922 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
9923 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
9924 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
9925 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
9926 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
9927 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
9928 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
9929
9930 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9931 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
9932 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
9933 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
9934 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +01009935 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -08009936
9937#undef cr4_fixed1_update
9938}
9939
Sheng Yang0e851882009-12-18 16:48:46 +08009940static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9941{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009942 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009943
Paolo Bonzini80154d72017-08-24 13:55:35 +02009944 if (cpu_has_secondary_exec_ctrls()) {
9945 vmx_compute_secondary_exec_control(vmx);
9946 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009947 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009948
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009949 if (nested_vmx_allowed(vcpu))
9950 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9951 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9952 else
9953 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9954 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -08009955
9956 if (nested_vmx_allowed(vcpu))
9957 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08009958}
9959
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009960static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9961{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009962 if (func == 1 && nested)
9963 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009964}
9965
Yang Zhang25d92082013-08-06 12:00:32 +03009966static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9967 struct x86_exception *fault)
9968{
Jan Kiszka533558b2014-01-04 18:47:20 +01009969 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -04009970 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +01009971 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -04009972 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +03009973
Bandan Dasc5f983f2017-05-05 15:25:14 -04009974 if (vmx->nested.pml_full) {
9975 exit_reason = EXIT_REASON_PML_FULL;
9976 vmx->nested.pml_full = false;
9977 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
9978 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009979 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009980 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009981 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -04009982
9983 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009984 vmcs12->guest_physical_address = fault->address;
9985}
9986
Peter Feiner995f00a2017-06-30 17:26:32 -07009987static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
9988{
David Hildenbrandbb97a012017-08-10 23:15:28 +02009989 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -07009990}
9991
Nadav Har'El155a97a2013-08-05 11:07:16 +03009992/* Callbacks for nested_ept_init_mmu_context: */
9993
9994static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9995{
9996 /* return the page table to be shadowed - in our case, EPT12 */
9997 return get_vmcs12(vcpu)->ept_pointer;
9998}
9999
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010000static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010001{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010002 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010003 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010004 return 1;
10005
10006 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010007 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010008 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010009 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010010 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010011 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10012 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10013 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10014
10015 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010016 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010017}
10018
10019static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10020{
10021 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10022}
10023
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010024static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10025 u16 error_code)
10026{
10027 bool inequality, bit;
10028
10029 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10030 inequality =
10031 (error_code & vmcs12->page_fault_error_code_mask) !=
10032 vmcs12->page_fault_error_code_match;
10033 return inequality ^ bit;
10034}
10035
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010036static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10037 struct x86_exception *fault)
10038{
10039 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10040
10041 WARN_ON(!is_guest_mode(vcpu));
10042
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010043 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10044 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010045 vmcs12->vm_exit_intr_error_code = fault->error_code;
10046 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10047 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10048 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10049 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010050 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010051 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010052 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010053}
10054
Paolo Bonzinic9923842017-12-13 14:16:30 +010010055static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10056 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010057
10058static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010059 struct vmcs12 *vmcs12)
10060{
10061 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010062 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010063 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010064
10065 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010066 /*
10067 * Translate L1 physical address to host physical
10068 * address for vmcs02. Keep the page pinned, so this
10069 * physical address remains valid. We keep a reference
10070 * to it so we can release it later.
10071 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010072 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010073 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010074 vmx->nested.apic_access_page = NULL;
10075 }
10076 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010077 /*
10078 * If translation failed, no matter: This feature asks
10079 * to exit when accessing the given address, and if it
10080 * can never be accessed, this feature won't do
10081 * anything anyway.
10082 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010083 if (!is_error_page(page)) {
10084 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010085 hpa = page_to_phys(vmx->nested.apic_access_page);
10086 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10087 } else {
10088 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10089 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10090 }
10091 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
10092 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
10093 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
10094 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10095 kvm_vcpu_reload_apic_access_page(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010096 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010097
10098 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010099 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010100 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010101 vmx->nested.virtual_apic_page = NULL;
10102 }
10103 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010104
10105 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010106 * If translation failed, VM entry will fail because
10107 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10108 * Failing the vm entry is _not_ what the processor
10109 * does but it's basically the only possibility we
10110 * have. We could still enter the guest if CR8 load
10111 * exits are enabled, CR8 store exits are enabled, and
10112 * virtualize APIC access is disabled; in this case
10113 * the processor would never use the TPR shadow and we
10114 * could simply clear the bit from the execution
10115 * control. But such a configuration is useless, so
10116 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010117 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010118 if (!is_error_page(page)) {
10119 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010120 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10121 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10122 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010123 }
10124
Wincy Van705699a2015-02-03 23:58:17 +080010125 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010126 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10127 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010128 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010129 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010130 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010131 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10132 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010133 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010134 vmx->nested.pi_desc_page = page;
10135 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010136 vmx->nested.pi_desc =
10137 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10138 (unsigned long)(vmcs12->posted_intr_desc_addr &
10139 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010140 vmcs_write64(POSTED_INTR_DESC_ADDR,
10141 page_to_phys(vmx->nested.pi_desc_page) +
10142 (unsigned long)(vmcs12->posted_intr_desc_addr &
10143 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010144 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010145 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010146 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10147 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010148 else
10149 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10150 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010151}
10152
Jan Kiszkaf4124502014-03-07 20:03:13 +010010153static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10154{
10155 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10156 struct vcpu_vmx *vmx = to_vmx(vcpu);
10157
10158 if (vcpu->arch.virtual_tsc_khz == 0)
10159 return;
10160
10161 /* Make sure short timeouts reliably trigger an immediate vmexit.
10162 * hrtimer_start does not guarantee this. */
10163 if (preemption_timeout <= 1) {
10164 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10165 return;
10166 }
10167
10168 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10169 preemption_timeout *= 1000000;
10170 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10171 hrtimer_start(&vmx->nested.preemption_timer,
10172 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10173}
10174
Jim Mattson56a20512017-07-06 16:33:06 -070010175static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10176 struct vmcs12 *vmcs12)
10177{
10178 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10179 return 0;
10180
10181 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10182 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10183 return -EINVAL;
10184
10185 return 0;
10186}
10187
Wincy Van3af18d92015-02-03 23:49:31 +080010188static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10189 struct vmcs12 *vmcs12)
10190{
Wincy Van3af18d92015-02-03 23:49:31 +080010191 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10192 return 0;
10193
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010194 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010195 return -EINVAL;
10196
10197 return 0;
10198}
10199
Jim Mattson712b12d2017-08-24 13:24:47 -070010200static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10201 struct vmcs12 *vmcs12)
10202{
10203 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10204 return 0;
10205
10206 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10207 return -EINVAL;
10208
10209 return 0;
10210}
10211
Wincy Van3af18d92015-02-03 23:49:31 +080010212/*
10213 * Merge L0's and L1's MSR bitmap, return false to indicate that
10214 * we do not use the hardware.
10215 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010216static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10217 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010218{
Wincy Van82f0dd42015-02-03 23:57:18 +080010219 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010220 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010221 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010222 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010223 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010224 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010225 *
10226 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10227 * ensures that we do not accidentally generate an L02 MSR bitmap
10228 * from the L12 MSR bitmap that is too permissive.
10229 * 2. That L1 or L2s have actually used the MSR. This avoids
10230 * unnecessarily merging of the bitmap if the MSR is unused. This
10231 * works properly because we only update the L01 MSR bitmap lazily.
10232 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10233 * updated to reflect this when L1 (or its L2s) actually write to
10234 * the MSR.
10235 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010236 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10237 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010238
Paolo Bonzinic9923842017-12-13 14:16:30 +010010239 /* Nothing to do if the MSR bitmap is not in use. */
10240 if (!cpu_has_vmx_msr_bitmap() ||
10241 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10242 return false;
10243
Ashok Raj15d45072018-02-01 22:59:43 +010010244 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010245 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010246 return false;
10247
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010248 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10249 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010250 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010251
Radim Krčmářd048c092016-08-08 20:16:22 +020010252 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010253 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10254 /*
10255 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10256 * just lets the processor take the value from the virtual-APIC page;
10257 * take those 256 bits directly from the L1 bitmap.
10258 */
10259 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10260 unsigned word = msr / BITS_PER_LONG;
10261 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10262 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010263 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010264 } else {
10265 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10266 unsigned word = msr / BITS_PER_LONG;
10267 msr_bitmap_l0[word] = ~0;
10268 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10269 }
10270 }
10271
10272 nested_vmx_disable_intercept_for_msr(
10273 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010274 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010275 MSR_TYPE_W);
10276
10277 if (nested_cpu_has_vid(vmcs12)) {
10278 nested_vmx_disable_intercept_for_msr(
10279 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010280 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010281 MSR_TYPE_W);
10282 nested_vmx_disable_intercept_for_msr(
10283 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010284 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010285 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010286 }
Ashok Raj15d45072018-02-01 22:59:43 +010010287
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010288 if (spec_ctrl)
10289 nested_vmx_disable_intercept_for_msr(
10290 msr_bitmap_l1, msr_bitmap_l0,
10291 MSR_IA32_SPEC_CTRL,
10292 MSR_TYPE_R | MSR_TYPE_W);
10293
Ashok Raj15d45072018-02-01 22:59:43 +010010294 if (pred_cmd)
10295 nested_vmx_disable_intercept_for_msr(
10296 msr_bitmap_l1, msr_bitmap_l0,
10297 MSR_IA32_PRED_CMD,
10298 MSR_TYPE_W);
10299
Wincy Vanf2b93282015-02-03 23:56:03 +080010300 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010301 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010302
10303 return true;
10304}
10305
10306static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10307 struct vmcs12 *vmcs12)
10308{
Wincy Van82f0dd42015-02-03 23:57:18 +080010309 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010310 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010311 !nested_cpu_has_vid(vmcs12) &&
10312 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010313 return 0;
10314
10315 /*
10316 * If virtualize x2apic mode is enabled,
10317 * virtualize apic access must be disabled.
10318 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010319 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10320 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010321 return -EINVAL;
10322
Wincy Van608406e2015-02-03 23:57:51 +080010323 /*
10324 * If virtual interrupt delivery is enabled,
10325 * we must exit on external interrupts.
10326 */
10327 if (nested_cpu_has_vid(vmcs12) &&
10328 !nested_exit_on_intr(vcpu))
10329 return -EINVAL;
10330
Wincy Van705699a2015-02-03 23:58:17 +080010331 /*
10332 * bits 15:8 should be zero in posted_intr_nv,
10333 * the descriptor address has been already checked
10334 * in nested_get_vmcs12_pages.
10335 */
10336 if (nested_cpu_has_posted_intr(vmcs12) &&
10337 (!nested_cpu_has_vid(vmcs12) ||
10338 !nested_exit_intr_ack_set(vcpu) ||
10339 vmcs12->posted_intr_nv & 0xff00))
10340 return -EINVAL;
10341
Wincy Vanf2b93282015-02-03 23:56:03 +080010342 /* tpr shadow is needed by all apicv features. */
10343 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10344 return -EINVAL;
10345
10346 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010347}
10348
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010349static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10350 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010351 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010352{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010353 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010354 u64 count, addr;
10355
10356 if (vmcs12_read_any(vcpu, count_field, &count) ||
10357 vmcs12_read_any(vcpu, addr_field, &addr)) {
10358 WARN_ON(1);
10359 return -EINVAL;
10360 }
10361 if (count == 0)
10362 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010363 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010364 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10365 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010366 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010367 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10368 addr_field, maxphyaddr, count, addr);
10369 return -EINVAL;
10370 }
10371 return 0;
10372}
10373
10374static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10375 struct vmcs12 *vmcs12)
10376{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010377 if (vmcs12->vm_exit_msr_load_count == 0 &&
10378 vmcs12->vm_exit_msr_store_count == 0 &&
10379 vmcs12->vm_entry_msr_load_count == 0)
10380 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010381 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010382 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010383 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010384 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010385 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010386 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010387 return -EINVAL;
10388 return 0;
10389}
10390
Bandan Dasc5f983f2017-05-05 15:25:14 -040010391static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10392 struct vmcs12 *vmcs12)
10393{
10394 u64 address = vmcs12->pml_address;
10395 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10396
10397 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10398 if (!nested_cpu_has_ept(vmcs12) ||
10399 !IS_ALIGNED(address, 4096) ||
10400 address >> maxphyaddr)
10401 return -EINVAL;
10402 }
10403
10404 return 0;
10405}
10406
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010407static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10408 struct vmx_msr_entry *e)
10409{
10410 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010411 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010412 return -EINVAL;
10413 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10414 e->index == MSR_IA32_UCODE_REV)
10415 return -EINVAL;
10416 if (e->reserved != 0)
10417 return -EINVAL;
10418 return 0;
10419}
10420
10421static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10422 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010423{
10424 if (e->index == MSR_FS_BASE ||
10425 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010426 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10427 nested_vmx_msr_check_common(vcpu, e))
10428 return -EINVAL;
10429 return 0;
10430}
10431
10432static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10433 struct vmx_msr_entry *e)
10434{
10435 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10436 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010437 return -EINVAL;
10438 return 0;
10439}
10440
10441/*
10442 * Load guest's/host's msr at nested entry/exit.
10443 * return 0 for success, entry index for failure.
10444 */
10445static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10446{
10447 u32 i;
10448 struct vmx_msr_entry e;
10449 struct msr_data msr;
10450
10451 msr.host_initiated = false;
10452 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010453 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10454 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010455 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010456 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10457 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010458 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010459 }
10460 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010461 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010462 "%s check failed (%u, 0x%x, 0x%x)\n",
10463 __func__, i, e.index, e.reserved);
10464 goto fail;
10465 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010466 msr.index = e.index;
10467 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010468 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010469 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010470 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10471 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010472 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010473 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010474 }
10475 return 0;
10476fail:
10477 return i + 1;
10478}
10479
10480static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10481{
10482 u32 i;
10483 struct vmx_msr_entry e;
10484
10485 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010486 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010487 if (kvm_vcpu_read_guest(vcpu,
10488 gpa + i * sizeof(e),
10489 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010490 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010491 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10492 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010493 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010494 }
10495 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010496 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010497 "%s check failed (%u, 0x%x, 0x%x)\n",
10498 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010499 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010500 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010501 msr_info.host_initiated = false;
10502 msr_info.index = e.index;
10503 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010504 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010505 "%s cannot read MSR (%u, 0x%x)\n",
10506 __func__, i, e.index);
10507 return -EINVAL;
10508 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010509 if (kvm_vcpu_write_guest(vcpu,
10510 gpa + i * sizeof(e) +
10511 offsetof(struct vmx_msr_entry, value),
10512 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010513 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010514 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010515 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010516 return -EINVAL;
10517 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010518 }
10519 return 0;
10520}
10521
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010522static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10523{
10524 unsigned long invalid_mask;
10525
10526 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10527 return (val & invalid_mask) == 0;
10528}
10529
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010530/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010531 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10532 * emulating VM entry into a guest with EPT enabled.
10533 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10534 * is assigned to entry_failure_code on failure.
10535 */
10536static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010537 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010538{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010539 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010540 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010541 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10542 return 1;
10543 }
10544
10545 /*
10546 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10547 * must not be dereferenced.
10548 */
10549 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10550 !nested_ept) {
10551 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10552 *entry_failure_code = ENTRY_FAIL_PDPTE;
10553 return 1;
10554 }
10555 }
10556
10557 vcpu->arch.cr3 = cr3;
10558 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10559 }
10560
10561 kvm_mmu_reset_context(vcpu);
10562 return 0;
10563}
10564
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010565static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10566 bool from_vmentry)
10567{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010568 struct vcpu_vmx *vmx = to_vmx(vcpu);
10569
10570 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10571 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10572 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10573 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10574 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10575 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10576 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10577 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10578 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10579 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10580 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10581 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10582 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10583 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10584 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10585 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10586 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10587 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10588 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10589 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10590 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10591 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10592 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10593 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10594 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10595 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10596 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10597 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10598 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10599 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10600 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010601
10602 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10603 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10604 vmcs12->guest_pending_dbg_exceptions);
10605 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10606 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10607
10608 if (nested_cpu_has_xsaves(vmcs12))
10609 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
10610 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10611
10612 if (cpu_has_vmx_posted_intr())
10613 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
10614
10615 /*
10616 * Whether page-faults are trapped is determined by a combination of
10617 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10618 * If enable_ept, L0 doesn't care about page faults and we should
10619 * set all of these to L1's desires. However, if !enable_ept, L0 does
10620 * care about (at least some) page faults, and because it is not easy
10621 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10622 * to exit on each and every L2 page fault. This is done by setting
10623 * MASK=MATCH=0 and (see below) EB.PF=1.
10624 * Note that below we don't need special code to set EB.PF beyond the
10625 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10626 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10627 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10628 */
10629 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10630 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10631 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10632 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10633
10634 /* All VMFUNCs are currently emulated through L0 vmexits. */
10635 if (cpu_has_vmx_vmfunc())
10636 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10637
10638 if (cpu_has_vmx_apicv()) {
10639 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
10640 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
10641 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
10642 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
10643 }
10644
10645 /*
10646 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10647 * Some constant fields are set here by vmx_set_constant_host_state().
10648 * Other fields are different per CPU, and will be set later when
10649 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10650 */
10651 vmx_set_constant_host_state(vmx);
10652
10653 /*
10654 * Set the MSR load/store lists to match L0's settings.
10655 */
10656 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10657 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10658 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10659 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10660 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10661
10662 set_cr4_guest_host_mask(vmx);
10663
10664 if (vmx_mpx_supported())
10665 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10666
10667 if (enable_vpid) {
10668 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
10669 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10670 else
10671 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10672 }
10673
10674 /*
10675 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10676 */
10677 if (enable_ept) {
10678 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10679 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10680 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10681 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10682 }
Radim Krčmář80132f42018-02-02 18:26:58 +010010683
10684 if (cpu_has_vmx_msr_bitmap())
10685 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010686}
10687
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010688/*
10689 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10690 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010691 * 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 +030010692 * guest in a way that will both be appropriate to L1's requests, and our
10693 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10694 * function also has additional necessary side-effects, like setting various
10695 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010010696 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10697 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010698 */
Ladi Prosekee146c12016-11-30 16:03:09 +010010699static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010700 bool from_vmentry, u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010701{
10702 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040010703 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010704
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010705 /*
10706 * First, the fields that are shadowed. This must be kept in sync
10707 * with vmx_shadow_fields.h.
10708 */
10709
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010710 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010711 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010712 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010713 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10714 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010715
10716 /*
10717 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
10718 * HOST_FS_BASE, HOST_GS_BASE.
10719 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010720
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010721 if (from_vmentry &&
10722 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020010723 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10724 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10725 } else {
10726 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10727 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10728 }
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010729 if (from_vmentry) {
10730 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10731 vmcs12->vm_entry_intr_info_field);
10732 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10733 vmcs12->vm_entry_exception_error_code);
10734 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10735 vmcs12->vm_entry_instruction_len);
10736 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10737 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070010738 vmx->loaded_vmcs->nmi_known_unmasked =
10739 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010740 } else {
10741 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10742 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030010743 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010744
Jan Kiszkaf4124502014-03-07 20:03:13 +010010745 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010746
Paolo Bonzini93140062016-07-06 13:23:51 +020010747 /* Preemption timer setting is only taken from vmcs01. */
10748 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10749 exec_control |= vmcs_config.pin_based_exec_ctrl;
10750 if (vmx->hv_deadline_tsc == -1)
10751 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10752
10753 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010754 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010755 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10756 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010757 } else {
Wincy Van705699a2015-02-03 23:58:17 +080010758 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010759 }
Wincy Van705699a2015-02-03 23:58:17 +080010760
Jan Kiszkaf4124502014-03-07 20:03:13 +010010761 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010762
Jan Kiszkaf4124502014-03-07 20:03:13 +010010763 vmx->nested.preemption_timer_expired = false;
10764 if (nested_cpu_has_preemption_timer(vmcs12))
10765 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010766
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010767 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020010768 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080010769
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010770 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010771 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020010772 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010773 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020010774 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010775 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040010776 SECONDARY_EXEC_APIC_REGISTER_VIRT |
10777 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010778 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040010779 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
10780 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
10781 ~SECONDARY_EXEC_ENABLE_PML;
10782 exec_control |= vmcs12_exec_ctrl;
10783 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010784
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010785 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080010786 vmcs_write16(GUEST_INTR_STATUS,
10787 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080010788
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010789 /*
10790 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10791 * nested_get_vmcs12_pages will either fix it up or
10792 * remove the VM execution control.
10793 */
10794 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
10795 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
10796
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010797 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10798 }
10799
Jim Mattson83bafef2016-10-04 10:48:38 -070010800 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010801 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10802 * entry, but only if the current (host) sp changed from the value
10803 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10804 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10805 * here we just force the write to happen on entry.
10806 */
10807 vmx->host_rsp = 0;
10808
10809 exec_control = vmx_exec_control(vmx); /* L0's desires */
10810 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10811 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10812 exec_control &= ~CPU_BASED_TPR_SHADOW;
10813 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010814
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010815 /*
10816 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
10817 * nested_get_vmcs12_pages can't fix it up, the illegal value
10818 * will result in a VM entry failure.
10819 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010820 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010821 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010822 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070010823 } else {
10824#ifdef CONFIG_X86_64
10825 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10826 CPU_BASED_CR8_STORE_EXITING;
10827#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010828 }
10829
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010830 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080010831 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
10832 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010833 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010834 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10835 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10836
10837 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10838
10839 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10840 * bitwise-or of what L1 wants to trap for L2, and what we want to
10841 * trap. Note that CR0.TS also needs updating - we do this later.
10842 */
10843 update_exception_bitmap(vcpu);
10844 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10845 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10846
Nadav Har'El8049d652013-08-05 11:07:06 +030010847 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10848 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10849 * bits are further modified by vmx_set_efer() below.
10850 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010010851 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010852
10853 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10854 * emulated by vmx_set_efer(), below.
10855 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010856 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010857 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10858 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010859 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10860
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010861 if (from_vmentry &&
10862 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010863 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010864 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010865 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010866 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010867 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010868
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010869 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10870 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010871 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010872 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010873 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010874 if (kvm_has_tsc_control)
10875 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010876
10877 if (enable_vpid) {
10878 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010879 * There is no direct mapping between vpid02 and vpid12, the
10880 * vpid02 is per-vCPU for L0 and reused while the value of
10881 * vpid12 is changed w/ one invvpid during nested vmentry.
10882 * The vpid12 is allocated by L1 for L2, so it will not
10883 * influence global bitmap(for vpid01 and vpid02 allocation)
10884 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010885 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010886 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070010887 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10888 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080010889 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070010890 }
10891 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080010892 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070010893 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010894 }
10895
Ladi Prosek1fb883b2017-04-04 14:18:53 +020010896 if (enable_pml) {
10897 /*
10898 * Conceptually we want to copy the PML address and index from
10899 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10900 * since we always flush the log on each vmexit, this happens
10901 * to be equivalent to simply resetting the fields in vmcs02.
10902 */
10903 ASSERT(vmx->pml_pg);
10904 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10905 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10906 }
10907
Nadav Har'El155a97a2013-08-05 11:07:16 +030010908 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010909 if (nested_ept_init_mmu_context(vcpu)) {
10910 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10911 return 1;
10912 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010913 } else if (nested_cpu_has2(vmcs12,
10914 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10915 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010916 }
10917
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010918 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080010919 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
10920 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010921 * The CR0_READ_SHADOW is what L2 should have expected to read given
10922 * the specifications by L1; It's not enough to take
10923 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10924 * have more bits than L1 expected.
10925 */
10926 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10927 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10928
10929 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10930 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10931
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010932 if (from_vmentry &&
10933 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080010934 vcpu->arch.efer = vmcs12->guest_ia32_efer;
10935 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
10936 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10937 else
10938 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10939 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10940 vmx_set_efer(vcpu, vcpu->arch.efer);
10941
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010942 if (vmx->nested.dirty_vmcs12) {
10943 prepare_vmcs02_full(vcpu, vmcs12, from_vmentry);
10944 vmx->nested.dirty_vmcs12 = false;
10945 }
10946
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010947 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010010948 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010949 entry_failure_code))
10950 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010010951
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010952 if (!enable_ept)
10953 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10954
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010955 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10956 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010010957 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010958}
10959
Jim Mattsonca0bde22016-11-30 12:03:46 -080010960static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10961{
10962 struct vcpu_vmx *vmx = to_vmx(vcpu);
10963
10964 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10965 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
10966 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10967
Jim Mattson56a20512017-07-06 16:33:06 -070010968 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
10969 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10970
Jim Mattsonca0bde22016-11-30 12:03:46 -080010971 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
10972 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10973
Jim Mattson712b12d2017-08-24 13:24:47 -070010974 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
10975 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10976
Jim Mattsonca0bde22016-11-30 12:03:46 -080010977 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
10978 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10979
10980 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
10981 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10982
Bandan Dasc5f983f2017-05-05 15:25:14 -040010983 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
10984 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10985
Jim Mattsonca0bde22016-11-30 12:03:46 -080010986 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
10987 vmx->nested.nested_vmx_procbased_ctls_low,
10988 vmx->nested.nested_vmx_procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070010989 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
10990 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
10991 vmx->nested.nested_vmx_secondary_ctls_low,
10992 vmx->nested.nested_vmx_secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080010993 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
10994 vmx->nested.nested_vmx_pinbased_ctls_low,
10995 vmx->nested.nested_vmx_pinbased_ctls_high) ||
10996 !vmx_control_verify(vmcs12->vm_exit_controls,
10997 vmx->nested.nested_vmx_exit_ctls_low,
10998 vmx->nested.nested_vmx_exit_ctls_high) ||
10999 !vmx_control_verify(vmcs12->vm_entry_controls,
11000 vmx->nested.nested_vmx_entry_ctls_low,
11001 vmx->nested.nested_vmx_entry_ctls_high))
11002 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11003
Bandan Das41ab9372017-08-03 15:54:43 -040011004 if (nested_cpu_has_vmfunc(vmcs12)) {
11005 if (vmcs12->vm_function_control &
11006 ~vmx->nested.nested_vmx_vmfunc_controls)
11007 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11008
11009 if (nested_cpu_has_eptp_switching(vmcs12)) {
11010 if (!nested_cpu_has_ept(vmcs12) ||
11011 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11012 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11013 }
11014 }
Bandan Das27c42a12017-08-03 15:54:42 -040011015
Jim Mattsonc7c2c702017-05-05 11:28:09 -070011016 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11017 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11018
Jim Mattsonca0bde22016-11-30 12:03:46 -080011019 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11020 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11021 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11022 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11023
11024 return 0;
11025}
11026
11027static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11028 u32 *exit_qual)
11029{
11030 bool ia32e;
11031
11032 *exit_qual = ENTRY_FAIL_DEFAULT;
11033
11034 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11035 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11036 return 1;
11037
11038 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11039 vmcs12->vmcs_link_pointer != -1ull) {
11040 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11041 return 1;
11042 }
11043
11044 /*
11045 * If the load IA32_EFER VM-entry control is 1, the following checks
11046 * are performed on the field for the IA32_EFER MSR:
11047 * - Bits reserved in the IA32_EFER MSR must be 0.
11048 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11049 * the IA-32e mode guest VM-exit control. It must also be identical
11050 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11051 * CR0.PG) is 1.
11052 */
11053 if (to_vmx(vcpu)->nested.nested_run_pending &&
11054 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11055 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11056 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11057 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11058 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11059 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11060 return 1;
11061 }
11062
11063 /*
11064 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11065 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11066 * the values of the LMA and LME bits in the field must each be that of
11067 * the host address-space size VM-exit control.
11068 */
11069 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11070 ia32e = (vmcs12->vm_exit_controls &
11071 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11072 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11073 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11074 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11075 return 1;
11076 }
11077
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011078 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11079 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11080 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11081 return 1;
11082
Jim Mattsonca0bde22016-11-30 12:03:46 -080011083 return 0;
11084}
11085
Jim Mattson858e25c2016-11-30 12:03:47 -080011086static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
11087{
11088 struct vcpu_vmx *vmx = to_vmx(vcpu);
11089 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011090 u32 msr_entry_idx;
11091 u32 exit_qual;
11092
Jim Mattson858e25c2016-11-30 12:03:47 -080011093 enter_guest_mode(vcpu);
11094
11095 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11096 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11097
Jim Mattsonde3a0022017-11-27 17:22:25 -060011098 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011099 vmx_segment_cache_clear(vmx);
11100
11101 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
11102 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011103 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080011104 nested_vmx_entry_failure(vcpu, vmcs12,
11105 EXIT_REASON_INVALID_STATE, exit_qual);
11106 return 1;
11107 }
11108
11109 nested_get_vmcs12_pages(vcpu, vmcs12);
11110
11111 msr_entry_idx = nested_vmx_load_msr(vcpu,
11112 vmcs12->vm_entry_msr_load_addr,
11113 vmcs12->vm_entry_msr_load_count);
11114 if (msr_entry_idx) {
11115 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011116 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080011117 nested_vmx_entry_failure(vcpu, vmcs12,
11118 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
11119 return 1;
11120 }
11121
Jim Mattson858e25c2016-11-30 12:03:47 -080011122 /*
11123 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11124 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11125 * returned as far as L1 is concerned. It will only return (and set
11126 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11127 */
11128 return 0;
11129}
11130
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011131/*
11132 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11133 * for running an L2 nested guest.
11134 */
11135static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11136{
11137 struct vmcs12 *vmcs12;
11138 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011139 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011140 u32 exit_qual;
11141 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011142
Kyle Hueyeb277562016-11-29 12:40:39 -080011143 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011144 return 1;
11145
Kyle Hueyeb277562016-11-29 12:40:39 -080011146 if (!nested_vmx_check_vmcs12(vcpu))
11147 goto out;
11148
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011149 vmcs12 = get_vmcs12(vcpu);
11150
Abel Gordon012f83c2013-04-18 14:39:25 +030011151 if (enable_shadow_vmcs)
11152 copy_shadow_to_vmcs12(vmx);
11153
Nadav Har'El7c177932011-05-25 23:12:04 +030011154 /*
11155 * The nested entry process starts with enforcing various prerequisites
11156 * on vmcs12 as required by the Intel SDM, and act appropriately when
11157 * they fail: As the SDM explains, some conditions should cause the
11158 * instruction to fail, while others will cause the instruction to seem
11159 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11160 * To speed up the normal (success) code path, we should avoid checking
11161 * for misconfigurations which will anyway be caught by the processor
11162 * when using the merged vmcs02.
11163 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011164 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11165 nested_vmx_failValid(vcpu,
11166 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11167 goto out;
11168 }
11169
Nadav Har'El7c177932011-05-25 23:12:04 +030011170 if (vmcs12->launch_state == launch) {
11171 nested_vmx_failValid(vcpu,
11172 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11173 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011174 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011175 }
11176
Jim Mattsonca0bde22016-11-30 12:03:46 -080011177 ret = check_vmentry_prereqs(vcpu, vmcs12);
11178 if (ret) {
11179 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011180 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011181 }
11182
Nadav Har'El7c177932011-05-25 23:12:04 +030011183 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011184 * After this point, the trap flag no longer triggers a singlestep trap
11185 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11186 * This is not 100% correct; for performance reasons, we delegate most
11187 * of the checks on host state to the processor. If those fail,
11188 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011189 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011190 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011191
Jim Mattsonca0bde22016-11-30 12:03:46 -080011192 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11193 if (ret) {
11194 nested_vmx_entry_failure(vcpu, vmcs12,
11195 EXIT_REASON_INVALID_STATE, exit_qual);
11196 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011197 }
11198
11199 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011200 * We're finally done with prerequisite checking, and can start with
11201 * the nested entry.
11202 */
11203
Jim Mattson858e25c2016-11-30 12:03:47 -080011204 ret = enter_vmx_non_root_mode(vcpu, true);
11205 if (ret)
11206 return ret;
Wincy Vanff651cb2014-12-11 08:52:58 +030011207
Chao Gao135a06c2018-02-11 10:06:30 +080011208 /*
11209 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11210 * by event injection, halt vcpu.
11211 */
11212 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
11213 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK))
Joel Schopp5cb56052015-03-02 13:43:31 -060011214 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010011215
Jan Kiszka7af40ad32014-01-04 18:47:23 +010011216 vmx->nested.nested_run_pending = 1;
11217
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011218 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011219
11220out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011221 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011222}
11223
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011224/*
11225 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11226 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11227 * This function returns the new value we should put in vmcs12.guest_cr0.
11228 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11229 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11230 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11231 * didn't trap the bit, because if L1 did, so would L0).
11232 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11233 * been modified by L2, and L1 knows it. So just leave the old value of
11234 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11235 * isn't relevant, because if L0 traps this bit it can set it to anything.
11236 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11237 * changed these bits, and therefore they need to be updated, but L0
11238 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11239 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11240 */
11241static inline unsigned long
11242vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11243{
11244 return
11245 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11246 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11247 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11248 vcpu->arch.cr0_guest_owned_bits));
11249}
11250
11251static inline unsigned long
11252vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11253{
11254 return
11255 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11256 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11257 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11258 vcpu->arch.cr4_guest_owned_bits));
11259}
11260
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011261static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11262 struct vmcs12 *vmcs12)
11263{
11264 u32 idt_vectoring;
11265 unsigned int nr;
11266
Wanpeng Li664f8e22017-08-24 03:35:09 -070011267 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011268 nr = vcpu->arch.exception.nr;
11269 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11270
11271 if (kvm_exception_is_soft(nr)) {
11272 vmcs12->vm_exit_instruction_len =
11273 vcpu->arch.event_exit_inst_len;
11274 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11275 } else
11276 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11277
11278 if (vcpu->arch.exception.has_error_code) {
11279 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11280 vmcs12->idt_vectoring_error_code =
11281 vcpu->arch.exception.error_code;
11282 }
11283
11284 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011285 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011286 vmcs12->idt_vectoring_info_field =
11287 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
11288 } else if (vcpu->arch.interrupt.pending) {
11289 nr = vcpu->arch.interrupt.nr;
11290 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11291
11292 if (vcpu->arch.interrupt.soft) {
11293 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11294 vmcs12->vm_entry_instruction_len =
11295 vcpu->arch.event_exit_inst_len;
11296 } else
11297 idt_vectoring |= INTR_TYPE_EXT_INTR;
11298
11299 vmcs12->idt_vectoring_info_field = idt_vectoring;
11300 }
11301}
11302
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011303static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11304{
11305 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011306 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020011307 bool block_nested_events =
11308 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011309
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011310 if (vcpu->arch.exception.pending &&
11311 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020011312 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011313 return -EBUSY;
11314 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011315 return 0;
11316 }
11317
Jan Kiszkaf4124502014-03-07 20:03:13 +010011318 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11319 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020011320 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010011321 return -EBUSY;
11322 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11323 return 0;
11324 }
11325
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011326 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011327 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011328 return -EBUSY;
11329 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11330 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11331 INTR_INFO_VALID_MASK, 0);
11332 /*
11333 * The NMI-triggered VM exit counts as injection:
11334 * clear this one and block further NMIs.
11335 */
11336 vcpu->arch.nmi_pending = 0;
11337 vmx_set_nmi_mask(vcpu, true);
11338 return 0;
11339 }
11340
11341 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11342 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011343 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011344 return -EBUSY;
11345 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080011346 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011347 }
11348
David Hildenbrand6342c502017-01-25 11:58:58 +010011349 vmx_complete_nested_posted_interrupt(vcpu);
11350 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011351}
11352
Jan Kiszkaf4124502014-03-07 20:03:13 +010011353static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11354{
11355 ktime_t remaining =
11356 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11357 u64 value;
11358
11359 if (ktime_to_ns(remaining) <= 0)
11360 return 0;
11361
11362 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11363 do_div(value, 1000000);
11364 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11365}
11366
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011367/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011368 * Update the guest state fields of vmcs12 to reflect changes that
11369 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11370 * VM-entry controls is also updated, since this is really a guest
11371 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011372 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011373static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011374{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011375 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11376 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11377
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011378 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11379 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11380 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11381
11382 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11383 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11384 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11385 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11386 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11387 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11388 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11389 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11390 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11391 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11392 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11393 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11394 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11395 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11396 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11397 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11398 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11399 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11400 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11401 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11402 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11403 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11404 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11405 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11406 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11407 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11408 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11409 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11410 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11411 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11412 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11413 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11414 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11415 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11416 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11417 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11418
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011419 vmcs12->guest_interruptibility_info =
11420 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11421 vmcs12->guest_pending_dbg_exceptions =
11422 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010011423 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11424 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11425 else
11426 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011427
Jan Kiszkaf4124502014-03-07 20:03:13 +010011428 if (nested_cpu_has_preemption_timer(vmcs12)) {
11429 if (vmcs12->vm_exit_controls &
11430 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11431 vmcs12->vmx_preemption_timer_value =
11432 vmx_get_preemption_timer_value(vcpu);
11433 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11434 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080011435
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011436 /*
11437 * In some cases (usually, nested EPT), L2 is allowed to change its
11438 * own CR3 without exiting. If it has changed it, we must keep it.
11439 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11440 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11441 *
11442 * Additionally, restore L2's PDPTR to vmcs12.
11443 */
11444 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010011445 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011446 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11447 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11448 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11449 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11450 }
11451
Jim Mattsond281e132017-06-01 12:44:46 -070011452 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011453
Wincy Van608406e2015-02-03 23:57:51 +080011454 if (nested_cpu_has_vid(vmcs12))
11455 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11456
Jan Kiszkac18911a2013-03-13 16:06:41 +010011457 vmcs12->vm_entry_controls =
11458 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011459 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011460
Jan Kiszka2996fca2014-06-16 13:59:43 +020011461 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11462 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11463 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11464 }
11465
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011466 /* TODO: These cannot have changed unless we have MSR bitmaps and
11467 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011468 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011469 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011470 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11471 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011472 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11473 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11474 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011475 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011476 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011477}
11478
11479/*
11480 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11481 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11482 * and this function updates it to reflect the changes to the guest state while
11483 * L2 was running (and perhaps made some exits which were handled directly by L0
11484 * without going back to L1), and to reflect the exit reason.
11485 * Note that we do not have to copy here all VMCS fields, just those that
11486 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11487 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11488 * which already writes to vmcs12 directly.
11489 */
11490static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11491 u32 exit_reason, u32 exit_intr_info,
11492 unsigned long exit_qualification)
11493{
11494 /* update guest state fields: */
11495 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011496
11497 /* update exit information fields: */
11498
Jan Kiszka533558b2014-01-04 18:47:20 +010011499 vmcs12->vm_exit_reason = exit_reason;
11500 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010011501 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020011502
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011503 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011504 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11505 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11506
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011507 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070011508 vmcs12->launch_state = 1;
11509
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011510 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11511 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011512 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011513
11514 /*
11515 * Transfer the event that L0 or L1 may wanted to inject into
11516 * L2 to IDT_VECTORING_INFO_FIELD.
11517 */
11518 vmcs12_save_pending_event(vcpu, vmcs12);
11519 }
11520
11521 /*
11522 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11523 * preserved above and would only end up incorrectly in L1.
11524 */
11525 vcpu->arch.nmi_injected = false;
11526 kvm_clear_exception_queue(vcpu);
11527 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011528}
11529
Wanpeng Li5af41572017-11-05 16:54:49 -080011530static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11531 struct vmcs12 *vmcs12)
11532{
11533 u32 entry_failure_code;
11534
11535 nested_ept_uninit_mmu_context(vcpu);
11536
11537 /*
11538 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11539 * couldn't have changed.
11540 */
11541 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11542 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11543
11544 if (!enable_ept)
11545 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11546}
11547
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011548/*
11549 * A part of what we need to when the nested L2 guest exits and we want to
11550 * run its L1 parent, is to reset L1's guest state to the host state specified
11551 * in vmcs12.
11552 * This function is to be called not only on normal nested exit, but also on
11553 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11554 * Failures During or After Loading Guest State").
11555 * This function should be called when the active VMCS is L1's (vmcs01).
11556 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011557static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11558 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011559{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011560 struct kvm_segment seg;
11561
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011562 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11563 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011564 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011565 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11566 else
11567 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11568 vmx_set_efer(vcpu, vcpu->arch.efer);
11569
11570 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11571 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011572 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011573 /*
11574 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011575 * actually changed, because vmx_set_cr0 refers to efer set above.
11576 *
11577 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11578 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011579 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011580 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020011581 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011582
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011583 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011584 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080011585 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011586
Wanpeng Li5af41572017-11-05 16:54:49 -080011587 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011588
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011589 if (enable_vpid) {
11590 /*
11591 * Trivially support vpid by letting L2s share their parent
11592 * L1's vpid. TODO: move to a more elaborate solution, giving
11593 * each L2 its own vpid and exposing the vpid feature to L1.
11594 */
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011595 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011596 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011597
11598 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11599 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11600 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11601 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11602 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020011603 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11604 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011605
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011606 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11607 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11608 vmcs_write64(GUEST_BNDCFGS, 0);
11609
Jan Kiszka44811c02013-08-04 17:17:27 +020011610 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011611 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011612 vcpu->arch.pat = vmcs12->host_ia32_pat;
11613 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011614 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11615 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11616 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011617
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011618 /* Set L1 segment info according to Intel SDM
11619 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11620 seg = (struct kvm_segment) {
11621 .base = 0,
11622 .limit = 0xFFFFFFFF,
11623 .selector = vmcs12->host_cs_selector,
11624 .type = 11,
11625 .present = 1,
11626 .s = 1,
11627 .g = 1
11628 };
11629 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11630 seg.l = 1;
11631 else
11632 seg.db = 1;
11633 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11634 seg = (struct kvm_segment) {
11635 .base = 0,
11636 .limit = 0xFFFFFFFF,
11637 .type = 3,
11638 .present = 1,
11639 .s = 1,
11640 .db = 1,
11641 .g = 1
11642 };
11643 seg.selector = vmcs12->host_ds_selector;
11644 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11645 seg.selector = vmcs12->host_es_selector;
11646 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11647 seg.selector = vmcs12->host_ss_selector;
11648 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11649 seg.selector = vmcs12->host_fs_selector;
11650 seg.base = vmcs12->host_fs_base;
11651 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11652 seg.selector = vmcs12->host_gs_selector;
11653 seg.base = vmcs12->host_gs_base;
11654 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11655 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011656 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011657 .limit = 0x67,
11658 .selector = vmcs12->host_tr_selector,
11659 .type = 11,
11660 .present = 1
11661 };
11662 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11663
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011664 kvm_set_dr(vcpu, 7, 0x400);
11665 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011666
Wincy Van3af18d92015-02-03 23:49:31 +080011667 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011668 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080011669
Wincy Vanff651cb2014-12-11 08:52:58 +030011670 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11671 vmcs12->vm_exit_msr_load_count))
11672 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011673}
11674
11675/*
11676 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11677 * and modify vmcs12 to make it see what it would expect to see there if
11678 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11679 */
Jan Kiszka533558b2014-01-04 18:47:20 +010011680static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11681 u32 exit_intr_info,
11682 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011683{
11684 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011685 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11686
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011687 /* trying to cancel vmlaunch/vmresume is a bug */
11688 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11689
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011690 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070011691 * The only expected VM-instruction error is "VM entry with
11692 * invalid control field(s)." Anything else indicates a
11693 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011694 */
Jim Mattson4f350c62017-09-14 16:31:44 -070011695 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
11696 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
11697
11698 leave_guest_mode(vcpu);
11699
11700 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011701 if (exit_reason == -1)
11702 sync_vmcs12(vcpu, vmcs12);
11703 else
11704 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11705 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070011706
11707 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11708 vmcs12->vm_exit_msr_store_count))
11709 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040011710 }
11711
Jim Mattson4f350c62017-09-14 16:31:44 -070011712 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020011713 vm_entry_controls_reset_shadow(vmx);
11714 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010011715 vmx_segment_cache_clear(vmx);
11716
Paolo Bonzini93140062016-07-06 13:23:51 +020011717 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070011718 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11719 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011720 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020011721 if (vmx->hv_deadline_tsc == -1)
11722 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11723 PIN_BASED_VMX_PREEMPTION_TIMER);
11724 else
11725 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11726 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070011727 if (kvm_has_tsc_control)
11728 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011729
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011730 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11731 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11732 vmx_set_virtual_x2apic_mode(vcpu,
11733 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011734 } else if (!nested_cpu_has_ept(vmcs12) &&
11735 nested_cpu_has2(vmcs12,
11736 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11737 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011738 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011739
11740 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11741 vmx->host_rsp = 0;
11742
11743 /* Unpin physical memory we referred to in vmcs02 */
11744 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011745 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011746 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011747 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011748 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011749 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011750 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011751 }
Wincy Van705699a2015-02-03 23:58:17 +080011752 if (vmx->nested.pi_desc_page) {
11753 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011754 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011755 vmx->nested.pi_desc_page = NULL;
11756 vmx->nested.pi_desc = NULL;
11757 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011758
11759 /*
Tang Chen38b99172014-09-24 15:57:54 +080011760 * We are now running in L2, mmu_notifier will force to reload the
11761 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11762 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011763 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011764
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011765 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030011766 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011767
11768 /* in case we halted in L2 */
11769 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070011770
11771 if (likely(!vmx->fail)) {
11772 /*
11773 * TODO: SDM says that with acknowledge interrupt on
11774 * exit, bit 31 of the VM-exit interrupt information
11775 * (valid interrupt) is always set to 1 on
11776 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
11777 * need kvm_cpu_has_interrupt(). See the commit
11778 * message for details.
11779 */
11780 if (nested_exit_intr_ack_set(vcpu) &&
11781 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
11782 kvm_cpu_has_interrupt(vcpu)) {
11783 int irq = kvm_cpu_get_interrupt(vcpu);
11784 WARN_ON(irq < 0);
11785 vmcs12->vm_exit_intr_info = irq |
11786 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11787 }
11788
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011789 if (exit_reason != -1)
11790 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11791 vmcs12->exit_qualification,
11792 vmcs12->idt_vectoring_info_field,
11793 vmcs12->vm_exit_intr_info,
11794 vmcs12->vm_exit_intr_error_code,
11795 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070011796
11797 load_vmcs12_host_state(vcpu, vmcs12);
11798
11799 return;
11800 }
11801
11802 /*
11803 * After an early L2 VM-entry failure, we're now back
11804 * in L1 which thinks it just finished a VMLAUNCH or
11805 * VMRESUME instruction, so we need to set the failure
11806 * flag and the VM-instruction error field of the VMCS
11807 * accordingly.
11808 */
11809 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080011810
11811 load_vmcs12_mmu_host_state(vcpu, vmcs12);
11812
Jim Mattson4f350c62017-09-14 16:31:44 -070011813 /*
11814 * The emulated instruction was already skipped in
11815 * nested_vmx_run, but the updated RIP was never
11816 * written back to the vmcs01.
11817 */
11818 skip_emulated_instruction(vcpu);
11819 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011820}
11821
Nadav Har'El7c177932011-05-25 23:12:04 +030011822/*
Jan Kiszka42124922014-01-04 18:47:19 +010011823 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11824 */
11825static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11826{
Wanpeng Li2f707d92017-03-06 04:03:28 -080011827 if (is_guest_mode(vcpu)) {
11828 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011829 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080011830 }
Jan Kiszka42124922014-01-04 18:47:19 +010011831 free_nested(to_vmx(vcpu));
11832}
11833
11834/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011835 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11836 * 23.7 "VM-entry failures during or after loading guest state" (this also
11837 * lists the acceptable exit-reason and exit-qualification parameters).
11838 * It should only be called before L2 actually succeeded to run, and when
11839 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11840 */
11841static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11842 struct vmcs12 *vmcs12,
11843 u32 reason, unsigned long qualification)
11844{
11845 load_vmcs12_host_state(vcpu, vmcs12);
11846 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11847 vmcs12->exit_qualification = qualification;
11848 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011849 if (enable_shadow_vmcs)
11850 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011851}
11852
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011853static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11854 struct x86_instruction_info *info,
11855 enum x86_intercept_stage stage)
11856{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020011857 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11858 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
11859
11860 /*
11861 * RDPID causes #UD if disabled through secondary execution controls.
11862 * Because it is marked as EmulateOnUD, we need to intercept it here.
11863 */
11864 if (info->intercept == x86_intercept_rdtscp &&
11865 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
11866 ctxt->exception.vector = UD_VECTOR;
11867 ctxt->exception.error_code_valid = false;
11868 return X86EMUL_PROPAGATE_FAULT;
11869 }
11870
11871 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011872 return X86EMUL_CONTINUE;
11873}
11874
Yunhong Jiang64672c92016-06-13 14:19:59 -070011875#ifdef CONFIG_X86_64
11876/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11877static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11878 u64 divisor, u64 *result)
11879{
11880 u64 low = a << shift, high = a >> (64 - shift);
11881
11882 /* To avoid the overflow on divq */
11883 if (high >= divisor)
11884 return 1;
11885
11886 /* Low hold the result, high hold rem which is discarded */
11887 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11888 "rm" (divisor), "0" (low), "1" (high));
11889 *result = low;
11890
11891 return 0;
11892}
11893
11894static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11895{
11896 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011897 u64 tscl = rdtsc();
11898 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11899 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011900
11901 /* Convert to host delta tsc if tsc scaling is enabled */
11902 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11903 u64_shl_div_u64(delta_tsc,
11904 kvm_tsc_scaling_ratio_frac_bits,
11905 vcpu->arch.tsc_scaling_ratio,
11906 &delta_tsc))
11907 return -ERANGE;
11908
11909 /*
11910 * If the delta tsc can't fit in the 32 bit after the multi shift,
11911 * we can't use the preemption timer.
11912 * It's possible that it fits on later vmentries, but checking
11913 * on every vmentry is costly so we just use an hrtimer.
11914 */
11915 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11916 return -ERANGE;
11917
11918 vmx->hv_deadline_tsc = tscl + delta_tsc;
11919 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11920 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070011921
11922 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011923}
11924
11925static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11926{
11927 struct vcpu_vmx *vmx = to_vmx(vcpu);
11928 vmx->hv_deadline_tsc = -1;
11929 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11930 PIN_BASED_VMX_PREEMPTION_TIMER);
11931}
11932#endif
11933
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011934static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011935{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011936 if (ple_gap)
11937 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011938}
11939
Kai Huang843e4332015-01-28 10:54:28 +080011940static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11941 struct kvm_memory_slot *slot)
11942{
11943 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11944 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11945}
11946
11947static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11948 struct kvm_memory_slot *slot)
11949{
11950 kvm_mmu_slot_set_dirty(kvm, slot);
11951}
11952
11953static void vmx_flush_log_dirty(struct kvm *kvm)
11954{
11955 kvm_flush_pml_buffers(kvm);
11956}
11957
Bandan Dasc5f983f2017-05-05 15:25:14 -040011958static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
11959{
11960 struct vmcs12 *vmcs12;
11961 struct vcpu_vmx *vmx = to_vmx(vcpu);
11962 gpa_t gpa;
11963 struct page *page = NULL;
11964 u64 *pml_address;
11965
11966 if (is_guest_mode(vcpu)) {
11967 WARN_ON_ONCE(vmx->nested.pml_full);
11968
11969 /*
11970 * Check if PML is enabled for the nested guest.
11971 * Whether eptp bit 6 is set is already checked
11972 * as part of A/D emulation.
11973 */
11974 vmcs12 = get_vmcs12(vcpu);
11975 if (!nested_cpu_has_pml(vmcs12))
11976 return 0;
11977
Dan Carpenter47698862017-05-10 22:43:17 +030011978 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040011979 vmx->nested.pml_full = true;
11980 return 1;
11981 }
11982
11983 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
11984
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011985 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
11986 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040011987 return 0;
11988
11989 pml_address = kmap(page);
11990 pml_address[vmcs12->guest_pml_index--] = gpa;
11991 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011992 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011993 }
11994
11995 return 0;
11996}
11997
Kai Huang843e4332015-01-28 10:54:28 +080011998static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11999 struct kvm_memory_slot *memslot,
12000 gfn_t offset, unsigned long mask)
12001{
12002 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12003}
12004
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012005static void __pi_post_block(struct kvm_vcpu *vcpu)
12006{
12007 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12008 struct pi_desc old, new;
12009 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012010
12011 do {
12012 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012013 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12014 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012015
12016 dest = cpu_physical_id(vcpu->cpu);
12017
12018 if (x2apic_enabled())
12019 new.ndst = dest;
12020 else
12021 new.ndst = (dest << 8) & 0xFF00;
12022
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012023 /* set 'NV' to 'notification vector' */
12024 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012025 } while (cmpxchg64(&pi_desc->control, old.control,
12026 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012027
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012028 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12029 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012030 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012031 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012032 vcpu->pre_pcpu = -1;
12033 }
12034}
12035
Feng Wuefc64402015-09-18 22:29:51 +080012036/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012037 * This routine does the following things for vCPU which is going
12038 * to be blocked if VT-d PI is enabled.
12039 * - Store the vCPU to the wakeup list, so when interrupts happen
12040 * we can find the right vCPU to wake up.
12041 * - Change the Posted-interrupt descriptor as below:
12042 * 'NDST' <-- vcpu->pre_pcpu
12043 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12044 * - If 'ON' is set during this process, which means at least one
12045 * interrupt is posted for this vCPU, we cannot block it, in
12046 * this case, return 1, otherwise, return 0.
12047 *
12048 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012049static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012050{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012051 unsigned int dest;
12052 struct pi_desc old, new;
12053 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12054
12055 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012056 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12057 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012058 return 0;
12059
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012060 WARN_ON(irqs_disabled());
12061 local_irq_disable();
12062 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12063 vcpu->pre_pcpu = vcpu->cpu;
12064 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12065 list_add_tail(&vcpu->blocked_vcpu_list,
12066 &per_cpu(blocked_vcpu_on_cpu,
12067 vcpu->pre_pcpu));
12068 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12069 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012070
12071 do {
12072 old.control = new.control = pi_desc->control;
12073
Feng Wubf9f6ac2015-09-18 22:29:55 +080012074 WARN((pi_desc->sn == 1),
12075 "Warning: SN field of posted-interrupts "
12076 "is set before blocking\n");
12077
12078 /*
12079 * Since vCPU can be preempted during this process,
12080 * vcpu->cpu could be different with pre_pcpu, we
12081 * need to set pre_pcpu as the destination of wakeup
12082 * notification event, then we can find the right vCPU
12083 * to wakeup in wakeup handler if interrupts happen
12084 * when the vCPU is in blocked state.
12085 */
12086 dest = cpu_physical_id(vcpu->pre_pcpu);
12087
12088 if (x2apic_enabled())
12089 new.ndst = dest;
12090 else
12091 new.ndst = (dest << 8) & 0xFF00;
12092
12093 /* set 'NV' to 'wakeup vector' */
12094 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012095 } while (cmpxchg64(&pi_desc->control, old.control,
12096 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012097
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012098 /* We should not block the vCPU if an interrupt is posted for it. */
12099 if (pi_test_on(pi_desc) == 1)
12100 __pi_post_block(vcpu);
12101
12102 local_irq_enable();
12103 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012104}
12105
Yunhong Jiangbc225122016-06-13 14:19:58 -070012106static int vmx_pre_block(struct kvm_vcpu *vcpu)
12107{
12108 if (pi_pre_block(vcpu))
12109 return 1;
12110
Yunhong Jiang64672c92016-06-13 14:19:59 -070012111 if (kvm_lapic_hv_timer_in_use(vcpu))
12112 kvm_lapic_switch_to_sw_timer(vcpu);
12113
Yunhong Jiangbc225122016-06-13 14:19:58 -070012114 return 0;
12115}
12116
12117static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012118{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012119 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012120 return;
12121
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012122 WARN_ON(irqs_disabled());
12123 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012124 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012125 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012126}
12127
Yunhong Jiangbc225122016-06-13 14:19:58 -070012128static void vmx_post_block(struct kvm_vcpu *vcpu)
12129{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012130 if (kvm_x86_ops->set_hv_timer)
12131 kvm_lapic_switch_to_hv_timer(vcpu);
12132
Yunhong Jiangbc225122016-06-13 14:19:58 -070012133 pi_post_block(vcpu);
12134}
12135
Feng Wubf9f6ac2015-09-18 22:29:55 +080012136/*
Feng Wuefc64402015-09-18 22:29:51 +080012137 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12138 *
12139 * @kvm: kvm
12140 * @host_irq: host irq of the interrupt
12141 * @guest_irq: gsi of the interrupt
12142 * @set: set or unset PI
12143 * returns 0 on success, < 0 on failure
12144 */
12145static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12146 uint32_t guest_irq, bool set)
12147{
12148 struct kvm_kernel_irq_routing_entry *e;
12149 struct kvm_irq_routing_table *irq_rt;
12150 struct kvm_lapic_irq irq;
12151 struct kvm_vcpu *vcpu;
12152 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012153 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012154
12155 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012156 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12157 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012158 return 0;
12159
12160 idx = srcu_read_lock(&kvm->irq_srcu);
12161 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012162 if (guest_irq >= irq_rt->nr_rt_entries ||
12163 hlist_empty(&irq_rt->map[guest_irq])) {
12164 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12165 guest_irq, irq_rt->nr_rt_entries);
12166 goto out;
12167 }
Feng Wuefc64402015-09-18 22:29:51 +080012168
12169 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12170 if (e->type != KVM_IRQ_ROUTING_MSI)
12171 continue;
12172 /*
12173 * VT-d PI cannot support posting multicast/broadcast
12174 * interrupts to a vCPU, we still use interrupt remapping
12175 * for these kind of interrupts.
12176 *
12177 * For lowest-priority interrupts, we only support
12178 * those with single CPU as the destination, e.g. user
12179 * configures the interrupts via /proc/irq or uses
12180 * irqbalance to make the interrupts single-CPU.
12181 *
12182 * We will support full lowest-priority interrupt later.
12183 */
12184
Radim Krčmář371313132016-07-12 22:09:27 +020012185 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012186 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12187 /*
12188 * Make sure the IRTE is in remapped mode if
12189 * we don't handle it in posted mode.
12190 */
12191 ret = irq_set_vcpu_affinity(host_irq, NULL);
12192 if (ret < 0) {
12193 printk(KERN_INFO
12194 "failed to back to remapped mode, irq: %u\n",
12195 host_irq);
12196 goto out;
12197 }
12198
Feng Wuefc64402015-09-18 22:29:51 +080012199 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012200 }
Feng Wuefc64402015-09-18 22:29:51 +080012201
12202 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12203 vcpu_info.vector = irq.vector;
12204
Feng Wub6ce9782016-01-25 16:53:35 +080012205 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012206 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12207
12208 if (set)
12209 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080012210 else
Feng Wuefc64402015-09-18 22:29:51 +080012211 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012212
12213 if (ret < 0) {
12214 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12215 __func__);
12216 goto out;
12217 }
12218 }
12219
12220 ret = 0;
12221out:
12222 srcu_read_unlock(&kvm->irq_srcu, idx);
12223 return ret;
12224}
12225
Ashok Rajc45dcc72016-06-22 14:59:56 +080012226static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12227{
12228 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12229 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12230 FEATURE_CONTROL_LMCE;
12231 else
12232 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12233 ~FEATURE_CONTROL_LMCE;
12234}
12235
Ladi Prosek72d7b372017-10-11 16:54:41 +020012236static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12237{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012238 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12239 if (to_vmx(vcpu)->nested.nested_run_pending)
12240 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012241 return 1;
12242}
12243
Ladi Prosek0234bf82017-10-11 16:54:40 +020012244static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12245{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012246 struct vcpu_vmx *vmx = to_vmx(vcpu);
12247
12248 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12249 if (vmx->nested.smm.guest_mode)
12250 nested_vmx_vmexit(vcpu, -1, 0, 0);
12251
12252 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12253 vmx->nested.vmxon = false;
Ladi Prosek0234bf82017-10-11 16:54:40 +020012254 return 0;
12255}
12256
12257static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12258{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012259 struct vcpu_vmx *vmx = to_vmx(vcpu);
12260 int ret;
12261
12262 if (vmx->nested.smm.vmxon) {
12263 vmx->nested.vmxon = true;
12264 vmx->nested.smm.vmxon = false;
12265 }
12266
12267 if (vmx->nested.smm.guest_mode) {
12268 vcpu->arch.hflags &= ~HF_SMM_MASK;
12269 ret = enter_vmx_non_root_mode(vcpu, false);
12270 vcpu->arch.hflags |= HF_SMM_MASK;
12271 if (ret)
12272 return ret;
12273
12274 vmx->nested.smm.guest_mode = false;
12275 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020012276 return 0;
12277}
12278
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012279static int enable_smi_window(struct kvm_vcpu *vcpu)
12280{
12281 return 0;
12282}
12283
Kees Cook404f6aa2016-08-08 16:29:06 -070012284static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080012285 .cpu_has_kvm_support = cpu_has_kvm_support,
12286 .disabled_by_bios = vmx_disabled_by_bios,
12287 .hardware_setup = hardware_setup,
12288 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030012289 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012290 .hardware_enable = hardware_enable,
12291 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080012292 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020012293 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012294
12295 .vcpu_create = vmx_create_vcpu,
12296 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030012297 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012298
Avi Kivity04d2cc72007-09-10 18:10:54 +030012299 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012300 .vcpu_load = vmx_vcpu_load,
12301 .vcpu_put = vmx_vcpu_put,
12302
Paolo Bonzinia96036b2015-11-10 11:55:36 +010012303 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060012304 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012305 .get_msr = vmx_get_msr,
12306 .set_msr = vmx_set_msr,
12307 .get_segment_base = vmx_get_segment_base,
12308 .get_segment = vmx_get_segment,
12309 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020012310 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012311 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020012312 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020012313 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030012314 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012315 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012316 .set_cr3 = vmx_set_cr3,
12317 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012318 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012319 .get_idt = vmx_get_idt,
12320 .set_idt = vmx_set_idt,
12321 .get_gdt = vmx_get_gdt,
12322 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010012323 .get_dr6 = vmx_get_dr6,
12324 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030012325 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010012326 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030012327 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012328 .get_rflags = vmx_get_rflags,
12329 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080012330
Avi Kivity6aa8b732006-12-10 02:21:36 -080012331 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012332
Avi Kivity6aa8b732006-12-10 02:21:36 -080012333 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020012334 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012335 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040012336 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12337 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020012338 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030012339 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012340 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020012341 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030012342 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020012343 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012344 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010012345 .get_nmi_mask = vmx_get_nmi_mask,
12346 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012347 .enable_nmi_window = enable_nmi_window,
12348 .enable_irq_window = enable_irq_window,
12349 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080012350 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080012351 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030012352 .get_enable_apicv = vmx_get_enable_apicv,
12353 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012354 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010012355 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012356 .hwapic_irr_update = vmx_hwapic_irr_update,
12357 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080012358 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12359 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012360
Izik Eiduscbc94022007-10-25 00:29:55 +020012361 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080012362 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080012363 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030012364
Avi Kivity586f9602010-11-18 13:09:54 +020012365 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020012366
Sheng Yang17cc3932010-01-05 19:02:27 +080012367 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080012368
12369 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080012370
12371 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000012372 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020012373
12374 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080012375
12376 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012377
12378 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020012379
12380 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012381
12382 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080012383 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000012384 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080012385 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020012386 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012387
12388 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012389
12390 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080012391
12392 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12393 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12394 .flush_log_dirty = vmx_flush_log_dirty,
12395 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040012396 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020012397
Feng Wubf9f6ac2015-09-18 22:29:55 +080012398 .pre_block = vmx_pre_block,
12399 .post_block = vmx_post_block,
12400
Wei Huang25462f72015-06-19 15:45:05 +020012401 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080012402
12403 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070012404
12405#ifdef CONFIG_X86_64
12406 .set_hv_timer = vmx_set_hv_timer,
12407 .cancel_hv_timer = vmx_cancel_hv_timer,
12408#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080012409
12410 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012411
Ladi Prosek72d7b372017-10-11 16:54:41 +020012412 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012413 .pre_enter_smm = vmx_pre_enter_smm,
12414 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012415 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012416};
12417
12418static int __init vmx_init(void)
12419{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012420 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
12421 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030012422 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012423 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080012424
Dave Young2965faa2015-09-09 15:38:55 -070012425#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012426 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12427 crash_vmclear_local_loaded_vmcss);
12428#endif
12429
He, Qingfdef3ad2007-04-30 09:45:24 +030012430 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080012431}
12432
12433static void __exit vmx_exit(void)
12434{
Dave Young2965faa2015-09-09 15:38:55 -070012435#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053012436 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012437 synchronize_rcu();
12438#endif
12439
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080012440 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080012441}
12442
12443module_init(vmx_init)
12444module_exit(vmx_exit)