blob: f427723dc7db34fab153b4faecbbb767b48f7e06 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf41245002014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
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 Kiszkaf41245002014-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 Kiszkaf41245002014-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 Kivity313dbd42008-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 Mattsonc7c2c7092017-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 Kiszkaf41245002014-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 Kiszkaf41245002014-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 Kiszkaf41245002014-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'Elafa61f7522013-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'Elafa61f7522013-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 Kiszkaf41245002014-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
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003229/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003230 * Reads an msr value (of 'msr_index') into 'pdata'.
3231 * Returns 0 on success, non-0 otherwise.
3232 * Assumes vcpu_load() was already called.
3233 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003234static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003236 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003237 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003238
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003239 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003240#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003242 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003243 break;
3244 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003245 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003247 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003248 vmx_load_host_state(vmx);
3249 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003250 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003251#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003252 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003253 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303254 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003255 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003256 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003257 case MSR_IA32_SPEC_CTRL:
3258 if (!msr_info->host_initiated &&
3259 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3260 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3261 return 1;
3262
3263 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3264 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003265 case MSR_IA32_ARCH_CAPABILITIES:
3266 if (!msr_info->host_initiated &&
3267 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3268 return 1;
3269 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3270 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003271 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003272 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003273 break;
3274 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003275 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276 break;
3277 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003278 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003279 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003280 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003281 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003282 (!msr_info->host_initiated &&
3283 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003284 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003285 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003286 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003287 case MSR_IA32_MCG_EXT_CTL:
3288 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003289 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003290 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003291 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003292 msr_info->data = vcpu->arch.mcg_ext_ctl;
3293 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003294 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003295 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003296 break;
3297 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3298 if (!nested_vmx_allowed(vcpu))
3299 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003300 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003301 case MSR_IA32_XSS:
3302 if (!vmx_xsaves_supported())
3303 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003304 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003305 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003306 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003307 if (!msr_info->host_initiated &&
3308 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003309 return 1;
3310 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003311 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003312 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003313 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003314 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003315 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003316 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003317 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003318 }
3319
Avi Kivity6aa8b732006-12-10 02:21:36 -08003320 return 0;
3321}
3322
Jan Kiszkacae50132014-01-04 18:47:22 +01003323static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3324
Avi Kivity6aa8b732006-12-10 02:21:36 -08003325/*
3326 * Writes msr value into into the appropriate "register".
3327 * Returns 0 on success, non-0 otherwise.
3328 * Assumes vcpu_load() was already called.
3329 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003330static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003331{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003332 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003333 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003334 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003335 u32 msr_index = msr_info->index;
3336 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003337
Avi Kivity6aa8b732006-12-10 02:21:36 -08003338 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003339 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003340 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003341 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003342#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003343 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003344 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003345 vmcs_writel(GUEST_FS_BASE, data);
3346 break;
3347 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003348 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003349 vmcs_writel(GUEST_GS_BASE, data);
3350 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003351 case MSR_KERNEL_GS_BASE:
3352 vmx_load_host_state(vmx);
3353 vmx->msr_guest_kernel_gs_base = data;
3354 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003355#endif
3356 case MSR_IA32_SYSENTER_CS:
3357 vmcs_write32(GUEST_SYSENTER_CS, data);
3358 break;
3359 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003360 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003361 break;
3362 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003363 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003364 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003365 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003366 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003367 (!msr_info->host_initiated &&
3368 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003369 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003370 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003371 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003372 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003373 vmcs_write64(GUEST_BNDCFGS, data);
3374 break;
3375 case MSR_IA32_TSC:
3376 kvm_write_tsc(vcpu, msr_info);
3377 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003378 case MSR_IA32_SPEC_CTRL:
3379 if (!msr_info->host_initiated &&
3380 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3381 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3382 return 1;
3383
3384 /* The STIBP bit doesn't fault even if it's not advertised */
3385 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
3386 return 1;
3387
3388 vmx->spec_ctrl = data;
3389
3390 if (!data)
3391 break;
3392
3393 /*
3394 * For non-nested:
3395 * When it's written (to non-zero) for the first time, pass
3396 * it through.
3397 *
3398 * For nested:
3399 * The handling of the MSR bitmap for L2 guests is done in
3400 * nested_vmx_merge_msr_bitmap. We should not touch the
3401 * vmcs02.msr_bitmap here since it gets completely overwritten
3402 * in the merging. We update the vmcs01 here for L1 as well
3403 * since it will end up touching the MSR anyway now.
3404 */
3405 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3406 MSR_IA32_SPEC_CTRL,
3407 MSR_TYPE_RW);
3408 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003409 case MSR_IA32_PRED_CMD:
3410 if (!msr_info->host_initiated &&
3411 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB) &&
3412 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3413 return 1;
3414
3415 if (data & ~PRED_CMD_IBPB)
3416 return 1;
3417
3418 if (!data)
3419 break;
3420
3421 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3422
3423 /*
3424 * For non-nested:
3425 * When it's written (to non-zero) for the first time, pass
3426 * it through.
3427 *
3428 * For nested:
3429 * The handling of the MSR bitmap for L2 guests is done in
3430 * nested_vmx_merge_msr_bitmap. We should not touch the
3431 * vmcs02.msr_bitmap here since it gets completely overwritten
3432 * in the merging.
3433 */
3434 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3435 MSR_TYPE_W);
3436 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003437 case MSR_IA32_ARCH_CAPABILITIES:
3438 if (!msr_info->host_initiated)
3439 return 1;
3440 vmx->arch_capabilities = data;
3441 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003442 case MSR_IA32_CR_PAT:
3443 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003444 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3445 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003446 vmcs_write64(GUEST_IA32_PAT, data);
3447 vcpu->arch.pat = data;
3448 break;
3449 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003450 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003451 break;
Will Auldba904632012-11-29 12:42:50 -08003452 case MSR_IA32_TSC_ADJUST:
3453 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003454 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003455 case MSR_IA32_MCG_EXT_CTL:
3456 if ((!msr_info->host_initiated &&
3457 !(to_vmx(vcpu)->msr_ia32_feature_control &
3458 FEATURE_CONTROL_LMCE)) ||
3459 (data & ~MCG_EXT_CTL_LMCE_EN))
3460 return 1;
3461 vcpu->arch.mcg_ext_ctl = data;
3462 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003463 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003464 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003465 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003466 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3467 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003468 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003469 if (msr_info->host_initiated && data == 0)
3470 vmx_leave_nested(vcpu);
3471 break;
3472 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003473 if (!msr_info->host_initiated)
3474 return 1; /* they are read-only */
3475 if (!nested_vmx_allowed(vcpu))
3476 return 1;
3477 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003478 case MSR_IA32_XSS:
3479 if (!vmx_xsaves_supported())
3480 return 1;
3481 /*
3482 * The only supported bit as of Skylake is bit 8, but
3483 * it is not supported on KVM.
3484 */
3485 if (data != 0)
3486 return 1;
3487 vcpu->arch.ia32_xss = data;
3488 if (vcpu->arch.ia32_xss != host_xss)
3489 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3490 vcpu->arch.ia32_xss, host_xss);
3491 else
3492 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3493 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003494 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003495 if (!msr_info->host_initiated &&
3496 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003497 return 1;
3498 /* Check reserved bit, higher 32 bits should be zero */
3499 if ((data >> 32) != 0)
3500 return 1;
3501 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003502 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003503 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003504 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003505 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003506 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003507 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3508 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003509 ret = kvm_set_shared_msr(msr->index, msr->data,
3510 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003511 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003512 if (ret)
3513 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003514 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003515 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003516 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003517 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003518 }
3519
Eddie Dong2cc51562007-05-21 07:28:09 +03003520 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003521}
3522
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003523static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003524{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003525 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3526 switch (reg) {
3527 case VCPU_REGS_RSP:
3528 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3529 break;
3530 case VCPU_REGS_RIP:
3531 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3532 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003533 case VCPU_EXREG_PDPTR:
3534 if (enable_ept)
3535 ept_save_pdptrs(vcpu);
3536 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003537 default:
3538 break;
3539 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003540}
3541
Avi Kivity6aa8b732006-12-10 02:21:36 -08003542static __init int cpu_has_kvm_support(void)
3543{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003544 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545}
3546
3547static __init int vmx_disabled_by_bios(void)
3548{
3549 u64 msr;
3550
3551 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003552 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003553 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003554 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3555 && tboot_enabled())
3556 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003557 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003558 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003559 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003560 && !tboot_enabled()) {
3561 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003562 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003563 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003564 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003565 /* launched w/o TXT and VMX disabled */
3566 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3567 && !tboot_enabled())
3568 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003569 }
3570
3571 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003572}
3573
Dongxiao Xu7725b892010-05-11 18:29:38 +08003574static void kvm_cpu_vmxon(u64 addr)
3575{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003576 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003577 intel_pt_handle_vmx(1);
3578
Dongxiao Xu7725b892010-05-11 18:29:38 +08003579 asm volatile (ASM_VMX_VMXON_RAX
3580 : : "a"(&addr), "m"(addr)
3581 : "memory", "cc");
3582}
3583
Radim Krčmář13a34e02014-08-28 15:13:03 +02003584static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003585{
3586 int cpu = raw_smp_processor_id();
3587 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003588 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003589
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003590 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003591 return -EBUSY;
3592
Nadav Har'Eld462b812011-05-24 15:26:10 +03003593 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003594 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3595 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003596
3597 /*
3598 * Now we can enable the vmclear operation in kdump
3599 * since the loaded_vmcss_on_cpu list on this cpu
3600 * has been initialized.
3601 *
3602 * Though the cpu is not in VMX operation now, there
3603 * is no problem to enable the vmclear operation
3604 * for the loaded_vmcss_on_cpu list is empty!
3605 */
3606 crash_enable_local_vmclear(cpu);
3607
Avi Kivity6aa8b732006-12-10 02:21:36 -08003608 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003609
3610 test_bits = FEATURE_CONTROL_LOCKED;
3611 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3612 if (tboot_enabled())
3613 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3614
3615 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003616 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003617 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3618 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003619 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02003620 if (enable_ept)
3621 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02003622
3623 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003624}
3625
Nadav Har'Eld462b812011-05-24 15:26:10 +03003626static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003627{
3628 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003629 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003630
Nadav Har'Eld462b812011-05-24 15:26:10 +03003631 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3632 loaded_vmcss_on_cpu_link)
3633 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003634}
3635
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003636
3637/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3638 * tricks.
3639 */
3640static void kvm_cpu_vmxoff(void)
3641{
3642 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003643
3644 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003645 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003646}
3647
Radim Krčmář13a34e02014-08-28 15:13:03 +02003648static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003649{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003650 vmclear_local_loaded_vmcss();
3651 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003652}
3653
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003654static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003655 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003656{
3657 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003658 u32 ctl = ctl_min | ctl_opt;
3659
3660 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3661
3662 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3663 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3664
3665 /* Ensure minimum (required) set of control bits are supported. */
3666 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003667 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003668
3669 *result = ctl;
3670 return 0;
3671}
3672
Avi Kivity110312c2010-12-21 12:54:20 +02003673static __init bool allow_1_setting(u32 msr, u32 ctl)
3674{
3675 u32 vmx_msr_low, vmx_msr_high;
3676
3677 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3678 return vmx_msr_high & ctl;
3679}
3680
Yang, Sheng002c7f72007-07-31 14:23:01 +03003681static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003682{
3683 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003684 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003685 u32 _pin_based_exec_control = 0;
3686 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003687 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003688 u32 _vmexit_control = 0;
3689 u32 _vmentry_control = 0;
3690
Raghavendra K T10166742012-02-07 23:19:20 +05303691 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003692#ifdef CONFIG_X86_64
3693 CPU_BASED_CR8_LOAD_EXITING |
3694 CPU_BASED_CR8_STORE_EXITING |
3695#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003696 CPU_BASED_CR3_LOAD_EXITING |
3697 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08003698 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003699 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003700 CPU_BASED_USE_TSC_OFFSETING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003701 CPU_BASED_INVLPG_EXITING |
3702 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003703
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003704 if (!kvm_mwait_in_guest())
3705 min |= CPU_BASED_MWAIT_EXITING |
3706 CPU_BASED_MONITOR_EXITING;
3707
Sheng Yangf78e0e22007-10-29 09:40:42 +08003708 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003709 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003710 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003711 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3712 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003713 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003714#ifdef CONFIG_X86_64
3715 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3716 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3717 ~CPU_BASED_CR8_STORE_EXITING;
3718#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003719 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003720 min2 = 0;
3721 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003722 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003723 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003724 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003725 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003726 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003727 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02003728 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00003729 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003730 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003731 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003732 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003733 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003734 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02003735 SECONDARY_EXEC_RDSEED_EXITING |
3736 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003737 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04003738 SECONDARY_EXEC_TSC_SCALING |
3739 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08003740 if (adjust_vmx_controls(min2, opt2,
3741 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003742 &_cpu_based_2nd_exec_control) < 0)
3743 return -EIO;
3744 }
3745#ifndef CONFIG_X86_64
3746 if (!(_cpu_based_2nd_exec_control &
3747 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3748 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3749#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003750
3751 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3752 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003753 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003754 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3755 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003756
Wanpeng Li61f1dd92017-10-18 16:02:19 -07003757 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
3758 &vmx_capability.ept, &vmx_capability.vpid);
3759
Sheng Yangd56f5462008-04-25 10:13:16 +08003760 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003761 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3762 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003763 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3764 CPU_BASED_CR3_STORE_EXITING |
3765 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07003766 } else if (vmx_capability.ept) {
3767 vmx_capability.ept = 0;
3768 pr_warn_once("EPT CAP should not exist if not support "
3769 "1-setting enable EPT VM-execution control\n");
3770 }
3771 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
3772 vmx_capability.vpid) {
3773 vmx_capability.vpid = 0;
3774 pr_warn_once("VPID CAP should not exist if not support "
3775 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08003776 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003777
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003778 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003779#ifdef CONFIG_X86_64
3780 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3781#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003782 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003783 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003784 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3785 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003786 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003787
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01003788 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3789 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3790 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003791 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3792 &_pin_based_exec_control) < 0)
3793 return -EIO;
3794
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003795 if (cpu_has_broken_vmx_preemption_timer())
3796 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003797 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003798 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003799 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3800
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003801 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003802 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003803 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3804 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003805 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003806
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003807 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003808
3809 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3810 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003811 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003812
3813#ifdef CONFIG_X86_64
3814 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3815 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003816 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003817#endif
3818
3819 /* Require Write-Back (WB) memory type for VMCS accesses. */
3820 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003821 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003822
Yang, Sheng002c7f72007-07-31 14:23:01 +03003823 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003824 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003825 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003826 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003827
Yang, Sheng002c7f72007-07-31 14:23:01 +03003828 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3829 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003830 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003831 vmcs_conf->vmexit_ctrl = _vmexit_control;
3832 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003833
Avi Kivity110312c2010-12-21 12:54:20 +02003834 cpu_has_load_ia32_efer =
3835 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3836 VM_ENTRY_LOAD_IA32_EFER)
3837 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3838 VM_EXIT_LOAD_IA32_EFER);
3839
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003840 cpu_has_load_perf_global_ctrl =
3841 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3842 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3843 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3844 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3845
3846 /*
3847 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003848 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003849 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3850 *
3851 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3852 *
3853 * AAK155 (model 26)
3854 * AAP115 (model 30)
3855 * AAT100 (model 37)
3856 * BC86,AAY89,BD102 (model 44)
3857 * BA97 (model 46)
3858 *
3859 */
3860 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3861 switch (boot_cpu_data.x86_model) {
3862 case 26:
3863 case 30:
3864 case 37:
3865 case 44:
3866 case 46:
3867 cpu_has_load_perf_global_ctrl = false;
3868 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3869 "does not work properly. Using workaround\n");
3870 break;
3871 default:
3872 break;
3873 }
3874 }
3875
Borislav Petkov782511b2016-04-04 22:25:03 +02003876 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003877 rdmsrl(MSR_IA32_XSS, host_xss);
3878
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003879 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003880}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003881
3882static struct vmcs *alloc_vmcs_cpu(int cpu)
3883{
3884 int node = cpu_to_node(cpu);
3885 struct page *pages;
3886 struct vmcs *vmcs;
3887
Vlastimil Babka96db8002015-09-08 15:03:50 -07003888 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003889 if (!pages)
3890 return NULL;
3891 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003892 memset(vmcs, 0, vmcs_config.size);
3893 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003894 return vmcs;
3895}
3896
Avi Kivity6aa8b732006-12-10 02:21:36 -08003897static void free_vmcs(struct vmcs *vmcs)
3898{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003899 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003900}
3901
Nadav Har'Eld462b812011-05-24 15:26:10 +03003902/*
3903 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3904 */
3905static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3906{
3907 if (!loaded_vmcs->vmcs)
3908 return;
3909 loaded_vmcs_clear(loaded_vmcs);
3910 free_vmcs(loaded_vmcs->vmcs);
3911 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003912 if (loaded_vmcs->msr_bitmap)
3913 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07003914 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003915}
3916
Paolo Bonzinif21f1652018-01-11 12:16:15 +01003917static struct vmcs *alloc_vmcs(void)
3918{
3919 return alloc_vmcs_cpu(raw_smp_processor_id());
3920}
3921
3922static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3923{
3924 loaded_vmcs->vmcs = alloc_vmcs();
3925 if (!loaded_vmcs->vmcs)
3926 return -ENOMEM;
3927
3928 loaded_vmcs->shadow_vmcs = NULL;
3929 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003930
3931 if (cpu_has_vmx_msr_bitmap()) {
3932 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
3933 if (!loaded_vmcs->msr_bitmap)
3934 goto out_vmcs;
3935 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
3936 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01003937 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003938
3939out_vmcs:
3940 free_loaded_vmcs(loaded_vmcs);
3941 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01003942}
3943
Sam Ravnborg39959582007-06-01 00:47:13 -07003944static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003945{
3946 int cpu;
3947
Zachary Amsden3230bb42009-09-29 11:38:37 -10003948 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003949 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003950 per_cpu(vmxarea, cpu) = NULL;
3951 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003952}
3953
Jim Mattsond37f4262017-12-22 12:12:16 -08003954enum vmcs_field_width {
3955 VMCS_FIELD_WIDTH_U16 = 0,
3956 VMCS_FIELD_WIDTH_U64 = 1,
3957 VMCS_FIELD_WIDTH_U32 = 2,
3958 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07003959};
3960
Jim Mattsond37f4262017-12-22 12:12:16 -08003961static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07003962{
3963 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08003964 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07003965 return (field >> 13) & 0x3 ;
3966}
3967
3968static inline int vmcs_field_readonly(unsigned long field)
3969{
3970 return (((field >> 10) & 0x3) == 1);
3971}
3972
Bandan Dasfe2b2012014-04-21 15:20:14 -04003973static void init_vmcs_shadow_fields(void)
3974{
3975 int i, j;
3976
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003977 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
3978 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08003979 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003980 (i + 1 == max_shadow_read_only_fields ||
3981 shadow_read_only_fields[i + 1] != field + 1))
3982 pr_err("Missing field from shadow_read_only_field %x\n",
3983 field + 1);
3984
3985 clear_bit(field, vmx_vmread_bitmap);
3986#ifdef CONFIG_X86_64
3987 if (field & 1)
3988 continue;
3989#endif
3990 if (j < i)
3991 shadow_read_only_fields[j] = field;
3992 j++;
3993 }
3994 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04003995
3996 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003997 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08003998 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003999 (i + 1 == max_shadow_read_write_fields ||
4000 shadow_read_write_fields[i + 1] != field + 1))
4001 pr_err("Missing field from shadow_read_write_field %x\n",
4002 field + 1);
4003
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004004 /*
4005 * PML and the preemption timer can be emulated, but the
4006 * processor cannot vmwrite to fields that don't exist
4007 * on bare metal.
4008 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004009 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004010 case GUEST_PML_INDEX:
4011 if (!cpu_has_vmx_pml())
4012 continue;
4013 break;
4014 case VMX_PREEMPTION_TIMER_VALUE:
4015 if (!cpu_has_vmx_preemption_timer())
4016 continue;
4017 break;
4018 case GUEST_INTR_STATUS:
4019 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004020 continue;
4021 break;
4022 default:
4023 break;
4024 }
4025
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004026 clear_bit(field, vmx_vmwrite_bitmap);
4027 clear_bit(field, vmx_vmread_bitmap);
4028#ifdef CONFIG_X86_64
4029 if (field & 1)
4030 continue;
4031#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004032 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004033 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004034 j++;
4035 }
4036 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004037}
4038
Avi Kivity6aa8b732006-12-10 02:21:36 -08004039static __init int alloc_kvm_area(void)
4040{
4041 int cpu;
4042
Zachary Amsden3230bb42009-09-29 11:38:37 -10004043 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004044 struct vmcs *vmcs;
4045
4046 vmcs = alloc_vmcs_cpu(cpu);
4047 if (!vmcs) {
4048 free_kvm_area();
4049 return -ENOMEM;
4050 }
4051
4052 per_cpu(vmxarea, cpu) = vmcs;
4053 }
4054 return 0;
4055}
4056
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004057static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004058 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004059{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004060 if (!emulate_invalid_guest_state) {
4061 /*
4062 * CS and SS RPL should be equal during guest entry according
4063 * to VMX spec, but in reality it is not always so. Since vcpu
4064 * is in the middle of the transition from real mode to
4065 * protected mode it is safe to assume that RPL 0 is a good
4066 * default value.
4067 */
4068 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004069 save->selector &= ~SEGMENT_RPL_MASK;
4070 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004071 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004072 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004073 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004074}
4075
4076static void enter_pmode(struct kvm_vcpu *vcpu)
4077{
4078 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004079 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004080
Gleb Natapovd99e4152012-12-20 16:57:45 +02004081 /*
4082 * Update real mode segment cache. It may be not up-to-date if sement
4083 * register was written while vcpu was in a guest mode.
4084 */
4085 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4086 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4087 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4088 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4089 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4090 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4091
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004092 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004093
Avi Kivity2fb92db2011-04-27 19:42:18 +03004094 vmx_segment_cache_clear(vmx);
4095
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004096 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004097
4098 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004099 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4100 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004101 vmcs_writel(GUEST_RFLAGS, flags);
4102
Rusty Russell66aee912007-07-17 23:34:16 +10004103 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4104 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004105
4106 update_exception_bitmap(vcpu);
4107
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004108 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4109 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4110 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4111 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4112 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4113 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004114}
4115
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004116static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004117{
Mathias Krause772e0312012-08-30 01:30:19 +02004118 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004119 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004120
Gleb Natapovd99e4152012-12-20 16:57:45 +02004121 var.dpl = 0x3;
4122 if (seg == VCPU_SREG_CS)
4123 var.type = 0x3;
4124
4125 if (!emulate_invalid_guest_state) {
4126 var.selector = var.base >> 4;
4127 var.base = var.base & 0xffff0;
4128 var.limit = 0xffff;
4129 var.g = 0;
4130 var.db = 0;
4131 var.present = 1;
4132 var.s = 1;
4133 var.l = 0;
4134 var.unusable = 0;
4135 var.type = 0x3;
4136 var.avl = 0;
4137 if (save->base & 0xf)
4138 printk_once(KERN_WARNING "kvm: segment base is not "
4139 "paragraph aligned when entering "
4140 "protected mode (seg=%d)", seg);
4141 }
4142
4143 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004144 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004145 vmcs_write32(sf->limit, var.limit);
4146 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004147}
4148
4149static void enter_rmode(struct kvm_vcpu *vcpu)
4150{
4151 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004152 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004153
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004154 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4155 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4156 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4157 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4158 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004159 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4160 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004161
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004162 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004163
Gleb Natapov776e58e2011-03-13 12:34:27 +02004164 /*
4165 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004166 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004167 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004168 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004169 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4170 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004171
Avi Kivity2fb92db2011-04-27 19:42:18 +03004172 vmx_segment_cache_clear(vmx);
4173
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004174 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004175 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004176 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4177
4178 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004179 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004180
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004181 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004182
4183 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004184 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004185 update_exception_bitmap(vcpu);
4186
Gleb Natapovd99e4152012-12-20 16:57:45 +02004187 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4188 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4189 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4190 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4191 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4192 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004193
Eddie Dong8668a3c2007-10-10 14:26:45 +08004194 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004195}
4196
Amit Shah401d10d2009-02-20 22:53:37 +05304197static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4198{
4199 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004200 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4201
4202 if (!msr)
4203 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304204
Avi Kivity44ea2b12009-09-06 15:55:37 +03004205 /*
4206 * Force kernel_gs_base reloading before EFER changes, as control
4207 * of this msr depends on is_long_mode().
4208 */
4209 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004210 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304211 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004212 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304213 msr->data = efer;
4214 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004215 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304216
4217 msr->data = efer & ~EFER_LME;
4218 }
4219 setup_msrs(vmx);
4220}
4221
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004222#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004223
4224static void enter_lmode(struct kvm_vcpu *vcpu)
4225{
4226 u32 guest_tr_ar;
4227
Avi Kivity2fb92db2011-04-27 19:42:18 +03004228 vmx_segment_cache_clear(to_vmx(vcpu));
4229
Avi Kivity6aa8b732006-12-10 02:21:36 -08004230 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004231 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004232 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4233 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004234 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004235 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4236 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004237 }
Avi Kivityda38f432010-07-06 11:30:49 +03004238 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004239}
4240
4241static void exit_lmode(struct kvm_vcpu *vcpu)
4242{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004243 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004244 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004245}
4246
4247#endif
4248
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004249static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4250 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004251{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004252 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004253 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4254 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004255 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004256 } else {
4257 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004258 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004259}
4260
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004261static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004262{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004263 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004264}
4265
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004266static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4267{
4268 if (enable_ept)
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004269 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004270}
4271
Avi Kivitye8467fd2009-12-29 18:43:06 +02004272static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4273{
4274 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4275
4276 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4277 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4278}
4279
Avi Kivityaff48ba2010-12-05 18:56:11 +02004280static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4281{
4282 if (enable_ept && is_paging(vcpu))
4283 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4284 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4285}
4286
Anthony Liguori25c4c272007-04-27 09:29:21 +03004287static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004288{
Avi Kivityfc78f512009-12-07 12:16:48 +02004289 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4290
4291 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4292 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004293}
4294
Sheng Yang14394422008-04-28 12:24:45 +08004295static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4296{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004297 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4298
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004299 if (!test_bit(VCPU_EXREG_PDPTR,
4300 (unsigned long *)&vcpu->arch.regs_dirty))
4301 return;
4302
Sheng Yang14394422008-04-28 12:24:45 +08004303 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004304 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4305 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4306 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4307 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004308 }
4309}
4310
Avi Kivity8f5d5492009-05-31 18:41:29 +03004311static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4312{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004313 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4314
Avi Kivity8f5d5492009-05-31 18:41:29 +03004315 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004316 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4317 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4318 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4319 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004320 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004321
4322 __set_bit(VCPU_EXREG_PDPTR,
4323 (unsigned long *)&vcpu->arch.regs_avail);
4324 __set_bit(VCPU_EXREG_PDPTR,
4325 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004326}
4327
David Matlack38991522016-11-29 18:14:08 -08004328static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4329{
4330 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4331 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4332 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4333
4334 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
4335 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4336 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4337 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4338
4339 return fixed_bits_valid(val, fixed0, fixed1);
4340}
4341
4342static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4343{
4344 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4345 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4346
4347 return fixed_bits_valid(val, fixed0, fixed1);
4348}
4349
4350static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4351{
4352 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0;
4353 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1;
4354
4355 return fixed_bits_valid(val, fixed0, fixed1);
4356}
4357
4358/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4359#define nested_guest_cr4_valid nested_cr4_valid
4360#define nested_host_cr4_valid nested_cr4_valid
4361
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004362static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004363
4364static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4365 unsigned long cr0,
4366 struct kvm_vcpu *vcpu)
4367{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004368 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4369 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004370 if (!(cr0 & X86_CR0_PG)) {
4371 /* From paging/starting to nonpaging */
4372 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004373 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004374 (CPU_BASED_CR3_LOAD_EXITING |
4375 CPU_BASED_CR3_STORE_EXITING));
4376 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004377 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004378 } else if (!is_paging(vcpu)) {
4379 /* From nonpaging to paging */
4380 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004381 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004382 ~(CPU_BASED_CR3_LOAD_EXITING |
4383 CPU_BASED_CR3_STORE_EXITING));
4384 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004385 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004386 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004387
4388 if (!(cr0 & X86_CR0_WP))
4389 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004390}
4391
Avi Kivity6aa8b732006-12-10 02:21:36 -08004392static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4393{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004394 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004395 unsigned long hw_cr0;
4396
Gleb Natapov50378782013-02-04 16:00:28 +02004397 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004398 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004399 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004400 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004401 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004402
Gleb Natapov218e7632013-01-21 15:36:45 +02004403 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4404 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004405
Gleb Natapov218e7632013-01-21 15:36:45 +02004406 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4407 enter_rmode(vcpu);
4408 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004409
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004410#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004411 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004412 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004413 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004414 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004415 exit_lmode(vcpu);
4416 }
4417#endif
4418
Avi Kivity089d0342009-03-23 18:26:32 +02004419 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004420 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4421
Avi Kivity6aa8b732006-12-10 02:21:36 -08004422 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004423 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004424 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004425
4426 /* depends on vcpu->arch.cr0 to be set to a new value */
4427 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004428}
4429
Yu Zhang855feb62017-08-24 20:27:55 +08004430static int get_ept_level(struct kvm_vcpu *vcpu)
4431{
4432 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4433 return 5;
4434 return 4;
4435}
4436
Peter Feiner995f00a2017-06-30 17:26:32 -07004437static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004438{
Yu Zhang855feb62017-08-24 20:27:55 +08004439 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004440
Yu Zhang855feb62017-08-24 20:27:55 +08004441 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004442
Peter Feiner995f00a2017-06-30 17:26:32 -07004443 if (enable_ept_ad_bits &&
4444 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004445 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004446 eptp |= (root_hpa & PAGE_MASK);
4447
4448 return eptp;
4449}
4450
Avi Kivity6aa8b732006-12-10 02:21:36 -08004451static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4452{
Sheng Yang14394422008-04-28 12:24:45 +08004453 unsigned long guest_cr3;
4454 u64 eptp;
4455
4456 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004457 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004458 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004459 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004460 if (is_paging(vcpu) || is_guest_mode(vcpu))
4461 guest_cr3 = kvm_read_cr3(vcpu);
4462 else
4463 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004464 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004465 }
4466
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004467 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08004468 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004469}
4470
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004471static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004472{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004473 /*
4474 * Pass through host's Machine Check Enable value to hw_cr4, which
4475 * is in force while we are in guest mode. Do not let guests control
4476 * this bit, even if host CR4.MCE == 0.
4477 */
4478 unsigned long hw_cr4 =
4479 (cr4_read_shadow() & X86_CR4_MCE) |
4480 (cr4 & ~X86_CR4_MCE) |
4481 (to_vmx(vcpu)->rmode.vm86_active ?
4482 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004483
Paolo Bonzini0367f202016-07-12 10:44:55 +02004484 if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) {
4485 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4486 SECONDARY_EXEC_DESC);
4487 hw_cr4 &= ~X86_CR4_UMIP;
4488 } else
4489 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4490 SECONDARY_EXEC_DESC);
4491
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004492 if (cr4 & X86_CR4_VMXE) {
4493 /*
4494 * To use VMXON (and later other VMX instructions), a guest
4495 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4496 * So basically the check on whether to allow nested VMX
4497 * is here.
4498 */
4499 if (!nested_vmx_allowed(vcpu))
4500 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004501 }
David Matlack38991522016-11-29 18:14:08 -08004502
4503 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004504 return 1;
4505
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004506 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004507 if (enable_ept) {
4508 if (!is_paging(vcpu)) {
4509 hw_cr4 &= ~X86_CR4_PAE;
4510 hw_cr4 |= X86_CR4_PSE;
4511 } else if (!(cr4 & X86_CR4_PAE)) {
4512 hw_cr4 &= ~X86_CR4_PAE;
4513 }
4514 }
Sheng Yang14394422008-04-28 12:24:45 +08004515
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004516 if (!enable_unrestricted_guest && !is_paging(vcpu))
4517 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004518 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4519 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4520 * to be manually disabled when guest switches to non-paging
4521 * mode.
4522 *
4523 * If !enable_unrestricted_guest, the CPU is always running
4524 * with CR0.PG=1 and CR4 needs to be modified.
4525 * If enable_unrestricted_guest, the CPU automatically
4526 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004527 */
Huaitong Handdba2622016-03-22 16:51:15 +08004528 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004529
Sheng Yang14394422008-04-28 12:24:45 +08004530 vmcs_writel(CR4_READ_SHADOW, cr4);
4531 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004532 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004533}
4534
Avi Kivity6aa8b732006-12-10 02:21:36 -08004535static void vmx_get_segment(struct kvm_vcpu *vcpu,
4536 struct kvm_segment *var, int seg)
4537{
Avi Kivitya9179492011-01-03 14:28:52 +02004538 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004539 u32 ar;
4540
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004541 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004542 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004543 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004544 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004545 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004546 var->base = vmx_read_guest_seg_base(vmx, seg);
4547 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4548 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004549 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004550 var->base = vmx_read_guest_seg_base(vmx, seg);
4551 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4552 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4553 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004554 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004555 var->type = ar & 15;
4556 var->s = (ar >> 4) & 1;
4557 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004558 /*
4559 * Some userspaces do not preserve unusable property. Since usable
4560 * segment has to be present according to VMX spec we can use present
4561 * property to amend userspace bug by making unusable segment always
4562 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4563 * segment as unusable.
4564 */
4565 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004566 var->avl = (ar >> 12) & 1;
4567 var->l = (ar >> 13) & 1;
4568 var->db = (ar >> 14) & 1;
4569 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004570}
4571
Avi Kivitya9179492011-01-03 14:28:52 +02004572static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4573{
Avi Kivitya9179492011-01-03 14:28:52 +02004574 struct kvm_segment s;
4575
4576 if (to_vmx(vcpu)->rmode.vm86_active) {
4577 vmx_get_segment(vcpu, &s, seg);
4578 return s.base;
4579 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004580 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004581}
4582
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004583static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004584{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004585 struct vcpu_vmx *vmx = to_vmx(vcpu);
4586
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004587 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004588 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004589 else {
4590 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004591 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004592 }
Avi Kivity69c73022011-03-07 15:26:44 +02004593}
4594
Avi Kivity653e3102007-05-07 10:55:37 +03004595static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004596{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004597 u32 ar;
4598
Avi Kivityf0495f92012-06-07 17:06:10 +03004599 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004600 ar = 1 << 16;
4601 else {
4602 ar = var->type & 15;
4603 ar |= (var->s & 1) << 4;
4604 ar |= (var->dpl & 3) << 5;
4605 ar |= (var->present & 1) << 7;
4606 ar |= (var->avl & 1) << 12;
4607 ar |= (var->l & 1) << 13;
4608 ar |= (var->db & 1) << 14;
4609 ar |= (var->g & 1) << 15;
4610 }
Avi Kivity653e3102007-05-07 10:55:37 +03004611
4612 return ar;
4613}
4614
4615static void vmx_set_segment(struct kvm_vcpu *vcpu,
4616 struct kvm_segment *var, int seg)
4617{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004618 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004619 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004620
Avi Kivity2fb92db2011-04-27 19:42:18 +03004621 vmx_segment_cache_clear(vmx);
4622
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004623 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4624 vmx->rmode.segs[seg] = *var;
4625 if (seg == VCPU_SREG_TR)
4626 vmcs_write16(sf->selector, var->selector);
4627 else if (var->s)
4628 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004629 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004630 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004631
Avi Kivity653e3102007-05-07 10:55:37 +03004632 vmcs_writel(sf->base, var->base);
4633 vmcs_write32(sf->limit, var->limit);
4634 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004635
4636 /*
4637 * Fix the "Accessed" bit in AR field of segment registers for older
4638 * qemu binaries.
4639 * IA32 arch specifies that at the time of processor reset the
4640 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004641 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004642 * state vmexit when "unrestricted guest" mode is turned on.
4643 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4644 * tree. Newer qemu binaries with that qemu fix would not need this
4645 * kvm hack.
4646 */
4647 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004648 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004649
Gleb Natapovf924d662012-12-12 19:10:55 +02004650 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004651
4652out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004653 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004654}
4655
Avi Kivity6aa8b732006-12-10 02:21:36 -08004656static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4657{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004658 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004659
4660 *db = (ar >> 14) & 1;
4661 *l = (ar >> 13) & 1;
4662}
4663
Gleb Natapov89a27f42010-02-16 10:51:48 +02004664static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004665{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004666 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4667 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004668}
4669
Gleb Natapov89a27f42010-02-16 10:51:48 +02004670static void vmx_set_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 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4673 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004674}
4675
Gleb Natapov89a27f42010-02-16 10:51:48 +02004676static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004677{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004678 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4679 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004680}
4681
Gleb Natapov89a27f42010-02-16 10:51:48 +02004682static void vmx_set_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 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4685 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004686}
4687
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004688static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4689{
4690 struct kvm_segment var;
4691 u32 ar;
4692
4693 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004694 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004695 if (seg == VCPU_SREG_CS)
4696 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004697 ar = vmx_segment_access_rights(&var);
4698
4699 if (var.base != (var.selector << 4))
4700 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004701 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004702 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004703 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004704 return false;
4705
4706 return true;
4707}
4708
4709static bool code_segment_valid(struct kvm_vcpu *vcpu)
4710{
4711 struct kvm_segment cs;
4712 unsigned int cs_rpl;
4713
4714 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004715 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004716
Avi Kivity1872a3f2009-01-04 23:26:52 +02004717 if (cs.unusable)
4718 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004719 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004720 return false;
4721 if (!cs.s)
4722 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004723 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004724 if (cs.dpl > cs_rpl)
4725 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004726 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004727 if (cs.dpl != cs_rpl)
4728 return false;
4729 }
4730 if (!cs.present)
4731 return false;
4732
4733 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4734 return true;
4735}
4736
4737static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4738{
4739 struct kvm_segment ss;
4740 unsigned int ss_rpl;
4741
4742 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004743 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004744
Avi Kivity1872a3f2009-01-04 23:26:52 +02004745 if (ss.unusable)
4746 return true;
4747 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004748 return false;
4749 if (!ss.s)
4750 return false;
4751 if (ss.dpl != ss_rpl) /* DPL != RPL */
4752 return false;
4753 if (!ss.present)
4754 return false;
4755
4756 return true;
4757}
4758
4759static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4760{
4761 struct kvm_segment var;
4762 unsigned int rpl;
4763
4764 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004765 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004766
Avi Kivity1872a3f2009-01-04 23:26:52 +02004767 if (var.unusable)
4768 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004769 if (!var.s)
4770 return false;
4771 if (!var.present)
4772 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004773 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004774 if (var.dpl < rpl) /* DPL < RPL */
4775 return false;
4776 }
4777
4778 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4779 * rights flags
4780 */
4781 return true;
4782}
4783
4784static bool tr_valid(struct kvm_vcpu *vcpu)
4785{
4786 struct kvm_segment tr;
4787
4788 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4789
Avi Kivity1872a3f2009-01-04 23:26:52 +02004790 if (tr.unusable)
4791 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004792 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004793 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004794 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004795 return false;
4796 if (!tr.present)
4797 return false;
4798
4799 return true;
4800}
4801
4802static bool ldtr_valid(struct kvm_vcpu *vcpu)
4803{
4804 struct kvm_segment ldtr;
4805
4806 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4807
Avi Kivity1872a3f2009-01-04 23:26:52 +02004808 if (ldtr.unusable)
4809 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004810 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004811 return false;
4812 if (ldtr.type != 2)
4813 return false;
4814 if (!ldtr.present)
4815 return false;
4816
4817 return true;
4818}
4819
4820static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4821{
4822 struct kvm_segment cs, ss;
4823
4824 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4825 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4826
Nadav Amitb32a9912015-03-29 16:33:04 +03004827 return ((cs.selector & SEGMENT_RPL_MASK) ==
4828 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004829}
4830
4831/*
4832 * Check if guest state is valid. Returns true if valid, false if
4833 * not.
4834 * We assume that registers are always usable
4835 */
4836static bool guest_state_valid(struct kvm_vcpu *vcpu)
4837{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004838 if (enable_unrestricted_guest)
4839 return true;
4840
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004841 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004842 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004843 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4844 return false;
4845 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4846 return false;
4847 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4848 return false;
4849 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4850 return false;
4851 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4852 return false;
4853 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4854 return false;
4855 } else {
4856 /* protected mode guest state checks */
4857 if (!cs_ss_rpl_check(vcpu))
4858 return false;
4859 if (!code_segment_valid(vcpu))
4860 return false;
4861 if (!stack_segment_valid(vcpu))
4862 return false;
4863 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4864 return false;
4865 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4866 return false;
4867 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4868 return false;
4869 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4870 return false;
4871 if (!tr_valid(vcpu))
4872 return false;
4873 if (!ldtr_valid(vcpu))
4874 return false;
4875 }
4876 /* TODO:
4877 * - Add checks on RIP
4878 * - Add checks on RFLAGS
4879 */
4880
4881 return true;
4882}
4883
Jim Mattson5fa99cb2017-07-06 16:33:07 -07004884static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
4885{
4886 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
4887}
4888
Mike Dayd77c26f2007-10-08 09:02:08 -04004889static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004890{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004891 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004892 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004893 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004894
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004895 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004896 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004897 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4898 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004899 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004900 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004901 r = kvm_write_guest_page(kvm, fn++, &data,
4902 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004903 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004904 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004905 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4906 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004907 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004908 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4909 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004910 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004911 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004912 r = kvm_write_guest_page(kvm, fn, &data,
4913 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4914 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004915out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004916 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004917 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004918}
4919
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004920static int init_rmode_identity_map(struct kvm *kvm)
4921{
Tang Chenf51770e2014-09-16 18:41:59 +08004922 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004923 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004924 u32 tmp;
4925
Tang Chena255d472014-09-16 18:41:58 +08004926 /* Protect kvm->arch.ept_identity_pagetable_done. */
4927 mutex_lock(&kvm->slots_lock);
4928
Tang Chenf51770e2014-09-16 18:41:59 +08004929 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004930 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004931
David Hildenbrandd8a6e362017-08-24 20:51:34 +02004932 if (!kvm->arch.ept_identity_map_addr)
4933 kvm->arch.ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004934 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004935
David Hildenbrandd8a6e362017-08-24 20:51:34 +02004936 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4937 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08004938 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004939 goto out2;
4940
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004941 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004942 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4943 if (r < 0)
4944 goto out;
4945 /* Set up identity-mapping pagetable for EPT in real mode */
4946 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4947 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4948 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4949 r = kvm_write_guest_page(kvm, identity_map_pfn,
4950 &tmp, i * sizeof(tmp), sizeof(tmp));
4951 if (r < 0)
4952 goto out;
4953 }
4954 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004955
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004956out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004957 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004958
4959out2:
4960 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004961 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004962}
4963
Avi Kivity6aa8b732006-12-10 02:21:36 -08004964static void seg_setup(int seg)
4965{
Mathias Krause772e0312012-08-30 01:30:19 +02004966 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004967 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004968
4969 vmcs_write16(sf->selector, 0);
4970 vmcs_writel(sf->base, 0);
4971 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004972 ar = 0x93;
4973 if (seg == VCPU_SREG_CS)
4974 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004975
4976 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004977}
4978
Sheng Yangf78e0e22007-10-29 09:40:42 +08004979static int alloc_apic_access_page(struct kvm *kvm)
4980{
Xiao Guangrong44841412012-09-07 14:14:20 +08004981 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004982 int r = 0;
4983
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004984 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004985 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004986 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004987 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4988 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004989 if (r)
4990 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004991
Tang Chen73a6d942014-09-11 13:38:00 +08004992 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004993 if (is_error_page(page)) {
4994 r = -EFAULT;
4995 goto out;
4996 }
4997
Tang Chenc24ae0d2014-09-24 15:57:58 +08004998 /*
4999 * Do not pin the page in memory, so that memory hot-unplug
5000 * is able to migrate it.
5001 */
5002 put_page(page);
5003 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005004out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005005 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005006 return r;
5007}
5008
Wanpeng Li991e7a02015-09-16 17:30:05 +08005009static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005010{
5011 int vpid;
5012
Avi Kivity919818a2009-03-23 18:01:29 +02005013 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005014 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005015 spin_lock(&vmx_vpid_lock);
5016 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005017 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005018 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005019 else
5020 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005021 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005022 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005023}
5024
Wanpeng Li991e7a02015-09-16 17:30:05 +08005025static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005026{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005027 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005028 return;
5029 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005030 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005031 spin_unlock(&vmx_vpid_lock);
5032}
5033
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005034static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5035 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005036{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005037 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005038
5039 if (!cpu_has_vmx_msr_bitmap())
5040 return;
5041
5042 /*
5043 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5044 * have the write-low and read-high bitmap offsets the wrong way round.
5045 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5046 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005047 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005048 if (type & MSR_TYPE_R)
5049 /* read-low */
5050 __clear_bit(msr, msr_bitmap + 0x000 / f);
5051
5052 if (type & MSR_TYPE_W)
5053 /* write-low */
5054 __clear_bit(msr, msr_bitmap + 0x800 / f);
5055
Sheng Yang25c5f222008-03-28 13:18:56 +08005056 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5057 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005058 if (type & MSR_TYPE_R)
5059 /* read-high */
5060 __clear_bit(msr, msr_bitmap + 0x400 / f);
5061
5062 if (type & MSR_TYPE_W)
5063 /* write-high */
5064 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5065
5066 }
5067}
5068
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005069static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5070 u32 msr, int type)
5071{
5072 int f = sizeof(unsigned long);
5073
5074 if (!cpu_has_vmx_msr_bitmap())
5075 return;
5076
5077 /*
5078 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5079 * have the write-low and read-high bitmap offsets the wrong way round.
5080 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5081 */
5082 if (msr <= 0x1fff) {
5083 if (type & MSR_TYPE_R)
5084 /* read-low */
5085 __set_bit(msr, msr_bitmap + 0x000 / f);
5086
5087 if (type & MSR_TYPE_W)
5088 /* write-low */
5089 __set_bit(msr, msr_bitmap + 0x800 / f);
5090
5091 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5092 msr &= 0x1fff;
5093 if (type & MSR_TYPE_R)
5094 /* read-high */
5095 __set_bit(msr, msr_bitmap + 0x400 / f);
5096
5097 if (type & MSR_TYPE_W)
5098 /* write-high */
5099 __set_bit(msr, msr_bitmap + 0xc00 / f);
5100
5101 }
5102}
5103
5104static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5105 u32 msr, int type, bool value)
5106{
5107 if (value)
5108 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5109 else
5110 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5111}
5112
Wincy Vanf2b93282015-02-03 23:56:03 +08005113/*
5114 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5115 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5116 */
5117static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5118 unsigned long *msr_bitmap_nested,
5119 u32 msr, int type)
5120{
5121 int f = sizeof(unsigned long);
5122
Wincy Vanf2b93282015-02-03 23:56:03 +08005123 /*
5124 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5125 * have the write-low and read-high bitmap offsets the wrong way round.
5126 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5127 */
5128 if (msr <= 0x1fff) {
5129 if (type & MSR_TYPE_R &&
5130 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5131 /* read-low */
5132 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5133
5134 if (type & MSR_TYPE_W &&
5135 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5136 /* write-low */
5137 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5138
5139 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5140 msr &= 0x1fff;
5141 if (type & MSR_TYPE_R &&
5142 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5143 /* read-high */
5144 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5145
5146 if (type & MSR_TYPE_W &&
5147 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5148 /* write-high */
5149 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5150
5151 }
5152}
5153
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005154static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005155{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005156 u8 mode = 0;
5157
5158 if (cpu_has_secondary_exec_ctrls() &&
5159 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5160 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5161 mode |= MSR_BITMAP_MODE_X2APIC;
5162 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5163 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5164 }
5165
5166 if (is_long_mode(vcpu))
5167 mode |= MSR_BITMAP_MODE_LM;
5168
5169 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005170}
5171
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005172#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5173
5174static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5175 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005176{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005177 int msr;
5178
5179 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5180 unsigned word = msr / BITS_PER_LONG;
5181 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5182 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005183 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005184
5185 if (mode & MSR_BITMAP_MODE_X2APIC) {
5186 /*
5187 * TPR reads and writes can be virtualized even if virtual interrupt
5188 * delivery is not in use.
5189 */
5190 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5191 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5192 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5193 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5194 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5195 }
5196 }
5197}
5198
5199static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5200{
5201 struct vcpu_vmx *vmx = to_vmx(vcpu);
5202 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5203 u8 mode = vmx_msr_bitmap_mode(vcpu);
5204 u8 changed = mode ^ vmx->msr_bitmap_mode;
5205
5206 if (!changed)
5207 return;
5208
5209 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5210 !(mode & MSR_BITMAP_MODE_LM));
5211
5212 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5213 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5214
5215 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005216}
5217
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005218static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005219{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005220 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005221}
5222
David Matlackc9f04402017-08-01 14:00:40 -07005223static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5224{
5225 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5226 gfn_t gfn;
5227
5228 /*
5229 * Don't need to mark the APIC access page dirty; it is never
5230 * written to by the CPU during APIC virtualization.
5231 */
5232
5233 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5234 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5235 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5236 }
5237
5238 if (nested_cpu_has_posted_intr(vmcs12)) {
5239 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5240 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5241 }
5242}
5243
5244
David Hildenbrand6342c502017-01-25 11:58:58 +01005245static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005246{
5247 struct vcpu_vmx *vmx = to_vmx(vcpu);
5248 int max_irr;
5249 void *vapic_page;
5250 u16 status;
5251
David Matlackc9f04402017-08-01 14:00:40 -07005252 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5253 return;
Wincy Van705699a2015-02-03 23:58:17 +08005254
David Matlackc9f04402017-08-01 14:00:40 -07005255 vmx->nested.pi_pending = false;
5256 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5257 return;
Wincy Van705699a2015-02-03 23:58:17 +08005258
David Matlackc9f04402017-08-01 14:00:40 -07005259 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5260 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005261 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005262 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5263 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005264 kunmap(vmx->nested.virtual_apic_page);
5265
5266 status = vmcs_read16(GUEST_INTR_STATUS);
5267 if ((u8)max_irr > ((u8)status & 0xff)) {
5268 status &= ~0xff;
5269 status |= (u8)max_irr;
5270 vmcs_write16(GUEST_INTR_STATUS, status);
5271 }
5272 }
David Matlackc9f04402017-08-01 14:00:40 -07005273
5274 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005275}
5276
Wincy Van06a55242017-04-28 13:13:59 +08005277static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5278 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005279{
5280#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005281 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5282
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005283 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005284 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005285 * The vector of interrupt to be delivered to vcpu had
5286 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005287 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005288 * Following cases will be reached in this block, and
5289 * we always send a notification event in all cases as
5290 * explained below.
5291 *
5292 * Case 1: vcpu keeps in non-root mode. Sending a
5293 * notification event posts the interrupt to vcpu.
5294 *
5295 * Case 2: vcpu exits to root mode and is still
5296 * runnable. PIR will be synced to vIRR before the
5297 * next vcpu entry. Sending a notification event in
5298 * this case has no effect, as vcpu is not in root
5299 * mode.
5300 *
5301 * Case 3: vcpu exits to root mode and is blocked.
5302 * vcpu_block() has already synced PIR to vIRR and
5303 * never blocks vcpu if vIRR is not cleared. Therefore,
5304 * a blocked vcpu here does not wait for any requested
5305 * interrupts in PIR, and sending a notification event
5306 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005307 */
Feng Wu28b835d2015-09-18 22:29:54 +08005308
Wincy Van06a55242017-04-28 13:13:59 +08005309 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005310 return true;
5311 }
5312#endif
5313 return false;
5314}
5315
Wincy Van705699a2015-02-03 23:58:17 +08005316static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5317 int vector)
5318{
5319 struct vcpu_vmx *vmx = to_vmx(vcpu);
5320
5321 if (is_guest_mode(vcpu) &&
5322 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005323 /*
5324 * If a posted intr is not recognized by hardware,
5325 * we will accomplish it in the next vmentry.
5326 */
5327 vmx->nested.pi_pending = true;
5328 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005329 /* the PIR and ON have been set by L1. */
5330 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5331 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005332 return 0;
5333 }
5334 return -1;
5335}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005336/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005337 * Send interrupt to vcpu via posted interrupt way.
5338 * 1. If target vcpu is running(non-root mode), send posted interrupt
5339 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5340 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5341 * interrupt from PIR in next vmentry.
5342 */
5343static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5344{
5345 struct vcpu_vmx *vmx = to_vmx(vcpu);
5346 int r;
5347
Wincy Van705699a2015-02-03 23:58:17 +08005348 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5349 if (!r)
5350 return;
5351
Yang Zhanga20ed542013-04-11 19:25:15 +08005352 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5353 return;
5354
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005355 /* If a previous notification has sent the IPI, nothing to do. */
5356 if (pi_test_and_set_on(&vmx->pi_desc))
5357 return;
5358
Wincy Van06a55242017-04-28 13:13:59 +08005359 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005360 kvm_vcpu_kick(vcpu);
5361}
5362
Avi Kivity6aa8b732006-12-10 02:21:36 -08005363/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005364 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5365 * will not change in the lifetime of the guest.
5366 * Note that host-state that does change is set elsewhere. E.g., host-state
5367 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5368 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005369static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005370{
5371 u32 low32, high32;
5372 unsigned long tmpl;
5373 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005374 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005375
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005376 cr0 = read_cr0();
5377 WARN_ON(cr0 & X86_CR0_TS);
5378 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005379
5380 /*
5381 * Save the most likely value for this task's CR3 in the VMCS.
5382 * We can't use __get_current_cr3_fast() because we're not atomic.
5383 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005384 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005385 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005386 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005387
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005388 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005389 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005390 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005391 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005392
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005393 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005394#ifdef CONFIG_X86_64
5395 /*
5396 * Load null selectors, so we can avoid reloading them in
5397 * __vmx_load_host_state(), in case userspace uses the null selectors
5398 * too (the expected case).
5399 */
5400 vmcs_write16(HOST_DS_SELECTOR, 0);
5401 vmcs_write16(HOST_ES_SELECTOR, 0);
5402#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005403 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5404 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005405#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005406 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5407 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5408
Juergen Gross87930012017-09-04 12:25:27 +02005409 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005410 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005411 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005412
Avi Kivity83287ea422012-09-16 15:10:57 +03005413 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005414
5415 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5416 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5417 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5418 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5419
5420 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5421 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5422 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5423 }
5424}
5425
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005426static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5427{
5428 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5429 if (enable_ept)
5430 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005431 if (is_guest_mode(&vmx->vcpu))
5432 vmx->vcpu.arch.cr4_guest_owned_bits &=
5433 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005434 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5435}
5436
Yang Zhang01e439b2013-04-11 19:25:12 +08005437static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5438{
5439 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5440
Andrey Smetanind62caab2015-11-10 15:36:33 +03005441 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005442 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005443
5444 if (!enable_vnmi)
5445 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5446
Yunhong Jiang64672c92016-06-13 14:19:59 -07005447 /* Enable the preemption timer dynamically */
5448 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005449 return pin_based_exec_ctrl;
5450}
5451
Andrey Smetanind62caab2015-11-10 15:36:33 +03005452static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5453{
5454 struct vcpu_vmx *vmx = to_vmx(vcpu);
5455
5456 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005457 if (cpu_has_secondary_exec_ctrls()) {
5458 if (kvm_vcpu_apicv_active(vcpu))
5459 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5460 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5461 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5462 else
5463 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5464 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5465 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5466 }
5467
5468 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005469 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005470}
5471
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005472static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5473{
5474 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005475
5476 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5477 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5478
Paolo Bonzini35754c92015-07-29 12:05:37 +02005479 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005480 exec_control &= ~CPU_BASED_TPR_SHADOW;
5481#ifdef CONFIG_X86_64
5482 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5483 CPU_BASED_CR8_LOAD_EXITING;
5484#endif
5485 }
5486 if (!enable_ept)
5487 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5488 CPU_BASED_CR3_LOAD_EXITING |
5489 CPU_BASED_INVLPG_EXITING;
5490 return exec_control;
5491}
5492
Jim Mattson45ec3682017-08-23 16:32:04 -07005493static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005494{
Jim Mattson45ec3682017-08-23 16:32:04 -07005495 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005496 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005497}
5498
Jim Mattson75f4fc82017-08-23 16:32:03 -07005499static bool vmx_rdseed_supported(void)
5500{
5501 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005502 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005503}
5504
Paolo Bonzini80154d72017-08-24 13:55:35 +02005505static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005506{
Paolo Bonzini80154d72017-08-24 13:55:35 +02005507 struct kvm_vcpu *vcpu = &vmx->vcpu;
5508
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005509 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005510
Paolo Bonzini80154d72017-08-24 13:55:35 +02005511 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005512 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5513 if (vmx->vpid == 0)
5514 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5515 if (!enable_ept) {
5516 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5517 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005518 /* Enable INVPCID for non-ept guests may cause performance regression. */
5519 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005520 }
5521 if (!enable_unrestricted_guest)
5522 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5523 if (!ple_gap)
5524 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02005525 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005526 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5527 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005528 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005529
5530 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5531 * in vmx_set_cr4. */
5532 exec_control &= ~SECONDARY_EXEC_DESC;
5533
Abel Gordonabc4fc52013-04-18 14:35:25 +03005534 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5535 (handle_vmptrld).
5536 We can NOT enable shadow_vmcs here because we don't have yet
5537 a current VMCS12
5538 */
5539 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005540
5541 if (!enable_pml)
5542 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005543
Paolo Bonzini3db13482017-08-24 14:48:03 +02005544 if (vmx_xsaves_supported()) {
5545 /* Exposing XSAVES only when XSAVE is exposed */
5546 bool xsaves_enabled =
5547 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5548 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5549
5550 if (!xsaves_enabled)
5551 exec_control &= ~SECONDARY_EXEC_XSAVES;
5552
5553 if (nested) {
5554 if (xsaves_enabled)
5555 vmx->nested.nested_vmx_secondary_ctls_high |=
5556 SECONDARY_EXEC_XSAVES;
5557 else
5558 vmx->nested.nested_vmx_secondary_ctls_high &=
5559 ~SECONDARY_EXEC_XSAVES;
5560 }
5561 }
5562
Paolo Bonzini80154d72017-08-24 13:55:35 +02005563 if (vmx_rdtscp_supported()) {
5564 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5565 if (!rdtscp_enabled)
5566 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5567
5568 if (nested) {
5569 if (rdtscp_enabled)
5570 vmx->nested.nested_vmx_secondary_ctls_high |=
5571 SECONDARY_EXEC_RDTSCP;
5572 else
5573 vmx->nested.nested_vmx_secondary_ctls_high &=
5574 ~SECONDARY_EXEC_RDTSCP;
5575 }
5576 }
5577
5578 if (vmx_invpcid_supported()) {
5579 /* Exposing INVPCID only when PCID is exposed */
5580 bool invpcid_enabled =
5581 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5582 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5583
5584 if (!invpcid_enabled) {
5585 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5586 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5587 }
5588
5589 if (nested) {
5590 if (invpcid_enabled)
5591 vmx->nested.nested_vmx_secondary_ctls_high |=
5592 SECONDARY_EXEC_ENABLE_INVPCID;
5593 else
5594 vmx->nested.nested_vmx_secondary_ctls_high &=
5595 ~SECONDARY_EXEC_ENABLE_INVPCID;
5596 }
5597 }
5598
Jim Mattson45ec3682017-08-23 16:32:04 -07005599 if (vmx_rdrand_supported()) {
5600 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5601 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005602 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005603
5604 if (nested) {
5605 if (rdrand_enabled)
5606 vmx->nested.nested_vmx_secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005607 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005608 else
5609 vmx->nested.nested_vmx_secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005610 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005611 }
5612 }
5613
Jim Mattson75f4fc82017-08-23 16:32:03 -07005614 if (vmx_rdseed_supported()) {
5615 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5616 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005617 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005618
5619 if (nested) {
5620 if (rdseed_enabled)
5621 vmx->nested.nested_vmx_secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005622 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005623 else
5624 vmx->nested.nested_vmx_secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005625 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005626 }
5627 }
5628
Paolo Bonzini80154d72017-08-24 13:55:35 +02005629 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005630}
5631
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005632static void ept_set_mmio_spte_mask(void)
5633{
5634 /*
5635 * EPT Misconfigurations can be generated if the value of bits 2:0
5636 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005637 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07005638 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5639 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005640}
5641
Wanpeng Lif53cd632014-12-02 19:14:58 +08005642#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005643/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005644 * Sets up the vmcs for emulated real mode.
5645 */
David Hildenbrand12d79912017-08-24 20:51:26 +02005646static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005647{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005648#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005649 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005650#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005651 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005652
Abel Gordon4607c2d2013-04-18 14:35:55 +03005653 if (enable_shadow_vmcs) {
5654 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5655 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5656 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005657 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005658 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005659
Avi Kivity6aa8b732006-12-10 02:21:36 -08005660 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5661
Avi Kivity6aa8b732006-12-10 02:21:36 -08005662 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005663 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005664 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005665
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005666 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005667
Dan Williamsdfa169b2016-06-02 11:17:24 -07005668 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02005669 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005670 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02005671 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07005672 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005673
Andrey Smetanind62caab2015-11-10 15:36:33 +03005674 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005675 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5676 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5677 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5678 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5679
5680 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005681
Li RongQing0bcf2612015-12-03 13:29:34 +08005682 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005683 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005684 }
5685
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005686 if (ple_gap) {
5687 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005688 vmx->ple_window = ple_window;
5689 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005690 }
5691
Xiao Guangrongc3707952011-07-12 03:28:04 +08005692 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5693 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005694 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5695
Avi Kivity9581d442010-10-19 16:46:55 +02005696 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5697 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005698 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005699#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005700 rdmsrl(MSR_FS_BASE, a);
5701 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5702 rdmsrl(MSR_GS_BASE, a);
5703 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5704#else
5705 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5706 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5707#endif
5708
Bandan Das2a499e42017-08-03 15:54:41 -04005709 if (cpu_has_vmx_vmfunc())
5710 vmcs_write64(VM_FUNCTION_CONTROL, 0);
5711
Eddie Dong2cc51562007-05-21 07:28:09 +03005712 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5713 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005714 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005715 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005716 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005717
Radim Krčmář74545702015-04-27 15:11:25 +02005718 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5719 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005720
Paolo Bonzini03916db2014-07-24 14:21:57 +02005721 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005722 u32 index = vmx_msr_index[i];
5723 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005724 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005725
5726 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5727 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005728 if (wrmsr_safe(index, data_low, data_high) < 0)
5729 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005730 vmx->guest_msrs[j].index = i;
5731 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005732 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005733 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005734 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005735
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01005736 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
5737 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02005738
5739 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005740
5741 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005742 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005743
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005744 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5745 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5746
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005747 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005748
Wanpeng Lif53cd632014-12-02 19:14:58 +08005749 if (vmx_xsaves_supported())
5750 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5751
Peter Feiner4e595162016-07-07 14:49:58 -07005752 if (enable_pml) {
5753 ASSERT(vmx->pml_pg);
5754 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5755 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5756 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005757}
5758
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005759static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005760{
5761 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005762 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005763 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005764
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005765 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01005766 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005767
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005768 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005769 kvm_set_cr8(vcpu, 0);
5770
5771 if (!init_event) {
5772 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5773 MSR_IA32_APICBASE_ENABLE;
5774 if (kvm_vcpu_is_reset_bsp(vcpu))
5775 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5776 apic_base_msr.host_initiated = true;
5777 kvm_set_apic_base(vcpu, &apic_base_msr);
5778 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005779
Avi Kivity2fb92db2011-04-27 19:42:18 +03005780 vmx_segment_cache_clear(vmx);
5781
Avi Kivity5706be02008-08-20 15:07:31 +03005782 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005783 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005784 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005785
5786 seg_setup(VCPU_SREG_DS);
5787 seg_setup(VCPU_SREG_ES);
5788 seg_setup(VCPU_SREG_FS);
5789 seg_setup(VCPU_SREG_GS);
5790 seg_setup(VCPU_SREG_SS);
5791
5792 vmcs_write16(GUEST_TR_SELECTOR, 0);
5793 vmcs_writel(GUEST_TR_BASE, 0);
5794 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5795 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5796
5797 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5798 vmcs_writel(GUEST_LDTR_BASE, 0);
5799 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5800 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5801
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005802 if (!init_event) {
5803 vmcs_write32(GUEST_SYSENTER_CS, 0);
5804 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5805 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5806 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5807 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005808
Wanpeng Lic37c2872017-11-20 14:52:21 -08005809 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01005810 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005811
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005812 vmcs_writel(GUEST_GDTR_BASE, 0);
5813 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5814
5815 vmcs_writel(GUEST_IDTR_BASE, 0);
5816 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5817
Anthony Liguori443381a2010-12-06 10:53:38 -06005818 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005819 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005820 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07005821 if (kvm_mpx_supported())
5822 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005823
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005824 setup_msrs(vmx);
5825
Avi Kivity6aa8b732006-12-10 02:21:36 -08005826 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5827
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005828 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005829 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005830 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005831 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005832 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005833 vmcs_write32(TPR_THRESHOLD, 0);
5834 }
5835
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005836 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005837
Sheng Yang2384d2b2008-01-17 15:14:33 +08005838 if (vmx->vpid != 0)
5839 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5840
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005841 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005842 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005843 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005844 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005845 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005846
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005847 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005848
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005849 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005850}
5851
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005852/*
5853 * In nested virtualization, check if L1 asked to exit on external interrupts.
5854 * For most existing hypervisors, this will always return true.
5855 */
5856static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5857{
5858 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5859 PIN_BASED_EXT_INTR_MASK;
5860}
5861
Bandan Das77b0f5d2014-04-19 18:17:45 -04005862/*
5863 * In nested virtualization, check if L1 has set
5864 * VM_EXIT_ACK_INTR_ON_EXIT
5865 */
5866static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5867{
5868 return get_vmcs12(vcpu)->vm_exit_controls &
5869 VM_EXIT_ACK_INTR_ON_EXIT;
5870}
5871
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005872static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5873{
5874 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5875 PIN_BASED_NMI_EXITING;
5876}
5877
Jan Kiszkac9a79532014-03-07 20:03:15 +01005878static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005879{
Paolo Bonzini47c01522016-12-19 11:44:07 +01005880 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5881 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005882}
5883
Jan Kiszkac9a79532014-03-07 20:03:15 +01005884static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005885{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005886 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005887 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01005888 enable_irq_window(vcpu);
5889 return;
5890 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005891
Paolo Bonzini47c01522016-12-19 11:44:07 +01005892 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5893 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005894}
5895
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005896static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005897{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005898 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005899 uint32_t intr;
5900 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005901
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005902 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005903
Avi Kivityfa89a812008-09-01 15:57:51 +03005904 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005905 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005906 int inc_eip = 0;
5907 if (vcpu->arch.interrupt.soft)
5908 inc_eip = vcpu->arch.event_exit_inst_len;
5909 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005910 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005911 return;
5912 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005913 intr = irq | INTR_INFO_VALID_MASK;
5914 if (vcpu->arch.interrupt.soft) {
5915 intr |= INTR_TYPE_SOFT_INTR;
5916 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5917 vmx->vcpu.arch.event_exit_inst_len);
5918 } else
5919 intr |= INTR_TYPE_EXT_INTR;
5920 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005921}
5922
Sheng Yangf08864b2008-05-15 18:23:25 +08005923static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5924{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005925 struct vcpu_vmx *vmx = to_vmx(vcpu);
5926
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005927 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005928 /*
5929 * Tracking the NMI-blocked state in software is built upon
5930 * finding the next open IRQ window. This, in turn, depends on
5931 * well-behaving guests: They have to keep IRQs disabled at
5932 * least as long as the NMI handler runs. Otherwise we may
5933 * cause NMI nesting, maybe breaking the guest. But as this is
5934 * highly unlikely, we can live with the residual risk.
5935 */
5936 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
5937 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5938 }
5939
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005940 ++vcpu->stat.nmi_injections;
5941 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005942
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005943 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005944 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005945 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005946 return;
5947 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005948
Sheng Yangf08864b2008-05-15 18:23:25 +08005949 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5950 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005951}
5952
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005953static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5954{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005955 struct vcpu_vmx *vmx = to_vmx(vcpu);
5956 bool masked;
5957
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005958 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005959 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005960 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02005961 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005962 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
5963 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5964 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005965}
5966
5967static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5968{
5969 struct vcpu_vmx *vmx = to_vmx(vcpu);
5970
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005971 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005972 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
5973 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
5974 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5975 }
5976 } else {
5977 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5978 if (masked)
5979 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5980 GUEST_INTR_STATE_NMI);
5981 else
5982 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5983 GUEST_INTR_STATE_NMI);
5984 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005985}
5986
Jan Kiszka2505dc92013-04-14 12:12:47 +02005987static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5988{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005989 if (to_vmx(vcpu)->nested.nested_run_pending)
5990 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005991
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005992 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005993 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
5994 return 0;
5995
Jan Kiszka2505dc92013-04-14 12:12:47 +02005996 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5997 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5998 | GUEST_INTR_STATE_NMI));
5999}
6000
Gleb Natapov78646122009-03-23 12:12:11 +02006001static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6002{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006003 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6004 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006005 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6006 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006007}
6008
Izik Eiduscbc94022007-10-25 00:29:55 +02006009static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6010{
6011 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006012
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006013 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6014 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006015 if (ret)
6016 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08006017 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006018 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006019}
6020
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006021static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006022{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006023 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006024 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006025 /*
6026 * Update instruction length as we may reinject the exception
6027 * from user space while in guest debugging mode.
6028 */
6029 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6030 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006031 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006032 return false;
6033 /* fall through */
6034 case DB_VECTOR:
6035 if (vcpu->guest_debug &
6036 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6037 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006038 /* fall through */
6039 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006040 case OF_VECTOR:
6041 case BR_VECTOR:
6042 case UD_VECTOR:
6043 case DF_VECTOR:
6044 case SS_VECTOR:
6045 case GP_VECTOR:
6046 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006047 return true;
6048 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006049 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006050 return false;
6051}
6052
6053static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6054 int vec, u32 err_code)
6055{
6056 /*
6057 * Instruction with address size override prefix opcode 0x67
6058 * Cause the #SS fault with 0 error code in VM86 mode.
6059 */
6060 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6061 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6062 if (vcpu->arch.halt_request) {
6063 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006064 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006065 }
6066 return 1;
6067 }
6068 return 0;
6069 }
6070
6071 /*
6072 * Forward all other exceptions that are valid in real mode.
6073 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6074 * the required debugging infrastructure rework.
6075 */
6076 kvm_queue_exception(vcpu, vec);
6077 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006078}
6079
Andi Kleena0861c02009-06-08 17:37:09 +08006080/*
6081 * Trigger machine check on the host. We assume all the MSRs are already set up
6082 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6083 * We pass a fake environment to the machine check handler because we want
6084 * the guest to be always treated like user space, no matter what context
6085 * it used internally.
6086 */
6087static void kvm_machine_check(void)
6088{
6089#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6090 struct pt_regs regs = {
6091 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6092 .flags = X86_EFLAGS_IF,
6093 };
6094
6095 do_machine_check(&regs, 0);
6096#endif
6097}
6098
Avi Kivity851ba692009-08-24 11:10:17 +03006099static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006100{
6101 /* already handled by vcpu_run */
6102 return 1;
6103}
6104
Avi Kivity851ba692009-08-24 11:10:17 +03006105static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006106{
Avi Kivity1155f762007-11-22 11:30:47 +02006107 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006108 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006109 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006110 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006111 u32 vect_info;
6112 enum emulation_result er;
6113
Avi Kivity1155f762007-11-22 11:30:47 +02006114 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006115 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006116
Andi Kleena0861c02009-06-08 17:37:09 +08006117 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006118 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006119
Jim Mattsonef85b672016-12-12 11:01:37 -08006120 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006121 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006122
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006123 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01006124 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alon61cb57c2017-11-05 16:56:32 +02006125 if (er == EMULATE_USER_EXIT)
6126 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006127 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02006128 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006129 return 1;
6130 }
6131
Avi Kivity6aa8b732006-12-10 02:21:36 -08006132 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006133 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006134 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006135
6136 /*
6137 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6138 * MMIO, it is better to report an internal error.
6139 * See the comments in vmx_handle_exit.
6140 */
6141 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6142 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6143 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6144 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006145 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006146 vcpu->run->internal.data[0] = vect_info;
6147 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006148 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006149 return 0;
6150 }
6151
Avi Kivity6aa8b732006-12-10 02:21:36 -08006152 if (is_page_fault(intr_info)) {
6153 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006154 /* EPT won't cause page fault directly */
6155 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006156 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006157 }
6158
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006159 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006160
6161 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6162 return handle_rmode_exception(vcpu, ex_no, error_code);
6163
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006164 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006165 case AC_VECTOR:
6166 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6167 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006168 case DB_VECTOR:
6169 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6170 if (!(vcpu->guest_debug &
6171 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006172 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006173 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01006174 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
6175 skip_emulated_instruction(vcpu);
6176
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006177 kvm_queue_exception(vcpu, DB_VECTOR);
6178 return 1;
6179 }
6180 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6181 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6182 /* fall through */
6183 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006184 /*
6185 * Update instruction length as we may reinject #BP from
6186 * user space while in guest debugging mode. Reading it for
6187 * #DB as well causes no harm, it is not used in that case.
6188 */
6189 vmx->vcpu.arch.event_exit_inst_len =
6190 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006191 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006192 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006193 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6194 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006195 break;
6196 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006197 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6198 kvm_run->ex.exception = ex_no;
6199 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006200 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006201 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006202 return 0;
6203}
6204
Avi Kivity851ba692009-08-24 11:10:17 +03006205static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006206{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006207 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006208 return 1;
6209}
6210
Avi Kivity851ba692009-08-24 11:10:17 +03006211static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006212{
Avi Kivity851ba692009-08-24 11:10:17 +03006213 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006214 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006215 return 0;
6216}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006217
Avi Kivity851ba692009-08-24 11:10:17 +03006218static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006219{
He, Qingbfdaab02007-09-12 14:18:28 +08006220 unsigned long exit_qualification;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006221 int size, in, string, ret;
Avi Kivity039576c2007-03-20 12:46:50 +02006222 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006223
He, Qingbfdaab02007-09-12 14:18:28 +08006224 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006225 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006226 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006227
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006228 ++vcpu->stat.io_exits;
6229
6230 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01006231 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006232
6233 port = exit_qualification >> 16;
6234 size = (exit_qualification & 7) + 1;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006235
Kyle Huey6affcbe2016-11-29 12:40:40 -08006236 ret = kvm_skip_emulated_instruction(vcpu);
6237
6238 /*
6239 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
6240 * KVM_EXIT_DEBUG here.
6241 */
6242 return kvm_fast_pio_out(vcpu, size, port) && ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006243}
6244
Ingo Molnar102d8322007-02-19 14:37:47 +02006245static void
6246vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6247{
6248 /*
6249 * Patch in the VMCALL instruction:
6250 */
6251 hypercall[0] = 0x0f;
6252 hypercall[1] = 0x01;
6253 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006254}
6255
Guo Chao0fa06072012-06-28 15:16:19 +08006256/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006257static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6258{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006259 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006260 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6261 unsigned long orig_val = val;
6262
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006263 /*
6264 * We get here when L2 changed cr0 in a way that did not change
6265 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006266 * but did change L0 shadowed bits. So we first calculate the
6267 * effective cr0 value that L1 would like to write into the
6268 * hardware. It consists of the L2-owned bits from the new
6269 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006270 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006271 val = (val & ~vmcs12->cr0_guest_host_mask) |
6272 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6273
David Matlack38991522016-11-29 18:14:08 -08006274 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006275 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006276
6277 if (kvm_set_cr0(vcpu, val))
6278 return 1;
6279 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006280 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006281 } else {
6282 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006283 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006284 return 1;
David Matlack38991522016-11-29 18:14:08 -08006285
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006286 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006287 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006288}
6289
6290static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6291{
6292 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006293 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6294 unsigned long orig_val = val;
6295
6296 /* analogously to handle_set_cr0 */
6297 val = (val & ~vmcs12->cr4_guest_host_mask) |
6298 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6299 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006300 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006301 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006302 return 0;
6303 } else
6304 return kvm_set_cr4(vcpu, val);
6305}
6306
Paolo Bonzini0367f202016-07-12 10:44:55 +02006307static int handle_desc(struct kvm_vcpu *vcpu)
6308{
6309 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6310 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6311}
6312
Avi Kivity851ba692009-08-24 11:10:17 +03006313static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006314{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006315 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006316 int cr;
6317 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006318 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006319 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006320
He, Qingbfdaab02007-09-12 14:18:28 +08006321 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006322 cr = exit_qualification & 15;
6323 reg = (exit_qualification >> 8) & 15;
6324 switch ((exit_qualification >> 4) & 3) {
6325 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006326 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006327 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006328 switch (cr) {
6329 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006330 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006331 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006332 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006333 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006334 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006335 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006336 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006337 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006338 case 8: {
6339 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006340 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006341 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006342 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006343 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006344 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006345 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006346 return ret;
6347 /*
6348 * TODO: we might be squashing a
6349 * KVM_GUESTDBG_SINGLESTEP-triggered
6350 * KVM_EXIT_DEBUG here.
6351 */
Avi Kivity851ba692009-08-24 11:10:17 +03006352 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006353 return 0;
6354 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006355 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006356 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006357 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006358 WARN_ONCE(1, "Guest should always own CR0.TS");
6359 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006360 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006361 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006362 case 1: /*mov from cr*/
6363 switch (cr) {
6364 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006365 val = kvm_read_cr3(vcpu);
6366 kvm_register_write(vcpu, reg, val);
6367 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006368 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006369 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006370 val = kvm_get_cr8(vcpu);
6371 kvm_register_write(vcpu, reg, val);
6372 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006373 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006374 }
6375 break;
6376 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006377 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006378 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006379 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006380
Kyle Huey6affcbe2016-11-29 12:40:40 -08006381 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006382 default:
6383 break;
6384 }
Avi Kivity851ba692009-08-24 11:10:17 +03006385 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006386 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006387 (int)(exit_qualification >> 4) & 3, cr);
6388 return 0;
6389}
6390
Avi Kivity851ba692009-08-24 11:10:17 +03006391static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006392{
He, Qingbfdaab02007-09-12 14:18:28 +08006393 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006394 int dr, dr7, reg;
6395
6396 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6397 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6398
6399 /* First, if DR does not exist, trigger UD */
6400 if (!kvm_require_dr(vcpu, dr))
6401 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006402
Jan Kiszkaf2483412010-01-20 18:20:20 +01006403 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006404 if (!kvm_require_cpl(vcpu, 0))
6405 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006406 dr7 = vmcs_readl(GUEST_DR7);
6407 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006408 /*
6409 * As the vm-exit takes precedence over the debug trap, we
6410 * need to emulate the latter, either for the host or the
6411 * guest debugging itself.
6412 */
6413 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006414 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006415 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006416 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006417 vcpu->run->debug.arch.exception = DB_VECTOR;
6418 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006419 return 0;
6420 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006421 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006422 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006423 kvm_queue_exception(vcpu, DB_VECTOR);
6424 return 1;
6425 }
6426 }
6427
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006428 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006429 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6430 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006431
6432 /*
6433 * No more DR vmexits; force a reload of the debug registers
6434 * and reenter on this instruction. The next vmexit will
6435 * retrieve the full state of the debug registers.
6436 */
6437 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6438 return 1;
6439 }
6440
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006441 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6442 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006443 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006444
6445 if (kvm_get_dr(vcpu, dr, &val))
6446 return 1;
6447 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006448 } else
Nadav Amit57773922014-06-18 17:19:23 +03006449 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006450 return 1;
6451
Kyle Huey6affcbe2016-11-29 12:40:40 -08006452 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006453}
6454
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006455static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6456{
6457 return vcpu->arch.dr6;
6458}
6459
6460static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6461{
6462}
6463
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006464static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6465{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006466 get_debugreg(vcpu->arch.db[0], 0);
6467 get_debugreg(vcpu->arch.db[1], 1);
6468 get_debugreg(vcpu->arch.db[2], 2);
6469 get_debugreg(vcpu->arch.db[3], 3);
6470 get_debugreg(vcpu->arch.dr6, 6);
6471 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6472
6473 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006474 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006475}
6476
Gleb Natapov020df072010-04-13 10:05:23 +03006477static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6478{
6479 vmcs_writel(GUEST_DR7, val);
6480}
6481
Avi Kivity851ba692009-08-24 11:10:17 +03006482static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006483{
Kyle Huey6a908b62016-11-29 12:40:37 -08006484 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006485}
6486
Avi Kivity851ba692009-08-24 11:10:17 +03006487static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006488{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006489 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006490 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006491
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006492 msr_info.index = ecx;
6493 msr_info.host_initiated = false;
6494 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006495 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006496 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006497 return 1;
6498 }
6499
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006500 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006501
Avi Kivity6aa8b732006-12-10 02:21:36 -08006502 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006503 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6504 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006505 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006506}
6507
Avi Kivity851ba692009-08-24 11:10:17 +03006508static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006509{
Will Auld8fe8ab42012-11-29 12:42:12 -08006510 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006511 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6512 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6513 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006514
Will Auld8fe8ab42012-11-29 12:42:12 -08006515 msr.data = data;
6516 msr.index = ecx;
6517 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006518 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006519 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006520 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006521 return 1;
6522 }
6523
Avi Kivity59200272010-01-25 19:47:02 +02006524 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006525 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006526}
6527
Avi Kivity851ba692009-08-24 11:10:17 +03006528static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006529{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01006530 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006531 return 1;
6532}
6533
Avi Kivity851ba692009-08-24 11:10:17 +03006534static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006535{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006536 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6537 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006538
Avi Kivity3842d132010-07-27 12:30:24 +03006539 kvm_make_request(KVM_REQ_EVENT, vcpu);
6540
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006541 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006542 return 1;
6543}
6544
Avi Kivity851ba692009-08-24 11:10:17 +03006545static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006546{
Avi Kivityd3bef152007-06-05 15:53:05 +03006547 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006548}
6549
Avi Kivity851ba692009-08-24 11:10:17 +03006550static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006551{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006552 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006553}
6554
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006555static int handle_invd(struct kvm_vcpu *vcpu)
6556{
Andre Przywara51d8b662010-12-21 11:12:02 +01006557 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006558}
6559
Avi Kivity851ba692009-08-24 11:10:17 +03006560static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006561{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006562 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006563
6564 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006565 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006566}
6567
Avi Kivityfee84b02011-11-10 14:57:25 +02006568static int handle_rdpmc(struct kvm_vcpu *vcpu)
6569{
6570 int err;
6571
6572 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006573 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02006574}
6575
Avi Kivity851ba692009-08-24 11:10:17 +03006576static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006577{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006578 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006579}
6580
Dexuan Cui2acf9232010-06-10 11:27:12 +08006581static int handle_xsetbv(struct kvm_vcpu *vcpu)
6582{
6583 u64 new_bv = kvm_read_edx_eax(vcpu);
6584 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6585
6586 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006587 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08006588 return 1;
6589}
6590
Wanpeng Lif53cd632014-12-02 19:14:58 +08006591static int handle_xsaves(struct kvm_vcpu *vcpu)
6592{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006593 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006594 WARN(1, "this should never happen\n");
6595 return 1;
6596}
6597
6598static int handle_xrstors(struct kvm_vcpu *vcpu)
6599{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006600 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006601 WARN(1, "this should never happen\n");
6602 return 1;
6603}
6604
Avi Kivity851ba692009-08-24 11:10:17 +03006605static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006606{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006607 if (likely(fasteoi)) {
6608 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6609 int access_type, offset;
6610
6611 access_type = exit_qualification & APIC_ACCESS_TYPE;
6612 offset = exit_qualification & APIC_ACCESS_OFFSET;
6613 /*
6614 * Sane guest uses MOV to write EOI, with written value
6615 * not cared. So make a short-circuit here by avoiding
6616 * heavy instruction emulation.
6617 */
6618 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6619 (offset == APIC_EOI)) {
6620 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006621 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03006622 }
6623 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006624 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006625}
6626
Yang Zhangc7c9c562013-01-25 10:18:51 +08006627static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6628{
6629 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6630 int vector = exit_qualification & 0xff;
6631
6632 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6633 kvm_apic_set_eoi_accelerated(vcpu, vector);
6634 return 1;
6635}
6636
Yang Zhang83d4c282013-01-25 10:18:49 +08006637static int handle_apic_write(struct kvm_vcpu *vcpu)
6638{
6639 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6640 u32 offset = exit_qualification & 0xfff;
6641
6642 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6643 kvm_apic_write_nodecode(vcpu, offset);
6644 return 1;
6645}
6646
Avi Kivity851ba692009-08-24 11:10:17 +03006647static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006648{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006649 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006650 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006651 bool has_error_code = false;
6652 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006653 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006654 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006655
6656 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006657 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006658 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006659
6660 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6661
6662 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006663 if (reason == TASK_SWITCH_GATE && idt_v) {
6664 switch (type) {
6665 case INTR_TYPE_NMI_INTR:
6666 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006667 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006668 break;
6669 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006670 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006671 kvm_clear_interrupt_queue(vcpu);
6672 break;
6673 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006674 if (vmx->idt_vectoring_info &
6675 VECTORING_INFO_DELIVER_CODE_MASK) {
6676 has_error_code = true;
6677 error_code =
6678 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6679 }
6680 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006681 case INTR_TYPE_SOFT_EXCEPTION:
6682 kvm_clear_exception_queue(vcpu);
6683 break;
6684 default:
6685 break;
6686 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006687 }
Izik Eidus37817f22008-03-24 23:14:53 +02006688 tss_selector = exit_qualification;
6689
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006690 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6691 type != INTR_TYPE_EXT_INTR &&
6692 type != INTR_TYPE_NMI_INTR))
6693 skip_emulated_instruction(vcpu);
6694
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006695 if (kvm_task_switch(vcpu, tss_selector,
6696 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6697 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006698 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6699 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6700 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006701 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006702 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006703
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006704 /*
6705 * TODO: What about debug traps on tss switch?
6706 * Are we supposed to inject them and update dr6?
6707 */
6708
6709 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006710}
6711
Avi Kivity851ba692009-08-24 11:10:17 +03006712static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006713{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006714 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006715 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01006716 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006717
Sheng Yangf9c617f2009-03-25 10:08:52 +08006718 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006719
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006720 /*
6721 * EPT violation happened while executing iret from NMI,
6722 * "blocked by NMI" bit has to be set before next VM entry.
6723 * There are errata that may cause this bit to not be set:
6724 * AAK134, BY25.
6725 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006726 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006727 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006728 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006729 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6730
Sheng Yang14394422008-04-28 12:24:45 +08006731 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006732 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006733
Junaid Shahid27959a42016-12-06 16:46:10 -08006734 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006735 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08006736 ? PFERR_USER_MASK : 0;
6737 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006738 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08006739 ? PFERR_WRITE_MASK : 0;
6740 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006741 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08006742 ? PFERR_FETCH_MASK : 0;
6743 /* ept page table entry is present? */
6744 error_code |= (exit_qualification &
6745 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6746 EPT_VIOLATION_EXECUTABLE))
6747 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006748
Paolo Bonzinieebed242016-11-28 14:39:58 +01006749 error_code |= (exit_qualification & 0x100) != 0 ?
6750 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006751
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006752 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006753 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006754}
6755
Avi Kivity851ba692009-08-24 11:10:17 +03006756static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006757{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006758 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006759 gpa_t gpa;
6760
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02006761 /*
6762 * A nested guest cannot optimize MMIO vmexits, because we have an
6763 * nGPA here instead of the required GPA.
6764 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006765 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02006766 if (!is_guest_mode(vcpu) &&
6767 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08006768 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006769 /*
6770 * Doing kvm_skip_emulated_instruction() depends on undefined
6771 * behavior: Intel's manual doesn't mandate
6772 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
6773 * occurs and while on real hardware it was observed to be set,
6774 * other hypervisors (namely Hyper-V) don't set it, we end up
6775 * advancing IP with some random value. Disable fast mmio when
6776 * running nested and keep it for real hardware in hope that
6777 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
6778 */
6779 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
6780 return kvm_skip_emulated_instruction(vcpu);
6781 else
6782 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
6783 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006784 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006785
Paolo Bonzinie08d26f2017-08-17 18:36:56 +02006786 ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
6787 if (ret >= 0)
6788 return ret;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006789
6790 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006791 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006792
Avi Kivity851ba692009-08-24 11:10:17 +03006793 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6794 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006795
6796 return 0;
6797}
6798
Avi Kivity851ba692009-08-24 11:10:17 +03006799static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006800{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006801 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01006802 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6803 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08006804 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006805 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006806
6807 return 1;
6808}
6809
Mohammed Gamal80ced182009-09-01 12:48:18 +02006810static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006811{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006812 struct vcpu_vmx *vmx = to_vmx(vcpu);
6813 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006814 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006815 u32 cpu_exec_ctrl;
6816 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006817 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006818
6819 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6820 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006821
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006822 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006823 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006824 return handle_interrupt_window(&vmx->vcpu);
6825
Radim Krčmář72875d82017-04-26 22:32:19 +02006826 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03006827 return 1;
6828
Liran Alon9b8ae632017-11-05 16:56:34 +02006829 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006830
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006831 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006832 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006833 ret = 0;
6834 goto out;
6835 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006836
Avi Kivityde5f70e2012-06-12 20:22:28 +03006837 if (err != EMULATE_DONE) {
6838 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6839 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6840 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006841 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03006842 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006843
Gleb Natapov8d76c492013-05-08 18:38:44 +03006844 if (vcpu->arch.halt_request) {
6845 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006846 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006847 goto out;
6848 }
6849
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006850 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006851 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006852 if (need_resched())
6853 schedule();
6854 }
6855
Mohammed Gamal80ced182009-09-01 12:48:18 +02006856out:
6857 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006858}
6859
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006860static int __grow_ple_window(int val)
6861{
6862 if (ple_window_grow < 1)
6863 return ple_window;
6864
6865 val = min(val, ple_window_actual_max);
6866
6867 if (ple_window_grow < ple_window)
6868 val *= ple_window_grow;
6869 else
6870 val += ple_window_grow;
6871
6872 return val;
6873}
6874
6875static int __shrink_ple_window(int val, int modifier, int minimum)
6876{
6877 if (modifier < 1)
6878 return ple_window;
6879
6880 if (modifier < ple_window)
6881 val /= modifier;
6882 else
6883 val -= modifier;
6884
6885 return max(val, minimum);
6886}
6887
6888static void grow_ple_window(struct kvm_vcpu *vcpu)
6889{
6890 struct vcpu_vmx *vmx = to_vmx(vcpu);
6891 int old = vmx->ple_window;
6892
6893 vmx->ple_window = __grow_ple_window(old);
6894
6895 if (vmx->ple_window != old)
6896 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006897
6898 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006899}
6900
6901static void shrink_ple_window(struct kvm_vcpu *vcpu)
6902{
6903 struct vcpu_vmx *vmx = to_vmx(vcpu);
6904 int old = vmx->ple_window;
6905
6906 vmx->ple_window = __shrink_ple_window(old,
6907 ple_window_shrink, ple_window);
6908
6909 if (vmx->ple_window != old)
6910 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006911
6912 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006913}
6914
6915/*
6916 * ple_window_actual_max is computed to be one grow_ple_window() below
6917 * ple_window_max. (See __grow_ple_window for the reason.)
6918 * This prevents overflows, because ple_window_max is int.
6919 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6920 * this process.
6921 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6922 */
6923static void update_ple_window_actual_max(void)
6924{
6925 ple_window_actual_max =
6926 __shrink_ple_window(max(ple_window_max, ple_window),
6927 ple_window_grow, INT_MIN);
6928}
6929
Feng Wubf9f6ac2015-09-18 22:29:55 +08006930/*
6931 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6932 */
6933static void wakeup_handler(void)
6934{
6935 struct kvm_vcpu *vcpu;
6936 int cpu = smp_processor_id();
6937
6938 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6939 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6940 blocked_vcpu_list) {
6941 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6942
6943 if (pi_test_on(pi_desc) == 1)
6944 kvm_vcpu_kick(vcpu);
6945 }
6946 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6947}
6948
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006949void vmx_enable_tdp(void)
6950{
6951 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
6952 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
6953 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
6954 0ull, VMX_EPT_EXECUTABLE_MASK,
6955 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05006956 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006957
6958 ept_set_mmio_spte_mask();
6959 kvm_enable_tdp();
6960}
6961
Tiejun Chenf2c76482014-10-28 10:14:47 +08006962static __init int hardware_setup(void)
6963{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006964 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006965
6966 rdmsrl_safe(MSR_EFER, &host_efer);
6967
6968 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6969 kvm_define_shared_msr(i, vmx_msr_index[i]);
6970
Radim Krčmář23611332016-09-29 22:41:33 +02006971 for (i = 0; i < VMX_BITMAP_NR; i++) {
6972 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
6973 if (!vmx_bitmap[i])
6974 goto out;
6975 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006976
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006977 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6978 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6979
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006980 if (setup_vmcs_config(&vmcs_config) < 0) {
6981 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02006982 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006983 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006984
6985 if (boot_cpu_has(X86_FEATURE_NX))
6986 kvm_enable_efer_bits(EFER_NX);
6987
Wanpeng Li08d839c2017-03-23 05:30:08 -07006988 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6989 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006990 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07006991
Tiejun Chenf2c76482014-10-28 10:14:47 +08006992 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02006993 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02006994 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07006995 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006996 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006997
Wanpeng Lifce6ac42017-05-11 02:58:56 -07006998 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006999 enable_ept_ad_bits = 0;
7000
Wanpeng Li8ad81822017-10-09 15:51:53 -07007001 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007002 enable_unrestricted_guest = 0;
7003
Paolo Bonziniad15a292015-01-30 16:18:49 +01007004 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007005 flexpriority_enabled = 0;
7006
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007007 if (!cpu_has_virtual_nmis())
7008 enable_vnmi = 0;
7009
Paolo Bonziniad15a292015-01-30 16:18:49 +01007010 /*
7011 * set_apic_access_page_addr() is used to reload apic access
7012 * page upon invalidation. No need to do anything if not
7013 * using the APIC_ACCESS_ADDR VMCS field.
7014 */
7015 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007016 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007017
7018 if (!cpu_has_vmx_tpr_shadow())
7019 kvm_x86_ops->update_cr8_intercept = NULL;
7020
7021 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7022 kvm_disable_largepages();
7023
Wanpeng Li0f107682017-09-28 18:06:24 -07007024 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007025 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007026 ple_window = 0;
7027 ple_window_grow = 0;
7028 ple_window_max = 0;
7029 ple_window_shrink = 0;
7030 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007031
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007032 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007033 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007034 kvm_x86_ops->sync_pir_to_irr = NULL;
7035 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007036
Haozhong Zhang64903d62015-10-20 15:39:09 +08007037 if (cpu_has_vmx_tsc_scaling()) {
7038 kvm_has_tsc_control = true;
7039 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7040 kvm_tsc_scaling_ratio_frac_bits = 48;
7041 }
7042
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007043 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7044
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007045 if (enable_ept)
7046 vmx_enable_tdp();
7047 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007048 kvm_disable_tdp();
7049
7050 update_ple_window_actual_max();
7051
Kai Huang843e4332015-01-28 10:54:28 +08007052 /*
7053 * Only enable PML when hardware supports PML feature, and both EPT
7054 * and EPT A/D bit features are enabled -- PML depends on them to work.
7055 */
7056 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7057 enable_pml = 0;
7058
7059 if (!enable_pml) {
7060 kvm_x86_ops->slot_enable_log_dirty = NULL;
7061 kvm_x86_ops->slot_disable_log_dirty = NULL;
7062 kvm_x86_ops->flush_log_dirty = NULL;
7063 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7064 }
7065
Yunhong Jiang64672c92016-06-13 14:19:59 -07007066 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7067 u64 vmx_msr;
7068
7069 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7070 cpu_preemption_timer_multi =
7071 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7072 } else {
7073 kvm_x86_ops->set_hv_timer = NULL;
7074 kvm_x86_ops->cancel_hv_timer = NULL;
7075 }
7076
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007077 if (!cpu_has_vmx_shadow_vmcs())
7078 enable_shadow_vmcs = 0;
7079 if (enable_shadow_vmcs)
7080 init_vmcs_shadow_fields();
7081
Feng Wubf9f6ac2015-09-18 22:29:55 +08007082 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
7083
Ashok Rajc45dcc72016-06-22 14:59:56 +08007084 kvm_mce_cap_supported |= MCG_LMCE_P;
7085
Tiejun Chenf2c76482014-10-28 10:14:47 +08007086 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007087
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007088out:
Radim Krčmář23611332016-09-29 22:41:33 +02007089 for (i = 0; i < VMX_BITMAP_NR; i++)
7090 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007091
7092 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007093}
7094
7095static __exit void hardware_unsetup(void)
7096{
Radim Krčmář23611332016-09-29 22:41:33 +02007097 int i;
7098
7099 for (i = 0; i < VMX_BITMAP_NR; i++)
7100 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007101
Tiejun Chenf2c76482014-10-28 10:14:47 +08007102 free_kvm_area();
7103}
7104
Avi Kivity6aa8b732006-12-10 02:21:36 -08007105/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007106 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7107 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7108 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007109static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007110{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007111 if (ple_gap)
7112 grow_ple_window(vcpu);
7113
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007114 /*
7115 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7116 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7117 * never set PAUSE_EXITING and just set PLE if supported,
7118 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7119 */
7120 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007121 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007122}
7123
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007124static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007125{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007126 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007127}
7128
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007129static int handle_mwait(struct kvm_vcpu *vcpu)
7130{
7131 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7132 return handle_nop(vcpu);
7133}
7134
Jim Mattson45ec3682017-08-23 16:32:04 -07007135static int handle_invalid_op(struct kvm_vcpu *vcpu)
7136{
7137 kvm_queue_exception(vcpu, UD_VECTOR);
7138 return 1;
7139}
7140
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007141static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7142{
7143 return 1;
7144}
7145
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007146static int handle_monitor(struct kvm_vcpu *vcpu)
7147{
7148 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7149 return handle_nop(vcpu);
7150}
7151
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007152/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007153 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7154 * set the success or error code of an emulated VMX instruction, as specified
7155 * by Vol 2B, VMX Instruction Reference, "Conventions".
7156 */
7157static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7158{
7159 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7160 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7161 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7162}
7163
7164static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7165{
7166 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7167 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7168 X86_EFLAGS_SF | X86_EFLAGS_OF))
7169 | X86_EFLAGS_CF);
7170}
7171
Abel Gordon145c28d2013-04-18 14:36:55 +03007172static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007173 u32 vm_instruction_error)
7174{
7175 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7176 /*
7177 * failValid writes the error number to the current VMCS, which
7178 * can't be done there isn't a current VMCS.
7179 */
7180 nested_vmx_failInvalid(vcpu);
7181 return;
7182 }
7183 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7184 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7185 X86_EFLAGS_SF | X86_EFLAGS_OF))
7186 | X86_EFLAGS_ZF);
7187 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7188 /*
7189 * We don't need to force a shadow sync because
7190 * VM_INSTRUCTION_ERROR is not shadowed
7191 */
7192}
Abel Gordon145c28d2013-04-18 14:36:55 +03007193
Wincy Vanff651cb2014-12-11 08:52:58 +03007194static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7195{
7196 /* TODO: not to reset guest simply here. */
7197 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007198 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007199}
7200
Jan Kiszkaf41245002014-03-07 20:03:13 +01007201static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7202{
7203 struct vcpu_vmx *vmx =
7204 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7205
7206 vmx->nested.preemption_timer_expired = true;
7207 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7208 kvm_vcpu_kick(&vmx->vcpu);
7209
7210 return HRTIMER_NORESTART;
7211}
7212
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007213/*
Bandan Das19677e32014-05-06 02:19:15 -04007214 * Decode the memory-address operand of a vmx instruction, as recorded on an
7215 * exit caused by such an instruction (run by a guest hypervisor).
7216 * On success, returns 0. When the operand is invalid, returns 1 and throws
7217 * #UD or #GP.
7218 */
7219static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7220 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007221 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007222{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007223 gva_t off;
7224 bool exn;
7225 struct kvm_segment s;
7226
Bandan Das19677e32014-05-06 02:19:15 -04007227 /*
7228 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7229 * Execution", on an exit, vmx_instruction_info holds most of the
7230 * addressing components of the operand. Only the displacement part
7231 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7232 * For how an actual address is calculated from all these components,
7233 * refer to Vol. 1, "Operand Addressing".
7234 */
7235 int scaling = vmx_instruction_info & 3;
7236 int addr_size = (vmx_instruction_info >> 7) & 7;
7237 bool is_reg = vmx_instruction_info & (1u << 10);
7238 int seg_reg = (vmx_instruction_info >> 15) & 7;
7239 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7240 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7241 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7242 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7243
7244 if (is_reg) {
7245 kvm_queue_exception(vcpu, UD_VECTOR);
7246 return 1;
7247 }
7248
7249 /* Addr = segment_base + offset */
7250 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007251 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007252 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007253 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007254 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007255 off += kvm_register_read(vcpu, index_reg)<<scaling;
7256 vmx_get_segment(vcpu, &s, seg_reg);
7257 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007258
7259 if (addr_size == 1) /* 32 bit */
7260 *ret &= 0xffffffff;
7261
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007262 /* Checks for #GP/#SS exceptions. */
7263 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007264 if (is_long_mode(vcpu)) {
7265 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7266 * non-canonical form. This is the only check on the memory
7267 * destination for long mode!
7268 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007269 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007270 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007271 /* Protected mode: apply checks for segment validity in the
7272 * following order:
7273 * - segment type check (#GP(0) may be thrown)
7274 * - usability check (#GP(0)/#SS(0))
7275 * - limit check (#GP(0)/#SS(0))
7276 */
7277 if (wr)
7278 /* #GP(0) if the destination operand is located in a
7279 * read-only data segment or any code segment.
7280 */
7281 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7282 else
7283 /* #GP(0) if the source operand is located in an
7284 * execute-only code segment
7285 */
7286 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007287 if (exn) {
7288 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7289 return 1;
7290 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007291 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7292 */
7293 exn = (s.unusable != 0);
7294 /* Protected mode: #GP(0)/#SS(0) if the memory
7295 * operand is outside the segment limit.
7296 */
7297 exn = exn || (off + sizeof(u64) > s.limit);
7298 }
7299 if (exn) {
7300 kvm_queue_exception_e(vcpu,
7301 seg_reg == VCPU_SREG_SS ?
7302 SS_VECTOR : GP_VECTOR,
7303 0);
7304 return 1;
7305 }
7306
Bandan Das19677e32014-05-06 02:19:15 -04007307 return 0;
7308}
7309
Radim Krčmářcbf71272017-05-19 15:48:51 +02007310static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007311{
7312 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007313 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007314
7315 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007316 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007317 return 1;
7318
Radim Krčmářcbf71272017-05-19 15:48:51 +02007319 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7320 sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007321 kvm_inject_page_fault(vcpu, &e);
7322 return 1;
7323 }
7324
Bandan Das3573e222014-05-06 02:19:16 -04007325 return 0;
7326}
7327
Jim Mattsone29acc52016-11-30 12:03:43 -08007328static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7329{
7330 struct vcpu_vmx *vmx = to_vmx(vcpu);
7331 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007332 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007333
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007334 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7335 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007336 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007337
7338 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7339 if (!vmx->nested.cached_vmcs12)
7340 goto out_cached_vmcs12;
7341
7342 if (enable_shadow_vmcs) {
7343 shadow_vmcs = alloc_vmcs();
7344 if (!shadow_vmcs)
7345 goto out_shadow_vmcs;
7346 /* mark vmcs as shadow */
7347 shadow_vmcs->revision_id |= (1u << 31);
7348 /* init shadow vmcs */
7349 vmcs_clear(shadow_vmcs);
7350 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7351 }
7352
Jim Mattsone29acc52016-11-30 12:03:43 -08007353 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7354 HRTIMER_MODE_REL_PINNED);
7355 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7356
7357 vmx->nested.vmxon = true;
7358 return 0;
7359
7360out_shadow_vmcs:
7361 kfree(vmx->nested.cached_vmcs12);
7362
7363out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007364 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007365
Jim Mattsonde3a0022017-11-27 17:22:25 -06007366out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007367 return -ENOMEM;
7368}
7369
Bandan Das3573e222014-05-06 02:19:16 -04007370/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007371 * Emulate the VMXON instruction.
7372 * Currently, we just remember that VMX is active, and do not save or even
7373 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7374 * do not currently need to store anything in that guest-allocated memory
7375 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7376 * argument is different from the VMXON pointer (which the spec says they do).
7377 */
7378static int handle_vmon(struct kvm_vcpu *vcpu)
7379{
Jim Mattsone29acc52016-11-30 12:03:43 -08007380 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007381 gpa_t vmptr;
7382 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007383 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007384 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7385 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007386
Jim Mattson70f3aac2017-04-26 08:53:46 -07007387 /*
7388 * The Intel VMX Instruction Reference lists a bunch of bits that are
7389 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7390 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7391 * Otherwise, we should fail with #UD. But most faulting conditions
7392 * have already been checked by hardware, prior to the VM-exit for
7393 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7394 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007395 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007396 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007397 kvm_queue_exception(vcpu, UD_VECTOR);
7398 return 1;
7399 }
7400
Abel Gordon145c28d2013-04-18 14:36:55 +03007401 if (vmx->nested.vmxon) {
7402 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007403 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007404 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007405
Haozhong Zhang3b840802016-06-22 14:59:54 +08007406 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007407 != VMXON_NEEDED_FEATURES) {
7408 kvm_inject_gp(vcpu, 0);
7409 return 1;
7410 }
7411
Radim Krčmářcbf71272017-05-19 15:48:51 +02007412 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007413 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007414
7415 /*
7416 * SDM 3: 24.11.5
7417 * The first 4 bytes of VMXON region contain the supported
7418 * VMCS revision identifier
7419 *
7420 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7421 * which replaces physical address width with 32
7422 */
7423 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7424 nested_vmx_failInvalid(vcpu);
7425 return kvm_skip_emulated_instruction(vcpu);
7426 }
7427
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007428 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7429 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007430 nested_vmx_failInvalid(vcpu);
7431 return kvm_skip_emulated_instruction(vcpu);
7432 }
7433 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7434 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007435 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007436 nested_vmx_failInvalid(vcpu);
7437 return kvm_skip_emulated_instruction(vcpu);
7438 }
7439 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007440 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007441
7442 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007443 ret = enter_vmx_operation(vcpu);
7444 if (ret)
7445 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007446
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007447 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007448 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007449}
7450
7451/*
7452 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7453 * for running VMX instructions (except VMXON, whose prerequisites are
7454 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07007455 * Note that many of these exceptions have priority over VM exits, so they
7456 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007457 */
7458static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7459{
Jim Mattson70f3aac2017-04-26 08:53:46 -07007460 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007461 kvm_queue_exception(vcpu, UD_VECTOR);
7462 return 0;
7463 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007464 return 1;
7465}
7466
David Matlack8ca44e82017-08-01 14:00:39 -07007467static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7468{
7469 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7470 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7471}
7472
Abel Gordone7953d72013-04-18 14:37:55 +03007473static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7474{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007475 if (vmx->nested.current_vmptr == -1ull)
7476 return;
7477
Abel Gordon012f83c2013-04-18 14:39:25 +03007478 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007479 /* copy to memory all shadowed fields in case
7480 they were modified */
7481 copy_shadow_to_vmcs12(vmx);
7482 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07007483 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03007484 }
Wincy Van705699a2015-02-03 23:58:17 +08007485 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007486
7487 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007488 kvm_vcpu_write_guest_page(&vmx->vcpu,
7489 vmx->nested.current_vmptr >> PAGE_SHIFT,
7490 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07007491
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007492 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03007493}
7494
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007495/*
7496 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7497 * just stops using VMX.
7498 */
7499static void free_nested(struct vcpu_vmx *vmx)
7500{
Wanpeng Lib7455822017-11-22 14:04:00 -08007501 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007502 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007503
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007504 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08007505 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007506 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07007507 vmx->nested.posted_intr_nv = -1;
7508 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007509 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07007510 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007511 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7512 free_vmcs(vmx->vmcs01.shadow_vmcs);
7513 vmx->vmcs01.shadow_vmcs = NULL;
7514 }
David Matlack4f2777b2016-07-13 17:16:37 -07007515 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06007516 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007517 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007518 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007519 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007520 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007521 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007522 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007523 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007524 }
Wincy Van705699a2015-02-03 23:58:17 +08007525 if (vmx->nested.pi_desc_page) {
7526 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007527 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08007528 vmx->nested.pi_desc_page = NULL;
7529 vmx->nested.pi_desc = NULL;
7530 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007531
Jim Mattsonde3a0022017-11-27 17:22:25 -06007532 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007533}
7534
7535/* Emulate the VMXOFF instruction */
7536static int handle_vmoff(struct kvm_vcpu *vcpu)
7537{
7538 if (!nested_vmx_check_permission(vcpu))
7539 return 1;
7540 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007541 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007542 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007543}
7544
Nadav Har'El27d6c862011-05-25 23:06:59 +03007545/* Emulate the VMCLEAR instruction */
7546static int handle_vmclear(struct kvm_vcpu *vcpu)
7547{
7548 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08007549 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007550 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007551
7552 if (!nested_vmx_check_permission(vcpu))
7553 return 1;
7554
Radim Krčmářcbf71272017-05-19 15:48:51 +02007555 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007556 return 1;
7557
Radim Krčmářcbf71272017-05-19 15:48:51 +02007558 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7559 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7560 return kvm_skip_emulated_instruction(vcpu);
7561 }
7562
7563 if (vmptr == vmx->nested.vmxon_ptr) {
7564 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7565 return kvm_skip_emulated_instruction(vcpu);
7566 }
7567
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007568 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007569 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007570
Jim Mattson587d7e722017-03-02 12:41:48 -08007571 kvm_vcpu_write_guest(vcpu,
7572 vmptr + offsetof(struct vmcs12, launch_state),
7573 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007574
Nadav Har'El27d6c862011-05-25 23:06:59 +03007575 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007576 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007577}
7578
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007579static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7580
7581/* Emulate the VMLAUNCH instruction */
7582static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7583{
7584 return nested_vmx_run(vcpu, true);
7585}
7586
7587/* Emulate the VMRESUME instruction */
7588static int handle_vmresume(struct kvm_vcpu *vcpu)
7589{
7590
7591 return nested_vmx_run(vcpu, false);
7592}
7593
Nadav Har'El49f705c2011-05-25 23:08:30 +03007594/*
7595 * Read a vmcs12 field. Since these can have varying lengths and we return
7596 * one type, we chose the biggest type (u64) and zero-extend the return value
7597 * to that size. Note that the caller, handle_vmread, might need to use only
7598 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7599 * 64-bit fields are to be returned).
7600 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007601static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7602 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007603{
7604 short offset = vmcs_field_to_offset(field);
7605 char *p;
7606
7607 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007608 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007609
7610 p = ((char *)(get_vmcs12(vcpu))) + offset;
7611
Jim Mattsond37f4262017-12-22 12:12:16 -08007612 switch (vmcs_field_width(field)) {
7613 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007614 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007615 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007616 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007617 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007618 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007619 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007620 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007621 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007622 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007623 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007624 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007625 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007626 WARN_ON(1);
7627 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007628 }
7629}
7630
Abel Gordon20b97fe2013-04-18 14:36:25 +03007631
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007632static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7633 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007634 short offset = vmcs_field_to_offset(field);
7635 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7636 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007637 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007638
Jim Mattsond37f4262017-12-22 12:12:16 -08007639 switch (vmcs_field_width(field)) {
7640 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007641 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007642 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007643 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007644 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007645 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007646 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007647 *(u64 *)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_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007650 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007651 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007652 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007653 WARN_ON(1);
7654 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007655 }
7656
7657}
7658
Abel Gordon16f5b902013-04-18 14:38:25 +03007659static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7660{
7661 int i;
7662 unsigned long field;
7663 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007664 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007665 const u16 *fields = shadow_read_write_fields;
Mathias Krausec2bae892013-06-26 20:36:21 +02007666 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007667
Jan Kiszka282da872014-10-08 18:05:39 +02007668 preempt_disable();
7669
Abel Gordon16f5b902013-04-18 14:38:25 +03007670 vmcs_load(shadow_vmcs);
7671
7672 for (i = 0; i < num_fields; i++) {
7673 field = fields[i];
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007674 field_value = __vmcs_readl(field);
Abel Gordon16f5b902013-04-18 14:38:25 +03007675 vmcs12_write_any(&vmx->vcpu, field, field_value);
7676 }
7677
7678 vmcs_clear(shadow_vmcs);
7679 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007680
7681 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007682}
7683
Abel Gordonc3114422013-04-18 14:38:55 +03007684static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7685{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007686 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02007687 shadow_read_write_fields,
7688 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007689 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007690 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007691 max_shadow_read_write_fields,
7692 max_shadow_read_only_fields
7693 };
7694 int i, q;
7695 unsigned long field;
7696 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007697 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007698
7699 vmcs_load(shadow_vmcs);
7700
Mathias Krausec2bae892013-06-26 20:36:21 +02007701 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007702 for (i = 0; i < max_fields[q]; i++) {
7703 field = fields[q][i];
7704 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007705 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03007706 }
7707 }
7708
7709 vmcs_clear(shadow_vmcs);
7710 vmcs_load(vmx->loaded_vmcs->vmcs);
7711}
7712
Nadav Har'El49f705c2011-05-25 23:08:30 +03007713/*
7714 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7715 * used before) all generate the same failure when it is missing.
7716 */
7717static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7718{
7719 struct vcpu_vmx *vmx = to_vmx(vcpu);
7720 if (vmx->nested.current_vmptr == -1ull) {
7721 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007722 return 0;
7723 }
7724 return 1;
7725}
7726
7727static int handle_vmread(struct kvm_vcpu *vcpu)
7728{
7729 unsigned long field;
7730 u64 field_value;
7731 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7732 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7733 gva_t gva = 0;
7734
Kyle Hueyeb277562016-11-29 12:40:39 -08007735 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007736 return 1;
7737
Kyle Huey6affcbe2016-11-29 12:40:40 -08007738 if (!nested_vmx_check_vmcs12(vcpu))
7739 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007740
Nadav Har'El49f705c2011-05-25 23:08:30 +03007741 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007742 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007743 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007744 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007745 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007746 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007747 }
7748 /*
7749 * Now copy part of this value to register or memory, as requested.
7750 * Note that the number of bits actually copied is 32 or 64 depending
7751 * on the guest's mode (32 or 64 bit), not on the given field's length.
7752 */
7753 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007754 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007755 field_value);
7756 } else {
7757 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007758 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007759 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07007760 /* _system ok, as hardware has verified cpl=0 */
Nadav Har'El49f705c2011-05-25 23:08:30 +03007761 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7762 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7763 }
7764
7765 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007766 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007767}
7768
7769
7770static int handle_vmwrite(struct kvm_vcpu *vcpu)
7771{
7772 unsigned long field;
7773 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007774 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007775 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7776 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007777
Nadav Har'El49f705c2011-05-25 23:08:30 +03007778 /* The value to write might be 32 or 64 bits, depending on L1's long
7779 * mode, and eventually we need to write that into a field of several
7780 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007781 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007782 * bits into the vmcs12 field.
7783 */
7784 u64 field_value = 0;
7785 struct x86_exception e;
7786
Kyle Hueyeb277562016-11-29 12:40:39 -08007787 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007788 return 1;
7789
Kyle Huey6affcbe2016-11-29 12:40:40 -08007790 if (!nested_vmx_check_vmcs12(vcpu))
7791 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007792
Nadav Har'El49f705c2011-05-25 23:08:30 +03007793 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007794 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007795 (((vmx_instruction_info) >> 3) & 0xf));
7796 else {
7797 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007798 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007799 return 1;
7800 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007801 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007802 kvm_inject_page_fault(vcpu, &e);
7803 return 1;
7804 }
7805 }
7806
7807
Nadav Amit27e6fb52014-06-18 17:19:26 +03007808 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007809 if (vmcs_field_readonly(field)) {
7810 nested_vmx_failValid(vcpu,
7811 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007812 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007813 }
7814
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007815 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007816 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007817 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007818 }
7819
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007820 switch (field) {
7821#define SHADOW_FIELD_RW(x) case x:
7822#include "vmx_shadow_fields.h"
7823 /*
7824 * The fields that can be updated by L1 without a vmexit are
7825 * always updated in the vmcs02, the others go down the slow
7826 * path of prepare_vmcs02.
7827 */
7828 break;
7829 default:
7830 vmx->nested.dirty_vmcs12 = true;
7831 break;
7832 }
7833
Nadav Har'El49f705c2011-05-25 23:08:30 +03007834 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007835 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007836}
7837
Jim Mattsona8bc2842016-11-30 12:03:44 -08007838static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
7839{
7840 vmx->nested.current_vmptr = vmptr;
7841 if (enable_shadow_vmcs) {
7842 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7843 SECONDARY_EXEC_SHADOW_VMCS);
7844 vmcs_write64(VMCS_LINK_POINTER,
7845 __pa(vmx->vmcs01.shadow_vmcs));
7846 vmx->nested.sync_shadow_vmcs = true;
7847 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007848 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08007849}
7850
Nadav Har'El63846662011-05-25 23:07:29 +03007851/* Emulate the VMPTRLD instruction */
7852static int handle_vmptrld(struct kvm_vcpu *vcpu)
7853{
7854 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007855 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007856
7857 if (!nested_vmx_check_permission(vcpu))
7858 return 1;
7859
Radim Krčmářcbf71272017-05-19 15:48:51 +02007860 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007861 return 1;
7862
Radim Krčmářcbf71272017-05-19 15:48:51 +02007863 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7864 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
7865 return kvm_skip_emulated_instruction(vcpu);
7866 }
7867
7868 if (vmptr == vmx->nested.vmxon_ptr) {
7869 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
7870 return kvm_skip_emulated_instruction(vcpu);
7871 }
7872
Nadav Har'El63846662011-05-25 23:07:29 +03007873 if (vmx->nested.current_vmptr != vmptr) {
7874 struct vmcs12 *new_vmcs12;
7875 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007876 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7877 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03007878 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007879 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007880 }
7881 new_vmcs12 = kmap(page);
7882 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7883 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007884 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03007885 nested_vmx_failValid(vcpu,
7886 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007887 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007888 }
Nadav Har'El63846662011-05-25 23:07:29 +03007889
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007890 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07007891 /*
7892 * Load VMCS12 from guest memory since it is not already
7893 * cached.
7894 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007895 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
7896 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007897 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007898
Jim Mattsona8bc2842016-11-30 12:03:44 -08007899 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03007900 }
7901
7902 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007903 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007904}
7905
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007906/* Emulate the VMPTRST instruction */
7907static int handle_vmptrst(struct kvm_vcpu *vcpu)
7908{
7909 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7910 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7911 gva_t vmcs_gva;
7912 struct x86_exception e;
7913
7914 if (!nested_vmx_check_permission(vcpu))
7915 return 1;
7916
7917 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007918 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007919 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07007920 /* ok to use *_system, as hardware has verified cpl=0 */
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007921 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7922 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7923 sizeof(u64), &e)) {
7924 kvm_inject_page_fault(vcpu, &e);
7925 return 1;
7926 }
7927 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007928 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007929}
7930
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007931/* Emulate the INVEPT instruction */
7932static int handle_invept(struct kvm_vcpu *vcpu)
7933{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007934 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007935 u32 vmx_instruction_info, types;
7936 unsigned long type;
7937 gva_t gva;
7938 struct x86_exception e;
7939 struct {
7940 u64 eptp, gpa;
7941 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007942
Wincy Vanb9c237b2015-02-03 23:56:30 +08007943 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7944 SECONDARY_EXEC_ENABLE_EPT) ||
7945 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007946 kvm_queue_exception(vcpu, UD_VECTOR);
7947 return 1;
7948 }
7949
7950 if (!nested_vmx_check_permission(vcpu))
7951 return 1;
7952
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007953 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007954 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007955
Wincy Vanb9c237b2015-02-03 23:56:30 +08007956 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007957
Jim Mattson85c856b2016-10-26 08:38:38 -07007958 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007959 nested_vmx_failValid(vcpu,
7960 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007961 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007962 }
7963
7964 /* According to the Intel VMX instruction reference, the memory
7965 * operand is read even if it isn't needed (e.g., for type==global)
7966 */
7967 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007968 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007969 return 1;
7970 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7971 sizeof(operand), &e)) {
7972 kvm_inject_page_fault(vcpu, &e);
7973 return 1;
7974 }
7975
7976 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007977 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007978 /*
7979 * TODO: track mappings and invalidate
7980 * single context requests appropriately
7981 */
7982 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007983 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007984 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007985 nested_vmx_succeed(vcpu);
7986 break;
7987 default:
7988 BUG_ON(1);
7989 break;
7990 }
7991
Kyle Huey6affcbe2016-11-29 12:40:40 -08007992 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007993}
7994
Petr Matouseka642fc32014-09-23 20:22:30 +02007995static int handle_invvpid(struct kvm_vcpu *vcpu)
7996{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007997 struct vcpu_vmx *vmx = to_vmx(vcpu);
7998 u32 vmx_instruction_info;
7999 unsigned long type, types;
8000 gva_t gva;
8001 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008002 struct {
8003 u64 vpid;
8004 u64 gla;
8005 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008006
8007 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
8008 SECONDARY_EXEC_ENABLE_VPID) ||
8009 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
8010 kvm_queue_exception(vcpu, UD_VECTOR);
8011 return 1;
8012 }
8013
8014 if (!nested_vmx_check_permission(vcpu))
8015 return 1;
8016
8017 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8018 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8019
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008020 types = (vmx->nested.nested_vmx_vpid_caps &
8021 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008022
Jim Mattson85c856b2016-10-26 08:38:38 -07008023 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008024 nested_vmx_failValid(vcpu,
8025 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008026 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008027 }
8028
8029 /* according to the intel vmx instruction reference, the memory
8030 * operand is read even if it isn't needed (e.g., for type==global)
8031 */
8032 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8033 vmx_instruction_info, false, &gva))
8034 return 1;
Jim Mattson40352602017-06-28 09:37:37 -07008035 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8036 sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008037 kvm_inject_page_fault(vcpu, &e);
8038 return 1;
8039 }
Jim Mattson40352602017-06-28 09:37:37 -07008040 if (operand.vpid >> 16) {
8041 nested_vmx_failValid(vcpu,
8042 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8043 return kvm_skip_emulated_instruction(vcpu);
8044 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008045
8046 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008047 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Yu Zhangfd8cb432017-08-24 20:27:56 +08008048 if (is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008049 nested_vmx_failValid(vcpu,
8050 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8051 return kvm_skip_emulated_instruction(vcpu);
8052 }
8053 /* fall through */
Paolo Bonzinief697a72016-03-18 16:58:38 +01008054 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008055 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008056 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008057 nested_vmx_failValid(vcpu,
8058 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008059 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008060 }
8061 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008062 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008063 break;
8064 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008065 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008066 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008067 }
8068
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008069 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008070 nested_vmx_succeed(vcpu);
8071
Kyle Huey6affcbe2016-11-29 12:40:40 -08008072 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008073}
8074
Kai Huang843e4332015-01-28 10:54:28 +08008075static int handle_pml_full(struct kvm_vcpu *vcpu)
8076{
8077 unsigned long exit_qualification;
8078
8079 trace_kvm_pml_full(vcpu->vcpu_id);
8080
8081 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8082
8083 /*
8084 * PML buffer FULL happened while executing iret from NMI,
8085 * "blocked by NMI" bit has to be set before next VM entry.
8086 */
8087 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008088 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008089 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8090 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8091 GUEST_INTR_STATE_NMI);
8092
8093 /*
8094 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8095 * here.., and there's no userspace involvement needed for PML.
8096 */
8097 return 1;
8098}
8099
Yunhong Jiang64672c92016-06-13 14:19:59 -07008100static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8101{
8102 kvm_lapic_expired_hv_timer(vcpu);
8103 return 1;
8104}
8105
Bandan Das41ab9372017-08-03 15:54:43 -04008106static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8107{
8108 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008109 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8110
8111 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008112 switch (address & VMX_EPTP_MT_MASK) {
8113 case VMX_EPTP_MT_UC:
Bandan Das41ab9372017-08-03 15:54:43 -04008114 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_UC_BIT))
8115 return false;
8116 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008117 case VMX_EPTP_MT_WB:
Bandan Das41ab9372017-08-03 15:54:43 -04008118 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_WB_BIT))
8119 return false;
8120 break;
8121 default:
8122 return false;
8123 }
8124
David Hildenbrandbb97a012017-08-10 23:15:28 +02008125 /* only 4 levels page-walk length are valid */
8126 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008127 return false;
8128
8129 /* Reserved bits should not be set */
8130 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8131 return false;
8132
8133 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008134 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Bandan Das41ab9372017-08-03 15:54:43 -04008135 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPT_AD_BIT))
8136 return false;
8137 }
8138
8139 return true;
8140}
8141
8142static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8143 struct vmcs12 *vmcs12)
8144{
8145 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8146 u64 address;
8147 bool accessed_dirty;
8148 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8149
8150 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8151 !nested_cpu_has_ept(vmcs12))
8152 return 1;
8153
8154 if (index >= VMFUNC_EPTP_ENTRIES)
8155 return 1;
8156
8157
8158 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8159 &address, index * 8, 8))
8160 return 1;
8161
David Hildenbrandbb97a012017-08-10 23:15:28 +02008162 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008163
8164 /*
8165 * If the (L2) guest does a vmfunc to the currently
8166 * active ept pointer, we don't have to do anything else
8167 */
8168 if (vmcs12->ept_pointer != address) {
8169 if (!valid_ept_address(vcpu, address))
8170 return 1;
8171
8172 kvm_mmu_unload(vcpu);
8173 mmu->ept_ad = accessed_dirty;
8174 mmu->base_role.ad_disabled = !accessed_dirty;
8175 vmcs12->ept_pointer = address;
8176 /*
8177 * TODO: Check what's the correct approach in case
8178 * mmu reload fails. Currently, we just let the next
8179 * reload potentially fail
8180 */
8181 kvm_mmu_reload(vcpu);
8182 }
8183
8184 return 0;
8185}
8186
Bandan Das2a499e42017-08-03 15:54:41 -04008187static int handle_vmfunc(struct kvm_vcpu *vcpu)
8188{
Bandan Das27c42a12017-08-03 15:54:42 -04008189 struct vcpu_vmx *vmx = to_vmx(vcpu);
8190 struct vmcs12 *vmcs12;
8191 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8192
8193 /*
8194 * VMFUNC is only supported for nested guests, but we always enable the
8195 * secondary control for simplicity; for non-nested mode, fake that we
8196 * didn't by injecting #UD.
8197 */
8198 if (!is_guest_mode(vcpu)) {
8199 kvm_queue_exception(vcpu, UD_VECTOR);
8200 return 1;
8201 }
8202
8203 vmcs12 = get_vmcs12(vcpu);
8204 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8205 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008206
8207 switch (function) {
8208 case 0:
8209 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8210 goto fail;
8211 break;
8212 default:
8213 goto fail;
8214 }
8215 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008216
8217fail:
8218 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8219 vmcs_read32(VM_EXIT_INTR_INFO),
8220 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008221 return 1;
8222}
8223
Nadav Har'El0140cae2011-05-25 23:06:28 +03008224/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008225 * The exit handlers return 1 if the exit was handled fully and guest execution
8226 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8227 * to be done to userspace and return 0.
8228 */
Mathias Krause772e0312012-08-30 01:30:19 +02008229static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008230 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8231 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008232 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008233 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008234 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008235 [EXIT_REASON_CR_ACCESS] = handle_cr,
8236 [EXIT_REASON_DR_ACCESS] = handle_dr,
8237 [EXIT_REASON_CPUID] = handle_cpuid,
8238 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8239 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8240 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8241 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008242 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008243 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008244 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008245 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008246 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008247 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008248 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008249 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008250 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008251 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008252 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008253 [EXIT_REASON_VMOFF] = handle_vmoff,
8254 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008255 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8256 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008257 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008258 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008259 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008260 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008261 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008262 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008263 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8264 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008265 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8266 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008267 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008268 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008269 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008270 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008271 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008272 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008273 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008274 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008275 [EXIT_REASON_XSAVES] = handle_xsaves,
8276 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008277 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008278 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008279 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008280};
8281
8282static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008283 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008284
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008285static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8286 struct vmcs12 *vmcs12)
8287{
8288 unsigned long exit_qualification;
8289 gpa_t bitmap, last_bitmap;
8290 unsigned int port;
8291 int size;
8292 u8 b;
8293
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008294 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008295 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008296
8297 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8298
8299 port = exit_qualification >> 16;
8300 size = (exit_qualification & 7) + 1;
8301
8302 last_bitmap = (gpa_t)-1;
8303 b = -1;
8304
8305 while (size > 0) {
8306 if (port < 0x8000)
8307 bitmap = vmcs12->io_bitmap_a;
8308 else if (port < 0x10000)
8309 bitmap = vmcs12->io_bitmap_b;
8310 else
Joe Perches1d804d02015-03-30 16:46:09 -07008311 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008312 bitmap += (port & 0x7fff) / 8;
8313
8314 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008315 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008316 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008317 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008318 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008319
8320 port++;
8321 size--;
8322 last_bitmap = bitmap;
8323 }
8324
Joe Perches1d804d02015-03-30 16:46:09 -07008325 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008326}
8327
Nadav Har'El644d7112011-05-25 23:12:35 +03008328/*
8329 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8330 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8331 * disinterest in the current event (read or write a specific MSR) by using an
8332 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8333 */
8334static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8335 struct vmcs12 *vmcs12, u32 exit_reason)
8336{
8337 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8338 gpa_t bitmap;
8339
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008340 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008341 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008342
8343 /*
8344 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8345 * for the four combinations of read/write and low/high MSR numbers.
8346 * First we need to figure out which of the four to use:
8347 */
8348 bitmap = vmcs12->msr_bitmap;
8349 if (exit_reason == EXIT_REASON_MSR_WRITE)
8350 bitmap += 2048;
8351 if (msr_index >= 0xc0000000) {
8352 msr_index -= 0xc0000000;
8353 bitmap += 1024;
8354 }
8355
8356 /* Then read the msr_index'th bit from this bitmap: */
8357 if (msr_index < 1024*8) {
8358 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008359 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008360 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008361 return 1 & (b >> (msr_index & 7));
8362 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008363 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008364}
8365
8366/*
8367 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8368 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8369 * intercept (via guest_host_mask etc.) the current event.
8370 */
8371static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8372 struct vmcs12 *vmcs12)
8373{
8374 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8375 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008376 int reg;
8377 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008378
8379 switch ((exit_qualification >> 4) & 3) {
8380 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008381 reg = (exit_qualification >> 8) & 15;
8382 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008383 switch (cr) {
8384 case 0:
8385 if (vmcs12->cr0_guest_host_mask &
8386 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008387 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008388 break;
8389 case 3:
8390 if ((vmcs12->cr3_target_count >= 1 &&
8391 vmcs12->cr3_target_value0 == val) ||
8392 (vmcs12->cr3_target_count >= 2 &&
8393 vmcs12->cr3_target_value1 == val) ||
8394 (vmcs12->cr3_target_count >= 3 &&
8395 vmcs12->cr3_target_value2 == val) ||
8396 (vmcs12->cr3_target_count >= 4 &&
8397 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008398 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008399 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008400 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008401 break;
8402 case 4:
8403 if (vmcs12->cr4_guest_host_mask &
8404 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008405 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008406 break;
8407 case 8:
8408 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008409 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008410 break;
8411 }
8412 break;
8413 case 2: /* clts */
8414 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8415 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008416 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008417 break;
8418 case 1: /* mov from cr */
8419 switch (cr) {
8420 case 3:
8421 if (vmcs12->cpu_based_vm_exec_control &
8422 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008423 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008424 break;
8425 case 8:
8426 if (vmcs12->cpu_based_vm_exec_control &
8427 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008428 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008429 break;
8430 }
8431 break;
8432 case 3: /* lmsw */
8433 /*
8434 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8435 * cr0. Other attempted changes are ignored, with no exit.
8436 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008437 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008438 if (vmcs12->cr0_guest_host_mask & 0xe &
8439 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008440 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008441 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8442 !(vmcs12->cr0_read_shadow & 0x1) &&
8443 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008444 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008445 break;
8446 }
Joe Perches1d804d02015-03-30 16:46:09 -07008447 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008448}
8449
8450/*
8451 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8452 * should handle it ourselves in L0 (and then continue L2). Only call this
8453 * when in is_guest_mode (L2).
8454 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02008455static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03008456{
Nadav Har'El644d7112011-05-25 23:12:35 +03008457 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8458 struct vcpu_vmx *vmx = to_vmx(vcpu);
8459 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8460
Jim Mattson4f350c62017-09-14 16:31:44 -07008461 if (vmx->nested.nested_run_pending)
8462 return false;
8463
8464 if (unlikely(vmx->fail)) {
8465 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8466 vmcs_read32(VM_INSTRUCTION_ERROR));
8467 return true;
8468 }
Jan Kiszka542060e2014-01-04 18:47:21 +01008469
David Matlackc9f04402017-08-01 14:00:40 -07008470 /*
8471 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06008472 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8473 * Page). The CPU may write to these pages via their host
8474 * physical address while L2 is running, bypassing any
8475 * address-translation-based dirty tracking (e.g. EPT write
8476 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07008477 *
8478 * Mark them dirty on every exit from L2 to prevent them from
8479 * getting out of sync with dirty tracking.
8480 */
8481 nested_mark_vmcs12_pages_dirty(vcpu);
8482
Jim Mattson4f350c62017-09-14 16:31:44 -07008483 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8484 vmcs_readl(EXIT_QUALIFICATION),
8485 vmx->idt_vectoring_info,
8486 intr_info,
8487 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8488 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03008489
8490 switch (exit_reason) {
8491 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008492 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008493 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008494 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07008495 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008496 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008497 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008498 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008499 else if (is_debug(intr_info) &&
8500 vcpu->guest_debug &
8501 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8502 return false;
8503 else if (is_breakpoint(intr_info) &&
8504 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8505 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008506 return vmcs12->exception_bitmap &
8507 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8508 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008509 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008510 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008511 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008512 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008513 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008514 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008515 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008516 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008517 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008518 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008519 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008520 case EXIT_REASON_HLT:
8521 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8522 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008523 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008524 case EXIT_REASON_INVLPG:
8525 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8526 case EXIT_REASON_RDPMC:
8527 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008528 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008529 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008530 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008531 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008532 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008533 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8534 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8535 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8536 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8537 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8538 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008539 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008540 /*
8541 * VMX instructions trap unconditionally. This allows L1 to
8542 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8543 */
Joe Perches1d804d02015-03-30 16:46:09 -07008544 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008545 case EXIT_REASON_CR_ACCESS:
8546 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8547 case EXIT_REASON_DR_ACCESS:
8548 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8549 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008550 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008551 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8552 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03008553 case EXIT_REASON_MSR_READ:
8554 case EXIT_REASON_MSR_WRITE:
8555 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8556 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008557 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008558 case EXIT_REASON_MWAIT_INSTRUCTION:
8559 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008560 case EXIT_REASON_MONITOR_TRAP_FLAG:
8561 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008562 case EXIT_REASON_MONITOR_INSTRUCTION:
8563 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8564 case EXIT_REASON_PAUSE_INSTRUCTION:
8565 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8566 nested_cpu_has2(vmcs12,
8567 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8568 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008569 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008570 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008571 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008572 case EXIT_REASON_APIC_ACCESS:
8573 return nested_cpu_has2(vmcs12,
8574 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008575 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008576 case EXIT_REASON_EOI_INDUCED:
8577 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008578 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008579 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008580 /*
8581 * L0 always deals with the EPT violation. If nested EPT is
8582 * used, and the nested mmu code discovers that the address is
8583 * missing in the guest EPT table (EPT12), the EPT violation
8584 * will be injected with nested_ept_inject_page_fault()
8585 */
Joe Perches1d804d02015-03-30 16:46:09 -07008586 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008587 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008588 /*
8589 * L2 never uses directly L1's EPT, but rather L0's own EPT
8590 * table (shadow on EPT) or a merged EPT table that L0 built
8591 * (EPT on EPT). So any problems with the structure of the
8592 * table is L0's fault.
8593 */
Joe Perches1d804d02015-03-30 16:46:09 -07008594 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02008595 case EXIT_REASON_INVPCID:
8596 return
8597 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8598 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008599 case EXIT_REASON_WBINVD:
8600 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8601 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008602 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008603 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8604 /*
8605 * This should never happen, since it is not possible to
8606 * set XSS to a non-zero value---neither in L1 nor in L2.
8607 * If if it were, XSS would have to be checked against
8608 * the XSS exit bitmap in vmcs12.
8609 */
8610 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008611 case EXIT_REASON_PREEMPTION_TIMER:
8612 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02008613 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04008614 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02008615 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04008616 case EXIT_REASON_VMFUNC:
8617 /* VM functions are emulated through L2->L0 vmexits. */
8618 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008619 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008620 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008621 }
8622}
8623
Paolo Bonzini7313c692017-07-27 10:31:25 +02008624static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8625{
8626 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8627
8628 /*
8629 * At this point, the exit interruption info in exit_intr_info
8630 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8631 * we need to query the in-kernel LAPIC.
8632 */
8633 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8634 if ((exit_intr_info &
8635 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8636 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8637 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8638 vmcs12->vm_exit_intr_error_code =
8639 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8640 }
8641
8642 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8643 vmcs_readl(EXIT_QUALIFICATION));
8644 return 1;
8645}
8646
Avi Kivity586f9602010-11-18 13:09:54 +02008647static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8648{
8649 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8650 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8651}
8652
Kai Huanga3eaa862015-11-04 13:46:05 +08008653static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008654{
Kai Huanga3eaa862015-11-04 13:46:05 +08008655 if (vmx->pml_pg) {
8656 __free_page(vmx->pml_pg);
8657 vmx->pml_pg = NULL;
8658 }
Kai Huang843e4332015-01-28 10:54:28 +08008659}
8660
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008661static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008662{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008663 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008664 u64 *pml_buf;
8665 u16 pml_idx;
8666
8667 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8668
8669 /* Do nothing if PML buffer is empty */
8670 if (pml_idx == (PML_ENTITY_NUM - 1))
8671 return;
8672
8673 /* PML index always points to next available PML buffer entity */
8674 if (pml_idx >= PML_ENTITY_NUM)
8675 pml_idx = 0;
8676 else
8677 pml_idx++;
8678
8679 pml_buf = page_address(vmx->pml_pg);
8680 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8681 u64 gpa;
8682
8683 gpa = pml_buf[pml_idx];
8684 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008685 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008686 }
8687
8688 /* reset PML index */
8689 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8690}
8691
8692/*
8693 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8694 * Called before reporting dirty_bitmap to userspace.
8695 */
8696static void kvm_flush_pml_buffers(struct kvm *kvm)
8697{
8698 int i;
8699 struct kvm_vcpu *vcpu;
8700 /*
8701 * We only need to kick vcpu out of guest mode here, as PML buffer
8702 * is flushed at beginning of all VMEXITs, and it's obvious that only
8703 * vcpus running in guest are possible to have unflushed GPAs in PML
8704 * buffer.
8705 */
8706 kvm_for_each_vcpu(i, vcpu, kvm)
8707 kvm_vcpu_kick(vcpu);
8708}
8709
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008710static void vmx_dump_sel(char *name, uint32_t sel)
8711{
8712 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05008713 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008714 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8715 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8716 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8717}
8718
8719static void vmx_dump_dtsel(char *name, uint32_t limit)
8720{
8721 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8722 name, vmcs_read32(limit),
8723 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8724}
8725
8726static void dump_vmcs(void)
8727{
8728 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8729 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8730 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8731 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8732 u32 secondary_exec_control = 0;
8733 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008734 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008735 int i, n;
8736
8737 if (cpu_has_secondary_exec_ctrls())
8738 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8739
8740 pr_err("*** Guest State ***\n");
8741 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8742 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8743 vmcs_readl(CR0_GUEST_HOST_MASK));
8744 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8745 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8746 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8747 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8748 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8749 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008750 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8751 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8752 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8753 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008754 }
8755 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8756 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8757 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8758 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8759 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8760 vmcs_readl(GUEST_SYSENTER_ESP),
8761 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8762 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8763 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8764 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8765 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8766 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8767 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8768 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8769 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8770 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8771 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8772 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8773 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008774 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8775 efer, vmcs_read64(GUEST_IA32_PAT));
8776 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8777 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008778 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8779 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008780 pr_err("PerfGlobCtl = 0x%016llx\n",
8781 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008782 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008783 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008784 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8785 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8786 vmcs_read32(GUEST_ACTIVITY_STATE));
8787 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8788 pr_err("InterruptStatus = %04x\n",
8789 vmcs_read16(GUEST_INTR_STATUS));
8790
8791 pr_err("*** Host State ***\n");
8792 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8793 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8794 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8795 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8796 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8797 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8798 vmcs_read16(HOST_TR_SELECTOR));
8799 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8800 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8801 vmcs_readl(HOST_TR_BASE));
8802 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8803 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8804 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8805 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8806 vmcs_readl(HOST_CR4));
8807 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8808 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8809 vmcs_read32(HOST_IA32_SYSENTER_CS),
8810 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8811 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008812 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8813 vmcs_read64(HOST_IA32_EFER),
8814 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008815 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008816 pr_err("PerfGlobCtl = 0x%016llx\n",
8817 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008818
8819 pr_err("*** Control State ***\n");
8820 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8821 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8822 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8823 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8824 vmcs_read32(EXCEPTION_BITMAP),
8825 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8826 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8827 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8828 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8829 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8830 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8831 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8832 vmcs_read32(VM_EXIT_INTR_INFO),
8833 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8834 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8835 pr_err(" reason=%08x qualification=%016lx\n",
8836 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8837 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8838 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8839 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008840 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008841 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008842 pr_err("TSC Multiplier = 0x%016llx\n",
8843 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008844 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8845 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8846 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8847 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8848 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008849 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008850 n = vmcs_read32(CR3_TARGET_COUNT);
8851 for (i = 0; i + 1 < n; i += 4)
8852 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8853 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8854 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8855 if (i < n)
8856 pr_err("CR3 target%u=%016lx\n",
8857 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8858 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8859 pr_err("PLE Gap=%08x Window=%08x\n",
8860 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8861 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8862 pr_err("Virtual processor ID = 0x%04x\n",
8863 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8864}
8865
Avi Kivity6aa8b732006-12-10 02:21:36 -08008866/*
8867 * The guest has exited. See if we can fix it or if we need userspace
8868 * assistance.
8869 */
Avi Kivity851ba692009-08-24 11:10:17 +03008870static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008871{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008872 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008873 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008874 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008875
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008876 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8877
Kai Huang843e4332015-01-28 10:54:28 +08008878 /*
8879 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8880 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8881 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8882 * mode as if vcpus is in root mode, the PML buffer must has been
8883 * flushed already.
8884 */
8885 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008886 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008887
Mohammed Gamal80ced182009-09-01 12:48:18 +02008888 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008889 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008890 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008891
Paolo Bonzini7313c692017-07-27 10:31:25 +02008892 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
8893 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03008894
Mohammed Gamal51207022010-05-31 22:40:54 +03008895 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008896 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008897 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8898 vcpu->run->fail_entry.hardware_entry_failure_reason
8899 = exit_reason;
8900 return 0;
8901 }
8902
Avi Kivity29bd8a72007-09-10 17:27:03 +03008903 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008904 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8905 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008906 = vmcs_read32(VM_INSTRUCTION_ERROR);
8907 return 0;
8908 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008909
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008910 /*
8911 * Note:
8912 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8913 * delivery event since it indicates guest is accessing MMIO.
8914 * The vm-exit can be triggered again after return to guest that
8915 * will cause infinite loop.
8916 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008917 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008918 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008919 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008920 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008921 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8922 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8923 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02008924 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008925 vcpu->run->internal.data[0] = vectoring_info;
8926 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02008927 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
8928 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
8929 vcpu->run->internal.ndata++;
8930 vcpu->run->internal.data[3] =
8931 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
8932 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008933 return 0;
8934 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008935
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008936 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01008937 vmx->loaded_vmcs->soft_vnmi_blocked)) {
8938 if (vmx_interrupt_allowed(vcpu)) {
8939 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8940 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
8941 vcpu->arch.nmi_pending) {
8942 /*
8943 * This CPU don't support us in finding the end of an
8944 * NMI-blocked window if the guest runs with IRQs
8945 * disabled. So we pull the trigger after 1 s of
8946 * futile waiting, but inform the user about this.
8947 */
8948 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8949 "state on VCPU %d after 1 s timeout\n",
8950 __func__, vcpu->vcpu_id);
8951 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8952 }
8953 }
8954
Avi Kivity6aa8b732006-12-10 02:21:36 -08008955 if (exit_reason < kvm_vmx_max_exit_handlers
8956 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008957 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008958 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01008959 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
8960 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008961 kvm_queue_exception(vcpu, UD_VECTOR);
8962 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008963 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008964}
8965
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008966static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008967{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008968 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8969
8970 if (is_guest_mode(vcpu) &&
8971 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8972 return;
8973
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008974 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008975 vmcs_write32(TPR_THRESHOLD, 0);
8976 return;
8977 }
8978
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008979 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008980}
8981
Yang Zhang8d146952013-01-25 10:18:50 +08008982static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8983{
8984 u32 sec_exec_control;
8985
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008986 /* Postpone execution until vmcs01 is the current VMCS. */
8987 if (is_guest_mode(vcpu)) {
8988 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8989 return;
8990 }
8991
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008992 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008993 return;
8994
Paolo Bonzini35754c92015-07-29 12:05:37 +02008995 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008996 return;
8997
8998 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8999
9000 if (set) {
9001 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9002 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9003 } else {
9004 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9005 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009006 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009007 }
9008 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9009
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009010 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009011}
9012
Tang Chen38b99172014-09-24 15:57:54 +08009013static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9014{
9015 struct vcpu_vmx *vmx = to_vmx(vcpu);
9016
9017 /*
9018 * Currently we do not handle the nested case where L2 has an
9019 * APIC access page of its own; that page is still pinned.
9020 * Hence, we skip the case where the VCPU is in guest mode _and_
9021 * L1 prepared an APIC access page for L2.
9022 *
9023 * For the case where L1 and L2 share the same APIC access page
9024 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
9025 * in the vmcs12), this function will only update either the vmcs01
9026 * or the vmcs02. If the former, the vmcs02 will be updated by
9027 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
9028 * the next L2->L1 exit.
9029 */
9030 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07009031 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009032 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08009033 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009034 vmx_flush_tlb_ept_only(vcpu);
9035 }
Tang Chen38b99172014-09-24 15:57:54 +08009036}
9037
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009038static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009039{
9040 u16 status;
9041 u8 old;
9042
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009043 if (max_isr == -1)
9044 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009045
9046 status = vmcs_read16(GUEST_INTR_STATUS);
9047 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009048 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009049 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009050 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009051 vmcs_write16(GUEST_INTR_STATUS, status);
9052 }
9053}
9054
9055static void vmx_set_rvi(int vector)
9056{
9057 u16 status;
9058 u8 old;
9059
Wei Wang4114c272014-11-05 10:53:43 +08009060 if (vector == -1)
9061 vector = 0;
9062
Yang Zhangc7c9c562013-01-25 10:18:51 +08009063 status = vmcs_read16(GUEST_INTR_STATUS);
9064 old = (u8)status & 0xff;
9065 if ((u8)vector != old) {
9066 status &= ~0xff;
9067 status |= (u8)vector;
9068 vmcs_write16(GUEST_INTR_STATUS, status);
9069 }
9070}
9071
9072static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9073{
Liran Alon851c1a182017-12-24 18:12:56 +02009074 /*
9075 * When running L2, updating RVI is only relevant when
9076 * vmcs12 virtual-interrupt-delivery enabled.
9077 * However, it can be enabled only when L1 also
9078 * intercepts external-interrupts and in that case
9079 * we should not update vmcs02 RVI but instead intercept
9080 * interrupt. Therefore, do nothing when running L2.
9081 */
9082 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009083 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009084}
9085
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009086static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009087{
9088 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009089 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009090 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009091
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009092 WARN_ON(!vcpu->arch.apicv_active);
9093 if (pi_test_on(&vmx->pi_desc)) {
9094 pi_clear_on(&vmx->pi_desc);
9095 /*
9096 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9097 * But on x86 this is just a compiler barrier anyway.
9098 */
9099 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009100 max_irr_updated =
9101 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9102
9103 /*
9104 * If we are running L2 and L1 has a new pending interrupt
9105 * which can be injected, we should re-evaluate
9106 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009107 * If L1 intercepts external-interrupts, we should
9108 * exit from L2 to L1. Otherwise, interrupt should be
9109 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009110 */
Liran Alon851c1a182017-12-24 18:12:56 +02009111 if (is_guest_mode(vcpu) && max_irr_updated) {
9112 if (nested_exit_on_intr(vcpu))
9113 kvm_vcpu_exiting_guest_mode(vcpu);
9114 else
9115 kvm_make_request(KVM_REQ_EVENT, vcpu);
9116 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009117 } else {
9118 max_irr = kvm_lapic_find_highest_irr(vcpu);
9119 }
9120 vmx_hwapic_irr_update(vcpu, max_irr);
9121 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009122}
9123
Andrey Smetanin63086302015-11-10 15:36:32 +03009124static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009125{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009126 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009127 return;
9128
Yang Zhangc7c9c562013-01-25 10:18:51 +08009129 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9130 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9131 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9132 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9133}
9134
Paolo Bonzini967235d2016-12-19 14:03:45 +01009135static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9136{
9137 struct vcpu_vmx *vmx = to_vmx(vcpu);
9138
9139 pi_clear_on(&vmx->pi_desc);
9140 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9141}
9142
Avi Kivity51aa01d2010-07-20 14:31:20 +03009143static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009144{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009145 u32 exit_intr_info = 0;
9146 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009147
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009148 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9149 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009150 return;
9151
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009152 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9153 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9154 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009155
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009156 /* if exit due to PF check for async PF */
9157 if (is_page_fault(exit_intr_info))
9158 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9159
Andi Kleena0861c02009-06-08 17:37:09 +08009160 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009161 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9162 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009163 kvm_machine_check();
9164
Gleb Natapov20f65982009-05-11 13:35:55 +03009165 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009166 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009167 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009168 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009169 kvm_after_handle_nmi(&vmx->vcpu);
9170 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009171}
Gleb Natapov20f65982009-05-11 13:35:55 +03009172
Yang Zhanga547c6d2013-04-11 19:25:10 +08009173static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9174{
9175 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9176
Yang Zhanga547c6d2013-04-11 19:25:10 +08009177 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9178 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9179 unsigned int vector;
9180 unsigned long entry;
9181 gate_desc *desc;
9182 struct vcpu_vmx *vmx = to_vmx(vcpu);
9183#ifdef CONFIG_X86_64
9184 unsigned long tmp;
9185#endif
9186
9187 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9188 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009189 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009190 asm volatile(
9191#ifdef CONFIG_X86_64
9192 "mov %%" _ASM_SP ", %[sp]\n\t"
9193 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9194 "push $%c[ss]\n\t"
9195 "push %[sp]\n\t"
9196#endif
9197 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009198 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009199 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009200 :
9201#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009202 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009203#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009204 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009205 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009206 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009207 [ss]"i"(__KERNEL_DS),
9208 [cs]"i"(__KERNEL_CS)
9209 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009210 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009211}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009212STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009213
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009214static bool vmx_has_high_real_mode_segbase(void)
9215{
9216 return enable_unrestricted_guest || emulate_invalid_guest_state;
9217}
9218
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009219static bool vmx_mpx_supported(void)
9220{
9221 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9222 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9223}
9224
Wanpeng Li55412b22014-12-02 19:21:30 +08009225static bool vmx_xsaves_supported(void)
9226{
9227 return vmcs_config.cpu_based_2nd_exec_ctrl &
9228 SECONDARY_EXEC_XSAVES;
9229}
9230
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009231static bool vmx_umip_emulated(void)
9232{
Paolo Bonzini0367f202016-07-12 10:44:55 +02009233 return vmcs_config.cpu_based_2nd_exec_ctrl &
9234 SECONDARY_EXEC_DESC;
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009235}
9236
Avi Kivity51aa01d2010-07-20 14:31:20 +03009237static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9238{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009239 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009240 bool unblock_nmi;
9241 u8 vector;
9242 bool idtv_info_valid;
9243
9244 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009245
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009246 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009247 if (vmx->loaded_vmcs->nmi_known_unmasked)
9248 return;
9249 /*
9250 * Can't use vmx->exit_intr_info since we're not sure what
9251 * the exit reason is.
9252 */
9253 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9254 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9255 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9256 /*
9257 * SDM 3: 27.7.1.2 (September 2008)
9258 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9259 * a guest IRET fault.
9260 * SDM 3: 23.2.2 (September 2008)
9261 * Bit 12 is undefined in any of the following cases:
9262 * If the VM exit sets the valid bit in the IDT-vectoring
9263 * information field.
9264 * If the VM exit is due to a double fault.
9265 */
9266 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9267 vector != DF_VECTOR && !idtv_info_valid)
9268 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9269 GUEST_INTR_STATE_NMI);
9270 else
9271 vmx->loaded_vmcs->nmi_known_unmasked =
9272 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9273 & GUEST_INTR_STATE_NMI);
9274 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9275 vmx->loaded_vmcs->vnmi_blocked_time +=
9276 ktime_to_ns(ktime_sub(ktime_get(),
9277 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009278}
9279
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009280static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009281 u32 idt_vectoring_info,
9282 int instr_len_field,
9283 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009284{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009285 u8 vector;
9286 int type;
9287 bool idtv_info_valid;
9288
9289 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009290
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009291 vcpu->arch.nmi_injected = false;
9292 kvm_clear_exception_queue(vcpu);
9293 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009294
9295 if (!idtv_info_valid)
9296 return;
9297
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009298 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009299
Avi Kivity668f6122008-07-02 09:28:55 +03009300 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9301 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009302
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009303 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009304 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009305 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009306 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009307 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009308 * Clear bit "block by NMI" before VM entry if a NMI
9309 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009310 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009311 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009312 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009313 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009314 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009315 /* fall through */
9316 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009317 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009318 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009319 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009320 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009321 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009322 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009323 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009324 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009325 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009326 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009327 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009328 break;
9329 default:
9330 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009331 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009332}
9333
Avi Kivity83422e12010-07-20 14:43:23 +03009334static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9335{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009336 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009337 VM_EXIT_INSTRUCTION_LEN,
9338 IDT_VECTORING_ERROR_CODE);
9339}
9340
Avi Kivityb463a6f2010-07-20 15:06:17 +03009341static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9342{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009343 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009344 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9345 VM_ENTRY_INSTRUCTION_LEN,
9346 VM_ENTRY_EXCEPTION_ERROR_CODE);
9347
9348 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9349}
9350
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009351static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9352{
9353 int i, nr_msrs;
9354 struct perf_guest_switch_msr *msrs;
9355
9356 msrs = perf_guest_get_msrs(&nr_msrs);
9357
9358 if (!msrs)
9359 return;
9360
9361 for (i = 0; i < nr_msrs; i++)
9362 if (msrs[i].host == msrs[i].guest)
9363 clear_atomic_switch_msr(vmx, msrs[i].msr);
9364 else
9365 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9366 msrs[i].host);
9367}
9368
Jiang Biao33365e72016-11-03 15:03:37 +08009369static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009370{
9371 struct vcpu_vmx *vmx = to_vmx(vcpu);
9372 u64 tscl;
9373 u32 delta_tsc;
9374
9375 if (vmx->hv_deadline_tsc == -1)
9376 return;
9377
9378 tscl = rdtsc();
9379 if (vmx->hv_deadline_tsc > tscl)
9380 /* sure to be 32 bit only because checked on set_hv_timer */
9381 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9382 cpu_preemption_timer_multi);
9383 else
9384 delta_tsc = 0;
9385
9386 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9387}
9388
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009389static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009390{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009391 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Li74c55932017-11-29 01:31:20 -08009392 unsigned long cr3, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02009393
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009394 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009395 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009396 vmx->loaded_vmcs->soft_vnmi_blocked))
9397 vmx->loaded_vmcs->entry_time = ktime_get();
9398
Avi Kivity104f2262010-11-18 13:12:52 +02009399 /* Don't enter VMX if guest state is invalid, let the exit handler
9400 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009401 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009402 return;
9403
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009404 if (vmx->ple_window_dirty) {
9405 vmx->ple_window_dirty = false;
9406 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9407 }
9408
Abel Gordon012f83c2013-04-18 14:39:25 +03009409 if (vmx->nested.sync_shadow_vmcs) {
9410 copy_vmcs12_to_shadow(vmx);
9411 vmx->nested.sync_shadow_vmcs = false;
9412 }
9413
Avi Kivity104f2262010-11-18 13:12:52 +02009414 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9415 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9416 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9417 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9418
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009419 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +02009420 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009421 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +02009422 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009423 }
9424
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009425 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +02009426 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009427 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +02009428 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009429 }
9430
Avi Kivity104f2262010-11-18 13:12:52 +02009431 /* When single-stepping over STI and MOV SS, we must clear the
9432 * corresponding interruptibility bits in the guest state. Otherwise
9433 * vmentry fails as it then expects bit 14 (BS) in pending debug
9434 * exceptions being set, but that's not correct for the guest debugging
9435 * case. */
9436 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9437 vmx_set_interrupt_shadow(vcpu, 0);
9438
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009439 if (static_cpu_has(X86_FEATURE_PKU) &&
9440 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9441 vcpu->arch.pkru != vmx->host_pkru)
9442 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009443
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009444 atomic_switch_perf_msrs(vmx);
9445
Yunhong Jiang64672c92016-06-13 14:19:59 -07009446 vmx_arm_hv_timer(vcpu);
9447
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009448 /*
9449 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9450 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9451 * is no need to worry about the conditional branch over the wrmsr
9452 * being speculatively taken.
9453 */
9454 if (vmx->spec_ctrl)
9455 wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
9456
Nadav Har'Eld462b812011-05-24 15:26:10 +03009457 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02009458 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009459 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009460 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9461 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9462 "push %%" _ASM_CX " \n\t"
9463 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009464 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009465 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009466 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009467 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009468 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009469 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9470 "mov %%cr2, %%" _ASM_DX " \n\t"
9471 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009472 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009473 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009474 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009475 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009476 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009477 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009478 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9479 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9480 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9481 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9482 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9483 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009484#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009485 "mov %c[r8](%0), %%r8 \n\t"
9486 "mov %c[r9](%0), %%r9 \n\t"
9487 "mov %c[r10](%0), %%r10 \n\t"
9488 "mov %c[r11](%0), %%r11 \n\t"
9489 "mov %c[r12](%0), %%r12 \n\t"
9490 "mov %c[r13](%0), %%r13 \n\t"
9491 "mov %c[r14](%0), %%r14 \n\t"
9492 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009493#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009494 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009495
Avi Kivity6aa8b732006-12-10 02:21:36 -08009496 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009497 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009498 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009499 "jmp 2f \n\t"
9500 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9501 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009502 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009503 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009504 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009505 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009506 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9507 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9508 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9509 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9510 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9511 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9512 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009513#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009514 "mov %%r8, %c[r8](%0) \n\t"
9515 "mov %%r9, %c[r9](%0) \n\t"
9516 "mov %%r10, %c[r10](%0) \n\t"
9517 "mov %%r11, %c[r11](%0) \n\t"
9518 "mov %%r12, %c[r12](%0) \n\t"
9519 "mov %%r13, %c[r13](%0) \n\t"
9520 "mov %%r14, %c[r14](%0) \n\t"
9521 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009522 "xor %%r8d, %%r8d \n\t"
9523 "xor %%r9d, %%r9d \n\t"
9524 "xor %%r10d, %%r10d \n\t"
9525 "xor %%r11d, %%r11d \n\t"
9526 "xor %%r12d, %%r12d \n\t"
9527 "xor %%r13d, %%r13d \n\t"
9528 "xor %%r14d, %%r14d \n\t"
9529 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009530#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009531 "mov %%cr2, %%" _ASM_AX " \n\t"
9532 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009533
Jim Mattson0cb5b302018-01-03 14:31:38 -08009534 "xor %%eax, %%eax \n\t"
9535 "xor %%ebx, %%ebx \n\t"
9536 "xor %%esi, %%esi \n\t"
9537 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009538 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009539 ".pushsection .rodata \n\t"
9540 ".global vmx_return \n\t"
9541 "vmx_return: " _ASM_PTR " 2b \n\t"
9542 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009543 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009544 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009545 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03009546 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009547 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9548 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9549 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9550 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9551 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9552 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9553 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009554#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009555 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9556 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9557 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9558 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9559 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9560 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9561 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9562 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009563#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009564 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9565 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009566 : "cc", "memory"
9567#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009568 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009569 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009570#else
9571 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009572#endif
9573 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009574
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009575 /*
9576 * We do not use IBRS in the kernel. If this vCPU has used the
9577 * SPEC_CTRL MSR it may have left it on; save the value and
9578 * turn it off. This is much more efficient than blindly adding
9579 * it to the atomic save/restore list. Especially as the former
9580 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9581 *
9582 * For non-nested case:
9583 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9584 * save it.
9585 *
9586 * For nested case:
9587 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9588 * save it.
9589 */
9590 if (!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))
9591 rdmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
9592
9593 if (vmx->spec_ctrl)
9594 wrmsrl(MSR_IA32_SPEC_CTRL, 0);
9595
David Woodhouse117cc7a2018-01-12 11:11:27 +00009596 /* Eliminate branch target predictions from guest mode */
9597 vmexit_fill_RSB();
9598
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009599 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08009600 if (vmx->host_debugctlmsr)
9601 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009602
Avi Kivityaa67f602012-08-01 16:48:03 +03009603#ifndef CONFIG_X86_64
9604 /*
9605 * The sysexit path does not restore ds/es, so we must set them to
9606 * a reasonable value ourselves.
9607 *
9608 * We can't defer this to vmx_load_host_state() since that function
9609 * may be executed in interrupt context, which saves and restore segments
9610 * around it, nullifying its effect.
9611 */
9612 loadsegment(ds, __USER_DS);
9613 loadsegment(es, __USER_DS);
9614#endif
9615
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009616 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009617 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009618 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009619 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009620 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009621 vcpu->arch.regs_dirty = 0;
9622
Gleb Natapove0b890d2013-09-25 12:51:33 +03009623 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009624 * eager fpu is enabled if PKEY is supported and CR4 is switched
9625 * back on host, so it is safe to read guest PKRU from current
9626 * XSAVE.
9627 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009628 if (static_cpu_has(X86_FEATURE_PKU) &&
9629 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9630 vcpu->arch.pkru = __read_pkru();
9631 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009632 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009633 }
9634
9635 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009636 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9637 * we did not inject a still-pending event to L1 now because of
9638 * nested_run_pending, we need to re-enable this bit.
9639 */
9640 if (vmx->nested.nested_run_pending)
9641 kvm_make_request(KVM_REQ_EVENT, vcpu);
9642
9643 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07009644 vmx->idt_vectoring_info = 0;
9645
9646 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9647 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9648 return;
9649
9650 vmx->loaded_vmcs->launched = 1;
9651 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03009652
Avi Kivity51aa01d2010-07-20 14:31:20 +03009653 vmx_complete_atomic_exit(vmx);
9654 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009655 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009656}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009657STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009658
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009659static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009660{
9661 struct vcpu_vmx *vmx = to_vmx(vcpu);
9662 int cpu;
9663
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009664 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009665 return;
9666
9667 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009668 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009669 vmx_vcpu_put(vcpu);
9670 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009671 put_cpu();
9672}
9673
Jim Mattson2f1fe812016-07-08 15:36:06 -07009674/*
9675 * Ensure that the current vmcs of the logical processor is the
9676 * vmcs01 of the vcpu before calling free_nested().
9677 */
9678static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9679{
9680 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009681
Christoffer Dallec7660c2017-12-04 21:35:23 +01009682 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009683 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009684 free_nested(vmx);
9685 vcpu_put(vcpu);
9686}
9687
Avi Kivity6aa8b732006-12-10 02:21:36 -08009688static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9689{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009690 struct vcpu_vmx *vmx = to_vmx(vcpu);
9691
Kai Huang843e4332015-01-28 10:54:28 +08009692 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009693 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009694 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009695 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009696 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009697 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009698 kfree(vmx->guest_msrs);
9699 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009700 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009701}
9702
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009703static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009704{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009705 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009706 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009707 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03009708 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009709
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009710 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009711 return ERR_PTR(-ENOMEM);
9712
Wanpeng Li991e7a02015-09-16 17:30:05 +08009713 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009714
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009715 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9716 if (err)
9717 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009718
Peter Feiner4e595162016-07-07 14:49:58 -07009719 err = -ENOMEM;
9720
9721 /*
9722 * If PML is turned on, failure on enabling PML just results in failure
9723 * of creating the vcpu, therefore we can simplify PML logic (by
9724 * avoiding dealing with cases, such as enabling PML partially on vcpus
9725 * for the guest, etc.
9726 */
9727 if (enable_pml) {
9728 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9729 if (!vmx->pml_pg)
9730 goto uninit_vcpu;
9731 }
9732
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009733 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009734 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9735 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009736
Peter Feiner4e595162016-07-07 14:49:58 -07009737 if (!vmx->guest_msrs)
9738 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009739
Paolo Bonzinif21f1652018-01-11 12:16:15 +01009740 err = alloc_loaded_vmcs(&vmx->vmcs01);
9741 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009742 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009743
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009744 msr_bitmap = vmx->vmcs01.msr_bitmap;
9745 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
9746 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
9747 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
9748 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
9749 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
9750 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
9751 vmx->msr_bitmap_mode = 0;
9752
Paolo Bonzinif21f1652018-01-11 12:16:15 +01009753 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03009754 cpu = get_cpu();
9755 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009756 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +02009757 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009758 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009759 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +02009760 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009761 err = alloc_apic_access_page(kvm);
9762 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009763 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009764 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009765
Sheng Yangb927a3c2009-07-21 10:42:48 +08009766 if (enable_ept) {
Tang Chenf51770e2014-09-16 18:41:59 +08009767 err = init_rmode_identity_map(kvm);
9768 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009769 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009770 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009771
Wanpeng Li5c614b32015-10-13 09:18:36 -07009772 if (nested) {
Wincy Vanb9c237b2015-02-03 23:56:30 +08009773 nested_vmx_setup_ctls_msrs(vmx);
Wanpeng Li5c614b32015-10-13 09:18:36 -07009774 vmx->nested.vpid02 = allocate_vpid();
9775 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009776
Wincy Van705699a2015-02-03 23:58:17 +08009777 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009778 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009779
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009780 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9781
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02009782 /*
9783 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9784 * or POSTED_INTR_WAKEUP_VECTOR.
9785 */
9786 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9787 vmx->pi_desc.sn = 1;
9788
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009789 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009790
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009791free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009792 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009793 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009794free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009795 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009796free_pml:
9797 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009798uninit_vcpu:
9799 kvm_vcpu_uninit(&vmx->vcpu);
9800free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009801 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009802 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009803 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009804}
9805
Yang, Sheng002c7f72007-07-31 14:23:01 +03009806static void __init vmx_check_processor_compat(void *rtn)
9807{
9808 struct vmcs_config vmcs_conf;
9809
9810 *(int *)rtn = 0;
9811 if (setup_vmcs_config(&vmcs_conf) < 0)
9812 *(int *)rtn = -EIO;
9813 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9814 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9815 smp_processor_id());
9816 *(int *)rtn = -EIO;
9817 }
9818}
9819
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009820static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009821{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009822 u8 cache;
9823 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009824
Sheng Yang522c68c2009-04-27 20:35:43 +08009825 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009826 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009827 * 2. EPT with VT-d:
9828 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009829 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009830 * b. VT-d with snooping control feature: snooping control feature of
9831 * VT-d engine can guarantee the cache correctness. Just set it
9832 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009833 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009834 * consistent with host MTRR
9835 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009836 if (is_mmio) {
9837 cache = MTRR_TYPE_UNCACHABLE;
9838 goto exit;
9839 }
9840
9841 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009842 ipat = VMX_EPT_IPAT_BIT;
9843 cache = MTRR_TYPE_WRBACK;
9844 goto exit;
9845 }
9846
9847 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9848 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009849 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009850 cache = MTRR_TYPE_WRBACK;
9851 else
9852 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009853 goto exit;
9854 }
9855
Xiao Guangrongff536042015-06-15 16:55:22 +08009856 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009857
9858exit:
9859 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009860}
9861
Sheng Yang17cc3932010-01-05 19:02:27 +08009862static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009863{
Sheng Yang878403b2010-01-05 19:02:29 +08009864 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9865 return PT_DIRECTORY_LEVEL;
9866 else
9867 /* For shadow and EPT supported 1GB page */
9868 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009869}
9870
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009871static void vmcs_set_secondary_exec_control(u32 new_ctl)
9872{
9873 /*
9874 * These bits in the secondary execution controls field
9875 * are dynamic, the others are mostly based on the hypervisor
9876 * architecture and the guest's CPUID. Do not touch the
9877 * dynamic bits.
9878 */
9879 u32 mask =
9880 SECONDARY_EXEC_SHADOW_VMCS |
9881 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02009882 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9883 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009884
9885 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9886
9887 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9888 (new_ctl & ~mask) | (cur_ctl & mask));
9889}
9890
David Matlack8322ebb2016-11-29 18:14:09 -08009891/*
9892 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
9893 * (indicating "allowed-1") if they are supported in the guest's CPUID.
9894 */
9895static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
9896{
9897 struct vcpu_vmx *vmx = to_vmx(vcpu);
9898 struct kvm_cpuid_entry2 *entry;
9899
9900 vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff;
9901 vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE;
9902
9903#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
9904 if (entry && (entry->_reg & (_cpuid_mask))) \
9905 vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \
9906} while (0)
9907
9908 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
9909 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
9910 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
9911 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
9912 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
9913 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
9914 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
9915 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
9916 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
9917 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
9918 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
9919 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
9920 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
9921 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
9922 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
9923
9924 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9925 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
9926 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
9927 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
9928 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +01009929 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -08009930
9931#undef cr4_fixed1_update
9932}
9933
Sheng Yang0e851882009-12-18 16:48:46 +08009934static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9935{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009936 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009937
Paolo Bonzini80154d72017-08-24 13:55:35 +02009938 if (cpu_has_secondary_exec_ctrls()) {
9939 vmx_compute_secondary_exec_control(vmx);
9940 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009941 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009942
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009943 if (nested_vmx_allowed(vcpu))
9944 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9945 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9946 else
9947 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9948 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -08009949
9950 if (nested_vmx_allowed(vcpu))
9951 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08009952}
9953
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009954static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9955{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009956 if (func == 1 && nested)
9957 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009958}
9959
Yang Zhang25d92082013-08-06 12:00:32 +03009960static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9961 struct x86_exception *fault)
9962{
Jan Kiszka533558b2014-01-04 18:47:20 +01009963 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -04009964 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +01009965 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -04009966 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +03009967
Bandan Dasc5f983f2017-05-05 15:25:14 -04009968 if (vmx->nested.pml_full) {
9969 exit_reason = EXIT_REASON_PML_FULL;
9970 vmx->nested.pml_full = false;
9971 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
9972 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009973 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009974 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009975 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -04009976
9977 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009978 vmcs12->guest_physical_address = fault->address;
9979}
9980
Peter Feiner995f00a2017-06-30 17:26:32 -07009981static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
9982{
David Hildenbrandbb97a012017-08-10 23:15:28 +02009983 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -07009984}
9985
Nadav Har'El155a97a2013-08-05 11:07:16 +03009986/* Callbacks for nested_ept_init_mmu_context: */
9987
9988static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9989{
9990 /* return the page table to be shadowed - in our case, EPT12 */
9991 return get_vmcs12(vcpu)->ept_pointer;
9992}
9993
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009994static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009995{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009996 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +02009997 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009998 return 1;
9999
10000 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010001 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010002 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010003 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010004 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010005 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10006 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10007 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10008
10009 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010010 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010011}
10012
10013static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10014{
10015 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10016}
10017
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010018static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10019 u16 error_code)
10020{
10021 bool inequality, bit;
10022
10023 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10024 inequality =
10025 (error_code & vmcs12->page_fault_error_code_mask) !=
10026 vmcs12->page_fault_error_code_match;
10027 return inequality ^ bit;
10028}
10029
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010030static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10031 struct x86_exception *fault)
10032{
10033 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10034
10035 WARN_ON(!is_guest_mode(vcpu));
10036
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010037 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10038 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010039 vmcs12->vm_exit_intr_error_code = fault->error_code;
10040 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10041 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10042 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10043 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010044 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010045 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010046 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010047}
10048
Paolo Bonzinic9923842017-12-13 14:16:30 +010010049static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10050 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010051
10052static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010053 struct vmcs12 *vmcs12)
10054{
10055 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010056 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010057 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010058
10059 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010060 /*
10061 * Translate L1 physical address to host physical
10062 * address for vmcs02. Keep the page pinned, so this
10063 * physical address remains valid. We keep a reference
10064 * to it so we can release it later.
10065 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010066 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010067 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010068 vmx->nested.apic_access_page = NULL;
10069 }
10070 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010071 /*
10072 * If translation failed, no matter: This feature asks
10073 * to exit when accessing the given address, and if it
10074 * can never be accessed, this feature won't do
10075 * anything anyway.
10076 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010077 if (!is_error_page(page)) {
10078 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010079 hpa = page_to_phys(vmx->nested.apic_access_page);
10080 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10081 } else {
10082 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10083 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10084 }
10085 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
10086 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
10087 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
10088 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10089 kvm_vcpu_reload_apic_access_page(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010090 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010091
10092 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010093 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010094 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010095 vmx->nested.virtual_apic_page = NULL;
10096 }
10097 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010098
10099 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010100 * If translation failed, VM entry will fail because
10101 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10102 * Failing the vm entry is _not_ what the processor
10103 * does but it's basically the only possibility we
10104 * have. We could still enter the guest if CR8 load
10105 * exits are enabled, CR8 store exits are enabled, and
10106 * virtualize APIC access is disabled; in this case
10107 * the processor would never use the TPR shadow and we
10108 * could simply clear the bit from the execution
10109 * control. But such a configuration is useless, so
10110 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010111 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010112 if (!is_error_page(page)) {
10113 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010114 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10115 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10116 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010117 }
10118
Wincy Van705699a2015-02-03 23:58:17 +080010119 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010120 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10121 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010122 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010123 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010124 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010125 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10126 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010127 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010128 vmx->nested.pi_desc_page = page;
10129 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010130 vmx->nested.pi_desc =
10131 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10132 (unsigned long)(vmcs12->posted_intr_desc_addr &
10133 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010134 vmcs_write64(POSTED_INTR_DESC_ADDR,
10135 page_to_phys(vmx->nested.pi_desc_page) +
10136 (unsigned long)(vmcs12->posted_intr_desc_addr &
10137 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010138 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010139 if (!nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010140 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10141 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010142}
10143
Jan Kiszkaf41245002014-03-07 20:03:13 +010010144static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10145{
10146 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10147 struct vcpu_vmx *vmx = to_vmx(vcpu);
10148
10149 if (vcpu->arch.virtual_tsc_khz == 0)
10150 return;
10151
10152 /* Make sure short timeouts reliably trigger an immediate vmexit.
10153 * hrtimer_start does not guarantee this. */
10154 if (preemption_timeout <= 1) {
10155 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10156 return;
10157 }
10158
10159 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10160 preemption_timeout *= 1000000;
10161 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10162 hrtimer_start(&vmx->nested.preemption_timer,
10163 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10164}
10165
Jim Mattson56a20512017-07-06 16:33:06 -070010166static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10167 struct vmcs12 *vmcs12)
10168{
10169 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10170 return 0;
10171
10172 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10173 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10174 return -EINVAL;
10175
10176 return 0;
10177}
10178
Wincy Van3af18d92015-02-03 23:49:31 +080010179static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10180 struct vmcs12 *vmcs12)
10181{
Wincy Van3af18d92015-02-03 23:49:31 +080010182 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10183 return 0;
10184
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010185 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010186 return -EINVAL;
10187
10188 return 0;
10189}
10190
Jim Mattson712b12d2017-08-24 13:24:47 -070010191static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10192 struct vmcs12 *vmcs12)
10193{
10194 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10195 return 0;
10196
10197 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10198 return -EINVAL;
10199
10200 return 0;
10201}
10202
Wincy Van3af18d92015-02-03 23:49:31 +080010203/*
10204 * Merge L0's and L1's MSR bitmap, return false to indicate that
10205 * we do not use the hardware.
10206 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010207static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10208 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010209{
Wincy Van82f0dd42015-02-03 23:57:18 +080010210 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010211 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010212 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010213 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010214 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010215 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010216 *
10217 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10218 * ensures that we do not accidentally generate an L02 MSR bitmap
10219 * from the L12 MSR bitmap that is too permissive.
10220 * 2. That L1 or L2s have actually used the MSR. This avoids
10221 * unnecessarily merging of the bitmap if the MSR is unused. This
10222 * works properly because we only update the L01 MSR bitmap lazily.
10223 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10224 * updated to reflect this when L1 (or its L2s) actually write to
10225 * the MSR.
10226 */
10227 bool pred_cmd = msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010228 bool spec_ctrl = msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010229
Paolo Bonzinic9923842017-12-13 14:16:30 +010010230 /* Nothing to do if the MSR bitmap is not in use. */
10231 if (!cpu_has_vmx_msr_bitmap() ||
10232 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10233 return false;
10234
Ashok Raj15d45072018-02-01 22:59:43 +010010235 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010236 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010237 return false;
10238
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010239 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10240 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010241 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010242
Radim Krčmářd048c092016-08-08 20:16:22 +020010243 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010244 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10245 /*
10246 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10247 * just lets the processor take the value from the virtual-APIC page;
10248 * take those 256 bits directly from the L1 bitmap.
10249 */
10250 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10251 unsigned word = msr / BITS_PER_LONG;
10252 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10253 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010254 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010255 } else {
10256 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10257 unsigned word = msr / BITS_PER_LONG;
10258 msr_bitmap_l0[word] = ~0;
10259 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10260 }
10261 }
10262
10263 nested_vmx_disable_intercept_for_msr(
10264 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010265 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010266 MSR_TYPE_W);
10267
10268 if (nested_cpu_has_vid(vmcs12)) {
10269 nested_vmx_disable_intercept_for_msr(
10270 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010271 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010272 MSR_TYPE_W);
10273 nested_vmx_disable_intercept_for_msr(
10274 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010275 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010276 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010277 }
Ashok Raj15d45072018-02-01 22:59:43 +010010278
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010279 if (spec_ctrl)
10280 nested_vmx_disable_intercept_for_msr(
10281 msr_bitmap_l1, msr_bitmap_l0,
10282 MSR_IA32_SPEC_CTRL,
10283 MSR_TYPE_R | MSR_TYPE_W);
10284
Ashok Raj15d45072018-02-01 22:59:43 +010010285 if (pred_cmd)
10286 nested_vmx_disable_intercept_for_msr(
10287 msr_bitmap_l1, msr_bitmap_l0,
10288 MSR_IA32_PRED_CMD,
10289 MSR_TYPE_W);
10290
Wincy Vanf2b93282015-02-03 23:56:03 +080010291 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010292 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010293
10294 return true;
10295}
10296
10297static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10298 struct vmcs12 *vmcs12)
10299{
Wincy Van82f0dd42015-02-03 23:57:18 +080010300 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010301 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010302 !nested_cpu_has_vid(vmcs12) &&
10303 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010304 return 0;
10305
10306 /*
10307 * If virtualize x2apic mode is enabled,
10308 * virtualize apic access must be disabled.
10309 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010310 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10311 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010312 return -EINVAL;
10313
Wincy Van608406e2015-02-03 23:57:51 +080010314 /*
10315 * If virtual interrupt delivery is enabled,
10316 * we must exit on external interrupts.
10317 */
10318 if (nested_cpu_has_vid(vmcs12) &&
10319 !nested_exit_on_intr(vcpu))
10320 return -EINVAL;
10321
Wincy Van705699a2015-02-03 23:58:17 +080010322 /*
10323 * bits 15:8 should be zero in posted_intr_nv,
10324 * the descriptor address has been already checked
10325 * in nested_get_vmcs12_pages.
10326 */
10327 if (nested_cpu_has_posted_intr(vmcs12) &&
10328 (!nested_cpu_has_vid(vmcs12) ||
10329 !nested_exit_intr_ack_set(vcpu) ||
10330 vmcs12->posted_intr_nv & 0xff00))
10331 return -EINVAL;
10332
Wincy Vanf2b93282015-02-03 23:56:03 +080010333 /* tpr shadow is needed by all apicv features. */
10334 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10335 return -EINVAL;
10336
10337 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010338}
10339
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010340static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10341 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010342 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010343{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010344 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010345 u64 count, addr;
10346
10347 if (vmcs12_read_any(vcpu, count_field, &count) ||
10348 vmcs12_read_any(vcpu, addr_field, &addr)) {
10349 WARN_ON(1);
10350 return -EINVAL;
10351 }
10352 if (count == 0)
10353 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010354 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010355 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10356 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010357 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010358 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10359 addr_field, maxphyaddr, count, addr);
10360 return -EINVAL;
10361 }
10362 return 0;
10363}
10364
10365static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10366 struct vmcs12 *vmcs12)
10367{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010368 if (vmcs12->vm_exit_msr_load_count == 0 &&
10369 vmcs12->vm_exit_msr_store_count == 0 &&
10370 vmcs12->vm_entry_msr_load_count == 0)
10371 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010372 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010373 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010374 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010375 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010376 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010377 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010378 return -EINVAL;
10379 return 0;
10380}
10381
Bandan Dasc5f983f2017-05-05 15:25:14 -040010382static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10383 struct vmcs12 *vmcs12)
10384{
10385 u64 address = vmcs12->pml_address;
10386 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10387
10388 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10389 if (!nested_cpu_has_ept(vmcs12) ||
10390 !IS_ALIGNED(address, 4096) ||
10391 address >> maxphyaddr)
10392 return -EINVAL;
10393 }
10394
10395 return 0;
10396}
10397
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010398static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10399 struct vmx_msr_entry *e)
10400{
10401 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010402 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010403 return -EINVAL;
10404 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10405 e->index == MSR_IA32_UCODE_REV)
10406 return -EINVAL;
10407 if (e->reserved != 0)
10408 return -EINVAL;
10409 return 0;
10410}
10411
10412static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10413 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010414{
10415 if (e->index == MSR_FS_BASE ||
10416 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010417 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10418 nested_vmx_msr_check_common(vcpu, e))
10419 return -EINVAL;
10420 return 0;
10421}
10422
10423static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10424 struct vmx_msr_entry *e)
10425{
10426 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10427 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010428 return -EINVAL;
10429 return 0;
10430}
10431
10432/*
10433 * Load guest's/host's msr at nested entry/exit.
10434 * return 0 for success, entry index for failure.
10435 */
10436static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10437{
10438 u32 i;
10439 struct vmx_msr_entry e;
10440 struct msr_data msr;
10441
10442 msr.host_initiated = false;
10443 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010444 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10445 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010446 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010447 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10448 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010449 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010450 }
10451 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010452 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010453 "%s check failed (%u, 0x%x, 0x%x)\n",
10454 __func__, i, e.index, e.reserved);
10455 goto fail;
10456 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010457 msr.index = e.index;
10458 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010459 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010460 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010461 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10462 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010463 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010464 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010465 }
10466 return 0;
10467fail:
10468 return i + 1;
10469}
10470
10471static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10472{
10473 u32 i;
10474 struct vmx_msr_entry e;
10475
10476 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010477 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010478 if (kvm_vcpu_read_guest(vcpu,
10479 gpa + i * sizeof(e),
10480 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010481 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010482 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10483 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010484 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010485 }
10486 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010487 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010488 "%s check failed (%u, 0x%x, 0x%x)\n",
10489 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010490 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010491 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010492 msr_info.host_initiated = false;
10493 msr_info.index = e.index;
10494 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010495 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010496 "%s cannot read MSR (%u, 0x%x)\n",
10497 __func__, i, e.index);
10498 return -EINVAL;
10499 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010500 if (kvm_vcpu_write_guest(vcpu,
10501 gpa + i * sizeof(e) +
10502 offsetof(struct vmx_msr_entry, value),
10503 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010504 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010505 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010506 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010507 return -EINVAL;
10508 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010509 }
10510 return 0;
10511}
10512
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010513static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10514{
10515 unsigned long invalid_mask;
10516
10517 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10518 return (val & invalid_mask) == 0;
10519}
10520
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010521/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010522 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10523 * emulating VM entry into a guest with EPT enabled.
10524 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10525 * is assigned to entry_failure_code on failure.
10526 */
10527static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010528 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010529{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010530 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010531 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010532 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10533 return 1;
10534 }
10535
10536 /*
10537 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10538 * must not be dereferenced.
10539 */
10540 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10541 !nested_ept) {
10542 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10543 *entry_failure_code = ENTRY_FAIL_PDPTE;
10544 return 1;
10545 }
10546 }
10547
10548 vcpu->arch.cr3 = cr3;
10549 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10550 }
10551
10552 kvm_mmu_reset_context(vcpu);
10553 return 0;
10554}
10555
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010556static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10557 bool from_vmentry)
10558{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010559 struct vcpu_vmx *vmx = to_vmx(vcpu);
10560
10561 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10562 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10563 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10564 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10565 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10566 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10567 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10568 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10569 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10570 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10571 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10572 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10573 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10574 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10575 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10576 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10577 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10578 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10579 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10580 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10581 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10582 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10583 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10584 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10585 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10586 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10587 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10588 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10589 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10590 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10591 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010592
10593 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10594 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10595 vmcs12->guest_pending_dbg_exceptions);
10596 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10597 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10598
10599 if (nested_cpu_has_xsaves(vmcs12))
10600 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
10601 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10602
10603 if (cpu_has_vmx_posted_intr())
10604 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
10605
10606 /*
10607 * Whether page-faults are trapped is determined by a combination of
10608 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10609 * If enable_ept, L0 doesn't care about page faults and we should
10610 * set all of these to L1's desires. However, if !enable_ept, L0 does
10611 * care about (at least some) page faults, and because it is not easy
10612 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10613 * to exit on each and every L2 page fault. This is done by setting
10614 * MASK=MATCH=0 and (see below) EB.PF=1.
10615 * Note that below we don't need special code to set EB.PF beyond the
10616 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10617 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10618 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10619 */
10620 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10621 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10622 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10623 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10624
10625 /* All VMFUNCs are currently emulated through L0 vmexits. */
10626 if (cpu_has_vmx_vmfunc())
10627 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10628
10629 if (cpu_has_vmx_apicv()) {
10630 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
10631 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
10632 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
10633 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
10634 }
10635
10636 /*
10637 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10638 * Some constant fields are set here by vmx_set_constant_host_state().
10639 * Other fields are different per CPU, and will be set later when
10640 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10641 */
10642 vmx_set_constant_host_state(vmx);
10643
10644 /*
10645 * Set the MSR load/store lists to match L0's settings.
10646 */
10647 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10648 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10649 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10650 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10651 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10652
10653 set_cr4_guest_host_mask(vmx);
10654
10655 if (vmx_mpx_supported())
10656 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10657
10658 if (enable_vpid) {
10659 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
10660 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10661 else
10662 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10663 }
10664
10665 /*
10666 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10667 */
10668 if (enable_ept) {
10669 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10670 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10671 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10672 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10673 }
Radim Krčmář80132f42018-02-02 18:26:58 +010010674
10675 if (cpu_has_vmx_msr_bitmap())
10676 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010677}
10678
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010679/*
10680 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10681 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010682 * 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 +030010683 * guest in a way that will both be appropriate to L1's requests, and our
10684 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10685 * function also has additional necessary side-effects, like setting various
10686 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010010687 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10688 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010689 */
Ladi Prosekee146c12016-11-30 16:03:09 +010010690static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010691 bool from_vmentry, u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010692{
10693 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040010694 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010695
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010696 /*
10697 * First, the fields that are shadowed. This must be kept in sync
10698 * with vmx_shadow_fields.h.
10699 */
10700
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010701 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010702 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010703 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010704 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10705 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010706
10707 /*
10708 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
10709 * HOST_FS_BASE, HOST_GS_BASE.
10710 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010711
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010712 if (from_vmentry &&
10713 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020010714 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10715 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10716 } else {
10717 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10718 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10719 }
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010720 if (from_vmentry) {
10721 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10722 vmcs12->vm_entry_intr_info_field);
10723 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10724 vmcs12->vm_entry_exception_error_code);
10725 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10726 vmcs12->vm_entry_instruction_len);
10727 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10728 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070010729 vmx->loaded_vmcs->nmi_known_unmasked =
10730 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010731 } else {
10732 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10733 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030010734 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010735
Jan Kiszkaf41245002014-03-07 20:03:13 +010010736 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010737
Paolo Bonzini9314006db2016-07-06 13:23:51 +020010738 /* Preemption timer setting is only taken from vmcs01. */
10739 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10740 exec_control |= vmcs_config.pin_based_exec_ctrl;
10741 if (vmx->hv_deadline_tsc == -1)
10742 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10743
10744 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010745 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010746 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10747 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010748 } else {
Wincy Van705699a2015-02-03 23:58:17 +080010749 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010750 }
Wincy Van705699a2015-02-03 23:58:17 +080010751
Jan Kiszkaf41245002014-03-07 20:03:13 +010010752 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010753
Jan Kiszkaf41245002014-03-07 20:03:13 +010010754 vmx->nested.preemption_timer_expired = false;
10755 if (nested_cpu_has_preemption_timer(vmcs12))
10756 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010757
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010758 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020010759 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080010760
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010761 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010762 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020010763 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010764 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020010765 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010766 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040010767 SECONDARY_EXEC_APIC_REGISTER_VIRT |
10768 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010769 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040010770 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
10771 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
10772 ~SECONDARY_EXEC_ENABLE_PML;
10773 exec_control |= vmcs12_exec_ctrl;
10774 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010775
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010776 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080010777 vmcs_write16(GUEST_INTR_STATUS,
10778 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080010779
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010780 /*
10781 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10782 * nested_get_vmcs12_pages will either fix it up or
10783 * remove the VM execution control.
10784 */
10785 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
10786 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
10787
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010788 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10789 }
10790
Jim Mattson83bafef2016-10-04 10:48:38 -070010791 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010792 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10793 * entry, but only if the current (host) sp changed from the value
10794 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10795 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10796 * here we just force the write to happen on entry.
10797 */
10798 vmx->host_rsp = 0;
10799
10800 exec_control = vmx_exec_control(vmx); /* L0's desires */
10801 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10802 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10803 exec_control &= ~CPU_BASED_TPR_SHADOW;
10804 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010805
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010806 /*
10807 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
10808 * nested_get_vmcs12_pages can't fix it up, the illegal value
10809 * will result in a VM entry failure.
10810 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010811 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010812 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010813 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070010814 } else {
10815#ifdef CONFIG_X86_64
10816 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10817 CPU_BASED_CR8_STORE_EXITING;
10818#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010819 }
10820
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010821 /*
Quan Xu8eb73e2d2017-12-12 16:44:21 +080010822 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
10823 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010824 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010825 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10826 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10827
10828 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10829
10830 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10831 * bitwise-or of what L1 wants to trap for L2, and what we want to
10832 * trap. Note that CR0.TS also needs updating - we do this later.
10833 */
10834 update_exception_bitmap(vcpu);
10835 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10836 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10837
Nadav Har'El8049d652013-08-05 11:07:06 +030010838 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10839 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10840 * bits are further modified by vmx_set_efer() below.
10841 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010010842 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010843
10844 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10845 * emulated by vmx_set_efer(), below.
10846 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010847 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010848 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10849 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010850 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10851
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010852 if (from_vmentry &&
10853 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010854 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010855 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010856 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010857 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010858 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010859
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010860 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10861 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010862 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010863 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010864 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010865 if (kvm_has_tsc_control)
10866 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010867
10868 if (enable_vpid) {
10869 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010870 * There is no direct mapping between vpid02 and vpid12, the
10871 * vpid02 is per-vCPU for L0 and reused while the value of
10872 * vpid12 is changed w/ one invvpid during nested vmentry.
10873 * The vpid12 is allocated by L1 for L2, so it will not
10874 * influence global bitmap(for vpid01 and vpid02 allocation)
10875 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010876 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010877 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070010878 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10879 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080010880 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070010881 }
10882 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080010883 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070010884 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010885 }
10886
Ladi Prosek1fb883b2017-04-04 14:18:53 +020010887 if (enable_pml) {
10888 /*
10889 * Conceptually we want to copy the PML address and index from
10890 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10891 * since we always flush the log on each vmexit, this happens
10892 * to be equivalent to simply resetting the fields in vmcs02.
10893 */
10894 ASSERT(vmx->pml_pg);
10895 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10896 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10897 }
10898
Nadav Har'El155a97a2013-08-05 11:07:16 +030010899 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010900 if (nested_ept_init_mmu_context(vcpu)) {
10901 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10902 return 1;
10903 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010904 } else if (nested_cpu_has2(vmcs12,
10905 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10906 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010907 }
10908
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010909 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080010910 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
10911 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010912 * The CR0_READ_SHADOW is what L2 should have expected to read given
10913 * the specifications by L1; It's not enough to take
10914 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10915 * have more bits than L1 expected.
10916 */
10917 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10918 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10919
10920 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10921 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10922
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010923 if (from_vmentry &&
10924 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080010925 vcpu->arch.efer = vmcs12->guest_ia32_efer;
10926 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
10927 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10928 else
10929 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10930 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10931 vmx_set_efer(vcpu, vcpu->arch.efer);
10932
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010933 if (vmx->nested.dirty_vmcs12) {
10934 prepare_vmcs02_full(vcpu, vmcs12, from_vmentry);
10935 vmx->nested.dirty_vmcs12 = false;
10936 }
10937
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010938 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010010939 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010940 entry_failure_code))
10941 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010010942
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010943 if (!enable_ept)
10944 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10945
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010946 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10947 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010010948 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010949}
10950
Jim Mattsonca0bde22016-11-30 12:03:46 -080010951static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10952{
10953 struct vcpu_vmx *vmx = to_vmx(vcpu);
10954
10955 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10956 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
10957 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10958
Jim Mattson56a20512017-07-06 16:33:06 -070010959 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
10960 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10961
Jim Mattsonca0bde22016-11-30 12:03:46 -080010962 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
10963 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10964
Jim Mattson712b12d2017-08-24 13:24:47 -070010965 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
10966 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10967
Jim Mattsonca0bde22016-11-30 12:03:46 -080010968 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
10969 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10970
10971 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
10972 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10973
Bandan Dasc5f983f2017-05-05 15:25:14 -040010974 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
10975 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10976
Jim Mattsonca0bde22016-11-30 12:03:46 -080010977 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
10978 vmx->nested.nested_vmx_procbased_ctls_low,
10979 vmx->nested.nested_vmx_procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070010980 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
10981 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
10982 vmx->nested.nested_vmx_secondary_ctls_low,
10983 vmx->nested.nested_vmx_secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080010984 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
10985 vmx->nested.nested_vmx_pinbased_ctls_low,
10986 vmx->nested.nested_vmx_pinbased_ctls_high) ||
10987 !vmx_control_verify(vmcs12->vm_exit_controls,
10988 vmx->nested.nested_vmx_exit_ctls_low,
10989 vmx->nested.nested_vmx_exit_ctls_high) ||
10990 !vmx_control_verify(vmcs12->vm_entry_controls,
10991 vmx->nested.nested_vmx_entry_ctls_low,
10992 vmx->nested.nested_vmx_entry_ctls_high))
10993 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10994
Bandan Das41ab9372017-08-03 15:54:43 -040010995 if (nested_cpu_has_vmfunc(vmcs12)) {
10996 if (vmcs12->vm_function_control &
10997 ~vmx->nested.nested_vmx_vmfunc_controls)
10998 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10999
11000 if (nested_cpu_has_eptp_switching(vmcs12)) {
11001 if (!nested_cpu_has_ept(vmcs12) ||
11002 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11003 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11004 }
11005 }
Bandan Das27c42a12017-08-03 15:54:42 -040011006
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070011007 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11008 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11009
Jim Mattsonca0bde22016-11-30 12:03:46 -080011010 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11011 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11012 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11013 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11014
11015 return 0;
11016}
11017
11018static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11019 u32 *exit_qual)
11020{
11021 bool ia32e;
11022
11023 *exit_qual = ENTRY_FAIL_DEFAULT;
11024
11025 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11026 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11027 return 1;
11028
11029 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11030 vmcs12->vmcs_link_pointer != -1ull) {
11031 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11032 return 1;
11033 }
11034
11035 /*
11036 * If the load IA32_EFER VM-entry control is 1, the following checks
11037 * are performed on the field for the IA32_EFER MSR:
11038 * - Bits reserved in the IA32_EFER MSR must be 0.
11039 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11040 * the IA-32e mode guest VM-exit control. It must also be identical
11041 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11042 * CR0.PG) is 1.
11043 */
11044 if (to_vmx(vcpu)->nested.nested_run_pending &&
11045 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11046 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11047 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11048 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11049 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11050 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11051 return 1;
11052 }
11053
11054 /*
11055 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11056 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11057 * the values of the LMA and LME bits in the field must each be that of
11058 * the host address-space size VM-exit control.
11059 */
11060 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11061 ia32e = (vmcs12->vm_exit_controls &
11062 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11063 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11064 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11065 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11066 return 1;
11067 }
11068
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011069 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11070 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11071 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11072 return 1;
11073
Jim Mattsonca0bde22016-11-30 12:03:46 -080011074 return 0;
11075}
11076
Jim Mattson858e25c2016-11-30 12:03:47 -080011077static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
11078{
11079 struct vcpu_vmx *vmx = to_vmx(vcpu);
11080 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011081 u32 msr_entry_idx;
11082 u32 exit_qual;
11083
Jim Mattson858e25c2016-11-30 12:03:47 -080011084 enter_guest_mode(vcpu);
11085
11086 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11087 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11088
Jim Mattsonde3a0022017-11-27 17:22:25 -060011089 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011090 vmx_segment_cache_clear(vmx);
11091
11092 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
11093 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011094 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080011095 nested_vmx_entry_failure(vcpu, vmcs12,
11096 EXIT_REASON_INVALID_STATE, exit_qual);
11097 return 1;
11098 }
11099
11100 nested_get_vmcs12_pages(vcpu, vmcs12);
11101
11102 msr_entry_idx = nested_vmx_load_msr(vcpu,
11103 vmcs12->vm_entry_msr_load_addr,
11104 vmcs12->vm_entry_msr_load_count);
11105 if (msr_entry_idx) {
11106 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011107 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080011108 nested_vmx_entry_failure(vcpu, vmcs12,
11109 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
11110 return 1;
11111 }
11112
Jim Mattson858e25c2016-11-30 12:03:47 -080011113 /*
11114 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11115 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11116 * returned as far as L1 is concerned. It will only return (and set
11117 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11118 */
11119 return 0;
11120}
11121
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011122/*
11123 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11124 * for running an L2 nested guest.
11125 */
11126static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11127{
11128 struct vmcs12 *vmcs12;
11129 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011130 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011131 u32 exit_qual;
11132 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011133
Kyle Hueyeb277562016-11-29 12:40:39 -080011134 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011135 return 1;
11136
Kyle Hueyeb277562016-11-29 12:40:39 -080011137 if (!nested_vmx_check_vmcs12(vcpu))
11138 goto out;
11139
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011140 vmcs12 = get_vmcs12(vcpu);
11141
Abel Gordon012f83c2013-04-18 14:39:25 +030011142 if (enable_shadow_vmcs)
11143 copy_shadow_to_vmcs12(vmx);
11144
Nadav Har'El7c177932011-05-25 23:12:04 +030011145 /*
11146 * The nested entry process starts with enforcing various prerequisites
11147 * on vmcs12 as required by the Intel SDM, and act appropriately when
11148 * they fail: As the SDM explains, some conditions should cause the
11149 * instruction to fail, while others will cause the instruction to seem
11150 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11151 * To speed up the normal (success) code path, we should avoid checking
11152 * for misconfigurations which will anyway be caught by the processor
11153 * when using the merged vmcs02.
11154 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011155 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11156 nested_vmx_failValid(vcpu,
11157 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11158 goto out;
11159 }
11160
Nadav Har'El7c177932011-05-25 23:12:04 +030011161 if (vmcs12->launch_state == launch) {
11162 nested_vmx_failValid(vcpu,
11163 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11164 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011165 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011166 }
11167
Jim Mattsonca0bde22016-11-30 12:03:46 -080011168 ret = check_vmentry_prereqs(vcpu, vmcs12);
11169 if (ret) {
11170 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011171 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011172 }
11173
Nadav Har'El7c177932011-05-25 23:12:04 +030011174 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011175 * After this point, the trap flag no longer triggers a singlestep trap
11176 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11177 * This is not 100% correct; for performance reasons, we delegate most
11178 * of the checks on host state to the processor. If those fail,
11179 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011180 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011181 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011182
Jim Mattsonca0bde22016-11-30 12:03:46 -080011183 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11184 if (ret) {
11185 nested_vmx_entry_failure(vcpu, vmcs12,
11186 EXIT_REASON_INVALID_STATE, exit_qual);
11187 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011188 }
11189
11190 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011191 * We're finally done with prerequisite checking, and can start with
11192 * the nested entry.
11193 */
11194
Jim Mattson858e25c2016-11-30 12:03:47 -080011195 ret = enter_vmx_non_root_mode(vcpu, true);
11196 if (ret)
11197 return ret;
Wincy Vanff651cb2014-12-11 08:52:58 +030011198
Jan Kiszka6dfacad2013-12-04 08:58:54 +010011199 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060011200 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010011201
Jan Kiszka7af40ad32014-01-04 18:47:23 +010011202 vmx->nested.nested_run_pending = 1;
11203
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011204 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011205
11206out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011207 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011208}
11209
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011210/*
11211 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11212 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11213 * This function returns the new value we should put in vmcs12.guest_cr0.
11214 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11215 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11216 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11217 * didn't trap the bit, because if L1 did, so would L0).
11218 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11219 * been modified by L2, and L1 knows it. So just leave the old value of
11220 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11221 * isn't relevant, because if L0 traps this bit it can set it to anything.
11222 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11223 * changed these bits, and therefore they need to be updated, but L0
11224 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11225 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11226 */
11227static inline unsigned long
11228vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11229{
11230 return
11231 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11232 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11233 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11234 vcpu->arch.cr0_guest_owned_bits));
11235}
11236
11237static inline unsigned long
11238vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11239{
11240 return
11241 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11242 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11243 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11244 vcpu->arch.cr4_guest_owned_bits));
11245}
11246
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011247static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11248 struct vmcs12 *vmcs12)
11249{
11250 u32 idt_vectoring;
11251 unsigned int nr;
11252
Wanpeng Li664f8e22017-08-24 03:35:09 -070011253 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011254 nr = vcpu->arch.exception.nr;
11255 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11256
11257 if (kvm_exception_is_soft(nr)) {
11258 vmcs12->vm_exit_instruction_len =
11259 vcpu->arch.event_exit_inst_len;
11260 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11261 } else
11262 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11263
11264 if (vcpu->arch.exception.has_error_code) {
11265 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11266 vmcs12->idt_vectoring_error_code =
11267 vcpu->arch.exception.error_code;
11268 }
11269
11270 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011271 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011272 vmcs12->idt_vectoring_info_field =
11273 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
11274 } else if (vcpu->arch.interrupt.pending) {
11275 nr = vcpu->arch.interrupt.nr;
11276 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11277
11278 if (vcpu->arch.interrupt.soft) {
11279 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11280 vmcs12->vm_entry_instruction_len =
11281 vcpu->arch.event_exit_inst_len;
11282 } else
11283 idt_vectoring |= INTR_TYPE_EXT_INTR;
11284
11285 vmcs12->idt_vectoring_info_field = idt_vectoring;
11286 }
11287}
11288
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011289static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11290{
11291 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011292 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020011293 bool block_nested_events =
11294 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011295
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011296 if (vcpu->arch.exception.pending &&
11297 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020011298 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011299 return -EBUSY;
11300 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011301 return 0;
11302 }
11303
Jan Kiszkaf41245002014-03-07 20:03:13 +010011304 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11305 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020011306 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010011307 return -EBUSY;
11308 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11309 return 0;
11310 }
11311
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011312 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011313 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011314 return -EBUSY;
11315 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11316 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11317 INTR_INFO_VALID_MASK, 0);
11318 /*
11319 * The NMI-triggered VM exit counts as injection:
11320 * clear this one and block further NMIs.
11321 */
11322 vcpu->arch.nmi_pending = 0;
11323 vmx_set_nmi_mask(vcpu, true);
11324 return 0;
11325 }
11326
11327 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11328 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011329 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011330 return -EBUSY;
11331 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080011332 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011333 }
11334
David Hildenbrand6342c502017-01-25 11:58:58 +010011335 vmx_complete_nested_posted_interrupt(vcpu);
11336 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011337}
11338
Jan Kiszkaf41245002014-03-07 20:03:13 +010011339static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11340{
11341 ktime_t remaining =
11342 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11343 u64 value;
11344
11345 if (ktime_to_ns(remaining) <= 0)
11346 return 0;
11347
11348 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11349 do_div(value, 1000000);
11350 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11351}
11352
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011353/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011354 * Update the guest state fields of vmcs12 to reflect changes that
11355 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11356 * VM-entry controls is also updated, since this is really a guest
11357 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011358 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011359static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011360{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011361 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11362 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11363
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011364 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11365 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11366 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11367
11368 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11369 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11370 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11371 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11372 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11373 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11374 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11375 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11376 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11377 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11378 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11379 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11380 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11381 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11382 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11383 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11384 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11385 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11386 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11387 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11388 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11389 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11390 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11391 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11392 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11393 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11394 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11395 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11396 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11397 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11398 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11399 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11400 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11401 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11402 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11403 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11404
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011405 vmcs12->guest_interruptibility_info =
11406 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11407 vmcs12->guest_pending_dbg_exceptions =
11408 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010011409 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11410 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11411 else
11412 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011413
Jan Kiszkaf41245002014-03-07 20:03:13 +010011414 if (nested_cpu_has_preemption_timer(vmcs12)) {
11415 if (vmcs12->vm_exit_controls &
11416 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11417 vmcs12->vmx_preemption_timer_value =
11418 vmx_get_preemption_timer_value(vcpu);
11419 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11420 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080011421
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011422 /*
11423 * In some cases (usually, nested EPT), L2 is allowed to change its
11424 * own CR3 without exiting. If it has changed it, we must keep it.
11425 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11426 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11427 *
11428 * Additionally, restore L2's PDPTR to vmcs12.
11429 */
11430 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010011431 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011432 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11433 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11434 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11435 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11436 }
11437
Jim Mattsond281e132017-06-01 12:44:46 -070011438 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011439
Wincy Van608406e2015-02-03 23:57:51 +080011440 if (nested_cpu_has_vid(vmcs12))
11441 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11442
Jan Kiszkac18911a2013-03-13 16:06:41 +010011443 vmcs12->vm_entry_controls =
11444 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011445 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011446
Jan Kiszka2996fca2014-06-16 13:59:43 +020011447 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11448 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11449 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11450 }
11451
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011452 /* TODO: These cannot have changed unless we have MSR bitmaps and
11453 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011454 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011455 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011456 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11457 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011458 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11459 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11460 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011461 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011462 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011463}
11464
11465/*
11466 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11467 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11468 * and this function updates it to reflect the changes to the guest state while
11469 * L2 was running (and perhaps made some exits which were handled directly by L0
11470 * without going back to L1), and to reflect the exit reason.
11471 * Note that we do not have to copy here all VMCS fields, just those that
11472 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11473 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11474 * which already writes to vmcs12 directly.
11475 */
11476static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11477 u32 exit_reason, u32 exit_intr_info,
11478 unsigned long exit_qualification)
11479{
11480 /* update guest state fields: */
11481 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011482
11483 /* update exit information fields: */
11484
Jan Kiszka533558b2014-01-04 18:47:20 +010011485 vmcs12->vm_exit_reason = exit_reason;
11486 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010011487 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020011488
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011489 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011490 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11491 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11492
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011493 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070011494 vmcs12->launch_state = 1;
11495
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011496 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11497 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011498 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011499
11500 /*
11501 * Transfer the event that L0 or L1 may wanted to inject into
11502 * L2 to IDT_VECTORING_INFO_FIELD.
11503 */
11504 vmcs12_save_pending_event(vcpu, vmcs12);
11505 }
11506
11507 /*
11508 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11509 * preserved above and would only end up incorrectly in L1.
11510 */
11511 vcpu->arch.nmi_injected = false;
11512 kvm_clear_exception_queue(vcpu);
11513 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011514}
11515
Wanpeng Li5af41572017-11-05 16:54:49 -080011516static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11517 struct vmcs12 *vmcs12)
11518{
11519 u32 entry_failure_code;
11520
11521 nested_ept_uninit_mmu_context(vcpu);
11522
11523 /*
11524 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11525 * couldn't have changed.
11526 */
11527 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11528 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11529
11530 if (!enable_ept)
11531 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11532}
11533
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011534/*
11535 * A part of what we need to when the nested L2 guest exits and we want to
11536 * run its L1 parent, is to reset L1's guest state to the host state specified
11537 * in vmcs12.
11538 * This function is to be called not only on normal nested exit, but also on
11539 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11540 * Failures During or After Loading Guest State").
11541 * This function should be called when the active VMCS is L1's (vmcs01).
11542 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011543static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11544 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011545{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011546 struct kvm_segment seg;
11547
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011548 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11549 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011550 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011551 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11552 else
11553 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11554 vmx_set_efer(vcpu, vcpu->arch.efer);
11555
11556 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11557 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011558 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011559 /*
11560 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011561 * actually changed, because vmx_set_cr0 refers to efer set above.
11562 *
11563 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11564 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011565 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011566 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020011567 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011568
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011569 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011570 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080011571 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011572
Wanpeng Li5af41572017-11-05 16:54:49 -080011573 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011574
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011575 if (enable_vpid) {
11576 /*
11577 * Trivially support vpid by letting L2s share their parent
11578 * L1's vpid. TODO: move to a more elaborate solution, giving
11579 * each L2 its own vpid and exposing the vpid feature to L1.
11580 */
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011581 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011582 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011583
11584 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11585 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11586 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11587 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11588 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020011589 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11590 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011591
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011592 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11593 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11594 vmcs_write64(GUEST_BNDCFGS, 0);
11595
Jan Kiszka44811c02013-08-04 17:17:27 +020011596 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011597 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011598 vcpu->arch.pat = vmcs12->host_ia32_pat;
11599 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011600 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11601 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11602 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011603
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011604 /* Set L1 segment info according to Intel SDM
11605 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11606 seg = (struct kvm_segment) {
11607 .base = 0,
11608 .limit = 0xFFFFFFFF,
11609 .selector = vmcs12->host_cs_selector,
11610 .type = 11,
11611 .present = 1,
11612 .s = 1,
11613 .g = 1
11614 };
11615 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11616 seg.l = 1;
11617 else
11618 seg.db = 1;
11619 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11620 seg = (struct kvm_segment) {
11621 .base = 0,
11622 .limit = 0xFFFFFFFF,
11623 .type = 3,
11624 .present = 1,
11625 .s = 1,
11626 .db = 1,
11627 .g = 1
11628 };
11629 seg.selector = vmcs12->host_ds_selector;
11630 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11631 seg.selector = vmcs12->host_es_selector;
11632 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11633 seg.selector = vmcs12->host_ss_selector;
11634 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11635 seg.selector = vmcs12->host_fs_selector;
11636 seg.base = vmcs12->host_fs_base;
11637 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11638 seg.selector = vmcs12->host_gs_selector;
11639 seg.base = vmcs12->host_gs_base;
11640 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11641 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011642 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011643 .limit = 0x67,
11644 .selector = vmcs12->host_tr_selector,
11645 .type = 11,
11646 .present = 1
11647 };
11648 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11649
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011650 kvm_set_dr(vcpu, 7, 0x400);
11651 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011652
Wincy Van3af18d92015-02-03 23:49:31 +080011653 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011654 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080011655
Wincy Vanff651cb2014-12-11 08:52:58 +030011656 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11657 vmcs12->vm_exit_msr_load_count))
11658 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011659}
11660
11661/*
11662 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11663 * and modify vmcs12 to make it see what it would expect to see there if
11664 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11665 */
Jan Kiszka533558b2014-01-04 18:47:20 +010011666static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11667 u32 exit_intr_info,
11668 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011669{
11670 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011671 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11672
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011673 /* trying to cancel vmlaunch/vmresume is a bug */
11674 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11675
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011676 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070011677 * The only expected VM-instruction error is "VM entry with
11678 * invalid control field(s)." Anything else indicates a
11679 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011680 */
Jim Mattson4f350c62017-09-14 16:31:44 -070011681 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
11682 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
11683
11684 leave_guest_mode(vcpu);
11685
11686 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011687 if (exit_reason == -1)
11688 sync_vmcs12(vcpu, vmcs12);
11689 else
11690 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11691 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070011692
11693 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11694 vmcs12->vm_exit_msr_store_count))
11695 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040011696 }
11697
Jim Mattson4f350c62017-09-14 16:31:44 -070011698 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020011699 vm_entry_controls_reset_shadow(vmx);
11700 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010011701 vmx_segment_cache_clear(vmx);
11702
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011703 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070011704 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11705 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011706 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011707 if (vmx->hv_deadline_tsc == -1)
11708 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11709 PIN_BASED_VMX_PREEMPTION_TIMER);
11710 else
11711 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11712 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070011713 if (kvm_has_tsc_control)
11714 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011715
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011716 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11717 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11718 vmx_set_virtual_x2apic_mode(vcpu,
11719 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011720 } else if (!nested_cpu_has_ept(vmcs12) &&
11721 nested_cpu_has2(vmcs12,
11722 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11723 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011724 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011725
11726 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11727 vmx->host_rsp = 0;
11728
11729 /* Unpin physical memory we referred to in vmcs02 */
11730 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011731 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011732 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011733 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011734 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011735 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011736 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011737 }
Wincy Van705699a2015-02-03 23:58:17 +080011738 if (vmx->nested.pi_desc_page) {
11739 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011740 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011741 vmx->nested.pi_desc_page = NULL;
11742 vmx->nested.pi_desc = NULL;
11743 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011744
11745 /*
Tang Chen38b99172014-09-24 15:57:54 +080011746 * We are now running in L2, mmu_notifier will force to reload the
11747 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11748 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011749 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011750
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011751 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030011752 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011753
11754 /* in case we halted in L2 */
11755 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070011756
11757 if (likely(!vmx->fail)) {
11758 /*
11759 * TODO: SDM says that with acknowledge interrupt on
11760 * exit, bit 31 of the VM-exit interrupt information
11761 * (valid interrupt) is always set to 1 on
11762 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
11763 * need kvm_cpu_has_interrupt(). See the commit
11764 * message for details.
11765 */
11766 if (nested_exit_intr_ack_set(vcpu) &&
11767 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
11768 kvm_cpu_has_interrupt(vcpu)) {
11769 int irq = kvm_cpu_get_interrupt(vcpu);
11770 WARN_ON(irq < 0);
11771 vmcs12->vm_exit_intr_info = irq |
11772 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11773 }
11774
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011775 if (exit_reason != -1)
11776 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11777 vmcs12->exit_qualification,
11778 vmcs12->idt_vectoring_info_field,
11779 vmcs12->vm_exit_intr_info,
11780 vmcs12->vm_exit_intr_error_code,
11781 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070011782
11783 load_vmcs12_host_state(vcpu, vmcs12);
11784
11785 return;
11786 }
11787
11788 /*
11789 * After an early L2 VM-entry failure, we're now back
11790 * in L1 which thinks it just finished a VMLAUNCH or
11791 * VMRESUME instruction, so we need to set the failure
11792 * flag and the VM-instruction error field of the VMCS
11793 * accordingly.
11794 */
11795 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080011796
11797 load_vmcs12_mmu_host_state(vcpu, vmcs12);
11798
Jim Mattson4f350c62017-09-14 16:31:44 -070011799 /*
11800 * The emulated instruction was already skipped in
11801 * nested_vmx_run, but the updated RIP was never
11802 * written back to the vmcs01.
11803 */
11804 skip_emulated_instruction(vcpu);
11805 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011806}
11807
Nadav Har'El7c177932011-05-25 23:12:04 +030011808/*
Jan Kiszka42124922014-01-04 18:47:19 +010011809 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11810 */
11811static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11812{
Wanpeng Li2f707d92017-03-06 04:03:28 -080011813 if (is_guest_mode(vcpu)) {
11814 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011815 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080011816 }
Jan Kiszka42124922014-01-04 18:47:19 +010011817 free_nested(to_vmx(vcpu));
11818}
11819
11820/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011821 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11822 * 23.7 "VM-entry failures during or after loading guest state" (this also
11823 * lists the acceptable exit-reason and exit-qualification parameters).
11824 * It should only be called before L2 actually succeeded to run, and when
11825 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11826 */
11827static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11828 struct vmcs12 *vmcs12,
11829 u32 reason, unsigned long qualification)
11830{
11831 load_vmcs12_host_state(vcpu, vmcs12);
11832 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11833 vmcs12->exit_qualification = qualification;
11834 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011835 if (enable_shadow_vmcs)
11836 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011837}
11838
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011839static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11840 struct x86_instruction_info *info,
11841 enum x86_intercept_stage stage)
11842{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020011843 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11844 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
11845
11846 /*
11847 * RDPID causes #UD if disabled through secondary execution controls.
11848 * Because it is marked as EmulateOnUD, we need to intercept it here.
11849 */
11850 if (info->intercept == x86_intercept_rdtscp &&
11851 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
11852 ctxt->exception.vector = UD_VECTOR;
11853 ctxt->exception.error_code_valid = false;
11854 return X86EMUL_PROPAGATE_FAULT;
11855 }
11856
11857 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011858 return X86EMUL_CONTINUE;
11859}
11860
Yunhong Jiang64672c92016-06-13 14:19:59 -070011861#ifdef CONFIG_X86_64
11862/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11863static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11864 u64 divisor, u64 *result)
11865{
11866 u64 low = a << shift, high = a >> (64 - shift);
11867
11868 /* To avoid the overflow on divq */
11869 if (high >= divisor)
11870 return 1;
11871
11872 /* Low hold the result, high hold rem which is discarded */
11873 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11874 "rm" (divisor), "0" (low), "1" (high));
11875 *result = low;
11876
11877 return 0;
11878}
11879
11880static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11881{
11882 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011883 u64 tscl = rdtsc();
11884 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11885 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011886
11887 /* Convert to host delta tsc if tsc scaling is enabled */
11888 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11889 u64_shl_div_u64(delta_tsc,
11890 kvm_tsc_scaling_ratio_frac_bits,
11891 vcpu->arch.tsc_scaling_ratio,
11892 &delta_tsc))
11893 return -ERANGE;
11894
11895 /*
11896 * If the delta tsc can't fit in the 32 bit after the multi shift,
11897 * we can't use the preemption timer.
11898 * It's possible that it fits on later vmentries, but checking
11899 * on every vmentry is costly so we just use an hrtimer.
11900 */
11901 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11902 return -ERANGE;
11903
11904 vmx->hv_deadline_tsc = tscl + delta_tsc;
11905 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11906 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070011907
11908 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011909}
11910
11911static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11912{
11913 struct vcpu_vmx *vmx = to_vmx(vcpu);
11914 vmx->hv_deadline_tsc = -1;
11915 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11916 PIN_BASED_VMX_PREEMPTION_TIMER);
11917}
11918#endif
11919
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011920static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011921{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011922 if (ple_gap)
11923 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011924}
11925
Kai Huang843e4332015-01-28 10:54:28 +080011926static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11927 struct kvm_memory_slot *slot)
11928{
11929 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11930 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11931}
11932
11933static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11934 struct kvm_memory_slot *slot)
11935{
11936 kvm_mmu_slot_set_dirty(kvm, slot);
11937}
11938
11939static void vmx_flush_log_dirty(struct kvm *kvm)
11940{
11941 kvm_flush_pml_buffers(kvm);
11942}
11943
Bandan Dasc5f983f2017-05-05 15:25:14 -040011944static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
11945{
11946 struct vmcs12 *vmcs12;
11947 struct vcpu_vmx *vmx = to_vmx(vcpu);
11948 gpa_t gpa;
11949 struct page *page = NULL;
11950 u64 *pml_address;
11951
11952 if (is_guest_mode(vcpu)) {
11953 WARN_ON_ONCE(vmx->nested.pml_full);
11954
11955 /*
11956 * Check if PML is enabled for the nested guest.
11957 * Whether eptp bit 6 is set is already checked
11958 * as part of A/D emulation.
11959 */
11960 vmcs12 = get_vmcs12(vcpu);
11961 if (!nested_cpu_has_pml(vmcs12))
11962 return 0;
11963
Dan Carpenter47698862017-05-10 22:43:17 +030011964 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040011965 vmx->nested.pml_full = true;
11966 return 1;
11967 }
11968
11969 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
11970
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011971 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
11972 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040011973 return 0;
11974
11975 pml_address = kmap(page);
11976 pml_address[vmcs12->guest_pml_index--] = gpa;
11977 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011978 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011979 }
11980
11981 return 0;
11982}
11983
Kai Huang843e4332015-01-28 10:54:28 +080011984static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11985 struct kvm_memory_slot *memslot,
11986 gfn_t offset, unsigned long mask)
11987{
11988 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11989}
11990
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011991static void __pi_post_block(struct kvm_vcpu *vcpu)
11992{
11993 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11994 struct pi_desc old, new;
11995 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011996
11997 do {
11998 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011999 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12000 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012001
12002 dest = cpu_physical_id(vcpu->cpu);
12003
12004 if (x2apic_enabled())
12005 new.ndst = dest;
12006 else
12007 new.ndst = (dest << 8) & 0xFF00;
12008
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012009 /* set 'NV' to 'notification vector' */
12010 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012011 } while (cmpxchg64(&pi_desc->control, old.control,
12012 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012013
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012014 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12015 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012016 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012017 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012018 vcpu->pre_pcpu = -1;
12019 }
12020}
12021
Feng Wuefc64402015-09-18 22:29:51 +080012022/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012023 * This routine does the following things for vCPU which is going
12024 * to be blocked if VT-d PI is enabled.
12025 * - Store the vCPU to the wakeup list, so when interrupts happen
12026 * we can find the right vCPU to wake up.
12027 * - Change the Posted-interrupt descriptor as below:
12028 * 'NDST' <-- vcpu->pre_pcpu
12029 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12030 * - If 'ON' is set during this process, which means at least one
12031 * interrupt is posted for this vCPU, we cannot block it, in
12032 * this case, return 1, otherwise, return 0.
12033 *
12034 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012035static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012036{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012037 unsigned int dest;
12038 struct pi_desc old, new;
12039 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12040
12041 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012042 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12043 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012044 return 0;
12045
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012046 WARN_ON(irqs_disabled());
12047 local_irq_disable();
12048 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12049 vcpu->pre_pcpu = vcpu->cpu;
12050 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12051 list_add_tail(&vcpu->blocked_vcpu_list,
12052 &per_cpu(blocked_vcpu_on_cpu,
12053 vcpu->pre_pcpu));
12054 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12055 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012056
12057 do {
12058 old.control = new.control = pi_desc->control;
12059
Feng Wubf9f6ac2015-09-18 22:29:55 +080012060 WARN((pi_desc->sn == 1),
12061 "Warning: SN field of posted-interrupts "
12062 "is set before blocking\n");
12063
12064 /*
12065 * Since vCPU can be preempted during this process,
12066 * vcpu->cpu could be different with pre_pcpu, we
12067 * need to set pre_pcpu as the destination of wakeup
12068 * notification event, then we can find the right vCPU
12069 * to wakeup in wakeup handler if interrupts happen
12070 * when the vCPU is in blocked state.
12071 */
12072 dest = cpu_physical_id(vcpu->pre_pcpu);
12073
12074 if (x2apic_enabled())
12075 new.ndst = dest;
12076 else
12077 new.ndst = (dest << 8) & 0xFF00;
12078
12079 /* set 'NV' to 'wakeup vector' */
12080 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012081 } while (cmpxchg64(&pi_desc->control, old.control,
12082 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012083
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012084 /* We should not block the vCPU if an interrupt is posted for it. */
12085 if (pi_test_on(pi_desc) == 1)
12086 __pi_post_block(vcpu);
12087
12088 local_irq_enable();
12089 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012090}
12091
Yunhong Jiangbc225122016-06-13 14:19:58 -070012092static int vmx_pre_block(struct kvm_vcpu *vcpu)
12093{
12094 if (pi_pre_block(vcpu))
12095 return 1;
12096
Yunhong Jiang64672c92016-06-13 14:19:59 -070012097 if (kvm_lapic_hv_timer_in_use(vcpu))
12098 kvm_lapic_switch_to_sw_timer(vcpu);
12099
Yunhong Jiangbc225122016-06-13 14:19:58 -070012100 return 0;
12101}
12102
12103static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012104{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012105 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012106 return;
12107
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012108 WARN_ON(irqs_disabled());
12109 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012110 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012111 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012112}
12113
Yunhong Jiangbc225122016-06-13 14:19:58 -070012114static void vmx_post_block(struct kvm_vcpu *vcpu)
12115{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012116 if (kvm_x86_ops->set_hv_timer)
12117 kvm_lapic_switch_to_hv_timer(vcpu);
12118
Yunhong Jiangbc225122016-06-13 14:19:58 -070012119 pi_post_block(vcpu);
12120}
12121
Feng Wubf9f6ac2015-09-18 22:29:55 +080012122/*
Feng Wuefc64402015-09-18 22:29:51 +080012123 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12124 *
12125 * @kvm: kvm
12126 * @host_irq: host irq of the interrupt
12127 * @guest_irq: gsi of the interrupt
12128 * @set: set or unset PI
12129 * returns 0 on success, < 0 on failure
12130 */
12131static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12132 uint32_t guest_irq, bool set)
12133{
12134 struct kvm_kernel_irq_routing_entry *e;
12135 struct kvm_irq_routing_table *irq_rt;
12136 struct kvm_lapic_irq irq;
12137 struct kvm_vcpu *vcpu;
12138 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012139 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012140
12141 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012142 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12143 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012144 return 0;
12145
12146 idx = srcu_read_lock(&kvm->irq_srcu);
12147 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012148 if (guest_irq >= irq_rt->nr_rt_entries ||
12149 hlist_empty(&irq_rt->map[guest_irq])) {
12150 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12151 guest_irq, irq_rt->nr_rt_entries);
12152 goto out;
12153 }
Feng Wuefc64402015-09-18 22:29:51 +080012154
12155 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12156 if (e->type != KVM_IRQ_ROUTING_MSI)
12157 continue;
12158 /*
12159 * VT-d PI cannot support posting multicast/broadcast
12160 * interrupts to a vCPU, we still use interrupt remapping
12161 * for these kind of interrupts.
12162 *
12163 * For lowest-priority interrupts, we only support
12164 * those with single CPU as the destination, e.g. user
12165 * configures the interrupts via /proc/irq or uses
12166 * irqbalance to make the interrupts single-CPU.
12167 *
12168 * We will support full lowest-priority interrupt later.
12169 */
12170
Radim Krčmář371313132016-07-12 22:09:27 +020012171 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012172 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12173 /*
12174 * Make sure the IRTE is in remapped mode if
12175 * we don't handle it in posted mode.
12176 */
12177 ret = irq_set_vcpu_affinity(host_irq, NULL);
12178 if (ret < 0) {
12179 printk(KERN_INFO
12180 "failed to back to remapped mode, irq: %u\n",
12181 host_irq);
12182 goto out;
12183 }
12184
Feng Wuefc64402015-09-18 22:29:51 +080012185 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012186 }
Feng Wuefc64402015-09-18 22:29:51 +080012187
12188 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12189 vcpu_info.vector = irq.vector;
12190
Feng Wub6ce9782016-01-25 16:53:35 +080012191 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012192 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12193
12194 if (set)
12195 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080012196 else
Feng Wuefc64402015-09-18 22:29:51 +080012197 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012198
12199 if (ret < 0) {
12200 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12201 __func__);
12202 goto out;
12203 }
12204 }
12205
12206 ret = 0;
12207out:
12208 srcu_read_unlock(&kvm->irq_srcu, idx);
12209 return ret;
12210}
12211
Ashok Rajc45dcc72016-06-22 14:59:56 +080012212static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12213{
12214 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12215 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12216 FEATURE_CONTROL_LMCE;
12217 else
12218 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12219 ~FEATURE_CONTROL_LMCE;
12220}
12221
Ladi Prosek72d7b372017-10-11 16:54:41 +020012222static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12223{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012224 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12225 if (to_vmx(vcpu)->nested.nested_run_pending)
12226 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012227 return 1;
12228}
12229
Ladi Prosek0234bf82017-10-11 16:54:40 +020012230static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12231{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012232 struct vcpu_vmx *vmx = to_vmx(vcpu);
12233
12234 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12235 if (vmx->nested.smm.guest_mode)
12236 nested_vmx_vmexit(vcpu, -1, 0, 0);
12237
12238 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12239 vmx->nested.vmxon = false;
Ladi Prosek0234bf82017-10-11 16:54:40 +020012240 return 0;
12241}
12242
12243static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12244{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012245 struct vcpu_vmx *vmx = to_vmx(vcpu);
12246 int ret;
12247
12248 if (vmx->nested.smm.vmxon) {
12249 vmx->nested.vmxon = true;
12250 vmx->nested.smm.vmxon = false;
12251 }
12252
12253 if (vmx->nested.smm.guest_mode) {
12254 vcpu->arch.hflags &= ~HF_SMM_MASK;
12255 ret = enter_vmx_non_root_mode(vcpu, false);
12256 vcpu->arch.hflags |= HF_SMM_MASK;
12257 if (ret)
12258 return ret;
12259
12260 vmx->nested.smm.guest_mode = false;
12261 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020012262 return 0;
12263}
12264
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012265static int enable_smi_window(struct kvm_vcpu *vcpu)
12266{
12267 return 0;
12268}
12269
Kees Cook404f6aa2016-08-08 16:29:06 -070012270static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080012271 .cpu_has_kvm_support = cpu_has_kvm_support,
12272 .disabled_by_bios = vmx_disabled_by_bios,
12273 .hardware_setup = hardware_setup,
12274 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030012275 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012276 .hardware_enable = hardware_enable,
12277 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080012278 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020012279 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012280
12281 .vcpu_create = vmx_create_vcpu,
12282 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030012283 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012284
Avi Kivity04d2cc72007-09-10 18:10:54 +030012285 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012286 .vcpu_load = vmx_vcpu_load,
12287 .vcpu_put = vmx_vcpu_put,
12288
Paolo Bonzinia96036b2015-11-10 11:55:36 +010012289 .update_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012290 .get_msr = vmx_get_msr,
12291 .set_msr = vmx_set_msr,
12292 .get_segment_base = vmx_get_segment_base,
12293 .get_segment = vmx_get_segment,
12294 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020012295 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012296 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020012297 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020012298 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030012299 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012300 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012301 .set_cr3 = vmx_set_cr3,
12302 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012303 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012304 .get_idt = vmx_get_idt,
12305 .set_idt = vmx_set_idt,
12306 .get_gdt = vmx_get_gdt,
12307 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010012308 .get_dr6 = vmx_get_dr6,
12309 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030012310 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010012311 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030012312 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012313 .get_rflags = vmx_get_rflags,
12314 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080012315
Avi Kivity6aa8b732006-12-10 02:21:36 -080012316 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012317
Avi Kivity6aa8b732006-12-10 02:21:36 -080012318 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020012319 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012320 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040012321 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12322 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020012323 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030012324 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012325 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020012326 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030012327 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020012328 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012329 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010012330 .get_nmi_mask = vmx_get_nmi_mask,
12331 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012332 .enable_nmi_window = enable_nmi_window,
12333 .enable_irq_window = enable_irq_window,
12334 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080012335 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080012336 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030012337 .get_enable_apicv = vmx_get_enable_apicv,
12338 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012339 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010012340 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012341 .hwapic_irr_update = vmx_hwapic_irr_update,
12342 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080012343 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12344 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012345
Izik Eiduscbc94022007-10-25 00:29:55 +020012346 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080012347 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080012348 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030012349
Avi Kivity586f9602010-11-18 13:09:54 +020012350 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020012351
Sheng Yang17cc3932010-01-05 19:02:27 +080012352 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080012353
12354 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080012355
12356 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000012357 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020012358
12359 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080012360
12361 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012362
12363 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020012364
12365 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012366
12367 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080012368 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000012369 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080012370 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020012371 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012372
12373 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012374
12375 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080012376
12377 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12378 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12379 .flush_log_dirty = vmx_flush_log_dirty,
12380 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040012381 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020012382
Feng Wubf9f6ac2015-09-18 22:29:55 +080012383 .pre_block = vmx_pre_block,
12384 .post_block = vmx_post_block,
12385
Wei Huang25462f72015-06-19 15:45:05 +020012386 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080012387
12388 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070012389
12390#ifdef CONFIG_X86_64
12391 .set_hv_timer = vmx_set_hv_timer,
12392 .cancel_hv_timer = vmx_cancel_hv_timer,
12393#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080012394
12395 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012396
Ladi Prosek72d7b372017-10-11 16:54:41 +020012397 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012398 .pre_enter_smm = vmx_pre_enter_smm,
12399 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012400 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012401};
12402
12403static int __init vmx_init(void)
12404{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012405 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
12406 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030012407 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012408 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080012409
Dave Young2965faa2015-09-09 15:38:55 -070012410#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012411 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12412 crash_vmclear_local_loaded_vmcss);
12413#endif
12414
He, Qingfdef3ad2007-04-30 09:45:24 +030012415 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080012416}
12417
12418static void __exit vmx_exit(void)
12419{
Dave Young2965faa2015-09-09 15:38:55 -070012420#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053012421 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012422 synchronize_rcu();
12423#endif
12424
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080012425 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080012426}
12427
12428module_init(vmx_init)
12429module_exit(vmx_exit)